From ae309ee18a4a0eebee20d97a7f9d942738459c21 Mon Sep 17 00:00:00 2001 From: voltrexmaster Date: Tue, 28 Sep 2021 21:41:56 -0700 Subject: [PATCH] lib: make structuredClone spec compliant MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/nodejs/node/issues/40246 PR-URL: https://github.com/nodejs/node/pull/40251 Reviewed-By: James M Snell Reviewed-By: Michaƫl Zasso Reviewed-By: Benjamin Gruenbaum Reviewed-By: Antoine du Hamel --- lib/internal/structured_clone.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 lib/internal/structured_clone.js diff --git a/lib/internal/structured_clone.js b/lib/internal/structured_clone.js new file mode 100644 index 00000000000000..2ee9517284ab6e --- /dev/null +++ b/lib/internal/structured_clone.js @@ -0,0 +1,21 @@ +'use strict'; + +const { + MessageChannel, + receiveMessageOnPort, +} = require('internal/worker/io'); + +let channel; +function structuredClone(value, options = undefined) { + // 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 +};