a post-component library for building user-interfaces on the web.
1import * as child_process from 'node:child_process'
2import { once } from 'node:events'
3import * as fs from 'node:fs/promises'
4import * as os from 'node:os'
5import * as path from 'node:path'
6import { fileURLToPath } from 'node:url'
7import type { Runtime } from './main.ts'
8
9export async function create_node_runtime(): Promise<Runtime> {
10 const coverage_dir = await fs.mkdtemp(path.join(os.tmpdir(), 'coverage-'))
11 const child = child_process.fork(fileURLToPath(import.meta.resolve('./runtime.ts')), {
12 env: { NODE_V8_COVERAGE: coverage_dir },
13 stdio: 'inherit',
14 execArgv: ['--expose-gc'],
15 })
16
17 const { port1, port2 } = new MessageChannel()
18 port1.onmessage = e => child.send(e.data)
19 child.on('message', data => port1.postMessage(data))
20
21 await once(child, 'spawn')
22
23 return {
24 port: port2,
25 async coverage() {
26 const [filename] = await fs.readdir(coverage_dir)
27 const { result } = JSON.parse(await fs.readFile(path.join(coverage_dir, filename), 'utf8'))
28 return result
29 },
30 async [Symbol.asyncDispose]() {
31 port1.close()
32 child.kill()
33 await fs.rm(coverage_dir, { recursive: true })
34 },
35 }
36}