@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.
@@ -0,0 +1,57 @@
1
+ import type { Tape, WireTape } from "../format/index";
2
+ export interface Head {
3
+ readonly property?: string;
4
+ readonly visitor?: string;
5
+ readonly url: string;
6
+ readonly pv: string;
7
+ }
8
+ export interface Answer {
9
+ readonly need?: ReadonlyArray<string>;
10
+ readonly snap?: boolean;
11
+ }
12
+ export interface Envelope extends Head {
13
+ readonly v: 2;
14
+ readonly seq?: number;
15
+ readonly base?: number;
16
+ readonly startedAt?: number;
17
+ readonly endedAt?: number;
18
+ readonly tape?: WireTape;
19
+ readonly blobs?: Readonly<Record<string, string>>;
20
+ readonly last?: true;
21
+ }
22
+ export interface Piece {
23
+ readonly tape: Tape;
24
+ readonly base: number;
25
+ readonly startedAt: number;
26
+ readonly endedAt: number;
27
+ /** For a snapshot: where it may be cut into pieces hashed one by one. */
28
+ readonly cuts?: ReadonlyArray<number>;
29
+ }
30
+ export interface ConnectOptions {
31
+ readonly endpoint: string;
32
+ /** The page view this goes with; undefined while it has no id yet (a prerendered page). */
33
+ readonly head: () => Head | undefined;
34
+ /** Posts a body and reads the answer; `fetch` by default. */
35
+ readonly post?: (endpoint: string, body: string) => Promise<Answer | undefined>;
36
+ /** Sends a body that must outlive the page; `navigator.sendBeacon` by default. */
37
+ readonly beacon?: (endpoint: string, body: string) => boolean;
38
+ /** The server saw a hole: take a new snapshot and send it. */
39
+ readonly onSnap?: () => void;
40
+ /** Sending failed for good: nothing more goes for this page view. */
41
+ readonly onFail?: () => void;
42
+ /** Waits between two tries; `setTimeout` by default. */
43
+ readonly wait?: (ms: number) => Promise<void>;
44
+ }
45
+ export interface Link {
46
+ send(piece: Piece): Promise<void>;
47
+ /** The page is going away: one beacon with what is left. */
48
+ leave(piece?: Piece): void;
49
+ /** Settles once everything queued has been sent, or given up. */
50
+ idle(): Promise<void>;
51
+ readonly failed: boolean;
52
+ }
53
+ /** 16 bytes of SHA-256, base64url. Null where `crypto.subtle` is missing (a page in `http`). */
54
+ export declare function hash(text: string): Promise<string | null>;
55
+ /** Replaces long strings and, for a snapshot, each piece by its hash. `kept` gets what was replaced. */
56
+ export declare function pack(tape: Tape, cuts: ReadonlyArray<number> | undefined, kept: Map<string, string>): Promise<WireTape>;
57
+ export declare function connect(o: ConnectOptions): Link;
package/dist/send.js ADDED
@@ -0,0 +1,241 @@
1
+ // src/send/index.ts
2
+ var LONG = 256;
3
+ var BEACON_BYTES = 6e4;
4
+ var RETRIES = [1e3, 4e3, 16e3];
5
+ var encoder = new TextEncoder();
6
+ async function hash(text) {
7
+ const subtle = globalThis.crypto?.subtle;
8
+ if (!subtle) return null;
9
+ const digest = await subtle.digest("SHA-256", encoder.encode(text));
10
+ let s = "";
11
+ for (const byte of new Uint8Array(digest, 0, 16)) s += String.fromCharCode(byte);
12
+ return btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
13
+ }
14
+ async function pack(tape, cuts, kept) {
15
+ const strings = async (from, to) => {
16
+ const out2 = [];
17
+ for (let i = from; i < to; i++) {
18
+ const token = tape[i];
19
+ if (typeof token === "string" && token.length > LONG) {
20
+ const h = await hash(token);
21
+ if (h === null) {
22
+ out2.push(token);
23
+ continue;
24
+ }
25
+ kept.set(h, token);
26
+ out2.push([h]);
27
+ } else out2.push(token);
28
+ }
29
+ return out2;
30
+ };
31
+ if (!cuts?.length) return strings(0, tape.length);
32
+ const out = await strings(0, cuts[0]);
33
+ for (let c = 0; c < cuts.length; c++) {
34
+ const from = cuts[c];
35
+ const to = c + 1 < cuts.length ? cuts[c + 1] : tape.length;
36
+ if (to <= from) continue;
37
+ const part = await strings(from, to);
38
+ const text = JSON.stringify(part);
39
+ const h = await hash(text);
40
+ if (h === null) {
41
+ for (const t of part) out.push(t);
42
+ continue;
43
+ }
44
+ kept.set(h, text);
45
+ out.push([h, 1]);
46
+ }
47
+ return out;
48
+ }
49
+ var bytesOf = (s) => encoder.encode(s).length;
50
+ function connect(o) {
51
+ const post = o.post || (async (endpoint, body) => {
52
+ const W = globalThis;
53
+ let payload = body;
54
+ if (W.CompressionStream)
55
+ payload = await new Response(
56
+ new Blob([body]).stream().pipeThrough(new W.CompressionStream("gzip"))
57
+ ).blob();
58
+ const response = await fetch(endpoint, {
59
+ method: "POST",
60
+ body: payload,
61
+ headers: { "content-type": "text/plain" }
62
+ });
63
+ if (!response.ok) throw new Error(`replay: ${response.status}`);
64
+ const text = await response.text();
65
+ return text ? JSON.parse(text) : void 0;
66
+ });
67
+ const beacon = o.beacon || ((endpoint, body) => {
68
+ try {
69
+ if (navigator.sendBeacon(endpoint, new Blob([body], { type: "text/plain" }))) return true;
70
+ } catch {
71
+ }
72
+ try {
73
+ fetch(endpoint, {
74
+ method: "POST",
75
+ body,
76
+ keepalive: true,
77
+ headers: { "content-type": "text/plain" }
78
+ }).catch(() => {
79
+ });
80
+ return true;
81
+ } catch {
82
+ return false;
83
+ }
84
+ });
85
+ const wait = o.wait || ((ms) => new Promise((r) => setTimeout(r, ms)));
86
+ let queue = Promise.resolve();
87
+ let pv;
88
+ let seq = 0;
89
+ let failed = false;
90
+ const kept = /* @__PURE__ */ new Map();
91
+ const wanted = /* @__PURE__ */ new Set();
92
+ let held = [];
93
+ function envelope(head, rest) {
94
+ return {
95
+ v: 2,
96
+ ...head.property === void 0 ? {} : { property: head.property },
97
+ ...head.visitor === void 0 ? {} : { visitor: String(head.visitor).slice(0, 64) },
98
+ url: head.url,
99
+ pv: head.pv,
100
+ ...rest
101
+ };
102
+ }
103
+ async function attempt(body) {
104
+ for (let k = 0; ; k++) {
105
+ try {
106
+ return await post(o.endpoint, body);
107
+ } catch (e) {
108
+ const delay = RETRIES[k];
109
+ if (delay === void 0) throw e;
110
+ await wait(delay);
111
+ }
112
+ }
113
+ }
114
+ const pending = [];
115
+ async function deliver(entry) {
116
+ const { head, piece } = entry;
117
+ const local = /* @__PURE__ */ new Map();
118
+ const tape = await pack(piece.tape, piece.cuts, local);
119
+ for (const [h, v] of local) kept.set(h, v);
120
+ const body = JSON.stringify(
121
+ envelope(head, {
122
+ seq: entry.seq,
123
+ base: piece.base,
124
+ startedAt: piece.startedAt,
125
+ endedAt: piece.endedAt,
126
+ tape
127
+ })
128
+ );
129
+ let answer = await attempt(body);
130
+ for (let round = 0; round < 3; round++) {
131
+ if (answer?.snap) o.onSnap?.();
132
+ const need = (answer?.need || []).filter((h) => kept.has(h));
133
+ if (!need.length) break;
134
+ for (const h of need) wanted.add(h);
135
+ const blobs = {};
136
+ for (const h of need) blobs[h] = kept.get(h);
137
+ answer = await attempt(JSON.stringify(envelope(head, { blobs })));
138
+ for (const h of need) {
139
+ wanted.delete(h);
140
+ kept.delete(h);
141
+ }
142
+ }
143
+ for (const h of local.keys()) kept.delete(h);
144
+ }
145
+ function current() {
146
+ const head = o.head();
147
+ if (head && head.pv !== pv) {
148
+ pv = head.pv;
149
+ seq = 0;
150
+ }
151
+ return head;
152
+ }
153
+ function send(piece) {
154
+ if (failed) return queue;
155
+ const head = current();
156
+ if (!head) {
157
+ held.push(piece);
158
+ return queue;
159
+ }
160
+ const pieces = [...held, piece];
161
+ held = [];
162
+ for (const p of pieces) {
163
+ if (!p.tape.length) continue;
164
+ const entry = { head, piece: p, seq: seq++, cancelled: false };
165
+ pending.push(entry);
166
+ queue = queue.then(() => {
167
+ const k = pending.indexOf(entry);
168
+ if (k >= 0) pending.splice(k, 1);
169
+ if (failed || entry.cancelled) return;
170
+ return deliver(entry);
171
+ }).catch(() => {
172
+ failed = true;
173
+ o.onFail?.();
174
+ });
175
+ }
176
+ return queue;
177
+ }
178
+ function leave(piece) {
179
+ if (failed) return;
180
+ const head = current();
181
+ if (!head) return;
182
+ const queued = pending.filter((e) => e.head.pv === head.pv && !e.cancelled);
183
+ const pieces = [
184
+ ...queued.map((e) => e.piece),
185
+ ...held,
186
+ ...piece && piece.tape.length ? [piece] : []
187
+ ];
188
+ held = [];
189
+ const tape = [];
190
+ for (const p of pieces) for (const t of p.tape) tape.push(t);
191
+ const first = pieces[0];
192
+ const lastPiece = pieces[pieces.length - 1];
193
+ const empty = JSON.stringify(envelope(head, { last: true }));
194
+ let body = empty;
195
+ if (first && lastPiece) {
196
+ const number = queued[0] ? queued[0].seq : seq;
197
+ const withTape = JSON.stringify(
198
+ envelope(head, {
199
+ seq: number,
200
+ base: first.base,
201
+ startedAt: first.startedAt,
202
+ endedAt: lastPiece.endedAt,
203
+ tape,
204
+ last: true
205
+ })
206
+ );
207
+ if (bytesOf(withTape) <= BEACON_BYTES) {
208
+ body = withTape;
209
+ for (const e of queued) e.cancelled = true;
210
+ seq = number + 1;
211
+ }
212
+ }
213
+ const blobs = {};
214
+ let size = bytesOf(body);
215
+ const order = [...wanted, ...[...kept.keys()].filter((h) => !wanted.has(h))];
216
+ for (const h of order) {
217
+ const v = kept.get(h);
218
+ if (v === void 0) continue;
219
+ const extra = bytesOf(JSON.stringify(h)) + bytesOf(JSON.stringify(v)) + 2;
220
+ if (size + extra + 12 > BEACON_BYTES) continue;
221
+ blobs[h] = v;
222
+ size += extra;
223
+ }
224
+ if (Object.keys(blobs).length) body = JSON.stringify({ ...JSON.parse(body), blobs });
225
+ if (body === empty) return;
226
+ beacon(o.endpoint, body);
227
+ }
228
+ return {
229
+ send,
230
+ leave,
231
+ idle: () => queue,
232
+ get failed() {
233
+ return failed;
234
+ }
235
+ };
236
+ }
237
+ export {
238
+ connect,
239
+ hash,
240
+ pack
241
+ };
@@ -0,0 +1,337 @@
1
+ // src/format/index.ts
2
+ var VERSION = 2;
3
+ var Op = /* @__PURE__ */ ((Op2) => {
4
+ Op2[Op2["X"] = 0] = "X";
5
+ Op2[Op2["E"] = 1] = "E";
6
+ Op2[Op2["T"] = 2] = "T";
7
+ Op2[Op2["D"] = 3] = "D";
8
+ Op2[Op2["B"] = 4] = "B";
9
+ Op2[Op2["TPL"] = 5] = "TPL";
10
+ Op2[Op2["SHADOW"] = 6] = "SHADOW";
11
+ Op2[Op2["FRAME"] = 7] = "FRAME";
12
+ Op2[Op2["R"] = 8] = "R";
13
+ Op2[Op2["INS"] = 16] = "INS";
14
+ Op2[Op2["DEL"] = 17] = "DEL";
15
+ Op2[Op2["ATTR"] = 18] = "ATTR";
16
+ Op2[Op2["TEXT"] = 19] = "TEXT";
17
+ Op2[Op2["VAL"] = 20] = "VAL";
18
+ Op2[Op2["OPEN"] = 21] = "OPEN";
19
+ Op2[Op2["SHEET"] = 32] = "SHEET";
20
+ Op2[Op2["RULE"] = 33] = "RULE";
21
+ Op2[Op2["ADOPT"] = 34] = "ADOPT";
22
+ Op2[Op2["TICK"] = 48] = "TICK";
23
+ Op2[Op2["MOVE"] = 49] = "MOVE";
24
+ Op2[Op2["TAP"] = 50] = "TAP";
25
+ Op2[Op2["SCROLL"] = 51] = "SCROLL";
26
+ Op2[Op2["SIZE"] = 52] = "SIZE";
27
+ Op2[Op2["FOCUS"] = 53] = "FOCUS";
28
+ Op2[Op2["HIDE"] = 54] = "HIDE";
29
+ Op2[Op2["META"] = 96] = "META";
30
+ Op2[Op2["PRINT"] = 97] = "PRINT";
31
+ Op2[Op2["END"] = 98] = "END";
32
+ Op2[Op2["SHOT"] = 128] = "SHOT";
33
+ Op2[Op2["ERR"] = 129] = "ERR";
34
+ return Op2;
35
+ })(Op || {});
36
+ var End = /* @__PURE__ */ ((End2) => {
37
+ End2[End2["Consent"] = 1] = "Consent";
38
+ End2[End2["Budget"] = 2] = "Budget";
39
+ End2[End2["Network"] = 3] = "Network";
40
+ return End2;
41
+ })(End || {});
42
+ var ARITY = (() => {
43
+ const a = [];
44
+ const set = (op, n) => {
45
+ a[op] = n;
46
+ };
47
+ set(0 /* X */, 0);
48
+ set(1 /* E */, -1);
49
+ set(2 /* T */, 1);
50
+ set(3 /* D */, 3);
51
+ set(4 /* B */, 3);
52
+ set(5 /* TPL */, 0);
53
+ set(6 /* SHADOW */, 1);
54
+ set(7 /* FRAME */, 0);
55
+ set(8 /* R */, 1);
56
+ set(16 /* INS */, 2);
57
+ set(17 /* DEL */, 1);
58
+ set(18 /* ATTR */, 3);
59
+ set(19 /* TEXT */, 2);
60
+ set(20 /* VAL */, 2);
61
+ set(21 /* OPEN */, 2);
62
+ set(32 /* SHEET */, 3);
63
+ set(33 /* RULE */, 3);
64
+ set(34 /* ADOPT */, -1);
65
+ set(48 /* TICK */, 1);
66
+ set(49 /* MOVE */, 2);
67
+ set(50 /* TAP */, 4);
68
+ set(51 /* SCROLL */, 3);
69
+ set(52 /* SIZE */, 2);
70
+ set(53 /* FOCUS */, 1);
71
+ set(54 /* HIDE */, 1);
72
+ set(96 /* META */, 6);
73
+ set(97 /* PRINT */, 2);
74
+ set(98 /* END */, 1);
75
+ return a;
76
+ })();
77
+ function makesNode(op) {
78
+ return op === 1 /* E */ || op === 2 /* T */ || op === 3 /* D */ || op === 4 /* B */ || op === 5 /* TPL */ || op === 6 /* SHADOW */ || op === 7 /* FRAME */;
79
+ }
80
+ function opens(op) {
81
+ return op === 1 /* E */ || op === 5 /* TPL */ || op === 6 /* SHADOW */ || op === 7 /* FRAME */ || op === 16 /* INS */;
82
+ }
83
+ function length(tape, i) {
84
+ const op = tape[i];
85
+ if (typeof op !== "number") return -1;
86
+ let n;
87
+ if (op >= 128) {
88
+ const framed = tape[i + 1];
89
+ if (!isCount(framed)) return -1;
90
+ n = 2 + framed;
91
+ } else if (op === 1 /* E */) {
92
+ const count = tape[i + 2];
93
+ if (!isCount(count)) return -1;
94
+ n = 3 + 2 * count;
95
+ } else if (op === 34 /* ADOPT */) {
96
+ const count = tape[i + 2];
97
+ if (!isCount(count)) return -1;
98
+ n = 3 + count;
99
+ } else {
100
+ const arity = ARITY[op];
101
+ if (arity === void 0) return -1;
102
+ n = 1 + arity;
103
+ }
104
+ return i + n <= tape.length ? n : -1;
105
+ }
106
+ function isCount(v) {
107
+ return typeof v === "number" && Number.isInteger(v) && v >= 0 && v < 1e6;
108
+ }
109
+ var isNum = (v) => typeof v === "number" && Number.isFinite(v);
110
+ var isId = (v) => typeof v === "number" && Number.isInteger(v) && v > 0;
111
+ var isStr = (v) => typeof v === "string";
112
+ function validate(tape, base) {
113
+ if (!Array.isArray(tape)) return { at: 0, reason: "not an array" };
114
+ let made2 = base === void 0 ? Number.POSITIVE_INFINITY : base - 1;
115
+ let depth = 0;
116
+ for (let i = 0; i < tape.length; ) {
117
+ const n = length(tape, i);
118
+ if (n < 0) return { at: i, reason: `bad op ${String(tape[i])}` };
119
+ const op = tape[i];
120
+ const a = (k) => tape[i + k];
121
+ const known = (v) => isId(v) && v <= made2;
122
+ const bad = (reason) => ({ at: i, reason });
123
+ switch (op) {
124
+ case 0 /* X */:
125
+ if (--depth < 0) return bad("X without open");
126
+ break;
127
+ case 1 /* E */:
128
+ if (!isStr(a(1))) return bad("E name");
129
+ for (let k = 3; k < n; k++) if (!isStr(a(k))) return bad("E attribute");
130
+ break;
131
+ case 2 /* T */:
132
+ if (!isStr(a(1))) return bad("T data");
133
+ break;
134
+ case 3 /* D */:
135
+ if (!isStr(a(1)) || !isStr(a(2)) || !isStr(a(3))) return bad("D");
136
+ break;
137
+ case 4 /* B */:
138
+ if (!isStr(a(1)) || !isNum(a(2)) || !isNum(a(3))) return bad("B");
139
+ break;
140
+ case 6 /* SHADOW */:
141
+ if (!isStr(a(1))) return bad("SHADOW mode");
142
+ break;
143
+ case 8 /* R */:
144
+ case 17 /* DEL */:
145
+ if (!known(a(1))) return bad("unknown node");
146
+ break;
147
+ case 16 /* INS */:
148
+ if (!known(a(1)) || !(a(2) === 0 || known(a(2))))
149
+ return bad("INS nodes");
150
+ break;
151
+ case 18 /* ATTR */:
152
+ if (!known(a(1)) || !isStr(a(2)) || !(a(3) === null || isStr(a(3))))
153
+ return bad("ATTR");
154
+ break;
155
+ case 19 /* TEXT */:
156
+ if (!known(a(1)) || !isStr(a(2))) return bad("TEXT");
157
+ break;
158
+ case 20 /* VAL */:
159
+ if (!known(a(1)) || !(isStr(a(2)) || a(2) === 0 || a(2) === 1))
160
+ return bad("VAL");
161
+ break;
162
+ case 21 /* OPEN */:
163
+ if (!known(a(1)) || !(a(2) === 0 || a(2) === 1 || a(2) === 2))
164
+ return bad("OPEN");
165
+ break;
166
+ case 32 /* SHEET */:
167
+ if (!(a(1) === 0 || known(a(1))) || !isCount(a(2)) || !isStr(a(3)))
168
+ return bad("SHEET");
169
+ break;
170
+ case 33 /* RULE */:
171
+ if (!isCount(a(1)) || !isStr(a(2)) || !(a(3) === null || isStr(a(3))))
172
+ return bad("RULE");
173
+ break;
174
+ case 34 /* ADOPT */:
175
+ if (!known(a(1))) return bad("ADOPT node");
176
+ for (let k = 3; k < n; k++) if (!isCount(a(k))) return bad("ADOPT sheet");
177
+ break;
178
+ case 48 /* TICK */:
179
+ if (!isNum(a(1)) || a(1) < 0) return bad("TICK");
180
+ break;
181
+ case 49 /* MOVE */:
182
+ case 52 /* SIZE */:
183
+ if (!isNum(a(1)) || !isNum(a(2))) return bad("numbers");
184
+ break;
185
+ case 50 /* TAP */:
186
+ if (!isNum(a(1)) || !isNum(a(2)) || !isNum(a(3)) || !isNum(a(4)))
187
+ return bad("TAP");
188
+ break;
189
+ case 51 /* SCROLL */:
190
+ if (!isNum(a(1)) || !isNum(a(2)) || !isNum(a(3))) return bad("SCROLL");
191
+ break;
192
+ case 53 /* FOCUS */:
193
+ case 54 /* HIDE */:
194
+ case 98 /* END */:
195
+ if (!isNum(a(1))) return bad("number");
196
+ break;
197
+ case 97 /* PRINT */:
198
+ if (!isNum(a(1)) || !isNum(a(2))) return bad("PRINT");
199
+ break;
200
+ case 96 /* META */:
201
+ if (a(1) !== VERSION) return bad(`version ${String(a(1))}`);
202
+ if (!isStr(a(2)) || !isNum(a(3)) || !isNum(a(4)) || !isNum(a(5)))
203
+ return bad("META");
204
+ if (depth !== 0) return bad("META inside an open node");
205
+ made2 = 1;
206
+ break;
207
+ }
208
+ if (makesNode(op)) made2++;
209
+ if (opens(op)) depth++;
210
+ i += n;
211
+ }
212
+ if (depth !== 0) return { at: tape.length, reason: "unclosed" };
213
+ return null;
214
+ }
215
+ function made(tape) {
216
+ let nodes = 0;
217
+ let fromMeta = false;
218
+ for (let i = 0; i < tape.length; ) {
219
+ const n = length(tape, i);
220
+ if (n < 0) break;
221
+ const op = tape[i];
222
+ if (op === 96 /* META */) {
223
+ nodes = 1;
224
+ fromMeta = true;
225
+ } else if (makesNode(op)) nodes++;
226
+ i += n;
227
+ }
228
+ return { nodes, fromMeta };
229
+ }
230
+ function span(tape) {
231
+ let start = null;
232
+ let ms = 0;
233
+ for (let i = 0; i < tape.length; ) {
234
+ const n = length(tape, i);
235
+ if (n < 0) break;
236
+ const op = tape[i];
237
+ if (op === 96 /* META */ && start === null) start = tape[i + 3];
238
+ else if (op === 48 /* TICK */) ms += tape[i + 1];
239
+ i += n;
240
+ }
241
+ return { start, ms };
242
+ }
243
+ function resolve(wire, blob, depth = 0) {
244
+ if (depth > 4) throw new Error("replay: pieces nested too deep");
245
+ const out = [];
246
+ for (const token of wire) {
247
+ if (!Array.isArray(token)) {
248
+ out.push(token);
249
+ continue;
250
+ }
251
+ const hash = token[0];
252
+ const stored = typeof hash === "string" ? blob(hash) : void 0;
253
+ if (stored === void 0) throw new MissingBlob(String(hash));
254
+ if (token[1] === 1) {
255
+ const piece = JSON.parse(stored);
256
+ if (!Array.isArray(piece)) throw new Error("replay: a piece is no tape");
257
+ for (const t of resolve(piece, blob, depth + 1)) out.push(t);
258
+ } else out.push(stored);
259
+ }
260
+ return out;
261
+ }
262
+ var MissingBlob = class extends Error {
263
+ constructor(hash) {
264
+ super(`replay: missing ${hash}`);
265
+ this.hash = hash;
266
+ }
267
+ };
268
+ function references(wire) {
269
+ const out = [];
270
+ for (const token of wire)
271
+ if (Array.isArray(token) && typeof token[0] === "string") out.push(token[0]);
272
+ return out;
273
+ }
274
+ function fnv(h, v) {
275
+ return Math.imul(h ^ v & 65535, 16777619) >>> 0;
276
+ }
277
+ var FNV_START = 2166136261;
278
+ function join(chunks, into = [], from = { next: 0, broken: true }) {
279
+ const gaps = [];
280
+ for (const chunk of chunks) {
281
+ const tape = chunk.tape;
282
+ if (validate(tape, tape[0] === 96 /* META */ ? void 0 : chunk.base)) {
283
+ const ms = span(tape).ms;
284
+ gaps.push({ index: into.length, ms });
285
+ if (ms) into.push(48 /* TICK */, ms);
286
+ from.broken = true;
287
+ continue;
288
+ }
289
+ let start = 0;
290
+ const skipped = chunk.seq !== void 0 && from.seq !== void 0 && chunk.seq !== from.seq + 1;
291
+ if (chunk.seq !== void 0) from.seq = chunk.seq;
292
+ if (from.broken || skipped || chunk.base !== from.next) {
293
+ const meta = findMeta(tape);
294
+ if (meta < 0) {
295
+ const ms = span(tape).ms;
296
+ gaps.push({ index: into.length, ms });
297
+ if (ms) into.push(48 /* TICK */, ms);
298
+ from.broken = true;
299
+ continue;
300
+ }
301
+ start = meta;
302
+ }
303
+ const part = start ? tape.slice(start) : tape;
304
+ for (const t of part) into.push(t);
305
+ const m = made(part);
306
+ from.next = m.fromMeta ? m.nodes + 1 : from.next + m.nodes;
307
+ from.broken = false;
308
+ }
309
+ return { tape: into, gaps, state: from };
310
+ }
311
+ function findMeta(tape) {
312
+ for (let i = 0; i < tape.length; ) {
313
+ if (tape[i] === 96 /* META */) return i;
314
+ const n = length(tape, i);
315
+ if (n < 0) return -1;
316
+ i += n;
317
+ }
318
+ return -1;
319
+ }
320
+
321
+ export {
322
+ VERSION,
323
+ Op,
324
+ End,
325
+ makesNode,
326
+ opens,
327
+ length,
328
+ validate,
329
+ made,
330
+ span,
331
+ resolve,
332
+ MissingBlob,
333
+ references,
334
+ fnv,
335
+ FNV_START,
336
+ join
337
+ };
@@ -0,0 +1,23 @@
1
+ import type { Options } from "../record/index";
2
+ type Fn = (d: Document, w: Window & typeof globalThis, wait: (ms: number) => Promise<void>) => unknown;
3
+ export interface Case {
4
+ readonly name: string;
5
+ readonly group: string;
6
+ readonly html: string;
7
+ readonly o?: Pick<Options, "mask">;
8
+ readonly before?: Fn;
9
+ readonly run?: Fn;
10
+ readonly after?: Fn;
11
+ readonly keys?: ReadonlyArray<readonly [string, string]>;
12
+ readonly clicks?: ReadonlyArray<string>;
13
+ readonly select?: ReadonlyArray<readonly [string, string]>;
14
+ /** Strings that must never leave the page, in clear. */
15
+ readonly secrets?: ReadonlyArray<string>;
16
+ /** What the browser must offer for the case to mean anything; skipped otherwise. */
17
+ readonly needs?: {
18
+ readonly what: string;
19
+ readonly test: (w: Window & typeof globalThis) => boolean;
20
+ };
21
+ }
22
+ export declare const CASES: ReadonlyArray<Case>;
23
+ export {};
@@ -0,0 +1,8 @@
1
+ /** A small seeded generator (mulberry32), so a failure replays from its seed. */
2
+ export declare function seeded(seed: number): () => number;
3
+ /** A fictional page with what sites hold: sections, lists, a form, SVG, a template. */
4
+ export declare const FUZZ_PAGE = "<!DOCTYPE html><html lang=\"fr\"><head><meta charset=\"utf-8\"><title>Jardins de Belleroche</title>\n<style>.carte{border:1px solid #ccc}.x{color:#333}</style></head><body>\n<header><nav><a href=\"/\">Accueil</a> <a href=\"/services\">Services</a> <a href=\"/contact\">Contact</a></nav><h1>Jardins de Belleroche</h1></header>\n<main><section id=\"services\"><h2>Nos services</h2><ul><li class=\"carte\">Taille de haies</li><li class=\"carte\">Tonte</li><li class=\"carte\">Cr\u00E9ation de massifs</li></ul></section>\n<section id=\"avis\"><h2>Avis</h2><blockquote>Travail soign\u00E9 <b>et rapide</b>.</blockquote><blockquote data-va-mask>Client : Paul L\u00E9ger</blockquote></section>\n<section id=\"devis\"><h2>Demander un devis</h2><form><label>Nom <input name=\"nom\" value=\"\"></label><label>Message <textarea name=\"message\">Bonjour</textarea></label>\n<select name=\"service\"><option>Taille</option><option selected>Tonte</option></select><label><input type=\"checkbox\" name=\"rappel\"> Me rappeler</label><button>Envoyer</button></form></section>\n<section id=\"carte\"><svg width=\"40\" height=\"40\"><circle cx=\"20\" cy=\"20\" r=\"10\"/></svg><div data-va-block><p>Carte interactive</p></div></section>\n<template id=\"modele\"><li class=\"carte\">Nouveau service</li></template>\n<div id=\"editeur\" contenteditable=\"true\"><p>Votre message</p></div>\n</main><footer><p>\u00A9 Jardins de Belleroche</p></footer></body></html>";
5
+ export declare function mutator(d: Document, rnd: () => number): {
6
+ log: string[];
7
+ round(): Promise<void>;
8
+ };
@@ -0,0 +1,5 @@
1
+ export { CASES, type Case } from "./cases";
2
+ export { compare, type Diff, type OracleOptions } from "./oracle";
3
+ /** The secrets found in what left the page, in clear: each one is a leak. */
4
+ export declare function leaks(sent: ReadonlyArray<string>, secrets: ReadonlyArray<string>): Array<string>;
5
+ export { FUZZ_PAGE, mutator, seeded } from "./fuzz";
@@ -0,0 +1,18 @@
1
+ import type { Mask } from "../record/index";
2
+ export interface OracleOptions {
3
+ readonly mask?: Mask;
4
+ readonly maskAttr?: string;
5
+ readonly blockAttr?: string;
6
+ readonly unmaskAttr?: string;
7
+ readonly max?: number;
8
+ readonly ignoreMode?: boolean;
9
+ }
10
+ export interface Diff {
11
+ readonly path: string;
12
+ readonly what: string;
13
+ readonly live?: string;
14
+ readonly copy?: string;
15
+ /** The two elements around the difference, as HTML, to read a failure. */
16
+ readonly around?: readonly [string, string];
17
+ }
18
+ export declare function compare(live: Document, copy: Document, o?: OracleOptions): Array<Diff>;