Skip to content

Commit f124f4d

Browse files
committed
Fix non-clonable objects not handled properly since node 22
1 parent b42ec93 commit f124f4d

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

promises/worker.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@ parentPort.on("message", ({ id, isClass, self, method, args }) => {
1515
let res;
1616
if (isClass) res = new self[method](...args.map(getArgValue));
1717
else res = self[method](...args.map(getArgValue));
18-
try {
19-
parentPort.postMessage({ id, res });
20-
} catch (e) {
21-
if (e.name === "DataCloneError") {
22-
registry[id] = res;
23-
parentPort.postMessage({ id, res: { _id: id } });
24-
} else {
25-
throw e;
26-
}
18+
if (isTransferable(res)) parentPort.postMessage({ id, res });
19+
else {
20+
registry[id] = res;
21+
parentPort.postMessage({ id, res: { _id: id } });
2722
}
2823
} catch (err) {
2924
parentPort.postMessage({ id, err });
@@ -34,3 +29,11 @@ parentPort.postMessage(null);
3429
function getArgValue(a) {
3530
return registry[a?._id] || a;
3631
}
32+
function isTransferable(obj) {
33+
return !(
34+
obj != null &&
35+
typeof obj === "object" &&
36+
obj.constructor !== Object &&
37+
obj.constructor !== Array
38+
);
39+
}

0 commit comments

Comments
 (0)