@wildshard/shard-format 0.70.0 → 0.71.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 (63) hide show
  1. package/package.json +15 -3
  2. package/src/cli/corpus-candidate.ts +228 -0
  3. package/src/cli/corpus-command.ts +32 -3
  4. package/src/cli/corpus-pins.ts +73 -0
  5. package/src/cli/corpus-report.ts +36 -0
  6. package/src/cli/corpus-tar.ts +28 -0
  7. package/src/cli/corpus-usage.ts +24 -3
  8. package/src/cli/corpus.ts +50 -68
  9. package/src/cli/format-diff-range.ts +112 -0
  10. package/src/cli/main.ts +39 -9
  11. package/src/cli/pack.ts +20 -3
  12. package/src/cli/validate.ts +1 -1
  13. package/src/format/clientOwnership.ts +1 -1
  14. package/src/format/clientTrails.ts +121 -46
  15. package/src/format/cook-deprecations.ts +53 -0
  16. package/src/format/cook-preparation.ts +2 -1
  17. package/src/format/creature-rest-rig.ts +248 -0
  18. package/src/format/creature-skin-bounds.ts +146 -0
  19. package/src/format/deprecations.ts +18 -21
  20. package/src/format/edge-observations.ts +67 -0
  21. package/src/format/empty-shardfile.ts +4 -2
  22. package/src/format/entry-read-set.ts +147 -0
  23. package/src/format/format-migrate-rebuild.ts +4 -1
  24. package/src/format/format-migrate.ts +7 -1
  25. package/src/format/format-migration-envelope.ts +19 -0
  26. package/src/format/format-migration-prune.ts +42 -1
  27. package/src/format/format-migrations.ts +8 -1
  28. package/src/format/format-window.ts +71 -0
  29. package/src/format/immutable.ts +4 -1
  30. package/src/format/json-schema.ts +1468 -0
  31. package/src/format/lootEconomy.ts +4 -1
  32. package/src/format/mat4.ts +225 -0
  33. package/src/format/memory-armed.json +1 -9
  34. package/src/format/memory-armed.ts +46 -17
  35. package/src/format/memory-epoch.ts +10 -3
  36. package/src/format/memory-line.ts +17 -6
  37. package/src/format/outputFields.ts +3 -2
  38. package/src/format/outputIndex.ts +14 -1
  39. package/src/format/platformDataSections.ts +5 -0
  40. package/src/format/platformFileFamilies.ts +3 -3
  41. package/src/format/play-area.ts +95 -0
  42. package/src/format/policy-verdict.ts +12 -2
  43. package/src/format/registeredJson.ts +2 -2
  44. package/src/format/runtime-tier.ts +14 -7
  45. package/src/format/save-revision.ts +105 -0
  46. package/src/format/schema-source.ts +70 -0
  47. package/src/format/sections.ts +4 -1
  48. package/src/format/server-plan.ts +42 -0
  49. package/src/format/server-reads.ts +23 -0
  50. package/src/format/shardSchema.ts +9 -4
  51. package/src/format/shardSections.ts +17 -7
  52. package/src/format/shardfile-assets.ts +5 -5
  53. package/src/format/shardfile-stream-admit.ts +19 -10
  54. package/src/format/shardfile-stream-facts.ts +2 -1
  55. package/src/format/shardfile-stream.ts +2 -3
  56. package/src/format/shardfileValidation.ts +16 -9
  57. package/src/format/species-deprecations.ts +79 -0
  58. package/src/format/stateLineage.ts +12 -6
  59. package/src/format/tread-boxes.ts +65 -0
  60. package/src/format/vehicle-hulls.ts +124 -0
  61. package/src/format/version.ts +74 -17
  62. package/src/format/whole-inputs.ts +51 -0
  63. package/src/tools/runtime-hosts.json +4 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildshard/shard-format",
3
- "version": "0.70.0",
3
+ "version": "0.71.0",
4
4
  "description": "The raw shardfile format: the spec, the validator and a validate CLI. The public, versioned contract every other repo builds on.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -58,6 +58,7 @@
58
58
  "./audioMeasure": "./src/format/audio-measure.ts",
59
59
  "./audioRules": "./src/format/audio-rules.ts",
