From 8ab0540d4a2c4e23d68783014b598768a1905884 Mon Sep 17 00:00:00 2001 From: Giora Guttsait Date: Tue, 25 Jan 2022 01:17:37 +0200 Subject: [PATCH] lib: throw error in structuedClone when no arguments are passed PR-URL: https://github.com/nodejs/node/pull/41651 Fixes: https://github.com/nodejs/node/issues/41450 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Mestery Reviewed-By: Darshan Sen --- lib/internal/structured_clone.js | 10 +++++++++- test/parallel/test-structuredClone-global.js | 8 ++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/internal/structured_clone.js b/lib/internal/structured_clone.js index 2ee9517284ab6e..0392232badf9fc 100644 --- a/lib/internal/structured_clone.js +++ b/lib/internal/structured_clone.js @@ -1,5 +1,9 @@ 'use strict'; +const { + codes: { ERR_MISSING_ARGS }, +} = require('internal/errors'); + const { MessageChannel, receiveMessageOnPort, @@ -7,6 +11,10 @@ const { 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(); @@ -17,5 +25,5 @@ function structuredClone(value, options = undefined) { } module.exports = { - structuredClone + structuredClone, }; diff --git a/test/parallel/test-structuredClone-global.js b/test/parallel/test-structuredClone-global.js index ae6120c04e0005..95dab1e8e8963b 100644 --- a/test/parallel/test-structuredClone-global.js +++ b/test/parallel/test-structuredClone-global.js @@ -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); @@ -17,3 +19,5 @@ strictEqual(globalThis.structuredClone, undefined); // Restore the value for the known globals check. structuredClone = _structuredClone; + +throws(() => _structuredClone(), /ERR_MISSING_ARGS/);