@seanhogg/builderforce-memory 2026.10.62 → 2026.10.66
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/dist/knowledge/pack/frame.d.ts +12 -3
- package/dist/knowledge/pack/frame.d.ts.map +1 -1
- package/dist/knowledge/pack/frame.js +18 -4
- package/dist/knowledge/pack/frame.js.map +1 -1
- package/dist/knowledge/pack/manifest.d.ts +11 -3
- package/dist/knowledge/pack/manifest.d.ts.map +1 -1
- package/dist/knowledge/pack/manifest.js +26 -12
- package/dist/knowledge/pack/manifest.js.map +1 -1
- package/dist/knowledge/pack/sections/extr.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/extr.js +16 -4
- package/dist/knowledge/pack/sections/extr.js.map +1 -1
- package/dist/knowledge/pack/sections/extrLists.d.ts +0 -1
- package/dist/knowledge/pack/sections/extrLists.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/extrLists.js +5 -6
- package/dist/knowledge/pack/sections/extrLists.js.map +1 -1
- package/dist/knowledge/pack/sections/facts.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/facts.js +3 -2
- package/dist/knowledge/pack/sections/facts.js.map +1 -1
- package/dist/knowledge/pack/sections/rows.d.ts.map +1 -1
- package/dist/knowledge/pack/sections/rows.js +11 -2
- package/dist/knowledge/pack/sections/rows.js.map +1 -1
- package/dist/knowledge/pack/strings.d.ts.map +1 -1
- package/dist/knowledge/pack/strings.js +6 -1
- package/dist/knowledge/pack/strings.js.map +1 -1
- package/dist/knowledge/pack/varint.d.ts +14 -0
- package/dist/knowledge/pack/varint.d.ts.map +1 -1
- package/dist/knowledge/pack/varint.js +32 -1
- package/dist/knowledge/pack/varint.js.map +1 -1
- package/dist/knowledge/review.d.ts +6 -6
- package/dist/knowledge/review.d.ts.map +1 -1
- package/dist/knowledge/review.js +56 -329
- package/dist/knowledge/review.js.map +1 -1
- package/dist/knowledge/reviewAnalyse.d.ts +17 -0
- package/dist/knowledge/reviewAnalyse.d.ts.map +1 -0
- package/dist/knowledge/reviewAnalyse.js +61 -0
- package/dist/knowledge/reviewAnalyse.js.map +1 -0
- package/dist/knowledge/reviewApply.d.ts +33 -0
- package/dist/knowledge/reviewApply.d.ts.map +1 -0
- package/dist/knowledge/reviewApply.js +121 -0
- package/dist/knowledge/reviewApply.js.map +1 -0
- package/dist/knowledge/reviewContext.d.ts +77 -0
- package/dist/knowledge/reviewContext.d.ts.map +1 -0
- package/dist/knowledge/reviewContext.js +111 -0
- package/dist/knowledge/reviewContext.js.map +1 -0
- package/dist/knowledge/reviewFinish.d.ts +9 -0
- package/dist/knowledge/reviewFinish.d.ts.map +1 -0
- package/dist/knowledge/reviewFinish.js +62 -0
- package/dist/knowledge/reviewFinish.js.map +1 -0
- package/dist/knowledge/reviewRunModel.d.ts +3 -0
- package/dist/knowledge/reviewRunModel.d.ts.map +1 -1
- package/dist/knowledge/reviewRunModel.js.map +1 -1
- package/dist/knowledge/stores/extractLogWriter.d.ts +16 -3
- package/dist/knowledge/stores/extractLogWriter.d.ts.map +1 -1
- package/dist/knowledge/stores/extractLogWriter.js +38 -13
- package/dist/knowledge/stores/extractLogWriter.js.map +1 -1
- package/dist/knowledge/stores/extractRefs.d.ts +68 -0
- package/dist/knowledge/stores/extractRefs.d.ts.map +1 -0
- package/dist/knowledge/stores/extractRefs.js +168 -0
- package/dist/knowledge/stores/extractRefs.js.map +1 -0
- package/dist/knowledge/stores/localSnapshotStore.d.ts +5 -0
- package/dist/knowledge/stores/localSnapshotStore.d.ts.map +1 -1
- package/dist/knowledge/stores/localSnapshotStore.js +25 -5
- package/dist/knowledge/stores/localSnapshotStore.js.map +1 -1
- package/dist/knowledge/stores/rowDelta.d.ts +6 -0
- package/dist/knowledge/stores/rowDelta.d.ts.map +1 -1
- package/dist/knowledge/stores/rowDelta.js.map +1 -1
- package/dist/knowledge/stores/rowMemory.d.ts +30 -0
- package/dist/knowledge/stores/rowMemory.d.ts.map +1 -1
- package/dist/knowledge/stores/rowMemory.js +46 -1
- package/dist/knowledge/stores/rowMemory.js.map +1 -1
- package/dist/knowledge/stores/rowSync.d.ts.map +1 -1
- package/dist/knowledge/stores/rowSync.js +2 -1
- package/dist/knowledge/stores/rowSync.js.map +1 -1
- package/dist/knowledge/stores/snapshotFormat.d.ts +13 -2
- package/dist/knowledge/stores/snapshotFormat.d.ts.map +1 -1
- package/dist/knowledge/stores/snapshotFormat.js +22 -9
- package/dist/knowledge/stores/snapshotFormat.js.map +1 -1
- package/dist/stats/distributions.d.ts +34 -0
- package/dist/stats/distributions.d.ts.map +1 -0
- package/dist/stats/distributions.js +180 -0
- package/dist/stats/distributions.js.map +1 -0
- package/dist/stats/estimators.d.ts +53 -0
- package/dist/stats/estimators.d.ts.map +1 -0
- package/dist/stats/estimators.js +79 -0
- package/dist/stats/estimators.js.map +1 -0
- package/dist/stats/fitting.d.ts +48 -0
- package/dist/stats/fitting.d.ts.map +1 -0
- package/dist/stats/fitting.js +209 -0
- package/dist/stats/fitting.js.map +1 -0
- package/dist/stats/index.d.ts +26 -0
- package/dist/stats/index.d.ts.map +1 -0
- package/dist/stats/index.js +18 -0
- package/dist/stats/index.js.map +1 -0
- package/dist/stats/information.d.ts +9 -0
- package/dist/stats/information.d.ts.map +1 -0
- package/dist/stats/information.js +21 -0
- package/dist/stats/information.js.map +1 -0
- package/dist/stats/multivariate.d.ts +25 -0
- package/dist/stats/multivariate.d.ts.map +1 -0
- package/dist/stats/multivariate.js +89 -0
- package/dist/stats/multivariate.js.map +1 -0
- package/dist/stats/policies.d.ts +19 -0
- package/dist/stats/policies.d.ts.map +1 -0
- package/dist/stats/policies.js +75 -0
- package/dist/stats/policies.js.map +1 -0
- package/dist/stats/prng.d.ts +11 -0
- package/dist/stats/prng.d.ts.map +1 -0
- package/dist/stats/prng.js +41 -0
- package/dist/stats/prng.js.map +1 -0
- package/dist/stats/scores.d.ts +33 -0
- package/dist/stats/scores.d.ts.map +1 -0
- package/dist/stats/scores.js +134 -0
- package/dist/stats/scores.js.map +1 -0
- package/dist/stats/tests.d.ts +48 -0
- package/dist/stats/tests.d.ts.map +1 -0
- package/dist/stats/tests.js +143 -0
- package/dist/stats/tests.js.map +1 -0
- package/dist/stats/timeSeries.d.ts +34 -0
- package/dist/stats/timeSeries.d.ts.map +1 -0
- package/dist/stats/timeSeries.js +71 -0
- package/dist/stats/timeSeries.js.map +1 -0
- package/dist/working/fold.d.ts +65 -0
- package/dist/working/fold.d.ts.map +1 -0
- package/dist/working/fold.js +228 -0
- package/dist/working/fold.js.map +1 -0
- package/dist/working/index.d.ts +3 -0
- package/dist/working/index.d.ts.map +1 -1
- package/dist/working/index.js +2 -0
- package/dist/working/index.js.map +1 -1
- package/dist/working/textSummary.d.ts +46 -0
- package/dist/working/textSummary.d.ts.map +1 -0
- package/dist/working/textSummary.js +142 -0
- package/dist/working/textSummary.js.map +1 -0
- package/package.json +5 -1
- package/src/contract/exports.snapshot.json +48 -1
- package/src/contract/regression.extractEviction.test.ts +162 -0
- package/src/contract/regression.fixCommits.test.ts +112 -0
- package/src/contract/regression.packWire.test.ts +174 -0
- package/src/contract/types.contract.test.ts +2 -1
- package/src/knowledge/pack/frame.ts +21 -5
- package/src/knowledge/pack/manifest.ts +26 -11
- package/src/knowledge/pack/sections/extr.ts +14 -4
- package/src/knowledge/pack/sections/extrLists.ts +4 -6
- package/src/knowledge/pack/sections/facts.ts +3 -2
- package/src/knowledge/pack/sections/rows.ts +11 -2
- package/src/knowledge/pack/strings.ts +5 -1
- package/src/knowledge/pack/varint.ts +33 -1
- package/src/knowledge/review.ts +58 -311
- package/src/knowledge/reviewAnalyse.ts +59 -0
- package/src/knowledge/reviewApply.ts +128 -0
- package/src/knowledge/reviewContext.ts +158 -0
- package/src/knowledge/reviewFinish.ts +63 -0
- package/src/knowledge/reviewRunModel.ts +3 -0
- package/src/knowledge/stores/extractLogWriter.ts +36 -12
- package/src/knowledge/stores/extractRefs.ts +161 -0
- package/src/knowledge/stores/localSnapshotStore.ts +22 -5
- package/src/knowledge/stores/rowDelta.ts +6 -0
- package/src/knowledge/stores/rowMemory.ts +50 -1
- package/src/knowledge/stores/rowSync.ts +2 -1
- package/src/knowledge/stores/snapshotFormat.ts +27 -10
- package/src/stats/distributions.ts +180 -0
- package/src/stats/estimators.ts +101 -0
- package/src/stats/fitting.ts +252 -0
- package/src/stats/index.ts +59 -0
- package/src/stats/information.ts +20 -0
- package/src/stats/multivariate.ts +93 -0
- package/src/stats/policies.ts +84 -0
- package/src/stats/prng.ts +45 -0
- package/src/stats/scores.ts +135 -0
- package/src/stats/stats.test.ts +294 -0
- package/src/stats/tests.ts +164 -0
- package/src/stats/timeSeries.ts +92 -0
- package/src/working/fold.test.ts +196 -0
- package/src/working/fold.ts +252 -0
- package/src/working/index.ts +13 -0
- package/src/working/textSummary.test.ts +113 -0
- package/src/working/textSummary.ts +156 -0
|
@@ -6,12 +6,14 @@
|
|
|
6
6
|
|
|
7
7
|
import type { Extracted, ExtrEntryDiff, ListDiff, PackExtr } from '../types.js';
|
|
8
8
|
import type { ReadTables, Tables } from '../strings.js';
|
|
9
|
-
import { boundedCount, ByteReader, ByteWriter, type Budget } from '../varint.js';
|
|
9
|
+
import { assertConsumed, assertFlagsKnown, boundedCount, ByteReader, ByteWriter, PackFormatError, type Budget } from '../varint.js';
|
|
10
10
|
import type { SectionCodec } from './columns.js';
|
|
11
11
|
import { readEdges, readKeys, readNodes, writeEdges, writeKeys, writeNodes } from './extrLists.js';
|
|
12
12
|
import { TAG } from './rows.js';
|
|
13
13
|
|
|
14
14
|
const HAS_VERSION = 1;
|
|
15
|
+
/** The kind byte of a hash: whole, or a diff against a prior hash. Any other value is from a writer this reader does not know. */
|
|
16
|
+
const KIND_FULL = 0, KIND_DIFF = 1;
|
|
15
17
|
|
|
16
18
|
function writeDiff<T>(w: ByteWriter, t: Tables, d: ListDiff<T>, items: (w: ByteWriter, t: Tables, xs: T[]) => void): void {
|
|
17
19
|
w.varint(d.remove.length);
|
|
@@ -29,6 +31,9 @@ function readDiff<T>(r: ByteReader, t: ReadTables, budget: Budget, items: (r: By
|
|
|
29
31
|
let prev = 0;
|
|
30
32
|
for (let i = 0; i < n; i++) { prev += r.varint(); ats.push(prev); }
|
|
31
33
|
const list = items(r, t, budget);
|
|
34
|
+
// The positions and the items are counted separately on the wire: a mismatch would store `undefined` (fewer items) or
|
|
35
|
+
// silently drop items (more), so it is a malformed pack.
|
|
36
|
+
if (list.length !== n) throw new PackFormatError(`diff: ${n} add positions but ${list.length} added items`);
|
|
32
37
|
return { remove, add: ats.map((at, i) => ({ at, item: list[i]! })) };
|
|
33
38
|
}
|
|
34
39
|
|
|
@@ -40,7 +45,7 @@ export const extrSection: SectionCodec = {
|
|
|
40
45
|
const w = new ByteWriter();
|
|
41
46
|
w.varint(rows.length);
|
|
42
47
|
for (const x of rows) {
|
|
43
|
-
w.u8(x.kind === 'full' ?
|
|
48
|
+
w.u8(x.kind === 'full' ? KIND_FULL : KIND_DIFF);
|
|
44
49
|
w.varint(t.hash(x.hash));
|
|
45
50
|
if (x.kind === 'diff') w.varint(t.hash(x.prior));
|
|
46
51
|
w.varint(x.entries.length);
|
|
@@ -65,7 +70,9 @@ export const extrSection: SectionCodec = {
|
|
|
65
70
|
const n = boundedCount(r, limits.budget, 'extracted hashes');
|
|
66
71
|
const out: PackExtr[] = [];
|
|
67
72
|
for (let i = 0; i < n; i++) {
|
|
68
|
-
const
|
|
73
|
+
const kind = r.u8();
|
|
74
|
+
if (kind !== KIND_FULL && kind !== KIND_DIFF) throw new PackFormatError(`unknown EXTR entry kind ${kind}`);
|
|
75
|
+
const diff = kind === KIND_DIFF;
|
|
69
76
|
const hash = t.hash(r.varint());
|
|
70
77
|
const prior = diff ? t.hash(r.varint()) : '';
|
|
71
78
|
const count = boundedCount(r, limits.budget, 'extracted entries');
|
|
@@ -73,7 +80,9 @@ export const extrSection: SectionCodec = {
|
|
|
73
80
|
for (let k = 0; k < count; k++) {
|
|
74
81
|
const sourceRef = t.str(r.varint());
|
|
75
82
|
const sourceHash = t.hash(r.varint());
|
|
76
|
-
const
|
|
83
|
+
const vflags = r.u8();
|
|
84
|
+
assertFlagsKnown(vflags, HAS_VERSION);
|
|
85
|
+
const version = vflags & HAS_VERSION ? { extractorVersion: r.varint() } : {};
|
|
77
86
|
if (!diff) {
|
|
78
87
|
entries.push({ sourceRef: sourceRef as Extracted['sourceRef'], sourceHash, ...version, nodes: readNodes(r, t, limits.budget), edges: readEdges(r, t, limits.budget), keys: readKeys(r, t, limits.budget) });
|
|
79
88
|
} else {
|
|
@@ -85,6 +94,7 @@ export const extrSection: SectionCodec = {
|
|
|
85
94
|
}
|
|
86
95
|
out.push(diff ? { kind: 'diff', hash, prior, entries: entries as ExtrEntryDiff[] } : { kind: 'full', hash, entries: entries as Extracted[] });
|
|
87
96
|
}
|
|
97
|
+
assertConsumed(r, 'EXTR section');
|
|
88
98
|
into.extracted = out;
|
|
89
99
|
},
|
|
90
100
|
};
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import type { KEdge, KNode, NodeRef } from '../types.js';
|
|
8
8
|
import type { ReadTables, Tables } from '../strings.js';
|
|
9
|
-
import { boundedCount, ByteReader, ByteWriter, PackFormatError, type Budget } from '../varint.js';
|
|
9
|
+
import { assertFlagsKnown, boundedCount, ByteReader, ByteWriter, PackFormatError, type Budget } from '../varint.js';
|
|
10
10
|
import { EDGE_KINDS, NODE_KINDS, ORIGINS, VERDICTS, readEnum, readWord, unzigzag, writeEnum, writeWord, zigzag } from './columns.js';
|
|
11
11
|
|
|
12
12
|
const N_PARTITION = 1, N_AREA = 2, N_STALE = 4, N_STALE_V = 8, N_BADGES = 16, N_SYSTEM = 32, N_APP = 64, N_COPY = 128, N_HASH = 256;
|
|
@@ -74,7 +74,9 @@ export function readNodes(r: ByteReader, t: ReadTables, budget: Budget): KNode[]
|
|
|
74
74
|
const count = boundedCount(r, budget, 'badges');
|
|
75
75
|
for (let b = 0; b < count; b++) {
|
|
76
76
|
const key = t.str(r.varint());
|
|
77
|
-
|
|
77
|
+
const type = r.u8();
|
|
78
|
+
if (type > 1) throw new PackFormatError('unknown badge value type');
|
|
79
|
+
badges[key] = type === 0 ? r.num() : t.str(r.varint());
|
|
78
80
|
}
|
|
79
81
|
node.badges = badges;
|
|
80
82
|
}
|
|
@@ -145,7 +147,3 @@ export function readKeys(r: ByteReader, t: ReadTables, budget: Budget): Array<{
|
|
|
145
147
|
for (let i = 0; i < n; i++) { prev += unzigzag(r.varint()); refs.push(t.str(prev)); }
|
|
146
148
|
return refs.map((ref) => ({ ref: ref as NodeRef, key: t.str(r.varint()) }));
|
|
147
149
|
}
|
|
148
|
-
|
|
149
|
-
export function assertFlagsKnown(flags: number, mask: number): void {
|
|
150
|
-
if (flags & ~mask) throw new PackFormatError('unknown flag bits');
|
|
151
|
-
}
|
|
@@ -4,9 +4,8 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import type { NodeRef, StoredFact, StoredRelation } from '../types.js';
|
|
7
|
-
import { boundedCount, ByteReader, ByteWriter } from '../varint.js';
|
|
7
|
+
import { assertConsumed, assertFlagsKnown, boundedCount, ByteReader, ByteWriter } from '../varint.js';
|
|
8
8
|
import { EDGE_KINDS, STORED_ORIGINS, readEnum, readWord, writeEnum, writeWord, type SectionCodec } from './columns.js';
|
|
9
|
-
import { assertFlagsKnown } from './extrLists.js';
|
|
10
9
|
import { TAG } from './rows.js';
|
|
11
10
|
|
|
12
11
|
const F_UPDATED = 1, F_BY_NULL = 2, F_BY = 4;
|
|
@@ -72,6 +71,7 @@ export const factSection: SectionCodec = {
|
|
|
72
71
|
f.fileAnchors.push(a);
|
|
73
72
|
}
|
|
74
73
|
});
|
|
74
|
+
assertConsumed(r, 'facts section');
|
|
75
75
|
into.facts = facts;
|
|
76
76
|
},
|
|
77
77
|
};
|
|
@@ -122,5 +122,6 @@ export const relationSection: SectionCodec = {
|
|
|
122
122
|
if (f & R_STALE_NULL) x.staleAt = null;
|
|
123
123
|
return x;
|
|
124
124
|
});
|
|
125
|
+
assertConsumed(r, 'relations section');
|
|
125
126
|
},
|
|
126
127
|
};
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* Rows arrive sorted (see `normalizePack`), so index runs are delta-encoded.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { boundedCount, ByteReader, ByteWriter, PackFormatError } from '../varint.js';
|
|
8
|
+
import { assertConsumed, assertFlagsKnown, boundedCount, ByteReader, ByteWriter, PackFormatError } from '../varint.js';
|
|
9
9
|
import { fromHex64, toHex } from '../hex.js';
|
|
10
10
|
import type { Pack } from '../types.js';
|
|
11
11
|
import type { SectionCodec } from './columns.js';
|
|
@@ -34,6 +34,7 @@ export const fileSection: SectionCodec = {
|
|
|
34
34
|
for (let i = 0; i < n; i++) { prev += r.varint(); paths.push(t.str(prev)); }
|
|
35
35
|
const hashes = paths.map(() => t.hash(r.varint()));
|
|
36
36
|
into.files = paths.map((path, i) => ({ path, hash: hashes[i]!, bytes: r.varint() }));
|
|
37
|
+
assertConsumed(r, 'FILE section');
|
|
37
38
|
},
|
|
38
39
|
};
|
|
39
40
|
|
|
@@ -56,6 +57,7 @@ export const staleSection: SectionCodec = {
|
|
|
56
57
|
let prev = 0;
|
|
57
58
|
for (let i = 0; i < n; i++) { prev += r.varint(); refs.push(t.str(prev)); }
|
|
58
59
|
into.stale = refs.map((sourceRef) => ({ sourceRef, hash: t.hash(r.varint()) }));
|
|
60
|
+
assertConsumed(r, 'STALE section');
|
|
59
61
|
},
|
|
60
62
|
};
|
|
61
63
|
|
|
@@ -90,6 +92,7 @@ export const deleteSection: SectionCodec = {
|
|
|
90
92
|
const from = Array.from({ length: n }, () => t.str(r.varint()));
|
|
91
93
|
const to = Array.from({ length: n }, () => t.str(r.varint()));
|
|
92
94
|
const kind = Array.from({ length: n }, () => t.str(r.varint()));
|
|
95
|
+
assertConsumed(r, 'DEL section');
|
|
93
96
|
into.deletes = { files: lists[0]!, facts: lists[1]!, relations: from.map((f, i) => ({ from: f, to: to[i]!, kind: kind[i]! })) };
|
|
94
97
|
},
|
|
95
98
|
};
|
|
@@ -115,8 +118,10 @@ export const scopeSection: SectionCodec = {
|
|
|
115
118
|
const n = boundedCount(r, limits.budget, 'packs');
|
|
116
119
|
for (let i = 0; i < n; i++) scope.packs.push(t.str(r.varint()));
|
|
117
120
|
const flags = r.u8();
|
|
121
|
+
assertFlagsKnown(flags, 3);
|
|
118
122
|
if (flags & 1) scope.lastDigest = t.str(r.varint());
|
|
119
123
|
if (flags & 2) scope.lastReviewedAt = t.str(r.varint());
|
|
124
|
+
assertConsumed(r, 'SCOPE section');
|
|
120
125
|
into.scope = scope;
|
|
121
126
|
},
|
|
122
127
|
};
|
|
@@ -138,7 +143,10 @@ export const analysisSection: SectionCodec = {
|
|
|
138
143
|
return w.finish();
|
|
139
144
|
},
|
|
140
145
|
read(bytes, t, _limits, into) {
|
|
141
|
-
|
|
146
|
+
const r = new ByteReader(bytes);
|
|
147
|
+
const text = t.str(r.varint());
|
|
148
|
+
assertConsumed(r, 'ANALYSIS section');
|
|
149
|
+
into.analysis = JSON.parse(text) as unknown;
|
|
142
150
|
},
|
|
143
151
|
};
|
|
144
152
|
|
|
@@ -161,6 +169,7 @@ export const leafSection: SectionCodec = {
|
|
|
161
169
|
let prev = 0;
|
|
162
170
|
for (let i = 0; i < n; i++) { prev += r.varint(); keys.push(t.str(prev)); }
|
|
163
171
|
into.leaves = keys.map((key) => ({ key, digest: t.hash(r.varint()) }));
|
|
172
|
+
assertConsumed(r, 'LEAF section');
|
|
164
173
|
},
|
|
165
174
|
};
|
|
166
175
|
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
import { fromHex64, HEX64, toHex } from './hex.js';
|
|
14
14
|
import { PackTooLargeError } from './types.js';
|
|
15
|
-
import { boundedCount, ByteReader, ByteWriter, PackFormatError, utf8 } from './varint.js';
|
|
15
|
+
import { assertConsumed, boundedCount, ByteReader, ByteWriter, PackFormatError, utf8 } from './varint.js';
|
|
16
16
|
|
|
17
17
|
/** What a section encoder needs: the index of a string and of a hash. */
|
|
18
18
|
export interface Tables {
|
|
@@ -78,6 +78,8 @@ export function readStringTable(bytes: Uint8Array, maxStrings: number, maxBytes:
|
|
|
78
78
|
for (let i = 0; i < n; i++) {
|
|
79
79
|
const shared = r.varint(), rest = r.varint();
|
|
80
80
|
if (shared > prev.length) throw new PackFormatError('string table: prefix longer than the previous string');
|
|
81
|
+
// A suffix the section does not hold is a malformed table (422), checked before the size cap (413) so it is never reported as "too large".
|
|
82
|
+
if (rest > r.remaining) throw new PackFormatError('string table: suffix runs past the end of the section');
|
|
81
83
|
total += shared + rest;
|
|
82
84
|
if (total > maxBytes) throw new PackTooLargeError(maxBytes);
|
|
83
85
|
const b = new Uint8Array(shared + rest);
|
|
@@ -86,6 +88,7 @@ export function readStringTable(bytes: Uint8Array, maxStrings: number, maxBytes:
|
|
|
86
88
|
try { out.push(dec.decode(b)); } catch { throw new PackFormatError('string table: invalid UTF-8'); }
|
|
87
89
|
prev = b;
|
|
88
90
|
}
|
|
91
|
+
assertConsumed(r, 'string table');
|
|
89
92
|
return out;
|
|
90
93
|
}
|
|
91
94
|
|
|
@@ -114,6 +117,7 @@ export function readHashTable(bytes: Uint8Array, maxHashes: number): string[] {
|
|
|
114
117
|
else if (flag === 1) out.push(r.str());
|
|
115
118
|
else throw new PackFormatError('hash table: unknown entry flag');
|
|
116
119
|
}
|
|
120
|
+
assertConsumed(r, 'hash table');
|
|
117
121
|
return out;
|
|
118
122
|
}
|
|
119
123
|
|
|
@@ -85,12 +85,21 @@ export class ByteReader {
|
|
|
85
85
|
return this.buf[this.pos++]!;
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Unsigned LEB128, canonical only: at most {@link MAX_VARINT}, and no overlong form (a final byte of 0 after a
|
|
90
|
+
* continuation byte names the same value in more bytes). Both are a malformed pack, never a size error.
|
|
91
|
+
*/
|
|
88
92
|
varint(): number {
|
|
89
93
|
let result = 0, scale = 1;
|
|
90
94
|
for (let i = 0; i < 8; i++) {
|
|
91
95
|
const b = this.u8();
|
|
92
96
|
result += (b & 0x7f) * scale;
|
|
93
|
-
if ((b & 0x80) === 0)
|
|
97
|
+
if ((b & 0x80) === 0) {
|
|
98
|
+
if (b === 0 && i > 0) throw new PackFormatError('non-canonical varint (overlong encoding)');
|
|
99
|
+
// Exact below 2^53; any larger value rounds to at least 2^53, so the comparison never lets one through.
|
|
100
|
+
if (result > MAX_VARINT) throw new PackFormatError('varint exceeds the safe integer range');
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
94
103
|
scale *= 0x80;
|
|
95
104
|
}
|
|
96
105
|
throw new PackFormatError('varint too long');
|
|
@@ -130,6 +139,29 @@ export class ByteReader {
|
|
|
130
139
|
}
|
|
131
140
|
}
|
|
132
141
|
|
|
142
|
+
/** A flag word with a bit outside `mask` is from a newer writer this reader cannot honour: a malformed pack, never ignored. */
|
|
143
|
+
export function assertFlagsKnown(flags: number, mask: number): void {
|
|
144
|
+
if (flags & ~mask) throw new PackFormatError('unknown flag bits');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** A section (or table) must be read to its last byte: bytes left over mean the writer and this reader disagree. */
|
|
148
|
+
export function assertConsumed(r: ByteReader, what: string): void {
|
|
149
|
+
if (!r.done) throw new PackFormatError(`${what}: ${r.remaining} trailing bytes`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** A lone UTF-16 surrogate: a high one not followed by a low one, or a low one not preceded by a high one. */
|
|
153
|
+
const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g;
|
|
154
|
+
const ANY_SURROGATE = /[\uD800-\uDFFF]/;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* `s` as it arrives after a trip through the wire: UTF-8 cannot carry a lone surrogate, so {@link utf8} (TextEncoder)
|
|
158
|
+
* writes U+FFFD in its place. Anything hashed on one side and compared with what the other side decoded must be hashed
|
|
159
|
+
* in THIS form. The identity for every well-formed string, so no existing digest changes.
|
|
160
|
+
*/
|
|
161
|
+
export function wireString(s: string): string {
|
|
162
|
+
return ANY_SURROGATE.test(s) ? s.replace(LONE_SURROGATE, '�') : s;
|
|
163
|
+
}
|
|
164
|
+
|
|
133
165
|
/** What a whole pack may still hold: every list a decode reads draws its count from ONE budget. */
|
|
134
166
|
export interface Budget { left: number }
|
|
135
167
|
|