@0001-labs/idv1 3.1.0 → 4.0.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/README.md +13 -9
- package/package.json +1 -1
- package/src/identifier.ts +37 -13
- package/src/idv1.ts +3 -3
- package/src/namespace.ts +3 -5
- package/src/taxonomy.ts +2 -5
- package/src/util/capacity.ts +3 -3
package/README.md
CHANGED
|
@@ -7,29 +7,29 @@ npm install @0001-labs/idv1
|
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
```ts
|
|
10
|
-
import { codeCandidate, parseCode, type
|
|
10
|
+
import { codeCandidate, parseCode, type LLLLLLId } from '@0001-labs/idv1'
|
|
11
11
|
|
|
12
|
-
const candidate:
|
|
12
|
+
const candidate: LLLLLLId = codeCandidate('LLLLLL')
|
|
13
13
|
const parsed = parseCode(candidate)
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
`parseCode(unknown)` returns `{ shape, id }` with a shape-branded ID, or `null`. `codeShape` and `isCodeOf(shape, value)` use the same parser. The types are `LLNNNNId`, `
|
|
16
|
+
`parseCode(unknown)` returns `{ shape, id }` with a shape-branded ID, or `null`. `codeShape` and `isCodeOf(shape, value)` use the same parser. The types are `LLNNNNId`, `LLNNLLId`, `LLLLNNId`, `LLLLLLId` and `XXXXXXId`, also available as `Id<Shape>`. Validation establishes syntax, not an assigned record.
|
|
17
17
|
|
|
18
18
|
| Shape | Structure |
|
|
19
19
|
| --- | --- |
|
|
20
20
|
| `LLNNNN` | Two ASCII letters, four digits |
|
|
21
|
-
| `
|
|
21
|
+
| `LLNNLL` | Two letters, two digits, two letters |
|
|
22
22
|
| `LLLLNN` | Four ASCII letters, two digits |
|
|
23
|
-
| `
|
|
23
|
+
| `LLLLLL` | Six ASCII letters |
|
|
24
24
|
| `XXXXXX` | Every remaining exact six-character ASCII base62 string |
|
|
25
25
|
|
|
26
|
-
`codeCandidate(shape)` draws with Web Crypto.
|
|
26
|
+
`codeCandidate(shape)` draws with Web Crypto. All shapes accept an optional exact two-letter prefix, preserving case. Candidates obey word filters; `LLLLNN` also excludes blocked digit pairs. A bounded draw fails after 1,000 attempts. An injected `random` must return finite numbers in `[0, 1)`. Claim a candidate in its namespace and store it beside the record’s UUID. A code is a short handle; its pool can reset and reissue codes.
|
|
27
27
|
|
|
28
28
|
`ID_CAPACITY` is keyed by the same patterns and counts structural capacity before generation filters. `TOTAL_ID_CAPACITY` is their sum. Scan encoding keeps its checksum and payload format; `parseCode` reads the shape of a branded scan result. `isScanIdOf(shape, value)` checks one shape.
|
|
29
29
|
|
|
30
30
|
## The idv1 command
|
|
31
31
|
|
|
32
|
-
A codebase keeps its vocabulary in a `.id` file of plain lines. The first five name the shapes in pattern order (`LLNNNN`, `
|
|
32
|
+
A codebase keeps its vocabulary in a `.id` file of plain lines. The first five name the shapes in pattern order (`LLNNNN`, `LLNNLL`, `LLLLNN`, `LLLLLL`, `XXXXXX`); a line left as its pattern keeps no name, and a shape name is a label, not a taxonomy reservation. After them, two capitals and a name are a namespace, and a six-character pattern starting with one lowercase pair and a name is a taxonomy, which carries its shape. Blank lines and `#` comments are free. Codes themselves live with their records, in a column beside the database's own key, never in `.id`.
|
|
33
33
|
|
|
34
34
|
```text
|
|
35
35
|
User
|
|
@@ -51,7 +51,7 @@ npx idv1 resolve co12Ca # the shape, taxonomy and namespace a code reads as
|
|
|
51
51
|
npx idv1 hardcoded src # exits 1 with each quoted -NS-code written into source
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
-
The same operations are exported: `readDirectory`, `writeDirectory`, `directoryHealth`, `resolveCode`, `taxonomyOf`, `describeDirectory`, `runDirectoryCommand` and `hardcodedCodes`. `recordHealth(directory, records, released)` checks an app's coded records against its `.id`: each code parses, fits its taxonomy pattern, sits in a declared namespace and is unique, and a record's code matches the one it was released with. Run both before every release, so codes are read from records and never typed into code.
|
|
54
|
+
The same operations are exported: `readDirectory`, `writeDirectory`, `directoryHealth`, `resolveCode`, `taxonomyOf`, `describeDirectory`, `runDirectoryCommand` and `hardcodedCodes`. `recordHealth(directory, records, released)` checks an app's coded records against its `.id`: each code parses, fits its taxonomy pattern, sits in a declared namespace and is unique, and a record's code matches the one it was released with. Run both before every release, so codes are read from records and never typed into code. To give database rows codes, store each in a column beside the row's own key: `mintCode(pattern, { taken })` draws an unused code for a shape or taxonomy pattern (`XXerXX`, `doNNXX`; `at` fixes characters such as an operation pair), and `backfillCodes(rows, pattern, { taken })` codes the rows kept before the column existed.
|
|
55
55
|
|
|
56
56
|
The package ships TypeScript source. Bundlers and `tsx` read it directly. Plain Node 22.18 or later imports `@0001-labs/idv1/register` first, then dynamically imports the package.
|
|
57
57
|
|
|
@@ -65,8 +65,12 @@ Optional `collections` carry source records and lazy `load` functions. Records m
|
|
|
65
65
|
|
|
66
66
|
Release 3 keeps `.id` to vocabulary: `Directory` holds shapes, namespaces and taxonomies; `Part`, facts and `idv1 status` are gone, and `idv1 health` names every other line it finds. `readDirectory` still reads the JSON form.
|
|
67
67
|
|
|
68
|
-
Release 2 changes API names, not code grammar, scan payloads or capacity. Replace semantic shape arguments with patterns, `parseCode(...).kind` with `.shape`, and `codeKind` with `codeShape`. Use `codeCandidate(shape)` and `isCodeOf(shape, value)` in place of per-kind wrappers. Regex constants are pattern-named, such as `
|
|
68
|
+
Release 2 changes API names, not code grammar, scan payloads or capacity. Replace semantic shape arguments with patterns, `parseCode(...).kind` with `.shape`, and `codeKind` with `codeShape`. Use `codeCandidate(shape)` and `isCodeOf(shape, value)` in place of per-kind wrappers. Regex constants are pattern-named, such as `LLLLLL_CODE`; `blockedLLLLNNCode` replaces the semantic name of its filter. `.id` part and taxonomy `shape` values use patterns. Keep application words in the optional `shapes` map.
|
|
69
69
|
|
|
70
70
|
## Documentation
|
|
71
71
|
|
|
72
72
|
The [specification](https://idv1.0001.dev/docs/spec) owns the code design. [MIT license](LICENSE).
|
|
73
|
+
|
|
74
|
+
## Release 4
|
|
75
|
+
|
|
76
|
+
The five masks are `LLNNNN`, `LLNNLL`, `LLLLNN`, `LLLLLL` and the remaining `XXXXXX` codes. Taxonomy always starts at position zero. Namespace is external context, never inferred from letters inside a code. Upgrading requires reminting affected records and their references; existing applications stay on their pinned release until migrated. Total structural capacity remains 62⁶; a pool can reset and reissue short handles when exhausted. Keep durable record keys and allocation context separate from those handles.
|
package/package.json
CHANGED
package/src/identifier.ts
CHANGED
|
@@ -6,27 +6,27 @@ export const DIGITS = '0123456789'
|
|
|
6
6
|
export const ALPHABET = DIGITS + LETTERS
|
|
7
7
|
|
|
8
8
|
export const LLNNNN_CODE = /^[A-Za-z]{2}[0-9]{4}(?![\s\S])/
|
|
9
|
-
export const
|
|
9
|
+
export const LLNNLL_CODE = /^[A-Za-z]{2}[0-9]{2}[A-Za-z]{2}(?![\s\S])/
|
|
10
10
|
export const LLLLNN_CODE = /^[A-Za-z]{4}[0-9]{2}(?![\s\S])/
|
|
11
|
-
export const
|
|
11
|
+
export const LLLLLL_CODE = /^[A-Za-z]{6}(?![\s\S])/
|
|
12
12
|
export const ANY_CODE = /^[A-Za-z0-9]{6}(?![\s\S])/
|
|
13
|
-
export const XXXXXX_CODE = new RegExp(`^(?!${[LLNNNN_CODE,
|
|
13
|
+
export const XXXXXX_CODE = new RegExp(`^(?!${[LLNNNN_CODE, LLNNLL_CODE, LLLLNN_CODE, LLLLLL_CODE].map(pattern => pattern.source.slice(1)).join('|')})[A-Za-z0-9]{6}(?![\\s\\S])`)
|
|
14
14
|
|
|
15
|
-
export type CodeShape = 'LLNNNN' | '
|
|
15
|
+
export type CodeShape = 'LLNNNN' | 'LLNNLL' | 'LLLLNN' | 'LLLLLL' | 'XXXXXX'
|
|
16
16
|
declare const idBrand: unique symbol
|
|
17
17
|
export type Id<K extends CodeShape = CodeShape> = string & { readonly [idBrand]: K }
|
|
18
18
|
export type LLNNNNId = Id<'LLNNNN'>
|
|
19
|
-
export type
|
|
19
|
+
export type LLNNLLId = Id<'LLNNLL'>
|
|
20
20
|
export type LLLLNNId = Id<'LLLLNN'>
|
|
21
|
-
export type
|
|
21
|
+
export type LLLLLLId = Id<'LLLLLL'>
|
|
22
22
|
export type XXXXXXId = Id<'XXXXXX'>
|
|
23
23
|
export type ParsedCode = { [K in CodeShape]: { shape: K; id: Id<K> } }[CodeShape]
|
|
24
24
|
|
|
25
|
-
export const CODE_PATTERN = { LLNNNN: LLNNNN_CODE,
|
|
25
|
+
export const CODE_PATTERN = { LLNNNN: LLNNNN_CODE, LLNNLL: LLNNLL_CODE, LLLLNN: LLLLNN_CODE, LLLLLL: LLLLLL_CODE, XXXXXX: XXXXXX_CODE } as const
|
|
26
26
|
|
|
27
27
|
export function parseCode(value: unknown): ParsedCode | null {
|
|
28
28
|
if (typeof value !== 'string' || !ANY_CODE.test(value)) return null
|
|
29
|
-
for (const shape of ['LLNNNN', '
|
|
29
|
+
for (const shape of ['LLNNNN', 'LLNNLL', 'LLLLNN', 'LLLLLL'] as const) {
|
|
30
30
|
if (CODE_PATTERN[shape].test(value)) return { shape, id: value } as ParsedCode
|
|
31
31
|
}
|
|
32
32
|
return { shape: 'XXXXXX', id: value as XXXXXXId }
|
|
@@ -71,17 +71,17 @@ export function nameLetters(name: string, email = ''): string | null {
|
|
|
71
71
|
export function codeCandidate<K extends CodeShape>(shape: K, options: { letters?: string | null; random?: Random } = {}): Id<K> {
|
|
72
72
|
const random = options.random ?? secureRandom
|
|
73
73
|
const prefix = options.letters ?? ''
|
|
74
|
-
if (prefix && (
|
|
75
|
-
throw new Error('A prefix must be exactly two ASCII letters
|
|
74
|
+
if (prefix && (!/^[A-Za-z]{2}(?![\s\S])/.test(prefix))) {
|
|
75
|
+
throw new Error('A prefix must be exactly two ASCII letters .')
|
|
76
76
|
}
|
|
77
77
|
for (let attempt = 0; attempt < 1000; attempt++) {
|
|
78
78
|
let candidate: string
|
|
79
79
|
switch (shape) {
|
|
80
80
|
case 'LLNNNN': candidate = (prefix || draw(LETTERS, 2, random)) + draw(DIGITS, 4, random); break
|
|
81
|
-
case '
|
|
81
|
+
case 'LLNNLL': candidate = (prefix || draw(LETTERS, 2, random)) + draw(DIGITS, 2, random) + draw(LETTERS, 2, random); break
|
|
82
82
|
case 'LLLLNN': candidate = (prefix || draw(LETTERS, 2, random)) + draw(LETTERS, 2, random) + draw(DIGITS, 2, random); break
|
|
83
|
-
case '
|
|
84
|
-
case 'XXXXXX': candidate = draw(ALPHABET, 6, random); break
|
|
83
|
+
case 'LLLLLL': candidate = (prefix || draw(LETTERS, 2, random)) + draw(LETTERS, 4, random); break
|
|
84
|
+
case 'XXXXXX': candidate = prefix ? prefix + draw(ALPHABET, 4, random) : draw(ALPHABET, 6, random); break
|
|
85
85
|
default: throw new Error('Unknown IDv1 shape.')
|
|
86
86
|
}
|
|
87
87
|
const parsed = parseCode(candidate)
|
|
@@ -91,6 +91,30 @@ export function codeCandidate<K extends CodeShape>(shape: K, options: { letters?
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
|
|
94
|
+
const SLOTS: Record<string, string> = { L: LETTERS, N: DIGITS, X: ALPHABET, O: CAPITALS, P: CAPITALS }
|
|
95
|
+
export type MintOptions = { taken?: (code: string) => boolean; random?: Random; at?: Record<number, string> }
|
|
96
|
+
|
|
97
|
+
export function mintCode(pattern: string, options: MintOptions = {}): string {
|
|
98
|
+
const random = options.random ?? secureRandom
|
|
99
|
+
const slots = pattern.replace(/\s/g, '')
|
|
100
|
+
const shape = slots.includes('X') ? 'XXXXXX' : parseCode(slots.replace(/N/g, '1').replace(/[LOP]/g, 'a'))?.shape
|
|
101
|
+
if (slots.length !== 6 || !shape) throw new Error(`${pattern} is not a six-character pattern.`)
|
|
102
|
+
for (let attempt = 0; attempt < 1000; attempt++) {
|
|
103
|
+
const candidate = [...slots].map((slot, index) => options.at?.[index] ?? (SLOTS[slot] ? pick(SLOTS[slot], random) : slot)).join('')
|
|
104
|
+
if (parseCode(candidate)?.shape === shape && !(shape === 'LLLLNN' ? blockedLLLLNNCode(candidate) : blockedCodeLetters(candidate)) && !options.taken?.(candidate)) return candidate
|
|
105
|
+
}
|
|
106
|
+
throw new Error(`No unused ${pattern} code could be drawn.`)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function backfillCodes<T>(rows: readonly T[], pattern: string, options: Omit<MintOptions, 'at'> & { at?: (row: T) => Record<number, string> | undefined } = {}): [T, string][] {
|
|
110
|
+
const minted = new Set<string>()
|
|
111
|
+
return rows.map(row => {
|
|
112
|
+
const code = mintCode(pattern, { random: options.random, at: options.at?.(row), taken: code => minted.has(code) || !!options.taken?.(code) })
|
|
113
|
+
minted.add(code)
|
|
114
|
+
return [row, code]
|
|
115
|
+
})
|
|
116
|
+
}
|
|
117
|
+
|
|
94
118
|
export function caseVariantsLLNNNN(code: string): string[] {
|
|
95
119
|
const [first, second] = [code[0], code[1]].map(letter => [...new Set([letter.toUpperCase(), letter.toLowerCase()])])
|
|
96
120
|
return first.flatMap(a => second.map(b => a + b + code.slice(2)))
|
package/src/idv1.ts
CHANGED
|
@@ -6,11 +6,11 @@ export type ShapeNames = Partial<Record<CodeShape, string>>
|
|
|
6
6
|
export type Directory = { shapes: ShapeNames; namespaces: Namespace[]; taxonomies: Taxonomy[]; unread?: string[] }
|
|
7
7
|
export type Resolved = { id: string; shape: CodeShape | null; namespace?: Namespace; taxonomy?: Taxonomy }
|
|
8
8
|
|
|
9
|
-
const SHAPES: CodeShape[] = ['LLNNNN', '
|
|
9
|
+
const SHAPES: CodeShape[] = ['LLNNNN', 'LLNNLL', 'LLLLNN', 'LLLLLL', 'XXXXXX']
|
|
10
10
|
const NAMESPACE_KEY = /^[A-Z]{2}$/
|
|
11
|
-
const TAXONOMY_KEY = /^
|
|
11
|
+
const TAXONOMY_KEY = /^[a-z]{2}[NLXOP]{4}$/
|
|
12
12
|
const list = <T>(value: unknown): T[] => (Array.isArray(value) ? value : []) as T[]
|
|
13
|
-
export const patternShape = (pattern: string) => parseCode(pattern.replace(/N/g, '1').replace(/[
|
|
13
|
+
export const patternShape = (pattern: string) => pattern.includes('X') ? 'XXXXXX' : parseCode(pattern.replace(/N/g, '1').replace(/[LOP]/g, 'a'))?.shape ?? null
|
|
14
14
|
|
|
15
15
|
export function readDirectory(text: string): Directory {
|
|
16
16
|
if (text.trimStart().startsWith('{')) {
|
package/src/namespace.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { parseCode } from './identifier'
|
|
2
|
-
|
|
3
1
|
export type Namespace = { shorthand: string; name: string }
|
|
4
2
|
|
|
5
|
-
export function namespaceOf({ namespaces }: { namespaces: Namespace[] },
|
|
6
|
-
const shorthand = qualified
|
|
7
|
-
return namespaces.find(namespace => namespace.shorthand === shorthand) ?? (namespaces.length === 1 ? namespaces[0] : undefined)
|
|
3
|
+
export function namespaceOf({ namespaces }: { namespaces: Namespace[] }, _id: string, qualified?: string): Namespace | undefined {
|
|
4
|
+
const shorthand = qualified
|
|
5
|
+
return namespaces.find(namespace => namespace.shorthand === shorthand) ?? (!qualified && namespaces.length === 1 ? namespaces[0] : undefined)
|
|
8
6
|
}
|
|
9
7
|
|
|
10
8
|
export function namespaceHealth(namespaces: Namespace[]): string[] {
|
package/src/taxonomy.ts
CHANGED
|
@@ -4,10 +4,7 @@ export type Taxonomy = { shorthand: string; name: string; shape: CodeShape; patt
|
|
|
4
4
|
|
|
5
5
|
export function taxonomyOf(directory: { taxonomies: Taxonomy[] }, id: string): Taxonomy | undefined {
|
|
6
6
|
const shape = parseCode(id)?.shape
|
|
7
|
-
return directory.taxonomies.find(taxonomy =>
|
|
8
|
-
const at = taxonomy.pattern.replace(/\s/g, '').indexOf(taxonomy.shorthand)
|
|
9
|
-
return taxonomy.shape === shape && at >= 0 && id.slice(at, at + 2) === taxonomy.shorthand
|
|
10
|
-
})
|
|
7
|
+
return directory.taxonomies.find(taxonomy => taxonomy.shape === shape && taxonomy.pattern.startsWith(taxonomy.shorthand) && id.startsWith(taxonomy.shorthand))
|
|
11
8
|
}
|
|
12
9
|
|
|
13
10
|
export function taxonomyHealth(taxonomies: Taxonomy[]): string[] {
|
|
@@ -17,7 +14,7 @@ export function taxonomyHealth(taxonomies: Taxonomy[]): string[] {
|
|
|
17
14
|
if (!name) problems.push(`Taxonomy ${shorthand} has no name`)
|
|
18
15
|
if (!Object.keys(CODE_PATTERN).includes(shape)) problems.push(`Taxonomy ${shorthand} has no shape`)
|
|
19
16
|
const compact = String(pattern ?? '').replace(/\s/g, '')
|
|
20
|
-
if (compact.length !== 6 || !compact.
|
|
17
|
+
if (compact.length !== 6 || !compact.startsWith(shorthand)) problems.push(`Taxonomy ${shorthand} has no six-character pattern starting with ${shorthand}`)
|
|
21
18
|
}
|
|
22
19
|
return problems
|
|
23
20
|
}
|
package/src/util/capacity.ts
CHANGED
|
@@ -2,11 +2,11 @@ const LETTERS = 52
|
|
|
2
2
|
const DIGITS = 10
|
|
3
3
|
export const TOTAL_ID_CAPACITY = (LETTERS + DIGITS) ** 6
|
|
4
4
|
const LLNNNN = LETTERS ** 2 * DIGITS ** 4
|
|
5
|
-
const
|
|
5
|
+
const LLNNLL = DIGITS ** 2 * LETTERS ** 4
|
|
6
6
|
const LLLLNN = LETTERS ** 4 * DIGITS ** 2
|
|
7
|
-
const
|
|
7
|
+
const LLLLLL = LETTERS ** 6
|
|
8
8
|
|
|
9
|
-
export const ID_CAPACITY = { LLNNNN,
|
|
9
|
+
export const ID_CAPACITY = { LLNNNN, LLNNLL, LLLLNN, LLLLLL, XXXXXX: TOTAL_ID_CAPACITY - LLNNNN - LLNNLL - LLLLNN - LLLLLL } as const
|
|
10
10
|
export type IdCapacityShape = keyof typeof ID_CAPACITY
|
|
11
11
|
|
|
12
12
|
export function idUsage(shape: IdCapacityShape, used: number): { used: number; capacity: number; left: number; percent: number } {
|