Files
faunosteo/scripts/browser/r4-import-page/worker.js
T
bchanot c84186b032 test(tests): R4.2e browser measurements of the Rust import (module worker page, same perimeter as the TS baseline)
r4-import.mjs runs the 226 baseline files and the hostile classes through a disposable
module worker calling SheetHandle.import_pdf in Chromium 156, Firefox 157 and WebKit 27.2
under the RSS watchdog (not a guaranteed limit); compare-imports.mjs prints TS vs Rust;
serve.mjs gains opt-in mounts (/r4/, /pkg/). Results 2026-10-18-r4-import.json: 666/678
agreements, the 12 others are the 4 declared files; W4 met (max 68 MiB linear); the
1 GiB ceiling traps in every engine.
2026-10-18 16:58:42 +02:00

115 lines
3.9 KiB
JavaScript

// Import worker of the R4 measurement page (R4.2e), the shape plan §B.1.5
// gives the application at R4.3: a module worker created for one import,
// the glue imported statically, the module received from the main thread
// (structured clone) or fetched from its URL, the File read here, one
// SheetHandle, `import_pdf` or the strict decode then `draft.restore`, one
// result message. The main thread terminates it in every case.
import init, { SheetHandle } from '/pkg/faunosteo_wasm.js'
const cspViolations = []
let memory = null
self.addEventListener('securitypolicyviolation', (event) => {
cspViolations.push(`${event.effectiveDirective} ${event.blockedURI}`)
})
const linearBytes = () => memory?.buffer.byteLength ?? null
function post(message) {
self.postMessage({ v: 1, ...message, csp: cspViolations })
}
function describe(error) {
const text = `${error?.name ?? 'Error'}: ${error?.message ?? error}`
return text.split('\n')[0].slice(0, 200)
}
/** Plan §B.1.5 kinds: a trap, an allocation failure, anything else. */
function kindOf(error) {
const name = error?.name ?? ''
if (name === 'RuntimeError' || name === 'InternalError') return 'trap'
if (name !== 'RangeError') return 'other'
return /memory|alloc|buffer/i.test(error.message) ? 'alloc' : 'trap'
}
function fail(phase, kind, error) {
post({ type: 'failed', phase, kind, error: describe(error) })
}
/** Instantiates once; `module` when the clone arrived, else the URL. */
async function start({ module, url }) {
const exports = await init({ module_or_path: module ?? url })
memory = exports.memory
post({
type: 'ready',
via: module ? 'module' : 'url',
linearBytes: linearBytes(),
})
}
/** A refusal decided here, in the envelope's shape. */
const refusal = (kind, detail) => JSON.stringify({ ok: false, kind, detail })
/** `import_pdf`, or the strict UTF-8 decode then `draft.restore`. */
function runImport(handle, route, bytes) {
if (route === 'pdf') return handle.import_pdf(new Uint8Array(bytes))
let text
try {
text = new TextDecoder('utf-8', { fatal: true }).decode(bytes)
} catch {
return refusal('invalid_utf8', 'strict TextDecoder refused the copy')
}
const input = JSON.stringify({ draft: text, archive: null })
return handle.call('draft.restore', input)
}
/** One step whose throwable becomes a `failed` message of its own kind. */
async function step(kind, work) {
try {
return { ok: true, value: await work() }
} catch (error) {
fail('task', kind ?? kindOf(error), error)
return { ok: false }
}
}
async function task({ route, file, sheet, limits }) {
const startedAt = performance.now()
const limit = route === 'copy' ? limits.copyBytes : limits.pdfBytes
if (file.size > limit) {
const out = refusal('file_too_large', `${file.size} > ${limit}`)
return post({ type: 'result', out, diag: { precheck: true } })
}
const bytes = await step('read', () => file.arrayBuffer())
if (!bytes.ok) return
const readAt = performance.now()
const handle = await step('sheet', () => new SheetHandle(sheet))
if (!handle.ok) return
const sheetAt = performance.now()
const linearBeforeRun = linearBytes()
const out = await step(null, () =>
runImport(handle.value, route, bytes.value),
)
if (!out.ok) return
const doneAt = performance.now()
const diag = {
ms: doneAt - startedAt,
readMs: readAt - startedAt,
sheetMs: sheetAt - readAt,
runMs: doneAt - sheetAt,
linearBeforeRun,
linearBytes: linearBytes(),
}
post({ type: 'result', out: out.value, diag })
}
self.onmessage = (event) => {
const message = event.data
const phase = message?.type === 'init' ? 'start' : 'task'
const work = phase === 'start' ? start(message) : task(message)
work.catch((error) => fail(phase, kindOf(error), error))
}
self.onmessageerror = () => post({ type: 'clone_failed' })
self.onunhandledrejection = (event) =>
fail('task', kindOf(event.reason), event.reason)