Files
faunosteo/scripts/browser/lib/import-inputs.mjs
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

100 lines
3.3 KiB
JavaScript

import {
existsSync,
mkdtempSync,
readFileSync,
rmdirSync,
statSync,
unlinkSync,
writeFileSync,
} from 'node:fs'
import { basename, join } from 'node:path'
import { expandGenerator, filesUnder, isGenerator } from './inputs.mjs'
import { FIXTURES_DIR, ORACLES_R4_DIR, relativeToRepo } from './paths.mjs'
/**
* The import file set of import-baseline.mjs (groups base, r3, step6,
* tolerance, read), listed the same way, so the R4 import measures the
* TypeScript baseline's perimeter file for file.
*
* shortcut: a copy of import-baseline.mjs's listing (R4.2e leaves the
* baseline script untouched); compare-imports.mjs checks that both runs
* hold the same files. Upgrade path: import-baseline.mjs imports this
* module.
*/
const READ_DIR = join(FIXTURES_DIR, 'pdf/r4/read')
const BASE_FILES = [
'report-v2.full.pdf',
'report-v2.minimal.pdf',
'report-v3.bare.pdf',
]
const isPdf = (path) => path.endsWith('.pdf')
function pdfsIn(group, dir, recursive = false) {
const paths = filesUnder(join(FIXTURES_DIR, dir), recursive).filter(isPdf)
return paths.map((path) => ({ group, path }))
}
function fixedGroups() {
return [
...BASE_FILES.map((f) => ({ group: 'base', path: join(FIXTURES_DIR, f) })),
...pdfsIn('r3', 'pdf/r3', true),
...pdfsIn('step6', 'pdf/step6'),
...pdfsIn('tolerance', 'pdf/r4/tolerance'),
]
}
/** The hostile set as read.json (O7) lists it; absent listed files reported. */
function hostileGroup() {
const oraclePath = join(ORACLES_R4_DIR, 'read.json')
if (!existsSync(READ_DIR)) {
return { inputs: [], missing: ['pdf/r4/read/'], listedBy: null }
}
const listed = existsSync(oraclePath)
? JSON.parse(readFileSync(oraclePath, 'utf8')).inputs
: filesUnder(READ_DIR).map((path) => ({ file: basename(path), kind: null }))
const present = listed.filter((i) => existsSync(join(READ_DIR, i.file)))
const missing = listed.filter((i) => !present.includes(i)).map((i) => i.file)
const inputs = present.map((i) => ({
group: 'read',
kind: i.kind,
path: join(READ_DIR, i.file),
}))
return {
inputs,
missing,
listedBy: existsSync(oraclePath) ? relativeToRepo(oraclePath) : 'directory',
}
}
/** Every import input, narrowed by `--groups` (null = all). */
export function importInputs(groups) {
const hostile = hostileGroup()
const wanted = (i) => !groups || groups.includes(i.group)
const inputs = [...fixedGroups(), ...hostile.inputs].filter(wanted)
return { inputs, hostile }
}
/** The bytes an input stands for: its size, or its description's length. */
export function inputBytes(path) {
if (!isGenerator(path)) return statSync(path).size
return JSON.parse(readFileSync(path, 'utf8')).length
}
/** A generator description becomes a temporary file, removed after the run. */
export function materialise(input, scratch) {
if (!isGenerator(input.path)) {
return { path: input.path, cleanup: () => undefined }
}
const path = join(scratch, `${basename(input.path, '.gen.json')}.pdf`)
writeFileSync(path, expandGenerator(input.path))
return { path, cleanup: () => unlinkSync(path) }
}
/** A fresh directory under `parent` for the run's temporary files. */
export function scratchDir(parent) {
const dir = mkdtempSync(join(parent, 'faunosteo-r4-import-'))
return { dir, remove: () => rmdirSync(dir) }
}