Skip to content

Commit

Permalink
simplify for cloudflare workers (#1)
Browse files Browse the repository at this point in the history
- Fork from ulidx
- Remove dependencies
- Remove PRNG detection
- Web Crypto API only
  • Loading branch information
ryan-mars authored Nov 11, 2021
1 parent 72aeb4a commit 935352a
Show file tree
Hide file tree
Showing 9 changed files with 5,957 additions and 2,888 deletions.
15 changes: 7 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# ulix changelog

## v0.2.0
_2021-09-24_

* `decodeTime` helper
# ulid-workers changelog

## v0.1.0
_2021-06-05_

* Initial release
_2021-11-10_

- Initial release
- Fork from ulidx
- Remove dependencies
- Remove PRNG detection
- Web Crypto API only
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# ulidx
> ULID generator for NodeJS and the browser
# ulid-workers

[![ulidx](https://img.shields.io/npm/v/ulidx?color=blue&label=ulidx&logo=npm&style=flat-square)](https://www.npmjs.com/package/ulidx) ![Tests status](https://github.com/perry-mitchell/ulidx/actions/workflows/test.yml/badge.svg)
> Zero dependency ULID generator for Cloudflare Workers
ULID generator library, based off of the original [ulid](https://github.com/ulid/javascript) for NodeJS and the browser. ULIDs are Universally Unique Lexicographically Sortable Identifiers. This library adheres to [this specification](https://github.com/ulid/spec).
ULID generator library for [Cloudflare Workers](https://developers.cloudflare.com/workers/), based off of the fork [ulidx](https://github.com/perry-mitchell/ulidx) which in turn was based on the original [ulid](https://github.com/ulid/javascript) for NodeJS and the browser.

ULIDs are Universally Unique Lexicographically Sortable Identifiers. This library adheres to [this specification](https://github.com/ulid/spec).

> The original [ulid](https://github.com/ulid/javascript) is no longer maintained, and has several outstanding compatibility-related issues that were never addressed. This library aims to address those and remain compatible in a larger range of environments.
## Installation

Install using npm by running: `npm install ulidx --save`.
Install using npm by running: `npm install ulid-workers --save`.

`ulidx` provides types and is written entirely in Typescript.
`ulid-workers` provides types and is written entirely in Typescript.

## Usage

Import `ulid` to generate new ULIDs:
Import `ulid-workers` to generate new ULIDs:

```typescript
import { ulid } from "ulidx";
import { ulid } from "ulid-workers";

ulid(); // 01F7DKCVCVDZN1Z5Q4FWANHHCC
```
Expand All @@ -38,7 +39,7 @@ ulid(1469918176385); // 01ARYZ6S41TSV4RRFFQ69G5FAV
To generate monotonically increasing ULIDs, create a monotonic counter using the factory:

```typescript
import { monotonicFactory } from "ulidx";
import { monotonicFactory } from "ulid-workers";

const ulid = monotonicFactory();

Expand All @@ -58,30 +59,28 @@ ulid(100000); // 000XAL6S41ACTAV9WEVGEMMVRD
Import `decodeTime` to extract the timestamp embedded in a ULID:

```typescript
import { decodeTime } from "ulidx";
import { decodeTime } from "ulid-workers";

// Extract milliseconds since UNIX Epoch from ULID
decodeTime("01ARYZ6S41TSV4RRFFQ69G5FAV"); // 1469918176385
```

## Pseudo-Random Number Generation (PRNG)

`ulidx` will attempt to locate a suitable cryptographically-secure random number generator in the environment where it's loaded. On NodeJS this will be `crypto.randomBytes` and in the browser it will be `crypto.getRandomValues`.
The Cloudflare Workers Runtime implements the [Web Crypto API](https://developers.cloudflare.com/workers/runtime-apis/web-crypto) (with some caveats). It does not provide Node's crypto module. Therefore, our Pseudo Random Number Generator (PRNG) uses `crypto.getRandomValues`.

`Math.random()` is **not supported**: The environment _must_ have a suitable crypto random number generator.
## Date Seed for ULID Time

## Compatibility
From the [Workers' docs](https://developers.cloudflare.com/workers/learning/security-model#step-1-disallow-timers-and-multi-threading):

`ulidx` is compatible with the following environments:
> In Workers, you can get the current time using the JavaScript Date API, for example by calling `Date.now()`. However, the time value returned by this is not really the current time. Instead, it is the **time at which the network message was received** which caused the application to begin executing. While the application executes, time is locked in place.
* NodeJS 10 and up
* Browsers with working `crypto` / `msCrypto` libraries
* Web workers
For this reason we recommend you use the [monotonic ULID factory](#monotonic-ulid-factory).

### Web
## Compatibility

`ulidx` is not currently bundled for web - you can do this yourself using a tool like Webpack or Rollup. You should absolutely disable polyfills for `crypto` in this case, as `ulidx` will use the built-in `crypto` global API rather than any polyfilled crypto anyway. Including a polyfill for crypto will just bloat your application.
`ulid-workers` is compatible with Cloudflare Workers and Durable Objects.

### Goals

React-Native support, with synchronous PRNG is a goal of this library. No ETA, however.
If it ain't broke don't fix it.
Loading

0 comments on commit 935352a

Please sign in to comment.