Skip to content

Commit

Permalink
lib: update punycode to 2.3.0
Browse files Browse the repository at this point in the history
PR-URL: #46719
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
  • Loading branch information
anonrig authored and nodejs-github-bot committed Feb 21, 2023
1 parent d1b29b4 commit be4f012
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
51 changes: 22 additions & 29 deletions lib/punycode.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
'use strict';

const { getOptionValue } = require('internal/options');
if (getOptionValue('--pending-deprecation')){
process.emitWarning(
'The `punycode` module is deprecated. Please use a userland ' +
'alternative instead.',
'DeprecationWarning',
'DEP0040',
);
}

/** Highest positive signed 32-bit float value */
const maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1

Expand All @@ -25,7 +15,7 @@ const delimiter = '-'; // '\x2D'

/** Regular expressions */
const regexPunycode = /^xn--/;
const regexNonASCII = /[^\0-\x7E]/; // non-ASCII chars
const regexNonASCII = /[^\0-\x7F]/; // Note: U+007F DEL is excluded too.
const regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g; // RFC 3490 separators

/** Error messages */
Expand Down Expand Up @@ -60,11 +50,11 @@ function error(type) {
* item.
* @returns {Array} A new array of values returned by the callback function.
*/
function map(array, fn) {
function map(array, callback) {
const result = [];
let length = array.length;
while (length--) {
result[length] = fn(array[length]);
result[length] = callback(array[length]);
}
return result;
}
Expand All @@ -76,22 +66,22 @@ function map(array, fn) {
* @param {String} domain The domain name or email address.
* @param {Function} callback The function that gets called for every
* character.
* @returns {Array} A new string of characters returned by the callback
* @returns {String} A new string of characters returned by the callback
* function.
*/
function mapDomain(string, fn) {
const parts = string.split('@');
function mapDomain(domain, callback) {
const parts = domain.split('@');
let result = '';
if (parts.length > 1) {
// In email addresses, only the domain name should be punycoded. Leave
// the local part (i.e. everything up to `@`) intact.
result = parts[0] + '@';
string = parts[1];
domain = parts[1];
}
// Avoid `split(regex)` for IE8 compatibility. See #17.
string = string.replace(regexSeparators, '\x2E');
const labels = string.split('.');
const encoded = map(labels, fn).join('.');
domain = domain.replace(regexSeparators, '\x2E');
const labels = domain.split('.');
const encoded = map(labels, callback).join('.');
return result + encoded;
}

Expand Down Expand Up @@ -140,7 +130,7 @@ function ucs2decode(string) {
* @param {Array} codePoints The array of numeric code points.
* @returns {String} The new Unicode string (UCS-2).
*/
const ucs2encode = array => String.fromCodePoint(...array);
const ucs2encode = codePoints => String.fromCodePoint(...codePoints);

/**
* Converts a basic code point into a digit/integer.
Expand All @@ -152,13 +142,13 @@ const ucs2encode = array => String.fromCodePoint(...array);
* the code point does not represent a value.
*/
const basicToDigit = function(codePoint) {
if (codePoint - 0x30 < 0x0A) {
return codePoint - 0x16;
if (codePoint >= 0x30 && codePoint < 0x3A) {
return 26 + (codePoint - 0x30);
}
if (codePoint - 0x41 < 0x1A) {
if (codePoint >= 0x41 && codePoint < 0x5B) {
return codePoint - 0x41;
}
if (codePoint - 0x61 < 0x1A) {
if (codePoint >= 0x61 && codePoint < 0x7B) {
return codePoint - 0x61;
}
return base;
Expand Down Expand Up @@ -238,7 +228,7 @@ const decode = function(input) {
// which gets added to `i`. The overflow checking is easier
// if we increase `i` as we go, then subtract off its starting
// value at the end to obtain `delta`.
let oldi = i;
const oldi = i;
for (let w = 1, k = base; /* no condition */; k += base) {

if (index >= inputLength) {
Expand All @@ -247,7 +237,10 @@ const decode = function(input) {

const digit = basicToDigit(input.charCodeAt(index++));

if (digit >= base || digit > floor((maxInt - i) / w)) {
if (digit >= base) {
error('invalid-input');
}
if (digit > floor((maxInt - i) / w)) {
error('overflow');
}

Expand Down Expand Up @@ -301,7 +294,7 @@ const encode = function(input) {
input = ucs2decode(input);

// Cache the length.
let inputLength = input.length;
const inputLength = input.length;

// Initialize the state.
let n = initialN;
Expand All @@ -315,7 +308,7 @@ const encode = function(input) {
}
}

let basicLength = output.length;
const basicLength = output.length;
let handledCPCount = basicLength;

// `handledCPCount` is the number of code points that have been handled;
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-punycode.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ assert.throws(() => {
}, /^RangeError: Illegal input >= 0x80 \(not a basic code point\)$/);
assert.throws(() => {
punycode.decode('あ');
}, /^RangeError: Overflow: input needs wider integers to process$/);
}, /^RangeError: Invalid input$/);

// http://tools.ietf.org/html/rfc3492#section-7.1
const tests = [
Expand Down

0 comments on commit be4f012

Please sign in to comment.