@voila.dev/replay 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +53 -2
- package/dist/format/index.d.ts +141 -0
- package/dist/format.js +34 -0
- package/dist/modules/index.d.ts +12 -0
- package/dist/modules.js +213 -0
- package/dist/play/index.d.ts +58 -0
- package/dist/play/mirror.d.ts +48 -0
- package/dist/play.js +903 -0
- package/dist/record/index.d.ts +91 -0
- package/dist/record/natives.d.ts +16 -0
- package/dist/record.js +880 -0
- package/dist/send/index.d.ts +57 -0
- package/dist/send.js +241 -0
- package/dist/shared/chunk-6US53Q3A.js +337 -0
- package/dist/testing/cases.d.ts +23 -0
- package/dist/testing/fuzz.d.ts +8 -0
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/oracle.d.ts +18 -0
- package/dist/testing.js +1207 -0
- package/package.json +63 -5
- package/src/format/index.ts +442 -0
- package/src/modules/index.ts +224 -0
- package/src/play/index.ts +299 -0
- package/src/play/mirror.ts +767 -0
- package/src/record/index.ts +1042 -0
- package/src/record/natives.ts +100 -0
- package/src/send/index.ts +339 -0
- package/src/testing/cases.ts +670 -0
- package/src/testing/fuzz.ts +317 -0
- package/src/testing/index.ts +8 -0
- package/src/testing/oracle.ts +318 -0
package/package.json
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@voila.dev/replay",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Session recording and replay for the web: a small recorder, a flat tape format, a DOM-built player.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/voila-voila-dev/replay.git"
|
|
10
|
+
},
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"src",
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
"./format": {
|
|
20
|
+
"types": "./dist/format/index.d.ts",
|
|
21
|
+
"default": "./dist/format.js"
|
|
22
|
+
},
|
|
23
|
+
"./record": {
|
|
24
|
+
"types": "./dist/record/index.d.ts",
|
|
25
|
+
"default": "./dist/record.js"
|
|
26
|
+
},
|
|
27
|
+
"./send": {
|
|
28
|
+
"types": "./dist/send/index.d.ts",
|
|
29
|
+
"default": "./dist/send.js"
|
|
30
|
+
},
|
|
31
|
+
"./play": {
|
|
32
|
+
"types": "./dist/play/index.d.ts",
|
|
33
|
+
"default": "./dist/play.js"
|
|
34
|
+
},
|
|
35
|
+
"./modules": {
|
|
36
|
+
"types": "./dist/modules/index.d.ts",
|
|
37
|
+
"default": "./dist/modules.js"
|
|
38
|
+
},
|
|
39
|
+
"./testing": {
|
|
40
|
+
"types": "./dist/testing/index.d.ts",
|
|
41
|
+
"default": "./dist/testing.js"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "node scripts/build.mjs",
|
|
46
|
+
"typecheck": "tsc --noEmit",
|
|
47
|
+
"test": "bun test test/unit",
|
|
48
|
+
"test:browser": "node scripts/build.mjs && node test/browser/run.mjs",
|
|
49
|
+
"size": "node scripts/build.mjs && node scripts/size.mjs",
|
|
50
|
+
"prepack": "node scripts/build.mjs",
|
|
51
|
+
"changeset": "changeset"
|
|
52
|
+
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@changesets/changelog-github": "0.5.2",
|
|
58
|
+
"@changesets/cli": "2.29.8",
|
|
59
|
+
"esbuild": "0.25.12",
|
|
60
|
+
"playwright": "1.61.1",
|
|
61
|
+
"typescript": "5.9.3",
|
|
62
|
+
"@types/bun": "1.3.3"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
// The tape: one flat array of numbers, strings and nulls. An opcode, then its operands.
|
|
2
|
+
// Nodes carry no number on the tape: each op that makes a node takes the next number, on
|
|
3
|
+
// both sides. After a META, the document is node 1.
|
|
4
|
+
//
|
|
5
|
+
// Codes 0 to 127 have a fixed length, frozen for the whole of version 2. Codes from 128 are
|
|
6
|
+
// framed (`code, n, …n operands`), so that a player skips the ones it does not know: every op
|
|
7
|
+
// added after version 2.0 goes there.
|
|
8
|
+
|
|
9
|
+
export const VERSION = 2;
|
|
10
|
+
|
|
11
|
+
export enum Op {
|
|
12
|
+
/** Closes the node opened last. */
|
|
13
|
+
X = 0,
|
|
14
|
+
/** name, n, then n (name, value) pairs. Makes an element and opens it. */
|
|
15
|
+
E = 1,
|
|
16
|
+
/** data. Makes a text node. */
|
|
17
|
+
T = 2,
|
|
18
|
+
/** name, publicId, systemId. Makes a doctype. */
|
|
19
|
+
D = 3,
|
|
20
|
+
/** name, width, height. Makes an element recorded as an empty box. */
|
|
21
|
+
B = 4,
|
|
22
|
+
/** Opens the content of the template opened last. Makes a node. */
|
|
23
|
+
TPL = 5,
|
|
24
|
+
/** mode. Opens the shadow root of the element opened last. Makes a node. */
|
|
25
|
+
SHADOW = 6,
|
|
26
|
+
/** Opens the document of the iframe opened last. Makes a node. */
|
|
27
|
+
FRAME = 7,
|
|
28
|
+
/** id. A node already numbered, moved here. */
|
|
29
|
+
R = 8,
|
|
30
|
+
|
|
31
|
+
/** parent, anchor (0: at the end). Opens a node to insert before the anchor. */
|
|
32
|
+
INS = 16,
|
|
33
|
+
/** id */
|
|
34
|
+
DEL = 17,
|
|
35
|
+
/** id, name, value or null */
|
|
36
|
+
ATTR = 18,
|
|
37
|
+
/** id, data */
|
|
38
|
+
TEXT = 19,
|
|
39
|
+
/** id, value: a string, or 0/1 for `checked` and for an option's `selected` */
|
|
40
|
+
VAL = 20,
|
|
41
|
+
/** id, state: 0 closed, 1 open, 2 modal */
|
|
42
|
+
OPEN = 21,
|
|
43
|
+
|
|
44
|
+
/** node or 0 (a constructed sheet), sheet number, rules as text */
|
|
45
|
+
SHEET = 32,
|
|
46
|
+
/** sheet number, index path ("3", "3.1"), rule text or null (deleted) */
|
|
47
|
+
RULE = 33,
|
|
48
|
+
/** node (the document or a shadow root), n, then n sheet numbers */
|
|
49
|
+
ADOPT = 34,
|
|
50
|
+
|
|
51
|
+
/** milliseconds since the previous tick */
|
|
52
|
+
TICK = 48,
|
|
53
|
+
/** x, y */
|
|
54
|
+
MOVE = 49,
|
|
55
|
+
/** kind (1 down, 2 up, 3 click), id, x, y */
|
|
56
|
+
TAP = 50,
|
|
57
|
+
/** id (1: the page), x, y */
|
|
58
|
+
SCROLL = 51,
|
|
59
|
+
/** width, height */
|
|
60
|
+
SIZE = 52,
|
|
61
|
+
/** id, or 0 when nothing has focus */
|
|
62
|
+
FOCUS = 53,
|
|
63
|
+
/** 0 visible, 1 hidden */
|
|
64
|
+
HIDE = 54,
|
|
65
|
+
|
|
66
|
+
/** version, url, time (ms since epoch), width, height, mode (0 standards, 1 quirks) */
|
|
67
|
+
META = 96,
|
|
68
|
+
/** nodes, fingerprint: what the player checks its copy against */
|
|
69
|
+
PRINT = 97,
|
|
70
|
+
/** reason (see End) */
|
|
71
|
+
END = 98,
|
|
72
|
+
|
|
73
|
+
/** framed: id, a canvas as a data URL */
|
|
74
|
+
SHOT = 128,
|
|
75
|
+
/** framed: the name of a module cut off after it threw */
|
|
76
|
+
ERR = 129,
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export enum End {
|
|
80
|
+
Consent = 1,
|
|
81
|
+
Budget = 2,
|
|
82
|
+
Network = 3,
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export type Token = number | string | null;
|
|
86
|
+
export type Tape = Array<Token>;
|
|
87
|
+
/** On the wire, `[hash]` stands for a long string and `[hash, 1]` for a piece of tape. */
|
|
88
|
+
export type WireToken = Token | [string] | [string, 1];
|
|
89
|
+
export type WireTape = Array<WireToken>;
|
|
90
|
+
|
|
91
|
+
/** Operand count of each fixed op. E and ADOPT carry their own counts. */
|
|
92
|
+
const ARITY: ReadonlyArray<number | undefined> = (() => {
|
|
93
|
+
const a: Array<number | undefined> = [];
|
|
94
|
+
const set = (op: Op, n: number) => {
|
|
95
|
+
a[op] = n;
|
|
96
|
+
};
|
|
97
|
+
set(Op.X, 0);
|
|
98
|
+
set(Op.E, -1);
|
|
99
|
+
set(Op.T, 1);
|
|
100
|
+
set(Op.D, 3);
|
|
101
|
+
set(Op.B, 3);
|
|
102
|
+
set(Op.TPL, 0);
|
|
103
|
+
set(Op.SHADOW, 1);
|
|
104
|
+
set(Op.FRAME, 0);
|
|
105
|
+
set(Op.R, 1);
|
|
106
|
+
set(Op.INS, 2);
|
|
107
|
+
set(Op.DEL, 1);
|
|
108
|
+
set(Op.ATTR, 3);
|
|
109
|
+
set(Op.TEXT, 2);
|
|
110
|
+
set(Op.VAL, 2);
|
|
111
|
+
set(Op.OPEN, 2);
|
|
112
|
+
set(Op.SHEET, 3);
|
|
113
|
+
set(Op.RULE, 3);
|
|
114
|
+
set(Op.ADOPT, -1);
|
|
115
|
+
set(Op.TICK, 1);
|
|
116
|
+
set(Op.MOVE, 2);
|
|
117
|
+
set(Op.TAP, 4);
|
|
118
|
+
set(Op.SCROLL, 3);
|
|
119
|
+
set(Op.SIZE, 2);
|
|
120
|
+
set(Op.FOCUS, 1);
|
|
121
|
+
set(Op.HIDE, 1);
|
|
122
|
+
set(Op.META, 6);
|
|
123
|
+
set(Op.PRINT, 2);
|
|
124
|
+
set(Op.END, 1);
|
|
125
|
+
return a;
|
|
126
|
+
})();
|
|
127
|
+
|
|
128
|
+
/** The ops that make a node, and so take the next number. */
|
|
129
|
+
export function makesNode(op: number): boolean {
|
|
130
|
+
return (
|
|
131
|
+
op === Op.E ||
|
|
132
|
+
op === Op.T ||
|
|
133
|
+
op === Op.D ||
|
|
134
|
+
op === Op.B ||
|
|
135
|
+
op === Op.TPL ||
|
|
136
|
+
op === Op.SHADOW ||
|
|
137
|
+
op === Op.FRAME
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The ops a matching X closes. */
|
|
142
|
+
export function opens(op: number): boolean {
|
|
143
|
+
return (
|
|
144
|
+
op === Op.E ||
|
|
145
|
+
op === Op.TPL ||
|
|
146
|
+
op === Op.SHADOW ||
|
|
147
|
+
op === Op.FRAME ||
|
|
148
|
+
op === Op.INS
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* How many tokens the op at `i` takes, its code included; -1 when the tape is cut short or the
|
|
154
|
+
* code is unknown and not framed.
|
|
155
|
+
*/
|
|
156
|
+
export function length(tape: ReadonlyArray<unknown>, i: number): number {
|
|
157
|
+
const op = tape[i];
|
|
158
|
+
if (typeof op !== "number") return -1;
|
|
159
|
+
let n: number;
|
|
160
|
+
if (op >= 128) {
|
|
161
|
+
const framed = tape[i + 1];
|
|
162
|
+
if (!isCount(framed)) return -1;
|
|
163
|
+
n = 2 + framed;
|
|
164
|
+
} else if (op === Op.E) {
|
|
165
|
+
const count = tape[i + 2];
|
|
166
|
+
if (!isCount(count)) return -1;
|
|
167
|
+
n = 3 + 2 * count;
|
|
168
|
+
} else if (op === Op.ADOPT) {
|
|
169
|
+
const count = tape[i + 2];
|
|
170
|
+
if (!isCount(count)) return -1;
|
|
171
|
+
n = 3 + count;
|
|
172
|
+
} else {
|
|
173
|
+
const arity = ARITY[op];
|
|
174
|
+
if (arity === undefined) return -1;
|
|
175
|
+
n = 1 + arity;
|
|
176
|
+
}
|
|
177
|
+
return i + n <= tape.length ? n : -1;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function isCount(v: unknown): v is number {
|
|
181
|
+
return typeof v === "number" && Number.isInteger(v) && v >= 0 && v < 1e6;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const isNum = (v: unknown): v is number =>
|
|
185
|
+
typeof v === "number" && Number.isFinite(v);
|
|
186
|
+
const isId = (v: unknown): v is number =>
|
|
187
|
+
typeof v === "number" && Number.isInteger(v) && v > 0;
|
|
188
|
+
const isStr = (v: unknown): v is string => typeof v === "string";
|
|
189
|
+
|
|
190
|
+
export type Invalid = { readonly at: number; readonly reason: string };
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Checks a tape before anything reads it: lengths, operand types, balanced opens, and node
|
|
194
|
+
* numbers that exist. `base` is the number the first node made in this tape takes (from the
|
|
195
|
+
* envelope); a tape that starts on a META needs none.
|
|
196
|
+
*/
|
|
197
|
+
export function validate(
|
|
198
|
+
tape: ReadonlyArray<unknown>,
|
|
199
|
+
base?: number,
|
|
200
|
+
): Invalid | null {
|
|
201
|
+
if (!Array.isArray(tape)) return { at: 0, reason: "not an array" };
|
|
202
|
+
let made = base === undefined ? Number.POSITIVE_INFINITY : base - 1;
|
|
203
|
+
let depth = 0;
|
|
204
|
+
for (let i = 0; i < tape.length; ) {
|
|
205
|
+
const n = length(tape, i);
|
|
206
|
+
if (n < 0) return { at: i, reason: `bad op ${String(tape[i])}` };
|
|
207
|
+
const op = tape[i] as number;
|
|
208
|
+
const a = (k: number) => tape[i + k];
|
|
209
|
+
const known = (v: unknown) => isId(v) && v <= made;
|
|
210
|
+
const bad = (reason: string) => ({ at: i, reason });
|
|
211
|
+
switch (op) {
|
|
212
|
+
case Op.X:
|
|
213
|
+
if (--depth < 0) return bad("X without open");
|
|
214
|
+
break;
|
|
215
|
+
case Op.E:
|
|
216
|
+
if (!isStr(a(1))) return bad("E name");
|
|
217
|
+
for (let k = 3; k < n; k++) if (!isStr(a(k))) return bad("E attribute");
|
|
218
|
+
break;
|
|
219
|
+
case Op.T:
|
|
220
|
+
if (!isStr(a(1))) return bad("T data");
|
|
221
|
+
break;
|
|
222
|
+
case Op.D:
|
|
223
|
+
if (!isStr(a(1)) || !isStr(a(2)) || !isStr(a(3))) return bad("D");
|
|
224
|
+
break;
|
|
225
|
+
case Op.B:
|
|
226
|
+
if (!isStr(a(1)) || !isNum(a(2)) || !isNum(a(3))) return bad("B");
|
|
227
|
+
break;
|
|
228
|
+
case Op.SHADOW:
|
|
229
|
+
if (!isStr(a(1))) return bad("SHADOW mode");
|
|
230
|
+
break;
|
|
231
|
+
case Op.R:
|
|
232
|
+
case Op.DEL:
|
|
233
|
+
if (!known(a(1))) return bad("unknown node");
|
|
234
|
+
break;
|
|
235
|
+
case Op.INS:
|
|
236
|
+
if (!known(a(1)) || !(a(2) === 0 || known(a(2))))
|
|
237
|
+
return bad("INS nodes");
|
|
238
|
+
break;
|
|
239
|
+
case Op.ATTR:
|
|
240
|
+
if (!known(a(1)) || !isStr(a(2)) || !(a(3) === null || isStr(a(3))))
|
|
241
|
+
return bad("ATTR");
|
|
242
|
+
break;
|
|
243
|
+
case Op.TEXT:
|
|
244
|
+
if (!known(a(1)) || !isStr(a(2))) return bad("TEXT");
|
|
245
|
+
break;
|
|
246
|
+
case Op.VAL:
|
|
247
|
+
if (!known(a(1)) || !(isStr(a(2)) || a(2) === 0 || a(2) === 1))
|
|
248
|
+
return bad("VAL");
|
|
249
|
+
break;
|
|
250
|
+
case Op.OPEN:
|
|
251
|
+
if (!known(a(1)) || !(a(2) === 0 || a(2) === 1 || a(2) === 2))
|
|
252
|
+
return bad("OPEN");
|
|
253
|
+
break;
|
|
254
|
+
case Op.SHEET:
|
|
255
|
+
if (!(a(1) === 0 || known(a(1))) || !isCount(a(2)) || !isStr(a(3)))
|
|
256
|
+
return bad("SHEET");
|
|
257
|
+
break;
|
|
258
|
+
case Op.RULE:
|
|
259
|
+
if (!isCount(a(1)) || !isStr(a(2)) || !(a(3) === null || isStr(a(3))))
|
|
260
|
+
return bad("RULE");
|
|
261
|
+
break;
|
|
262
|
+
case Op.ADOPT:
|
|
263
|
+
if (!known(a(1))) return bad("ADOPT node");
|
|
264
|
+
for (let k = 3; k < n; k++) if (!isCount(a(k))) return bad("ADOPT sheet");
|
|
265
|
+
break;
|
|
266
|
+
case Op.TICK:
|
|
267
|
+
if (!isNum(a(1)) || (a(1) as number) < 0) return bad("TICK");
|
|
268
|
+
break;
|
|
269
|
+
case Op.MOVE:
|
|
270
|
+
case Op.SIZE:
|
|
271
|
+
if (!isNum(a(1)) || !isNum(a(2))) return bad("numbers");
|
|
272
|
+
break;
|
|
273
|
+
case Op.TAP:
|
|
274
|
+
if (!isNum(a(1)) || !isNum(a(2)) || !isNum(a(3)) || !isNum(a(4)))
|
|
275
|
+
return bad("TAP");
|
|
276
|
+
break;
|
|
277
|
+
case Op.SCROLL:
|
|
278
|
+
if (!isNum(a(1)) || !isNum(a(2)) || !isNum(a(3))) return bad("SCROLL");
|
|
279
|
+
break;
|
|
280
|
+
case Op.FOCUS:
|
|
281
|
+
case Op.HIDE:
|
|
282
|
+
case Op.END:
|
|
283
|
+
if (!isNum(a(1))) return bad("number");
|
|
284
|
+
break;
|
|
285
|
+
case Op.PRINT:
|
|
286
|
+
if (!isNum(a(1)) || !isNum(a(2))) return bad("PRINT");
|
|
287
|
+
break;
|
|
288
|
+
case Op.META:
|
|
289
|
+
if (a(1) !== VERSION) return bad(`version ${String(a(1))}`);
|
|
290
|
+
if (!isStr(a(2)) || !isNum(a(3)) || !isNum(a(4)) || !isNum(a(5)))
|
|
291
|
+
return bad("META");
|
|
292
|
+
if (depth !== 0) return bad("META inside an open node");
|
|
293
|
+
made = 1;
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
if (makesNode(op)) made++;
|
|
297
|
+
if (opens(op)) depth++;
|
|
298
|
+
i += n;
|
|
299
|
+
}
|
|
300
|
+
if (depth !== 0) return { at: tape.length, reason: "unclosed" };
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** How many nodes a tape makes; when it holds a META, counted from its last META. */
|
|
305
|
+
export function made(tape: Tape): { nodes: number; fromMeta: boolean } {
|
|
306
|
+
let nodes = 0;
|
|
307
|
+
let fromMeta = false;
|
|
308
|
+
for (let i = 0; i < tape.length; ) {
|
|
309
|
+
const n = length(tape, i);
|
|
310
|
+
if (n < 0) break;
|
|
311
|
+
const op = tape[i] as number;
|
|
312
|
+
if (op === Op.META) {
|
|
313
|
+
nodes = 1;
|
|
314
|
+
fromMeta = true;
|
|
315
|
+
} else if (makesNode(op)) nodes++;
|
|
316
|
+
i += n;
|
|
317
|
+
}
|
|
318
|
+
return { nodes, fromMeta };
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** Recorded milliseconds of a tape: the time of its first META (if any) plus its ticks. */
|
|
322
|
+
export function span(tape: Tape): { start: number | null; ms: number } {
|
|
323
|
+
let start: number | null = null;
|
|
324
|
+
let ms = 0;
|
|
325
|
+
for (let i = 0; i < tape.length; ) {
|
|
326
|
+
const n = length(tape, i);
|
|
327
|
+
if (n < 0) break;
|
|
328
|
+
const op = tape[i];
|
|
329
|
+
if (op === Op.META && start === null) start = tape[i + 3] as number;
|
|
330
|
+
else if (op === Op.TICK) ms += tape[i + 1] as number;
|
|
331
|
+
i += n;
|
|
332
|
+
}
|
|
333
|
+
return { start, ms };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** Puts back what the wire replaced by hashes. Throws on a hash it cannot find. */
|
|
337
|
+
export function resolve(
|
|
338
|
+
wire: ReadonlyArray<WireToken>,
|
|
339
|
+
blob: (hash: string) => string | undefined,
|
|
340
|
+
depth = 0,
|
|
341
|
+
): Tape {
|
|
342
|
+
if (depth > 4) throw new Error("replay: pieces nested too deep");
|
|
343
|
+
const out: Tape = [];
|
|
344
|
+
for (const token of wire) {
|
|
345
|
+
if (!Array.isArray(token)) {
|
|
346
|
+
out.push(token);
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
const hash = token[0];
|
|
350
|
+
const stored = typeof hash === "string" ? blob(hash) : undefined;
|
|
351
|
+
if (stored === undefined) throw new MissingBlob(String(hash));
|
|
352
|
+
if (token[1] === 1) {
|
|
353
|
+
const piece: unknown = JSON.parse(stored);
|
|
354
|
+
if (!Array.isArray(piece)) throw new Error("replay: a piece is no tape");
|
|
355
|
+
for (const t of resolve(piece as WireTape, blob, depth + 1)) out.push(t);
|
|
356
|
+
} else out.push(stored);
|
|
357
|
+
}
|
|
358
|
+
return out;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export class MissingBlob extends Error {
|
|
362
|
+
constructor(readonly hash: string) {
|
|
363
|
+
super(`replay: missing ${hash}`);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** The hashes a wire tape refers to, pieces' own references not included. */
|
|
368
|
+
export function references(wire: ReadonlyArray<WireToken>): Array<string> {
|
|
369
|
+
const out: Array<string> = [];
|
|
370
|
+
for (const token of wire)
|
|
371
|
+
if (Array.isArray(token) && typeof token[0] === "string") out.push(token[0]);
|
|
372
|
+
return out;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** 32-bit FNV-1a, step by step: what PRINT's fingerprint is made of. */
|
|
376
|
+
export function fnv(h: number, v: number): number {
|
|
377
|
+
return Math.imul(h ^ (v & 0xffff), 16777619) >>> 0;
|
|
378
|
+
}
|
|
379
|
+
export const FNV_START = 2166136261;
|
|
380
|
+
|
|
381
|
+
/** What a chunk carries besides its tape, as both sides see it. */
|
|
382
|
+
export interface Chunk {
|
|
383
|
+
/** The number the first node made in this chunk takes. */
|
|
384
|
+
readonly base: number;
|
|
385
|
+
/** Its rank in the page view: 0, 1, 2… A missing rank is a hole, even one that made no node. */
|
|
386
|
+
readonly seq?: number;
|
|
387
|
+
readonly tape: Tape;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Joins chunks into one tape: a hole (a chunk lost on the way) becomes a tick of the time it
|
|
392
|
+
* spanned, and nothing is played until the next snapshot.
|
|
393
|
+
*/
|
|
394
|
+
export function join(
|
|
395
|
+
chunks: ReadonlyArray<Chunk>,
|
|
396
|
+
into: Tape = [],
|
|
397
|
+
from: { next: number; broken: boolean; seq?: number } = { next: 0, broken: true },
|
|
398
|
+
) {
|
|
399
|
+
const gaps: Array<{ index: number; ms: number }> = [];
|
|
400
|
+
for (const chunk of chunks) {
|
|
401
|
+
const tape = chunk.tape;
|
|
402
|
+
if (validate(tape, tape[0] === Op.META ? undefined : chunk.base)) {
|
|
403
|
+
// A chunk that does not read is a hole like a lost one.
|
|
404
|
+
const ms = span(tape).ms;
|
|
405
|
+
gaps.push({ index: into.length, ms });
|
|
406
|
+
if (ms) into.push(Op.TICK, ms);
|
|
407
|
+
from.broken = true;
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
let start = 0;
|
|
411
|
+
const skipped = chunk.seq !== undefined && from.seq !== undefined && chunk.seq !== from.seq + 1;
|
|
412
|
+
if (chunk.seq !== undefined) from.seq = chunk.seq;
|
|
413
|
+
if (from.broken || skipped || chunk.base !== from.next) {
|
|
414
|
+
const meta = findMeta(tape);
|
|
415
|
+
if (meta < 0) {
|
|
416
|
+
const ms = span(tape).ms;
|
|
417
|
+
gaps.push({ index: into.length, ms });
|
|
418
|
+
if (ms) into.push(Op.TICK, ms);
|
|
419
|
+
from.broken = true;
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
start = meta;
|
|
423
|
+
}
|
|
424
|
+
const part = start ? tape.slice(start) : tape;
|
|
425
|
+
for (const t of part) into.push(t);
|
|
426
|
+
const m = made(part);
|
|
427
|
+
from.next = m.fromMeta ? m.nodes + 1 : from.next + m.nodes;
|
|
428
|
+
from.broken = false;
|
|
429
|
+
}
|
|
430
|
+
return { tape: into, gaps, state: from };
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function findMeta(tape: Tape): number {
|
|
434
|
+
for (let i = 0; i < tape.length; ) {
|
|
435
|
+
if (tape[i] === Op.META) return i;
|
|
436
|
+
const n = length(tape, i);
|
|
437
|
+
if (n < 0) return -1;
|
|
438
|
+
i += n;
|
|
439
|
+
}
|
|
440
|
+
return -1;
|
|
441
|
+
}
|
|
442
|
+
|