castle-web-cli 0.4.211 → 0.4.213
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/cover-bridge/play-cover-bridge.js +2 -0
- package/dist/externalSourcemaps.d.ts +29 -0
- package/dist/externalSourcemaps.js +163 -0
- package/dist/fontFaceImports.d.ts +7 -0
- package/dist/fontFaceImports.js +129 -0
- package/dist/ide.js +7 -2
- package/dist/playCover/bridge.d.ts +6 -0
- package/dist/playCover/bridge.js +159 -0
- package/dist/playCover/builtins.d.ts +29 -0
- package/dist/playCover/builtins.js +189 -0
- package/dist/playCover/capture.d.ts +53 -0
- package/dist/playCover/capture.js +419 -0
- package/dist/playCover/clone.d.ts +40 -0
- package/dist/playCover/clone.js +317 -0
- package/dist/playCover/engine.d.ts +37 -0
- package/dist/playCover/engine.js +76 -0
- package/dist/playCover/errors.d.ts +6 -0
- package/dist/playCover/errors.js +18 -0
- package/dist/playCover/images.d.ts +21 -0
- package/dist/playCover/images.js +180 -0
- package/dist/playCover/instrument.d.ts +49 -0
- package/dist/playCover/instrument.js +334 -0
- package/dist/playCover/protocol.d.ts +14 -0
- package/dist/playCover/protocol.js +139 -0
- package/dist/playCover/rasterize.d.ts +21 -0
- package/dist/playCover/rasterize.js +408 -0
- package/dist/playCover/resources.d.ts +31 -0
- package/dist/playCover/resources.js +823 -0
- package/dist/playCover/scratchCanvas.d.ts +4 -0
- package/dist/playCover/scratchCanvas.js +16 -0
- package/dist/playCover/textSamples.d.ts +36 -0
- package/dist/playCover/textSamples.js +412 -0
- package/dist/playCover/validate.d.ts +79 -0
- package/dist/playCover/validate.js +883 -0
- package/dist/playCover/verify.d.ts +56 -0
- package/dist/playCover/verify.js +578 -0
- package/dist/playCover/verifyRecord.d.ts +71 -0
- package/dist/playCover/verifyRecord.js +243 -0
- package/dist/playCoverGate.d.ts +4 -0
- package/dist/playCoverGate.js +57 -0
- package/dist/playCoverPlacement.d.ts +11 -0
- package/dist/playCoverPlacement.js +110 -0
- package/dist/serve.d.ts +1 -0
- package/dist/serve.js +42 -17
- package/dist/shell/assets/index-BPhS3Ysy.css +1 -0
- package/dist/shell/assets/{index-DzLbNBc7.js → index-CcJiCq02.js} +94 -92
- package/dist/shell/index.html +2 -2
- package/package.json +2 -2
- package/dist/shell/assets/index-BCrgYh76.css +0 -1
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
// One Play cover capture, start to finish: prepare (validate the card, embed
|
|
2
|
+
// its images and fonts, pause CSS animation), wait for the end of the deck's
|
|
3
|
+
// next animation frame, copy the card synchronously right there -- WebGL
|
|
4
|
+
// buffers included, before the browser presents and clears them -- and then
|
|
5
|
+
// release the deck so it runs on.
|
|
6
|
+
//
|
|
7
|
+
// Adapted from the visual-snapshot prototype's serializer (see instrument.ts
|
|
8
|
+
// for provenance and the one deliberate difference: the capture boundary is
|
|
9
|
+
// the end of the frame's whole callback batch). The stale-frame guard, the
|
|
10
|
+
// synchronous WebGL-to-DOM copy and the always-release rule are the
|
|
11
|
+
// prototype's, unchanged in substance.
|
|
12
|
+
import { fail, CoverCaptureError } from './errors.js';
|
|
13
|
+
import { canvasRevisions, canvasWriteBatch, contexts, currentBatchId, freezeAnimationFrames, hasScheduledAnimationFrame, natives, resumeAnimationFrames, setBatchEndListener, setWriteTracking, } from './instrument.js';
|
|
14
|
+
import { preloadResources } from './resources.js';
|
|
15
|
+
import { serializeCard } from './clone.js';
|
|
16
|
+
import { visibleSnapshotElements } from './validate.js';
|
|
17
|
+
import { collectWebKitRefusedEffects } from './verifyRecord.js';
|
|
18
|
+
import { refuseWebKitCapture, refuseWebKitEffects, runningInWebKit } from './engine.js';
|
|
19
|
+
let activeSession = null;
|
|
20
|
+
function visibleCanvases(root) {
|
|
21
|
+
return visibleSnapshotElements(root).filter((element) => element instanceof HTMLCanvasElement);
|
|
22
|
+
}
|
|
23
|
+
// Visible WebGL canvases whose buffer the browser clears once it presents it.
|
|
24
|
+
function nonPersistentWebgl(root) {
|
|
25
|
+
return visibleCanvases(root).filter((canvas) => {
|
|
26
|
+
const tracked = contexts.get(canvas);
|
|
27
|
+
if (tracked?.kind !== 'webgl')
|
|
28
|
+
return false;
|
|
29
|
+
const gl = tracked.context;
|
|
30
|
+
return gl.getContextAttributes()?.preserveDrawingBuffer !== true;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function hashText(hash, text) {
|
|
34
|
+
let next = hash;
|
|
35
|
+
for (let index = 0; index < text.length; index++) {
|
|
36
|
+
next ^= text.charCodeAt(index);
|
|
37
|
+
next = Math.imul(next, 16777619);
|
|
38
|
+
}
|
|
39
|
+
return next >>> 0;
|
|
40
|
+
}
|
|
41
|
+
function frameFingerprint(root) {
|
|
42
|
+
let hash = 2166136261;
|
|
43
|
+
const add = (value) => {
|
|
44
|
+
hash = hashText(hash, String(value));
|
|
45
|
+
};
|
|
46
|
+
for (const element of visibleSnapshotElements(root)) {
|
|
47
|
+
const rect = element.getBoundingClientRect();
|
|
48
|
+
add(element.namespaceURI);
|
|
49
|
+
add(element.localName);
|
|
50
|
+
add(`${rect.x},${rect.y},${rect.width},${rect.height},${element.scrollLeft},${element.scrollTop}`);
|
|
51
|
+
if (element instanceof HTMLInputElement && ['checkbox', 'radio'].includes(element.type))
|
|
52
|
+
add(element.checked);
|
|
53
|
+
for (const pseudo of [null, '::before', '::after']) {
|
|
54
|
+
const style = getComputedStyle(element, pseudo);
|
|
55
|
+
// A pseudo-element with no content generates no box and paints nothing;
|
|
56
|
+
// walking its ~350 properties would only make the guard slower. One that
|
|
57
|
+
// gains content changes `content`, which is still read here.
|
|
58
|
+
if (pseudo && (style.content === 'none' || style.content === 'normal')) {
|
|
59
|
+
add(style.content);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
for (const property of style) {
|
|
63
|
+
add(property);
|
|
64
|
+
add(style.getPropertyValue(property));
|
|
65
|
+
add(style.getPropertyPriority(property));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
add(getComputedStyle(document.body).backgroundColor);
|
|
70
|
+
return hash;
|
|
71
|
+
}
|
|
72
|
+
function startFrameGuard(session) {
|
|
73
|
+
const root = session.source;
|
|
74
|
+
const observer = new MutationObserver((records) => {
|
|
75
|
+
if (session.guard)
|
|
76
|
+
session.guard.mutations += records.length;
|
|
77
|
+
});
|
|
78
|
+
observer.observe(root, { subtree: true, attributes: true, childList: true, characterData: true });
|
|
79
|
+
for (let ancestor = root.parentElement; ancestor; ancestor = ancestor.parentElement) {
|
|
80
|
+
observer.observe(ancestor, { attributes: true });
|
|
81
|
+
}
|
|
82
|
+
observer.observe(document.head, {
|
|
83
|
+
subtree: true,
|
|
84
|
+
attributes: true,
|
|
85
|
+
childList: true,
|
|
86
|
+
characterData: true,
|
|
87
|
+
});
|
|
88
|
+
session.guard = {
|
|
89
|
+
observer,
|
|
90
|
+
mutations: 0,
|
|
91
|
+
fingerprint: frameFingerprint(root),
|
|
92
|
+
canvases: visibleCanvases(root).map((canvas) => ({
|
|
93
|
+
canvas,
|
|
94
|
+
revision: canvasRevisions.get(canvas) ?? 0,
|
|
95
|
+
width: canvas.width,
|
|
96
|
+
height: canvas.height,
|
|
97
|
+
})),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Rejects if anything the copy depends on moved after it was made: a DOM or
|
|
102
|
+
* style mutation, a canvas write or resize, or a change in computed style,
|
|
103
|
+
* geometry or scrolling. Detected, never waited out.
|
|
104
|
+
*/
|
|
105
|
+
export function assertFrameUnchanged(session) {
|
|
106
|
+
const guard = session.guard;
|
|
107
|
+
if (!session.captured || !guard)
|
|
108
|
+
fail('CAPTURE_NOT_COMPLETED', 'the frame guard starts at the capture boundary');
|
|
109
|
+
guard.mutations += guard.observer.takeRecords().length;
|
|
110
|
+
if (guard.mutations !== 0) {
|
|
111
|
+
fail('FRAME_CHANGED_AFTER_CAPTURE', 'observed a DOM/style mutation after the synchronous copy');
|
|
112
|
+
}
|
|
113
|
+
for (const baseline of guard.canvases) {
|
|
114
|
+
if ((canvasRevisions.get(baseline.canvas) ?? 0) !== baseline.revision ||
|
|
115
|
+
baseline.canvas.width !== baseline.width ||
|
|
116
|
+
baseline.canvas.height !== baseline.height) {
|
|
117
|
+
fail('FRAME_CHANGED_AFTER_CAPTURE', 'observed a canvas write or resize after the synchronous copy');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (frameFingerprint(session.source) !== guard.fingerprint) {
|
|
121
|
+
fail('FRAME_CHANGED_AFTER_CAPTURE', 'computed style, geometry, or scrolling changed after the synchronous copy');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function webglPixelsToDataUrl(pixels, width, height) {
|
|
125
|
+
const surface = document.createElement('canvas');
|
|
126
|
+
surface.width = width;
|
|
127
|
+
surface.height = height;
|
|
128
|
+
const context = Reflect.apply(natives.getContext, surface, ['2d']);
|
|
129
|
+
const flipped = new Uint8ClampedArray(pixels.length);
|
|
130
|
+
const stride = width * 4;
|
|
131
|
+
for (let row = 0; row < height; row++) {
|
|
132
|
+
flipped.set(pixels.subarray(row * stride, (row + 1) * stride), (height - row - 1) * stride);
|
|
133
|
+
}
|
|
134
|
+
context.putImageData(new ImageData(flipped, width, height), 0, 0);
|
|
135
|
+
const dataUrl = surface.toDataURL('image/png');
|
|
136
|
+
surface.width = 0;
|
|
137
|
+
surface.height = 0;
|
|
138
|
+
return dataUrl;
|
|
139
|
+
}
|
|
140
|
+
// Reads a WebGL default framebuffer at the end of its render. Only valid for
|
|
141
|
+
// this synchronous turn: a non-persistent buffer is promised for nothing more.
|
|
142
|
+
function readWebglFrame(canvas) {
|
|
143
|
+
const tracked = contexts.get(canvas);
|
|
144
|
+
if (tracked?.kind !== 'webgl')
|
|
145
|
+
fail('WEBGL_CONTEXT_NOT_TRACKED', 'install the capture before creating the WebGL context');
|
|
146
|
+
const gl = tracked.context;
|
|
147
|
+
if (gl.isContextLost())
|
|
148
|
+
fail('WEBGL_CONTEXT_LOST', 'cannot capture a lost context');
|
|
149
|
+
gl.finish();
|
|
150
|
+
if (gl.getParameter(gl.FRAMEBUFFER_BINDING) !== null) {
|
|
151
|
+
fail('WEBGL_NONDEFAULT_FRAMEBUFFER', 'bind the default framebuffer');
|
|
152
|
+
}
|
|
153
|
+
if (tracked.requestedType === 'webgl2') {
|
|
154
|
+
const gl2 = gl;
|
|
155
|
+
if (gl2.getParameter(gl2.READ_FRAMEBUFFER_BINDING) !== null) {
|
|
156
|
+
fail('WEBGL_NONDEFAULT_READ_FRAMEBUFFER', 'bind the default read framebuffer');
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const width = gl.drawingBufferWidth;
|
|
160
|
+
const height = gl.drawingBufferHeight;
|
|
161
|
+
if (!width || !height)
|
|
162
|
+
fail('WEBGL_EMPTY_BUFFER', 'drawing buffer has no pixels');
|
|
163
|
+
const pixels = new Uint8Array(width * height * 4);
|
|
164
|
+
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
|
|
165
|
+
const error = gl.getError();
|
|
166
|
+
if (error !== gl.NO_ERROR)
|
|
167
|
+
fail('WEBGL_READBACK_FAILED', `gl.readPixels error ${error}`);
|
|
168
|
+
for (let alpha = 3; alpha < pixels.length; alpha += 4) {
|
|
169
|
+
if (pixels[alpha] !== 255) {
|
|
170
|
+
fail('WEBGL_TRANSPARENCY_UNSUPPORTED', 'only opaque WebGL default framebuffers are supported');
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const frame = {
|
|
174
|
+
dataUrl: webglPixelsToDataUrl(pixels, width, height),
|
|
175
|
+
width,
|
|
176
|
+
height,
|
|
177
|
+
valid: true,
|
|
178
|
+
};
|
|
179
|
+
queueMicrotask(() => {
|
|
180
|
+
frame.valid = false;
|
|
181
|
+
});
|
|
182
|
+
return frame;
|
|
183
|
+
}
|
|
184
|
+
function readVisibleWebgl(root) {
|
|
185
|
+
const frames = new Map();
|
|
186
|
+
for (const canvas of visibleCanvases(root)) {
|
|
187
|
+
if (contexts.get(canvas)?.kind === 'webgl')
|
|
188
|
+
frames.set(canvas, readWebglFrame(canvas));
|
|
189
|
+
}
|
|
190
|
+
return frames;
|
|
191
|
+
}
|
|
192
|
+
function clearArm(session) {
|
|
193
|
+
session.armed = false;
|
|
194
|
+
if (session.nativeTickId !== undefined)
|
|
195
|
+
natives.cancelAnimationFrame(session.nativeTickId);
|
|
196
|
+
session.nativeTickId = undefined;
|
|
197
|
+
setBatchEndListener(null);
|
|
198
|
+
session.frameEnd?.disconnect();
|
|
199
|
+
session.frameEnd = null;
|
|
200
|
+
if (session.armTimer !== undefined)
|
|
201
|
+
window.clearTimeout(session.armTimer);
|
|
202
|
+
session.armTimer = undefined;
|
|
203
|
+
if (session.fallbackNativeId !== undefined)
|
|
204
|
+
natives.cancelAnimationFrame(session.fallbackNativeId);
|
|
205
|
+
session.fallbackNativeId = undefined;
|
|
206
|
+
}
|
|
207
|
+
function completeCapture(session) {
|
|
208
|
+
const settle = session.settle;
|
|
209
|
+
clearArm(session);
|
|
210
|
+
freezeAnimationFrames();
|
|
211
|
+
try {
|
|
212
|
+
startFrameGuard(session);
|
|
213
|
+
// Deliberately no await between reading the WebGL buffers and the copy.
|
|
214
|
+
const frames = readVisibleWebgl(session.source);
|
|
215
|
+
const { budget, fontCss, fontFamilies } = session.resources;
|
|
216
|
+
const packet = serializeCard(session.source, budget, fontCss, fontFamilies, frames);
|
|
217
|
+
session.captured = true;
|
|
218
|
+
settle?.resolve(packet);
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
settle?.reject(error instanceof Error ? error : new Error(String(error)));
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
// A frame is capturable when every visible non-persistent WebGL canvas was
|
|
225
|
+
// drawn during it -- otherwise that buffer is already presented and cleared.
|
|
226
|
+
// Also: not resized since that draw. Setting width or height clears the
|
|
227
|
+
// buffer, so a deck that resizes its canvas in its own ResizeObserver and draws
|
|
228
|
+
// on the next frame (three.js's setSize pattern) holds an empty buffer at this
|
|
229
|
+
// boundary -- solid black under alpha:false, which would otherwise pass as a
|
|
230
|
+
// picture. Such a frame is skipped, and the next one captured.
|
|
231
|
+
function frameRendered(session) {
|
|
232
|
+
const batch = currentBatchId();
|
|
233
|
+
return nonPersistentWebgl(session.source).every((canvas) => {
|
|
234
|
+
const write = canvasWriteBatch.get(canvas);
|
|
235
|
+
return (write !== undefined &&
|
|
236
|
+
write.batch === batch &&
|
|
237
|
+
write.width === canvas.width &&
|
|
238
|
+
write.height === canvas.height);
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
function onFrameEnd(session) {
|
|
242
|
+
session.frameEnd?.disconnect();
|
|
243
|
+
session.frameEnd = null;
|
|
244
|
+
if (!session.armed)
|
|
245
|
+
return;
|
|
246
|
+
if (!frameRendered(session)) {
|
|
247
|
+
session.batchesWithoutRender++;
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
completeCapture(session);
|
|
251
|
+
}
|
|
252
|
+
// The last animation callback of the frame has returned, but whatever it
|
|
253
|
+
// queued as microtasks -- the rest of an async render loop -- has not run yet.
|
|
254
|
+
// A fresh ResizeObserver's first notification is delivered later in the same
|
|
255
|
+
// rendering update: after every animation callback AND the microtask checkpoint
|
|
256
|
+
// that follows each one, and before the frame is painted and presented, so a
|
|
257
|
+
// non-persistent WebGL buffer still holds what was drawn. That is the capture
|
|
258
|
+
// boundary. (A plain queueMicrotask would run ahead of any render loop more
|
|
259
|
+
// than one await deep.)
|
|
260
|
+
//
|
|
261
|
+
// What the stale-frame guard can still see from here: it is checked in the
|
|
262
|
+
// microtasks right after this callback, with animation frames held, so it
|
|
263
|
+
// catches a microtask queued by the capture's own turn that writes to the card
|
|
264
|
+
// (DOM, style, canvas pixels or size). It cannot see a ResizeObserver the deck
|
|
265
|
+
// registered later in this delivery, a timer, or anything else that runs after
|
|
266
|
+
// that check -- by then the copy is inert data and what the page does next is
|
|
267
|
+
// not part of it.
|
|
268
|
+
function onBatchEnd(session) {
|
|
269
|
+
if (!session.armed || session.frameEnd)
|
|
270
|
+
return;
|
|
271
|
+
const observer = new ResizeObserver(() => onFrameEnd(session));
|
|
272
|
+
session.frameEnd = observer;
|
|
273
|
+
observer.observe(document.documentElement);
|
|
274
|
+
}
|
|
275
|
+
/** Resolves with the copy made at the end of the deck's next frame. */
|
|
276
|
+
export function captureNextFrame(session, timeoutMs) {
|
|
277
|
+
if (session.released)
|
|
278
|
+
fail('CAPTURE_CANCELED', 'the capture was released');
|
|
279
|
+
return new Promise((resolve, reject) => {
|
|
280
|
+
session.settle = { resolve, reject };
|
|
281
|
+
session.armed = true;
|
|
282
|
+
setBatchEndListener(() => onBatchEnd(session));
|
|
283
|
+
if (!hasScheduledAnimationFrame() && nonPersistentWebgl(session.source).length === 0) {
|
|
284
|
+
// Static HTML, Canvas 2D or persistent WebGL with no animation callback
|
|
285
|
+
// to follow: one native paint boundary, then the stale-frame guard. Not
|
|
286
|
+
// a claim that the deck finished rendering anything.
|
|
287
|
+
session.fallbackNativeId = natives.requestAnimationFrame(() => {
|
|
288
|
+
session.fallbackNativeId = undefined;
|
|
289
|
+
if (session.armed)
|
|
290
|
+
completeCapture(session);
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
countNativeTicks(session);
|
|
294
|
+
session.armTimer = window.setTimeout(() => {
|
|
295
|
+
clearArm(session);
|
|
296
|
+
reject(armTimeoutError(session, timeoutMs));
|
|
297
|
+
}, timeoutMs);
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
// Browsers stop animation frames for a frame that is hidden or scrolled out of
|
|
301
|
+
// view (Safari does it to an offscreen iframe). Counting the browser's own
|
|
302
|
+
// frames while armed is how a timeout tells "the browser paused this deck" from
|
|
303
|
+
// "the deck drew nothing".
|
|
304
|
+
function countNativeTicks(session) {
|
|
305
|
+
const tick = () => {
|
|
306
|
+
session.nativeTicks++;
|
|
307
|
+
if (session.armed)
|
|
308
|
+
session.nativeTickId = natives.requestAnimationFrame(tick);
|
|
309
|
+
};
|
|
310
|
+
session.nativeTickId = natives.requestAnimationFrame(tick);
|
|
311
|
+
}
|
|
312
|
+
function notVisible(detail) {
|
|
313
|
+
return new CoverCaptureError('FRAME_NOT_VISIBLE', `${detail} (visibilityState=${document.visibilityState})`);
|
|
314
|
+
}
|
|
315
|
+
function armTimeoutError(session, timeoutMs) {
|
|
316
|
+
if (session.nativeTicks === 0)
|
|
317
|
+
return notVisible(`the browser ran no animation frames in ${timeoutMs} ms`);
|
|
318
|
+
if (session.batchesWithoutRender > 0) {
|
|
319
|
+
return new CoverCaptureError('WEBGL_NOT_RENDERED_THIS_FRAME', 'no frame drew every visible WebGL canvas');
|
|
320
|
+
}
|
|
321
|
+
return new CoverCaptureError('APPLICATION_FRAME_TIMEOUT', `the browser ran ${session.nativeTicks} frames but the deck completed none`);
|
|
322
|
+
}
|
|
323
|
+
const PAUSE_PAINT_TIMEOUT_MS = 3_000;
|
|
324
|
+
function waitNativeFrames() {
|
|
325
|
+
return new Promise((resolve, reject) => {
|
|
326
|
+
const timer = window.setTimeout(() => reject(notVisible(`the browser ran no animation frames in ${PAUSE_PAINT_TIMEOUT_MS} ms`)), PAUSE_PAINT_TIMEOUT_MS);
|
|
327
|
+
natives.requestAnimationFrame(() => natives.requestAnimationFrame(() => {
|
|
328
|
+
window.clearTimeout(timer);
|
|
329
|
+
resolve();
|
|
330
|
+
}));
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Validates the card and embeds its resources, then pauses CSS animation and
|
|
335
|
+
* gives the pause two frames to paint. The returned session holds the deck's
|
|
336
|
+
* animation until `releaseCapture`, which every caller must reach.
|
|
337
|
+
*/
|
|
338
|
+
export async function prepareCapture(source, signal, onSession) {
|
|
339
|
+
if (activeSession)
|
|
340
|
+
fail('CAPTURE_BUSY', 'release the previous capture first');
|
|
341
|
+
if (document.visibilityState === 'hidden')
|
|
342
|
+
throw notVisible('the deck page is hidden');
|
|
343
|
+
// WebKit draws some shadows wrong in ways the check cannot see, so it is
|
|
344
|
+
// not trusted with any: refused before anything is fetched or frozen.
|
|
345
|
+
refuseWebKitCapture();
|
|
346
|
+
if (runningInWebKit())
|
|
347
|
+
refuseWebKitEffects(collectWebKitRefusedEffects(source));
|
|
348
|
+
if (document.compatMode !== 'CSS1Compat') {
|
|
349
|
+
fail('QUIRKS_MODE_DOCUMENT', 'the deck document renders in quirks mode');
|
|
350
|
+
}
|
|
351
|
+
const resources = await preloadResources(source, signal);
|
|
352
|
+
if (signal.aborted)
|
|
353
|
+
fail('CAPTURE_CANCELED', 'the capture was canceled');
|
|
354
|
+
if (activeSession)
|
|
355
|
+
fail('CAPTURE_BUSY', 'release the previous capture first');
|
|
356
|
+
const freeze = document.createElement('style');
|
|
357
|
+
freeze.setAttribute('data-castle-cover-freeze', '');
|
|
358
|
+
freeze.textContent =
|
|
359
|
+
'*,*::before,*::after{animation-play-state:paused!important;transition:none!important;caret-color:transparent!important}';
|
|
360
|
+
document.head.append(freeze);
|
|
361
|
+
const session = {
|
|
362
|
+
source,
|
|
363
|
+
resources,
|
|
364
|
+
freeze,
|
|
365
|
+
released: false,
|
|
366
|
+
armed: false,
|
|
367
|
+
captured: false,
|
|
368
|
+
batchesWithoutRender: 0,
|
|
369
|
+
nativeTicks: 0,
|
|
370
|
+
nativeTickId: undefined,
|
|
371
|
+
frameEnd: null,
|
|
372
|
+
guard: null,
|
|
373
|
+
armTimer: undefined,
|
|
374
|
+
fallbackNativeId: undefined,
|
|
375
|
+
settle: null,
|
|
376
|
+
};
|
|
377
|
+
activeSession = session;
|
|
378
|
+
setWriteTracking(true);
|
|
379
|
+
// Handed over the moment it holds the freeze, so its owner can release it
|
|
380
|
+
// even while this is still waiting for the pause to paint.
|
|
381
|
+
onSession?.(session);
|
|
382
|
+
try {
|
|
383
|
+
await waitNativeFrames();
|
|
384
|
+
}
|
|
385
|
+
catch (error) {
|
|
386
|
+
releaseCapture(session);
|
|
387
|
+
throw error;
|
|
388
|
+
}
|
|
389
|
+
if (session.released || signal.aborted) {
|
|
390
|
+
releaseCapture(session);
|
|
391
|
+
fail('CAPTURE_CANCELED', 'the capture was canceled');
|
|
392
|
+
}
|
|
393
|
+
return session;
|
|
394
|
+
}
|
|
395
|
+
/** Undoes everything a session holds. Safe to call more than once. */
|
|
396
|
+
export function releaseCapture(session) {
|
|
397
|
+
// A second release is a no-op, and must be: by then another capture may own
|
|
398
|
+
// the batch listener, the write tracking and the RAF hold.
|
|
399
|
+
if (session.released)
|
|
400
|
+
return;
|
|
401
|
+
const wasArmed = session.armed;
|
|
402
|
+
const settle = session.settle;
|
|
403
|
+
clearArm(session);
|
|
404
|
+
session.released = true;
|
|
405
|
+
session.guard?.observer.disconnect();
|
|
406
|
+
session.freeze.remove();
|
|
407
|
+
if (activeSession === session) {
|
|
408
|
+
activeSession = null;
|
|
409
|
+
setWriteTracking(false);
|
|
410
|
+
}
|
|
411
|
+
resumeAnimationFrames();
|
|
412
|
+
if (wasArmed)
|
|
413
|
+
settle?.reject(new CoverCaptureError('CAPTURE_CANCELED', 'the capture was released before a frame completed'));
|
|
414
|
+
}
|
|
415
|
+
/** Releases whatever capture is in progress (cancel, timeout, pagehide). */
|
|
416
|
+
export function releaseActiveCapture() {
|
|
417
|
+
if (activeSession)
|
|
418
|
+
releaseCapture(activeSession);
|
|
419
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type ResourceBudget } from './resources.js';
|
|
2
|
+
import { type Effect, type EffectClips, type EmbeddedBitmap, type TextSample } from './verifyRecord.js';
|
|
3
|
+
export interface WebglFrame {
|
|
4
|
+
dataUrl: string;
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
valid: boolean;
|
|
8
|
+
}
|
|
9
|
+
/** The inert copy: everything the rasterizer needs, nothing it could fetch. */
|
|
10
|
+
export interface CoverPacket {
|
|
11
|
+
version: number;
|
|
12
|
+
width: number;
|
|
13
|
+
height: number;
|
|
14
|
+
backdropColor: string;
|
|
15
|
+
html: string;
|
|
16
|
+
fontCss: string;
|
|
17
|
+
byteLength: number;
|
|
18
|
+
/** Bitmaps whose painted pixels the rasterizer checks (verifyRecord.ts). */
|
|
19
|
+
bitmaps: EmbeddedBitmap[];
|
|
20
|
+
/** Bitmaps painted in a way the check cannot predict. */
|
|
21
|
+
unverifiedBitmaps: number;
|
|
22
|
+
/** Text drawn in each embedded web font, for the rasterizer's font check. */
|
|
23
|
+
textSamples: TextSample[];
|
|
24
|
+
/** Blur-type effects the card uses, each probed by the rasterizer's check. */
|
|
25
|
+
effects: Effect[];
|
|
26
|
+
/** Everything in the captured card WebKit is refused (engine.ts). */
|
|
27
|
+
webkitEffects: string[];
|
|
28
|
+
/**
|
|
29
|
+
* The attribute prefix this capture marks its own elements with in the copy
|
|
30
|
+
* (a random `data-cover-<nonce>`); a deck's own data-cover-* attributes are
|
|
31
|
+
* stripped from the copy besides.
|
|
32
|
+
*/
|
|
33
|
+
marker: string;
|
|
34
|
+
/** How many of the deck's own data-cover-* attributes were left out of the copy. */
|
|
35
|
+
strippedMarks: number;
|
|
36
|
+
/** Where those effects are clipped, for the rasterizer's unclipped redraw. */
|
|
37
|
+
effectClips: EffectClips;
|
|
38
|
+
}
|
|
39
|
+
/** Copies `source` into an inert packet. Synchronous by contract. */
|
|
40
|
+
export declare function serializeCard(source: Element, budget: ResourceBudget, fontCss: string, fontFamilies: string[], webglFrames: Map<HTMLCanvasElement, WebglFrame>): CoverPacket;
|