@wildshard/shard-format 0.66.1 → 0.68.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/package.json +7 -19
- package/src/cli/corpus-deprecations.ts +41 -0
- package/src/cli/corpus.ts +300 -0
- package/src/cli/main.ts +32 -0
- package/src/format/cook-preparation.ts +22 -6
- package/src/format/deprecations.ts +132 -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/items.ts +21 -3
- package/src/format/journal-glyphs.json +12 -0
- package/src/format/lootEconomy.ts +9 -1
- 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/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,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 }
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// oxlint-disable typescript/prefer-readonly-parameter-types -- an op edits the shard it is handed, in place
|
|
2
|
+
/** The migration chain's ops (SHARD-COMPAT FE8): small data-only edits a step is built from, so a step reads as what it
|
|
3
|
+
* changes. Each op addresses one JSON document (the index, a section by name, or a JSON cook mount by path) and a key
|
|
4
|
+
* path inside it, where `*` stands for every element of an array or every value of an object. */
|
|
5
|
+
import type { MigratingShard } from './format-migrations.ts'
|
|
6
|
+
|
|
7
|
+
type JsonRecord = Record<string, unknown>
|
|
8
|
+
type Op = (shard: MigratingShard) => void
|
|
9
|
+
|
|
10
|
+
/** The document an op edits. */
|
|
11
|
+
type Target =
|
|
12
|
+
| { readonly index: true }
|
|
13
|
+
| { readonly section: string }
|
|
14
|
+
| { readonly mount: string }
|
|
15
|
+
|
|
16
|
+
const EVERY = '*'
|
|
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
|
+
function documentOf(shard: Readonly<MigratingShard>, target: Target): unknown {
|
|
24
|
+
if ('index' in target) {
|
|
25
|
+
return shard.index
|
|
26
|
+
}
|
|
27
|
+
if ('section' in target) {
|
|
28
|
+
return shard.sections.get(target.section)
|
|
29
|
+
}
|
|
30
|
+
return shard.mounts.get(target.mount)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The children `key` names under each of `values`. */
|
|
34
|
+
function step(values: readonly unknown[], key: string): unknown[] {
|
|
35
|
+
return values.flatMap((value: unknown): unknown[] => {
|
|
36
|
+
if (key === EVERY) {
|
|
37
|
+
if (Array.isArray(value)) {
|
|
38
|
+
return value
|
|
39
|
+
}
|
|
40
|
+
return isRecord(value) ? Object.values(value) : []
|
|
41
|
+
}
|
|
42
|
+
return isRecord(value) && Object.hasOwn(value, key) ? [value[key]] : []
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Every object at `path` in the target document. */
|
|
47
|
+
function objectsAt(
|
|
48
|
+
shard: Readonly<MigratingShard>,
|
|
49
|
+
target: Target,
|
|
50
|
+
path: readonly string[],
|
|
51
|
+
): JsonRecord[] {
|
|
52
|
+
let values: unknown[] = [documentOf(shard, target)]
|
|
53
|
+
for (const key of path) {
|
|
54
|
+
values = step(values, key)
|
|
55
|
+
}
|
|
56
|
+
return values.filter((value: unknown): value is JsonRecord => isRecord(value))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Rewrite an object's own keys in order, so a renamed key keeps its place. */
|
|
60
|
+
function rewriteKeys(
|
|
61
|
+
row: JsonRecord,
|
|
62
|
+
entries: readonly (readonly [string, unknown])[],
|
|
63
|
+
): void {
|
|
64
|
+
for (const key of Object.keys(row)) {
|
|
65
|
+
Reflect.deleteProperty(row, key)
|
|
66
|
+
}
|
|
67
|
+
for (const [key, value] of entries) {
|
|
68
|
+
row[key] = value
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** `from` becomes `to` in each object at `path`; an object holding both with different values is refused. */
|
|
73
|
+
function rename(
|
|
74
|
+
target: Target,
|
|
75
|
+
path: readonly string[],
|
|
76
|
+
keys: Readonly<{ from: string; to: string }>,
|
|
77
|
+
): Op {
|
|
78
|
+
return (shard: MigratingShard): void => {
|
|
79
|
+
for (const row of objectsAt(shard, target, path)) {
|
|
80
|
+
if (Object.hasOwn(row, keys.from)) {
|
|
81
|
+
if (Object.hasOwn(row, keys.to) && row[keys.to] !== row[keys.from]) {
|
|
82
|
+
throw new Error(`Rename ${keys.from} to ${keys.to}: both are set`)
|
|
83
|
+
}
|
|
84
|
+
const entries = Object.entries(row)
|
|
85
|
+
.filter(
|
|
86
|
+
([key]: readonly [string, unknown]): boolean => key !== keys.to,
|
|
87
|
+
)
|
|
88
|
+
.map(
|
|
89
|
+
([key, value]: readonly [string, unknown]): readonly [
|
|
90
|
+
string,
|
|
91
|
+
unknown,
|
|
92
|
+
] => (key === keys.from ? [keys.to, value] : [key, value]),
|
|
93
|
+
)
|
|
94
|
+
rewriteKeys(row, entries)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** `key` leaves each object at `path`. */
|
|
101
|
+
function drop(target: Target, path: readonly string[], key: string): Op {
|
|
102
|
+
return (shard: MigratingShard): void => {
|
|
103
|
+
for (const row of objectsAt(shard, target, path)) {
|
|
104
|
+
Reflect.deleteProperty(row, key)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Each object at `path` without `key` gets `value` (a fresh copy each time). */
|
|
110
|
+
function fill(
|
|
111
|
+
target: Target,
|
|
112
|
+
path: readonly string[],
|
|
113
|
+
field: Readonly<{ key: string; value: unknown }>,
|
|
114
|
+
): Op {
|
|
115
|
+
return (shard: MigratingShard): void => {
|
|
116
|
+
for (const row of objectsAt(shard, target, path)) {
|
|
117
|
+
if (!Object.hasOwn(row, field.key)) {
|
|
118
|
+
row[field.key] = structuredClone(field.value)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Each object at `path` holding `key` has its value replaced by `change(value)`. */
|
|
125
|
+
function map(
|
|
126
|
+
target: Target,
|
|
127
|
+
path: readonly string[],
|
|
128
|
+
field: Readonly<{ key: string; change: (value: unknown) => unknown }>,
|
|
129
|
+
): Op {
|
|
130
|
+
return (shard: MigratingShard): void => {
|
|
131
|
+
for (const row of objectsAt(shard, target, path)) {
|
|
132
|
+
if (Object.hasOwn(row, field.key)) {
|
|
133
|
+
row[field.key] = field.change(row[field.key])
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The section `from` becomes `to` under schema id `schema`: its payload, index row and file path move together. */
|
|
140
|
+
function moveSection(
|
|
141
|
+
sections: Readonly<{ from: string; to: string; schema: string }>,
|
|
142
|
+
): Op {
|
|
143
|
+
return (shard: MigratingShard): void => {
|
|
144
|
+
const payload = shard.sections.get(sections.from)
|
|
145
|
+
const rows: unknown = shard.index['sections']
|
|
146
|
+
const row: unknown = Array.isArray(rows)
|
|
147
|
+
? rows.find(
|
|
148
|
+
(entry: unknown): boolean =>
|
|
149
|
+
isRecord(entry) && entry['name'] === sections.from,
|
|
150
|
+
)
|
|
151
|
+
: undefined
|
|
152
|
+
if (payload === undefined || !isRecord(row)) {
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
if (shard.sections.has(sections.to)) {
|
|
156
|
+
throw new Error(`Move ${sections.from} to ${sections.to}: it exists`)
|
|
157
|
+
}
|
|
158
|
+
shard.sections.delete(sections.from)
|
|
159
|
+
shard.sections.set(sections.to, payload)
|
|
160
|
+
Object.assign(row, {
|
|
161
|
+
name: sections.to,
|
|
162
|
+
path: `sections/${sections.to}.json`,
|
|
163
|
+
schema: sections.schema,
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Every string equal to `from` under one of `keys`, anywhere in a section or JSON mount, becomes `to`. */
|
|
169
|
+
function renameKind(
|
|
170
|
+
kinds: Readonly<{ from: string; to: string; keys: readonly string[] }>,
|
|
171
|
+
): Op {
|
|
172
|
+
const keys = new Set(kinds.keys)
|
|
173
|
+
const visit = (value: unknown): void => {
|
|
174
|
+
if (Array.isArray(value)) {
|
|
175
|
+
for (const item of value) {
|
|
176
|
+
visit(item)
|
|
177
|
+
}
|
|
178
|
+
} else if (isRecord(value)) {
|
|
179
|
+
for (const [key, item] of Object.entries(value)) {
|
|
180
|
+
if (keys.has(key) && item === kinds.from) {
|
|
181
|
+
value[key] = kinds.to
|
|
182
|
+
} else {
|
|
183
|
+
visit(item)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return (shard: MigratingShard): void => {
|
|
189
|
+
for (const value of [
|
|
190
|
+
...shard.sections.values(),
|
|
191
|
+
...shard.mounts.values(),
|
|
192
|
+
]) {
|
|
193
|
+
visit(value)
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Run ops in order as one step's `apply`. */
|
|
199
|
+
function ops(...steps: readonly Op[]): Op {
|
|
200
|
+
return (shard: MigratingShard): void => {
|
|
201
|
+
for (const op of steps) {
|
|
202
|
+
op(shard)
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Whether any object at `path` still holds `key` (a repair step's `repairs`). */
|
|
208
|
+
function holds(
|
|
209
|
+
target: Target,
|
|
210
|
+
path: readonly string[],
|
|
211
|
+
key: string,
|
|
212
|
+
): (shard: Readonly<MigratingShard>) => boolean {
|
|
213
|
+
return (shard: Readonly<MigratingShard>): boolean =>
|
|
214
|
+
objectsAt(shard, target, path).filter(
|
|
215
|
+
(row: Readonly<JsonRecord>): boolean => Object.hasOwn(row, key),
|
|
216
|
+
).length > NONE
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export {
|
|
220
|
+
type Op,
|
|
221
|
+
type Target,
|
|
222
|
+
drop,
|
|
223
|
+
fill,
|
|
224
|
+
holds,
|
|
225
|
+
map,
|
|
226
|
+
moveSection,
|
|
227
|
+
ops,
|
|
228
|
+
rename,
|
|
229
|
+
renameKind,
|
|
230
|
+
}
|