File size: 13,762 Bytes
5b3cd1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
811f3a9
 
 
 
 
 
5b3cd1e
 
 
 
 
 
 
 
 
811f3a9
 
5b3cd1e
 
811f3a9
 
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// This browser's own scanner in a design test. Whether the browser has one the test can use is found once a page
// session, sending nothing (the probe and its working check), and it is named by platform. In a test it reads the
// scenes' rebuilt frames (frames.js), the plain code's and the design's, as they arrive, and stops once no reads of
// the scenes left could change its answer (the exact early stop), or when the scans come too slowly for the budget.
// The rule every scanner is judged by is here too, as the server has it. What this scanner reads stays in the page.
import { CAPTURE, FRAME, decodeGrey, ending, fetchScene, linked, sha256Hex, toBitmap } from './frames.js';

let T = null; // web/data/test.json (useTest)
let C = null; // binomial coefficients C[n][k], for n up to the test's scenes
const encoder = new TextEncoder();

/** test.js, before useTest: what the code relies on in test.json: whole numbers where it counts, the frame sizes the
 *  shrink is exact for, and a sign test whose alpha is one over a whole number. */
export function fits(s) {
  const whole = (...xs) => xs.every((x) => Number.isInteger(x) && x >= 0);
  const pair = (a, b) => Array.isArray(a) && a[0] === b[0] && a[1] === b[1];
  const inv = 1 / s.rule.alpha;
  return typeof s.page === 'boolean' && whole(s.scenes.s, s.rule.like_cost, s.rule.q, s.join, s.limits.max_version)
    && s.scenes.s >= 1 && s.scenes.s <= 20 && Math.abs(inv - Math.round(inv)) < 1e-9 && pair(s.frames.capture, CAPTURE)
    && pair(s.frames.frame, FRAME) && Boolean(s.check.png && s.check.jpeg && s.mb.jpeg && s.mb.png && s.figures);
}

/** test.js, once test.json is in: the settings everything here reads. */
export function useTest(settings) {
  T = settings;
  C = [[1]];
  for (let n = 1; n <= T.scenes.s; n += 1) C.push(C[n - 1].map((v, k) => v + (C[n - 1][k - 1] || 0)).concat(1));
}

// ---- the rule, per scanner, over the paired scenes -----------------------------------------------------------------

/** The one-sided exact sign test on (b, c) under the rule's alpha, in whole numbers: sum over k = b..n of C(n, k),
 *  times 1 / alpha, under 2^n (n = b + c), so this and the server's never round differently. */
const under = (b, c) => Math.round(1 / T.rule.alpha) * C[b + c].slice(b).reduce((sum, v) => sum + v, 0) < 2 ** (b + c);

/** One scanner's answer: b counts the scenes where it read the plain code and missed the design, c the reverse, t
 *  and d the scenes where it read the plain code and the design. 'worse' when the sign test confirms the loss; 'like'
 *  (as well as a plain code) with a cost b - c of at most like_cost, and t or d at least q; otherwise it can't tell:
 *  'close' with a higher cost, else 'few'. */
export function answer(b, c, t, d) {
  const { like_cost: cost, q } = T.rule;
  if (under(b, c)) return 'worse';
  if (b - c <= cost && (t >= q || d >= q)) return 'like';
  return b - c > cost ? 'close' : 'few';
}

/** The answer once no reads of the r scenes left could change it, else null: the exact early stop in closed form
 *  (equal to trying every way the scenes left could read). At r = 0 it is the answer itself. */
export function settled(b, c, t, d, r) {
  const { like_cost: cost, q } = T.rule;
  if (r === 0) return answer(b, c, t, d);
  if (under(b, c + r)) return 'worse';
  if ((t >= q || d >= q) && b - c + r <= cost) return 'like';
  if (b - c - r > cost && !under(b + r, c)) return 'close';
  if (b - c + r <= cost && t + r < q && d + r < q) return 'few';
  return null;
}

// ---- the probe: once a page session, sending nothing ----------------------------------------------------------------

let probing = null;
let found = { state: 'pending' };

/** Whether this browser has a scanner the test can use: { state: 'ready', name: 'apple' | 'google', frames: 'jpeg' |
 *  'png' }, 'none' with the step that failed (1: no BarcodeDetector here, 2: not Google Chrome on macOS or Android,
 *  3: it lists no QR codes), 'broken' (the working check's read failed) or 'blocked' (this browser's canvas reads are
 *  altered or blocked). Run once a page session, when a picture is first marked The original graphic. */
export function probe() {
  probing ||= steps().catch(() => ({ state: 'none', step: 1 })).then((r) => { found = r; return r; });
  return probing;
}

/** The probe's result so far: { state: 'pending' } until it has one. */
export const probed = () => found;