60
60
  "./audioWaiver": "./src/format/audio-waiver.ts",
61
+ "./jsonSchema": "./src/format/json-schema.ts",
61
62
  "./shardfileAudio": "./src/format/shardfile-audio.ts",
62
63
  "./audioRouting": "./src/format/audioRouting.ts",
63
64
  "./audioScene": "./src/format/audioScene.ts",
@@ -131,6 +132,7 @@
131
132
  "./discoveryValues": "./src/format/discoveryValues.ts",
132
133
  "./easedFields": "./src/format/easedFields.ts",
133
134
  "./edgeProfiles": "./src/format/edgeProfiles.ts",
135
+ "./edgeObservations": "./src/format/edge-observations.ts",
134
136
  "./eliteContinuation": "./src/format/eliteContinuation.ts",
135
137
  "./eliteEntryRows": "./src/format/eliteEntryRows.ts",
136
138
  "./eliteFeature": "./src/format/eliteFeature.ts",
@@ -161,6 +163,14 @@
161
163
  "./forestPage": "./src/format/forestPage.ts",
162
164
  "./forestWeather": "./src/format/forestWeather.ts",
163
165
  "./geometryPack": "./src/format/geometryPack.ts",
166
+ "./creatureRestRig": "./src/format/creature-rest-rig.ts",
167
+ "./creatureSkinBounds": "./src/format/creature-skin-bounds.ts",
168
+ "./playArea": "./src/format/play-area.ts",
169
+ "./entryReadSet": "./src/format/entry-read-set.ts",
170
+ "./serverReads": "./src/format/server-reads.ts",
171
+ "./wholeInputs": "./src/format/whole-inputs.ts",
172
+ "./treadBoxes": "./src/format/tread-boxes.ts",
173
+ "./vehicleHulls": "./src/format/vehicle-hulls.ts",
164
174
  "./glbBuffers": "./src/format/glbBuffers.ts",
165
175
  "./glbCost": "./src/format/glbCost.ts",
166
176
  "./glbImages": "./src/format/glbImages.ts",
@@ -463,7 +473,7 @@
463
473
  "lock:write": "node src/tools/format-lock.ts --write",
464
474
  "check:lock": "node src/tools/format-lock.ts",
465
475
  "check:format-gate": "node src/tools/format-gate.ts",
466
- "corpus:usage": "node src/cli/main.ts corpus --cache tmp/corpus --usage corpus/usage.json",
476
+ "corpus:usage": "node src/cli/main.ts corpus --cache tmp/corpus --usage corpus/usage.json --pins https://raw.githubusercontent.com/project-wildshard/engine/main/test/pins/shardfiles.json",
467
477
  "check:asks": "node kit/scripts/asks.mjs check",
468
478
  "check:agents": "node .githooks/agents-md.mjs lint",
469
479
  "check:parity": "node scripts/agent-parity.mjs --strict",
@@ -476,9 +486,11 @@
476
486
  "check:wire": "pnpm --dir packages/wire check && pnpm --dir packages/wire test && pnpm check:wire-proofs",
477
487
  "check:wire-proofs": "node --test src/scripts/wire/recordings-proof.mjs src/scripts/wire/graph-proof.mjs src/scripts/wire/prepared-frame-proof.mjs",
478
488
  "bench": "pnpm --dir packages/wire bench",
479
- "check:bin-pins": "node kit/scripts/bin-pins.mjs"
489
+ "check:bin-pins": "node kit/scripts/bin-pins.mjs",
490
+ "reupload": "node src/tools/reupload.ts"
480
491
  },
481
492
  "dependencies": {
493
+ "@valibot/to-json-schema": "1.8.0",
482
494
  "meshoptimizer": "1.2.0",
483
495
  "valibot": "1.5.0"
484
496
  },
