@typed/id 0.17.2 → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +164 -0
- package/dist/Cuid.d.ts +28 -17
- package/dist/Cuid.d.ts.map +1 -0
- package/dist/Cuid.js +41 -46
- package/dist/DateTimes.d.ts +15 -15
- package/dist/DateTimes.d.ts.map +1 -0
- package/dist/DateTimes.js +18 -15
- package/dist/Ids.d.ts +72 -0
- package/dist/Ids.d.ts.map +1 -0
- package/dist/Ids.js +52 -0
- package/dist/Ksuid.d.ts +8 -7
- package/dist/Ksuid.d.ts.map +1 -0
- package/dist/Ksuid.js +26 -33
- package/dist/NanoId.d.ts +8 -32
- package/dist/NanoId.d.ts.map +1 -0
- package/dist/NanoId.js +10 -14
- package/dist/RandomValues.d.ts +13 -0
- package/dist/RandomValues.d.ts.map +1 -0
- package/dist/RandomValues.js +19 -0
- package/dist/Ulid.d.ts +8 -30
- package/dist/Ulid.d.ts.map +1 -0
- package/dist/Ulid.js +15 -20
- package/dist/Uuid4.d.ts +7 -6
- package/dist/Uuid4.d.ts.map +1 -0
- package/dist/Uuid4.js +8 -10
- package/dist/Uuid5.d.ts +13 -19
- package/dist/Uuid5.d.ts.map +1 -0
- package/dist/Uuid5.js +14 -16
- package/dist/Uuid7.d.ts +35 -18
- package/dist/Uuid7.d.ts.map +1 -0
- package/dist/Uuid7.js +23 -22
- package/dist/_sha.d.ts +4 -0
- package/dist/_sha.d.ts.map +1 -0
- package/dist/_sha.js +3 -0
- package/dist/{UuidStringify.d.ts → _uuid-stringify.d.ts} +1 -0
- package/dist/_uuid-stringify.d.ts.map +1 -0
- package/dist/_uuid-stringify.js +28 -0
- package/dist/index.d.ts +11 -9
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -10
- package/package.json +25 -32
- package/src/Cuid.ts +87 -100
- package/src/DateTimes.ts +22 -26
- package/src/Id.test.ts +231 -0
- package/src/Ids.ts +128 -0
- package/src/Ksuid.ts +54 -62
- package/src/NanoId.ts +20 -47
- package/src/RandomValues.ts +33 -0
- package/src/Ulid.ts +37 -68
- package/src/Uuid4.ts +20 -18
- package/src/Uuid5.ts +39 -46
- package/src/Uuid7.ts +79 -80
- package/src/_sha.ts +11 -0
- package/src/_uuid-stringify.ts +30 -0
- package/src/index.ts +10 -9
- package/.nvmrc +0 -1
- package/biome.json +0 -36
- package/dist/Cuid.js.map +0 -1
- package/dist/DateTimes.js.map +0 -1
- package/dist/GetRandomValues.d.ts +0 -11
- package/dist/GetRandomValues.js +0 -17
- package/dist/GetRandomValues.js.map +0 -1
- package/dist/Ksuid.js.map +0 -1
- package/dist/NanoId.js.map +0 -1
- package/dist/Ulid.js.map +0 -1
- package/dist/Uuid4.js.map +0 -1
- package/dist/Uuid5.js.map +0 -1
- package/dist/Uuid7.js.map +0 -1
- package/dist/UuidStringify.js +0 -29
- package/dist/UuidStringify.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/readme.md +0 -169
- package/src/GetRandomValues.ts +0 -29
- package/src/UuidStringify.ts +0 -31
- package/src/id.test.ts +0 -133
- package/tsconfig.json +0 -26
package/README.md
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# @typed/id
|
|
2
|
+
|
|
3
|
+
> **Beta:** This package is in beta; APIs may change.
|
|
4
|
+
|
|
5
|
+
`@typed/id` provides **type-safe ID generation** (Effect-based) for multiple formats: **Cuid**, **Ksuid**, **NanoId**, **Ulid**, **Uuid4**, **Uuid5**, **Uuid7**, plus **DateTimes**, **Ids**, and **RandomValues**. Each typically exposes a branded Schema and an Effect that generates the ID (often requiring a service like `RandomValues` or `CuidState`). Use it when you need typed, reproducible IDs in Effect programs.
|
|
6
|
+
|
|
7
|
+
## Dependencies
|
|
8
|
+
|
|
9
|
+
- `effect`
|
|
10
|
+
|
|
11
|
+
## API overview
|
|
12
|
+
|
|
13
|
+
- **Cuid** — Schema + type; `CuidState` service; Effect to generate Cuid.
|
|
14
|
+
- **Ksuid** — K-sortable unique IDs.
|
|
15
|
+
- **NanoId** — Schema + type + `nanoId` Effect (depends on `RandomValues`).
|
|
16
|
+
- **Ulid** — ULID schema and generation.
|
|
17
|
+
- **Uuid4**, **Uuid5**, **Uuid7** — UUID variants with Schema and generation.
|
|
18
|
+
- **DateTimes** — Date/time helpers used by some ID generators.
|
|
19
|
+
- **Ids** — Generic ID helpers.
|
|
20
|
+
- **RandomValues** — Service for secure random bytes (used by NanoId, etc.).
|
|
21
|
+
|
|
22
|
+
## Example
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { Ids, Uuid5Namespace } from "@typed/id";
|
|
26
|
+
|
|
27
|
+
const id = yield * Ids.cuid;
|
|
28
|
+
const id = yield * Ids.ksuid;
|
|
29
|
+
const id = yield * Ids.nanoId;
|
|
30
|
+
const id = yield * Ids.ulid;
|
|
31
|
+
const id = yield * Ids.uuid4;
|
|
32
|
+
const id = yield * Ids.uuid5("https://effect.website", Uuid5Namespace.URL);
|
|
33
|
+
const id = yield * Ids.uuid7;
|
|
34
|
+
|
|
35
|
+
// Provide Ids services
|
|
36
|
+
Effect.provide(Ids.Default);
|
|
37
|
+
Effect.provide(Ids.Test());
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## API reference
|
|
41
|
+
|
|
42
|
+
### Ids
|
|
43
|
+
|
|
44
|
+
Unified service for generating all ID types. Requires `DateTimes`, `RandomValues`, `CuidState`, and `Uuid7State` (use `Ids.Default` or `Ids.Test()` to provide them).
|
|
45
|
+
|
|
46
|
+
| Member | Type | Description |
|
|
47
|
+
| -------------------- | ------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------- |
|
|
48
|
+
| `Ids.cuid` | `Effect<Cuid, never, Ids>` | Generate a Cuid. |
|
|
49
|
+
| `Ids.ksuid` | `Effect<Ksuid, never, Ids>` | Generate a Ksuid. |
|
|
50
|
+
| `Ids.nanoId` | `Effect<NanoId, never, Ids>` | Generate a NanoId. |
|
|
51
|
+
| `Ids.ulid` | `Effect<Ulid, never, Ids>` | Generate a ULID. |
|
|
52
|
+
| `Ids.uuid4` | `Effect<Uuid4, never, Ids>` | Generate a UUID v4. |
|
|
53
|
+
| `Ids.uuid5` | `(name, namespace) => Effect<Uuid5, never, Ids>` | Generate a UUID v5 from a name and namespace. Also has `Ids.uuid5.dns`, `.url`, `.oid`, `.x500` taking a single `name`. |
|
|
54
|
+
| `Ids.uuid7` | `Effect<Uuid7, never, Ids>` | Generate a UUID v7. |
|
|
55
|
+
| `Ids.Default` | `Layer<Ids \| DateTimes \| RandomValues>` | Layer that provides `Ids` with default Cuid/Uuid7/DateTimes/RandomValues. |
|
|
56
|
+
| `Ids.Test(options?)` | `Layer<Ids \| DateTimes \| RandomValues>` | Layer for tests; optional `currentTime` and `envData`. |
|
|
57
|
+
|
|
58
|
+
**TestOptions:** `{ currentTime?: number | string | Date; envData?: string }`
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
### Cuid
|
|
63
|
+
|
|
64
|
+
| Export | Type | Description |
|
|
65
|
+
| ------------------- | ---------------------------------- | -------------------------------------------------- |
|
|
66
|
+
| `Cuid` | `Schema<string, Cuid>` | Branded schema for Cuid strings. |
|
|
67
|
+
| `Cuid` (type) | `string` | Branded Cuid type. |
|
|
68
|
+
| `isCuid` | `(value: string) => value is Cuid` | Type guard. |
|
|
69
|
+
| `CuidState` | Service | Provides `next: Effect<CuidSeed>`. Used by `cuid`. |
|
|
70
|
+
| `CuidState.Default` | `Layer<CuidState>` | Default CuidState (uses `"node"` envData). |
|
|
71
|
+
| `cuid` | `Effect<Cuid, never, CuidState>` | Generate a Cuid. |
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
### Ksuid
|
|
76
|
+
|
|
77
|
+
| Export | Type | Description |
|
|
78
|
+
| -------------- | ------------------------------------------------- | ----------------------------------------- |
|
|
79
|
+
| `Ksuid` | `Schema<string, Ksuid>` | Branded schema for 27-char base62 Ksuids. |
|
|
80
|
+
| `Ksuid` (type) | `string` | Branded Ksuid type. |
|
|
81
|
+
| `isKsuid` | `(value: string) => value is Ksuid` | Type guard. |
|
|
82
|
+
| `ksuid` | `Effect<Ksuid, never, DateTimes \| RandomValues>` | Generate a Ksuid. |
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
### NanoId
|
|
87
|
+
|
|
88
|
+
| Export | Type | Description |
|
|
89
|
+
| --------------- | ------------------------------------- | -------------------------------------------- |
|
|
90
|
+
| `NanoId` | `Schema<string, NanoId>` | Branded schema for `[0-9a-zA-Z_-]+` strings. |
|
|
91
|
+
| `NanoId` (type) | `string` | Branded NanoId type. |
|
|
92
|
+
| `isNanoId` | `(value: string) => value is NanoId` | Type guard. |
|
|
93
|
+
| `nanoId` | `Effect<NanoId, never, RandomValues>` | Generate a 21-char NanoId. |
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
### Ulid
|
|
98
|
+
|
|
99
|
+
| Export | Type | Description |
|
|
100
|
+
| ------------- | ------------------------------------------------ | -------------------------------- |
|
|
101
|
+
| `Ulid` | `Schema<string, Ulid>` | Branded schema for ULID strings. |
|
|
102
|
+
| `Ulid` (type) | `string` | Branded Ulid type. |
|
|
103
|
+
| `isUlid` | `(value: string) => value is Ulid` | Type guard. |
|
|
104
|
+
| `ulid` | `Effect<Ulid, never, DateTimes \| RandomValues>` | Generate a ULID. |
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
### Uuid4
|
|
109
|
+
|
|
110
|
+
| Export | Type | Description |
|
|
111
|
+
| -------------- | ------------------------------------ | --------------------------- |
|
|
112
|
+
| `Uuid4` | `Schema<string, Uuid4>` | Branded schema for UUID v4. |
|
|
113
|
+
| `Uuid4` (type) | `string` | Branded Uuid4 type. |
|
|
114
|
+
| `isUuid4` | `(value: string) => value is Uuid4` | Type guard. |
|
|
115
|
+
| `uuid4` | `Effect<Uuid4, never, RandomValues>` | Generate a UUID v4. |
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
### Uuid5
|
|
120
|
+
|
|
121
|
+
| Export | Type | Description |
|
|
122
|
+
| ----------------------------------------------- | -------------------------------------------------------------------------------- | ------------------------------------------------------ |
|
|
123
|
+
| `Uuid5` | `Schema<string, Uuid5>` | Branded schema for UUID v5. |
|
|
124
|
+
| `Uuid5` (type) | `string` | Branded Uuid5 type. |
|
|
125
|
+
| `Uuid5Namespace` | `Uint8Array` (type) + const object | Namespace type; const has `DNS`, `URL`, `OID`, `X500`. |
|
|
126
|
+
| `isUuid5` | `(value: string) => value is Uuid5` | Type guard. |
|
|
127
|
+
| `uuid5` | `(name, namespace) => Effect<Uuid5>` or `(namespace) => (name) => Effect<Uuid5>` | Generate UUID v5 from name + namespace. |
|
|
128
|
+
| `dnsUuid5`, `urlUuid5`, `oidUuid5`, `x500Uuid5` | `(name: string) => Effect<Uuid5>` | Pre-bound effects for standard namespaces. |
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
### Uuid7
|
|
133
|
+
|
|
134
|
+
| Export | Type | Description |
|
|
135
|
+
| -------------------- | ----------------------------------- | ---------------------------------------------------- |
|
|
136
|
+
| `Uuid7` | `Schema<string, Uuid7>` | Branded schema for UUID v7. |
|
|
137
|
+
| `Uuid7` (type) | `string` | Branded Uuid7 type. |
|
|
138
|
+
| `isUuid7` | `(value: string) => value is Uuid7` | Type guard. |
|
|
139
|
+
| `Uuid7State` | Service | Provides `next: Effect<Uuid7Seed>`. Used by `uuid7`. |
|
|
140
|
+
| `Uuid7State.Default` | `Layer<Uuid7State>` | Default Uuid7State. |
|
|
141
|
+
| `uuid7` | `Effect<Uuid7, never, Uuid7State>` | Generate a UUID v7. |
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
### DateTimes
|
|
146
|
+
|
|
147
|
+
| Export | Type | Description |
|
|
148
|
+
| --------------------------- | ---------------------------------- | --------------------------------------------------------------- |
|
|
149
|
+
| `DateTimes` | Service | Provides `now: Effect<number>`, `date: Effect<Date>`. |
|
|
150
|
+
| `DateTimes.now` | `Effect<number, never, DateTimes>` | Current time in ms. |
|
|
151
|
+
| `DateTimes.date` | `Effect<Date, never, DateTimes>` | Current date. |
|
|
152
|
+
| `DateTimes.Default` | `Layer<DateTimes>` | Real clock. |
|
|
153
|
+
| `DateTimes.Fixed(baseDate)` | `Layer<DateTimes>` | Fixed time for tests; `baseDate` is `number \| string \| Date`. |
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
### RandomValues
|
|
158
|
+
|
|
159
|
+
| Export | Type | Description |
|
|
160
|
+
| --------------------------- | ----------------------------------------- | ----------------------------------------------------- |
|
|
161
|
+
| `RandomValues` | Service | Provides a function `(length) => Effect<Uint8Array>`. |
|
|
162
|
+
| `RandomValues.call(length)` | `Effect<Uint8Array, never, RandomValues>` | Request `length` cryptographically random bytes. |
|
|
163
|
+
| `RandomValues.Default` | `Layer<RandomValues>` | Uses `crypto.getRandomValues`. |
|
|
164
|
+
| `RandomValues.Random` | `Layer<RandomValues>` | Uses Effect `Random` (e.g. for tests). |
|
package/dist/Cuid.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import * as Effect from
|
|
2
|
-
import * as Layer from
|
|
3
|
-
import * as Schema from
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import * as Layer from "effect/Layer";
|
|
3
|
+
import * as Schema from "effect/Schema";
|
|
4
|
+
import * as ServiceMap from "effect/ServiceMap";
|
|
5
|
+
import { DateTimes } from "./DateTimes.js";
|
|
6
|
+
import { RandomValues } from "./RandomValues.js";
|
|
7
|
+
export declare const Cuid: Schema.brand<Schema.String, "@typed/id/CUID">;
|
|
7
8
|
export type Cuid = Schema.Schema.Type<typeof Cuid>;
|
|
8
9
|
export declare const isCuid: (value: string) => value is Cuid;
|
|
9
10
|
export type CuidSeed = {
|
|
@@ -12,18 +13,28 @@ export type CuidSeed = {
|
|
|
12
13
|
readonly random: Uint8Array;
|
|
13
14
|
readonly fingerprint: string;
|
|
14
15
|
};
|
|
15
|
-
declare const CuidState_base:
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
16
|
+
declare const CuidState_base: ServiceMap.ServiceClass<CuidState, "@typed/id/CuidState", Effect.Effect<{
|
|
17
|
+
timestamp: number;
|
|
18
|
+
counter: number;
|
|
19
|
+
random: Uint8Array<ArrayBufferLike>;
|
|
20
|
+
fingerprint: string;
|
|
21
|
+
}, never, never>> & {
|
|
22
|
+
readonly make: (envData: string) => Effect.Effect<Effect.Effect<{
|
|
23
|
+
timestamp: number;
|
|
24
|
+
counter: number;
|
|
25
|
+
random: Uint8Array<ArrayBufferLike>;
|
|
26
|
+
fingerprint: string;
|
|
27
|
+
}, never, never>, never, DateTimes | RandomValues>;
|
|
23
28
|
};
|
|
24
29
|
export declare class CuidState extends CuidState_base {
|
|
25
|
-
static readonly
|
|
26
|
-
|
|
30
|
+
static readonly next: Effect.Effect<{
|
|
31
|
+
timestamp: number;
|
|
32
|
+
counter: number;
|
|
33
|
+
random: Uint8Array<ArrayBufferLike>;
|
|
34
|
+
fingerprint: string;
|
|
35
|
+
}, never, CuidState>;
|
|
36
|
+
static readonly Default: Layer.Layer<DateTimes | RandomValues | CuidState, never, never>;
|
|
27
37
|
}
|
|
28
|
-
export declare const
|
|
38
|
+
export declare const cuid: Effect.Effect<Cuid, never, CuidState>;
|
|
29
39
|
export {};
|
|
40
|
+
//# sourceMappingURL=Cuid.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Cuid.d.ts","sourceRoot":"","sources":["../src/Cuid.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAQjD,eAAO,MAAM,IAAI,+CAGhB,CAAC;AACF,MAAM,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,IAAI,CAAC,CAAC;AAEnD,eAAO,MAAM,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,KAAK,IAAI,IAAsB,CAAC;AAGxE,MAAM,MAAM,QAAQ,GAAG;IACrB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;CAC9B,CAAC;;;;;;;6BAGgB,MAAM;;;;;;;AADxB,qBAAa,SAAU,SAAQ,cA8B7B;IACA,MAAM,CAAC,QAAQ,CAAC,IAAI;;;;;yBAAwC;IAE5D,MAAM,CAAC,QAAQ,CAAC,OAAO,kEAErB;CACH;AAED,eAAO,MAAM,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,EAAE,SAAS,CAGtD,CAAC"}
|
package/dist/Cuid.js
CHANGED
|
@@ -1,20 +1,50 @@
|
|
|
1
|
-
import * as Effect from
|
|
2
|
-
import * as Layer from
|
|
3
|
-
import * as Schema from
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import * as Layer from "effect/Layer";
|
|
3
|
+
import * as Schema from "effect/Schema";
|
|
4
|
+
import * as ServiceMap from "effect/ServiceMap";
|
|
5
|
+
import { sha512 } from "./_sha.js";
|
|
6
|
+
import { DateTimes } from "./DateTimes.js";
|
|
7
|
+
import { RandomValues } from "./RandomValues.js";
|
|
6
8
|
// Constants
|
|
7
9
|
const DEFAULT_LENGTH = 24;
|
|
8
10
|
const BIG_LENGTH = 32;
|
|
9
11
|
const INITIAL_COUNT_MAX = 476782367;
|
|
10
12
|
// Schema
|
|
11
|
-
export const Cuid = Schema.String.pipe(Schema.
|
|
13
|
+
export const Cuid = Schema.String.pipe(Schema.check(Schema.isPattern(/^[a-z][0-9a-z]+$/)), Schema.brand("@typed/id/CUID"));
|
|
12
14
|
export const isCuid = Schema.is(Cuid);
|
|
15
|
+
export class CuidState extends ServiceMap.Service()("@typed/id/CuidState", {
|
|
16
|
+
make: (envData) => Effect.gen(function* () {
|
|
17
|
+
const { now } = yield* DateTimes;
|
|
18
|
+
const getRandomValues = yield* RandomValues;
|
|
19
|
+
const initialBytes = yield* getRandomValues(4);
|
|
20
|
+
const initialValue = Math.abs((initialBytes[0] << 24) |
|
|
21
|
+
(initialBytes[1] << 16) |
|
|
22
|
+
(initialBytes[2] << 8) |
|
|
23
|
+
initialBytes[3]) % INITIAL_COUNT_MAX;
|
|
24
|
+
// Create fingerprint from environment data
|
|
25
|
+
const fingerprint = (yield* hash(envData)).substring(0, BIG_LENGTH);
|
|
26
|
+
let counter = initialValue;
|
|
27
|
+
return Effect.gen(function* () {
|
|
28
|
+
const timestamp = yield* now;
|
|
29
|
+
const random = yield* getRandomValues(32);
|
|
30
|
+
return {
|
|
31
|
+
timestamp,
|
|
32
|
+
counter: counter++,
|
|
33
|
+
random,
|
|
34
|
+
fingerprint,
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
}),
|
|
38
|
+
}) {
|
|
39
|
+
static next = Effect.flatten(CuidState.asEffect());
|
|
40
|
+
static Default = Layer.effect(CuidState, CuidState.make("node")).pipe(Layer.provideMerge([DateTimes.Default, RandomValues.Default]));
|
|
41
|
+
}
|
|
42
|
+
export const cuid = Effect.flatMap(CuidState.next, cuidFromSeed);
|
|
13
43
|
// Utilities
|
|
14
44
|
const ALPHABET = Array.from({ length: 26 }, (_, i) => String.fromCharCode(i + 97));
|
|
15
45
|
const encoder = new TextEncoder();
|
|
16
46
|
function createEntropy(length, random) {
|
|
17
|
-
let entropy =
|
|
47
|
+
let entropy = "";
|
|
18
48
|
let offset = 0;
|
|
19
49
|
while (entropy.length < length) {
|
|
20
50
|
const value = random[offset];
|
|
@@ -24,10 +54,7 @@ function createEntropy(length, random) {
|
|
|
24
54
|
return entropy;
|
|
25
55
|
}
|
|
26
56
|
function hash(input) {
|
|
27
|
-
|
|
28
|
-
const data = encoder.encode(input);
|
|
29
|
-
// Create a hash using the Web Crypto API
|
|
30
|
-
return crypto.subtle.digest('SHA-512', data).then((buffer) => {
|
|
57
|
+
return Effect.map(sha512(encoder.encode(input)), (buffer) => {
|
|
31
58
|
const view = new Uint8Array(buffer);
|
|
32
59
|
let value = 0n;
|
|
33
60
|
for (const byte of view) {
|
|
@@ -37,36 +64,7 @@ function hash(input) {
|
|
|
37
64
|
return value.toString(36).slice(1);
|
|
38
65
|
});
|
|
39
66
|
}
|
|
40
|
-
|
|
41
|
-
export class CuidState extends Effect.Tag('CuidState')() {
|
|
42
|
-
static layer = (envData) => Layer.effect(this, Effect.gen(function* () {
|
|
43
|
-
const { now } = yield* DateTimes;
|
|
44
|
-
const getRandomValues = yield* GetRandomValues;
|
|
45
|
-
const initialBytes = yield* getRandomValues(4);
|
|
46
|
-
const initialValue = Math.abs((initialBytes[0] << 24) |
|
|
47
|
-
(initialBytes[1] << 16) |
|
|
48
|
-
(initialBytes[2] << 8) |
|
|
49
|
-
initialBytes[3]) % INITIAL_COUNT_MAX;
|
|
50
|
-
// Create fingerprint from environment data
|
|
51
|
-
const fingerprint = yield* Effect.promise(() => hash(envData).then((h) => h.substring(0, BIG_LENGTH)));
|
|
52
|
-
let counter = initialValue;
|
|
53
|
-
return {
|
|
54
|
-
next: Effect.gen(function* () {
|
|
55
|
-
const timestamp = yield* now;
|
|
56
|
-
const random = yield* getRandomValues(32);
|
|
57
|
-
return {
|
|
58
|
-
timestamp,
|
|
59
|
-
counter: counter++,
|
|
60
|
-
random,
|
|
61
|
-
fingerprint,
|
|
62
|
-
};
|
|
63
|
-
}),
|
|
64
|
-
};
|
|
65
|
-
}));
|
|
66
|
-
static Default = this.layer('node');
|
|
67
|
-
}
|
|
68
|
-
// Core Functions
|
|
69
|
-
function cuidFromSeed({ timestamp, counter, random, fingerprint }) {
|
|
67
|
+
function cuidFromSeed({ counter, fingerprint, random, timestamp }) {
|
|
70
68
|
return Effect.gen(function* () {
|
|
71
69
|
// First letter is always a random lowercase letter from the seed
|
|
72
70
|
const firstLetter = ALPHABET[random[0] % ALPHABET.length];
|
|
@@ -77,12 +75,9 @@ function cuidFromSeed({ timestamp, counter, random, fingerprint }) {
|
|
|
77
75
|
const salt = createEntropy(4, random.slice(1));
|
|
78
76
|
// Hash all components together
|
|
79
77
|
const hashInput = `${time}${salt}${count}${fingerprint}`;
|
|
80
|
-
const hashed = yield*
|
|
78
|
+
const hashed = yield* hash(hashInput);
|
|
81
79
|
// Construct the final CUID
|
|
82
80
|
const id = `${firstLetter}${hashed.substring(0, DEFAULT_LENGTH - 1)}`;
|
|
83
|
-
return Cuid.
|
|
81
|
+
return Cuid.makeUnsafe(id);
|
|
84
82
|
});
|
|
85
83
|
}
|
|
86
|
-
// Public API
|
|
87
|
-
export const makeCuid = Effect.flatMap(CuidState.next, cuidFromSeed);
|
|
88
|
-
//# sourceMappingURL=Cuid.js.map
|
package/dist/DateTimes.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import * as Effect from
|
|
2
|
-
import * as Layer from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
readonly now: Effect.Effect<number>;
|
|
8
|
-
readonly date: Effect.Effect<Date>;
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import * as Layer from "effect/Layer";
|
|
3
|
+
import * as ServiceMap from "effect/ServiceMap";
|
|
4
|
+
declare const DateTimes_base: ServiceMap.ServiceClass<DateTimes, "@typed/id/DateTimes", {
|
|
5
|
+
now: Effect.Effect<number, never, never>;
|
|
6
|
+
date: Effect.Effect<Date, never, never>;
|
|
9
7
|
}> & {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
8
|
+
readonly make: Effect.Effect<{
|
|
9
|
+
now: Effect.Effect<number, never, never>;
|
|
10
|
+
date: Effect.Effect<Date, never, never>;
|
|
11
|
+
}, never, never>;
|
|
14
12
|
};
|
|
15
13
|
export declare class DateTimes extends DateTimes_base {
|
|
16
|
-
static readonly
|
|
17
|
-
static readonly
|
|
18
|
-
static readonly
|
|
14
|
+
static readonly now: Effect.Effect<number, never, DateTimes>;
|
|
15
|
+
static readonly date: Effect.Effect<Date, never, DateTimes>;
|
|
16
|
+
static readonly Default: Layer.Layer<DateTimes, never, never>;
|
|
17
|
+
static readonly Fixed: (baseDate: number | string | Date) => Layer.Layer<DateTimes, never, never>;
|
|
19
18
|
}
|
|
20
19
|
export {};
|
|
20
|
+
//# sourceMappingURL=DateTimes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DateTimes.d.ts","sourceRoot":"","sources":["../src/DateTimes.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAC;;;;;;;;;;AAEhD,qBAAa,SAAU,SAAQ,cAK7B;IACA,MAAM,CAAC,QAAQ,CAAC,GAAG,0CAA0D;IAC7E,MAAM,CAAC,QAAQ,CAAC,IAAI,wCAA4D;IAEhF,MAAM,CAAC,QAAQ,CAAC,OAAO,uCAA2C;IAElE,MAAM,CAAC,QAAQ,CAAC,KAAK,GAAI,UAAU,MAAM,GAAG,MAAM,GAAG,IAAI,0CAkBrD;CACL"}
|
package/dist/DateTimes.js
CHANGED
|
@@ -1,22 +1,25 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import * as
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import * as Clock from "effect/Clock";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import * as Layer from "effect/Layer";
|
|
4
|
+
import * as ServiceMap from "effect/ServiceMap";
|
|
5
|
+
export class DateTimes extends ServiceMap.Service()("@typed/id/DateTimes", {
|
|
6
|
+
make: Effect.succeed({
|
|
7
|
+
now: Effect.sync(() => Date.now()),
|
|
8
|
+
date: Effect.sync(() => new Date()),
|
|
9
|
+
}),
|
|
10
|
+
}) {
|
|
11
|
+
static now = Effect.flatMap(DateTimes.asEffect(), ({ now }) => now);
|
|
12
|
+
static date = Effect.flatMap(DateTimes.asEffect(), ({ date }) => date);
|
|
13
|
+
static Default = Layer.effect(DateTimes, DateTimes.make);
|
|
14
|
+
static Fixed = (baseDate) => Layer.effect(DateTimes, Effect.gen(function* () {
|
|
15
|
+
const clock = yield* Clock.Clock;
|
|
16
|
+
const base = new Date(baseDate);
|
|
11
17
|
const baseN = BigInt(base.getTime());
|
|
12
18
|
const startMillis = yield* clock.currentTimeMillis;
|
|
13
19
|
const now = clock.currentTimeMillis.pipe(Effect.map((millis) =>
|
|
14
20
|
// Use BigInt to avoid floating point precision issues which can break deterministic testing
|
|
15
21
|
Number(baseN + BigInt(millis) - BigInt(startMillis))));
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
date: now.pipe(Effect.map((millis) => new Date(millis))),
|
|
19
|
-
};
|
|
22
|
+
const date = now.pipe(Effect.map((millis) => new Date(millis)));
|
|
23
|
+
return DateTimes.of({ now, date });
|
|
20
24
|
}));
|
|
21
25
|
}
|
|
22
|
-
//# sourceMappingURL=DateTimes.js.map
|
package/dist/Ids.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import * as Layer from "effect/Layer";
|
|
3
|
+
import * as ServiceMap from "effect/ServiceMap";
|
|
4
|
+
import type { Cuid } from "./Cuid.js";
|
|
5
|
+
import { CuidState } from "./Cuid.js";
|
|
6
|
+
import { DateTimes } from "./DateTimes.js";
|
|
7
|
+
import type { Ksuid } from "./Ksuid.js";
|
|
8
|
+
import type { NanoId } from "./NanoId.js";
|
|
9
|
+
import { RandomValues } from "./RandomValues.js";
|
|
10
|
+
import type { Ulid } from "./Ulid.js";
|
|
11
|
+
import type { Uuid4 } from "./Uuid4.js";
|
|
12
|
+
import { type Uuid5, Uuid5Namespace } from "./Uuid5.js";
|
|
13
|
+
import type { Uuid7 } from "./Uuid7.js";
|
|
14
|
+
import { Uuid7State } from "./Uuid7.js";
|
|
15
|
+
import { TestClock } from "effect/testing";
|
|
16
|
+
declare const Ids_base: ServiceMap.ServiceClass<Ids, "@typed/id/Ids", {
|
|
17
|
+
cuid: Effect.Effect<string & import("effect/Brand").Brand<"@typed/id/CUID">, never, never>;
|
|
18
|
+
ksuid: Effect.Effect<string & import("effect/Brand").Brand<"@typed/id/KSUID">, never, never>;
|
|
19
|
+
nanoId: Effect.Effect<string & import("effect/Brand").Brand<"@typed/id/NanoId">, never, never>;
|
|
20
|
+
ulid: Effect.Effect<string & import("effect/Brand").Brand<"@typed/id/ULID">, never, never>;
|
|
21
|
+
uuid4: Effect.Effect<string & import("effect/Brand").Brand<"@typed/id/UUID4">, never, never>;
|
|
22
|
+
uuid5: {
|
|
23
|
+
(namespace: Uuid5Namespace): (name: string) => Effect.Effect<Uuid5>;
|
|
24
|
+
(name: string, namespace: Uuid5Namespace): Effect.Effect<Uuid5>;
|
|
25
|
+
readonly dns: (name: string) => Effect.Effect<Uuid5>;
|
|
26
|
+
readonly url: (name: string) => Effect.Effect<Uuid5>;
|
|
27
|
+
readonly oid: (name: string) => Effect.Effect<Uuid5>;
|
|
28
|
+
readonly x500: (name: string) => Effect.Effect<Uuid5>;
|
|
29
|
+
};
|
|
30
|
+
uuid7: Effect.Effect<string & import("effect/Brand").Brand<"@typed/id/UUID7">, never, never>;
|
|
31
|
+
}> & {
|
|
32
|
+
readonly make: Effect.Effect<{
|
|
33
|
+
cuid: Effect.Effect<string & import("effect/Brand").Brand<"@typed/id/CUID">, never, never>;
|
|
34
|
+
ksuid: Effect.Effect<string & import("effect/Brand").Brand<"@typed/id/KSUID">, never, never>;
|
|
35
|
+
nanoId: Effect.Effect<string & import("effect/Brand").Brand<"@typed/id/NanoId">, never, never>;
|
|
36
|
+
ulid: Effect.Effect<string & import("effect/Brand").Brand<"@typed/id/ULID">, never, never>;
|
|
37
|
+
uuid4: Effect.Effect<string & import("effect/Brand").Brand<"@typed/id/UUID4">, never, never>;
|
|
38
|
+
uuid5: {
|
|
39
|
+
(namespace: Uuid5Namespace): (name: string) => Effect.Effect<Uuid5>;
|
|
40
|
+
(name: string, namespace: Uuid5Namespace): Effect.Effect<Uuid5>;
|
|
41
|
+
readonly dns: (name: string) => Effect.Effect<Uuid5>;
|
|
42
|
+
readonly url: (name: string) => Effect.Effect<Uuid5>;
|
|
43
|
+
readonly oid: (name: string) => Effect.Effect<Uuid5>;
|
|
44
|
+
readonly x500: (name: string) => Effect.Effect<Uuid5>;
|
|
45
|
+
};
|
|
46
|
+
uuid7: Effect.Effect<string & import("effect/Brand").Brand<"@typed/id/UUID7">, never, never>;
|
|
47
|
+
}, never, DateTimes | RandomValues | CuidState | Uuid7State>;
|
|
48
|
+
};
|
|
49
|
+
export declare class Ids extends Ids_base {
|
|
50
|
+
static readonly cuid: Effect.Effect<Cuid, never, Ids>;
|
|
51
|
+
static readonly ksuid: Effect.Effect<Ksuid, never, Ids>;
|
|
52
|
+
static readonly nanoId: Effect.Effect<NanoId, never, Ids>;
|
|
53
|
+
static readonly ulid: Effect.Effect<Ulid, never, Ids>;
|
|
54
|
+
static readonly uuid4: Effect.Effect<Uuid4, never, Ids>;
|
|
55
|
+
static readonly uuid5: {
|
|
56
|
+
(namespace: Uuid5Namespace): (name: string) => Effect.Effect<Uuid5, never, Ids>;
|
|
57
|
+
(name: string, namespace: Uuid5Namespace): Effect.Effect<Uuid5, never, Ids>;
|
|
58
|
+
readonly dns: (name: string) => Effect.Effect<Uuid5, never, Ids>;
|
|
59
|
+
readonly url: (name: string) => Effect.Effect<Uuid5, never, Ids>;
|
|
60
|
+
readonly oid: (name: string) => Effect.Effect<Uuid5, never, Ids>;
|
|
61
|
+
readonly x500: (name: string) => Effect.Effect<Uuid5, never, Ids>;
|
|
62
|
+
};
|
|
63
|
+
static readonly uuid7: Effect.Effect<Uuid7, never, Ids>;
|
|
64
|
+
static readonly Default: Layer.Layer<Ids | DateTimes | RandomValues, never, never>;
|
|
65
|
+
static readonly Test: (options?: TestOptions) => Layer.Layer<Ids | DateTimes | RandomValues | TestClock.TestClock>;
|
|
66
|
+
}
|
|
67
|
+
export type TestOptions = {
|
|
68
|
+
readonly currentTime?: number | string | Date;
|
|
69
|
+
readonly envData?: string;
|
|
70
|
+
};
|
|
71
|
+
export {};
|
|
72
|
+
//# sourceMappingURL=Ids.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Ids.d.ts","sourceRoot":"","sources":["../src/Ids.ts"],"names":[],"mappings":"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"}
|
package/dist/Ids.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import { dual } from "effect/Function";
|
|
3
|
+
import * as Layer from "effect/Layer";
|
|
4
|
+
import * as ServiceMap from "effect/ServiceMap";
|
|
5
|
+
import { cuid, CuidState } from "./Cuid.js";
|
|
6
|
+
import { DateTimes } from "./DateTimes.js";
|
|
7
|
+
import { ksuid } from "./Ksuid.js";
|
|
8
|
+
import { nanoId } from "./NanoId.js";
|
|
9
|
+
import { RandomValues } from "./RandomValues.js";
|
|
10
|
+
import { ulid } from "./Ulid.js";
|
|
11
|
+
import { uuid4 } from "./Uuid4.js";
|
|
12
|
+
import { uuid5, Uuid5Namespace } from "./Uuid5.js";
|
|
13
|
+
import { uuid7, Uuid7State } from "./Uuid7.js";
|
|
14
|
+
import { TestClock } from "effect/testing";
|
|
15
|
+
export class Ids extends ServiceMap.Service()("@typed/id/Ids", {
|
|
16
|
+
make: Effect.gen(function* () {
|
|
17
|
+
const services = yield* Effect.services();
|
|
18
|
+
const uuid5_ = Object.assign(dual(2, (name, namespace) => Effect.provide(uuid5(name, namespace), services)), {
|
|
19
|
+
dns: uuid5(Uuid5Namespace.DNS),
|
|
20
|
+
url: uuid5(Uuid5Namespace.URL),
|
|
21
|
+
oid: uuid5(Uuid5Namespace.OID),
|
|
22
|
+
x500: uuid5(Uuid5Namespace.X500),
|
|
23
|
+
});
|
|
24
|
+
return {
|
|
25
|
+
cuid: Effect.provide(cuid, services),
|
|
26
|
+
ksuid: Effect.provide(ksuid, services),
|
|
27
|
+
nanoId: Effect.provide(nanoId, services),
|
|
28
|
+
ulid: Effect.provide(ulid, services),
|
|
29
|
+
uuid4: Effect.provide(uuid4, services),
|
|
30
|
+
uuid5: uuid5_,
|
|
31
|
+
uuid7: Effect.provide(uuid7, services),
|
|
32
|
+
};
|
|
33
|
+
}),
|
|
34
|
+
}) {
|
|
35
|
+
static cuid = Effect.flatMap(Ids.asEffect(), ({ cuid }) => cuid);
|
|
36
|
+
static ksuid = Effect.flatMap(Ids.asEffect(), ({ ksuid }) => ksuid);
|
|
37
|
+
static nanoId = Effect.flatMap(Ids.asEffect(), ({ nanoId }) => nanoId);
|
|
38
|
+
static ulid = Effect.flatMap(Ids.asEffect(), ({ ulid }) => ulid);
|
|
39
|
+
static uuid4 = Effect.flatMap(Ids.asEffect(), ({ uuid4 }) => uuid4);
|
|
40
|
+
static uuid5 = Object.assign(dual(2, (name, namespace) => Effect.flatMap(Ids.asEffect(), ({ uuid5 }) => uuid5(name, namespace))), {
|
|
41
|
+
dns: (name) => Effect.flatMap(Ids.asEffect(), ({ uuid5 }) => uuid5.dns(name)),
|
|
42
|
+
url: (name) => Effect.flatMap(Ids.asEffect(), ({ uuid5 }) => uuid5.url(name)),
|
|
43
|
+
oid: (name) => Effect.flatMap(Ids.asEffect(), ({ uuid5 }) => uuid5.oid(name)),
|
|
44
|
+
x500: (name) => Effect.flatMap(Ids.asEffect(), ({ uuid5 }) => uuid5.x500(name)),
|
|
45
|
+
});
|
|
46
|
+
static uuid7 = Effect.flatMap(Ids.asEffect(), ({ uuid7 }) => uuid7);
|
|
47
|
+
static Default = Layer.effect(Ids, Ids.make).pipe(Layer.provide([CuidState.Default, Uuid7State.Default]), Layer.provideMerge([DateTimes.Default, RandomValues.Default]));
|
|
48
|
+
static Test = (options) => Layer.effect(Ids, Ids.make).pipe(Layer.provide([
|
|
49
|
+
Layer.effect(CuidState, CuidState.make(options?.envData ?? "node")),
|
|
50
|
+
Uuid7State.Default,
|
|
51
|
+
]), Layer.provideMerge([DateTimes.Fixed(options?.currentTime ?? 0), RandomValues.Random]), Layer.provideMerge(TestClock.layer({})));
|
|
52
|
+
}
|
package/dist/Ksuid.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import * as Effect from
|
|
2
|
-
import * as Schema from
|
|
3
|
-
import { DateTimes } from
|
|
4
|
-
import {
|
|
5
|
-
export declare const Ksuid: Schema.brand<Schema.
|
|
6
|
-
export type Ksuid =
|
|
1
|
+
import * as Effect from "effect/Effect";
|
|
2
|
+
import * as Schema from "effect/Schema";
|
|
3
|
+
import { DateTimes } from "./DateTimes.js";
|
|
4
|
+
import { RandomValues } from "./RandomValues.js";
|
|
5
|
+
export declare const Ksuid: Schema.brand<Schema.String, "@typed/id/KSUID">;
|
|
6
|
+
export type Ksuid = typeof Ksuid.Type;
|
|
7
7
|
export declare const isKsuid: (value: string) => value is Ksuid;
|
|
8
|
-
export declare const
|
|
8
|
+
export declare const ksuid: Effect.Effect<Ksuid, never, DateTimes | RandomValues>;
|
|
9
|
+
//# sourceMappingURL=Ksuid.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Ksuid.d.ts","sourceRoot":"","sources":["../src/Ksuid.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAUjD,eAAO,MAAM,KAAK,gDAGjB,CAAC;AACF,MAAM,MAAM,KAAK,GAAG,OAAO,KAAK,CAAC,IAAI,CAAC;AAEtC,eAAO,MAAM,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,KAAK,IAAI,KAAwB,CAAC;AAM3E,eAAO,MAAM,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,SAAS,GAAG,YAAY,CAyBvE,CAAC"}
|