rig-parts 2.68.0 → 2.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/README.md +1 -0
- package/cli.ts +58 -4
- package/docs/AUTHORING.md +97 -3
- package/docs/STABILITY.md +81 -17
- package/package.json +2 -1
- package/src/build.ts +2 -0
- package/src/check.ts +96 -11
- package/src/editedparts.ts +57 -21
- package/src/parts.ts +27 -21
- package/src/partscheck.ts +13 -23
- package/src/partsfiles.ts +62 -0
- package/src/replacementrecord.ts +172 -0
- package/src/replaceparts.ts +564 -0
- package/src/rig.ts +1 -1
- package/src/runstamp.ts +24 -3
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/** Browser-free replacement record schema and deterministic parts-tree digest. */
|
|
2
|
+
import { join, resolve } from 'node:path';
|
|
3
|
+
import { SHA256_PATTERN } from './editreceipt.ts';
|
|
4
|
+
import { editedPartReceiptProblem, type EditedPartReceipt, type EditedPixelCounts } from './editedparts.ts';
|
|
5
|
+
import { PartsError, refuseIfAny, type Problem } from './errors.ts';
|
|
6
|
+
import { inventoryPath, partsInventoryDigest, partsSha256, readRegularFile, walkPartsFiles, type PartsInventoryRow } from './partsfiles.ts';
|
|
7
|
+
import { receiptObject } from './receiptproducer.ts';
|
|
8
|
+
import type { DepthReading } from './depthsheets.ts';
|
|
9
|
+
|
|
10
|
+
export const REPLACEMENT_RECORD_SPEC = 'rig-parts-replacement/1';
|
|
11
|
+
export const REPLACEMENT_RECORD_PATH = /^\.parts-replacements\/[0-9a-f]{64}\.json$/;
|
|
12
|
+
|
|
13
|
+
export interface ReplacementRow extends EditedPixelCounts {
|
|
14
|
+
part: string;
|
|
15
|
+
receipt: EditedPartReceipt;
|
|
16
|
+
depth?: {
|
|
17
|
+
from_sha256: string;
|
|
18
|
+
sha256: string;
|
|
19
|
+
reseeded: boolean;
|
|
20
|
+
support?: 'alpha-nonzero' | 'alpha-at-least-128';
|
|
21
|
+
reading?: DepthReading;
|
|
22
|
+
};
|
|
23
|
+
/** Original manifest fields removed from live metadata during replacement. */
|
|
24
|
+
historical?: Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ReplacementRecord {
|
|
28
|
+
spec: typeof REPLACEMENT_RECORD_SPEC;
|
|
29
|
+
source_sha256: string;
|
|
30
|
+
result_sha256: string;
|
|
31
|
+
rig_size: [number, number];
|
|
32
|
+
replaced: ReplacementRow[];
|
|
33
|
+
copied: string[];
|
|
34
|
+
rederived: string[];
|
|
35
|
+
omitted: string[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const replacementRecordPath = (source: string): string => `.parts-replacements/${source}.json`;
|
|
39
|
+
export const replacementHash = (value: unknown): value is string => typeof value === 'string' && SHA256_PATTERN.test(value);
|
|
40
|
+
const count = (value: unknown): value is number => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
|
|
41
|
+
const grid = (value: unknown): value is [number, number] => Array.isArray(value) && value.length === 2 && value.every(n => count(n) && n > 0);
|
|
42
|
+
const changedKeys = ['changed_px', 'changed_counted_px', 'changed_excluded_px', 'removed_opaque_px'] as const;
|
|
43
|
+
export const REPLACEMENT_HISTORICAL_FIELDS = [
|
|
44
|
+
'visible_px', 'occluded_px', 'visible_not_projected_px', 'painting_extension_px', 'provenance', 'taken_layers',
|
|
45
|
+
'fringe_pushed_back', 'source_visibility', 'source_state_map', 'source_overlaps',
|
|
46
|
+
] as const;
|
|
47
|
+
|
|
48
|
+
export function readReplacementRecord(bytes: Uint8Array): ReplacementRecord {
|
|
49
|
+
const problems: Problem[] = [];
|
|
50
|
+
const fail = (object: string, detail: string): void => { problems.push({ code: 'REPLACE_PART_RECORD', object, detail }); };
|
|
51
|
+
let raw: unknown;
|
|
52
|
+
try { raw = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes)); }
|
|
53
|
+
catch { fail('replacement record', 'requires a readable UTF-8 JSON object'); }
|
|
54
|
+
if (!receiptObject(raw)) {
|
|
55
|
+
if (problems.length === 0) fail('replacement record', 'requires a JSON object');
|
|
56
|
+
throw new PartsError(problems);
|
|
57
|
+
}
|
|
58
|
+
const fields = ['spec', 'source_sha256', 'result_sha256', 'rig_size', 'replaced', 'copied', 'rederived', 'omitted'];
|
|
59
|
+
for (const key of Object.keys(raw)) if (!fields.includes(key)) fail(key, 'unknown replacement record field');
|
|
60
|
+
if (raw.spec !== REPLACEMENT_RECORD_SPEC) fail('spec', `requires ${REPLACEMENT_RECORD_SPEC}`);
|
|
61
|
+
for (const key of ['source_sha256', 'result_sha256']) if (!replacementHash(raw[key])) fail(key, 'requires a lowercase SHA-256');
|
|
62
|
+
if (!grid(raw.rig_size)) fail('rig_size', 'requires two positive safe integers');
|
|
63
|
+
const paths = new Set<string>();
|
|
64
|
+
for (const key of ['copied', 'rederived', 'omitted']) {
|
|
65
|
+
const list = raw[key];
|
|
66
|
+
if (!Array.isArray(list)) { fail(key, 'requires a sorted list of relative file paths'); continue; }
|
|
67
|
+
let previous: string | undefined;
|
|
68
|
+
for (const value of list) {
|
|
69
|
+
if (typeof value !== 'string' || !inventoryPath(value)) { fail(key, 'requires safe relative file paths'); continue; }
|
|
70
|
+
if (previous !== undefined && value <= previous) fail(key, `path ${JSON.stringify(value)} must be in code-unit order without repeats`);
|
|
71
|
+
if (paths.has(value)) fail(key, `path ${JSON.stringify(value)} was already classified`);
|
|
72
|
+
previous = value;
|
|
73
|
+
paths.add(value);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const seen = new Set<string>();
|
|
77
|
+
if (!Array.isArray(raw.replaced) || raw.replaced.length === 0) fail('replaced', 'requires at least one replacement row');
|
|
78
|
+
else for (const [index, row] of raw.replaced.entries()) {
|
|
79
|
+
const at = `replaced[${index}]`;
|
|
80
|
+
if (!receiptObject(row)) { fail(at, 'requires a replacement row object'); continue; }
|
|
81
|
+
const allowed = ['part', 'receipt', ...changedKeys, 'removed_opaque_regions', 'depth', 'historical'];
|
|
82
|
+
for (const key of Object.keys(row)) if (!allowed.includes(key)) fail(`${at}.${key}`, 'unknown replacement row field');
|
|
83
|
+
if (typeof row.part !== 'string' || row.part.trim() === '' || /[\\/\0]/.test(row.part) || row.part.startsWith('.') || seen.has(row.part)) fail(`${at}.part`, 'requires a unique file-name-safe part name');
|
|
84
|
+
if (typeof row.part === 'string') seen.add(row.part);
|
|
85
|
+
const invalidReceipt = editedPartReceiptProblem(row.receipt);
|
|
86
|
+
if (invalidReceipt !== undefined || !receiptObject(row.receipt) || row.receipt.part !== row.part || row.receipt.sha256 === row.receipt.from_sha256) {
|
|
87
|
+
fail(`${at}.receipt`, invalidReceipt ?? 'requires the enclosing part and distinct replacement and source hashes');
|
|
88
|
+
}
|
|
89
|
+
if (!changedKeys.every(key => count(row[key])) ||
|
|
90
|
+
Number(row.changed_px) !== Number(row.changed_counted_px) + Number(row.changed_excluded_px) ||
|
|
91
|
+
Number(row.removed_opaque_px) > Number(row.changed_excluded_px)) fail(at, 'change counts must be nonnegative integers and add up');
|
|
92
|
+
if (Object.hasOwn(row, 'removed_opaque_regions')) {
|
|
93
|
+
const regions = row.removed_opaque_regions;
|
|
94
|
+
if (!Array.isArray(regions) || regions.length === 0 || !regions.every(region => receiptObject(region) &&
|
|
95
|
+
Object.keys(region).sort().join(',') === 'box,px' && Array.isArray(region.box) && region.box.length === 4 &&
|
|
96
|
+
region.box.every(count) && region.box[2] > 0 && region.box[3] > 0 && count(region.px) &&
|
|
97
|
+
region.px > 0 && region.px <= region.box[2] * region.box[3]) ||
|
|
98
|
+
regions.reduce((sum, region) => sum + Number(region.px), 0) !== row.removed_opaque_px) {
|
|
99
|
+
fail(`${at}.removed_opaque_regions`, 'requires bounded nonempty region rows whose counts sum to removed_opaque_px');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (Object.hasOwn(row, 'depth')) {
|
|
103
|
+
const depth = row.depth;
|
|
104
|
+
if (!receiptObject(depth) || Object.keys(depth).some(key => !['from_sha256', 'sha256', 'reseeded', 'support', 'reading'].includes(key)) ||
|
|
105
|
+
!replacementHash(depth.from_sha256) || !replacementHash(depth.sha256) || typeof depth.reseeded !== 'boolean' ||
|
|
106
|
+
(Object.hasOwn(depth, 'support') && depth.support !== 'alpha-nonzero' && depth.support !== 'alpha-at-least-128') ||
|
|
107
|
+
(Object.hasOwn(depth, 'reading') && !validDepthReading(depth.reading))) {
|
|
108
|
+
fail(`${at}.depth`, 'requires both lowercase depth hashes, a boolean reseeded, and valid support or reading if present');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (Object.hasOwn(row, 'historical') && (!receiptObject(row.historical) || Object.keys(row.historical).length === 0 ||
|
|
112
|
+
Object.keys(row.historical).some(key => !(REPLACEMENT_HISTORICAL_FIELDS as readonly string[]).includes(key)))) {
|
|
113
|
+
fail(`${at}.historical`, 'requires original values of recognized assembly fields');
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
refuseIfAny(problems);
|
|
117
|
+
return raw as unknown as ReplacementRecord;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function validDepthReading(raw: unknown): boolean {
|
|
121
|
+
const level = (value: unknown): boolean => value === null || (typeof value === 'number' && Number.isFinite(value));
|
|
122
|
+
return receiptObject(raw) && Object.keys(raw).sort().join(',') === 'orderingMedian,relation,sheetMedian' &&
|
|
123
|
+
level(raw.orderingMedian) && level(raw.sheetMedian) &&
|
|
124
|
+
(raw.relation === 'same' || raw.relation === 'opposite' || raw.relation === 'indeterminate');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function captureReplacementTree(root: string): Map<string, PartsInventoryRow> {
|
|
128
|
+
const files = new Map<string, PartsInventoryRow>(), problems: Problem[] = [];
|
|
129
|
+
const invalid = (path: string, detail: string): void => {
|
|
130
|
+
problems.push({ code: 'REPLACE_PART_INPUT', object: path, detail });
|
|
131
|
+
};
|
|
132
|
+
walkPartsFiles(root, new Set(), (name, path) => {
|
|
133
|
+
if (!inventoryPath(name)) { invalid(name, 'requires a safe relative file path'); return; }
|
|
134
|
+
const bytes = readRegularFile(path);
|
|
135
|
+
files.set(name, { path: name, bytes: bytes.length, sha256: partsSha256(bytes) });
|
|
136
|
+
}, (name, error) => invalid(name, error === undefined
|
|
137
|
+
? 'found a link or special file; regular files and directories are required'
|
|
138
|
+
: 'could not read a regular file or directory'));
|
|
139
|
+
refuseIfAny(problems);
|
|
140
|
+
return files;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function digestReplacementInventory(files: ReadonlyMap<string, PartsInventoryRow>, recordPath?: string): string {
|
|
144
|
+
return partsInventoryDigest([...files.values()].filter(row => row.path !== recordPath));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function readBoundReplacementFile(root: string, files: ReadonlyMap<string, PartsInventoryRow>, path: string): Buffer {
|
|
148
|
+
const row = files.get(path);
|
|
149
|
+
let bytes: Buffer | undefined;
|
|
150
|
+
if (row !== undefined) {
|
|
151
|
+
try { bytes = readRegularFile(join(root, path)); }
|
|
152
|
+
catch { /* A read failure invalidates the inventory binding. */ }
|
|
153
|
+
if (bytes !== undefined && bytes.length === row.bytes && partsSha256(bytes) === row.sha256) return bytes;
|
|
154
|
+
}
|
|
155
|
+
throw new PartsError([{ code: 'REPLACE_PART_INPUT', object: path,
|
|
156
|
+
detail: row === undefined ? 'file is absent from the input inventory' : 'file changed or became unreadable after the input inventory' }]);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The source digest covers all files. The result digest omits precisely the active record. */
|
|
160
|
+
export function replacementTreeDigest(root: string, options?: { result?: boolean }): string {
|
|
161
|
+
const home = resolve(root), files = captureReplacementTree(home);
|
|
162
|
+
if (options?.result !== true) return digestReplacementInventory(files);
|
|
163
|
+
let raw: unknown;
|
|
164
|
+
try { raw = JSON.parse(readBoundReplacementFile(home, files, 'parts.json').toString('utf8')); }
|
|
165
|
+
catch { /* The result digest requires a readable manifest and an existing record. */ }
|
|
166
|
+
if (!receiptObject(raw) || typeof raw.replacement_record !== 'string' || !REPLACEMENT_RECORD_PATH.test(raw.replacement_record) ||
|
|
167
|
+
!files.has(raw.replacement_record)) {
|
|
168
|
+
throw new PartsError([{ code: 'REPLACE_PART_RECORD', object: 'parts.json replacement_record',
|
|
169
|
+
detail: 'a result digest requires a valid pointer to an existing replacement record inside the set' }]);
|
|
170
|
+
}
|
|
171
|
+
return digestReplacementInventory(files, raw.replacement_record);
|
|
172
|
+
}
|