@@ -0,0 +1,228 @@
1
+ /** SHARD-COMPAT FE3b (J486 Q51): the corpus runs a candidate chain and validator beside the current one, so a format change
2
+ * is judged against every kept original before it ships. A candidate is a module that names its extra migrations (run
3
+ * after the current chain, over the copy's JSON sections: a candidate's shape is one the current reader refuses) and a
4
+ * check over the copy (the parts the candidate's validator changes). An original is `migrates` when the candidate's
5
+ * steps changed its copy and the copy passes, `valid` when they changed nothing and it passes, and `fails` naming the
6
+ * path the candidate refuses. */
7
+ import { createHash } from 'node:crypto'
8
+ import { readFile } from 'node:fs/promises'
9
+ import path from 'node:path'
10
+ import { pathToFileURL } from 'node:url'
11
+ import { promisify } from 'node:util'
12
+ import { brotliDecompress } from 'node:zlib'
13
+
14
+ import {
15
+ INDEX,
16
+ readJson,
17
+ readSections,
18
+ record,
19
+ rows,
20
+ } from '../format/format-migrate-inventory.ts'
21
+ import { migrateShardfile } from '../format/format-migrate.ts'
22
+ import { FORMAT_MIGRATIONS } from '../format/format-migrations.ts'
23
+ import { readTar } from '../format/tar.ts'
24
+
25
+ import type { JsonRecord } from '../format/format-migrate-inventory.ts'
26
+ import type {
27
+ FormatMigration,
28
+ MigratingShard,
29
+ } from '../format/format-migrations.ts'
30
+
31
+ type Files = ReadonlyMap<string, Uint8Array>
32
+
33
+ /** One part a candidate refuses: the member path and the place in it. */
34
+ interface CandidateFailure {
35
+ readonly path: string
36
+ readonly reason: string
37
+ }
38
+
39
+ /** A candidate format change, as a module's `candidate` export. */
40
+ interface Candidate {
41
+ readonly id: string
42
+ /** Steps the candidate adds after the current chain, over its JSON sections. */
43
+ readonly migrations?: readonly FormatMigration[]
44
+ /** What the candidate's validator refuses in the migrated copy. */
45
+ readonly check: (files: Files) => readonly CandidateFailure[]
46
+ }
47
+
48
+ interface CandidateVerdict {
49
+ readonly verdict: 'valid' | 'migrates' | 'fails'
50
+ /** The candidate's own steps that changed the copy. */
51
+ readonly applied: readonly string[]
52
+ readonly failures: readonly CandidateFailure[]
53
+ }
54
+
55
+ interface Named {
56
+ readonly shard: string
57
+ readonly revision: number
58
+ }
59
+
60
+ const NONE = 0
61
+ const unbrotli = promisify(brotliDecompress)
62
+ const encoder = new TextEncoder()
63
+
64
+ function sha256(bytes: Readonly<Uint8Array>): string {
65
+ return createHash('sha256').update(bytes).digest('hex')
66
+ }
67
+
68
+ function isCandidate(value: unknown): value is Candidate {
69
+ return (
70
+ typeof value === 'object' &&
71
+ value !== null &&
72
+ typeof Reflect.get(value, 'id') === 'string' &&
73
+ typeof Reflect.get(value, 'check') === 'function'
74
+ )
75
+ }
76
+
77
+ /** The `candidate` a module exports. */
78
+ async function loadCandidate(module: string): Promise<Candidate> {
79
+ const loaded: unknown = await import(pathToFileURL(path.resolve(module)).href)
80
+ const found: unknown =
81
+ typeof loaded === 'object' && loaded !== null
82
+ ? Reflect.get(loaded, 'candidate')
83
+ : undefined
84
+ if (!isCandidate(found)) {
85
+ throw new Error(`${module}: no candidate (id, check) exported`)
86
+ }
87
+ return found
88
+ }
89
+
90
+ /** The copy's JSON sections as a migrating shard, with each section's member path. */
91
+ function sectionsOf(
92
+ files: Files,
93
+ ): Readonly<{ shard: MigratingShard; paths: ReadonlyMap<string, string> }> {
94
+ const index = record(readJson(files, INDEX), INDEX)
95
+ const shard = {
96
+ index,
97
+ sections: readSections(files, index),
98
+ mounts: new Map<string, unknown>(),
99
+ }
100
+ const paths = new Map(
101
+ rows(index['sections']).map(
102
+ (row: Readonly<JsonRecord>): [string, string] => [
103
+ String(row['name']),
104
+ String(row['path']),
105
+ ],
106
+ ),
107
+ )
108
+ return { shard, paths }
109
+ }
110
+
111
+ /** Run the candidate's steps over the copy's sections: the ids that changed something, and the rewritten files. */
112
+ function candidateCopy(
113
+ files: Files,
114
+ steps: readonly FormatMigration[],
115
+ ): Readonly<{ applied: readonly string[]; files: Files }> {
116
+ const { shard, paths } = sectionsOf(files)
117
+ const applied = steps.filter((step: Readonly<FormatMigration>): boolean => {
118
+ const before = JSON.stringify([...shard.sections])
119
+ step.apply(shard)
120
+ return JSON.stringify([...shard.sections]) !== before
121
+ })
122
+ const out = new Map(files)
123
+ for (const [name, value] of shard.sections) {
124
+ out.set(
125
+ paths.get(name) ?? `sections/${name}.json`,
126
+ encoder.encode(JSON.stringify(value)),
127
+ )
128
+ }
129
+ return {
130
+ applied: applied.map((step: Readonly<FormatMigration>): string => step.id),
131
+ files: out,
132
+ }
133
+ }
134
+
135
+ /** The current chain's copy, or the reason it cannot run. */
136
+ function currentCopy(files: Files): Files | string {
137
+ try {
138
+ return migrateShardfile(files, {
139
+ hash: sha256,
140
+ migrations: FORMAT_MIGRATIONS,
141
+ }).files
142
+ } catch (error: unknown) {
143
+ return error instanceof Error ? error.message : String(error)
144
+ }
145
+ }
146
+
147
+ /** Judge one original's files under `candidate`. A chain that cannot run fails with its own reason. */
148
+ function judgeCandidate(
149
+ files: Files,
150
+ candidate: Readonly<Candidate>,
151
+ ): CandidateVerdict {
152
+ const copy = currentCopy(files)
153
+ if (typeof copy === 'string') {
154
+ const failures = [{ path: 'chain', reason: copy }]
155
+ return { verdict: 'fails', applied: [], failures }
156
+ }
157
+ const moved = candidateCopy(copy, candidate.migrations ?? [])
158
+ const failures = candidate.check(moved.files)
159
+ if (failures.length > NONE) {
160
+ return { verdict: 'fails', applied: moved.applied, failures }
161
+ }
162
+ const verdict = moved.applied.length > NONE ? 'migrates' : 'valid'
163
+ return { verdict, applied: moved.applied, failures }
164
+ }
165
+
166
+ /** Judge one stored original (brotli unless `plain`) under `candidate`. */
167
+ async function candidateVerdict(
168
+ file: string,
169
+ plain: boolean,
170
+ candidate: Readonly<Candidate>,
171
+ ): Promise<CandidateVerdict> {
172
+ const stored = new Uint8Array(await readFile(file))
173
+ const tar = plain ? stored : new Uint8Array(await unbrotli(stored))
174
+ return judgeCandidate(readTar(tar), candidate)
175
+ }
176
+
177
+ /** The report's text for one verdict: `migrates (steps)` or `fails path reason; …`. */
178
+ function candidateText(found: Readonly<CandidateVerdict>): string {
179
+ if (found.verdict === 'fails') {
180
+ const parts = found.failures.map(
181
+ (row: Readonly<CandidateFailure>): string => `${row.path} ${row.reason}`,
182
+ )
183
+ return `fails ${parts.join('; ')}`
184
+ }
185
+ return found.verdict === 'migrates'
186
+ ? `migrates (${found.applied.join(', ')})`
187
+ : 'valid'
188
+ }
189
+
190
+ /** The row field a candidate run adds, or none without a candidate. */
191
+ async function candidateField(
192
+ candidate: Readonly<Candidate> | undefined,
193
+ file: string,
194
+ plain: boolean,
195
+ ): Promise<Readonly<{ candidate?: string }>> {
196
+ if (candidate === undefined) {
197
+ return {}
198
+ }
199
+ const found = await candidateVerdict(file, plain, candidate)
200
+ return { candidate: candidateText(found) }
201
+ }
202
+
203
+ /** Whether a kept original is one `--only` names (`slug`, or `slug@r5`). */
204
+ function onlyNamed(
205
+ only: readonly string[] | undefined,
206
+ revisions: readonly Named[],
207
+ ): boolean {
208
+ return (
209
+ only === undefined ||
210
+ revisions.some(
211
+ (named: Named): boolean =>
212
+ only.includes(named.shard) ||
213
+ only.includes(`${named.shard}@r${String(named.revision)}`),
214
+ )
215
+ )
216
+ }
217
+
218
+ export {
219
+ type Candidate,
220
+ type CandidateFailure,
221
+ type CandidateVerdict,
222
+ candidateField,
223
+ candidateText,
224
+ candidateVerdict,
225
+ judgeCandidate,
226
+ loadCandidate,
227
+ onlyNamed,
228
+ }
@@ -1,14 +1,22 @@
1
1
  import { readFile } from 'node:fs/promises'
