@wildshard/shard-format 0.68.1 → 0.69.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 +1 -1
- package/src/cli/pack.ts +1 -1
- package/src/format/deprecations.ts +34 -0
- package/src/format/format-migrate.ts +7 -3
- package/src/format/format-migration-envelope.ts +74 -0
- package/src/format/format-migrations.ts +6 -0
- package/src/format/index-format-version.ts +33 -0
- package/src/format/mixedPopulation.ts +6 -3
- package/src/format/outputDownload.ts +6 -2
- package/src/format/outputFields.ts +2 -1
- package/src/format/outputIndex.ts +55 -17
- package/src/format/shardSections.ts +4 -3
- package/src/format/shardfile-reads.ts +3 -2
- package/src/format/shardfile-stream-packs.ts +1 -1
- package/src/format/stateLineage.ts +8 -5
- package/src/format/version.ts +89 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wildshard/shard-format",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.69.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": {
|
package/src/cli/pack.ts
CHANGED
|
@@ -58,7 +58,7 @@ interface HeldIndex {
|
|
|
58
58
|
readonly build: Readonly<{ sourceHash: string }>
|
|
59
59
|
readonly files: readonly Listed[]
|
|
60
60
|
readonly packs?: readonly PackRow[] | undefined
|
|
61
|
-
readonly schemas
|
|
61
|
+
readonly schemas?: readonly Listed[] | undefined
|
|
62
62
|
readonly sections: readonly Section[]
|
|
63
63
|
}
|
|
64
64
|
|
|
@@ -33,6 +33,16 @@ const LEGACY_SPELLING = '0.1'
|
|
|
33
33
|
const NONE = 0
|
|
34
34
|
/** Deprecated in the current format with no data change: a shard file of any version may still carry it. */
|
|
35
35
|
const CURRENT = '0.1.0.1'
|
|
36
|
+
/** Format 0.1.0.2's envelope (SHARD-COMPAT CP14, FORMAT-PRUNE FP4 and FP25). */
|
|
37
|
+
const ENVELOPE = '0.1.0.2'
|
|
38
|
+
const LEGACY_LAYOUT = 2
|
|
39
|
+
/** FORMAT-PRUNE FP4: always empty, never read. */
|
|
40
|
+
const EMPTY_INDEX_FIELDS: readonly (readonly [string, string | undefined])[] = [
|
|
41
|
+
['schemas', undefined],
|
|
42
|
+
['runtime', 'files'],
|
|
43
|
+
['runtime', 'lines'],
|
|
44
|
+
['requires', 'capabilities'],
|
|
45
|
+
]
|
|
36
46
|
/** FORMAT-PRUNE FP15: OF30 cut B leaves these as plain JSON nothing reads. */
|
|
37
47
|
const SPECIES_PROGRAM_FAMILY: readonly string[] = [
|
|
38
48
|
'actors/species-programs',
|
|
@@ -72,6 +82,30 @@ const RULES: readonly DeprecationRule[] = [
|
|
|
72
82
|
uses: (shard: Readonly<DeprecationInput>): boolean =>
|
|
73
83
|
field(shard.index, 'version') === LEGACY_SPELLING,
|
|
74
84
|
},
|
|
85
|
+
{
|
|
86
|
+
id: 'shard.version',
|
|
87
|
+
path: 'shard.json version beside the layout marker formatVersion 2 (SHARD-COMPAT CP14)',
|
|
88
|
+
since: ENVELOPE,
|
|
89
|
+
replacement:
|
|
90
|
+
'formatVersion: the four-part format version, with no version field',
|
|
91
|
+
migration: 'cp14-format-version',
|
|
92
|
+
uses: (shard: Readonly<DeprecationInput>): boolean =>
|
|
93
|
+
field(shard.index, 'formatVersion') === LEGACY_LAYOUT,
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
id: 'shard.empty-fields',
|
|
97
|
+
path: 'shard.json schemas, runtime.files, runtime.lines and requires.capabilities (FORMAT-PRUNE FP4)',
|
|
98
|
+
since: ENVELOPE,
|
|
99
|
+
replacement: 'nothing: each is always empty and no reader reads it',
|
|
100
|
+
migration: 'fp4-drop-empty-index-fields',
|
|
101
|
+
uses: (shard: Readonly<DeprecationInput>): boolean =>
|
|
102
|
+
EMPTY_INDEX_FIELDS.some(
|
|
103
|
+
([outer, inner]: readonly [string, string | undefined]): boolean =>
|
|
104
|
+
inner === undefined
|
|
105
|
+
? field(shard.index, outer) !== undefined
|
|
106
|
+
: field(field(shard.index, outer), inner) !== undefined,
|
|
107
|
+
),
|
|
108
|
+
},
|
|
75
109
|
{
|
|
76
110
|
id: 'manifest.gridShardfile',
|
|
77
111
|
path: 'sections/ui/level manifest.gridShardfile (and the discovery mount)',
|
|
@@ -17,6 +17,10 @@ import {
|
|
|
17
17
|
verifyInventory,
|
|
18
18
|
} from './format-migrate-inventory.ts'
|
|
19
19
|
import { rebuildInventory } from './format-migrate-rebuild.ts'
|
|
20
|
+
import {
|
|
21
|
+
declaredShardfileFormat,
|
|
22
|
+
writeShardfileFormat,
|
|
23
|
+
} from './format-migration-envelope.ts'
|
|
20
24
|
import {
|
|
21
25
|
FORMAT_MIGRATIONS,
|
|
22
26
|
checkMigrations,
|
|
@@ -117,7 +121,7 @@ function admitOriginal(files: Files, hash: Hash, to: string): Admitted {
|
|
|
117
121
|
const cook = rawCook === undefined ? undefined : readCookData(rawCook)
|
|
118
122
|
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- read only for its slug, sdk, files and cook row
|
|
119
123
|
requireCookPreparation(index as unknown as OutputIndex, cook, hash)
|
|
120
|
-
return { index, cook, from: checkWindow(index
|
|
124
|
+
return { index, cook, from: checkWindow(declaredShardfileFormat(index), to) }
|
|
121
125
|
}
|
|
122
126
|
|
|
123
127
|
const MOUNT = /^mounts\/.*\.json$/u
|
|
@@ -154,7 +158,7 @@ function runSteps(
|
|
|
154
158
|
): void {
|
|
155
159
|
for (const step of steps) {
|
|
156
160
|
step.apply(shard)
|
|
157
|
-
shard.index
|
|
161
|
+
writeShardfileFormat(shard.index, step.to)
|
|
158
162
|
}
|
|
159
163
|
}
|
|
160
164
|
|
|
@@ -254,7 +258,7 @@ function migrateShardfile(
|
|
|
254
258
|
return {
|
|
255
259
|
files: copy,
|
|
256
260
|
from: admitted.from,
|
|
257
|
-
to: String(admitted.index
|
|
261
|
+
to: String(declaredShardfileFormat(admitted.index)),
|
|
258
262
|
applied: steps.map((step: Readonly<FormatMigration>): string => step.id),
|
|
259
263
|
chainId: chainId(migrations, options.hash),
|
|
260
264
|
digest: options.hash(copy.get(INDEX) ?? new Uint8Array()),
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// oxlint-disable typescript/prefer-readonly-parameter-types -- a step edits the shard it is handed, in place
|
|
2
|
+
/** Format 0.1.0.2's chain steps (SHARD-COMPAT CP14, FORMAT-PRUNE FP4 and FP25, J486). The runner writes the copy's
|
|
3
|
+
* version into `formatVersion` from 0.1.0.2 on (`writeShardfileFormat`), replacing the frozen layout marker `2`; these
|
|
4
|
+
* steps drop the fields the new envelope no longer carries. Every dropped field held nothing: `version` repeats the
|
|
5
|
+
* format version, and the other four are always empty with no reader. */
|
|
6
|
+
import { LEGACY_LAYOUT, writesIndexFormatVersion } from './version.ts'
|
|
7
|
+
|
|
8
|
+
import type { FormatMigration, MigratingShard } from './format-migrations.ts'
|
|
9
|
+
|
|
10
|
+
type JsonRecord = Record<string, unknown>
|
|
11
|
+
|
|
12
|
+
const ENVELOPE = '0.1.0.2'
|
|
13
|
+
|
|
14
|
+
function isRecord(value: unknown): value is JsonRecord {
|
|
15
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** SHARD-COMPAT CP14 (FP25): `version` becomes `formatVersion`; the layout marker goes in the same step. */
|
|
19
|
+
const formatVersionField: FormatMigration = {
|
|
20
|
+
id: 'cp14-format-version',
|
|
21
|
+
to: ENVELOPE,
|
|
22
|
+
touches: 'data',
|
|
23
|
+
affects: [],
|
|
24
|
+
apply: (shard: MigratingShard): void => {
|
|
25
|
+
Reflect.deleteProperty(shard.index, 'version')
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** FORMAT-PRUNE FP4: `schemas`, `runtime.files`, `runtime.lines` and `requires.capabilities`, always empty, never read. */
|
|
30
|
+
const dropEmptyIndexFields: FormatMigration = {
|
|
31
|
+
id: 'fp4-drop-empty-index-fields',
|
|
32
|
+
to: ENVELOPE,
|
|
33
|
+
touches: 'data',
|
|
34
|
+
affects: [],
|
|
35
|
+
apply: (shard: MigratingShard): void => {
|
|
36
|
+
const { index } = shard
|
|
37
|
+
Reflect.deleteProperty(index, 'schemas')
|
|
38
|
+
const { runtime, requires } = index
|
|
39
|
+
if (isRecord(runtime)) {
|
|
40
|
+
Reflect.deleteProperty(runtime, 'files')
|
|
41
|
+
Reflect.deleteProperty(runtime, 'lines')
|
|
42
|
+
}
|
|
43
|
+
if (isRecord(requires)) {
|
|
44
|
+
Reflect.deleteProperty(requires, 'capabilities')
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The format version a `shard.json` declares, as written, or `undefined`: `formatVersion` from 0.1.0.2, else the
|
|
50
|
+
* legacy `version` beside the layout marker. */
|
|
51
|
+
function declaredShardfileFormat(index: Readonly<JsonRecord>): unknown {
|
|
52
|
+
const declared: unknown = index['formatVersion']
|
|
53
|
+
return declared === LEGACY_LAYOUT || declared === undefined
|
|
54
|
+
? index['version']
|
|
55
|
+
: declared
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Write a copy's format version the way that version spells it: `formatVersion` from 0.1.0.2 (with no `version`),
|
|
59
|
+
* else `version` beside the layout marker. */
|
|
60
|
+
function writeShardfileFormat(index: JsonRecord, version: string): void {
|
|
61
|
+
if (writesIndexFormatVersion(version)) {
|
|
62
|
+
Reflect.deleteProperty(index, 'version')
|
|
63
|
+
index['formatVersion'] = version
|
|
64
|
+
} else {
|
|
65
|
+
index['version'] = version
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export {
|
|
70
|
+
declaredShardfileFormat,
|
|
71
|
+
dropEmptyIndexFields,
|
|
72
|
+
formatVersionField,
|
|
73
|
+
writeShardfileFormat,
|
|
74
|
+
}
|
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
/** The migration chain's registry (SHARD-COMPAT CP1, J486). A migration is one pure, data-only step that rewrites an
|
|
3
3
|
* older shard file's JSON to a newer format version. It is not a save migration (`migrations.ts`) and not a catalogue
|
|
4
4
|
* schema change (api-server). The change's author adds its migration here in the same release (J486, Q49). */
|
|
5
|
+
import {
|
|
6
|
+
dropEmptyIndexFields,
|
|
7
|
+
formatVersionField,
|
|
8
|
+
} from './format-migration-envelope.ts'
|
|
5
9
|
import { journalGlyphImages } from './format-migration-journal.ts'
|
|
6
10
|
import { dropRuntimeTrust, shardfilePointer } from './format-migration-prune.ts'
|
|
7
11
|
import {
|
|
@@ -65,6 +69,8 @@ const FORMAT_MIGRATIONS: readonly FormatMigration[] = [
|
|
|
65
69
|
journalGlyphImages,
|
|
66
70
|
shardfilePointer,
|
|
67
71
|
dropRuntimeTrust,
|
|
72
|
+
formatVersionField,
|
|
73
|
+
dropEmptyIndexFields,
|
|
68
74
|
]
|
|
69
75
|
|
|
70
76
|
/** Whether a file at `version` still needs a step that writes `to`. The legacy two-part spelling sits just below its
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** The two `shard.json` envelopes' version fields (SHARD-COMPAT CP14, FORMAT-PRUNE FP25), shared by every index
|
|
2
|
+
* reader: the full parser (`outputIndex.ts`), the lineage reader and the download reader. */
|
|
3
|
+
import { exactOptional, literal, picklist, union } from 'valibot'
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
INDEX_FORMAT_VERSIONS,
|
|
7
|
+
LEGACY_INDEX_VERSIONS,
|
|
8
|
+
LEGACY_LAYOUT,
|
|
9
|
+
} from './version.ts'
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
ExactOptionalSchema,
|
|
13
|
+
LiteralSchema,
|
|
14
|
+
PicklistSchema,
|
|
15
|
+
UnionSchema,
|
|
16
|
+
} from 'valibot'
|
|
17
|
+
|
|
18
|
+
/** `formatVersion`: the frozen layout marker `2` (formats 0.1 to 0.1.0.1), or the format version itself (0.1.0.2 on). */
|
|
19
|
+
const IndexFormatVersionSchema: UnionSchema<
|
|
20
|
+
[
|
|
21
|
+
LiteralSchema<typeof LEGACY_LAYOUT, undefined>,
|
|
22
|
+
PicklistSchema<typeof INDEX_FORMAT_VERSIONS, undefined>,
|
|
23
|
+
],
|
|
24
|
+
undefined
|
|
25
|
+
> = union([literal(LEGACY_LAYOUT), picklist(INDEX_FORMAT_VERSIONS)])
|
|
26
|
+
|
|
27
|
+
/** `version`: only the legacy envelope carries it, beside the layout marker. */
|
|
28
|
+
const LegacyIndexVersionSchema: ExactOptionalSchema<
|
|
29
|
+
PicklistSchema<typeof LEGACY_INDEX_VERSIONS, undefined>,
|
|
30
|
+
undefined
|
|
31
|
+
> = exactOptional(picklist(LEGACY_INDEX_VERSIONS))
|
|
32
|
+
|
|
33
|
+
export { IndexFormatVersionSchema, LegacyIndexVersionSchema }
|
|
@@ -8,12 +8,15 @@ const completeReturn:v.GenericSchema<v.InferInput<typeof ReturnProgramSchema>,v.
|
|
|
8
8
|
const finite = v.pipe(v.number(), v.finite());
|
|
9
9
|
const key = v.pipe(v.string(), v.minLength(1), v.maxLength(128));
|
|
10
10
|
const point = v.strictObject({ x: finite, z: finite });
|
|
11
|
+
/** FORMAT-PRUNE FP3: no reader in any repo; optional now, and Driftwood stops writing it at its Track F row (SM1). */
|
|
12
|
+
const legacyMemory = v.exactOptional(v.array(v.strictObject({ from: key, to: key, axis: v.picklist(['x', 'z', 'zero']) })));
|
|
11
13
|
/** Mixed native roster policy, pose, reserve and physical volley composition. */
|
|
12
14
|
export const MixedPopulationSchema: v.StrictObjectSchema<{ readonly step: v.GenericSchema<MixedPopulationRowsInput['step'], MixedPopulationRows['step']>; readonly limit: v.GenericSchema<MixedPopulationRowsInput['limit'], MixedPopulationRows['limit']>; readonly attackTurnCap: v.GenericSchema<MixedPopulationRowsInput['attackTurnCap'], MixedPopulationRows['attackTurnCap']>; readonly hunted: v.GenericSchema<MixedPopulationRowsInput['hunted'], MixedPopulationRows['hunted']>; readonly floorKinds: v.GenericSchema<MixedPopulationRowsInput['floorKinds'], MixedPopulationRows['floorKinds']>; readonly practice: v.GenericSchema<MixedPopulationRowsInput['practice'], MixedPopulationRows['practice']>; readonly reserve: v.GenericSchema<MixedPopulationRowsInput['reserve'], MixedPopulationRows['reserve']>; readonly volley: v.GenericSchema<MixedPopulationRowsInput['volley'], MixedPopulationRows['volley']>; readonly poses: v.GenericSchema<MixedPopulationRowsInput['poses'], MixedPopulationRows['poses']>; readonly perch: v.GenericSchema<MixedPopulationRowsInput['perch'], MixedPopulationRows['perch']>; }, undefined> = v.strictObject({
|
|
13
15
|
step: key, limit: v.pipe(finite, v.integer(), v.minValue(1), v.maxValue(256)),
|
|
14
16
|
attackTurnCap: finite, hunted: v.array(key), floorKinds: v.array(key),
|
|
15
17
|
practice: v.strictObject({ at: point, fade: finite, delay: finite, away: finite }),
|
|
16
|
-
reserve: v.strictObject({ kind: key, variant: key, scale: v.tuple([finite, finite]),
|
|
18
|
+
reserve: v.strictObject({ kind: key, variant: key, scale: v.tuple([finite, finite]),
|
|
19
|
+
legacyMemory }),
|
|
17
20
|
volley: v.strictObject({ owner: key, strike: key, tags: v.array(v.custom<`${string}.${string}`>(value => typeof value === 'string' && /^[a-z][a-zA-Z0-9.-]*\.[a-zA-Z0-9.-]+$/u.test(value))) }),
|
|
18
21
|
poses: v.array(v.strictObject({ kind: key, clips: v.array(NumericPoseSchema),
|
|
19
22
|
lift: v.exactOptional(v.tuple([finite, finite, finite, finite])) })),
|
|
@@ -66,7 +69,7 @@ interface MixedPopulationRowsInputFields {
|
|
|
66
69
|
number,
|
|
67
70
|
number
|
|
68
71
|
];
|
|
69
|
-
legacyMemory
|
|
72
|
+
legacyMemory?: ({
|
|
70
73
|
from: string;
|
|
71
74
|
to: string;
|
|
72
75
|
axis: 'x' | 'z' | 'zero';
|
|
@@ -154,7 +157,7 @@ interface MixedPopulationRowsFields {
|
|
|
154
157
|
number,
|
|
155
158
|
number
|
|
156
159
|
];
|
|
157
|
-
legacyMemory
|
|
160
|
+
legacyMemory?: ({
|
|
158
161
|
from: string;
|
|
159
162
|
to: string;
|
|
160
163
|
axis: 'x' | 'z' | 'zero';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as v from 'valibot';
|
|
2
|
+
import { IndexFormatVersionSchema } from './index-format-version.ts';
|
|
2
3
|
|
|
3
4
|
interface Measured { readonly path: string; readonly hash: string; readonly bytes: number }
|
|
4
5
|
export interface OutputDownloadIndex {
|
|
@@ -18,9 +19,11 @@ export function readOutputDownloadIndex(input: unknown): OutputDownloadIndex {
|
|
|
18
19
|
const bytes = v.pipe(v.number(), v.safeInteger(), v.minValue(0));
|
|
19
20
|
const hash = v.pipe(v.string(), v.regex(/^[a-f0-9]{64}$/u));
|
|
20
21
|
const measured = { path, hash, bytes };
|
|
21
|
-
const
|
|
22
|
+
const listed = v.pipe(v.array(v.object(measured)), v.maxLength(128));
|
|
23
|
+
// SHARD-COMPAT CP14: either envelope; format 0.1.0.2 drops the always-empty schemas list (FORMAT-PRUNE FP4).
|
|
24
|
+
const parsed = v.parse(v.object({ format: v.literal('wildshard.shardfile'), formatVersion: IndexFormatVersionSchema,
|
|
22
25
|
sections: v.pipe(v.array(v.object({ ...measured, name: v.string() })), v.maxLength(256)),
|
|
23
|
-
schemas: v.
|
|
26
|
+
schemas: v.exactOptional(listed),
|
|
24
27
|
// The compiler and runtime share tier/variant selection. Dropping these
|
|
25
28
|
// existing index facts here reserves bodies this device will never read.
|
|
26
29
|
files: v.pipe(v.array(v.object({ path, hash, compressed: bytes, name: v.exactOptional(v.string()),
|
|
@@ -28,6 +31,7 @@ export function readOutputDownloadIndex(input: unknown): OutputDownloadIndex {
|
|
|
28
31
|
runtime: v.unknown(),
|
|
29
32
|
packs: v.exactOptional(v.pipe(v.array(v.object({ ...measured, entries: v.pipe(v.array(v.object({ hash, offset: bytes, bytes })), v.maxLength(8192)) })), v.maxLength(64))),
|
|
30
33
|
}), input);
|
|
34
|
+
const index = { ...parsed, schemas: parsed.schemas ?? [] };
|
|
31
35
|
const paths = ['shard.json', 'validation.json', ...index.sections.map(row => row.path), ...index.schemas.map(row => row.path), ...index.files.map(row => row.path)];
|
|
32
36
|
if (new Set(paths).size !== paths.length) throw new Error('Duplicate shard file download path');
|
|
33
37
|
return index;
|
|
@@ -2,6 +2,7 @@ import type { Shardfile } from './shardSchema.ts';
|
|
|
2
2
|
import type { PlatformSectionName } from './sections.ts';
|
|
3
3
|
import type { PlatformDataSections } from './platformDataSections.ts';
|
|
4
4
|
import type { OutputIndex } from './outputIndex.ts';
|
|
5
|
+
import { shardfileFormatOf } from './version.ts';
|
|
5
6
|
|
|
6
7
|
export interface SectionMapping { readonly name: PlatformSectionName; readonly fields: readonly (keyof Shardfile)[]; readonly row?: keyof Shardfile['rows']; readonly spawns?: true; }
|
|
7
8
|
/** Root-key envelopes preserve optional-field absence and every existing host's values. */
|
|
@@ -105,7 +106,7 @@ export function outputSectionPayloads(source: Shardfile): ReadonlyMap<string, un
|
|
|
105
106
|
export function assembleOutputTransport(index: OutputIndex, sections: ReadonlyMap<string, unknown>, checks?: { readonly bounds: (name: string, value: unknown) => void; readonly platform: (name: keyof PlatformDataSections, value: unknown) => void; readonly fields: (name: string, value: object) => Record<string, unknown> }): Record<string, unknown> {
|
|
106
107
|
// J91: the server class is the product index's alone, never a field of the Shardfile a client admits.
|
|
107
108
|
const {server:_server,audioWaiver:_audioWaiver,...admission}=index.admission;
|
|
108
|
-
const result: Record<string, unknown> = {version:index
|
|
109
|
+
const result: Record<string, unknown> = {version:shardfileFormatOf(index),identity:index.identity,requires:index.requires,...admission,files:index.files.filter(file => file.commons !== true).map(({path: _path,name: _name,tier: _tier,variantOf: _variant,commons: _commons,jsonSchema: _schema,kind,...rest}) => Object.assign(rest,{kind:kind === 'tile' || kind === 'bin' ? 'binary' : kind})),runtime:index.runtime === null ? null : {kind:index.runtime.kind,cost:index.runtime.cost,binds:index.runtime.binds}};
|
|
109
110
|
if (sections.size !== index.sections.length || index.sections.some(entry => !sections.has(entry.name))) throw new Error('Missing or surplus output section');
|
|
110
111
|
const rows: Record<string, unknown> = {};
|
|
111
112
|
for (const entry of index.sections) {
|
|
@@ -15,8 +15,9 @@ import { ClientPlanRuntimeSchema } from './runtime.ts';
|
|
|
15
15
|
import { RuntimeSpawnsSchema } from './runtimeSpawns.ts';
|
|
16
16
|
import { RowsSchema } from './rows.ts';
|
|
17
17
|
import { ShardHashSchema, ShardNaturalSchema, IdentitySchema, RequiresSchema, FilesSchema, type ShardIdentityInput, type ShardRequiresInput, type ShardReferencesInput, type ShardIdentity, type ShardRequires, type ShardReferences } from './shardSections.ts';
|
|
18
|
-
import {
|
|
19
|
-
import
|
|
18
|
+
import { LEGACY_LAYOUT } from './version.ts';
|
|
19
|
+
import { IndexFormatVersionSchema, LegacyIndexVersionSchema } from './index-format-version.ts';
|
|
20
|
+
import type { IndexFormatVersion, LegacyIndexVersion } from './version.ts';
|
|
20
21
|
import { isJsonData, isJsonDataWithFileSchemas } from './json.ts';
|
|
21
22
|
import { clientOwnershipRules } from './clientOwnership.ts';
|
|
22
23
|
import { PLATFORM_DATA_SECTIONS } from './platformDataSections.ts';
|
|
@@ -27,17 +28,24 @@ const path = v.pipe(v.string(), v.maxLength(240), v.regex(/^(?:[a-zA-Z0-9_-][a-z
|
|
|
27
28
|
const sectionName = v.pipe(v.string(), v.regex(/^[a-z][a-zA-Z0-9-]*\/[a-zA-Z][a-zA-Z0-9_-]*$/u));
|
|
28
29
|
const measuredFile = { path, hash: ShardHashSchema, bytes: ShardNaturalSchema };
|
|
29
30
|
const section = v.strictObject({ ...measuredFile, name: sectionName, schema: v.pipe(v.string(), v.maxLength(240)) });
|
|
31
|
+
/** Always empty; format 0.1.0.2 drops it (FORMAT-PRUNE FP4). */
|
|
32
|
+
const emptySchemas = v.exactOptional(v.pipe(v.array(v.strictObject(measuredFile)), v.maxLength(0)));
|
|
30
33
|
const cookRow = v.exactOptional(v.strictObject({ ...measuredFile, path: v.literal('cook.json') }));
|
|
31
34
|
const costs = v.omit(FilesSchema.pipe[0].item, ['kind']).entries;
|
|
32
35
|
const fileSchema = v.strictObject({ ...costs, path, kind: v.picklist(['glb', 'ktx2', 'audio', 'json', 'wasm', 'tile', 'bin', 'image']), name: v.exactOptional(v.pipe(v.string(), v.maxLength(240))), tier: v.exactOptional(v.picklist(['phone', 'desktop'])), variantOf: v.exactOptional(v.pipe(v.string(), v.minLength(1), v.maxLength(240))), commons: v.exactOptional(v.literal(true)), jsonSchema: v.exactOptional(v.record(v.string(), v.unknown())) });
|
|
33
36
|
/** `trust` is retired (OF1): new indexes omit it, old products still carry their value and nothing reads it. */
|
|
34
|
-
|
|
37
|
+
/** `files` and `lines` are always empty: the 0.1.0.2 envelope drops them (FORMAT-PRUNE FP4), the legacy one still carries them. */
|
|
38
|
+
const runtimeFiles = v.exactOptional(v.pipe(v.array(v.strictObject({ ...measuredFile, lines: ShardNaturalSchema })), v.maxLength(0)));
|
|
39
|
+
const runtime = v.strictObject({ ...v.omit(ClientPlanRuntimeSchema.pipe[0], ['spawns']).entries, trust: v.exactOptional(v.unknown()), files: runtimeFiles, lines: v.exactOptional(v.literal(0)) });
|
|
35
40
|
/** Format 2 inventories every emitted byte except its index and validation receipt. */
|
|
36
41
|
export const OutputIndexSchema: v.GenericSchema<OutputIndexRowsInput, OutputIndexRows> & { readonly entries: { readonly [Key in keyof OutputIndexRows]-?: v.GenericSchema<OutputIndexRowsInput[Key], OutputIndexRows[Key]> } } = v.strictObject({
|
|
37
|
-
format: v.literal('wildshard.shardfile'),
|
|
42
|
+
format: v.literal('wildshard.shardfile'),
|
|
43
|
+
/** SHARD-COMPAT CP14 (FP25): the format version itself from 0.1.0.2; the legacy envelope carries the frozen layout marker
|
|
44
|
+
* `2` here and its version in `version`. `checkIndexEnvelope` holds each envelope to its own fields. */
|
|
45
|
+
formatVersion: IndexFormatVersionSchema, sdk: v.picklist(['0.2', '0.3']), version: LegacyIndexVersionSchema,
|
|
38
46
|
identity: IdentitySchema, build: v.strictObject({ sourceHash: ShardHashSchema, validator: v.pipe(v.string(), v.minLength(1), v.maxLength(160)) }), requires: RequiresSchema,
|
|
39
47
|
admission: v.strictObject({ budgets: ShardfileFieldSchemas.budgets, serverBudget: ShardfileFieldSchemas.serverBudget, authorCaps: ShardfileFieldSchemas.authorCaps, critical: ShardfileFieldSchemas.critical, library: ShardfileFieldSchemas.library, late: v.exactOptional(AdmissionLateSchema), server: v.exactOptional(AdmissionServerSchema), audioWaiver: v.exactOptional(AudioWaiverSchema) }),
|
|
40
|
-
sections: v.pipe(v.array(section), v.maxLength(256)), schemas:
|
|
48
|
+
sections: v.pipe(v.array(section), v.maxLength(256)), schemas: emptySchemas,
|
|
41
49
|
files: v.pipe(v.array(fileSchema), v.maxLength(8192)), runtime: v.nullable(runtime),
|
|
42
50
|
ktx2Costs: v.exactOptional(v.pipe(v.record(ShardHashSchema,v.strictObject({rgba:ShardNaturalSchema,astc:ShardNaturalSchema,bc7:ShardNaturalSchema})),v.check(rows=>Object.keys(rows).length<=8192,'bounded texture cost table'))),
|
|
43
51
|
/** iPhone downloads I5: content-hashed concatenations of the small sections, schemas and files, so a reader can fetch
|
|
@@ -63,8 +71,38 @@ export function parseOutputDescriptorIndex(input: unknown): OutputIndex | null {
|
|
|
63
71
|
if (!isJsonDataWithFileSchemas(input)) throw new Error('Shardfile descriptor requires bounded plain JSON');
|
|
64
72
|
return isOutputIndex(input) ? parseOutputIndexRows(input) : null;
|
|
65
73
|
}
|
|
74
|
+
/** The fields only the legacy envelope (`formatVersion: 2`, formats 0.1 to 0.1.0.1) carries; 0.1.0.2 drops each
|
|
75
|
+
* (SHARD-COMPAT CP14; FORMAT-PRUNE FP3 `runtime.trust`, FP4 the always-empty four). */
|
|
76
|
+
// oxlint-disable-next-line typescript/prefer-readonly-parameter-types -- valibot output, only read
|
|
77
|
+
function legacyIndexFields(index: OutputIndex): readonly string[] {
|
|
78
|
+
return [
|
|
79
|
+
index.version === undefined ? undefined : 'version',
|
|
80
|
+
index.schemas === undefined ? undefined : 'schemas',
|
|
81
|
+
index.requires.capabilities === undefined ? undefined : 'requires.capabilities',
|
|
82
|
+
index.runtime?.files === undefined ? undefined : 'runtime.files',
|
|
83
|
+
index.runtime?.lines === undefined ? undefined : 'runtime.lines',
|
|
84
|
+
index.runtime === null || !Object.hasOwn(index.runtime, 'trust') ? undefined : 'runtime.trust',
|
|
85
|
+
].filter((field): field is string => field !== undefined);
|
|
86
|
+
}
|
|
87
|
+
/** Hold each envelope to its own fields: the legacy one keeps every field its readers require, 0.1.0.2 carries none. */
|
|
88
|
+
// oxlint-disable-next-line typescript/prefer-readonly-parameter-types -- valibot output, only read
|
|
89
|
+
function checkIndexEnvelope(index: OutputIndex): void {
|
|
90
|
+
const carried = legacyIndexFields(index);
|
|
91
|
+
if (index.formatVersion !== LEGACY_LAYOUT) {
|
|
92
|
+
if (carried.length > 0) {
|
|
93
|
+
throw new Error(`Format ${index.formatVersion} drops ${carried.join(', ')} (shard.json)`);
|
|
94
|
+
}
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const needed = ['version', 'schemas', 'requires.capabilities', ...index.runtime === null ? [] : ['runtime.files', 'runtime.lines']];
|
|
98
|
+
const missing = needed.filter((field: string): boolean => !carried.includes(field));
|
|
99
|
+
if (missing.length > 0) {
|
|
100
|
+
throw new Error(`Legacy shard.json (formatVersion 2) needs ${missing.join(', ')}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
66
103
|
function parseOutputIndexRows(input: unknown): OutputIndex {
|
|
67
104
|
const index = v.parse(OutputIndexSchema, input);
|
|
105
|
+
checkIndexEnvelope(index);
|
|
68
106
|
if (index.sdk === '0.3' && index.cook === undefined) { throw new Error(`Missing cook preparation: run wildshard prepare <folder> --base <exact-preview-url> (${index.identity.slug})`); }
|
|
69
107
|
if (index.sdk === '0.3' && index.cook !== undefined && index.cook.bytes > MAX_COOK_BYTES) { throw new Error('Cook preparation exceeds byte cap'); }
|
|
70
108
|
const paths = new Set(['shard.json', 'validation.json']), names = new Set<string>();
|
|
@@ -76,7 +114,7 @@ function parseOutputIndexRows(input: unknown): OutputIndex {
|
|
|
76
114
|
}
|
|
77
115
|
}
|
|
78
116
|
if (index.sections.reduce((total, entry) => total + entry.bytes, 0) > limits.sourceBytes) throw new Error('Output sections exceed the source byte cap');
|
|
79
|
-
for (const entry of [...index.sections, ...index.schemas, ...index.files, ...index.runtime?.files ?? []]) {
|
|
117
|
+
for (const entry of [...index.sections, ...index.schemas ?? [], ...index.files, ...index.runtime?.files ?? []]) {
|
|
80
118
|
if (entry.path.split('/').some(part => part === '.' || part === '..') || paths.has(entry.path)) throw new Error('Duplicate or escaping output path');
|
|
81
119
|
paths.add(entry.path);
|
|
82
120
|
}
|
|
@@ -108,7 +146,7 @@ function parseOutputIndexRows(input: unknown): OutputIndex {
|
|
|
108
146
|
}
|
|
109
147
|
if (!serverFilesExclusive(index.admission.server ?? [], index.files)) throw new Error('A server file is never also a client file');
|
|
110
148
|
// I5: a pack lives beside the blobs, is listed once, and holds only listed members at their exact sizes, in order
|
|
111
|
-
const members = new Map([...index.sections, ...index.schemas].map(entry => [entry.hash, entry.bytes] as const));
|
|
149
|
+
const members = new Map([...index.sections, ...index.schemas ?? []].map(entry => [entry.hash, entry.bytes] as const));
|
|
112
150
|
for (const entry of index.files) members.set(entry.hash, entry.compressed);
|
|
113
151
|
for (const pack of index.packs ?? []) {
|
|
114
152
|
if (pack.path !== `blobs/pack/${pack.hash}.pack` || paths.has(pack.path)) throw new Error('Duplicate or mismatched pack path');
|
|
@@ -160,9 +198,9 @@ export function assembleOutputShardfile(input: unknown, sections: ReadonlyMap<st
|
|
|
160
198
|
/** Exact admitted data, independent of validator implementation metadata. */
|
|
161
199
|
interface OutputIndexRowsInputFields {
|
|
162
200
|
format: "wildshard.shardfile";
|
|
163
|
-
formatVersion:
|
|
201
|
+
formatVersion: typeof LEGACY_LAYOUT | IndexFormatVersion;
|
|
164
202
|
sdk: "0.2" | "0.3";
|
|
165
|
-
version
|
|
203
|
+
version?: LegacyIndexVersion;
|
|
166
204
|
identity: ShardIdentityInput;
|
|
167
205
|
build: {
|
|
168
206
|
sourceHash: string;
|
|
@@ -187,7 +225,7 @@ interface OutputIndexRowsInputFields {
|
|
|
187
225
|
hash: string;
|
|
188
226
|
bytes: number;
|
|
189
227
|
}[];
|
|
190
|
-
schemas
|
|
228
|
+
schemas?: {
|
|
191
229
|
path: string;
|
|
192
230
|
hash: string;
|
|
193
231
|
bytes: number;
|
|
@@ -226,13 +264,13 @@ interface OutputIndexRowsInputFields {
|
|
|
226
264
|
} | undefined;
|
|
227
265
|
runtime: {
|
|
228
266
|
trust?: unknown;
|
|
229
|
-
files
|
|
267
|
+
files?: {
|
|
230
268
|
lines: number;
|
|
231
269
|
path: string;
|
|
232
270
|
hash: string;
|
|
233
271
|
bytes: number;
|
|
234
272
|
}[];
|
|
235
|
-
lines
|
|
273
|
+
lines?: 0;
|
|
236
274
|
kind: "client-plan";
|
|
237
275
|
cost: {
|
|
238
276
|
imagesFirst?: {
|
|
@@ -258,9 +296,9 @@ interface OutputIndexRowsInputFields {
|
|
|
258
296
|
export type OutputIndexRowsInput = { [Key in keyof OutputIndexRowsInputFields]: OutputIndexRowsInputFields[Key] };
|
|
259
297
|
interface OutputIndexRowsFields {
|
|
260
298
|
format: "wildshard.shardfile";
|
|
261
|
-
formatVersion:
|
|
299
|
+
formatVersion: typeof LEGACY_LAYOUT | IndexFormatVersion;
|
|
262
300
|
sdk: "0.2" | "0.3";
|
|
263
|
-
version
|
|
301
|
+
version?: LegacyIndexVersion;
|
|
264
302
|
identity: ShardIdentity;
|
|
265
303
|
build: {
|
|
266
304
|
sourceHash: string;
|
|
@@ -285,7 +323,7 @@ interface OutputIndexRowsFields {
|
|
|
285
323
|
hash: string;
|
|
286
324
|
bytes: number;
|
|
287
325
|
}[];
|
|
288
|
-
schemas
|
|
326
|
+
schemas?: {
|
|
289
327
|
path: string;
|
|
290
328
|
hash: string;
|
|
291
329
|
bytes: number;
|
|
@@ -324,13 +362,13 @@ interface OutputIndexRowsFields {
|
|
|
324
362
|
} | undefined;
|
|
325
363
|
runtime: {
|
|
326
364
|
trust?: unknown;
|
|
327
|
-
files
|
|
365
|
+
files?: {
|
|
328
366
|
lines: number;
|
|
329
367
|
path: string;
|
|
330
368
|
hash: string;
|
|
331
369
|
bytes: number;
|
|
332
370
|
}[];
|
|
333
|
-
lines
|
|
371
|
+
lines?: 0;
|
|
334
372
|
kind: "client-plan";
|
|
335
373
|
cost: {
|
|
336
374
|
imagesFirst?: {
|
|
@@ -40,7 +40,7 @@ const tileCount = (2 * CHUNK_HALF / WIRE_TILE_SIZES.l0) ** 2 + (2 * CHUNK_HALF /
|
|
|
40
40
|
/** Stable authored shard identity and deterministic seed. */
|
|
41
41
|
export const IdentitySchema: v.GenericSchema<ShardIdentityInput, ShardIdentity> = v.strictObject({ slug: name, name: v.pipe(v.string(), v.minLength(1), v.maxLength(limits.idCharacters)), author: v.pipe(v.string(), v.minLength(1), v.maxLength(limits.idCharacters)), revision: positive, seed: ShardNaturalSchema });
|
|
42
42
|
/** Wire revision, capabilities and exact commons declarations. */
|
|
43
|
-
export const RequiresSchema: v.GenericSchema<ShardRequiresInput, ShardRequires> = v.strictObject({ sdk: v.picklist(SHARDFILE_VERSIONS), capabilities: ShardNamesSchema, commons: v.pipe(v.array(ShardHashSchema), v.maxLength(limits.commons)), commonsWire: v.optional(v.record(ShardHashSchema, ShardNaturalSchema), {}), commonsCosts: v.optional(CommonsCostsSchema, {}) });
|
|
43
|
+
export const RequiresSchema: v.GenericSchema<ShardRequiresInput, ShardRequires> = v.strictObject({ sdk: v.picklist(SHARDFILE_VERSIONS), capabilities: v.exactOptional(ShardNamesSchema), commons: v.pipe(v.array(ShardHashSchema), v.maxLength(limits.commons)), commonsWire: v.optional(v.record(ShardHashSchema, ShardNaturalSchema), {}), commonsCosts: v.optional(CommonsCostsSchema, {}) });
|
|
44
44
|
/** Authored compressed and resident ceilings. */
|
|
45
45
|
export const BudgetsSchema: v.GenericSchema<ShardBudgetsInput, ShardBudgets> = v.strictObject({
|
|
46
46
|
/** Reviewed whole-client floor retained when an opaque runtime becomes declared data. */
|
|
@@ -224,7 +224,8 @@ interface ShardIdentityFields {
|
|
|
224
224
|
export type ShardIdentity = { [Key in keyof ShardIdentityFields]: ShardIdentityFields[Key] };
|
|
225
225
|
interface ShardRequiresInputFields {
|
|
226
226
|
sdk: ShardfileVersion;
|
|
227
|
-
|
|
227
|
+
/** Always empty, never read; format 0.1.0.2 drops it (FORMAT-PRUNE FP4). */
|
|
228
|
+
capabilities?: ShardNamesInput;
|
|
228
229
|
commons: (ShardHashInput)[];
|
|
229
230
|
commonsWire?: Record<string, ShardNaturalInput> | undefined;
|
|
230
231
|
commonsCosts?: v.InferInput<typeof CommonsCostsSchema> | undefined;
|
|
@@ -232,7 +233,7 @@ interface ShardRequiresInputFields {
|
|
|
232
233
|
export type ShardRequiresInput = { [Key in keyof ShardRequiresInputFields]: ShardRequiresInputFields[Key] };
|
|
233
234
|
interface ShardRequiresFields {
|
|
234
235
|
sdk: ShardfileVersion;
|
|
235
|
-
capabilities
|
|
236
|
+
capabilities?: ShardNames;
|
|
236
237
|
commons: (ShardHash)[];
|
|
237
238
|
commonsWire: Record<string, ShardNatural>;
|
|
238
239
|
commonsCosts: v.InferOutput<typeof CommonsCostsSchema>;
|
|
@@ -15,7 +15,8 @@ interface Listed {
|
|
|
15
15
|
}
|
|
16
16
|
interface Members {
|
|
17
17
|
readonly sections: readonly Listed[]
|
|
18
|
-
|
|
18
|
+
/** Absent from format 0.1.0.2 on (FORMAT-PRUNE FP4). */
|
|
19
|
+
readonly schemas?: readonly Listed[] | undefined
|
|
19
20
|
readonly files: readonly Listed[]
|
|
20
21
|
}
|
|
21
22
|
type Assets = ReadonlyMap<string, Uint8Array>
|
|
@@ -123,7 +124,7 @@ function packFromMembers(
|
|
|
123
124
|
index: Members,
|
|
124
125
|
read: ReadFile,
|
|
125
126
|
): Uint8Array {
|
|
126
|
-
const rows = [...index.sections, ...index.schemas, ...index.files]
|
|
127
|
+
const rows = [...index.sections, ...(index.schemas ?? []), ...index.files]
|
|
127
128
|
const paths = new Map(
|
|
128
129
|
rows.map((row: Listed): [string, string] => [row.hash, row.path]),
|
|
129
130
|
)
|
|
@@ -38,7 +38,7 @@ function memberPaths(
|
|
|
38
38
|
index: OutputIndex,
|
|
39
39
|
): ReadonlyMap<string, string> {
|
|
40
40
|
return new Map(
|
|
41
|
-
[...index.sections, ...index.schemas, ...index.files].map(
|
|
41
|
+
[...index.sections, ...(index.schemas ?? []), ...index.files].map(
|
|
42
42
|
(row: Readonly<{ hash: string; path: string }>): [string, string] => [
|
|
43
43
|
row.hash,
|
|
44
44
|
row.path,
|
|
@@ -3,7 +3,7 @@ import { assertMigrationCompatibility } from './migrations.ts';
|
|
|
3
3
|
import { StateSchema } from './state.ts';
|
|
4
4
|
import type { Shardfile } from './shardSchema.ts';
|
|
5
5
|
import { isJsonDataWithFileSchemas } from './json.ts';
|
|
6
|
-
import {
|
|
6
|
+
import { IndexFormatVersionSchema, LegacyIndexVersionSchema } from './index-format-version.ts';
|
|
7
7
|
import { SHARDFILE_ADMISSION_LIMITS as limits } from './admissionLimits.ts';
|
|
8
8
|
|
|
9
9
|
const identity = v.object({ slug: v.pipe(v.string(), v.regex(/^[a-z][a-z0-9.-]*$/u)),
|
|
@@ -16,11 +16,13 @@ export function parseStateLineage(input: unknown): StateLineage { return v.parse
|
|
|
16
16
|
const path = v.pipe(v.string(), v.maxLength(240), v.regex(/^(?:[a-zA-Z0-9_-][a-zA-Z0-9_.-]*\/)*[a-zA-Z0-9_-][a-zA-Z0-9_.-]*$/u));
|
|
17
17
|
const measured = { path, hash: v.pipe(v.string(), v.regex(/^[a-f0-9]{64}$/u)),
|
|
18
18
|
bytes: v.pipe(v.number(), v.integer(), v.minValue(0), v.maxValue(Number.MAX_SAFE_INTEGER)) };
|
|
19
|
-
const
|
|
20
|
-
|
|
19
|
+
const lineageSchemas = v.exactOptional(v.pipe(v.array(v.strictObject(measured)), v.maxLength(128)));
|
|
20
|
+
// SHARD-COMPAT CP14: either envelope; 0.1.0.2 names its format in formatVersion and drops version and schemas.
|
|
21
|
+
const inventory = v.object({ format: v.literal('wildshard.shardfile'), formatVersion: IndexFormatVersionSchema, sdk: v.picklist(['0.2', '0.3']),
|
|
22
|
+
version: LegacyIndexVersionSchema, identity,
|
|
21
23
|
sections: v.pipe(v.array(v.strictObject({ ...measured, name: v.pipe(v.string(), v.regex(/^[a-z][a-zA-Z0-9-]*\/[a-zA-Z][a-zA-Z0-9_-]*$/u)),
|
|
22
24
|
schema: v.pipe(v.string(), v.maxLength(240)) })), v.maxLength(256)),
|
|
23
|
-
schemas:
|
|
25
|
+
schemas: lineageSchemas });
|
|
24
26
|
export interface OutputLineageIndex {
|
|
25
27
|
readonly identity: { readonly slug: string; readonly revision: number };
|
|
26
28
|
readonly sections: readonly { readonly path: string; readonly hash: string; readonly bytes: number; readonly name: string; readonly schema: string }[];
|
|
@@ -30,7 +32,8 @@ export interface OutputLineageIndex {
|
|
|
30
32
|
* Obsolete host/asset declarations are never parsed or admitted by this reader. */
|
|
31
33
|
export function parseOutputLineageIndex(input: unknown): OutputLineageIndex {
|
|
32
34
|
if (!isJsonDataWithFileSchemas(input)) throw new Error('Shardfile descriptor requires bounded plain JSON');
|
|
33
|
-
const
|
|
35
|
+
const parsed = v.parse(inventory, input), paths = new Set(['shard.json', 'validation.json']), names = new Set<string>();
|
|
36
|
+
const index = { ...parsed, schemas: parsed.schemas ?? [] };
|
|
34
37
|
if (index.sections.reduce((total, row) => total + row.bytes, 0) > limits.sourceBytes) throw new Error('Output sections exceed the source byte cap');
|
|
35
38
|
for (const row of [...index.sections, ...index.schemas]) {
|
|
36
39
|
if (row.path.split('/').some(part => part === '.' || part === '..') || paths.has(row.path)) throw new Error('Duplicate or escaping output path');
|
package/src/format/version.ts
CHANGED
|
@@ -3,14 +3,30 @@
|
|
|
3
3
|
const ZERO = 0
|
|
4
4
|
const ONE = 1
|
|
5
5
|
|
|
6
|
-
const SHARDFILE_VERSION = '0.1.0.
|
|
7
|
-
/**
|
|
8
|
-
|
|
6
|
+
const SHARDFILE_VERSION = '0.1.0.2'
|
|
7
|
+
/** Formats whose `shard.json` names its version in `version`, beside the frozen layout marker `formatVersion: 2`.
|
|
8
|
+
* Legacy 0.1 is the byte-preserving spelling of 0.1.0.0. */
|
|
9
|
+
const LEGACY_INDEX_VERSIONS: readonly ['0.1', '0.1.0.0', '0.1.0.1'] = [
|
|
9
10
|
'0.1',
|
|
10
11
|
'0.1.0.0',
|
|
11
|
-
|
|
12
|
-
]
|
|
12
|
+
'0.1.0.1',
|
|
13
|
+
] as const
|
|
14
|
+
/** Formats whose `shard.json` names its version in `formatVersion` (SHARD-COMPAT CP14, FORMAT-PRUNE FP25): the
|
|
15
|
+
* layout marker and `version` are gone, so a reader tells the two envelopes apart by the field's type. */
|
|
16
|
+
const INDEX_FORMAT_VERSIONS: readonly [typeof SHARDFILE_VERSION] = [
|
|
17
|
+
SHARDFILE_VERSION,
|
|
18
|
+
] as const
|
|
19
|
+
const SHARDFILE_VERSIONS: readonly [
|
|
20
|
+
...typeof LEGACY_INDEX_VERSIONS,
|
|
21
|
+
...typeof INDEX_FORMAT_VERSIONS,
|
|
22
|
+
] = [...LEGACY_INDEX_VERSIONS, ...INDEX_FORMAT_VERSIONS] as const
|
|
13
23
|
type ShardfileVersion = (typeof SHARDFILE_VERSIONS)[number]
|
|
24
|
+
type LegacyIndexVersion = (typeof LEGACY_INDEX_VERSIONS)[number]
|
|
25
|
+
type IndexFormatVersion = (typeof INDEX_FORMAT_VERSIONS)[number]
|
|
26
|
+
/** The frozen layout marker the legacy envelope carries in `formatVersion`. */
|
|
27
|
+
const LEGACY_LAYOUT = 2
|
|
28
|
+
/** The first format that writes its version in `formatVersion`. */
|
|
29
|
+
const SHARDFILE_VERSION_INDEX_RENAME = '0.1.0.2'
|
|
14
30
|
type ShardfileVersionParts = readonly [number, number, number, number]
|
|
15
31
|
|
|
16
32
|
/** Read canonical bounded decimal components; the legacy two-part spelling has zero counters. */
|
|
@@ -67,9 +83,77 @@ function compatibleShardfileVersion(
|
|
|
67
83
|
}
|
|
68
84
|
}
|
|
69
85
|
|
|
86
|
+
/** The format version a `shard.json` declares: `formatVersion` when it is a version string (0.1.0.2 on), else the
|
|
87
|
+
* legacy `version` beside the layout marker `formatVersion: 2`. Throws when neither names a format. */
|
|
88
|
+
function shardfileFormatOf(index: unknown): ShardfileVersion {
|
|
89
|
+
const declared: unknown =
|
|
90
|
+
typeof index === 'object' && index !== null
|
|
91
|
+
? Reflect.get(index, 'formatVersion')
|
|
92
|
+
: undefined
|
|
93
|
+
const legacy: unknown =
|
|
94
|
+
typeof index === 'object' && index !== null
|
|
95
|
+
? Reflect.get(index, 'version')
|
|
96
|
+
: undefined
|
|
97
|
+
const found: unknown = declared === LEGACY_LAYOUT ? legacy : declared
|
|
98
|
+
const known = SHARDFILE_VERSIONS.find(
|
|
99
|
+
(version: string): boolean => version === found,
|
|
100
|
+
)
|
|
101
|
+
if (known === undefined) {
|
|
102
|
+
throw new Error('Shardfile index names no known format version')
|
|
103
|
+
}
|
|
104
|
+
return known
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Whether a format version writes the 0.1.0.2 envelope (`formatVersion` as the version, no `version`). */
|
|
108
|
+
function writesIndexFormatVersion(version: string): boolean {
|
|
109
|
+
return (
|
|
110
|
+
compareShardfileVersions(version, SHARDFILE_VERSION_INDEX_RENAME) >= ZERO
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** A copy of a JSON object without `keys`; anything else as it is. */
|
|
115
|
+
function withoutKeys(value: unknown, keys: readonly string[]): unknown {
|
|
116
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
117
|
+
return value
|
|
118
|
+
}
|
|
119
|
+
return Object.fromEntries(
|
|
120
|
+
Object.entries(value).filter(
|
|
121
|
+
([key]: readonly [string, unknown]): boolean => !keys.includes(key),
|
|
122
|
+
),
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** SHARD-COMPAT CP14: writers build the legacy `shard.json` rows (`formatVersion: 2`, `version`, the always-empty lists)
|
|
127
|
+
* and pass them here. At a `version` of 0.1.0.2 or later this returns the envelope that version writes: `formatVersion`
|
|
128
|
+
* is the version, and `version`, `schemas`, `runtime.files`, `runtime.lines`, `runtime.trust` and
|
|
129
|
+
* `requires.capabilities` go. An older `version` comes back unchanged. */
|
|
130
|
+
function outputIndexEnvelope(
|
|
131
|
+
rows: Readonly<Record<string, unknown>>,
|
|
132
|
+
): Record<string, unknown> {
|
|
133
|
+
const { version } = rows
|
|
134
|
+
if (typeof version !== 'string' || !writesIndexFormatVersion(version)) {
|
|
135
|
+
return { ...rows }
|
|
136
|
+
}
|
|
137
|
+
const { version: _version, schemas: _schemas, ...rest } = rows
|
|
138
|
+
return {
|
|
139
|
+
...rest,
|
|
140
|
+
formatVersion: version,
|
|
141
|
+
requires: withoutKeys(rest['requires'], ['capabilities']),
|
|
142
|
+
runtime: withoutKeys(rest['runtime'], ['files', 'lines', 'trust']),
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
70
146
|
export {
|
|
147
|
+
outputIndexEnvelope,
|
|
71
148
|
SHARDFILE_VERSION,
|
|
72
149
|
SHARDFILE_VERSIONS,
|
|
150
|
+
LEGACY_INDEX_VERSIONS,
|
|
151
|
+
INDEX_FORMAT_VERSIONS,
|
|
152
|
+
LEGACY_LAYOUT,
|
|
153
|
+
type LegacyIndexVersion,
|
|
154
|
+
type IndexFormatVersion,
|
|
155
|
+
shardfileFormatOf,
|
|
156
|
+
writesIndexFormatVersion,
|
|
73
157
|
SHARDFILE_FLOOR,
|
|
74
158
|
type ShardfileVersion,
|
|
75
159
|
type ShardfileVersionParts,
|