Skip to content

Commit 3fbc172

Browse files
VoltrexKeyvadanielleadams
authored andcommitted
lib: make structuredClone spec compliant
Fixes: #40246 PR-URL: #40251 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 416c60f commit 3fbc172

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lib/internal/structured_clone.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const {
4+
MessageChannel,
5+
receiveMessageOnPort,
6+
} = require('internal/worker/io');
7+
8+
let channel;
9+
function structuredClone(value, options = undefined) {
10+
// TODO: Improve this with a more efficient solution that avoids
11+
// instantiating a MessageChannel
12+
channel ??= new MessageChannel();
13+
channel.port1.unref();
14+
channel.port2.unref();
15+
channel.port1.postMessage(value, options?.transfer);
16+
return receiveMessageOnPort(channel.port2).message;
17+
}
18+
19+
module.exports = {
20+
structuredClone
21+
};

0 commit comments

Comments
 (0)