2
2
 
3
+ import { loadCandidate } from './corpus-candidate.ts'
3
4
  import { corpusGate, readKnownFailures } from './corpus-gate.ts'
5
+ import { readPins } from './corpus-pins.ts'
6
+ import { corpusReport } from './corpus-report.ts'
4
7
  import { corpusUsage, writeUsage } from './corpus-usage.ts'
5
- import { corpusReport, runCorpus } from './corpus.ts'
8
+ import { runCorpus } from './corpus.ts'
6
9
  import { readVersion } from './version.ts'
7
10
 
11
+ import type { CorpusRow } from './corpus.ts'
12
+
8
13
  interface CorpusArgs {
9
14
  readonly api: string
10
15
  readonly cache: string
11
16
  readonly usage: string | undefined
17
+ readonly pins: string | undefined
18
+ readonly candidate: string | undefined
19
+ readonly only: string | undefined
12
20
  }
13
21
 
14
22
  const EXIT_OK = 0
@@ -16,10 +24,31 @@ const EXIT_INVALID = 3
16
24
  const NONE = 0
17
25
  const KNOWN_FAILURES = 'corpus/known-failures.json'
18
26
 
27
+ /** Every kept original's row, with engine's pins when `--pins` names them (FE13). */
28
+ async function corpusRows(
29
+ args: Readonly<CorpusArgs>,
30
+ ): Promise<Readonly<{ rows: CorpusRow[]; validator: string }>> {
31
+ const validator = await readVersion()
32
+ const pins = args.pins === undefined ? [] : await readPins(args.pins)
33
+ const candidate =
34
+ args.candidate === undefined
35
+ ? {}
36
+ : { candidate: await loadCandidate(args.candidate) }
37
+ const only = args.only === undefined ? {} : { only: args.only.split(',') }
38
+ const rows = await runCorpus({
39
+ api: args.api,
40
+ cache: args.cache,
41
+ validator,
42
+ pins,
43
+ ...candidate,
44
+ ...only,
45
+ })
46
+ return { rows, validator }
47
+ }
48
+
19
49
  /** `shard-format corpus`: every kept original, as stored (FE3), gated by FE5's known failures; `--usage` writes FE13's record. */
