@crombolo/core 0.28.0 → 0.29.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/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/keys.ts +41 -0
package/package.json
CHANGED
package/src/index.ts
CHANGED
package/src/keys.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keys made from names (stories 161, 163): RFC 4122 version 5 uuids, so every
|
|
3
|
+
* node and every environment computes the same key for the same thing without
|
|
4
|
+
* writing it down. Each family has a namespace of its own, fixed for ever:
|
|
5
|
+
* changing one re-keys everything made under it.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Seed items (story 163): content, Library and media, named by kind and path. */
|
|
9
|
+
export const SEED_NAMESPACE = '6f1d3c0e-7a52-4b8e-9c41-2e5f8a1b7d63'
|
|
10
|
+
|
|
11
|
+
/** Schema elements (story 161): types, properties, data types, forms, workflows, webhooks. */
|
|
12
|
+
export const SCHEMA_NAMESPACE = 'b3c8e2a4-1f6d-4a9b-8e37-5d0c2f9a6b14'
|
|
13
|
+
|
|
14
|
+
const bytesOf = (uuid: string): Uint8Array => {
|
|
15
|
+
const hex = uuid.replaceAll('-', '')
|
|
16
|
+
const out = new Uint8Array(16)
|
|
17
|
+
for (let i = 0; i < 16; i++) out[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16)
|
|
18
|
+
return out
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A version 5 uuid for `name` in `namespace`. Pure. */
|
|
22
|
+
export function uuidV5(name: string, namespace: string): string {
|
|
23
|
+
const hash = new Bun.CryptoHasher('sha1')
|
|
24
|
+
.update(bytesOf(namespace))
|
|
25
|
+
.update(name)
|
|
26
|
+
.digest() as Uint8Array
|
|
27
|
+
const bytes = hash.subarray(0, 16)
|
|
28
|
+
bytes[6] = ((bytes[6] as number) & 0x0f) | 0x50
|
|
29
|
+
bytes[8] = ((bytes[8] as number) & 0x3f) | 0x80
|
|
30
|
+
const hex = [...bytes].map((b) => b.toString(16).padStart(2, '0')).join('')
|
|
31
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The key a schema element has when its file gives none (story 161), from
|
|
36
|
+
* what it names: `derivedKey('type', 'document', 'homePage')`,
|
|
37
|
+
* `derivedKey(typeKey, 'title')` for a property. The parts are joined with a
|
|
38
|
+
* NUL, so no two lists of parts collide. Pure.
|
|
39
|
+
*/
|
|
40
|
+
export const derivedKey = (...parts: readonly string[]): string =>
|
|
41
|
+
uuidV5(parts.map((part) => part.toLowerCase()).join('\0'), SCHEMA_NAMESPACE)
|