@crombolo/core 0.28.0 → 0.29.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/core",
3
- "version": "0.28.0",
3
+ "version": "0.29.1",
4
4
  "description": "Domain model, entity keys and shared contracts for crombolo. No I/O.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ export * from './documents.ts'
4
4
  export * from './form-submissions.ts'
5
5
  export * from './forms.ts'
6
6
  export * from './json-patch.ts'
7
+ export * from './keys.ts'
7
8
  export * from './notifications.ts'
8
9
  export * from './object-types.ts'
9
10
  export * from './paging.ts'
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)