File size: 10,666 Bytes
5b3cd1e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | // A design test's simulated scans, rebuilt in this browser exactly as the server's scanners read them. For each scene:
// its facts (the box where the plain code's frame differs from the design's, and both frames' SHA-256), the design's
// capture JPEG, decoded by this browser and shrunk to the 1280 x 960 grey frame by the whole-number rule, and the
// plain code's patch pasted over a copy of that frame. A frame whose SHA-256 isn't the server's comes again as PNG.
// Pixels are read back through an OffscreenCanvas that is never shown: no frame is shown, sent or kept.
export const CAPTURE = [1920, 1440]; // a scene's capture, as its JPEG comes
export const FRAME = [1280, 960]; // the grey frame every scanner reads: the shrink below is exact for these two sizes
const [CW, CH] = CAPTURE;
const [FW, FH] = FRAME;
const HEX = /^[0-9a-f]{64}$/;
const hex = (buf) => Array.from(new Uint8Array(buf), (b) => b.toString(16).padStart(2, '0')).join('');
/** The SHA-256 of some bytes, in lower-case hex. */
export const sha256Hex = async (bytes) => hex(await crypto.subtle.digest('SHA-256', bytes));
/** An error that says how the browser's part ends: 'stopped', 'slow', 'dropped' or 'aborted'. */
export const ending = (end) => Object.assign(new Error(end), { end });
/** An AbortController that aborts with signal too (each request its own, so one can be aborted alone). */
export function linked(signal) {
const ctl = new AbortController();
if (signal.aborted) ctl.abort();
else signal.addEventListener('abort', () => ctl.abort(), { once: true });
return ctl;
}
/** Wait ms, or reject as 'aborted' once signal aborts. */
export const sleep = (ms, signal) => new Promise((resolve, reject) => {
if (signal.aborted) { reject(ending('aborted')); return; }
const id = setTimeout(resolve, ms);
signal.addEventListener('abort', () => { clearTimeout(id); reject(ending('aborted')); }, { once: true });
});
let away = 0; // ms this page spent frozen before `since`, which the pictures' budget leaves out (get)
let since = null; // when the page was frozen, while it still is
/** test.js, on the page's freeze and resume: whether the page is frozen now (it doesn't run). A page hidden but
* running isn't away: it keeps fetching at the link's own pace. */
export function hidden(now) {
if (now && since === null) since = performance.now();
if (!now && since !== null) {
away += performance.now() - since;
since = null;
}
}
const awayMs = () => away + (since === null ? 0 : performance.now() - since);
const contexts = new Map(); // a 2D context for each full size, reused; a fresh one for any other size
function contextFor(w, h) {
const key = `${w}x${h}`;
if (contexts.has(key)) return contexts.get(key);
const ctx = new OffscreenCanvas(w, h).getContext('2d', { willReadFrequently: true });
if ((w === CW && h === CH) || (w === FW && h === FH)) contexts.set(key, ctx);
return ctx;
}
/** A picture's pixels as one grey byte each, its red channel (every test picture is grey), decoded by this browser:
* createImageBitmap, drawn into an OffscreenCanvas made for frequent reads, read back with getImageData. Throws when
* it doesn't decode, or not at w x h. */
export async function decodeGrey(bytes, type, w, h) {
const bitmap = await createImageBitmap(new Blob([bytes], { type }));
try {
if (bitmap.width !== w || bitmap.height !== h) throw new Error(`decoded at ${bitmap.width} x ${bitmap.height}`);
const ctx = contextFor(w, h);
ctx.clearRect(0, 0, w, h);
ctx.drawImage(bitmap, 0, 0);
const rgba = ctx.getImageData(0, 0, w, h).data;
const grey = new Uint8Array(w * h);
for (let i = 0, j = 0; i < grey.length; i += 1, j += 4) grey[i] = rgba[j];
return grey;
} finally {
bitmap.close();
}
}
/** The whole-number shrink, 1920 x 1440 to 1280 x 960, as the server makes its frames (equal to OpenCV's INTER_AREA
* at this size): on each axis, output i reads source (3i + 1) >> 1 with weight 2/3 and 3 (i >> 1) + 1 with weight
* 1/3, so frame = (4a + 2b + 2c + d + 4) div 9, with a weighted 2/3 both ways and d 1/3 both ways. */
export function shrink(cap) {
const out = new Uint8Array(FW * FH);
for (let y = 0; y < FH; y += 1) {
const r2 = ((3 * y + 1) >> 1) * CW;
const r1 = (3 * (y >> 1) + 1) * CW;
for (let x = 0; x < FW; x += 1) {
const c2 = (3 * x + 1) >> 1;
const c1 = 3 * (x >> 1) + 1;
out[y * FW + x] = ((4 * cap[r2 + c2] + 2 * cap[r2 + c1] + 2 * cap[r1 + c2] + cap[r1 + c1] + 4) / 9) | 0;
}
}
return out;
}
/** A copy of the design's frame with the plain code's patch (h rows of w bytes) written at (x, y): the plain code's
* frame. Throws when the box isn't inside the frame, or the patch isn't the box's size. */
export function paste(base, patch, box) {
const [x, y, w, h] = box;
if (!(x >= 0 && y >= 0 && w > 0 && h > 0 && x + w <= FW && y + h <= FH) || patch.length !== w * h) {
throw new Error('the patch doesn’t fit');
}
const out = new Uint8Array(base);
for (let r = 0; r < h; r += 1) out.set(patch.subarray(r * w, (r + 1) * w), (y + r) * FW + x);
return out;
}
/** An ImageBitmap of grey bytes (w x h) with every alpha 255: what the browser's scanner reads. */
export function toBitmap(grey, w, h) {
const rgba = new Uint8ClampedArray(w * h * 4);
for (let i = 0, j = 0; i < grey.length; i += 1, j += 4) {
rgba[j] = grey[i];
rgba[j + 1] = grey[i];
rgba[j + 2] = grey[i];
rgba[j + 3] = 255;
}
return createImageBitmap(new ImageData(rgba, w, h));
}
/** One of a scene's routes, as JSON or (picture) as bytes. A 503 (the scene isn't rendered yet) is asked again after
* meter.wait and isn't a failure; a 404 means the server no longer has the test; any other failure is tried again up
* to meter.tries in all, then stops the browser's part. A picture counts its bytes and the time it took, from the
* request to its last byte, in the budget (meter.ms; 503s aside), and ends the part as 'slow' when the budget's
* time (meter.limit) runs out while it comes; the time the page spent frozen meanwhile counts in neither.
* A scene's facts that haven't come within meter.hold (longer than the server ever holds the request) count as a
* failed fetch. */
async function get(url, signal, meter, picture) {
for (let fails = 0; ;) {
if (signal.aborted) throw ending('aborted');
const ctl = linked(signal);
let slow = false;
const limit = picture ? Math.max(0, meter.limit - meter.ms) : meter.hold;
const t0 = performance.now();
const a0 = awayMs();
const took = () => performance.now() - t0 - (picture ? awayMs() - a0 : 0);
let timer = 0;
const due = () => { // past the limit by the page's own time; set again for the time it spent away
if (took() < limit) timer = setTimeout(due, limit - took());
else { slow = picture; ctl.abort(); }
};
timer = setTimeout(due, limit);
let res = null;
let body = null;
try {
res = await fetch(url, { cache: 'no-store', signal: ctl.signal });
if (res.ok) body = picture ? new Uint8Array(await res.arrayBuffer()) : await res.json();
} catch { /* a failed fetch, or aborted: below */ }
clearTimeout(timer);
if (picture && res?.status !== 503) meter.ms += took();
if (slow) throw ending('slow');
if (signal.aborted) throw ending('aborted');
if (body) {
if (picture) meter.bytes += body.length;
return body;
}
if (res?.status === 404) throw ending('dropped');
if (res?.status === 503) await sleep(meter.wait, signal);
else if ((fails += 1) >= meter.tries) throw ending('stopped');
}
}
/** The scene's facts: the box (an empty box when the two frames are the same) and both frames' SHA-256. */
function factsOf(j) {
const box = j?.box;
const ok = Array.isArray(box) && box.length === 4 && box.every((v) => Number.isInteger(v) && v >= 0);
if (!ok || !HEX.test(j.sha256?.plain) || !HEX.test(j.sha256?.design)) throw ending('stopped');
return { box, empty: box[2] === 0 || box[3] === 0, plain: j.sha256.plain, design: j.sha256.design };
}
/** Decoded grey bytes, or null when this browser doesn't decode the picture at that size. */
async function grey(bytes, type, w, h, signal) {
let out = null;
try { out = await decodeGrey(bytes, type, w, h); } catch { /* checked as a failed frame */ }
if (signal.aborted) throw ending('aborted');
return out;
}
const same = async (frame, want) => Boolean(frame) && (await sha256Hex(frame)) === want;
/** A frame fetched as PNG (k = 2s for the plain code's, 2s + 1 for the design's), which must pass its check, or the
* browser's part stops. */
async function pngFrame(base, k, want, signal, meter) {
const frame = await grey(await get(`${base}/frame/${k}`, signal, meter, true), 'image/png', FW, FH, signal);
if (!(await same(frame, want))) throw ending('stopped');
return frame;
}
/** One scene's two frames, rebuilt and checked against the server's SHA-256: { plain, design } (1280 x 960 grey bytes
* each) and switched, true when a frame failed its check (the rest of the test then takes PNG frames). mode 'jpeg'
* rebuilds the design's frame from its capture JPEG, and 'png' fetches it as PNG, as it does for a JPEG frame that
* fails. The plain code's frame is its patch over the design's (the design's own for an empty box), and comes as
* PNG when that fails. meter: the budget's count and the fetches' limits (scanner.js: bytes, ms, scenes, limit,
* tries, wait and hold). Throws ending(...) as get() and pngFrame() do. */
export async function fetchScene(base, s, mode, signal, meter) {
const f = factsOf(await get(`${base}/scene/${s}`, signal, meter, false));
let switched = false;
let design = null;
if (mode === 'jpeg') {
const cap = await grey(await get(`${base}/scene/${s}/jpeg`, signal, meter, true), 'image/jpeg', CW, CH, signal);
design = cap && shrink(cap);
if (!(await same(design, f.design))) {
design = null;
switched = true;
}
}
design ||= await pngFrame(base, 2 * s + 1, f.design, signal, meter);
let plain = f.empty && f.plain === f.design ? design : null;
if (!f.empty) {
const [, , w, h] = f.box;
const patch = await grey(await get(`${base}/scene/${s}/patch`, signal, meter, true), 'image/png', w, h, signal);
try { plain = patch && paste(design, patch, f.box); } catch { /* a box outside the frame: checked as failed */ }
if (!(await same(plain, f.plain))) plain = null;
}
if (!plain) {
switched = true;
plain = await pngFrame(base, 2 * s, f.plain, signal, meter);
}
meter.scenes += 1;
return { plain, design, switched };
}
|