nish 0.39.0 → 0.41.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.
Files changed (81) hide show
  1. package/README.md +190 -555
  2. package/docs/AI.md +112 -33
  3. package/docs/INSTALL.md +79 -133
  4. package/llms.txt +2 -9
  5. package/package.json +17 -19
  6. package/runtime/nish.d.ts +21 -17
  7. package/runtime/nish.h +10 -0
  8. package/runtime/page-frame.d.ts +123 -0
  9. package/runtime/page-frame.mjs +736 -0
  10. package/runtime/page-game.d.ts +132 -0
  11. package/runtime/page-game.mjs +738 -0
  12. package/runtime/runtime-aes.c +85 -8
  13. package/runtime/runtime-bytes.c +88 -4
  14. package/runtime/runtime-net.c +32 -0
  15. package/runtime/runtime-sandbox.c +55 -28
  16. package/runtime/runtime-uring.c +1194 -0
  17. package/runtime/runtime-wasm.c +422 -0
  18. package/runtime/vite.mjs +1 -1
  19. package/scripts/build.sh +22 -6
  20. package/std/README.md +74 -87
  21. package/std/array.ts +36 -3
  22. package/std/collections.ts +51 -0
  23. package/std/crypto/hmac.ts +37 -10
  24. package/std/crypto/p256.ts +1388 -2664
  25. package/std/crypto/pki.ts +116 -4
  26. package/std/crypto/sha256.ts +34 -6
  27. package/std/crypto/x509.ts +7 -0
  28. package/std/host/game.ts +273 -0
  29. package/std/input.ts +56 -0
  30. package/std/json-tokenizer.ts +325 -112
  31. package/std/json-typed.ts +43 -8
  32. package/std/json-value.ts +144 -3
  33. package/std/json.ts +63 -8
  34. package/std/net/fetch.ts +22 -4
  35. package/std/net/http-server.ts +3382 -0
  36. package/std/net/http1-client-codec.ts +6 -2
  37. package/std/net/http1-codec.ts +137 -12
  38. package/std/net/http1-server.ts +83 -29
  39. package/std/net/http2.ts +47 -14
  40. package/std/net/http3-server.ts +29 -3
  41. package/std/net/http3.ts +180 -7
  42. package/std/net/prefork.ts +49 -13
  43. package/std/net/quic-recovery.ts +240 -9
  44. package/std/net/quic-stream.ts +21 -1
  45. package/std/net/quic.ts +98 -4
  46. package/std/net/router.ts +378 -31
  47. package/std/net/serve.ts +873 -65
  48. package/std/net/tls/verify.ts +19 -1
  49. package/std/net/url.ts +83 -35
  50. package/std/net/websocket.ts +74 -1
  51. package/std/net/webtransport.ts +7 -1
  52. package/std/raster.ts +837 -0
  53. package/std/release.ts +593 -0
  54. package/std/synth.ts +143 -0
  55. package/std/temporal.ts +3091 -0
  56. package/std/threads.ts +298 -0
  57. package/std/yaml-json.ts +368 -28
  58. package/std/yaml.ts +122 -27
  59. package/scripts/bootstrap.sh +0 -375
  60. package/scripts/build-id-smoke.sh +0 -28
  61. package/scripts/changelog-gen.mjs +0 -649
  62. package/scripts/changelog-section.sh +0 -28
  63. package/scripts/check-hardening.mjs +0 -202
  64. package/scripts/ci-profile.mjs +0 -200
  65. package/scripts/codes-registry.js +0 -85
  66. package/scripts/fuzz.sh +0 -280
  67. package/scripts/gen-diagnostic-codes.mjs +0 -243
  68. package/scripts/gen-json-pow5.mjs +0 -130
  69. package/scripts/gen-p256-table.mjs +0 -177
  70. package/scripts/gen-pow10-table.py +0 -80
  71. package/scripts/gen-regex-unicode.mjs +0 -822
  72. package/scripts/gen-union-scale.mjs +0 -294
  73. package/scripts/nish-compiler.sh +0 -19
  74. package/scripts/platform-package.mjs +0 -93
  75. package/scripts/security-scope.mjs +0 -139
  76. package/scripts/shard-costs.mjs +0 -429
  77. package/scripts/size-report.sh +0 -84
  78. package/scripts/smoke.sh +0 -105
  79. package/scripts/std-module-paths.mjs +0 -25
  80. package/scripts/undocumented.txt +0 -14
  81. package/scripts/verify-binaries.sh +0 -214
