@voila.dev/replay 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +50 -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 +243 -0
- package/dist/shared/chunk-6US53Q3A.js +337 -0
- package/dist/testing/cases.d.ts +18 -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 +1197 -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 +338 -0
- package/src/testing/cases.ts +667 -0
- package/src/testing/fuzz.ts +317 -0
- package/src/testing/index.ts +8 -0
- package/src/testing/oracle.ts +307 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// What the core leaves out, as code a site only ships when it needs it. Each module sees nodes
|
|
2
|
+
// already put in a zone by the core, and serializes through `kit.ser`.
|
|
3
|
+
import { Op } from "../format/index";
|
|
4
|
+
import type { Kit, Module, Zone } from "../record/index";
|
|
5
|
+
|
|
6
|
+
/** Shadow roots, closed ones included: `attachShadow` is watched from the start. */
|
|
7
|
+
export function shadow(): Module {
|
|
8
|
+
const closed = new WeakMap<Element, ShadowRoot>();
|
|
9
|
+
let kit: Kit;
|
|
10
|
+
let restore: (() => void) | undefined;
|
|
11
|
+
const rootOf = (el: Element) => kit.native.shadow(el) || closed.get(el) || null;
|
|
12
|
+
return {
|
|
13
|
+
name: "shadow",
|
|
14
|
+
setup(k) {
|
|
15
|
+
kit = k;
|
|
16
|
+
const proto = (k.w as Window & typeof globalThis).Element.prototype;
|
|
17
|
+
const original = proto.attachShadow;
|
|
18
|
+
if (!original) return;
|
|
19
|
+
proto.attachShadow = function (this: Element, init: ShadowRootInit) {
|
|
20
|
+
const root = original.call(this, init);
|
|
21
|
+
if (init.mode === "closed") closed.set(this, root);
|
|
22
|
+
// A root added after the host was recorded: the host is recorded again.
|
|
23
|
+
if (kit.id(this)) kit.redo(this);
|
|
24
|
+
return root;
|
|
25
|
+
};
|
|
26
|
+
restore = () => {
|
|
27
|
+
proto.attachShadow = original;
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
onElement(el: Element, zone: Zone) {
|
|
31
|
+
const root = rootOf(el);
|
|
32
|
+
if (!root) return;
|
|
33
|
+
kit.take(root, el);
|
|
34
|
+
kit.push(Op.SHADOW, root.mode);
|
|
35
|
+
for (let c = kit.native.first(root); c; c = kit.native.next(c)) kit.ser(c, zone);
|
|
36
|
+
kit.push(Op.X);
|
|
37
|
+
kit.observe(root);
|
|
38
|
+
kit.listen(root);
|
|
39
|
+
},
|
|
40
|
+
inner: (n) => (n.nodeType === 1 ? rootOf(n as Element) : null),
|
|
41
|
+
onStop: () => restore?.(),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Iframes of the same origin: their document is recorded inside the page. */
|
|
46
|
+
export function iframes(): Module {
|
|
47
|
+
let kit: Kit;
|
|
48
|
+
const loaded = new WeakSet<Element>();
|
|
49
|
+
const docOf = (el: Element): Document | null => {
|
|
50
|
+
if (kit.native.local(el) !== "iframe") return null;
|
|
51
|
+
try {
|
|
52
|
+
return (el as HTMLIFrameElement).contentDocument;
|
|
53
|
+
} catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
name: "iframes",
|
|
59
|
+
setup(k) {
|
|
60
|
+
kit = k;
|
|
61
|
+
},
|
|
62
|
+
onElement(el: Element, zone: Zone) {
|
|
63
|
+
const doc = kit.native.local(el) === "iframe" ? docOf(el) : null;
|
|
64
|
+
if (kit.native.local(el) === "iframe" && !loaded.has(el)) {
|
|
65
|
+
loaded.add(el);
|
|
66
|
+
// A frame that navigates holds another document: recorded again.
|
|
67
|
+
el.addEventListener("load", () => kit.redo(el));
|
|
68
|
+
}
|
|
69
|
+
if (!doc) return;
|
|
70
|
+
kit.take(doc, el);
|
|
71
|
+
kit.push(Op.FRAME);
|
|
72
|
+
for (let c = kit.native.first(doc); c; c = kit.native.next(c)) kit.ser(c, zone);
|
|
73
|
+
kit.push(Op.X);
|
|
74
|
+
kit.observe(doc);
|
|
75
|
+
kit.listen(doc);
|
|
76
|
+
},
|
|
77
|
+
inner: (n) => (n.nodeType === 1 ? docOf(n as Element) : null),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** 2D canvases, as pictures, at most every `everyMs`. Never in a masked or blocked zone. */
|
|
82
|
+
export function canvas(everyMs = 500, quality = 0.5): Module {
|
|
83
|
+
let kit: Kit;
|
|
84
|
+
const seen = new Set<HTMLCanvasElement>();
|
|
85
|
+
const shown = new WeakMap<HTMLCanvasElement, string>();
|
|
86
|
+
let timer: ReturnType<typeof setInterval> | undefined;
|
|
87
|
+
return {
|
|
88
|
+
name: "canvas",
|
|
89
|
+
setup(k) {
|
|
90
|
+
kit = k;
|
|
91
|
+
timer = setInterval(() => {
|
|
92
|
+
if (seen.size) kit.dirty();
|
|
93
|
+
}, everyMs);
|
|
94
|
+
},
|
|
95
|
+
onElement(el: Element, zone: Zone) {
|
|
96
|
+
if (zone === 0 && kit.native.local(el) === "canvas") seen.add(el as HTMLCanvasElement);
|
|
97
|
+
},
|
|
98
|
+
onSync() {
|
|
99
|
+
seen.forEach((c) => {
|
|
100
|
+
const id = kit.id(c);
|
|
101
|
+
if (!id || !c.isConnected) {
|
|
102
|
+
seen.delete(c);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (!c.width || !c.height || kit.zone(c) !== 0) return;
|
|
106
|
+
let url: string;
|
|
107
|
+
try {
|
|
108
|
+
url = c.toDataURL("image/webp", quality);
|
|
109
|
+
} catch {
|
|
110
|
+
// A canvas that drew something from another origin cannot be read.
|
|
111
|
+
seen.delete(c);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (shown.get(c) === url) return;
|
|
115
|
+
shown.set(c, url);
|
|
116
|
+
kit.push(Op.SHOT, 2, id, url);
|
|
117
|
+
});
|
|
118
|
+
},
|
|
119
|
+
onStop: () => clearInterval(timer),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* What scripts do to stylesheets: `insertRule`, `deleteRule`, `replace`, `replaceSync`, and
|
|
125
|
+
* `adoptedStyleSheets`. A changed sheet is sent again whole, once per read at rest.
|
|
126
|
+
*/
|
|
127
|
+
export function cssom(): Module {
|
|
128
|
+
let kit: Kit;
|
|
129
|
+
const changed = new Set<CSSStyleSheet>();
|
|
130
|
+
const adopting = new Set<Document | ShadowRoot>();
|
|
131
|
+
const restores: Array<() => void> = [];
|
|
132
|
+
|
|
133
|
+
const text = (sheet: CSSStyleSheet) => {
|
|
134
|
+
let css = "";
|
|
135
|
+
for (const rule of Array.from(sheet.cssRules)) css += rule.cssText;
|
|
136
|
+
return css;
|
|
137
|
+
};
|
|
138
|
+
const send = (sheet: CSSStyleSheet) => {
|
|
139
|
+
const node = sheet.ownerNode as Element | null;
|
|
140
|
+
const id = node ? kit.id(node) : 0;
|
|
141
|
+
if (node && !id) return;
|
|
142
|
+
if (node && kit.zone(node) === 2) return;
|
|
143
|
+
try {
|
|
144
|
+
kit.push(Op.SHEET, id || 0, kit.sheet(sheet), text(sheet));
|
|
145
|
+
} catch {
|
|
146
|
+
// Another origin's sheet: the player loads it from its address.
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const adopt = (root: Document | ShadowRoot) => {
|
|
150
|
+
const id = kit.id(root);
|
|
151
|
+
const sheets = root.adoptedStyleSheets;
|
|
152
|
+
if (!id || !sheets) return;
|
|
153
|
+
const numbers: Array<number> = [];
|
|
154
|
+
for (const s of sheets) {
|
|
155
|
+
if (!kit.sheetKnown(s)) send(s);
|
|
156
|
+
numbers.push(kit.sheet(s));
|
|
157
|
+
}
|
|
158
|
+
kit.push(Op.ADOPT, id, numbers.length, ...numbers);
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
name: "cssom",
|
|
163
|
+
setup(k) {
|
|
164
|
+
kit = k;
|
|
165
|
+
const W = k.w as Window & typeof globalThis;
|
|
166
|
+
const proto = W.CSSStyleSheet?.prototype as unknown as Record<string, unknown>;
|
|
167
|
+
if (!proto) return;
|
|
168
|
+
for (const method of ["insertRule", "deleteRule", "replace", "replaceSync"]) {
|
|
169
|
+
const original = proto[method] as ((...a: Array<unknown>) => unknown) | undefined;
|
|
170
|
+
if (typeof original !== "function") continue;
|
|
171
|
+
proto[method] = function (this: CSSStyleSheet, ...args: Array<unknown>) {
|
|
172
|
+
const out = original.apply(this, args);
|
|
173
|
+
const mark = () => {
|
|
174
|
+
changed.add(this);
|
|
175
|
+
kit.dirty();
|
|
176
|
+
};
|
|
177
|
+
if (out && typeof (out as Promise<unknown>).then === "function")
|
|
178
|
+
(out as Promise<unknown>).then(mark, () => {});
|
|
179
|
+
else mark();
|
|
180
|
+
return out;
|
|
181
|
+
};
|
|
182
|
+
restores.push(() => {
|
|
183
|
+
proto[method] = original;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
for (const target of [W.Document?.prototype, W.ShadowRoot?.prototype]) {
|
|
187
|
+
if (!target) continue;
|
|
188
|
+
const d = Object.getOwnPropertyDescriptor(target, "adoptedStyleSheets");
|
|
189
|
+
if (!d?.set || !d.get) continue;
|
|
190
|
+
Object.defineProperty(target, "adoptedStyleSheets", {
|
|
191
|
+
configurable: true,
|
|
192
|
+
enumerable: d.enumerable,
|
|
193
|
+
get: d.get,
|
|
194
|
+
set(this: Document | ShadowRoot, v: Array<CSSStyleSheet>) {
|
|
195
|
+
d.set?.call(this, v);
|
|
196
|
+
adopting.add(this);
|
|
197
|
+
kit.dirty();
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
restores.push(() => Object.defineProperty(target, "adoptedStyleSheets", d));
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
onElement(el: Element) {
|
|
204
|
+
const root = kit.native.shadow(el);
|
|
205
|
+
if (root?.adoptedStyleSheets?.length) adopting.add(root);
|
|
206
|
+
},
|
|
207
|
+
onSnap() {
|
|
208
|
+
if (kit.d.adoptedStyleSheets?.length) adopting.add(kit.d);
|
|
209
|
+
},
|
|
210
|
+
onSync() {
|
|
211
|
+
if (changed.size) {
|
|
212
|
+
for (const s of changed) if (kit.sheetKnown(s) || s.ownerNode) send(s);
|
|
213
|
+
changed.clear();
|
|
214
|
+
}
|
|
215
|
+
if (adopting.size) {
|
|
216
|
+
for (const root of adopting) adopt(root);
|
|
217
|
+
adopting.clear();
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
onStop() {
|
|
221
|
+
for (const f of restores) f();
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
}
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
// The player: a mirror in a sandboxed iframe, a clock, and what a viewer needs around them
|
|
2
|
+
// (speed, idle skipping, seeking back, the visitor's pointer). A chunk whose first node number
|
|
3
|
+
// does not follow the last one is a hole: the player says so and resumes at the next snapshot,
|
|
4
|
+
// rather than replaying a page that is no longer the one recorded.
|
|
5
|
+
import { type Chunk, join, length, Op } from "../format/index";
|
|
6
|
+
import { type Checkpoint, fingerprint, type Mirror, mirror } from "./mirror";
|
|
7
|
+
|
|
8
|
+
export { join } from "../format/index";
|
|
9
|
+
export { fingerprint, mirror } from "./mirror";
|
|
10
|
+
export type { Checkpoint, Mirror, MirrorHooks } from "./mirror";
|
|
11
|
+
|
|
12
|
+
export interface PlayerOptions {
|
|
13
|
+
readonly root: HTMLElement;
|
|
14
|
+
readonly chunks: ReadonlyArray<Chunk>;
|
|
15
|
+
readonly speed?: number;
|
|
16
|
+
readonly skipIdle?: boolean;
|
|
17
|
+
/** Ms of recording between two checkpoints; a seek starts from the nearest one. */
|
|
18
|
+
readonly every?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface Gap {
|
|
22
|
+
/** Ms from the start of the recording. */
|
|
23
|
+
readonly at: number;
|
|
24
|
+
readonly ms: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface Print {
|
|
28
|
+
readonly at: number;
|
|
29
|
+
readonly ok: boolean;
|
|
30
|
+
readonly detail?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type Events = {
|
|
34
|
+
time: number;
|
|
35
|
+
size: { width: number; height: number };
|
|
36
|
+
end: undefined;
|
|
37
|
+
print: Print;
|
|
38
|
+
play: undefined;
|
|
39
|
+
pause: undefined;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export interface Player {
|
|
43
|
+
readonly iframe: HTMLIFrameElement;
|
|
44
|
+
readonly document: Document;
|
|
45
|
+
/** Ms of recording. */
|
|
46
|
+
readonly duration: number;
|
|
47
|
+
/** Ms from the start of the recording. */
|
|
48
|
+
readonly time: number;
|
|
49
|
+
readonly size: { width: number; height: number };
|
|
50
|
+
readonly playing: boolean;
|
|
51
|
+
readonly gaps: ReadonlyArray<Gap>;
|
|
52
|
+
readonly prints: ReadonlyArray<Print>;
|
|
53
|
+
play(): void;
|
|
54
|
+
pause(): void;
|
|
55
|
+
seek(ms: number): void;
|
|
56
|
+
setSpeed(speed: number): void;
|
|
57
|
+
setSkipIdle(on: boolean): void;
|
|
58
|
+
/** Adds chunks that came after the ones given at first. */
|
|
59
|
+
append(chunks: ReadonlyArray<Chunk>): void;
|
|
60
|
+
on<K extends keyof Events>(event: K, handler: (payload: Events[K]) => void): () => void;
|
|
61
|
+
destroy(): void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Idle stretches longer than this are skipped down to a second. */
|
|
65
|
+
const IDLE_MS = 5000;
|
|
66
|
+
/** Past this many checkpoints, one in two goes and they come twice as far apart. */
|
|
67
|
+
const MAX_CHECKPOINTS = 64;
|
|
68
|
+
|
|
69
|
+
export function createPlayer(options: PlayerOptions): Player {
|
|
70
|
+
const { root } = options;
|
|
71
|
+
let speed = options.speed ?? 1;
|
|
72
|
+
let skipIdle = options.skipIdle ?? true;
|
|
73
|
+
let every = Math.max(1, options.every ?? 10_000);
|
|
74
|
+
|
|
75
|
+
const stage = root.ownerDocument.createElement("div");
|
|
76
|
+
stage.style.cssText = "position:relative;overflow:hidden;transform-origin:0 0";
|
|
77
|
+
const iframe = root.ownerDocument.createElement("iframe");
|
|
78
|
+
// No scripts, no forms, no navigation: the copy only shows.
|
|
79
|
+
iframe.setAttribute("sandbox", "allow-same-origin");
|
|
80
|
+
iframe.setAttribute("referrerpolicy", "no-referrer");
|
|
81
|
+
iframe.style.cssText = "border:0;display:block;background:#fff";
|
|
82
|
+
const cursor = root.ownerDocument.createElement("div");
|
|
83
|
+
cursor.style.cssText =
|
|
84
|
+
"position:absolute;left:0;top:0;width:14px;height:14px;margin:-7px 0 0 -7px;border-radius:50%;background:#e5484d;box-shadow:0 0 0 3px #e5484d55;pointer-events:none;transition:transform 60ms linear;display:none";
|
|
85
|
+
stage.append(iframe, cursor);
|
|
86
|
+
root.append(stage);
|
|
87
|
+
const doc = iframe.contentDocument as Document;
|
|
88
|
+
|
|
89
|
+
const listeners: { [K in keyof Events]?: Set<(p: Events[K]) => void> } = {};
|
|
90
|
+
function emit<K extends keyof Events>(event: K, payload: Events[K]) {
|
|
91
|
+
const set = listeners[event] as Set<(p: Events[K]) => void> | undefined;
|
|
92
|
+
set?.forEach((h) => {
|
|
93
|
+
try {
|
|
94
|
+
h(payload);
|
|
95
|
+
} catch {}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
let size = { width: 1280, height: 720 };
|
|
100
|
+
const prints: Array<Print> = [];
|
|
101
|
+
let m: Mirror = make();
|
|
102
|
+
function make() {
|
|
103
|
+
return mirror(doc, {
|
|
104
|
+
pointer(x, y) {
|
|
105
|
+
cursor.style.display = "block";
|
|
106
|
+
cursor.style.transform = `translate(${x}px,${y}px)`;
|
|
107
|
+
},
|
|
108
|
+
tap(kind) {
|
|
109
|
+
if (kind === 1) cursor.style.background = "#30a46c";
|
|
110
|
+
else cursor.style.background = "#e5484d";
|
|
111
|
+
},
|
|
112
|
+
size(width, height) {
|
|
113
|
+
size = { width, height };
|
|
114
|
+
iframe.style.width = stage.style.width = `${width}px`;
|
|
115
|
+
iframe.style.height = stage.style.height = `${height}px`;
|
|
116
|
+
emit("size", size);
|
|
117
|
+
},
|
|
118
|
+
print(ok, at, detail) {
|
|
119
|
+
const p = { ok, at: at - start, detail };
|
|
120
|
+
if (!prints.some((q) => q.at === p.at)) prints.push(p);
|
|
121
|
+
emit("print", p);
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const joined = join(options.chunks);
|
|
127
|
+
const tape = joined.tape;
|
|
128
|
+
let state = joined.state;
|
|
129
|
+
const gapsAt: Array<{ index: number; ms: number }> = [...joined.gaps];
|
|
130
|
+
|
|
131
|
+
// Where each snapshot starts, and the recorded time there: seeking back starts from one.
|
|
132
|
+
let metas: Array<{ index: number; time: number }> = [];
|
|
133
|
+
let start = 0;
|
|
134
|
+
let end = 0;
|
|
135
|
+
function index() {
|
|
136
|
+
metas = [];
|
|
137
|
+
let now = 0;
|
|
138
|
+
for (let i = 0; i < tape.length; ) {
|
|
139
|
+
const n = length(tape, i);
|
|
140
|
+
if (n < 0) break;
|
|
141
|
+
const op = tape[i];
|
|
142
|
+
if (op === Op.META) {
|
|
143
|
+
now = tape[i + 3] as number;
|
|
144
|
+
if (!metas.length) start = now;
|
|
145
|
+
metas.push({ index: i, time: now });
|
|
146
|
+
} else if (op === Op.TICK) now += tape[i + 1] as number;
|
|
147
|
+
i += n;
|
|
148
|
+
}
|
|
149
|
+
end = now;
|
|
150
|
+
}
|
|
151
|
+
index();
|
|
152
|
+
|
|
153
|
+
let at = 0; // where the mirror stopped in the tape
|
|
154
|
+
let clock = 0; // ms from the start, as shown
|
|
155
|
+
let playing = false;
|
|
156
|
+
let frame = 0;
|
|
157
|
+
let lastFrame = 0;
|
|
158
|
+
|
|
159
|
+
// Taken on the way, at the first rest past each step of `every` ms; never ahead of the tape run.
|
|
160
|
+
const checkpoints: Array<{ index: number; time: number; cp: Checkpoint }> = [];
|
|
161
|
+
const after = (t: number) => start + (Math.floor((t - start) / every) + 1) * every;
|
|
162
|
+
|
|
163
|
+
function keep() {
|
|
164
|
+
if (!m.rest || checkpoints.some((c) => c.index === at)) return;
|
|
165
|
+
const c = { index: at, time: m.time, cp: m.save() };
|
|
166
|
+
let k = checkpoints.length;
|
|
167
|
+
while (k > 0 && (checkpoints[k - 1] as { index: number }).index > at) k--;
|
|
168
|
+
checkpoints.splice(k, 0, c);
|
|
169
|
+
if (checkpoints.length > MAX_CHECKPOINTS) {
|
|
170
|
+
every *= 2;
|
|
171
|
+
for (let i = checkpoints.length - 1; i >= 0; i -= 2) checkpoints.splice(i, 1);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function runTo(ms: number) {
|
|
176
|
+
const until = start + ms;
|
|
177
|
+
for (let step = after(Math.max(m.time, start)); step < until; ) {
|
|
178
|
+
at = m.run(tape, at, step);
|
|
179
|
+
if (at >= tape.length || tape[at] !== Op.TICK) break;
|
|
180
|
+
keep();
|
|
181
|
+
step = after(m.time + (tape[at + 1] as number));
|
|
182
|
+
}
|
|
183
|
+
at = m.run(tape, at, until);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function seek(ms: number) {
|
|
187
|
+
ms = Math.max(0, Math.min(ms, end - start));
|
|
188
|
+
// The latest place to start from that is not past `ms`: a snapshot or a checkpoint.
|
|
189
|
+
let from: { index: number; cp?: Checkpoint } | undefined;
|
|
190
|
+
for (const x of metas) if (x.time - start <= ms && (!from || x.index > from.index)) from = x;
|
|
191
|
+
for (const x of checkpoints) if (x.time - start <= ms && (!from || x.index > from.index)) from = x;
|
|
192
|
+
if (from && (ms < clock || at === 0 || from.index > at)) {
|
|
193
|
+
m = make();
|
|
194
|
+
if (from.cp) m.restore(from.cp);
|
|
195
|
+
at = from.index;
|
|
196
|
+
} else if (!from && (ms < clock || at === 0)) return;
|
|
197
|
+
clock = ms;
|
|
198
|
+
runTo(ms);
|
|
199
|
+
emit("time", clock);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function loop(t: number) {
|
|
203
|
+
if (!playing) return;
|
|
204
|
+
const dt = lastFrame ? Math.min(t - lastFrame, 250) : 0;
|
|
205
|
+
lastFrame = t;
|
|
206
|
+
let next = clock + dt * speed;
|
|
207
|
+
if (skipIdle && at < tape.length && tape[at] === Op.TICK) {
|
|
208
|
+
const gap = tape[at + 1] as number;
|
|
209
|
+
if (gap > IDLE_MS) next = Math.max(next, m.time - start + gap - 1000);
|
|
210
|
+
}
|
|
211
|
+
clock = Math.min(next, end - start);
|
|
212
|
+
runTo(clock);
|
|
213
|
+
emit("time", clock);
|
|
214
|
+
if (clock >= end - start) {
|
|
215
|
+
playing = false;
|
|
216
|
+
emit("end", undefined);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
frame = requestAnimationFrame(loop);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
seek(0);
|
|
223
|
+
|
|
224
|
+
return {
|
|
225
|
+
iframe,
|
|
226
|
+
document: doc,
|
|
227
|
+
get duration() {
|
|
228
|
+
return end - start;
|
|
229
|
+
},
|
|
230
|
+
get time() {
|
|
231
|
+
return clock;
|
|
232
|
+
},
|
|
233
|
+
get size() {
|
|
234
|
+
return size;
|
|
235
|
+
},
|
|
236
|
+
get playing() {
|
|
237
|
+
return playing;
|
|
238
|
+
},
|
|
239
|
+
get gaps() {
|
|
240
|
+
const out: Array<Gap> = [];
|
|
241
|
+
let now = 0;
|
|
242
|
+
let g = 0;
|
|
243
|
+
for (let i = 0; i < tape.length && g < gapsAt.length; ) {
|
|
244
|
+
while (g < gapsAt.length && (gapsAt[g] as { index: number }).index <= i) {
|
|
245
|
+
out.push({ at: Math.max(0, now - start), ms: (gapsAt[g] as { ms: number }).ms });
|
|
246
|
+
g++;
|
|
247
|
+
}
|
|
248
|
+
const n = length(tape, i);
|
|
249
|
+
if (n < 0) break;
|
|
250
|
+
if (tape[i] === Op.META) now = tape[i + 3] as number;
|
|
251
|
+
else if (tape[i] === Op.TICK) now += tape[i + 1] as number;
|
|
252
|
+
i += n;
|
|
253
|
+
}
|
|
254
|
+
for (; g < gapsAt.length; g++)
|
|
255
|
+
out.push({ at: Math.max(0, now - start), ms: (gapsAt[g] as { ms: number }).ms });
|
|
256
|
+
return out;
|
|
257
|
+
},
|
|
258
|
+
prints,
|
|
259
|
+
play() {
|
|
260
|
+
if (playing) return;
|
|
261
|
+
if (clock >= end - start) seek(0);
|
|
262
|
+
playing = true;
|
|
263
|
+
lastFrame = 0;
|
|
264
|
+
emit("play", undefined);
|
|
265
|
+
frame = requestAnimationFrame(loop);
|
|
266
|
+
},
|
|
267
|
+
pause() {
|
|
268
|
+
playing = false;
|
|
269
|
+
cancelAnimationFrame(frame);
|
|
270
|
+
emit("pause", undefined);
|
|
271
|
+
},
|
|
272
|
+
seek,
|
|
273
|
+
setSpeed(s) {
|
|
274
|
+
speed = s;
|
|
275
|
+
},
|
|
276
|
+
setSkipIdle(on) {
|
|
277
|
+
skipIdle = on;
|
|
278
|
+
},
|
|
279
|
+
append(chunks) {
|
|
280
|
+
const more = join(chunks, tape, state);
|
|
281
|
+
state = more.state;
|
|
282
|
+
gapsAt.push(...more.gaps);
|
|
283
|
+
index();
|
|
284
|
+
runTo(clock);
|
|
285
|
+
},
|
|
286
|
+
on(event, handler) {
|
|
287
|
+
let set = listeners[event] as Set<typeof handler> | undefined;
|
|
288
|
+
if (!set) listeners[event] = set = new Set() as never;
|
|
289
|
+
set.add(handler);
|
|
290
|
+
return () => set?.delete(handler);
|
|
291
|
+
},
|
|
292
|
+
destroy() {
|
|
293
|
+
playing = false;
|
|
294
|
+
cancelAnimationFrame(frame);
|
|
295
|
+
stage.remove();
|
|
296
|
+
},
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|