@typed/id 1.0.0-beta.4 → 1.0.0-beta.6

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.
Files changed (58) hide show
  1. package/README.md +171 -65
  2. package/dist/Cuid.d.ts +127 -12
  3. package/dist/Cuid.d.ts.map +1 -1
  4. package/dist/Cuid.js +133 -39
  5. package/dist/DateTimes.d.ts +64 -1
  6. package/dist/DateTimes.d.ts.map +1 -1
  7. package/dist/DateTimes.js +70 -3
  8. package/dist/Ids.d.ts +171 -28
  9. package/dist/Ids.d.ts.map +1 -1
  10. package/dist/Ids.js +159 -16
  11. package/dist/Ksuid.d.ts +51 -1
  12. package/dist/Ksuid.d.ts.map +1 -1
  13. package/dist/Ksuid.js +58 -6
  14. package/dist/NanoId.d.ts +47 -0
  15. package/dist/NanoId.d.ts.map +1 -1
  16. package/dist/NanoId.js +47 -0
  17. package/dist/RandomValues.d.ts +62 -3
  18. package/dist/RandomValues.d.ts.map +1 -1
  19. package/dist/RandomValues.js +72 -8
  20. package/dist/Ulid.d.ts +49 -1
  21. package/dist/Ulid.d.ts.map +1 -1
  22. package/dist/Ulid.js +53 -3
  23. package/dist/Uuid4.d.ts +47 -0
  24. package/dist/Uuid4.d.ts.map +1 -1
  25. package/dist/Uuid4.js +47 -0
  26. package/dist/Uuid5.d.ts +178 -6
  27. package/dist/Uuid5.d.ts.map +1 -1
  28. package/dist/Uuid5.js +151 -14
  29. package/dist/Uuid7.d.ts +120 -14
  30. package/dist/Uuid7.d.ts.map +1 -1
  31. package/dist/Uuid7.js +121 -13
  32. package/dist/__tests__/helpers.d.ts +7 -0
  33. package/dist/__tests__/helpers.d.ts.map +1 -0
  34. package/dist/__tests__/helpers.js +19 -0
  35. package/dist/__tests__/public-contract.type-test.d.ts +2 -0
  36. package/dist/__tests__/public-contract.type-test.d.ts.map +1 -0
  37. package/dist/__tests__/public-contract.type-test.js +3 -0
  38. package/dist/_sha.d.ts +32 -0
  39. package/dist/_sha.d.ts.map +1 -1
  40. package/dist/_sha.js +32 -0
  41. package/dist/_uuid-stringify.d.ts +15 -0
  42. package/dist/_uuid-stringify.d.ts.map +1 -1
  43. package/dist/_uuid-stringify.js +15 -0
  44. package/package.json +68 -14
  45. package/src/Cuid.ts +0 -124
  46. package/src/DateTimes.ts +0 -36
  47. package/src/Id.test.ts +0 -230
  48. package/src/Ids.ts +0 -128
  49. package/src/Ksuid.ts +0 -74
  50. package/src/NanoId.ts +0 -33
  51. package/src/RandomValues.ts +0 -33
  52. package/src/Ulid.ts +0 -51
  53. package/src/Uuid4.ts +0 -24
  54. package/src/Uuid5.ts +0 -71
  55. package/src/Uuid7.ts +0 -104
  56. package/src/_sha.ts +0 -11
  57. package/src/_uuid-stringify.ts +0 -30
  58. package/src/index.ts +0 -10