async function steps() {
  if (!(window.isSecureContext && 'BarcodeDetector' in window)) return { state: 'none', step: 1 };
  if (!platformName()) return { state: 'none', step: 2 };
  let formats = [];
  try { formats = await BarcodeDetector.getSupportedFormats(); } catch { /* none listed */ }
  if (!Array.isArray(formats) || !formats.includes('qr_code')) return { state: 'none', step: 3 };
  return check();
}

/** After 'broken', a test's start runs the working check again (local, with the same two pictures), so the next test
 *  or Try again can bring the scanner back; any other result stands for the page session. */
export async function recheck() {
  if (found.state === 'broken') found = await check();
  return found;
}

/** 'apple' or 'google' for Google Chrome on macOS or on Android, from userAgentData's brands and platform (never its
 *  high-entropy hints, which the Hub's iframe may not pass), else ''. */
function platformName() {
  const ua = navigator.userAgentData;
  const { brand, platforms } = T.browsers;
  const chrome = Array.isArray(ua?.brands) && ua.brands.some((b) => b.brand === brand);
  return chrome && Object.hasOwn(platforms, ua.platform) ? platforms[ua.platform] : '';
}

/** Which words fit this browser: 'chrome-mac', 'chrome-android', 'mac' (a Mac, not Chrome: "Macintosh" with no touch
 *  points, so an iPad isn't taken for one) or 'other'. It only chooses words. */
export function platformKind() {
  if (platformName()) return navigator.userAgentData.platform === 'Android' ? 'chrome-android' : 'chrome-mac';
  return /Macintosh/.test(navigator.userAgent) && navigator.maxTouchPoints === 0 ? 'mac' : 'other';
}

/** Whether this browser's scanner can read a test's code, given whether its two truths differ (differ: the SHA-256 of
 *  its raw bytes, then of its text in UTF-8, from the 202, or before it where the first reading shows it, test_turn.js
 *  rawNotText): in Chrome on Android only when they don't, raw bytes that are UTF-8 text; elsewhere always. Chrome 113
 *  on Android crashed its GPU process on every other code measured (detect rejected with NotSupportedError, each
 *  time), so trying again can't help there. */
export const readsContent = (differ) => platformKind() !== 'chrome-android' || !differ;

/** Whether this browser asks to save data: read at every test's start, so turning it off and Try again bring the
 *  scanner in. */
export const saveDataOn = () => navigator.connection?.saveData === true;

/** What this browser's scanner does in a test (rec: test.js's record; joins: tests it joined by itself so far this
 *  page session), from the probe's result: { decision, name, frames }. It joins (with its frames: 'jpeg', or 'png'
 *  when this browser decodes JPEG differently); is offered, once a page session's first {join} tests have taken it
 *  by themselves, unless Add was pressed or this test's first decision was to join; is kept out by Save-Data; or
 *  isn't in the test: 'late' (the probe not done in time), 'broken', 'none' or 'blocked'. A code the record already
 *  knows this scanner can't read (readsContent) gives 'none' from the start, whatever the probe found. */
export function partFor(rec, joins, pr = found) {
  const part = { name: pr.name || platformName(), frames: pr.frames || 'jpeg' };
  if (!readsContent(rec.truthsDiffer)) part.decision = 'none';
  else if (pr.state !== 'ready') part.decision = pr.state === 'pending' ? 'late' : pr.state;
  else if (saveDataOn()) part.decision = 'save_data';
  else part.decision = rec.added || rec.first === 'joins' || joins < T.join ? 'joins' : 'offered';
  return part;
}

/** At a test's start: the probe, waited for at most probe_wait_s, and after 'broken' the working check again. */
export async function decide(rec, joins) {
  if (found.state === 'pending') await Promise.race([probe(), wait(T.page_waits.probe_wait_s * 1000)]);
  if (found.state === 'broken') await recheck();
  return partFor(rec, joins);
}

const bytesOf = (b64) => Uint8Array.from(atob(b64), (ch) => ch.charCodeAt(0));
const wait = (ms) => new Promise((resolve) => { setTimeout(resolve, ms); });

/** The working check, on test.json's two pictures, decoded along the frames' own path: the plain code's PNG must
 *  give its pinned grey bytes (else 'blocked': this browser's canvas reads are altered or blocked, as a readback that
 *  throws is), and a new detector must read its known content (else 'broken'); either is tried once more after
 *  check_retry_s. A JPEG that decodes to other bytes keeps the scanner, with PNG frames from the start. */
async function check() {
  let got = await checkOnce();
  if (got.state !== 'ready') {
    await wait(T.page_waits.check_retry_s * 1000);
    got = await checkOnce();
  }
  return got;
}