@@ -1,294 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Write a discriminated union at typescript-go scale: the shape of its AST
4
- * node data, with as many variants as asked.
5
- *
6
- * node scripts/gen-union-scale.mjs tests/cases/union_scale.ts, 200 variants
7
- * node scripts/gen-union-scale.mjs --variants N N variants instead
8
- * node scripts/gen-union-scale.mjs --nodes N build N nodes instead of 4000 (for timing the walk)
9
- * node scripts/gen-union-scale.mjs --out FILE write FILE instead ("-" is stdout)
10
- * node scripts/gen-union-scale.mjs --check exit 1 if the case is not what this writes
11
- *
12
- * The program is an `enum Kind` of N members; N classes, each tagged
13
- * `readonly kind: Kind.X = Kind.X` and carrying 2 to 6 further fields (an
14
- * `i32`, a `string`, a `Node | null` child, a `Node[]` of children); `type
15
- * Node` over all of them; a builder that makes a tree of a few thousand nodes
16
- * from a fixed seed; a `switch (n.kind)` over every variant that reads the
17
- * variant's own fields; a forEachChild-style walk; and accessors for the two
18
- * fields many variants share. Its checksum stays below 2^31 at every step, so
19
- * Node prints what the native binary prints. The case's purpose is the
20
- * compile time and the IR of a 200-way union (`tests/cases/union_scale.ts`),
21
- * so the output is deterministic and the generator is the only author: edit
22
- * this file, never the case.
23
- */
24
- import { readFileSync, writeFileSync } from "node:fs"
25
- import { dirname, join } from "node:path"
26
- import { fileURLToPath } from "node:url"
27
-
28
- const root = join(dirname(fileURLToPath(import.meta.url)), "..")
29
- const args = process.argv.slice(2)
30
- const flag = (option) => {
31
- const at = args.indexOf(option)
32
- return at < 0 ? null : args[at + 1]
33
- }
34
- const variants = Number(flag("--variants") ?? 200)
35
- const nodes = Number(flag("--nodes") ?? 4000)
36
- const out = flag("--out") ?? join(root, "tests/cases/union_scale.ts")
37
- if (!Number.isInteger(variants) || variants < 2 || !Number.isInteger(nodes) || nodes < 1) {
38
- console.error("gen-union-scale: --variants takes an integer of at least 2, --nodes one of at least 1")
39
- process.exit(2)
40
- }
41
-
42
- // Field kinds, in the order a variant's fields cycle through them.
43
- const I32 = 0
44
- const STR = 1
45
- const CHILD = 2
46
- const LIST = 3
47
-
48
- /** The fields after the tag of variant `i`: 2 to 6, a fixed mix of kinds. */
49
- const fieldsOf = (i) => {
50
- const count = 2 + ((i * 7) % 5)
51
- const fields = []
52
- // `flags` and `name` are the fields many variants share, as typescript-go's
53
- // node data shares `Flags` and `Name()`: an accessor switches over them.
54
- if (i % 4 === 0) {
55
- fields.push({ name: "flags", kind: I32 })
56
- }
57
- if (i % 3 === 0) {
58
- fields.push({ name: "name", kind: STR })
59
- }
60
- for (let j = 0; fields.length < count; j++) {
61
- const kind = (i * 3 + j) % 4
62
- const prefix = ["v", "s", "c", "cs"][kind]
63
- fields.push({ name: `${prefix}${j}`, kind })
64
- }
65
- // Laid out with no padding: one `i32` beside the tag fills its word, then
66
- // the pointers, then the other `i32`s.
67
- const ints = fields.filter((f) => f.kind === I32)
68
- const refs = fields.filter((f) => f.kind !== I32)
69
- return [...ints.slice(0, 1), ...refs, ...ints.slice(1)]
70
- }
71
-
72
- const typeOf = (kind) => ["i32", "string", "Node | null", "Node[]"][kind]
73
-
74
- const lines = []
75
- const emit = (s = "") => lines.push(s)
76
- const name = (i) => `N${i}`
77
- const tag = (i) => `K${i}`
78
- const all = Array.from({ length: variants }, (_, i) => {
79
- const fields = fieldsOf(i)
80
- return { i, fields, kids: fields.filter((f) => f.kind === CHILD || f.kind === LIST) }
81
- })
82
-
83
- /** The body of a walk's case: `call(child, state)` on every child and list element. */
84
- const emitKids = (kids, call, state) => {
85
- for (const f of kids) {
86
- if (f.kind === CHILD) {
87
- emit(` const ${f.name} = n.${f.name};`)
88
- emit(` if (${f.name} !== null) {`)
89
- emit(` ${call}(${f.name}, ${state});`)
90
- emit(" }")
91
- } else {
92
- emit(` for (const c of n.${f.name}) {`)
93
- emit(` ${call}(c, ${state});`)
94
- emit(" }")
95
- }
96
- }
97
- }
98
-
99
- emit("// GENERATED by scripts/gen-union-scale.mjs: edit the generator, never this file.")
100
- emit(`// A discriminated union of ${variants} variants shaped like typescript-go's node`)
101
- emit("// data: each class tagged by its first field, with 2 to 6 fields of scalars,")
102
- emit("// strings, children and lists of children; a tree built from a fixed seed; a")
103
- emit("// `switch (n.kind)` over every variant that reads its own fields; a")
104
- emit("// forEachChild-style walk; and accessors for the fields many variants share.")
105
- emit("enum Kind {")
106
- for (const { i } of all) {
107
- emit(` ${tag(i)},`)
108
- }
109
- emit("}")
110
- emit()
111
- for (const { i, fields } of all) {
112
- emit(`class ${name(i)} {`)
113
- emit(` readonly kind: Kind.${tag(i)} = Kind.${tag(i)};`)
114
- for (const f of fields) {
115
- emit(` ${f.name}: ${typeOf(f.kind)};`)
116
- }
117
- emit(` constructor(${fields.map((f) => `${f.name}: ${typeOf(f.kind)}`).join(", ")}) {`)
118
- for (const f of fields) {
119
- emit(` this.${f.name} = ${f.name};`)
120
- }
121
- emit(" }")
122
- emit("}")
123
- emit()
124
- }
125
- emit("type Node =")
126
- // One variant a line, the `|` trailing: the parser takes no leading `|`.
127
- for (const { i } of all) {
128
- emit(` ${name(i)}${i === variants - 1 ? ";" : " |"}`)
129
- }
130
- emit()
131
-
132
- // The builder: a linear congruential seed and a budget of nodes left.
133
- emit("class Builder {")
134
- emit(" left: i32;")
135
- emit(" seed: i32 = 7;")
136
- emit(" constructor(left: i32) {")
137
- emit(" this.left = left;")
138
- emit(" }")
139
- emit()
140
- emit(" next(): i32 {")
141
- // A full-period generator mod 2^16 (a - 1 divisible by 4, c odd) whose low
142
- // bits are dropped, because they cycle with a short period.
143
- emit(" this.seed = (this.seed * 1105 + 12345) % 65536;")
144
- emit(" return this.seed >> 4;")
145
- emit(" }")
146
- emit("}")
147
- emit()
148
- emit("const child = (b: Builder, depth: i32): Node | null =>")
149
- emit(" depth <= 0 || b.left <= 0 ? null : build(b, depth - 1);")
150
- emit()
151
- emit("const children = (b: Builder, depth: i32): Node[] => {")
152
- emit(" const out: Node[] = [];")
153
- emit(" if (depth > 0) {")
154
- emit(" const n = b.next() % 4;")
155
- emit(" for (let i = 0; i < n && b.left > 0; i++) {")
156
- emit(" out.push(build(b, depth - 1));")
157
- emit(" }")
158
- emit(" }")
159
- emit(" return out;")
160
- emit("};")
161
- emit()
162
- const arg = (f) => {
163
- switch (f.kind) {
164
- case I32:
165
- return "b.next()"
166
- case STR:
167
- // biome-ignore lint/suspicious/noTemplateCurlyInString: Nish source text; the template is the program's
168
- return "`s${b.next() % 100}`"
169
- case CHILD:
170
- return "child(b, depth)"
171
- default:
172
- return "children(b, depth)"
173
- }
174
- }
175
- emit("const build = (b: Builder, depth: i32): Node => {")
176
- emit(" b.left -= 1;")
177
- emit(` switch (b.next() % ${variants}) {`)
178
- for (const { i, fields } of all) {
179
- emit(i === variants - 1 ? " default:" : ` case ${i}:`)
180
- emit(` return new ${name(i)}(${fields.map(arg).join(", ")});`)
181
- }
182
- emit(" }")
183
- emit("};")
184
- emit()
185
-
186
- // The switch over every variant, reading the variant's own scalars.
187
- const scalar = (f) => (f.kind === I32 ? `n.${f.name}` : `n.${f.name}.length`)
188
- emit("const score = (n: Node): i32 => {")
189
- emit(" switch (n.kind) {")
190
- for (const { i, fields } of all) {
191
- const terms = fields.filter((f) => f.kind === I32 || f.kind === STR).map(scalar)
192
- emit(` case Kind.${tag(i)}:`)
193
- emit(` return ${[String(i), ...terms].join(" + ")};`)
194
- }
195
- emit(" }")
196
- emit("};")
197
- emit()
198
-
199
- // forEachChild: every child field and every list, per variant.
200
- emit("class Walk {")
201
- emit(" count: i32 = 0;")
202
- emit(" sum: i32 = 0;")
203
- emit("}")
204
- emit()
205
- emit("const visit = (n: Node, w: Walk): void => {")
206
- emit(" w.count += 1;")
207
- emit(" w.sum = (w.sum * 31 + score(n)) % 1000003;")
208
- emit(" forEachChild(n, w);")
209
- emit("};")
210
- emit()
211
- emit("const forEachChild = (n: Node, w: Walk): void => {")
212
- emit(" switch (n.kind) {")
213
- for (const { i, kids } of all) {
214
- emit(` case Kind.${tag(i)}: {`)
215
- emitKids(kids, "visit", "w")
216
- emit(" break;")
217
- emit(" }")
218
- }
219
- emit(" }")
220
- emit("};")
221
- emit()
222
-
223
- // Accessors over the fields many variants share; every other variant answers
224
- // the default, as typescript-go's `Name()` answers nil.
225
- const accessor = (fn, field, type, none) => {
226
- emit(`const ${fn} = (n: Node): ${type} => {`)
227
- emit(" switch (n.kind) {")
228
- for (const { i, fields } of all) {
229
- if (fields.some((f) => f.name === field)) {
230
- emit(` case Kind.${tag(i)}:`)
231
- emit(` return n.${field};`)
232
- }
233
- }
234
- emit(" default:")
235
- emit(` return ${none};`)
236
- emit(" }")
237
- emit("};")
238
- emit()
239
- }
240
- accessor("flagsOf", "flags", "i32", "0")
241
- accessor("nameOf", "name", "string", '""')
242
-
243
- emit("class Tally {")
244
- emit(" flags: i32 = 0;")
245
- emit(" names: i32 = 0;")
246
- emit("}")
247
- emit()
248
- emit("const tally = (n: Node, t: Tally): void => {")
249
- emit(" t.flags = (t.flags + flagsOf(n)) % 1000003;")
250
- emit(" t.names += nameOf(n).length;")
251
- emit(" switch (n.kind) {")
252
- for (const { i, kids } of all) {
253
- if (kids.length === 0) {
254
- continue
255
- }
256
- emit(` case Kind.${tag(i)}: {`)
257
- emitKids(kids, "tally", "t")
258
- emit(" break;")
259
- emit(" }")
260
- }
261
- emit(" default:")
262
- emit(" break;")
263
- emit(" }")
264
- emit("};")
265
- emit()
266
- emit("export const main = (): void => {")
267
- emit(` const b = new Builder(${nodes});`)
268
- emit(" const roots: Node[] = [];")
269
- emit(" while (b.left > 0) {")
270
- emit(" roots.push(build(b, 8));")
271
- emit(" }")
272
- emit(" const w = new Walk();")
273
- emit(" const t = new Tally();")
274
- emit(" for (const r of roots) {")
275
- emit(" visit(r, w);")
276
- emit(" tally(r, t);")
277
- emit(" }")
278
- // biome-ignore lint/suspicious/noTemplateCurlyInString: Nish source text; the template is the program's
279
- emit(" console.log(`roots ${roots.length} nodes ${w.count} checksum ${w.sum}`);")
280
- // biome-ignore lint/suspicious/noTemplateCurlyInString: Nish source text; the template is the program's
281
- emit(" console.log(`flags ${t.flags} names ${t.names}`);")
282
- emit("};")
283
-
284
- const text = `${lines.join("\n")}\n`
285
- if (args.includes("--check")) {
286
- if (readFileSync(out, "utf8") !== text) {
287
- console.error(`gen-union-scale: ${out} is not what the generator writes; rerun it`)
288
- process.exit(1)
289
- }
290
- } else if (out === "-") {
291
- process.stdout.write(text)
292
- } else {
293
- writeFileSync(out, text)
294
- }
@@ -1,19 +0,0 @@
1
- # shellcheck shell=bash
2
- # Sourced, not run: how a shell script turns a compiler path into a command.
3
- #
4
- # . scripts/nish-compiler.sh
5
- # nish_compiler "${NISH:-build/nish}"
6
- # "${compiler[@]}" program.ts -o out.ll
7
- #
8
- # A Node entry point (.js, .mjs, .cjs) is run under node and anything else --
9
- # a native `nish` -- directly. That is the rule `NISH_BOOTSTRAP` follows in
10
- # scripts/bootstrap.sh and tests/self/seed.js follows as `NODE_ENTRY`, so one
11
- # path names a compiler the same way to every tool. Bash, because the answer is
12
- # an array: a compiler path with a space in it stays one word.
13
- # shellcheck disable=SC2034 # `compiler` is the answer, read by the script that sourced this
14
- nish_compiler() {
15
- case "$1" in
16
- *.js | *.mjs | *.cjs) compiler=(node "$1") ;;
17
- *) compiler=("$1") ;;
18
- esac
19
- }
@@ -1,93 +0,0 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Turn a staged release directory into a publishable platform package.
4
- *
5
- * node scripts/platform-package.mjs <stage-dir> <asset> [--version <v>]
6
- *
7
- * `release.yml`'s `binaries` job already stages exactly what one of these
8
- * needs, because the release tarball and the npm package want the same thing:
9
- * `bin/nish` beside the `runtime/` and `scripts/` it resolves one level up
10
- * from itself. So this script adds a `package.json` to that directory and
11
- * changes nothing else — `npm publish <stage-dir>` then uploads the same bytes
12
- * the tarball carries, built and smoke-tested by the steps above it.
13
- *
14
- * The package declares `os` and `cpu`, which is the whole mechanism: it is an
15
- * `optionalDependencies` entry of the main package, so npm installs the one
16
- * that matches this machine and skips the others without failing. Nothing here
17
- * is compiled on the way in.
18
- *
19
- * It deliberately declares no `bin`. The `nish` command belongs to the main
20
- * package's launcher, which is the only thing that knows which platform's
21
- * binary to hand over to -- and, since 0.6.0, the only thing that knows how to
22
- * say so when none was installed; two packages claiming one command name would
23
- * leave which binary wins up to npm's link order.
24
- */
25
- import fs from "node:fs"
26
- import path from "node:path"
27
- import { platformPackageName, targetForAsset } from "../bin/packaging.js"
28
-
29
- const args = process.argv.slice(2)
30
- const flag = (name) => {
31
- const i = args.indexOf(name)
32
- return i < 0 ? null : args[i + 1]
33
- }
34
- const positional = args.filter((a, i) => !a.startsWith("--") && !(args[i - 1] ?? "").startsWith("--"))
35
- const [stageDir, asset] = positional
36
-
37
- if (stageDir === undefined || asset === undefined) {
38
- console.error("usage: platform-package.mjs <stage-dir> <asset> [--version <v>]")
39
- process.exit(2)
40
- }
41
-
42
- const root = path.resolve(import.meta.dirname, "..")
43
- const main = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8"))
44
- const version = flag("--version") ?? main.version
45
-
46
- const target = targetForAsset(asset)
47
- if (target === null) {
48
- console.error(`platform-package.mjs: ${asset} is not a platform this project builds for`)
49
- process.exit(1)
50
- }
51
-
52
- const binary = path.join(stageDir, "bin", "nish")
53
- if (!fs.existsSync(binary)) {
54
- console.error(`platform-package.mjs: ${binary} does not exist; stage the binary before packaging it`)
55
- process.exit(1)
56
- }
57
-
58
- const pretty = { linux: "Linux", darwin: "macOS" }[target.os] ?? target.os
59
- const chip = { x64: "x86_64", arm64: "ARM64" }[target.cpu] ?? target.cpu
60
-
61
- const manifest = {
62
- name: platformPackageName(asset),
63
- version,
64
- description: `The ${main.name} native compiler for ${pretty} on ${chip}`,
65
- license: main.license,
66
- os: [target.os],
67
- cpu: [target.cpu],
68
- // `./package.json` is how the launcher finds this package: it resolves the
69
- // manifest and joins `bin/nish` to its directory. Everything else is read by
70
- // the binary itself, relative to its own path, never through a specifier.
71
- exports: { "./package.json": "./package.json" },
72
- // `std` is in this list for the reason `runtime` is, and it was missing until
73
- // 2026-09-20: the compiler resolves a `nish/<module>` specifier against its
74
- // own package root, so a platform package without it answers every one of
75
- // them with ``Module `nish/text` is not part of the standard library``, while
76
- // naming `text` among the modules it has. The message is the static list of
77
- // module names; what is absent is the file. Nothing caught it because every
78
- // program the release smoke-tests imports by relative path or not at all,
79
- // and because the pack-and-install round trip in `tests/run.js` packs the
80
- // MAIN package, which has carried `std` in its own `files` all along --
81
- // so the fallback to `dist/` worked and the path a user actually gets did
82
- // not. See docs/wp12-release.md and wp19 §5a.
83
- // `docs` carries `AI.md`, which `nish --help` names to a coding agent by its
84
- // absolute path, found under this package rather than the main one.
85
- files: ["bin", "runtime", "scripts", "std", "docs", "LICENSE", "INSTALL.md"],
86
- repository: main.repository,
87
- bugs: main.bugs,
88
- homepage: main.homepage,
89
- }
90
-
91
- const out = path.join(stageDir, "package.json")
92
- fs.writeFileSync(out, `${JSON.stringify(manifest, null, 2)}\n`)
93
- console.log(`${manifest.name}@${version} (os ${target.os}, cpu ${target.cpu}) -> ${out}`)
@@ -1,139 +0,0 @@
1
- #!/usr/bin/env node
2
- // Every file of the attacker-facing surface is named in a security record's
3
- // scope, or listed in the index's "Not yet audited" table with a reason.
4
- //
5
- // node scripts/security-scope.mjs check the tree (CI + npm test)
6
- // node scripts/security-scope.mjs --root D check the tree under D instead
7
- //
8
- // The surface is `std/net/**/*.ts`, `std/crypto/*.ts` and `runtime/*.c`. A file
9
- // is in scope when its repository path, or a glob that matches it, is
10
- // backticked in the first cell of a row of a record's `## Scope` table, or in
11
- // the Scope column of `docs/security/README.md`'s Areas table. A row of the
12
- // "Not yet audited" table is refused when its file is covered or gone, so the
13
- // list only shrinks. The rule it checks is CLAUDE.md's: new attacker-facing
14
- // code ships with its record.
15
- import fs from "node:fs"
16
- import path from "node:path"
17
-
18
- const args = process.argv.slice(2)
19
- const at = args.indexOf("--root")
20
- const root = at >= 0 ? path.resolve(args[at + 1]) : path.resolve(import.meta.dirname, "..")
21
- const securityDir = path.join(root, "docs", "security")
22
-
23
- const walk = (dir, keep) => {
24
- if (!fs.existsSync(path.join(root, dir))) {
25
- return []
26
- }
27
- const out = []
28
- for (const entry of fs.readdirSync(path.join(root, dir), { withFileTypes: true })) {
29
- const rel = `${dir}/${entry.name}`
30
- if (entry.isDirectory()) {
31
- out.push(...walk(rel, keep))
32
- } else if (keep(entry.name)) {
33
- out.push(rel)
34
- }
35
- }
36
- return out
37
- }
38
-
39
- const surface = [
40
- ...walk("std/net", (n) => n.endsWith(".ts")),
41
- ...fs
42
- .readdirSync(path.join(root, "std", "crypto"))
43
- .filter((n) => n.endsWith(".ts"))
44
- .map((n) => `std/crypto/${n}`),
45
- ...fs
46
- .readdirSync(path.join(root, "runtime"))
47
- .filter((n) => n.endsWith(".c"))
48
- .map((n) => `runtime/${n}`),
49
- ].sort()
50
-
51
- const ticked = (text) => [...text.matchAll(/`([^`]+)`/g)].map((m) => m[1].trim())
52
- const cells = (line) => line.split("|").slice(1, -1)
53
-
54
- // A glob here is a path with `*` in its last part, nothing more.
55
- const matches = (pattern, file) => {
56
- if (!pattern.includes("*")) {
57
- return pattern === file
58
- }
59
- const re = new RegExp(`^${pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, "[^/]*")}$`)
60
- return re.test(file)
61
- }
62
-
63
- const patterns = new Set()
64
- const index = fs.readFileSync(path.join(securityDir, "README.md"), "utf8").split("\n")
65
- for (const name of fs.readdirSync(securityDir)) {
66
- if (!name.endsWith(".md") || name === "README.md") {
67
- continue
68
- }
69
- const lines = fs.readFileSync(path.join(securityDir, name), "utf8").split("\n")
70
- let inScope = false
71
- for (const line of lines) {
72
- if (line.startsWith("## ")) {
73
- inScope = line.trim() === "## Scope"
74
- } else if (inScope && line.startsWith("|")) {
75
- for (const t of ticked(cells(line)[0] ?? "")) {
76
- patterns.add(t)
77
- }
78
- }
79
- }
80
- }
81
-
82
- // The Areas table: its Scope column is the third. The "Not yet audited" table:
83
- // file, then reason.
84
- const pending = new Map()
85
- let table = ""
86
- for (const line of index) {
87
- if (line.startsWith("## ")) {
88
- table = line.slice(3).trim()
89
- }
90
- if (
91
- !line.startsWith("|") ||
92
- line.startsWith("| ---") ||
93
- line.startsWith("| Area") ||
94
- line.startsWith("| File")
95
- ) {
96
- continue
97
- }
98
- const row = cells(line)
99
- if (table === "Areas") {
100
- for (const t of ticked(row[2] ?? "")) {
101
- patterns.add(t)
102
- }
103
- }
104
- if (table === "Not yet audited") {
105
- const [file] = ticked(row[0] ?? "")
106
- if (file) {
107
- pending.set(file, (row[1] ?? "").trim())
108
- }
109
- }
110
- }
111
-
112
- const problems = []
113
- const covered = (file) => [...patterns].some((p) => matches(p, file))
114
- for (const file of surface) {
115
- if (covered(file)) {
116
- if (pending.has(file)) {
117
- problems.push(`${file}: in a record's scope, so its "Not yet audited" row goes`)
118
- }
119
- } else if (!pending.has(file)) {
120
- problems.push(`${file}: in no record's scope; it ships with its docs/security/ record (CLAUDE.md)`)
121
- }
122
- }
123
- for (const [file, reason] of pending) {
124
- if (!surface.includes(file)) {
125
- problems.push(`${file}: listed under "Not yet audited" but not on the surface`)
126
- } else if (reason === "") {
127
- problems.push(`${file}: its "Not yet audited" row gives no reason`)
128
- }
129
- }
130
-
131
- if (problems.length > 0) {
132
- for (const p of problems) {
133
- console.error(`security-scope: ${p}`)
134
- }
135
- process.exit(1)
136
- }
137
- console.log(
138
- `security-scope: ${surface.length} files, ${surface.length - pending.size} in a record, ${pending.size} not yet audited`
139
- )