castle-web-cli 0.4.212 → 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.js +2 -1
- 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 +35 -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,180 @@
|
|
|
1
|
+
// What an embedded image may contain: only pixels the page itself shows.
|
|
2
|
+
//
|
|
3
|
+
// A cover's images are fetched again at capture time, and a second request
|
|
4
|
+
// can return something else (a per-request image, one the page is still
|
|
5
|
+
// loading, one its CSP blocked, a cache entry another document rewrote). So
|
|
6
|
+
// each fetched image is tied back to the page's own pixels before it is used:
|
|
7
|
+
//
|
|
8
|
+
// - An <img> must have finished loading, and the page's decoded image must be
|
|
9
|
+
// readable (same origin, or loaded with crossorigin under CORS). Its pixels
|
|
10
|
+
// are compared with the fetched copy's; where they differ the page's own
|
|
11
|
+
// pixels are embedded.
|
|
12
|
+
// - A CSS image (a background, say) must have a finished resource-timing
|
|
13
|
+
// entry, and a fresh Image of the same URL must be readable (same origin)
|
|
14
|
+
// and decode to the fetched copy's pixels.
|
|
15
|
+
// - An image whose pixels the page cannot read refuses (IMAGE_NOT_READABLE):
|
|
16
|
+
// workers, frames and other documents can rewrite the shared HTTP cache
|
|
17
|
+
// without a trace in this page, so nothing short of the pixels themselves
|
|
18
|
+
// ties a copy to what the page drew.
|
|
19
|
+
// - A URL the page's CSP img-src refused is never embedded.
|
|
20
|
+
import { parseReportedUrl } from './builtins.js';
|
|
21
|
+
import { fail } from './errors.js';
|
|
22
|
+
import { blockedImageUrls, loadedResources } from './instrument.js';
|
|
23
|
+
import { scratchCanvas } from './scratchCanvas.js';
|
|
24
|
+
import { releaseCanvas } from './verify.js';
|
|
25
|
+
// Past this many pixels an image is not decoded again for the comparison.
|
|
26
|
+
const MAX_COMPARED_PIXELS = 4096 * 4096;
|
|
27
|
+
/** Whether a record of reported URLs (CSP, resource timing) names this one. */
|
|
28
|
+
export function recordNames(record, rawUrl, base) {
|
|
29
|
+
const url = parseReportedUrl(rawUrl, base);
|
|
30
|
+
if (!url)
|
|
31
|
+
return false;
|
|
32
|
+
// A report names the URL without its fragment or credentials, or only its
|
|
33
|
+
// origin when it must not reveal more, or only its scheme ("data", "blob")
|
|
34
|
+
// for a URL with no origin to name.
|
|
35
|
+
return (record[url.reported] === true ||
|
|
36
|
+
record[url.origin] === true ||
|
|
37
|
+
record[url.protocol.slice(0, -1)] === true);
|
|
38
|
+
}
|
|
39
|
+
export function refuseBlockedImage(url, label) {
|
|
40
|
+
if (recordNames(blockedImageUrls, url, document.baseURI)) {
|
|
41
|
+
fail('RESOURCE_BLOCKED_BY_PAGE', `${label}: the page's CSP img-src blocked ${url}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** A CSS image URL the page has not finished loading refuses. */
|
|
45
|
+
export function refuseUnloadedCssImage(url, label) {
|
|
46
|
+
if (!recordNames(loadedResources, url, document.baseURI)) {
|
|
47
|
+
fail('IMAGE_NOT_LOADED', `${label}: the page has not finished loading ${url}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** An <img> that has not finished loading (or failed) refuses. */
|
|
51
|
+
export function refuseUnloadedImg(image, label) {
|
|
52
|
+
if (!image.complete || image.naturalWidth === 0) {
|
|
53
|
+
fail('IMAGE_NOT_LOADED', `${label} has not finished loading`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function scratch(width, height) {
|
|
57
|
+
return scratchCanvas(width, height, 'an image check');
|
|
58
|
+
}
|
|
59
|
+
/** The image's pixels at its natural size, or null when the page may not read them. */
|
|
60
|
+
function readPixels(image, width, height) {
|
|
61
|
+
const { canvas, context } = scratch(width, height);
|
|
62
|
+
try {
|
|
63
|
+
context.drawImage(image, 0, 0, width, height);
|
|
64
|
+
return context.getImageData(0, 0, width, height);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (error instanceof DOMException && error.name === 'SecurityError')
|
|
68
|
+
return null;
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
releaseCanvas(canvas);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function samePixels(a, b) {
|
|
76
|
+
if (a.width !== b.width || a.height !== b.height)
|
|
77
|
+
return false;
|
|
78
|
+
const left = new Uint32Array(a.data.buffer);
|
|
79
|
+
const right = new Uint32Array(b.data.buffer);
|
|
80
|
+
for (let index = 0; index < left.length; index++)
|
|
81
|
+
if (left[index] !== right[index])
|
|
82
|
+
return false;
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
function pngOf(pixels) {
|
|
86
|
+
const { canvas, context } = scratch(pixels.width, pixels.height);
|
|
87
|
+
try {
|
|
88
|
+
context.putImageData(pixels, 0, 0);
|
|
89
|
+
return canvas.toDataURL('image/png');
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
releaseCanvas(canvas);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async function decodeFetched(bytes, mime) {
|
|
96
|
+
try {
|
|
97
|
+
return await createImageBitmap(new Blob([bytes], { type: mime }));
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return fail('RESOURCE_TYPE_MISMATCH', 'a fetched image could not be decoded');
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function loadImage(url) {
|
|
104
|
+
return new Promise((resolve) => {
|
|
105
|
+
const image = new Image();
|
|
106
|
+
image.decoding = 'sync';
|
|
107
|
+
// Cross-origin, only a CORS load can be read to compare; one the server
|
|
108
|
+
// does not allow simply fails to load and refuses.
|
|
109
|
+
if (new URL(url, document.baseURI).origin !== location.origin)
|
|
110
|
+
image.crossOrigin = 'anonymous';
|
|
111
|
+
image.onload = () => resolve(image);
|
|
112
|
+
image.onerror = () => resolve(null);
|
|
113
|
+
image.src = url;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The data: URL to embed for a fetched image, tied to what the page shows.
|
|
118
|
+
* `pageImage` is the page's own <img> when there is one; for a CSS image a
|
|
119
|
+
* fresh Image of the same URL stands in for it.
|
|
120
|
+
*/
|
|
121
|
+
/** Only PNG and JPEG bytes, and no animated PNG (the page shows its frames, the cover one). */
|
|
122
|
+
export function checkImageBytes(bytes, mime, url) {
|
|
123
|
+
const png = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
|
|
124
|
+
const isPng = png.every((byte, index) => bytes[index] === byte);
|
|
125
|
+
const isJpeg = bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff;
|
|
126
|
+
if (mime === 'image/png' ? !isPng : !isJpeg) {
|
|
127
|
+
fail('RESOURCE_TYPE_MISMATCH', `${url}: its bytes are not the ${mime} it is labelled`);
|
|
128
|
+
}
|
|
129
|
+
if (!isPng)
|
|
130
|
+
return;
|
|
131
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
132
|
+
for (let at = 8; at + 8 <= bytes.length;) {
|
|
133
|
+
const length = view.getUint32(at);
|
|
134
|
+
const type = String.fromCharCode(bytes[at + 4], bytes[at + 5], bytes[at + 6], bytes[at + 7]);
|
|
135
|
+
if (type === 'acTL')
|
|
136
|
+
fail('RESOURCE_TYPE_MISMATCH', `${url}: an animated PNG`);
|
|
137
|
+
if (type === 'IDAT' || type === 'IEND')
|
|
138
|
+
return;
|
|
139
|
+
at += 12 + length;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
export async function pageTiedImage(url, fetched, pageImage) {
|
|
143
|
+
// A CSS image has no element of its own to read: its stand-in is another
|
|
144
|
+
// load, which for a response that may not be reused is new bytes, not the
|
|
145
|
+
// ones the page decoded.
|
|
146
|
+
if (!pageImage && fetched.uncacheable) {
|
|
147
|
+
fail('IMAGE_CHANGED', `${url} may not be reused (Cache-Control), so the page's copy cannot be matched`);
|
|
148
|
+
}
|
|
149
|
+
const bitmap = await decodeFetched(fetched.bytes, fetched.mime);
|
|
150
|
+
try {
|
|
151
|
+
const page = pageImage ?? (await loadImage(url));
|
|
152
|
+
if (!page || page.naturalWidth === 0) {
|
|
153
|
+
fail('IMAGE_NOT_LOADED', `${url} does not load in this page`);
|
|
154
|
+
}
|
|
155
|
+
const width = page.naturalWidth;
|
|
156
|
+
const height = page.naturalHeight;
|
|
157
|
+
if (width * height > MAX_COMPARED_PIXELS) {
|
|
158
|
+
fail('RESOURCE_TOO_LARGE', `${url} is too large to check (${width}x${height})`);
|
|
159
|
+
}
|
|
160
|
+
const shown = readPixels(page, width, height);
|
|
161
|
+
if (!shown) {
|
|
162
|
+
fail('IMAGE_NOT_READABLE', `${url}: the page's copy of this image cannot be read to compare (cross-origin without CORS)`);
|
|
163
|
+
}
|
|
164
|
+
if (bitmap.width !== width || bitmap.height !== height) {
|
|
165
|
+
fail('IMAGE_CHANGED', `${url} is ${bitmap.width}x${bitmap.height} now, ${width}x${height} on the page`);
|
|
166
|
+
}
|
|
167
|
+
const copy = readPixels(bitmap, width, height);
|
|
168
|
+
if (copy && samePixels(shown, copy))
|
|
169
|
+
return fetched.dataUrl;
|
|
170
|
+
// The fetched copy is not what the page shows. The page's own <img> is:
|
|
171
|
+
// embed its pixels. A CSS image's stand-in is only another load, so there
|
|
172
|
+
// is nothing certain to embed.
|
|
173
|
+
if (pageImage)
|
|
174
|
+
return pngOf(shown);
|
|
175
|
+
return fail('IMAGE_CHANGED', `${url} returned different pixels than the page shows`);
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
bitmap.close();
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export type ContextKind = '2d' | 'webgl' | 'unsupported';
|
|
2
|
+
export interface TrackedContext {
|
|
3
|
+
kind: ContextKind;
|
|
4
|
+
context: RenderingContext;
|
|
5
|
+
requestedType: string;
|
|
6
|
+
}
|
|
7
|
+
export interface TrackedFont {
|
|
8
|
+
family: string;
|
|
9
|
+
source: unknown;
|
|
10
|
+
descriptors: Record<string, unknown>;
|
|
11
|
+
/** The document's base URL when the face was made: what its URLs resolve against. */
|
|
12
|
+
baseUrl: string;
|
|
13
|
+
}
|
|
14
|
+
export declare const contexts: WeakMap<HTMLCanvasElement, TrackedContext>;
|
|
15
|
+
export declare const canvasRevisions: WeakMap<HTMLCanvasElement, number>;
|
|
16
|
+
export interface CanvasWrite {
|
|
17
|
+
batch: number;
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
}
|
|
21
|
+
export declare const canvasWriteBatch: WeakMap<HTMLCanvasElement, CanvasWrite>;
|
|
22
|
+
export declare const trackedFontFaces: WeakMap<FontFace, TrackedFont>;
|
|
23
|
+
export declare const blockedFontUrls: Record<string, true>;
|
|
24
|
+
export declare const blockedImageUrls: Record<string, true>;
|
|
25
|
+
export declare const loadedResources: Record<string, true>;
|
|
26
|
+
export declare const natives: {
|
|
27
|
+
getContext: {
|
|
28
|
+
(contextId: "2d", options?: CanvasRenderingContext2DSettings): CanvasRenderingContext2D | null;
|
|
29
|
+
(contextId: "bitmaprenderer", options?: ImageBitmapRenderingContextSettings): ImageBitmapRenderingContext | null;
|
|
30
|
+
(contextId: "webgl", options?: WebGLContextAttributes): WebGLRenderingContext | null;
|
|
31
|
+
(contextId: "webgl2", options?: WebGLContextAttributes): WebGL2RenderingContext | null;
|
|
32
|
+
(contextId: string, options?: any): RenderingContext | null;
|
|
33
|
+
};
|
|
34
|
+
requestAnimationFrame: ((callback: FrameRequestCallback) => number) & typeof requestAnimationFrame;
|
|
35
|
+
cancelAnimationFrame: ((handle: number) => void) & typeof cancelAnimationFrame;
|
|
36
|
+
getRandomValues: <T extends ArrayBufferView>(array: T) => T;
|
|
37
|
+
toBlob: (callback: BlobCallback, type?: string, quality?: number) => void;
|
|
38
|
+
};
|
|
39
|
+
export declare function setWriteTracking(on: boolean): void;
|
|
40
|
+
/** The id of the RAF batch running now, or of the last one that ran. */
|
|
41
|
+
export declare function currentBatchId(): number;
|
|
42
|
+
/** Whether the deck has any animation callback waiting for a frame. */
|
|
43
|
+
export declare function hasScheduledAnimationFrame(): boolean;
|
|
44
|
+
/** Called once every callback scheduled for a frame has run. */
|
|
45
|
+
export declare function setBatchEndListener(listener: (() => void) | null): void;
|
|
46
|
+
export declare function freezeAnimationFrames(): void;
|
|
47
|
+
export declare function resumeAnimationFrames(): void;
|
|
48
|
+
/** Installs every hook, once. Must run before any deck code. */
|
|
49
|
+
export declare function installInstrumentation(): void;
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
// The part of the Play cover capture that has to run BEFORE the deck's own
|
|
2
|
+
// code: it classifies every canvas context the deck creates, counts writes to
|
|
3
|
+
// visible canvases, remembers dynamic FontFace sources, and puts a cooperative
|
|
4
|
+
// hold on requestAnimationFrame. None of that can be reconstructed after the
|
|
5
|
+
// fact -- a context created before these hooks is one the capture cannot
|
|
6
|
+
// vouch for, and says so (CANVAS_CONTEXT_UNKNOWN) rather than guessing.
|
|
7
|
+
//
|
|
8
|
+
// Adapted from the reviewed visual-snapshot prototype
|
|
9
|
+
// (castle-sandboxes `scripts/visual-snapshot-prototype/serializer.js`, see its
|
|
10
|
+
// README and `docs/browser-capture-context.md`). One deliberate change: the
|
|
11
|
+
// prototype captured right after the FIRST animation callback following arming;
|
|
12
|
+
// this captures at the end of the whole callback batch for that frame
|
|
13
|
+
// (`onBatchEnd`), because a deck with more than one RAF loop (a physics step
|
|
14
|
+
// and a renderer, say) would otherwise be read between them -- and a WebGL
|
|
15
|
+
// canvas with preserveDrawingBuffer:false read before its renderer ran is an
|
|
16
|
+
// empty buffer.
|
|
17
|
+
import { newRecord, parseReportedUrl, violationFields } from './builtins.js';
|
|
18
|
+
export const contexts = new WeakMap();
|
|
19
|
+
export const canvasRevisions = new WeakMap();
|
|
20
|
+
export const canvasWriteBatch = new WeakMap();
|
|
21
|
+
// WebGL extension objects draw into their context's canvas but carry no
|
|
22
|
+
// `canvas` of their own; getExtension records which one each belongs to.
|
|
23
|
+
const extensionCanvas = new WeakMap();
|
|
24
|
+
export const trackedFontFaces = new WeakMap();
|
|
25
|
+
// Font URLs the page's CSP font-src refused to load. The cover's own fetch is
|
|
26
|
+
// governed by connect-src, and its SVG image by no page CSP at all, so without
|
|
27
|
+
// this a source the page could not use would be embedded and drawn.
|
|
28
|
+
// A null-prototype record (builtins.ts): no Set method a deck could replace.
|
|
29
|
+
export const blockedFontUrls = newRecord();
|
|
30
|
+
// The same for img-src: an image the page could not show is never embedded.
|
|
31
|
+
export const blockedImageUrls = newRecord();
|
|
32
|
+
// Every resource the page has finished loading (resource timing, observed
|
|
33
|
+
// from load with no buffer limit), fragment and credentials removed: a CSS
|
|
34
|
+
// image not named here is one the page may not be showing yet.
|
|
35
|
+
export const loadedResources = newRecord();
|
|
36
|
+
export const natives = {
|
|
37
|
+
// Always invoked through Reflect.apply with the canvas as `this`.
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
39
|
+
getContext: HTMLCanvasElement.prototype.getContext,
|
|
40
|
+
requestAnimationFrame: window.requestAnimationFrame.bind(window),
|
|
41
|
+
cancelAnimationFrame: window.cancelAnimationFrame.bind(window),
|
|
42
|
+
// For the copy's per-capture mark prefix: taken before deck code can replace it.
|
|
43
|
+
getRandomValues: crypto.getRandomValues.bind(crypto),
|
|
44
|
+
// The final PNG: taken here rather than watched, since zone.js's canvas
|
|
45
|
+
// patch replaces the slot. Always invoked through Reflect.apply.
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
47
|
+
toBlob: HTMLCanvasElement.prototype.toBlob,
|
|
48
|
+
};
|
|
49
|
+
let installed = false;
|
|
50
|
+
let rafFrozen = false;
|
|
51
|
+
let nextRafId = 1;
|
|
52
|
+
const rafEntries = new Map();
|
|
53
|
+
let batchTimestamp = null;
|
|
54
|
+
let batchId = 0;
|
|
55
|
+
let batchMembers = new Set();
|
|
56
|
+
let batchEndListener = null;
|
|
57
|
+
// Canvas writes are only counted while a capture holds a session: the
|
|
58
|
+
// wrappers stay installed (a deck may keep references to the methods), but
|
|
59
|
+
// outside a capture each one costs a flag check and nothing else.
|
|
60
|
+
let writeTracking = false;
|
|
61
|
+
export function setWriteTracking(on) {
|
|
62
|
+
writeTracking = on;
|
|
63
|
+
}
|
|
64
|
+
/** The id of the RAF batch running now, or of the last one that ran. */
|
|
65
|
+
export function currentBatchId() {
|
|
66
|
+
return batchId;
|
|
67
|
+
}
|
|
68
|
+
/** Whether the deck has any animation callback waiting for a frame. */
|
|
69
|
+
export function hasScheduledAnimationFrame() {
|
|
70
|
+
return rafEntries.size > 0;
|
|
71
|
+
}
|
|
72
|
+
/** Called once every callback scheduled for a frame has run. */
|
|
73
|
+
export function setBatchEndListener(listener) {
|
|
74
|
+
batchEndListener = listener;
|
|
75
|
+
}
|
|
76
|
+
export function freezeAnimationFrames() {
|
|
77
|
+
rafFrozen = true;
|
|
78
|
+
}
|
|
79
|
+
export function resumeAnimationFrames() {
|
|
80
|
+
rafFrozen = false;
|
|
81
|
+
for (const entry of rafEntries.values()) {
|
|
82
|
+
if (entry.pending) {
|
|
83
|
+
entry.pending = false;
|
|
84
|
+
scheduleRafEntry(entry);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function markCanvasWrite(canvas) {
|
|
89
|
+
if (!(canvas instanceof HTMLCanvasElement))
|
|
90
|
+
return;
|
|
91
|
+
canvasRevisions.set(canvas, (canvasRevisions.get(canvas) ?? 0) + 1);
|
|
92
|
+
canvasWriteBatch.set(canvas, { batch: batchId, width: canvas.width, height: canvas.height });
|
|
93
|
+
}
|
|
94
|
+
const contextCanvas = (self) => self.canvas;
|
|
95
|
+
const extensionOwner = (self) => extensionCanvas.get(self);
|
|
96
|
+
function trackDrawingMethods(prototype, methods, canvasOf = contextCanvas) {
|
|
97
|
+
if (!prototype)
|
|
98
|
+
return;
|
|
99
|
+
const target = prototype;
|
|
100
|
+
for (const name of methods) {
|
|
101
|
+
const original = target[name];
|
|
102
|
+
if (typeof original !== 'function')
|
|
103
|
+
continue;
|
|
104
|
+
target[name] = function trackedDrawingMethod() {
|
|
105
|
+
// `arguments`, not a rest parameter: this runs on every draw call a deck
|
|
106
|
+
// makes, and it should not allocate an array to do it.
|
|
107
|
+
// eslint-disable-next-line prefer-rest-params
|
|
108
|
+
const result = Reflect.apply(original, this, arguments);
|
|
109
|
+
if (writeTracking)
|
|
110
|
+
markCanvasWrite(canvasOf(this));
|
|
111
|
+
return result;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Draw calls that live on extensions rather than the context: instancing for
|
|
116
|
+
// WebGL 1, and the multi-draw / base-instance families.
|
|
117
|
+
const EXTENSION_DRAW_METHODS = [
|
|
118
|
+
'drawArraysInstancedANGLE',
|
|
119
|
+
'drawElementsInstancedANGLE',
|
|
120
|
+
'multiDrawArraysWEBGL',
|
|
121
|
+
'multiDrawElementsWEBGL',
|
|
122
|
+
'multiDrawArraysInstancedWEBGL',
|
|
123
|
+
'multiDrawElementsInstancedWEBGL',
|
|
124
|
+
'drawArraysInstancedBaseInstanceWEBGL',
|
|
125
|
+
'drawElementsInstancedBaseVertexBaseInstanceWEBGL',
|
|
126
|
+
'multiDrawArraysInstancedBaseInstanceWEBGL',
|
|
127
|
+
'multiDrawElementsInstancedBaseVertexBaseInstanceWEBGL',
|
|
128
|
+
];
|
|
129
|
+
const trackedExtensionPrototypes = new WeakSet();
|
|
130
|
+
function trackExtension(extension, canvas) {
|
|
131
|
+
if (!extension || typeof extension !== 'object' || !(canvas instanceof HTMLCanvasElement))
|
|
132
|
+
return;
|
|
133
|
+
extensionCanvas.set(extension, canvas);
|
|
134
|
+
const prototype = Object.getPrototypeOf(extension);
|
|
135
|
+
if (!prototype || trackedExtensionPrototypes.has(prototype))
|
|
136
|
+
return;
|
|
137
|
+
trackedExtensionPrototypes.add(prototype);
|
|
138
|
+
trackDrawingMethods(prototype, EXTENSION_DRAW_METHODS, extensionOwner);
|
|
139
|
+
}
|
|
140
|
+
function trackGetExtension(prototype) {
|
|
141
|
+
if (!prototype || !Object.prototype.hasOwnProperty.call(prototype, 'getExtension'))
|
|
142
|
+
return;
|
|
143
|
+
const target = prototype;
|
|
144
|
+
const original = target.getExtension;
|
|
145
|
+
if (typeof original !== 'function')
|
|
146
|
+
return;
|
|
147
|
+
target.getExtension = function trackedGetExtension(name) {
|
|
148
|
+
const extension = Reflect.apply(original, this, [name]);
|
|
149
|
+
trackExtension(extension, this.canvas);
|
|
150
|
+
return extension;
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function contextKind(requestedType) {
|
|
154
|
+
if (requestedType === '2d')
|
|
155
|
+
return '2d';
|
|
156
|
+
return ['webgl', 'experimental-webgl', 'webgl2'].includes(requestedType)
|
|
157
|
+
? 'webgl'
|
|
158
|
+
: 'unsupported';
|
|
159
|
+
}
|
|
160
|
+
function installCanvasTracking() {
|
|
161
|
+
const getContext = natives.getContext;
|
|
162
|
+
const patched = function patchedGetContext(type, ...args) {
|
|
163
|
+
const context = Reflect.apply(getContext, this, [type, ...args]);
|
|
164
|
+
if (context) {
|
|
165
|
+
const requestedType = String(type).toLowerCase();
|
|
166
|
+
contexts.set(this, { kind: contextKind(requestedType), context, requestedType });
|
|
167
|
+
}
|
|
168
|
+
return context;
|
|
169
|
+
};
|
|
170
|
+
HTMLCanvasElement.prototype.getContext = patched;
|
|
171
|
+
trackDrawingMethods(window.CanvasRenderingContext2D?.prototype, [
|
|
172
|
+
'clearRect',
|
|
173
|
+
'drawImage',
|
|
174
|
+
'fill',
|
|
175
|
+
'fillRect',
|
|
176
|
+
'fillText',
|
|
177
|
+
'putImageData',
|
|
178
|
+
'stroke',
|
|
179
|
+
'strokeRect',
|
|
180
|
+
'strokeText',
|
|
181
|
+
]);
|
|
182
|
+
const webglDrawingMethods = [
|
|
183
|
+
'blitFramebuffer',
|
|
184
|
+
'clear',
|
|
185
|
+
'clearBufferfi',
|
|
186
|
+
'clearBufferfv',
|
|
187
|
+
'clearBufferiv',
|
|
188
|
+
'clearBufferuiv',
|
|
189
|
+
'drawArrays',
|
|
190
|
+
'drawArraysInstanced',
|
|
191
|
+
'drawElements',
|
|
192
|
+
'drawElementsInstanced',
|
|
193
|
+
'drawRangeElements',
|
|
194
|
+
];
|
|
195
|
+
trackDrawingMethods(window.WebGLRenderingContext?.prototype, webglDrawingMethods);
|
|
196
|
+
trackDrawingMethods(window.WebGL2RenderingContext?.prototype, webglDrawingMethods);
|
|
197
|
+
trackGetExtension(window.WebGLRenderingContext?.prototype);
|
|
198
|
+
trackGetExtension(window.WebGL2RenderingContext?.prototype);
|
|
199
|
+
}
|
|
200
|
+
// A dynamic data/BufferSource FontFace is embeddable only because the capture
|
|
201
|
+
// saw its constructor. An unobserved custom DOM font stays an honest rejection.
|
|
202
|
+
// A Proxy with a construct trap, so the constructor keeps its name, still
|
|
203
|
+
// throws when called without `new`, and can still be subclassed.
|
|
204
|
+
function installFontFaceTracking() {
|
|
205
|
+
if (typeof window.FontFace !== 'function')
|
|
206
|
+
return;
|
|
207
|
+
window.FontFace = new Proxy(window.FontFace, {
|
|
208
|
+
construct(target, args, newTarget) {
|
|
209
|
+
const face = Reflect.construct(target, args, newTarget);
|
|
210
|
+
const [family, source, descriptors] = args;
|
|
211
|
+
trackedFontFaces.set(face, {
|
|
212
|
+
family: String(family),
|
|
213
|
+
source,
|
|
214
|
+
descriptors: { ...(descriptors ?? {}) },
|
|
215
|
+
baseUrl: document.baseURI,
|
|
216
|
+
});
|
|
217
|
+
return face;
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
function startBatch(timestamp) {
|
|
222
|
+
batchTimestamp = timestamp;
|
|
223
|
+
batchId++;
|
|
224
|
+
// Everything already handed to the browser runs in this frame; anything
|
|
225
|
+
// requested from here on runs in the next one.
|
|
226
|
+
batchMembers = new Set();
|
|
227
|
+
for (const entry of rafEntries.values())
|
|
228
|
+
if (entry.nativeId !== null)
|
|
229
|
+
batchMembers.add(entry.id);
|
|
230
|
+
}
|
|
231
|
+
function batchRemaining() {
|
|
232
|
+
let remaining = 0;
|
|
233
|
+
for (const id of batchMembers)
|
|
234
|
+
if (typeof rafEntries.get(id)?.nativeId === 'number')
|
|
235
|
+
remaining++;
|
|
236
|
+
return remaining;
|
|
237
|
+
}
|
|
238
|
+
function runRafEntry(entry, timestamp) {
|
|
239
|
+
if (!rafEntries.has(entry.id))
|
|
240
|
+
return;
|
|
241
|
+
if (timestamp !== batchTimestamp)
|
|
242
|
+
startBatch(timestamp);
|
|
243
|
+
entry.nativeId = null;
|
|
244
|
+
batchMembers.delete(entry.id);
|
|
245
|
+
if (rafFrozen) {
|
|
246
|
+
entry.pending = true;
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
rafEntries.delete(entry.id);
|
|
250
|
+
try {
|
|
251
|
+
entry.callback(timestamp);
|
|
252
|
+
}
|
|
253
|
+
finally {
|
|
254
|
+
if (batchRemaining() === 0)
|
|
255
|
+
batchEndListener?.();
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function scheduleRafEntry(entry) {
|
|
259
|
+
entry.nativeId = natives.requestAnimationFrame((timestamp) => runRafEntry(entry, timestamp));
|
|
260
|
+
}
|
|
261
|
+
// Cooperative RAF hold: callbacks requested while frozen are kept and handed
|
|
262
|
+
// to the browser on resume, so a live deck never stays paused.
|
|
263
|
+
function installRafHold() {
|
|
264
|
+
window.requestAnimationFrame = (callback) => {
|
|
265
|
+
if (typeof callback !== 'function') {
|
|
266
|
+
throw new TypeError('requestAnimationFrame callback must be a function');
|
|
267
|
+
}
|
|
268
|
+
const entry = { id: nextRafId++, callback, nativeId: null, pending: false };
|
|
269
|
+
rafEntries.set(entry.id, entry);
|
|
270
|
+
if (rafFrozen)
|
|
271
|
+
entry.pending = true;
|
|
272
|
+
else
|
|
273
|
+
scheduleRafEntry(entry);
|
|
274
|
+
return entry.id;
|
|
275
|
+
};
|
|
276
|
+
window.cancelAnimationFrame = (id) => {
|
|
277
|
+
const entry = rafEntries.get(id);
|
|
278
|
+
if (!entry)
|
|
279
|
+
return;
|
|
280
|
+
rafEntries.delete(id);
|
|
281
|
+
if (entry.nativeId !== null)
|
|
282
|
+
natives.cancelAnimationFrame(entry.nativeId);
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
/** Installs every hook, once. Must run before any deck code. */
|
|
286
|
+
export function installInstrumentation() {
|
|
287
|
+
if (installed)
|
|
288
|
+
return;
|
|
289
|
+
installed = true;
|
|
290
|
+
installCanvasTracking();
|
|
291
|
+
installFontFaceTracking();
|
|
292
|
+
installRafHold();
|
|
293
|
+
installCspTracking();
|
|
294
|
+
installResourceTracking();
|
|
295
|
+
}
|
|
296
|
+
function installResourceTracking() {
|
|
297
|
+
if (typeof PerformanceObserver !== 'function')
|
|
298
|
+
return;
|
|
299
|
+
const observer = new PerformanceObserver((list) => {
|
|
300
|
+
for (const entry of list.getEntries()) {
|
|
301
|
+
if (!(entry instanceof PerformanceResourceTiming) || entry.responseEnd <= 0)
|
|
302
|
+
continue;
|
|
303
|
+
const url = parseReportedUrl(entry.name, document.baseURI);
|
|
304
|
+
if (!url)
|
|
305
|
+
continue;
|
|
306
|
+
loadedResources[url.reported] = true;
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
try {
|
|
310
|
+
observer.observe({ type: 'resource', buffered: true });
|
|
311
|
+
}
|
|
312
|
+
catch {
|
|
313
|
+
// No resource timing here: every CSS image then refuses as not loaded.
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
// Registered on window in the capture phase before any deck script runs, so
|
|
317
|
+
// it hears every violation first; a deck cannot hide one by stopping it.
|
|
318
|
+
function installCspTracking() {
|
|
319
|
+
window.addEventListener('securitypolicyviolation', (event) => {
|
|
320
|
+
if (!event.isTrusted)
|
|
321
|
+
return;
|
|
322
|
+
// Through the getters taken at load, so a deck cannot relabel one.
|
|
323
|
+
const fields = violationFields(event);
|
|
324
|
+
// A report-only policy blocks nothing: the page did load that source.
|
|
325
|
+
if (!fields || fields.disposition !== 'enforce')
|
|
326
|
+
return;
|
|
327
|
+
if (typeof fields.blockedURI !== 'string' || !fields.blockedURI)
|
|
328
|
+
return;
|
|
329
|
+
if (fields.effectiveDirective === 'font-src')
|
|
330
|
+
blockedFontUrls[fields.blockedURI] = true;
|
|
331
|
+
if (fields.effectiveDirective === 'img-src')
|
|
332
|
+
blockedImageUrls[fields.blockedURI] = true;
|
|
333
|
+
}, true);
|
|
334
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const PLAY_COVER_CAPTURE = "castle-play-cover-capture";
|
|
2
|
+
export declare const PLAY_COVER_CANCEL = "castle-play-cover-cancel";
|
|
3
|
+
export declare const PLAY_COVER_RESULT = "castle-play-cover-result";
|
|
4
|
+
export declare const MAX_REQUEST_ID_LENGTH = 64;
|
|
5
|
+
export declare const COVER_TARGET_SELECTOR = "#castle-card, [data-castle-card]";
|
|
6
|
+
export declare const COVER_SCALE = 2;
|
|
7
|
+
export declare const COVER_MAX_EDGE = 1400;
|
|
8
|
+
export declare const MAX_COVER_PNG_BYTES: number;
|
|
9
|
+
export declare const PNG_DATA_URL_PREFIX = "data:image/png;base64,";
|
|
10
|
+
export declare const FRAME_TIMEOUT_MS = 5000;
|
|
11
|
+
export declare const BRIDGE_DEADLINE_MS = 25000;
|
|
12
|
+
export declare const SHELL_CAPTURE_TIMEOUT_MS = 30000;
|
|
13
|
+
/** The sentence a creator sees for a rejection code (`webkit`: the browser showing it is WebKit). */
|
|
14
|
+
export declare function coverFailureSentence(code: string, webkit?: boolean): string;
|