@0001-labs/idv1 3.0.1 → 3.2.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 +2 -1
- package/bin/idv1.mjs +13 -2
- package/package.json +1 -1
- package/src/identifier.ts +24 -0
- package/src/idv1.ts +28 -1
package/README.md
CHANGED
|
@@ -48,9 +48,10 @@ NNsvLL Service
|
|
|
48
48
|
npx idv1 directory # the vocabulary, and whether it is healthy
|
|
49
49
|
npx idv1 health # exits 1 with each problem
|
|
50
50
|
npx idv1 resolve co12Ca # the shape, taxonomy and namespace a code reads as
|
|
51
|
+
npx idv1 hardcoded src # exits 1 with each quoted -NS-code written into source
|
|
51
52
|
```
|
|
52
53
|
|
|
53
|
-
The same operations are exported: `readDirectory`, `writeDirectory`, `directoryHealth`, `resolveCode`, `taxonomyOf`, `describeDirectory` and `
|
|
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.
|
|
54
55
|
|
|
55
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.
|
|
56
57
|
|
package/bin/idv1.mjs
CHANGED
|
@@ -1,10 +1,21 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { existsSync, readFileSync } from 'node:fs'
|
|
2
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
|
|
3
3
|
import { dirname, join, resolve } from 'node:path'
|
|
4
4
|
import '../register.mjs'
|
|
5
|
-
const { runDirectoryCommand } = await import('../src/idv1.ts')
|
|
5
|
+
const { runDirectoryCommand, hardcodedCodes } = await import('../src/idv1.ts')
|
|
6
6
|
|
|
7
7
|
const [command, ...args] = process.argv.slice(2)
|
|
8
|
+
if (command === 'hardcoded') {
|
|
9
|
+
const files = {}
|
|
10
|
+
const walk = path => {
|
|
11
|
+
if (statSync(path).isDirectory()) { for (const name of readdirSync(path)) if (!name.startsWith('.') && !['node_modules', 'dist', 'build'].includes(name)) walk(join(path, name)) }
|
|
12
|
+
else if (/\.(c|m)?(j|t)sx?$|\.swift$/.test(path)) files[path] = readFileSync(path, 'utf8')
|
|
13
|
+
}
|
|
14
|
+
for (const path of args.length ? args : ['.']) walk(path)
|
|
15
|
+
const found = hardcodedCodes(files)
|
|
16
|
+
console.log(found.length ? found.map(({ path, line, code }) => `${path}:${line} ${code}`).join('\n') : 'no codes written into source')
|
|
17
|
+
process.exit(found.length ? 1 : 0)
|
|
18
|
+
}
|
|
8
19
|
let dir = resolve(process.cwd())
|
|
9
20
|
while (!existsSync(join(dir, '.id')) && dirname(dir) !== dir) dir = dirname(dir)
|
|
10
21
|
const file = join(dir, '.id')
|
package/package.json
CHANGED
package/src/identifier.ts
CHANGED
|
@@ -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 = parseCode(slots.replace(/N/g, '1').replace(/[LXOP]/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
|
@@ -47,6 +47,33 @@ export function directoryHealth(directory: Directory): string[] {
|
|
|
47
47
|
return problems
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
export type CodeRecord = { key: string; code: string; namespace?: string; pattern?: string }
|
|
51
|
+
|
|
52
|
+
export function recordHealth(directory: Directory, records: CodeRecord[], released: CodeRecord[] = []): string[] {
|
|
53
|
+
const problems: string[] = []
|
|
54
|
+
const owners = new Map<string, string>()
|
|
55
|
+
const before = new Map(released.map(record => [record.key, record]))
|
|
56
|
+
for (const { key, code, namespace, pattern } of records) {
|
|
57
|
+
const qualified = `-${namespace ?? ''}-${code}`
|
|
58
|
+
if (!parseCode(code)) problems.push(`${key}: ${code} is not a code`)
|
|
59
|
+
else if (pattern && taxonomyOf(directory, code)?.pattern.replace(/\s/g, '') !== pattern.replace(/\s/g, '')) problems.push(`${key}: ${code} does not fit ${pattern}`)
|
|
60
|
+
if (namespace && !directory.namespaces.some(entry => entry.shorthand === namespace)) problems.push(`${key}: namespace ${namespace} is not in .id`)
|
|
61
|
+
if (owners.has(qualified)) problems.push(`${key}: ${qualified} already belongs to ${owners.get(qualified)}`)
|
|
62
|
+
owners.set(qualified, key)
|
|
63
|
+
const old = before.get(key)
|
|
64
|
+
if (old && (old.code !== code || (old.namespace ?? '') !== (namespace ?? ''))) problems.push(`${key}: released as -${old.namespace ?? ''}-${old.code}, now ${qualified}; a code never changes`)
|
|
65
|
+
}
|
|
66
|
+
return problems
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const QUOTED_CODE = /(['"`])-([A-Z]{2})-([A-Za-z0-9]{6}(?:-[A-Za-z0-9]{6})*)\1/g
|
|
70
|
+
|
|
71
|
+
export function hardcodedCodes(files: Record<string, string>): { path: string; line: number; code: string }[] {
|
|
72
|
+
return Object.entries(files).flatMap(([path, text]) => [...text.matchAll(QUOTED_CODE)]
|
|
73
|
+
.filter(match => match[3].split(JUNCTION_SEPARATOR).every(part => parseCode(part)))
|
|
74
|
+
.map(match => ({ path, line: text.slice(0, match.index).split('\n').length, code: `-${match[2]}-${match[3]}` })))
|
|
75
|
+
}
|
|
76
|
+
|
|
50
77
|
export function resolveCode(directory: Directory, value: string): Resolved[] {
|
|
51
78
|
const qualified = /^-([A-Z]{2})-(.+)$/.exec(value)
|
|
52
79
|
return (qualified ? qualified[2] : value).split(JUNCTION_SEPARATOR).map(id => {
|
|
@@ -85,6 +112,6 @@ export function runDirectoryCommand(command: string | undefined, args: string[],
|
|
|
85
112
|
case 'health': return { code: problems.length ? 1 : 0, out: problems.length ? problems.join('\n') : 'healthy' }
|
|
86
113
|
case 'resolve': return args.length ? { code: 0, out: args.map(value => describeResolved(directory, resolveCode(directory, value))).join('\n\n') } : { code: 2, out: 'idv1 resolve <code | junction | -NS-code>' }
|
|
87
114
|
case 'directory': case undefined: return { code: 0, out: describeDirectory(directory, problems) }
|
|
88
|
-
default: return { code: 2, out: 'idv1 directory | health | resolve <code>' }
|
|
115
|
+
default: return { code: 2, out: 'idv1 directory | health | resolve <code> | hardcoded [path…]' }
|
|
89
116
|
}
|
|
90
117
|
}
|