Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: throw error in structuredClone when no arguments are passed #41651

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/internal/structured_clone.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
'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();
Expand All @@ -17,5 +25,5 @@ function structuredClone(value, options = undefined) {
}

module.exports = {
structuredClone
structuredClone,
};
8 changes: 6 additions & 2 deletions test/parallel/test-structuredClone-global.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
require('../common');

const {
structuredClone: _structuredClone
structuredClone: _structuredClone,
} = require('internal/structured_clone');

const {
strictEqual
strictEqual,
throws,
} = require('assert');

strictEqual(globalThis.structuredClone, _structuredClone);
Expand All @@ -17,3 +19,5 @@ strictEqual(globalThis.structuredClone, undefined);

// Restore the value for the known globals check.
structuredClone = _structuredClone;

throws(() => _structuredClone(), /ERR_MISSING_ARGS/);