Skip to content

Commit

Permalink
lib: throw error in structuedClone when no arguments are passed
Browse files Browse the repository at this point in the history
PR-URL: nodejs#41651
Fixes: nodejs#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>
  • Loading branch information
gioragutt authored and Linkgoron committed Jan 31, 2022
1 parent bdce281 commit 0192c02
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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/);

0 comments on commit 0192c02

Please sign in to comment.