Skip to content

Commit e3f0cb0

Browse files
committed
[search] Test using web workers in node.
See: nodejs/node#43583 (comment)
1 parent c223a53 commit e3f0cb0

3 files changed

Lines changed: 15 additions & 36 deletions

File tree

src/cubing/search/instantiator.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { Worker as NodeWorker } from "node:worker_threads";
2-
import { PortableWorker, wrap } from "@cubing/comlink-everywhere";
1+
import { wrap } from "@cubing/comlink-everywhere";
32
import type { WorkerAPI } from "./inside/api";
43
import { searchOutsideDebugGlobals } from "./outside";
54
import {
@@ -8,7 +7,7 @@ import {
87
searchWorkerURLNewURLImportMetaURL,
98
} from "./worker-workarounds";
109

11-
function wrapAPI(worker: Worker | NodeWorker): WorkerAPI {
10+
function wrapAPI(worker: Worker): WorkerAPI {
1211
return wrap<WorkerAPI>(worker);
1312
}
1413

@@ -18,7 +17,7 @@ async function instantiateModuleWorker(
1817
// biome-ignore lint/suspicious/noAsyncPromiseExecutor: TODO
1918
return new Promise<WorkerAPI>(async (resolve, reject) => {
2019
try {
21-
const worker = new PortableWorker(workerEntryFileURL);
20+
const worker = new Worker(workerEntryFileURL, { type: "module" });
2221

2322
// TODO: Remove this once we can remove the workarounds for lack of `import.meta.resolve(…)` support.
2423
const onFirstMessage = (messageData: string) => {
@@ -36,21 +35,17 @@ async function instantiateModuleWorker(
3635
reject(e);
3736
};
3837

39-
if ("once" in worker /* hack to detect `node` */) {
40-
// We have to use `once` so the `unref()` from `comlink-everywhere` allows the process to quit as expected.
41-
worker.once("message", onFirstMessage);
42-
} else {
43-
worker.addEventListener("error", onError, {
38+
console.log({ worker });
39+
worker.addEventListener("error", onError, {
40+
once: true,
41+
});
42+
worker.addEventListener(
43+
"message",
44+
(e: MessageEvent) => onFirstMessage(e.data),
45+
{
4446
once: true,
45-
});
46-
worker.addEventListener(
47-
"message",
48-
(e: MessageEvent) => onFirstMessage(e.data),
49-
{
50-
once: true,
51-
},
52-
);
53-
}
47+
},
48+
);
5449
} catch (e) {
5550
reject(e);
5651
}

src/cubing/search/worker-workarounds/search-worker-entry.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@ import { exposeAPI } from "./worker-guard";
22

33
if (exposeAPI.expose) {
44
void import("../inside").then(() => {
5-
// Workaround for `node`'
6-
if (globalThis.postMessage) {
7-
globalThis.postMessage("comlink-exposed"); // TODO: remove this
8-
} else {
9-
globalThis.process
10-
.getBuiltinModule("node:worker_threads")
11-
.parentPort?.postMessage("comlink-exposed");
12-
}
5+
globalThis.postMessage("comlink-exposed"); // TODO: remove this
136
});
147
}
158

tsconfig.lib.no-dom.jsonc

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,7 @@
22
"schema": "https://json.schemastore.org/tsconfig",
33
"extends": "./tsconfig.lib.jsonc",
44
"compilerOptions": {
5-
"skipLibCheck": true,
6-
"types": [
7-
// Specifying a deeply nested type file like this is bad practice, but the
8-
// `@types/node` types are constructed well and including this one file
9-
// does a great job of preventing much more complicated workarounds.
10-
//
11-
// Hopefully some day this won't be necessary:
12-
// https://github.com/nodejs/node/issues/43583
13-
"./node_modules/@types/node/worker_threads.d.ts"
14-
]
5+
"skipLibCheck": true
156
},
167
"exclude": [
178
"./**/*.test.dom.ts",

0 commit comments

Comments
 (0)