20
50
  async function corpusCommand(args: Readonly<CorpusArgs>): Promise<number> {
21
- const validator = await readVersion()
22
- const rows = await runCorpus({ api: args.api, cache: args.cache, validator })
51
+ const { rows, validator } = await corpusRows(args)
23
52
  process.stdout.write(`${corpusReport(rows)}\n`)
24
53
  if (args.usage !== undefined) {
25
54
  await writeUsage(args.usage, corpusUsage(rows, validator, new Date()))
@@ -0,0 +1,73 @@
1
+ import { readFile } from 'node:fs/promises'
2
+
3
+ import { array, number, object, parse, pipe, regex, string } from 'valibot'
4
+
5
+ import type { StoredRecord } from './corpus.ts'
6
+
7
+ // SHARD-COMPAT FE13, the pins half: the delete gate counts users over every
8
+ // original GC keeps and every shard file engine pins. `corpus --pins <file or
9
+ // URL>` reads engine's `test/pins/shardfiles.json`; a pinned original the
10
+ // API's public list leaves out (an older revision past GC's window) joins
11
+ // the run, judged by hash like any other.
12
+
13
+ const HASH = pipe(string(), regex(/^[a-f0-9]{64}$/u))
14
+ const PinSchema = object({
15
+ slug: string(),
16
+ revision: number(),
17
+ hash: HASH,
18
+ bytes: number(),
19
+ })
20
+ const PinsSchema = object({ shards: array(PinSchema) })
21
+
22
+ /** One shard file engine pins. */
23
+ interface Pin {
24
+ readonly slug: string
25
+ readonly revision: number
26
+ readonly hash: string
27
+ readonly bytes: number
28
+ }
29
+
30
+ const FETCH_TIMEOUT_MS = 60_000
31
+
32
+ async function pinsText(source: string, get: typeof fetch): Promise<string> {
33
+ if (!/^https?:\/\//u.test(source)) {
34
+ return await readFile(source, 'utf8')
35
+ }
36
+ const answer = await get(source, {
37
+ signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
38
+ })
39
+ if (!answer.ok) {
40
+ throw new Error(`pins ${source}: HTTP ${String(answer.status)}`)
41
+ }
42
+ return await answer.text()
43
+ }
44
+
45
+ /** Engine's shard-file pins, from a file or a URL. */
46
+ async function readPins(
47
+ source: string,
48
+ get: typeof fetch = fetch,
49
+ ): Promise<readonly Pin[]> {
50
+ const text = await pinsText(source, get)
51
+ const parsed = parse(PinsSchema, JSON.parse(text))
52
+ return parsed.shards
53
+ }
54
+
55
+ /** The listed originals, then each pinned one the list leaves out. */
56
+ function withPins(
57
+ listed: readonly StoredRecord[],
58
+ pins: readonly Pin[],
59
+ ): readonly StoredRecord[] {
60
+ const known = new Set(
61
+ listed.map((stored: StoredRecord): string => stored.hash),
62
+ )
63
+ const extra = pins
64
+ .filter((pin: Readonly<Pin>): boolean => !known.has(pin.hash))
65
+ .map((pin: Readonly<Pin>): StoredRecord => ({
66
+ hash: pin.hash,
67
+ storedBytes: pin.bytes,
68
+ revisions: [{ shard: pin.slug, revision: pin.revision }],
69
+ }))
70
+ return [...listed, ...extra]
71
+ }
72
+
73
+ export { type Pin, readPins, withPins }
@@ -0,0 +1,36 @@
1
+ import { deprecationLines } from './corpus-deprecations.ts'
2
+
3
+ import type { CorpusRow } from './corpus.ts'
4
+
5
+ // `shard-format corpus`'s text report (FE3): one line per original, the
6
+ // totals, then FE7's deprecation users.
7
+
8
+ const NONE = 0
9
+ const FIRST = 0
10
+ const SHORT_HASH = 12
11
+ const VERDICT_WIDTH = 5
12
+
13
+ function corpusReport(rows: readonly CorpusRow[]): string {
14
+ const lines = rows.map(
15
+ (found: Readonly<CorpusRow>): string =>
16
+ `${found.verdict.padEnd(VERDICT_WIDTH)} ${found.slug} r${String(found.revision)} ${found.hash.slice(FIRST, SHORT_HASH)}${found.reason === undefined ? '' : ` ${found.reason}`}${found.candidate === undefined ? '' : ` | candidate: ${found.candidate}`}`,
17
+ )
18
+ const failed = rows.filter(
19
+ (found: Readonly<CorpusRow>): boolean => found.verdict === 'fails',
20
+ ).length
21
+ const fetched = rows.filter(
22
+ (found: Readonly<CorpusRow>): boolean => !found.cached,
23
+ )
24
+ const bytes = fetched.reduce(
25
+ (sum: number, found: Readonly<CorpusRow>): number =>
26
+ sum + found.storedBytes,
27
+ NONE,
28
+ )
29
+ lines.push(
30
+ `corpus: ${String(rows.length)} kept originals, ${String(rows.length - failed)} valid, ${String(failed)} fail; fetched ${String(fetched.length)} (${String(bytes)} stored bytes), ${String(rows.length - fetched.length)} from cache`,
31
+ ...deprecationLines(rows),
32
+ )
33
+ return lines.join('\n')
34
+ }
35
+
36
+ export { corpusReport }
@@ -0,0 +1,28 @@
1
+ /** A downloaded original's first bytes: plain tarball or Brotli (FE3). */
2
+ import { open } from 'node:fs/promises'
3
+
4
+ const NONE = 0
5
+ const TAR_MAGIC_AT = 257
6
+ const TAR_MAGIC = 'ustar'
7
+ const PEEK_BYTES = 512
8
+
9
+ /** Whether the first bytes are a plain tarball (`ustar` at 257) rather than Brotli. */
10
+ function isPlainTar(head: Readonly<Uint8Array>): boolean {
11
+ const magic = new TextDecoder().decode(
12
+ head.subarray(TAR_MAGIC_AT, TAR_MAGIC_AT + TAR_MAGIC.length),
13
+ )
14
+ return magic === TAR_MAGIC
15
+ }
16
+
17
+ async function firstBytes(file: string): Promise<Uint8Array> {
18
+ const handle = await open(file)
19
+ try {
20
+ const bytes = new Uint8Array(PEEK_BYTES)
21
+ await handle.read(bytes, NONE, PEEK_BYTES, NONE)
22
+ return bytes
23
+ } finally {
24
+ await handle.close()
25
+ }
26
+ }
27
+
28
+ export { firstBytes, isPlainTar }
@@ -1,6 +1,14 @@
1
1
  import { writeFile } from 'node:fs/promises'
2
2
 
3
- import { array, number, parse, record, strictObject, string } from 'valibot'
3
+ import {
4
+ array,
5
+ number,
6
+ optional,
7
+ parse,
8
+ record,
9
+ strictObject,
10
+ string,
11
+ } from 'valibot'
4
12
 
5
13
  import type { CorpusRow } from './corpus.ts'
6
14
 
@@ -17,6 +25,8 @@ interface Usage {
17
25
  readonly originals: number
18
26
  readonly deprecations: Readonly<Record<string, readonly string[]>>
19
27
  readonly migrations: Readonly<Record<string, readonly string[]>>
28
+ /** FE13: how many engine-pinned shard files the run covered; absent when it ran without `--pins`. */
29
+ readonly pins?: number | undefined
20
30
  }
21
31
 
22
32
  const Users = record(string(), array(string()))
@@ -26,9 +36,11 @@ const UsageSchema = strictObject({
26
36
  originals: number(),
27
37
  deprecations: Users,
28
38
  migrations: Users,
39
+ pins: optional(number()),
29
40
  })
30
41
  const MIGRATED = 'migrated: '
31
42
  const INDENT = 1
43
+ const NO_PINS = 0
32
44
 
33
45
  function add(
34
46
  users: Map<string, string[]>,
@@ -50,6 +62,13 @@ function stepsOf(reason: string | undefined): readonly string[] {
50
62
  : []
51
63
  }
52
64
 
65
+ /** FE13: how many rows engine pins. */
66
+ function pinsOf(rows: readonly Readonly<CorpusRow>[]): number {
67
+ return rows.filter(
68
+ (found: Readonly<CorpusRow>): boolean => found.pinned === true,
69
+ ).length
70
+ }
71
+
53
72
  /** Who uses each deprecated path and each chain step, over the kept originals. */
54
73
  function corpusUsage(
55
74
  rows: readonly Readonly<CorpusRow>[],
@@ -59,17 +78,19 @@ function corpusUsage(
59
78
  const deprecations = new Map<string, string[]>()
60
79
  const migrations = new Map<string, string[]>()
61
80
  for (const found of rows) {
62
- const name = `${found.slug} r${String(found.revision)}`
81
+ const name = `${found.slug} r${String(found.revision)}${found.pinned === true ? ' (engine pin)' : ''}`
63
82
  add(deprecations, found.deprecations, name)
64
83
  add(migrations, stepsOf(found.reason), name)
65
84
  }
66
- return {
85
+ const usage: Usage = {
67
86
  measuredAt: measuredAt.toISOString(),
68
87
  validator,
69
88
  originals: rows.length,
70
89
  deprecations: Object.fromEntries(deprecations),
71
90
  migrations: Object.fromEntries(migrations),
72
91
  }
92
+ const pins = pinsOf(rows)
93
+ return pins === NO_PINS ? usage : { ...usage, pins }
73
94
  }
74
95
 
75
96
  async function writeUsage(file: string, usage: Readonly<Usage>): Promise<void> {