Skip to content

Commit 372de54

Browse files
Copilotkalwalt
andcommitted
fix(worker): prevent CORS error by checking environment before importing node:worker_threads
Only attempt to import node:worker_threads when in Node.js environment (typeof self === 'undefined'). In browser environments, skip the import entirely to avoid CORS policy violations. Co-authored-by: kalwalt <[email protected]>
1 parent 66dd809 commit 372de54

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/worker/worker.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ let parent = null;
88
let arController = null;
99
let arControllerInitialized = false;
1010

11-
try {
12-
const wt = await import('node:worker_threads').catch(() => null);
13-
if (wt && wt.parentPort) {
14-
isNodeWorker = true;
15-
parent = wt.parentPort;
11+
// Detect environment and setup worker communication
12+
// Only try to import node:worker_threads if we're in a Node.js environment
13+
if (typeof self === 'undefined') {
14+
// We're in Node.js (no 'self' global)
15+
try {
16+
const wt = await import('node:worker_threads').catch(() => null);
17+
if (wt && wt.parentPort) {
18+
isNodeWorker = true;
19+
parent = wt.parentPort;
20+
}
21+
} catch (e) {
22+
// Fallback: not in worker_threads context
23+
isNodeWorker = false;
24+
parent = null;
1625
}
17-
} catch (e) {
18-
isNodeWorker = false;
19-
parent = null;
2026
}
2127

2228
function onMessage(fn) {

0 commit comments

Comments
 (0)