Files
faunosteo/scripts/browser/r4-import-page/main.js
T
bchanot 49591c53cd test(tests): R4.3a browser re-measure of large files, leak series, WebKit repetition (stream sink, BYOB)
Streaming into FileBytes removes the file's JavaScript copy in Chromium and Firefox
(50 MiB: 65 and 67 MiB summed growth against 56 and 79 for the TypeScript); WebKit keeps
about 180 MiB for any file read inside a worker (272 against 76), a main-thread BYOB read
feeding the worker's sink measured at 106. Corpus 666/678 on the stream path. Leak series
(3 engines × 3 documents × 3 repetitions): Chromium ≤ 0.5 MiB over the last 5, Firefox
within ±3 (idle only), WebKit copy +13 then level (12-import series −2.3). 20 dense
imports in WebKit: no worker error event.
2026-10-18 18:50:47 +02:00

320 lines
10 KiB
JavaScript

// Main thread of the R4 measurement page (R4.2e, R4.3a). Compiles the module once
// (`WebAssembly.compileStreaming`), routes a picked file as HomeRoute does
// (first KiB: PDF magic, JSON object or `.json` name), then runs each import
// in a module worker created for it (plan §B.1.5): the compiled Module
// posted by structured clone (or, when an engine refuses the clone, the
// worker fetches the URL, and the refusal is recorded), the File and the
// sheet bundle text posted on `ready`, `terminate()` in every case. The
// worker's read path is `stream` (R4.3a, default) or `arrayBuffer` (R4.2e),
// per call or for the page through `?read=arrayBuffer`. Every `error` and
// `messageerror` event of a worker is recorded with its text, the first one
// settles the run.
//
// window.r4Import(fileOrBytes, bundleJson, mode = 'auto' | 'pdf' | 'copy',
// readPath = 'stream' | 'arrayBuffer')
// window.r4Proof('ceiling' | 'cap_off', bundleJson, file?)
// window.r4LastResult, window.r4Page (CSP violations, long tasks, compile)
const WASM_URL = '/pkg/faunosteo_wasm_bg.wasm'
const IMPORT_WORKER = '/r4/worker.js'
const PROOFS_WORKER = '/r4/proofs-worker.js'
const MiB = 1024 * 1024
const LIMITS = { pdfBytes: 50 * MiB, copyBytes: 16 * MiB }
const TIMEOUTS = { startMs: 10_000, taskMs: 120_000 }
const SNIFF_BYTES = 1024
const PDF_MAGIC = '%PDF-'
const READ_PATHS = ['stream', 'arrayBuffer']
const PAGE_READ_PATH =
new URLSearchParams(location.search).get('read') === 'arrayBuffer'
? 'arrayBuffer'
: 'stream'
const page = {
csp: [],
longTaskSupported: false,
longTasks: [],
file: null,
changeAt: null,
limits: LIMITS,
timeouts: TIMEOUTS,
}
window.r4Page = page
const now = () => performance.now()
const describe = (error) =>
`${error?.name ?? 'Error'}: ${error?.message ?? error}`
.split('\n')[0]
.slice(0, 200)
function listen() {
document.addEventListener('securitypolicyviolation', (event) =>
page.csp.push(`${event.effectiveDirective} ${event.blockedURI}`),
)
document.getElementById('file').addEventListener('change', (event) => {
page.file = event.target.files?.[0] ?? null
page.changeAt = now()
})
const types = PerformanceObserver.supportedEntryTypes ?? []
if (!types.includes('longtask')) return
new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
page.longTasks.push({ start: entry.startTime, ms: entry.duration })
}
}).observe({ type: 'longtask' })
page.longTaskSupported = true
}
async function compileModule() {
const startedAt = now()
try {
const module = await WebAssembly.compileStreaming(fetch(WASM_URL))
return { module, ms: now() - startedAt, error: null }
} catch (error) {
return { module: null, ms: now() - startedAt, error: describe(error) }
}
}
/** HomeRoute: first non-blank byte after an optional UTF-8 BOM. */
function firstContentByte(head) {
const bom = head[0] === 0xef && head[1] === 0xbb && head[2] === 0xbf
const blank = [0x20, 0x09, 0x0a, 0x0d]
return head.slice(bom ? 3 : 0).find((b) => !blank.includes(b))
}
/** HomeRoute's routing, from the first KiB only. */
async function routeOf(file) {
const head = new Uint8Array(await file.slice(0, SNIFF_BYTES).arrayBuffer())
const magic = new TextDecoder('latin1').decode(head.slice(0, 5))
if (magic === PDF_MAGIC) return 'pdf'
if (firstContentByte(head) === 0x7b) return 'copy'
const json =
file.name.toLowerCase().endsWith('.json') ||
file.type === 'application/json'
return json ? 'copy' : 'pdf'
}
/** The envelope's facts the comparison needs (never the report itself). */
function summaryOf(value) {
const read = value?.read ?? null
const rebase = value?.rebase ?? null
return {
origin: read?.origin ?? null,
migrated: read?.migrated ?? null,
snapshotRejected: read?.snapshotRejected ?? null,
archive: Boolean(read ? read.archive : value?.archive),
rebased: rebase?.rebased ?? null,
unresolved: rebase
? rebase.unresolvedZones + rebase.unresolvedFindings
: null,
}
}
/** The result message: the envelope parsed here, as the app will parse it. */
function resultOf(message) {
const startedAt = now()
const envelope = JSON.parse(message.out)
const parseMs = now() - startedAt
const head = { parseMs, outChars: message.out.length, diag: message.diag }
if (envelope.ok) {
return { outcome: 'ok', ...head, summary: summaryOf(envelope.value) }
}
const detail = JSON.stringify(envelope.detail ?? null).slice(0, 200)
return { outcome: 'refused', kind: envelope.kind, detail, ...head }
}
/** An `error` event at the Worker: an ErrorEvent, or a bare load failure. */
function errorText(event) {
if (!event.message) return `${event.type} event without message`
const where = event.filename ? ` (${event.filename}:${event.lineno})` : ''
return `${event.message}${where}`.slice(0, 200)
}
/** One worker, one task; the first settlement wins, the worker dies. */
class WorkerRun {
constructor({ url, protocol, module, task }) {
this.url = url
this.protocol = protocol
this.module = module
this.task = task
this.createdAt = now()
this.readyAt = null
this.transfer = null
this.cloneError = null
this.workerCsp = []
this.events = []
this.done = new Promise((resolve) => (this.resolve = resolve))
}
start() {
try {
this.worker = new Worker(this.url, { type: 'module' })
} catch (error) {
return this.settle(this.failure('start', 'worker_unavailable', error))
}
this.worker.onmessage = (event) => this.onMessage(event.data)
this.worker.onerror = (event) => this.onEvent('worker_error', event)
this.worker.onmessageerror = (event) => this.onEvent('messageerror', event)
this.arm(TIMEOUTS.startMs, 'start')
this.postInit()
return this.done
}
phase() {
return this.readyAt === null ? 'start' : 'task'
}
/** Every error / messageerror event kept with its text; the first settles. */
onEvent(kind, event) {
const text = kind === 'worker_error' ? errorText(event) : event.type
const at = Math.round(now() - this.createdAt)
this.events.push({ kind, phase: this.phase(), text, atMs: at })
this.settle(this.failure(this.phase(), kind, text))
}
failure(phase, kind, error) {
const text = typeof error === 'string' ? error : error && describe(error)
return { outcome: 'failed', phase, kind, error: text ?? null }
}
arm(ms, phase) {
clearTimeout(this.timer)
this.timer = setTimeout(
() => this.settle({ outcome: 'timeout', phase }),
ms,
)
}
/** Import: init (Module clone, else URL), then the task on `ready`. */
postInit() {
if (this.protocol === 'proof') {
this.transfer = 'worker fetch'
this.arm(TIMEOUTS.taskMs, 'task')
return this.post(this.task)
}
if (!this.module) return this.postUrl()
try {
this.worker.postMessage({ v: 1, type: 'init', module: this.module })
this.transfer = 'clone'
} catch (error) {
this.cloneError = describe(error)
this.postUrl()
}
}
postUrl() {
this.transfer = 'fetch'
this.post({ type: 'init', url: WASM_URL })
}
post(message) {
try {
this.worker.postMessage({ v: 1, ...message })
} catch (error) {
this.settle(this.failure(this.phase(), 'post_failed', error))
}
}
onMessage(message) {
if (message?.csp) this.workerCsp = message.csp
if (message?.type === 'clone_failed') {
this.cloneError = 'messageerror in the worker (Module not cloned)'
return this.postUrl()
}
if (message?.type === 'ready') return this.onReady(message)
if (message?.type === 'failed') {
return this.settle({ outcome: 'failed', ...message })
}
if (message?.type === 'proof') {
return this.settle({ outcome: 'proof', value: message.value })
}
if (message?.type === 'result') this.settle(resultOf(message))
}
onReady(message) {
this.readyAt = now()
this.readyLinearBytes = message.linearBytes
this.via = message.via
this.arm(TIMEOUTS.taskMs, 'task')
this.post(this.task)
}
settle(result) {
if (this.settledAt !== undefined) return
this.settledAt = now()
clearTimeout(this.timer)
this.worker?.terminate()
const { v: _v, type: _type, csp: _csp, ...rest } = result
this.resolve({ ...rest, ...this.facts() })
}
facts() {
const started = this.readyAt === null ? null : this.readyAt - this.createdAt
return {
startMs: started,
workerMs: this.settledAt - this.createdAt,
transfer: this.transfer,
via: this.via ?? null,
cloneError: this.cloneError,
readyLinearBytes: this.readyLinearBytes ?? null,
workerCsp: this.workerCsp,
workerEvents: this.events,
}
}
}
const asFile = (input) =>
input instanceof Blob ? input : new File([input], 'input.pdf')
async function r4Import(input, bundleJson, mode = 'auto', readPath) {
const calledAt = now()
const file = asFile(input)
const route = mode === 'auto' ? await routeOf(file) : mode
const { module } = await window.r4Ready.compiled
const path = READ_PATHS.includes(readPath) ? readPath : PAGE_READ_PATH
const task = {
type: 'task',
route,
file,
sheet: bundleJson,
limits: LIMITS,
readPath: path,
}
const run = new WorkerRun({
url: IMPORT_WORKER,
protocol: 'import',
module,
task,
})
const result = { route, readPath: path, ...(await run.start()) }
result.pageMs = now() - calledAt
window.r4LastResult = result
document.getElementById('status').textContent = JSON.stringify(result)
return result
}
async function r4Proof(name, bundleJson, input = null) {
const calledAt = now()
const file = input && asFile(input)
const task = { type: 'proof', name, sheet: bundleJson, file }
const run = new WorkerRun({
url: PROOFS_WORKER,
protocol: 'proof',
module: null,
task,
})
const result = { name, ...(await run.start()), pageMs: now() - calledAt }
window.r4LastResult = result
return result
}
listen()
const compiled = compileModule()
window.r4Ready = {
compiled,
facts: compiled.then(({ ms, error }) => ({ compileMs: ms, error })),
}
window.r4Import = r4Import
window.r4Proof = r4Proof
compiled.then(({ error }) => {
document.getElementById('status').textContent = error ?? 'ready'
})