218 lines
7.1 KiB
JavaScript
218 lines
7.1 KiB
JavaScript
// O9 browser verdicts (R4.1e, plan §B.1.2 "Inflation", §B.5.1): runs, in
|
|
// each engine, the algorithm of the R3 engine's `inflateFlate` (the frozen
|
|
// copy packages/core-wasm/tests/n-1/scanner/attachment/inflate.ts; copied
|
|
// into the page below) on every row of
|
|
// crates/core/tests/oracles/r4/inflate.json, with the row's limit, on the
|
|
// input bytes of apps/app/tests/fixtures/pdf/r4/inflate/. Writes
|
|
// crates/core/tests/oracles/r4/inflate.browsers.json (per row: Node's
|
|
// verdict from inflate.json and each engine's, null or { length, sha256 })
|
|
// and lists every row where an engine disagrees with Node.
|
|
//
|
|
// PLAYWRIGHT_NODE_MODULES=... node scripts/browser/inflate-verdicts.mjs \
|
|
// [--engines chromium,firefox,webkit]
|
|
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
import { parseArgs } from 'node:util'
|
|
import { untilStopped } from './lib/async.mjs'
|
|
import { checkBuild } from './lib/app-build.mjs'
|
|
import { sha256 } from './lib/inputs.mjs'
|
|
import { ORACLES_R4_DIR, REPO_ROOT, relativeToRepo } from './lib/paths.mjs'
|
|
import {
|
|
ENGINES,
|
|
closeEngine,
|
|
launchEngine,
|
|
loadPlaywright,
|
|
} from './lib/playwright.mjs'
|
|
import { watchdogFacts } from './lib/watchdog.mjs'
|
|
import { startServer } from './serve.mjs'
|
|
|
|
const ORACLE = join(ORACLES_R4_DIR, 'inflate.json')
|
|
const OUT = join(ORACLES_R4_DIR, 'inflate.browsers.json')
|
|
|
|
/**
|
|
* inflate.ts inflateFlate, verbatim in behaviour: DecompressionStream
|
|
* ('deflate'), 16 KiB slices each awaited, the drain concurrent with the
|
|
* feed, null past `limit` (both sides torn down) or on any stream error.
|
|
* Self-contained: Playwright serialises it into the page.
|
|
*/
|
|
async function inflateInPage({ base64, limit }) {
|
|
const WRITE_SLICE_BYTES = 16 * 1024
|
|
const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0))
|
|
function concat(chunks, total) {
|
|
const out = new Uint8Array(total)
|
|
let offset = 0
|
|
for (const chunk of chunks) {
|
|
out.set(chunk, offset)
|
|
offset += chunk.byteLength
|
|
}
|
|
return out
|
|
}
|
|
async function feedSlices(writer, source) {
|
|
for (let at = 0; at < source.byteLength; at += WRITE_SLICE_BYTES) {
|
|
await writer.write(source.subarray(at, at + WRITE_SLICE_BYTES))
|
|
}
|
|
await writer.close()
|
|
}
|
|
async function drainUnder(reader) {
|
|
const chunks = []
|
|
let total = 0
|
|
for (;;) {
|
|
const { done, value } = await reader.read()
|
|
if (done) return concat(chunks, total)
|
|
total += value.byteLength
|
|
if (total > limit) return null
|
|
chunks.push(value)
|
|
}
|
|
}
|
|
async function inflateFlate() {
|
|
if (typeof DecompressionStream === 'undefined') return null
|
|
const inflater = new DecompressionStream('deflate')
|
|
const writer = inflater.writable.getWriter()
|
|
const reader = inflater.readable.getReader()
|
|
const fed = feedSlices(writer, bytes)
|
|
fed.catch(() => undefined)
|
|
try {
|
|
const out = await drainUnder(reader)
|
|
if (out === null) {
|
|
await Promise.allSettled([reader.cancel(), writer.abort()])
|
|
}
|
|
return out
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
const out = await inflateFlate()
|
|
if (out === null) return null
|
|
const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', out))
|
|
const hex = [...digest].map((b) => b.toString(16).padStart(2, '0')).join('')
|
|
return { length: out.byteLength, sha256: hex }
|
|
}
|
|
|
|
/** inflate.json and its inputs, each checked against the recorded digest. */
|
|
function loadOracle() {
|
|
if (!existsSync(ORACLE)) {
|
|
throw new Error(
|
|
`${relativeToRepo(ORACLE)} missing: run the O9 generator first`,
|
|
)
|
|
}
|
|
const oracle = JSON.parse(readFileSync(ORACLE, 'utf8'))
|
|
const dir = join(REPO_ROOT, oracle.inputsDir)
|
|
const inputs = new Map()
|
|
for (const input of oracle.inputs) {
|
|
const bytes = readFileSync(join(dir, input.file))
|
|
if (bytes.length !== input.length || sha256(bytes) !== input.sha256) {
|
|
throw new Error(`${input.file}: bytes differ from inflate.json`)
|
|
}
|
|
inputs.set(input.file, bytes.toString('base64'))
|
|
}
|
|
return { oracle, inputs }
|
|
}
|
|
|
|
async function engineVerdicts(pw, engine, baseUrl, { oracle, inputs }) {
|
|
const session = await launchEngine(pw, engine)
|
|
try {
|
|
const page = await session.browser.newPage()
|
|
await page.goto(`${baseUrl}/`)
|
|
const verdicts = []
|
|
for (const row of oracle.rows) {
|
|
const arg = { base64: inputs.get(row.file), limit: row.limit }
|
|
const end = await untilStopped(
|
|
session,
|
|
page
|
|
.evaluate(inflateInPage, arg)
|
|
.then((value) => ({ kind: 'ok', value })),
|
|
)
|
|
if (end.kind !== 'ok')
|
|
throw new Error(`${engine} ${row.file}: ${end.kind}`)
|
|
verdicts.push(end.value)
|
|
}
|
|
return { version: session.version, verdicts }
|
|
} finally {
|
|
await closeEngine(session)
|
|
}
|
|
}
|
|
|
|
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b)
|
|
|
|
function disagreements(rows, engines) {
|
|
return rows.flatMap((row) =>
|
|
engines
|
|
.filter((engine) => !same(row.verdicts[engine], row.verdicts.node))
|
|
.map((engine) => ({
|
|
file: row.file,
|
|
case: row.case,
|
|
limit: row.limit,
|
|
engine,
|
|
node: row.verdicts.node,
|
|
browser: row.verdicts[engine],
|
|
})),
|
|
)
|
|
}
|
|
|
|
function resultOf(oracle, engines, columns) {
|
|
const rows = oracle.rows.map((row, i) => ({
|
|
file: row.file,
|
|
case: row.case,
|
|
limit: row.limit,
|
|
verdicts: {
|
|
node: row.verdicts.node,
|
|
...Object.fromEntries(engines.map((e) => [e, columns[e].verdicts[i]])),
|
|
},
|
|
}))
|
|
const versions = engines.map((e) => [e, { version: columns[e].version }])
|
|
return {
|
|
oracle: 'O9 browser columns',
|
|
source: relativeToRepo(ORACLE),
|
|
harness: 'scripts/browser/inflate-verdicts.mjs',
|
|
function: oracle.function,
|
|
page: 'Home of the build served by serve.mjs (DEPLOY.md CSP)',
|
|
watchdog: watchdogFacts(),
|
|
engines: { node: oracle.engines.node, ...Object.fromEntries(versions) },
|
|
rows,
|
|
disagreements: disagreements(rows, engines),
|
|
}
|
|
}
|
|
|
|
async function allColumns(engines, loaded) {
|
|
const { pw } = loadPlaywright()
|
|
const server = await startServer()
|
|
const columns = {}
|
|
try {
|
|
for (const engine of engines) {
|
|
columns[engine] = await engineVerdicts(pw, engine, server.url, loaded)
|
|
}
|
|
} finally {
|
|
server.close()
|
|
}
|
|
return columns
|
|
}
|
|
|
|
function report(result) {
|
|
const engines = Object.keys(result.engines).filter((e) => e !== 'node')
|
|
console.log(
|
|
`${relativeToRepo(OUT)}: ${result.rows.length} rows, ${engines.join(', ')}`,
|
|
)
|
|
for (const d of result.disagreements) {
|
|
const node = JSON.stringify(d.node)
|
|
const browser = JSON.stringify(d.browser)
|
|
console.log(`DIFF ${d.engine} ${d.file} ${d.limit}: ${node} / ${browser}`)
|
|
}
|
|
if (result.disagreements.length === 0)
|
|
console.log('every engine agrees with Node')
|
|
}
|
|
|
|
async function main() {
|
|
const { values } = parseArgs({
|
|
options: { engines: { type: 'string', default: ENGINES.join(',') } },
|
|
})
|
|
const engines = values.engines.split(',').filter(Boolean)
|
|
const loaded = loadOracle()
|
|
checkBuild()
|
|
const columns = await allColumns(engines, loaded)
|
|
const result = resultOf(loaded.oracle, engines, columns)
|
|
writeFileSync(OUT, `${JSON.stringify(result, null, 2)}\n`)
|
|
report(result)
|
|
}
|
|
|
|
await main()
|