@wildshard/shard-format 0.67.0 → 0.68.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 +7 -19
- package/src/cli/corpus-deprecations.ts +57 -0
- package/src/cli/corpus-gate.ts +114 -0
- package/src/cli/corpus-migrated.ts +68 -0
- package/src/cli/corpus.ts +300 -0
- package/src/cli/main.ts +41 -0
- package/src/format/cook-preparation.ts +22 -6
- package/src/format/creature-layers.ts +4 -0
- package/src/format/deprecations.ts +228 -0
- package/src/format/empty-shardfile.json +56 -0
- package/src/format/empty-shardfile.ts +43 -0
- package/src/format/format-migrate-inventory.ts +142 -0
- package/src/format/format-migrate-rebuild.ts +269 -0
- package/src/format/format-migrate.ts +290 -0
- package/src/format/format-migration-journal.ts +129 -0
- package/src/format/format-migration-ops.ts +230 -0
- package/src/format/format-migration-prune.ts +106 -0
- package/src/format/format-migrations.ts +124 -0
- package/src/format/issue-codes.ts +3 -1
- package/src/format/journal-glyphs.json +12 -0
- package/src/format/memory-armed.json +1 -39
- package/src/format/memory-armed.ts +41 -8
- package/src/format/memory-epoch.ts +2 -2
- package/src/format/memory-line.ts +4 -3
- package/src/format/memoryAdmission.ts +1 -1
- package/src/format/policy-verdict.ts +95 -0
- package/src/format/rows.ts +6 -4
- package/src/format/runtime-tier.ts +3 -3
- package/src/format/shardSections.ts +11 -1
- package/src/format/shardfile-assets.ts +12 -8
- package/src/format/shardfile-stream-admit.ts +41 -16
- package/src/format/shardfile-stream-rows.ts +3 -1
- package/src/format/shardfile-stream.ts +12 -9
- package/src/format/shardfileValidation.ts +30 -18
- package/src/format/stateLineage.ts +1 -1
- package/src/format/version.ts +11 -13
- package/src/format/baked-terrain.golden.json +0 -2760
- package/src/format/colliders.golden.json +0 -1619
- package/src/format/forest-placement.golden.json +0 -4588
- package/src/format/geometry-layout.golden.json +0 -180
- package/src/format/interact-table.golden.json +0 -340
- package/src/format/item-catalogue.golden.json +0 -2310
- package/src/format/memory-ratchet.ts +0 -107
- package/src/format/small-laws.golden.json +0 -57
- package/src/format/terrain-field.golden.json +0 -3998
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// oxlint-disable typescript/prefer-readonly-parameter-types -- the copy's index and files are rebuilt in place
|
|
2
|
+
/** The migration chain rebuilds a copy's inventory after its steps (SHARD-COMPAT CP1): the chain, never a step, writes
|
|
3
|
+
* section hashes and sizes, packs, the declared source hash, JSON cook mounts with their rows and fence, and a
|
|
4
|
+
* data-only copy's re-sealed author preparation. */
|
|
5
|
+
import { readCookData } from './cook-data.ts'
|
|
6
|
+
import { cookPreparationBytes } from './cook-preparation.ts'
|
|
7
|
+
import {
|
|
8
|
+
listed,
|
|
9
|
+
listedRows,
|
|
10
|
+
readJson,
|
|
11
|
+
record,
|
|
12
|
+
rows,
|
|
13
|
+
} from './format-migrate-inventory.ts'
|
|
14
|
+
import { assembleOutputShardfile } from './outputIndex.ts'
|
|
15
|
+
import { validationSourceBytes } from './validationReceipt.ts'
|
|
16
|
+
|
|
17
|
+
import type { Hash, JsonRecord, Listed } from './format-migrate-inventory.ts'
|
|
18
|
+
import type { MigratingShard } from './format-migrations.ts'
|
|
19
|
+
import type { OutputIndex } from './outputIndex.ts'
|
|
20
|
+
|
|
21
|
+
/** The copy being written: its index (edited in place) and its files (edited in place). */
|
|
22
|
+
interface Copy {
|
|
23
|
+
readonly index: JsonRecord
|
|
24
|
+
readonly out: Map<string, Uint8Array>
|
|
25
|
+
readonly hash: Hash
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** How the copy's cook is rebuilt: each JSON mount's text as read (a changed one is written again), and whether the
|
|
29
|
+
* original's preparation is re-sealed (prepared, and no step changes what renders). */
|
|
30
|
+
interface CookPlan {
|
|
31
|
+
readonly mountText: ReadonlyMap<string, string>
|
|
32
|
+
readonly reseal: boolean
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const NO_BYTES = 0
|
|
36
|
+
|
|
37
|
+
/** A step may rewrite or delete a section; adding one waits for FE8's moveSection. */
|
|
38
|
+
function refuseNewSections(
|
|
39
|
+
copy: Readonly<Copy>,
|
|
40
|
+
shard: Readonly<MigratingShard>,
|
|
41
|
+
): void {
|
|
42
|
+
const names = new Set(
|
|
43
|
+
rows(copy.index['sections']).map((row: Readonly<JsonRecord>): string =>
|
|
44
|
+
String(row['name']),
|
|
45
|
+
),
|
|
46
|
+
)
|
|
47
|
+
for (const name of shard.sections.keys()) {
|
|
48
|
+
if (!names.has(name)) {
|
|
49
|
+
throw new Error(`A new section needs FE8's moveSection: ${name}`)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Write one section again, or drop it; the old and new hash when it stays. */
|
|
55
|
+
function rewriteSection(
|
|
56
|
+
copy: Readonly<Copy>,
|
|
57
|
+
shard: Readonly<MigratingShard>,
|
|
58
|
+
row: JsonRecord,
|
|
59
|
+
): readonly [string, string] | undefined {
|
|
60
|
+
const old = listed(row)
|
|
61
|
+
const name = String(row['name'])
|
|
62
|
+
if (!shard.sections.has(name)) {
|
|
63
|
+
copy.out.delete(old.path)
|
|
64
|
+
return undefined
|
|
65
|
+
}
|
|
66
|
+
const bytes = validationSourceBytes(shard.sections.get(name))
|
|
67
|
+
const now = copy.hash(bytes)
|
|
68
|
+
copy.out.set(old.path, bytes)
|
|
69
|
+
Object.assign(row, { hash: now, bytes: bytes.length })
|
|
70
|
+
return [old.hash, now]
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Write each section again and return each old section hash's new one; a section a step deleted leaves. */
|
|
74
|
+
function rebuildSections(
|
|
75
|
+
copy: Readonly<Copy>,
|
|
76
|
+
shard: Readonly<MigratingShard>,
|
|
77
|
+
): Map<string, string> {
|
|
78
|
+
refuseNewSections(copy, shard)
|
|
79
|
+
const swapped = new Map<string, string>()
|
|
80
|
+
const kept: JsonRecord[] = []
|
|
81
|
+
for (const row of rows(copy.index['sections'])) {
|
|
82
|
+
const moved = rewriteSection(copy, shard, row)
|
|
83
|
+
if (moved !== undefined) {
|
|
84
|
+
swapped.set(...moved)
|
|
85
|
+
kept.push(row)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
copy.index['sections'] = kept
|
|
89
|
+
return swapped
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Every pack member's bytes by hash: sections as rebuilt, schemas and files as they are. */
|
|
93
|
+
function members(copy: Readonly<Copy>): Map<string, Uint8Array> {
|
|
94
|
+
const found = new Map<string, Uint8Array>()
|
|
95
|
+
const all: readonly Listed[] = [
|
|
96
|
+
...listedRows(copy.index['sections']),
|
|
97
|
+
...listedRows(copy.index['schemas']),
|
|
98
|
+
...listedRows(copy.index['files'], 'compressed'),
|
|
99
|
+
]
|
|
100
|
+
for (const row of all) {
|
|
101
|
+
const bytes = copy.out.get(row.path)
|
|
102
|
+
if (bytes !== undefined) {
|
|
103
|
+
found.set(row.hash, bytes)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return found
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function joinParts(
|
|
110
|
+
parts: readonly Readonly<Uint8Array>[],
|
|
111
|
+
hash: Hash,
|
|
112
|
+
): { joined: Uint8Array; entries: JsonRecord[] } {
|
|
113
|
+
const joined = new Uint8Array(
|
|
114
|
+
parts.reduce(
|
|
115
|
+
(total: number, part: Readonly<Uint8Array>): number =>
|
|
116
|
+
total + part.length,
|
|
117
|
+
NO_BYTES,
|
|
118
|
+
),
|
|
119
|
+
)
|
|
120
|
+
let offset = NO_BYTES
|
|
121
|
+
const entries = parts.map((part: Readonly<Uint8Array>): JsonRecord => {
|
|
122
|
+
const entry = { hash: hash(part), offset, bytes: part.length }
|
|
123
|
+
joined.set(part, offset)
|
|
124
|
+
offset += part.length
|
|
125
|
+
return entry
|
|
126
|
+
})
|
|
127
|
+
return { joined, entries }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Concatenate each pack again from its members, a rewritten section by its new bytes. A pack the original carried is
|
|
131
|
+
* written again; one it left out stays out, listed with its new identity. */
|
|
132
|
+
function rebuildPacks(
|
|
133
|
+
copy: Readonly<Copy>,
|
|
134
|
+
swapped: ReadonlyMap<string, string>,
|
|
135
|
+
): void {
|
|
136
|
+
const found = members(copy)
|
|
137
|
+
for (const pack of rows(copy.index['packs'])) {
|
|
138
|
+
const old = listed(pack)
|
|
139
|
+
const parts = rows(pack['entries']).map(
|
|
140
|
+
(entry: Readonly<JsonRecord>): Uint8Array => {
|
|
141
|
+
const was = String(entry['hash'])
|
|
142
|
+
const bytes = found.get(swapped.get(was) ?? was)
|
|
143
|
+
if (bytes === undefined) {
|
|
144
|
+
throw new Error(
|
|
145
|
+
`Pack member outside its listed inventory: ${old.path}`,
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
return bytes
|
|
149
|
+
},
|
|
150
|
+
)
|
|
151
|
+
const { joined, entries } = joinParts(parts, copy.hash)
|
|
152
|
+
const packHash = copy.hash(joined)
|
|
153
|
+
const path = `blobs/pack/${packHash}.pack`
|
|
154
|
+
if (copy.out.delete(old.path)) {
|
|
155
|
+
copy.out.set(path, joined)
|
|
156
|
+
}
|
|
157
|
+
Object.assign(pack, { path, hash: packHash, bytes: joined.length, entries })
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The declared source hash over the copy's assembled shard, as the validator computes it. */
|
|
162
|
+
function rebuildSourceHash(
|
|
163
|
+
copy: Readonly<Copy>,
|
|
164
|
+
shard: Readonly<MigratingShard>,
|
|
165
|
+
): void {
|
|
166
|
+
const source = assembleOutputShardfile(copy.index, shard.sections)
|
|
167
|
+
record(copy.index['build'], 'build')['sourceHash'] = copy.hash(
|
|
168
|
+
validationSourceBytes(source),
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** The copy's cook file, read from the copy as rebuilt so far. */
|
|
173
|
+
function cookFile(copy: Readonly<Copy>): { path: string; raw: JsonRecord } {
|
|
174
|
+
const { path } = listed(record(copy.index['cook'], 'cook'))
|
|
175
|
+
return { path, raw: record(readJson(copy.out, path), 'cook.json') }
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Write the cook file again and its index row. */
|
|
179
|
+
function writeCook(copy: Readonly<Copy>, path: string, raw: JsonRecord): void {
|
|
180
|
+
const bytes = validationSourceBytes(raw)
|
|
181
|
+
copy.out.set(path, bytes)
|
|
182
|
+
Object.assign(record(copy.index['cook'], 'cook'), {
|
|
183
|
+
hash: copy.hash(bytes),
|
|
184
|
+
bytes: bytes.length,
|
|
185
|
+
})
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Write each JSON mount a step changed; the old hash to the new row, by path. */
|
|
189
|
+
function writeMounts(
|
|
190
|
+
copy: Readonly<Copy>,
|
|
191
|
+
shard: Readonly<MigratingShard>,
|
|
192
|
+
mountText: ReadonlyMap<string, string>,
|
|
193
|
+
): Map<string, Readonly<{ hash: string; bytes: number }>> {
|
|
194
|
+
const swapped = new Map<string, Readonly<{ hash: string; bytes: number }>>()
|
|
195
|
+
for (const [path, value] of shard.mounts) {
|
|
196
|
+
const text = JSON.stringify(value)
|
|
197
|
+
if (text !== mountText.get(path)) {
|
|
198
|
+
const bytes = new TextEncoder().encode(text)
|
|
199
|
+
copy.out.set(path, bytes)
|
|
200
|
+
swapped.set(path, { hash: copy.hash(bytes), bytes: bytes.length })
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return swapped
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Point each cook row at its mount's new bytes and each fence entry at the new hash. */
|
|
207
|
+
function swapCookRows(
|
|
208
|
+
raw: JsonRecord,
|
|
209
|
+
swapped: ReadonlyMap<string, Readonly<{ hash: string; bytes: number }>>,
|
|
210
|
+
): void {
|
|
211
|
+
const hashes = new Map<string, string>()
|
|
212
|
+
for (const row of rows(raw['mounts'])) {
|
|
213
|
+
const now = swapped.get(String(row['path']))
|
|
214
|
+
if (now !== undefined) {
|
|
215
|
+
hashes.set(String(row['hash']), now.hash)
|
|
216
|
+
Object.assign(row, now)
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const { fence } = raw
|
|
220
|
+
if (Array.isArray(fence)) {
|
|
221
|
+
raw['fence'] = fence.map((row: unknown): unknown =>
|
|
222
|
+
typeof row === 'string' ? (hashes.get(row) ?? row) : row,
|
|
223
|
+
)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Write each changed JSON mount, then its cook rows and its fence entry by the new hash. */
|
|
228
|
+
function rebuildMounts(
|
|
229
|
+
copy: Readonly<Copy>,
|
|
230
|
+
shard: Readonly<MigratingShard>,
|
|
231
|
+
mountText: ReadonlyMap<string, string>,
|
|
232
|
+
): void {
|
|
233
|
+
const swapped = writeMounts(copy, shard, mountText)
|
|
234
|
+
if (swapped.size === NO_BYTES) {
|
|
235
|
+
return
|
|
236
|
+
}
|
|
237
|
+
const { path, raw } = cookFile(copy)
|
|
238
|
+
swapCookRows(raw, swapped)
|
|
239
|
+
writeCook(copy, path, raw)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Re-seal a prepared file's input hash over the rebuilt index and mounts; its fence and programs stand. */
|
|
243
|
+
function resealPreparation(copy: Readonly<Copy>): void {
|
|
244
|
+
const { path, raw } = cookFile(copy)
|
|
245
|
+
const cook = readCookData(raw)
|
|
246
|
+
const renderer = 'renderer' in cook ? cook.renderer : undefined
|
|
247
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the rebuilt index, read only for its preparation bytes
|
|
248
|
+
const index = copy.index as unknown as OutputIndex
|
|
249
|
+
raw['inputHash'] = copy.hash(
|
|
250
|
+
cookPreparationBytes(index, cook.mounts, renderer),
|
|
251
|
+
)
|
|
252
|
+
writeCook(copy, path, raw)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Sections, packs and the source hash, then the cook's JSON mounts and a data-only copy's re-sealed preparation. */
|
|
256
|
+
function rebuildInventory(
|
|
257
|
+
copy: Readonly<Copy>,
|
|
258
|
+
shard: Readonly<MigratingShard>,
|
|
259
|
+
cook: Readonly<CookPlan>,
|
|
260
|
+
): void {
|
|
261
|
+
rebuildPacks(copy, rebuildSections(copy, shard))
|
|
262
|
+
rebuildSourceHash(copy, shard)
|
|
263
|
+
rebuildMounts(copy, shard, cook.mountText)
|
|
264
|
+
if (cook.reseal) {
|
|
265
|
+
resealPreparation(copy)
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export { type CookPlan, type Copy, rebuildInventory }
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
// oxlint-disable typescript/prefer-readonly-parameter-types -- a migrated copy's file map is built in place
|
|
2
|
+
/** The migration chain's runner (SHARD-COMPAT CP1, J486): `migrateShardfile` turns an original shard file into its
|
|
3
|
+
* migrated copy at a reader's format version. It verifies the original first (every listed byte and the author
|
|
4
|
+
* preparation), runs each registered step the file is below, then rebuilds the inventory (`format-migrate-rebuild.ts`).
|
|
5
|
+
* It is deterministic (canonical JSON, no clock, network or randomness), always starts from the original, and twice
|
|
6
|
+
* equals once. */
|
|
7
|
+
import { readCookData } from './cook-data.ts'
|
|
8
|
+
import { requireCookPreparation } from './cook-preparation.ts'
|
|
9
|
+
import {
|
|
10
|
+
INDEX,
|
|
11
|
+
RECEIPT,
|
|
12
|
+
listed,
|
|
13
|
+
listedRows,
|
|
14
|
+
readJson,
|
|
15
|
+
readSections,
|
|
16
|
+
record,
|
|
17
|
+
verifyInventory,
|
|
18
|
+
} from './format-migrate-inventory.ts'
|
|
19
|
+
import { rebuildInventory } from './format-migrate-rebuild.ts'
|
|
20
|
+
import {
|
|
21
|
+
FORMAT_MIGRATIONS,
|
|
22
|
+
checkMigrations,
|
|
23
|
+
versionBelow,
|
|
24
|
+
} from './format-migrations.ts'
|
|
25
|
+
import { validateShardfileFolder } from './shardfileValidation.ts'
|
|
26
|
+
import { validationSourceBytes } from './validationReceipt.ts'
|
|
27
|
+
import {
|
|
28
|
+
SHARDFILE_FLOOR,
|
|
29
|
+
SHARDFILE_VERSION,
|
|
30
|
+
compareShardfileVersions,
|
|
31
|
+
} from './version.ts'
|
|
32
|
+
|
|
33
|
+
import type { CookData } from './cook-data.ts'
|
|
34
|
+
import type { Files, Hash, JsonRecord } from './format-migrate-inventory.ts'
|
|
35
|
+
import type { FormatMigration, MigratingShard } from './format-migrations.ts'
|
|
36
|
+
import type { OutputIndex } from './outputIndex.ts'
|
|
37
|
+
import type {
|
|
38
|
+
ShardfileValidationPorts,
|
|
39
|
+
ValidatedShardfile,
|
|
40
|
+
} from './shardfileValidation.ts'
|
|
41
|
+
|
|
42
|
+
interface MigrateOptions {
|
|
43
|
+
/** Lowercase hex SHA-256 of exact bytes. */
|
|
44
|
+
readonly hash: Hash
|
|
45
|
+
/** The reader's format version; the current one by default. */
|
|
46
|
+
readonly to?: string
|
|
47
|
+
/** The steps to run; the registry by default (tests inject their own). */
|
|
48
|
+
readonly migrations?: readonly FormatMigration[]
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface MigratedShardfile {
|
|
52
|
+
/** The migrated copy: the original's own map when nothing ran. */
|
|
53
|
+
readonly files: Files
|
|
54
|
+
/** The original's format version, as spelled. */
|
|
55
|
+
readonly from: string
|
|
56
|
+
/** The copy's format version. */
|
|
57
|
+
readonly to: string
|
|
58
|
+
/** Migration ids, in the order they ran. */
|
|
59
|
+
readonly applied: readonly string[]
|
|
60
|
+
/** Hash over every registered step and the runner (`migrated_shardfiles.chain_id`). */
|
|
61
|
+
readonly chainId: string
|
|
62
|
+
/** The copy's `shard.json` hash, which names every byte it inventories. */
|
|
63
|
+
readonly digest: string
|
|
64
|
+
/** `author` re-sealed or untouched, `stale` after a render step (the box's cook re-prepares), `none` unprepared. */
|
|
65
|
+
readonly preparation: 'author' | 'none' | 'stale'
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** An original that passed its own inventory and preparation. */
|
|
69
|
+
interface Admitted {
|
|
70
|
+
readonly index: JsonRecord
|
|
71
|
+
readonly cook: CookData | undefined
|
|
72
|
+
readonly from: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const RUNNER = 1
|
|
76
|
+
const PREPARED = 3
|
|
77
|
+
const NONE = 0
|
|
78
|
+
|
|
79
|
+
function chainId(migrations: readonly FormatMigration[], hash: Hash): string {
|
|
80
|
+
const steps = migrations.map(
|
|
81
|
+
(step: Readonly<FormatMigration>): JsonRecord => ({
|
|
82
|
+
id: step.id,
|
|
83
|
+
to: step.to,
|
|
84
|
+
touches: step.touches,
|
|
85
|
+
affects: [...step.affects],
|
|
86
|
+
}),
|
|
87
|
+
)
|
|
88
|
+
return hash(validationSourceBytes({ runner: RUNNER, migrations: steps }))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Refuse a format above the reader or below the floor. */
|
|
92
|
+
function checkWindow(from: unknown, to: string): string {
|
|
93
|
+
if (typeof from !== 'string') {
|
|
94
|
+
throw new TypeError('Original needs a format version')
|
|
95
|
+
}
|
|
96
|
+
if (compareShardfileVersions(from, to) > NONE) {
|
|
97
|
+
throw new Error(`Format ${from} is newer than the reader's ${to}`)
|
|
98
|
+
}
|
|
99
|
+
if (compareShardfileVersions(from, SHARDFILE_FLOOR) < NONE) {
|
|
100
|
+
throw new Error(`Format ${from} is below the floor ${SHARDFILE_FLOOR}`)
|
|
101
|
+
}
|
|
102
|
+
return from
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Verify the original's bytes, preparation and format window before any step runs. */
|
|
106
|
+
function admitOriginal(files: Files, hash: Hash, to: string): Admitted {
|
|
107
|
+
const index = record(readJson(files, INDEX), INDEX)
|
|
108
|
+
const cookPath =
|
|
109
|
+
index['cook'] === undefined
|
|
110
|
+
? undefined
|
|
111
|
+
: listed(record(index['cook'], 'cook')).path
|
|
112
|
+
const rawCook =
|
|
113
|
+
cookPath === undefined
|
|
114
|
+
? undefined
|
|
115
|
+
: record(readJson(files, cookPath), 'cook.json')
|
|
116
|
+
verifyInventory(files, { index, cook: rawCook }, hash)
|
|
117
|
+
const cook = rawCook === undefined ? undefined : readCookData(rawCook)
|
|
118
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- read only for its slug, sdk, files and cook row
|
|
119
|
+
requireCookPreparation(index as unknown as OutputIndex, cook, hash)
|
|
120
|
+
return { index, cook, from: checkWindow(index['version'], to) }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const MOUNT = /^mounts\/.*\.json$/u
|
|
124
|
+
|
|
125
|
+
/** Each JSON cook mount under `mounts/`, parsed, with its text as read (to write back only what a step changed). */
|
|
126
|
+
function readMounts(
|
|
127
|
+
files: Files,
|
|
128
|
+
cook: Readonly<CookData> | undefined,
|
|
129
|
+
): { mounts: Map<string, unknown>; mountText: Map<string, string> } {
|
|
130
|
+
const mounts = new Map<string, unknown>()
|
|
131
|
+
const mountText = new Map<string, string>()
|
|
132
|
+
for (const { path } of cook?.mounts ?? []) {
|
|
133
|
+
const bytes = files.get(path)
|
|
134
|
+
if (MOUNT.test(path) && bytes !== undefined && !mounts.has(path)) {
|
|
135
|
+
const value: unknown = JSON.parse(new TextDecoder().decode(bytes))
|
|
136
|
+
mounts.set(path, value)
|
|
137
|
+
mountText.set(path, JSON.stringify(value))
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return { mounts, mountText }
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function rendersAgain(steps: readonly FormatMigration[]): boolean {
|
|
144
|
+
return steps.some(
|
|
145
|
+
(step: Readonly<FormatMigration>): boolean =>
|
|
146
|
+
step.affects.includes('render') || step.touches === 'files',
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Run each step in order; the chain, not a step, writes the format version. */
|
|
151
|
+
function runSteps(
|
|
152
|
+
shard: MigratingShard,
|
|
153
|
+
steps: readonly FormatMigration[],
|
|
154
|
+
): void {
|
|
155
|
+
for (const step of steps) {
|
|
156
|
+
step.apply(shard)
|
|
157
|
+
shard.index['version'] = step.to
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Whether a step runs: the file is below it, or sits at it with the shape a repair step mends; never above the reader. */
|
|
162
|
+
function runs(
|
|
163
|
+
step: Readonly<FormatMigration>,
|
|
164
|
+
shard: Readonly<MigratingShard>,
|
|
165
|
+
window: Readonly<{ from: string; to: string }>,
|
|
166
|
+
): boolean {
|
|
167
|
+
if (compareShardfileVersions(step.to, window.to) > NONE) {
|
|
168
|
+
return false
|
|
169
|
+
}
|
|
170
|
+
if (versionBelow(window.from, step.to)) {
|
|
171
|
+
return true
|
|
172
|
+
}
|
|
173
|
+
return (
|
|
174
|
+
compareShardfileVersions(window.from, step.to) === NONE &&
|
|
175
|
+
step.repairs?.(shard) === true
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Run the steps, then rebuild the inventory around what they changed. */
|
|
180
|
+
function writeCopy(
|
|
181
|
+
files: Files,
|
|
182
|
+
shard: MigratingShard,
|
|
183
|
+
plan: Readonly<{
|
|
184
|
+
cook: CookData | undefined
|
|
185
|
+
mountText: ReadonlyMap<string, string>
|
|
186
|
+
steps: readonly FormatMigration[]
|
|
187
|
+
hash: Hash
|
|
188
|
+
}>,
|
|
189
|
+
): Map<string, Uint8Array> {
|
|
190
|
+
const { index } = shard
|
|
191
|
+
const { cook, mountText } = plan
|
|
192
|
+
// The build's validation receipt names the old source hash, and a moved or dropped section leaves its old path: the
|
|
193
|
+
// copy drops both, and the rebuild writes every section that stays.
|
|
194
|
+
const gone = new Set([
|
|
195
|
+
RECEIPT,
|
|
196
|
+
...listedRows(index['sections']).map(
|
|
197
|
+
(row: Readonly<{ path: string }>): string => row.path,
|
|
198
|
+
),
|
|
199
|
+
])
|
|
200
|
+
runSteps(shard, plan.steps)
|
|
201
|
+
const out = new Map(
|
|
202
|
+
[...files].filter(
|
|
203
|
+
([path]: readonly [string, Uint8Array]): boolean => !gone.has(path),
|
|
204
|
+
),
|
|
205
|
+
)
|
|
206
|
+
const copy = { index, out, hash: plan.hash }
|
|
207
|
+
rebuildInventory(copy, shard, {
|
|
208
|
+
mountText,
|
|
209
|
+
reseal: cook?.version === PREPARED && !rendersAgain(plan.steps),
|
|
210
|
+
})
|
|
211
|
+
copy.out.set(INDEX, validationSourceBytes(index))
|
|
212
|
+
return copy.out
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function preparationOf(
|
|
216
|
+
cook: Readonly<CookData> | undefined,
|
|
217
|
+
steps: readonly FormatMigration[],
|
|
218
|
+
): MigratedShardfile['preparation'] {
|
|
219
|
+
if (cook?.version !== PREPARED) {
|
|
220
|
+
return 'none'
|
|
221
|
+
}
|
|
222
|
+
return rendersAgain(steps) ? 'stale' : 'author'
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Run the chain from an original to `to`. Throws, naming the path, when the original fails its own inventory or
|
|
226
|
+
* preparation, sits above the reader or below the floor, or a step leaves a file the reader cannot assemble. */
|
|
227
|
+
function migrateShardfile(
|
|
228
|
+
files: Files,
|
|
229
|
+
options: Readonly<MigrateOptions>,
|
|
230
|
+
): MigratedShardfile {
|
|
231
|
+
const to = options.to ?? SHARDFILE_VERSION
|
|
232
|
+
const migrations = options.migrations ?? FORMAT_MIGRATIONS
|
|
233
|
+
checkMigrations(migrations)
|
|
234
|
+
const admitted = admitOriginal(files, options.hash, to)
|
|
235
|
+
const { mounts, mountText } = readMounts(files, admitted.cook)
|
|
236
|
+
const shard = {
|
|
237
|
+
index: admitted.index,
|
|
238
|
+
sections: readSections(files, admitted.index),
|
|
239
|
+
mounts,
|
|
240
|
+
}
|
|
241
|
+
const window = { from: admitted.from, to }
|
|
242
|
+
const steps = migrations.filter((step: Readonly<FormatMigration>): boolean =>
|
|
243
|
+
runs(step, shard, window),
|
|
244
|
+
)
|
|
245
|
+
const copy =
|
|
246
|
+
steps.length === NONE
|
|
247
|
+
? files
|
|
248
|
+
: writeCopy(files, shard, {
|
|
249
|
+
cook: admitted.cook,
|
|
250
|
+
mountText,
|
|
251
|
+
steps,
|
|
252
|
+
hash: options.hash,
|
|
253
|
+
})
|
|
254
|
+
return {
|
|
255
|
+
files: copy,
|
|
256
|
+
from: admitted.from,
|
|
257
|
+
to: String(admitted.index['version']),
|
|
258
|
+
applied: steps.map((step: Readonly<FormatMigration>): string => step.id),
|
|
259
|
+
chainId: chainId(migrations, options.hash),
|
|
260
|
+
digest: options.hash(copy.get(INDEX) ?? new Uint8Array()),
|
|
261
|
+
preparation: preparationOf(admitted.cook, steps),
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Validate a migrated copy at the reader's format. A render step leaves the author preparation stale, so by default this
|
|
266
|
+
* skips `requireCookPreparation` (the box's cook re-prepares, SP1b); pass `preparation: 'author'` to hold a data-only
|
|
267
|
+
* copy's re-sealed preparation to the full check. */
|
|
268
|
+
function validateMigrated(
|
|
269
|
+
files: Files,
|
|
270
|
+
ports: Readonly<ShardfileValidationPorts>,
|
|
271
|
+
): ValidatedShardfile {
|
|
272
|
+
const read = (path: string, maxBytes: number): Uint8Array => {
|
|
273
|
+
const bytes = files.get(path)
|
|
274
|
+
if (bytes === undefined || bytes.length > maxBytes) {
|
|
275
|
+
throw new Error(`Missing or oversized migrated file: ${path}`)
|
|
276
|
+
}
|
|
277
|
+
return bytes
|
|
278
|
+
}
|
|
279
|
+
return validateShardfileFolder(
|
|
280
|
+
{ read, paths: (): Iterable<string> => files.keys() },
|
|
281
|
+
{ preparation: 'migrated', ...ports },
|
|
282
|
+
)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export {
|
|
286
|
+
type MigrateOptions,
|
|
287
|
+
type MigratedShardfile,
|
|
288
|
+
migrateShardfile,
|
|
289
|
+
validateMigrated,
|
|
290
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// oxlint-disable typescript/prefer-readonly-parameter-types -- a step edits the shard it is handed, in place
|
|
2
|
+
/** A chain step for a break that shipped without one (SHARD-COMPAT CP1, J486): shard-format 036b383 made a journal
|
|
3
|
+
* compendium's icons image-only, so Pine Hollow r1 to r6 (r5 is production's pointer) stopped validating. The step
|
|
4
|
+
* writes each retired glyph name as the inert SVG image the engine drew for it (`journal-glyphs.json`), and raises the
|
|
5
|
+
* declared library budget by those images' rasters, which a glyph never cost. It repairs 0.1.0.1 files too, since the
|
|
6
|
+
* break landed inside 0.1.0.1. */
|
|
7
|
+
import table from './journal-glyphs.json' with { type: 'json' }
|
|
8
|
+
import { parseSketchSvg } from './sketch.ts'
|
|
9
|
+
|
|
10
|
+
import type { FormatMigration, MigratingShard } from './format-migrations.ts'
|
|
11
|
+
|
|
12
|
+
type JsonRecord = Record<string, unknown>
|
|
13
|
+
|
|
14
|
+
const SECTION = 'ui/compendium'
|
|
15
|
+
const GLYPHS: Readonly<Record<string, string>> = table.glyphs
|
|
16
|
+
const PREFIX = 'data:image/svg+xml,'
|
|
17
|
+
const NONE = 0
|
|
18
|
+
|
|
19
|
+
function isRecord(value: unknown): value is JsonRecord {
|
|
20
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The retired glyph an icon names, if it names one. */
|
|
24
|
+
function glyphOf(icon: unknown): string | undefined {
|
|
25
|
+
if (!isRecord(icon)) {
|
|
26
|
+
return undefined
|
|
27
|
+
}
|
|
28
|
+
const { icon: glyph } = icon
|
|
29
|
+
return typeof glyph === 'string' ? glyph : undefined
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A journal row's icons, when the row is a journal with icons. */
|
|
33
|
+
function journalIcons(row: unknown): readonly unknown[] | undefined {
|
|
34
|
+
if (!isRecord(row) || row['kind'] !== 'journal') {
|
|
35
|
+
return undefined
|
|
36
|
+
}
|
|
37
|
+
const { icons } = row
|
|
38
|
+
return Array.isArray(icons) ? icons : undefined
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function compendiumRows(shard: Readonly<MigratingShard>): readonly unknown[] {
|
|
42
|
+
const section = shard.sections.get(SECTION)
|
|
43
|
+
const rows =
|
|
44
|
+
isRecord(section) && isRecord(section['rows'])
|
|
45
|
+
? section['rows']['compendiums']
|
|
46
|
+
: undefined
|
|
47
|
+
return Array.isArray(rows) ? rows : []
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** One icon: a retired glyph becomes its image, tinted by the page as before; any other icon stays as it is. */
|
|
51
|
+
function imageIcon(icon: unknown): unknown {
|
|
52
|
+
const glyph = glyphOf(icon)
|
|
53
|
+
if (glyph === undefined || !isRecord(icon)) {
|
|
54
|
+
return icon
|
|
55
|
+
}
|
|
56
|
+
const image = GLYPHS[glyph]
|
|
57
|
+
if (image === undefined) {
|
|
58
|
+
throw new Error(`No image for the retired journal glyph ${glyph}`)
|
|
59
|
+
}
|
|
60
|
+
const { icon: _retired, ...selectors } = icon
|
|
61
|
+
return { ...selectors, image, tint: 'currentColor' }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Rewrite one journal row's icons; the images it newly names. */
|
|
65
|
+
function migrateRow(row: unknown): readonly string[] {
|
|
66
|
+
const icons = journalIcons(row)
|
|
67
|
+
if (icons === undefined || !isRecord(row)) {
|
|
68
|
+
return []
|
|
69
|
+
}
|
|
70
|
+
row['icons'] = icons.map((icon: unknown): unknown => imageIcon(icon))
|
|
71
|
+
return icons.flatMap((icon: unknown): string[] => {
|
|
72
|
+
const glyph = glyphOf(icon)
|
|
73
|
+
return glyph === undefined ? [] : [GLYPHS[glyph] ?? '']
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The raster bytes the new images add, each distinct image once, as compendium sketch admission counts them. */
|
|
78
|
+
function rasterBytes(images: readonly string[]): number {
|
|
79
|
+
let total = NONE
|
|
80
|
+
for (const image of new Set(images)) {
|
|
81
|
+
const sketch = parseSketchSvg(
|
|
82
|
+
decodeURIComponent(image.slice(PREFIX.length)),
|
|
83
|
+
)
|
|
84
|
+
total += sketch.decoded + sketch.gpu
|
|
85
|
+
}
|
|
86
|
+
return total
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Raise `admission.budgets.library.resident` by `bytes`. */
|
|
90
|
+
function raiseLibrary(index: JsonRecord, bytes: number): void {
|
|
91
|
+
const { admission } = index
|
|
92
|
+
const budgets = isRecord(admission) ? admission['budgets'] : undefined
|
|
93
|
+
const library = isRecord(budgets) ? budgets['library'] : undefined
|
|
94
|
+
if (!isRecord(library) || typeof library['resident'] !== 'number') {
|
|
95
|
+
throw new TypeError(
|
|
96
|
+
'A journal glyph migration needs a declared library budget',
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
library['resident'] += bytes
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function applyJournalImages(shard: MigratingShard): void {
|
|
103
|
+
const images = compendiumRows(shard).flatMap(
|
|
104
|
+
(row: unknown): readonly string[] => migrateRow(row),
|
|
105
|
+
)
|
|
106
|
+
if (images.length > NONE) {
|
|
107
|
+
raiseLibrary(shard.index, rasterBytes(images))
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Whether any journal row still names a retired glyph (0.1.0.1 files written before the break carry them). */
|
|
112
|
+
function namesGlyph(shard: Readonly<MigratingShard>): boolean {
|
|
113
|
+
return compendiumRows(shard).some((row: unknown): boolean =>
|
|
114
|
+
(journalIcons(row) ?? []).some(
|
|
115
|
+
(icon: unknown): boolean => glyphOf(icon) !== undefined,
|
|
116
|
+
),
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const journalGlyphImages: FormatMigration = {
|
|
121
|
+
id: 'journal-glyph-images',
|
|
122
|
+
to: '0.1.0.1',
|
|
123
|
+
touches: 'data',
|
|
124
|
+
affects: [],
|
|
125
|
+
apply: applyJournalImages,
|
|
126
|
+
repairs: namesGlyph,
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export { journalGlyphImages }
|