mirror of
https://github.com/nodejs/node.git
synced 2025-05-03 07:36:51 +00:00

PR-URL: https://github.com/nodejs/node/pull/41651 Fixes: https://github.com/nodejs/node/issues/41450 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
30 lines
661 B
JavaScript
30 lines
661 B
JavaScript
'use strict';
|
|
|
|
const {
|
|
codes: { ERR_MISSING_ARGS },
|
|
} = require('internal/errors');
|
|
|
|
const {
|
|
MessageChannel,
|
|
receiveMessageOnPort,
|
|
} = require('internal/worker/io');
|
|
|
|
let channel;
|
|
function structuredClone(value, options = undefined) {
|
|
if (arguments.length === 0) {
|
|
throw new ERR_MISSING_ARGS('value');
|
|
}
|
|
|
|
// TODO: Improve this with a more efficient solution that avoids
|
|
// instantiating a MessageChannel
|
|
channel ??= new MessageChannel();
|
|
channel.port1.unref();
|
|
channel.port2.unref();
|
|
channel.port1.postMessage(value, options?.transfer);
|
|
return receiveMessageOnPort(channel.port2).message;
|
|
}
|
|
|
|
module.exports = {
|
|
structuredClone,
|
|
};
|