Skip to content
Source
Sign inSign up
TanStack/container
Open History to see the last commit

node-reference.mjs

75 lines2.4 KB

1// Executes ONLY the checked-in trusted probe corpus, never submitted guest code.
2import { readFile, writeFile, mkdir, mkdtemp } from 'node:fs/promises'
3import { execFile } from 'node:child_process'
4import { promisify } from 'node:util'
5import { tmpdir } from 'node:os'
6import { join, dirname } from 'node:path'
7import { createHash } from 'node:crypto'
8const run = promisify(execFile)
9const source = new URL('../src/feasibility/cases.json', import.meta.url)
10const raw = await readFile(source, 'utf8')
11const cases = JSON.parse(raw)
12const directory = await mkdtemp(join(tmpdir(), 'sandbox-node-reference-'))
13const results = []
14for (const probe of cases) {
15 if (!/^[a-z0-9-]+$/.test(probe.id)) throw new Error('Invalid probe id')
16 const cwd = join(directory, probe.id)
17 for (const [path, text] of Object.entries({
18 ...probe.files,
19 'main.mjs': probe.code,
20 })) {
21 if (
22 path.startsWith('/') ||
23 path.split('/').some((x) => x === '..') ||
24 path.includes('\\')
25 )
26 throw new Error('Invalid fixture path')
27 const target = join(cwd, path)
28 await mkdir(dirname(target), { recursive: true })
29 await writeFile(target, text)
30 }
31 let result
32 try {
33 const output = await run(process.execPath, ['main.mjs', 'arg'], {
34 cwd,
35 env: { PROBE_VALUE: 'scoped' },
36 timeout: 5000,
37 maxBuffer: 1024 * 1024,
38 })
39 result = { exitCode: 0, stdout: output.stdout, stderr: output.stderr }
40 } catch (error) {
41 if (typeof error.code !== 'number' || error.killed) throw error
42 result = {
43 exitCode: error.code,
44 stdout: error.stdout,
45 stderr: error.stderr,
46 }
47 }
48 // Nonzero exit is deliberate only in the exitCode case. Broken fixtures fail the baseline build.
49 if (result.exitCode !== (probe.id === 'process-exit-code' ? 7 : 0))
50 throw new Error(`${probe.id}: ${result.stderr}`)
51 results.push({ id: probe.id, ...result })
53const destination = new URL(
54 '../public/feasibility/node-reference.json',
55 import.meta.url,
57await mkdir(new URL('.', destination), { recursive: true })
58await writeFile(
59 destination,
60 JSON.stringify(
61 {
62 schema: 1,
63 node: process.version,
64 platform: process.platform,
65 generatedAt: new Date().toISOString(),
66 corpusSHA256: createHash('sha256').update(raw).digest('hex'),
67 results,
68 },
69 null,
70 2,
71 ) + '\n',
73console.log(
74 `Node ${process.version}: ${results.length} reference probes, ${destination.pathname}`,