@typed/id 0.14.0 → 0.15.0

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/dist/index.d.ts CHANGED
@@ -4,5 +4,4 @@ export * from './NanoId.js';
4
4
  export * from './Ulid.js';
5
5
  export * from './Uuid4.js';
6
6
  export * from './Uuid5.js';
7
- export * from './Uuid6.js';
8
7
  export * from './Uuid7.js';
package/dist/index.js CHANGED
@@ -4,6 +4,5 @@ export * from './NanoId.js';
4
4
  export * from './Ulid.js';
5
5
  export * from './Uuid4.js';
6
6
  export * from './Uuid5.js';
7
- export * from './Uuid6.js';
8
7
  export * from './Uuid7.js';
9
8
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,sBAAsB,CAAA;AACpC,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,sBAAsB,CAAA;AACpC,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typed/id",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "Common ID format generation for Effect",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/readme.md CHANGED
@@ -32,9 +32,7 @@ import {
32
32
  GetRandomValues,
33
33
  makeUuid4,
34
34
  makeUuid5,
35
- makeUuid6,
36
35
  makeUuid7,
37
- Uuid6State,
38
36
  Uuid7State,
39
37
  Uuid5Namespace,
40
38
  Sha1,
@@ -58,15 +56,6 @@ await makeUuid5(Uuid5Namespace.URL, 'https://example.com').pipe(
58
56
  )
59
57
  // Output: "2ed6657d-e927-568b-95e1-2665a8aea6a2"
60
58
 
61
- // Generate a UUID v6 (reordered time-based)
62
- await makeUuid6.pipe(
63
- Effect.provide(Uuid6State.Default),
64
- Effect.provide([GetRandomValues.CryptoRandom, DateTimes.Default]),
65
- Effect.flatMap(Effect.log),
66
- Effect.runPromise
67
- )
68
- // Output: "1ee6742c-8f9c-6000-b9d1-0242ac120002"
69
-
70
59
  // Generate a UUID v7 (time-sortable)
71
60
  await makeUuid7.pipe(
72
61
  Effect.provide(Uuid7State.Default),
package/src/id.test.ts CHANGED
@@ -28,7 +28,7 @@ const makeTestValues = (length: number) => {
28
28
  }
29
29
 
30
30
  const provideTestValues = flow(
31
- Effect.provide([Uuid6State.Default, Uuid7State.Default]),
31
+ Effect.provide([Uuid7State.Default, Uuid6State.Default]),
32
32
  Effect.provide(Sha1.Default),
33
33
  Effect.provide([
34
34
  GetRandomValues.layer((length) => Effect.succeed(makeTestValues(length))),
package/src/index.ts CHANGED
@@ -4,5 +4,4 @@ export * from './NanoId.js'
4
4
  export * from './Ulid.js'
5
5
  export * from './Uuid4.js'
6
6
  export * from './Uuid5.js'
7
- export * from './Uuid6.js'
8
7
  export * from './Uuid7.js'
package/src/Uuid6.ts DELETED
@@ -1,141 +0,0 @@
1
- import * as Effect from 'effect/Effect'
2
- import { GetRandomValues } from './GetRandomValues.js'
3
- import { Layer, Schema } from 'effect'
4
- import { DateTimes } from './DateTimes.js'
5
- import { uuidStringify } from './UuidStringify.js'
6
-
7
- export const Uuid6 = Schema.UUID.pipe(Schema.brand('@typed/id/UUID6'))
8
- export type Uuid6 = Schema.Schema.Type<typeof Uuid6>
9
-
10
- export const isUuid6: (value: string) => value is Uuid6 = Schema.is(Uuid6)
11
-
12
- export type Uuid6Seed = {
13
- readonly msecs: number
14
- readonly nsecs: number
15
- readonly clockSeq: number
16
- readonly nodeId: Uint8Array
17
- }
18
-
19
- export class Uuid6State extends Effect.Tag('Uuid6State')<
20
- Uuid6State,
21
- {
22
- readonly next: Effect.Effect<Uuid6Seed>
23
- }
24
- >() {
25
- static Default: Layer.Layer<Uuid6State, never, GetRandomValues | DateTimes> = Layer.effect(
26
- this,
27
- Effect.gen(function* () {
28
- const { now } = yield* DateTimes
29
- const getRandomValues = yield* GetRandomValues
30
-
31
- const state = {
32
- msecs: Number.NEGATIVE_INFINITY,
33
- nsecs: 0,
34
- clockSeq: -1,
35
- nodeId: undefined as Uint8Array | undefined,
36
- }
37
-
38
- function updateState(currentTime: number, randomBytes: Uint8Array) {
39
- if (currentTime === state.msecs) {
40
- // Same msec-interval = simulate higher clock resolution by bumping `nsecs`
41
- state.nsecs++
42
-
43
- // Check for `nsecs` overflow (nsecs is capped at 10K intervals / msec)
44
- if (state.nsecs >= 10000) {
45
- state.nodeId = undefined
46
- state.nsecs = 0
47
- }
48
- } else if (currentTime > state.msecs) {
49
- // Reset nsec counter when clock advances to a new msec interval
50
- state.nsecs = 0
51
- } else if (currentTime < state.msecs) {
52
- // Handle clock regression
53
- state.nodeId = undefined
54
- }
55
-
56
- // Init node and clock sequence if needed
57
- if (!state.nodeId) {
58
- state.nodeId = randomBytes.slice(10, 16)
59
- // Set multicast bit
60
- state.nodeId[0] |= 0x01
61
-
62
- // Clock sequence must be randomized
63
- state.clockSeq = ((randomBytes[8] << 8) | randomBytes[9]) & 0x3fff
64
- }
65
-
66
- state.msecs = currentTime
67
-
68
- return {
69
- msecs: state.msecs,
70
- nsecs: state.nsecs,
71
- clockSeq: state.clockSeq,
72
- nodeId: state.nodeId,
73
- }
74
- }
75
-
76
- return {
77
- next: Effect.gen(function* () {
78
- const timestamp = yield* now
79
- const randomBytes = yield* getRandomValues(16)
80
- return updateState(timestamp, randomBytes)
81
- }),
82
- }
83
- }),
84
- )
85
- }
86
-
87
- export const makeUuid6: Effect.Effect<Uuid6, never, Uuid6State> = Effect.map(
88
- Uuid6State.next,
89
- uuid6FromSeed,
90
- )
91
-
92
- function uuid6FromSeed({ msecs, nsecs, clockSeq, nodeId }: Uuid6Seed): Uuid6 {
93
- // First generate the fields as they would be in a v1 UUID
94
- const result = new Uint8Array(16)
95
-
96
- // Offset to Gregorian epoch
97
- msecs += 12219292800000
98
-
99
- // `time_low`
100
- const tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000
101
- const time_low_bytes = new Uint8Array([
102
- (tl >>> 24) & 0xff,
103
- (tl >>> 16) & 0xff,
104
- (tl >>> 8) & 0xff,
105
- tl & 0xff,
106
- ])
107
-
108
- // `time_mid` and `time_high`
109
- const tmh = ((msecs / 0x100000000) * 10000) & 0xfffffff
110
- const time_mid_high_bytes = new Uint8Array([
111
- (tmh >>> 8) & 0xff,
112
- tmh & 0xff,
113
- ((tmh >>> 24) & 0xf) | 0x10, // include version 1
114
- (tmh >>> 16) & 0xff,
115
- ])
116
-
117
- result[0] = ((time_mid_high_bytes[2] & 0x0f) << 4) | ((time_mid_high_bytes[3] >> 4) & 0x0f)
118
- result[1] = ((time_mid_high_bytes[3] & 0x0f) << 4) | ((time_mid_high_bytes[0] & 0xf0) >> 4)
119
- result[2] = ((time_mid_high_bytes[0] & 0x0f) << 4) | ((time_mid_high_bytes[1] & 0xf0) >> 4)
120
- result[3] = ((time_mid_high_bytes[1] & 0x0f) << 4) | ((time_low_bytes[0] & 0xf0) >> 4)
121
- result[4] = ((time_low_bytes[0] & 0x0f) << 4) | ((time_low_bytes[1] & 0xf0) >> 4)
122
- result[5] = ((time_low_bytes[1] & 0x0f) << 4) | ((time_low_bytes[2] & 0xf0) >> 4)
123
- result[6] = 0x60 | (time_low_bytes[2] & 0x0f)
124
- result[7] = time_low_bytes[3]
125
-
126
- // clock_seq_hi_and_reserved
127
- result[8] = (clockSeq >>> 8) | 0x80 // variant bits
128
-
129
- // clock_seq_low
130
- result[9] = clockSeq & 0xff
131
-
132
- // node
133
- result[10] = nodeId[0]
134
- result[11] = nodeId[1]
135
- result[12] = nodeId[2]
136
- result[13] = nodeId[3]
137
- result[14] = nodeId[4]
138
- result[15] = nodeId[5]
139
-
140
- return uuidStringify(result) as Uuid6
141
- }