package/src/Ulid.ts DELETED
@@ -1,51 +0,0 @@
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
-
6
- export const Ulid = Schema.String.pipe(
7
- Schema.check(Schema.isULID()),
8
- Schema.brand("@typed/id/ULID"),
9
- );
10
- export type Ulid = typeof Ulid.Type;
11
-
12
- export const isUlid: (value: string) => value is Ulid = Schema.is(Ulid);
13
-
14
- type UlidSeed = Uint8Array & { length: 16 };
15
-
16
- // Crockford's Base32
17
- const ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
18
- const ENCODING_LEN = ENCODING.length;
19
- const TIME_MAX = 2 ** 48 - 1;
20
- const TIME_LEN = 10;
21
- const RANDOM_LEN = 16;
22
-
23
- export const ulid: Effect.Effect<Ulid, never, RandomValues | DateTimes> = Effect.zipWith(
24
- DateTimes.now,
25
- RandomValues.call<UlidSeed>(16),
26
- (now, seed) => {
27
- if (now > TIME_MAX) {
28
- throw new Error("Cannot generate ULID due to timestamp overflow");
29
- }
30
-
31
- return Ulid.make(encodeTime(now, TIME_LEN) + encodeRandom(seed));
32
- },
33
- );
34
-
35
- function encodeTime(now: number, len: number): string {
36
- let str = "";
37
- for (let i = len - 1; i >= 0; i--) {
38
- const mod = now % ENCODING_LEN;
39
- str = ENCODING.charAt(mod) + str;
40
- now = (now - mod) / ENCODING_LEN;
41
- }
42
- return str;
43
- }
44
-
45
- function encodeRandom(seed: UlidSeed): string {
46
- let str = "";
47
- for (let i = 0; i < RANDOM_LEN; i++) {
48
- str = str + ENCODING.charAt(seed[i] % ENCODING_LEN);
49
- }
50
- return str;
51
- }
package/src/Uuid4.ts DELETED
@@ -1,24 +0,0 @@
1
- import * as Effect from "effect/Effect";
2
- import * as Schema from "effect/Schema";
3
- import { uuidStringify } from "./_uuid-stringify.js";
4
- import { RandomValues } from "./RandomValues.js";
5
-
6
- export const Uuid4 = Schema.String.pipe(
7
- Schema.check(Schema.isUUID(4)),
8
- Schema.brand("@typed/id/UUID4"),
9
- );
10
- export type Uuid4 = typeof Uuid4.Type;
11
-
12
- export const isUuid4: (value: string) => value is Uuid4 = Schema.is(Uuid4);
13
-
14
- type Uuid4Seed = Uint8Array & { length: 16 };
15
-
16
- export const uuid4: Effect.Effect<Uuid4, never, RandomValues> = Effect.map(
17
- RandomValues.call<Uuid4Seed>(16),
18
- (seed: Uuid4Seed): Uuid4 => {
19
- // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
20
- seed[6] = (seed[6] & 0x0f) | 0x40;
21
- seed[8] = (seed[8] & 0x3f) | 0x80;
22
- return Uuid4.make(uuidStringify(seed));
23
- },
24
- );
package/src/Uuid5.ts DELETED
@@ -1,71 +0,0 @@
1
- import * as Effect from "effect/Effect";
2
- import { dual } from "effect/Function";
3
- import * as Schema from "effect/Schema";
4
- import { sha1 } from "./_sha.js";
5
- import { uuidStringify } from "./_uuid-stringify.js";
6
-
7
- export const Uuid5 = Schema.String.pipe(
8
- Schema.check(Schema.isUUID(5)),
9
- Schema.brand("@typed/id/UUID5"),
10
- );
11
- export type Uuid5 = typeof Uuid5.Type;
12
-
13
- export const isUuid5: (value: string) => value is Uuid5 = Schema.is(Uuid5);
14
-
15
- export type Uuid5Namespace = Uint8Array;
16
-
17
- const textEncoder = new TextEncoder();
18
-
19
- // Pre-defined namespaces from RFC 4122
20
- export const Uuid5Namespace = {
21
- DNS: new Uint8Array([
22
- 0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
23
- ]),
24
-
25
- URL: new Uint8Array([
26
- 0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
27
- ]),
28
-
29
- OID: new Uint8Array([
30
- 0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
31
- ]),
32
-
33
- X500: new Uint8Array([
34
- 0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
35
- ]),
36
- } as const;
37
-
38
- export const uuid5: {
39
- (namespace: Uuid5Namespace): (name: string) => Effect.Effect<Uuid5>;
40
- (name: string, namespace: Uuid5Namespace): Effect.Effect<Uuid5>;
41
- } = dual(2, function uuid5(name: string, namespace: Uuid5Namespace): Effect.Effect<Uuid5> {
42
- return Effect.gen(function* () {
43
- // Convert name to UTF-8 bytes
44
- const nameBytes = textEncoder.encode(name);
45
-
46
- // Concatenate namespace and name
47
- const buffer = new Uint8Array(namespace.length + nameBytes.length);
48
- buffer.set(namespace);
49
- buffer.set(nameBytes, namespace.length);
50
-
51
- // Hash the concatenated bytes
52
- const hash = yield* sha1(buffer);
53
-
54
- // Format as UUID v5
55
- const result = new Uint8Array(16);
56
-
57
- // Copy first 16 bytes of the hash
58
- result.set(hash.subarray(0, 16));
59
-
60
- // Set version (5) and variant bits
61
- result[6] = (result[6] & 0x0f) | 0x50; // version 5
62
- result[8] = (result[8] & 0x3f) | 0x80; // variant 1
63
-
64
- return Uuid5.make(uuidStringify(result));
65
- });
66
- });
67
-
68
- export const dnsUuid5 = uuid5(Uuid5Namespace.DNS);
69
- export const urlUuid5 = uuid5(Uuid5Namespace.URL);
70
- export const oidUuid5 = uuid5(Uuid5Namespace.OID);
71
- export const x500Uuid5 = uuid5(Uuid5Namespace.X500);
package/src/Uuid7.ts DELETED
@@ -1,104 +0,0 @@
1
- import * as Effect from "effect/Effect";
2
- import * as Layer from "effect/Layer";
3
- import * as Schema from "effect/Schema";
4
- import * as Context from "effect/Context";
5
- import { uuidStringify } from "./_uuid-stringify.js";
6
- import { DateTimes } from "./DateTimes.js";
7
- import { RandomValues } from "./RandomValues.js";
8
-
9
- export const Uuid7 = Schema.String.pipe(
10
- Schema.check(Schema.isUUID(7)),
11
- Schema.brand("@typed/id/UUID7"),
12
- );
13
- export type Uuid7 = typeof Uuid7.Type;
14
-
15
- export const isUuid7: (value: string) => value is Uuid7 = Schema.is(Uuid7);
16
-
17
- export type Uuid7Seed = {
18
- readonly timestamp: number;
19
- readonly seq: number;
20
- readonly randomBytes: Uint8Array & { length: 16 };
21
- };
22
-
23
- export class Uuid7State extends Context.Service<Uuid7State>()("@typed/id/Uuid7State", {
24
- make: Effect.gen(function* () {
25
- const { now } = yield* DateTimes;
26
- const getRandomValues = yield* RandomValues;
27
- const state = {
28
- msecs: Number.NEGATIVE_INFINITY,
29
- seq: 0,
30
- };
31
-
32
- function updateV7State(now: number, randomBytes: Uint8Array) {
33
- if (now > state.msecs) {
34
- // Time has moved on! Pick a new random sequence number
35
- state.seq =
36
- (randomBytes[6] << 23) | (randomBytes[7] << 16) | (randomBytes[8] << 8) | randomBytes[9];
37
- state.msecs = now;
38
- } else {
39
- // Bump sequence counter w/ 32-bit rollover
40
- state.seq = (state.seq + 1) | 0;
41
-
42
- // In case of rollover, bump timestamp to preserve monotonicity. This is
43
- // allowed by the RFC and should self-correct as the system clock catches
44
- // up. See https://www.rfc-editor.org/rfc/rfc9562.html#section-6.2-9.4
45
- if (state.seq === 0) {
46
- state.msecs++;
47
- }
48
- }
49
- }
50
-
51
- return Effect.gen(function* () {
52
- const randomBytes = yield* getRandomValues<Uuid7Seed["randomBytes"]>(16);
53
- updateV7State(yield* now, randomBytes);
54
- return { timestamp: state.msecs, seq: state.seq, randomBytes };
55
- });
56
- }),
57
- }) {
58
- static readonly next = Effect.flatten(Uuid7State.asEffect());
59
-
60
- static readonly Default = Layer.effect(Uuid7State, Uuid7State.make).pipe(
61
- Layer.provideMerge([DateTimes.Default, RandomValues.Default]),
62
- );
63
- }
64
-
65
- export const uuid7: Effect.Effect<Uuid7, never, Uuid7State> = Effect.map(
66
- Uuid7State.next,
67
- uuid7FromSeed,
68
- );
69
-
70
- function uuid7FromSeed({ randomBytes, seq, timestamp }: Uuid7Seed): Uuid7 {
71
- const result = new Uint8Array(16);
72
-
73
- // byte 0-5: timestamp (48 bits)
74
- result[0] = (timestamp / 0x10000000000) & 0xff;
75
- result[1] = (timestamp / 0x100000000) & 0xff;
76
- result[2] = (timestamp / 0x1000000) & 0xff;
77
- result[3] = (timestamp / 0x10000) & 0xff;
78
- result[4] = (timestamp / 0x100) & 0xff;
79
- result[5] = timestamp & 0xff;
80
-
81
- // byte 6: `version` (4 bits) | sequence bits 28-31 (4 bits)
82
- result[6] = 0x70 | ((seq >>> 28) & 0x0f);
83
-
84
- // byte 7: sequence bits 20-27 (8 bits)
85
- result[7] = (seq >>> 20) & 0xff;
86
-
87
- // byte 8: `variant` (2 bits) | sequence bits 14-19 (6 bits)
88
- result[8] = 0x80 | ((seq >>> 14) & 0x3f);
89
-
90
- // byte 9: sequence bits 6-13 (8 bits)
91
- result[9] = (seq >>> 6) & 0xff;
92
-
93
- // byte 10: sequence bits 0-5 (6 bits) | random (2 bits)
94
- result[10] = ((seq << 2) & 0xff) | (randomBytes[10] & 0x03);
95
-
96
- // bytes 11-15: random (40 bits)
97
- result[11] = randomBytes[11];
98
- result[12] = randomBytes[12];
99
- result[13] = randomBytes[13];
100
- result[14] = randomBytes[14];
101
- result[15] = randomBytes[15];
102
-
103
- return Uuid7.make(uuidStringify(result));
104
- }
package/src/_sha.ts DELETED
@@ -1,11 +0,0 @@
1
- import * as Effect from "effect/Effect";
2
-
3
- export const sha1 = (data: BufferSource) =>
4
- Effect.promise(() =>
5
- crypto.subtle.digest("SHA-1", data).then((buffer) => new Uint8Array(buffer)),
6
- );
7
-
8
- export const sha512 = (data: BufferSource) =>
9
- Effect.promise(() =>
10
- crypto.subtle.digest("SHA-512", data).then((buffer) => new Uint8Array(buffer)),
11
- );
@@ -1,30 +0,0 @@
1
- const byteToHex: Array<string> = [];
2
-
3
- for (let i = 0; i < 256; ++i) {
4
- byteToHex.push((i + 0x100).toString(16).slice(1));
5
- }
6
-
7
- export function uuidStringify(seed: Uint8Array): string {
8
- return [
9
- byteToHex[seed[0]],
10
- byteToHex[seed[1]],
11
- byteToHex[seed[2]],
12
- byteToHex[seed[3]],
13
- "-",
14
- byteToHex[seed[4]],
15
- byteToHex[seed[5]],
16
- "-",
17
- byteToHex[seed[6]],
18
- byteToHex[seed[7]],
19
- "-",
20
- byteToHex[seed[8]],
21
- byteToHex[seed[9]],
22
- "-",
23
- byteToHex[seed[10]],
24
- byteToHex[seed[11]],
25
- byteToHex[seed[12]],
26
- byteToHex[seed[13]],
27
- byteToHex[seed[14]],
28
- byteToHex[seed[15]],
29
- ].join("");
30
- }
package/src/index.ts DELETED
@@ -1,10 +0,0 @@
1
- export * from "./Cuid.js";
2
- export * from "./DateTimes.js";
3
- export * from "./Ids.js";
4
- export * from "./Ksuid.js";
5
- export * from "./NanoId.js";
6
- export * from "./RandomValues.js";
7
- export * from "./Ulid.js";
8
- export * from "./Uuid4.js";
9
- export * from "./Uuid5.js";
10
- export * from "./Uuid7.js";