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.
274 lines
9.1 KiB
JavaScript
274 lines
9.1 KiB
JavaScript
// Import worker of the R4 measurement page (R4.2e, R4.3a), 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, one result message. The main thread terminates it in every
|
|
// case. Two read paths, chosen per task (`readPath`):
|
|
// stream (default, R4.3a) `file.stream()` pushed chunk by chunk into
|
|
// a `FileBytes` sink in linear memory, then `import_pdf_bytes`
|
|
// or `restore_copy_bytes` (the core decodes the copy): the
|
|
// file lives once, in linear memory. A BYOB reader reads into
|
|
// one reused 1 MiB buffer (no garbage chunk per read); a
|
|
// stream that refuses BYOB is read with its default reader,
|
|
// each chunk dropped once pushed; without `stream` the file
|
|
// is read whole and pushed once. `readVia` records which;
|
|
// arrayBuffer (R4.2e, kept to measure both) one ArrayBuffer of the file,
|
|
// `import_pdf` (the glue copies it into linear memory) or the
|
|
// strict TextDecoder then `draft.restore`.
|
|
import init, { FileBytes, SheetHandle } from '/pkg/faunosteo_wasm.js'
|
|
|
|
const BYOB_BUFFER_BYTES = 1024 * 1024
|
|
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 })
|
|
|
|
/** The size pre-check, before any read: the kinds the reader would give. */
|
|
function precheck(route, file, limits) {
|
|
const limit = route === 'copy' ? limits.copyBytes : limits.pdfBytes
|
|
if (file.size <= limit) return null
|
|
const kind = route === 'copy' ? 'invalid' : 'invalid_pdf'
|
|
return refusal(kind, `file size ${file.size} > ${limit}, not read`)
|
|
}
|
|
|
|
/** R4.2e: `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)
|
|
}
|
|
|
|
/** R4.3a: the sink consumed by the import (the wrapper is invalidated). */
|
|
function runSink(handle, route, sink) {
|
|
if (route === 'pdf') return handle.import_pdf_bytes(sink)
|
|
return handle.restore_copy_bytes(sink)
|
|
}
|
|
|
|
/** The R4.2e read: one ArrayBuffer, alive until the import returns. */
|
|
async function readWhole(file) {
|
|
let buffer
|
|
try {
|
|
buffer = await file.arrayBuffer()
|
|
} catch (error) {
|
|
return { kind: 'read', error }
|
|
}
|
|
const size = buffer.byteLength
|
|
return {
|
|
facts: { readVia: 'arrayBuffer', chunks: 1, maxChunkBytes: size },
|
|
run: (handle, route) => runImport(handle, route, buffer),
|
|
release: () => undefined,
|
|
}
|
|
}
|
|
|
|
/** A read's result, or `{ error }` when the browser cannot read the file. */
|
|
const settleRead = (pending) => pending.catch((error) => ({ error }))
|
|
const ignore = () => undefined
|
|
|
|
/** BYOB when the stream is a byte stream, else its default reader. */
|
|
function byobReader(stream) {
|
|
try {
|
|
return stream.getReader({ mode: 'byob' })
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The stream's chunks: BYOB reads into one buffer, recycled after each push
|
|
* (its bytes were copied into the sink); else the stream's own chunks.
|
|
*/
|
|
function chunkSource(stream) {
|
|
const byob = byobReader(stream)
|
|
if (!byob) {
|
|
const reader = stream.getReader()
|
|
return {
|
|
via: 'stream-default',
|
|
next: () => settleRead(reader.read()),
|
|
recycle: ignore,
|
|
cancel: () => reader.cancel().catch(ignore),
|
|
}
|
|
}
|
|
let buffer = new ArrayBuffer(BYOB_BUFFER_BYTES)
|
|
return {
|
|
via: 'stream-byob',
|
|
next: () => settleRead(byob.read(new Uint8Array(buffer))),
|
|
recycle: (view) => (buffer = view.buffer),
|
|
cancel: () => byob.cancel().catch(ignore),
|
|
}
|
|
}
|
|
|
|
/** Pushes one chunk; `{ kind, error }` when refused or when it throws. */
|
|
function pushChunk(sink, chunk, facts) {
|
|
facts.chunks += 1
|
|
facts.maxChunkBytes = Math.max(facts.maxChunkBytes, chunk.byteLength)
|
|
try {
|
|
if (sink.push(chunk)) return null
|
|
} catch (error) {
|
|
return { kind: kindOf(error), error }
|
|
}
|
|
return { kind: 'read', error: new Error('more bytes than file.size') }
|
|
}
|
|
|
|
/**
|
|
* `file.stream()` into the sink, chunk by chunk (no chunk list, no
|
|
* concatenation), so the whole file exists only in the sink.
|
|
*/
|
|
async function streamInto(file, sink, facts) {
|
|
let source
|
|
try {
|
|
source = chunkSource(file.stream())
|
|
} catch (error) {
|
|
return { kind: 'read', error }
|
|
}
|
|
facts.readVia = source.via
|
|
for (;;) {
|
|
let next = await source.next()
|
|
if (next.error) return { kind: 'read', error: next.error }
|
|
if (next.done) return null
|
|
const refused = pushChunk(sink, next.value, facts)
|
|
source.recycle(next.value)
|
|
next = null // the chunk is unreachable before the next read suspends
|
|
if (refused) {
|
|
source.cancel()
|
|
return refused
|
|
}
|
|
}
|
|
}
|
|
|
|
/** No `file.stream()`: the file read whole, pushed once, then dropped. */
|
|
async function bufferInto(file, sink, facts) {
|
|
facts.readVia = 'arrayBuffer-fallback'
|
|
let buffer
|
|
try {
|
|
buffer = await file.arrayBuffer()
|
|
} catch (error) {
|
|
return { kind: 'read', error }
|
|
}
|
|
return pushChunk(sink, new Uint8Array(buffer), facts)
|
|
}
|
|
|
|
/** The file gathered in a `FileBytes` sink sized from `file.size`. */
|
|
async function readStreamed(file) {
|
|
let sink
|
|
try {
|
|
sink = new FileBytes(file.size)
|
|
} catch (error) {
|
|
return { kind: kindOf(error), error }
|
|
}
|
|
const facts = { readVia: 'stream', chunks: 0, maxChunkBytes: 0 }
|
|
const fill = typeof file.stream === 'function' ? streamInto : bufferInto
|
|
const failure = (await fill(file, sink, facts)) ?? shortRead(file, sink)
|
|
if (failure) {
|
|
sink.free()
|
|
return failure
|
|
}
|
|
return {
|
|
facts,
|
|
run: (handle, route) => runSink(handle, route, sink),
|
|
release: () => sink.free(),
|
|
}
|
|
}
|
|
|
|
/** A read that ended before `file.size`: a read failure, never a PDF. */
|
|
function shortRead(file, sink) {
|
|
if (sink.len() === file.size) return null
|
|
const text = `read ${sink.len()} of ${file.size} bytes`
|
|
return { kind: 'read', error: new Error(text) }
|
|
}
|
|
|
|
/** 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, readPath = 'stream' }) {
|
|
const startedAt = performance.now()
|
|
const refused = precheck(route, file, limits)
|
|
if (refused) {
|
|
const diag = { precheck: true, readPath }
|
|
return post({ type: 'result', out: refused, diag })
|
|
}
|
|
const read = readPath === 'arrayBuffer' ? readWhole : readStreamed
|
|
const bytes = await read(file)
|
|
if (bytes.error) return fail('task', bytes.kind, bytes.error)
|
|
const readAt = performance.now()
|
|
const handle = await step('sheet', () => new SheetHandle(sheet))
|
|
if (!handle.ok) return bytes.release()
|
|
const sheetAt = performance.now()
|
|
const linearBeforeRun = linearBytes()
|
|
const out = await step(null, () => bytes.run(handle.value, route))
|
|
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(),
|
|
readPath,
|
|
...bytes.facts,
|
|
}
|
|
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)
|