Skip to content

Commit fe9644c

Browse files
committed
scripts/test: switch to capnweb
1 parent 7ffe0ce commit fe9644c

File tree

6 files changed

+27
-55
lines changed

6 files changed

+27
-55
lines changed

package-lock.json

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/test/browser-runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export async function create_browser_runtime(): Promise<Runtime> {
2323
'dhtml/client': '/dist/client.js',
2424
'dhtml/server': '/dist/server.js',
2525
birpc: '/node_modules/birpc/dist/index.mjs',
26-
devalue: '/node_modules/devalue/index.js',
26+
capnweb: '/node_modules/capnweb/dist/index.js',
2727
mitata: '/node_modules/mitata/src/main.mjs',
2828
},
2929
})}</script>

scripts/test/devalue.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

scripts/test/main.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { createBirpc } from 'birpc'
1+
import { newMessagePortRpcSession } from 'capnweb'
22
import * as fs from 'node:fs/promises'
33
import * as path from 'node:path'
44
import { fileURLToPath } from 'node:url'
55
import { parseArgs, styleText } from 'node:util'
66
import { create_browser_runtime } from './browser-runtime.ts'
77
import { handle_coverage, type Coverage } from './coverage.ts'
8-
import * as devalue from './devalue.ts'
98
import { create_node_runtime } from './node-runtime.ts'
109
import type { ClientFunctions, TestResult } from './runtime.ts'
1110

@@ -45,7 +44,8 @@ for (const [runtime, files] of Object.entries(all_files)) {
4544
const rt = runtime === 'node' ? await create_node_runtime() : await create_browser_runtime()
4645
await using _ = rt // workaround for https://issues.chromium.org/issues/409478039
4746

48-
const client = createBirpc<ClientFunctions, ServerFunctions>(
47+
using client = newMessagePortRpcSession<ClientFunctions>(
48+
rt.port,
4949
{
5050
report_result(run) {
5151
if (run.result === 'pass') {
@@ -58,15 +58,8 @@ for (const [runtime, files] of Object.entries(all_files)) {
5858

5959
results.push(run)
6060
},
61-
},
62-
{
63-
post: data => rt.port.postMessage(data),
64-
on: fn => {
65-
rt.port.onmessage = e => fn(e.data)
66-
},
67-
serialize: devalue.stringify,
68-
deserialize: devalue.parse,
69-
},
61+
} satisfies ServerFunctions,
62+
{ onSendError: e => e },
7063
)
7164

7265
await client.define('__DEV__', !args.values.prod)

scripts/test/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
"@types/istanbul-reports": "^3.0.4",
99
"amaro": "^1.1.0",
1010
"ast-v8-to-istanbul": "^0.3.3",
11-
"birpc": "^2.5.0",
12-
"devalue": "^5.1.1",
11+
"capnweb": "^0.1.0",
1312
"hono": "^4.8.5",
1413
"istanbul-lib-coverage": "^3.2.2",
1514
"istanbul-lib-report": "^3.0.1",

scripts/test/runtime.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { createBirpc } from 'birpc'
1+
import { newMessagePortRpcSession, RpcStub } from 'capnweb'
22
import * as mitata from 'mitata'
3-
import * as devalue from './devalue.ts'
43
import type { ServerFunctions } from './main.ts'
54

65
export type TestResult = { name: string } & ({ result: 'pass'; duration: number } | { result: 'fail'; reason: unknown })
@@ -17,8 +16,8 @@ const client: ClientFunctions = {
1716
define(name, value) {
1817
globalThis[name] = value
1918
},
20-
import(path) {
21-
return import(path)
19+
async import(path) {
20+
return new RpcStub((await import(path)) as object)
2221
},
2322
async run_tests(options) {
2423
for (const test of tests) {
@@ -51,21 +50,15 @@ declare global {
5150
var __postMessage: (data: any) => void
5251
}
5352

54-
const server = createBirpc<ServerFunctions, ClientFunctions>(
55-
client,
56-
typeof process === 'undefined'
57-
? {
58-
post: window.__postMessage,
59-
on: fn => (window.__onmessage = fn),
60-
serialize: devalue.stringify,
61-
deserialize: devalue.parse,
62-
}
63-
: {
64-
post: data => process.send!(data),
65-
on: fn => process.on('message', fn),
66-
serialize: devalue.stringify,
67-
deserialize: devalue.parse,
68-
},
69-
)
53+
const { port1, port2 } = new MessageChannel()
54+
if (typeof process === 'undefined') {
55+
window.__onmessage = data => port1.postMessage(data)
56+
port1.onmessage = event => window.__postMessage(event.data)
57+
} else {
58+
process.on('message', data => port1.postMessage(data))
59+
port1.onmessage = event => process.send!(event.data)
60+
}
61+
62+
const server = newMessagePortRpcSession<ServerFunctions>(port2, client, { onSendError: e => e })
7063

7164
export const tests: Array<{ name: string; fn: () => void | Promise<void> }> = []

0 commit comments

Comments
 (0)