async function checkOnce() {
  const { png, jpeg } = T.check;
  let code;
  let frames = 'jpeg';
  try {
    code = await decodeGrey(bytesOf(png.b64), 'image/png', png.w, png.h);
    if ((await sha256Hex(code)) !== png.grey_sha256) return { state: 'blocked' };
    const scene = await decodeGrey(bytesOf(jpeg.b64), 'image/jpeg', jpeg.w, jpeg.h);
    if ((await sha256Hex(scene)) !== jpeg.grey_sha256) frames = 'png';
  } catch {
    return { state: 'blocked' };
  }
  try {
    const detector = new BarcodeDetector({ formats: ['qr_code'] });
    if (!(await reads(detector, code, png.w, png.h, [png.content_sha256]))) return { state: 'broken' };
  } catch {
    return { state: 'broken' };
  }
  return { state: 'ready', name: platformName(), frames };
}

/** Whether the detector reads one of truths (the content's UTF-8 SHA-256s) from grey bytes, through an ImageBitmap
 *  of them; throws when detect throws, or takes over detect_s. */
async function reads(detector, grey, w, h, truths) {
  const bitmap = await toBitmap(grey, w, h);
  const detecting = detector.detect(bitmap);
  detecting.then(() => bitmap.close(), () => bitmap.close());
  let timer = 0;
  const late = new Promise((resolve, reject) => {
    timer = setTimeout(() => reject(new Error('detect')), T.budget.detect_s * 1000);
  });
  try {
    const codes = await Promise.race([detecting, late]);
    for (const code of codes) if (truths.includes(await sha256Hex(encoder.encode(code.rawValue)))) return true;
    return false;
  } finally {
    clearTimeout(timer);
  }
}

// ---- the browser's part of one test --------------------------------------------------------------------------------

/** The browser's part of a test. opts: base (the test's routes), truths, mode ('jpeg' or 'png': the probe's frames),
 *  signal (aborted when the page leaves the test, or Leave it out is pressed), onScene(k) after each whole scene, and
 *  onMode('png') once the rest of the test takes PNG frames. One detector reads every frame; one scene is in hand and
 *  the next in flight. The budget counts only the pictures' fetches: after project_after_scenes scenes, and after
 *  each one from then on, the part stops when the time they took, projected over every scene from the link's own
 *  pace, passes fetch_s (it can't count on an early stop), and at once when they reach it. Resolves to { end, answer }:
 *  'settled' (the early stop) or 'complete', with the scanner's answer; 'start', 'stopped' or 'slow' (a fallback);
 *  'dropped' (the server no longer has the test); or 'aborted'. Never rejects. */
export async function runBrowserPart(opts) {
  const n = T.scenes.s;
  const [w, h] = FRAME;
  const B = T.budget;
  const meter = {
    bytes: 0, ms: 0, scenes: 0, limit: B.fetch_s * 1000, tries: B.fetch_tries, wait: T.page_waits.scene_retry_s * 1000,
    hold: 2 * T.server.scene_wait_s * 1000, // facts come within scene_wait_s, a 503 included: twice that is a stall
  };
  let detector;
  try {
    detector = new BarcodeDetector({ formats: ['qr_code'] });
  } catch {
    return { end: 'start' };
  }
  let mode = opts.mode;
  const fetchOne = (i) => {
    const ctl = linked(opts.signal);
    const job = fetchScene(opts.base, i, mode, ctl.signal, meter);
    job.catch(() => {}); // awaited in turn, or aborted unread
    return { ctl, job };
  };
  let next = fetchOne(0);
  let b = 0;
  let c = 0;
  let t = 0;
  let d = 0;
  try {
    for (let i = 0; i < n; i += 1) {
      const scene = await next.job;
      if (scene.switched && mode === 'jpeg') {
        mode = 'png';
        opts.onMode('png');
      }
      next = i + 1 < n ? fetchOne(i + 1) : null; // in flight while this scene is read, so a switch wastes nothing
      const plain = await reads(detector, scene.plain, w, h, opts.truths);
      const design = await reads(detector, scene.design, w, h, opts.truths);
      if (opts.signal.aborted) throw ending('aborted');
      b += plain && !design;
      c += design && !plain;
      t += plain;
      d += design;
      const k = i + 1;
      opts.onScene(k);
      const a = settled(b, c, t, d, n - k);
      if (a) {
        next?.ctl.abort();
        return { end: k === n ? 'complete' : 'settled', answer: a };
      }
      if (k >= B.project_after_scenes && (meter.ms * n) / Math.max(1, meter.scenes) > meter.limit) {
        next?.ctl.abort();
        return { end: 'slow' };
      }
    }
  } catch (err) {
    next?.ctl.abort();
    return { end: err.end || (opts.signal.aborted ? 'aborted' : 'stopped') };
  }
  return { end: 'stopped' }; // not reached: the last scene always settles
}