diff --git a/lib/punycode.js b/lib/punycode.js index 34da3ca5ad13b6..ac32cfd4f49571 100644 --- a/lib/punycode.js +++ b/lib/punycode.js @@ -194,6 +194,9 @@ const adapt = function(delta, numPoints, firstTime) { * @returns {String} The resulting string of Unicode symbols. */ const decode = function(input) { + if (typeof input !== 'string') { + throw new TypeError('Argument must be a string'); + } // Don't use UCS-2. const output = []; const inputLength = input.length; @@ -285,6 +288,9 @@ const decode = function(input) { * @returns {String} The resulting Punycode string of ASCII-only symbols. */ const encode = function(input) { + if (input === undefined || input === null) { + throw new TypeError('Argument must not be "undefined" and "null"'); + } const output = []; // Convert the input in UCS-2 to an array of Unicode code points. diff --git a/test/parallel/test-punycode.js b/test/parallel/test-punycode.js index 60175557042ff9..1edb3aded00118 100644 --- a/test/parallel/test-punycode.js +++ b/test/parallel/test-punycode.js @@ -13,6 +13,12 @@ assert.strictEqual( 'Willst du die Blthe des frhen, die Frchte des spteren Jahres-x9e96lkal' ); assert.strictEqual(punycode.encode('日本語'), 'wgv71a119e'); +assert.throws(() => { + punycode.encode(); +}, /^TypeError: Argument must not be "undefined" and "null"$/); +assert.throws(() => { + punycode.encode(null); +}, /^TypeError: Argument must not be "undefined" and "null"$/); assert.strictEqual(punycode.decode('tda'), 'ü'); assert.strictEqual(punycode.decode('Goethe-'), 'Goethe'); @@ -25,14 +31,20 @@ assert.strictEqual( ); assert.strictEqual(punycode.decode('wgv71a119e'), '日本語'); assert.throws(() => { - punycode.decode(' '); -}, /^RangeError: Invalid input$/); + punycode.decode(); +}, /^TypeError: Argument must be a string$/); +assert.throws(() => { + punycode.decode([]); +}, /^TypeError: Argument must be a string$/); assert.throws(() => { punycode.decode('α-'); }, /^RangeError: Illegal input >= 0x80 \(not a basic code point\)$/); assert.throws(() => { punycode.decode('あ'); }, /^RangeError: Overflow: input needs wider integers to process$/); +assert.throws(() => { + punycode.decode(' '); +}, /^RangeError: Invalid input$/); // http://tools.ietf.org/html/rfc3492#section-7.1 const tests = [