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 })
15
16 const { port1, port2 } = new MessageChannel()
17 port1.onmessage = e => child.send(e.data)
18 child.on('message', data => port1.postMessage(data))
19
20 await once(child, 'spawn')
21
22 return {
23 port: port2,
24 async coverage() {
25 const [filename] = await fs.readdir(coverage_dir)
26 const { result } = JSON.parse(await fs.readFile(path.join(coverage_dir, filename), 'utf8'))
27 return result
28 },
29 async [Symbol.asyncDispose]() {
30 port1.close()
31 child.kill()
32 await fs.rm(coverage_dir, { recursive: true })
33 },
34 }
35}