| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,15 +1,5 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | - const { getOptionValue } = require('internal/options'); | ||
| 4 | - if (getOptionValue('--pending-deprecation')){ | ||
| 5 | - process.emitWarning( | ||
| 6 | - 'The `punycode` module is deprecated. Please use a userland ' + | ||
| 7 | - 'alternative instead.', | ||
| 8 | - 'DeprecationWarning', | ||
| 9 | - 'DEP0040', | ||
| 10 | - ); | ||
| 11 | - } | ||
| 12 | - | ||
| 13 | 3 | /** Highest positive signed 32-bit float value */ | |
| 14 | 4 | const maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1 | |
| 15 | 5 | ||
@@ -25,7 +15,7 @@ const delimiter = '-'; // '\x2D' | |||
| 25 | 15 | ||
| 26 | 16 | /** Regular expressions */ | |
| 27 | 17 | const regexPunycode = /^xn--/; | |
| 28 | - const regexNonASCII = /[^\0-\x7E]/; // non-ASCII chars | ||
| 18 | + const regexNonASCII = /[^\0-\x7F]/; // Note: U+007F DEL is excluded too. | ||
| 29 | 19 | const regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g; // RFC 3490 separators | |
| 30 | 20 | ||
| 31 | 21 | /** Error messages */ | |
@@ -60,11 +50,11 @@ function error(type) { | |||
| 60 | 50 | * item. | |
| 61 | 51 | * @returns {Array} A new array of values returned by the callback function. | |
| 62 | 52 | */ | |
| 63 | - function map(array, fn) { | ||
| 53 | + function map(array, callback) { | ||
| 64 | 54 | const result = []; | |
| 65 | 55 | let length = array.length; | |
| 66 | 56 | while (length--) { | |
| 67 | - result[length] = fn(array[length]); | ||
| 57 | + result[length] = callback(array[length]); | ||
| 68 | 58 | } | |
| 69 | 59 | return result; | |
| 70 | 60 | } | |
@@ -76,22 +66,22 @@ function map(array, fn) { | |||
| 76 | 66 | * @param {String} domain The domain name or email address. | |
| 77 | 67 | * @param {Function} callback The function that gets called for every | |
| 78 | 68 | * character. | |
| 79 | - * @returns {Array} A new string of characters returned by the callback | ||
| 69 | + * @returns {String} A new string of characters returned by the callback | ||
| 80 | 70 | * function. | |
| 81 | 71 | */ | |
| 82 | - function mapDomain(string, fn) { | ||
| 83 | - const parts = string.split('@'); | ||
| 72 | + function mapDomain(domain, callback) { | ||
| 73 | + const parts = domain.split('@'); | ||
| 84 | 74 | let result = ''; | |
| 85 | 75 | if (parts.length > 1) { | |
| 86 | 76 | // In email addresses, only the domain name should be punycoded. Leave | |
| 87 | 77 | // the local part (i.e. everything up to `@`) intact. | |
| 88 | 78 | result = parts[0] + '@'; | |
| 89 | - string = parts[1]; | ||
| 79 | + domain = parts[1]; | ||
| 90 | 80 | } | |
| 91 | 81 | // Avoid `split(regex)` for IE8 compatibility. See #17. | |
| 92 | - string = string.replace(regexSeparators, '\x2E'); | ||
| 93 | - const labels = string.split('.'); | ||
| 94 | - const encoded = map(labels, fn).join('.'); | ||
| 82 | + domain = domain.replace(regexSeparators, '\x2E'); | ||
| 83 | + const labels = domain.split('.'); | ||
| 84 | + const encoded = map(labels, callback).join('.'); | ||
| 95 | 85 | return result + encoded; | |
| 96 | 86 | } | |
| 97 | 87 | ||
@@ -140,7 +130,7 @@ function ucs2decode(string) { | |||
| 140 | 130 | * @param {Array} codePoints The array of numeric code points. | |
| 141 | 131 | * @returns {String} The new Unicode string (UCS-2). | |
| 142 | 132 | */ | |
| 143 | - const ucs2encode = array => String.fromCodePoint(...array); | ||
| 133 | + const ucs2encode = codePoints => String.fromCodePoint(...codePoints); | ||
| 144 | 134 | ||
| 145 | 135 | /** | |
| 146 | 136 | * Converts a basic code point into a digit/integer. | |
@@ -152,13 +142,13 @@ const ucs2encode = array => String.fromCodePoint(...array); | |||
| 152 | 142 | * the code point does not represent a value. | |
| 153 | 143 | */ | |
| 154 | 144 | const basicToDigit = function(codePoint) { | |
| 155 | - if (codePoint - 0x30 < 0x0A) { | ||
| 156 | - return codePoint - 0x16; | ||
| 145 | + if (codePoint >= 0x30 && codePoint < 0x3A) { | ||
| 146 | + return 26 + (codePoint - 0x30); | ||
| 157 | 147 | } | |
| 158 | - if (codePoint - 0x41 < 0x1A) { | ||
| 148 | + if (codePoint >= 0x41 && codePoint < 0x5B) { | ||
| 159 | 149 | return codePoint - 0x41; | |
| 160 | 150 | } | |
| 161 | - if (codePoint - 0x61 < 0x1A) { | ||
| 151 | + if (codePoint >= 0x61 && codePoint < 0x7B) { | ||
| 162 | 152 | return codePoint - 0x61; | |
| 163 | 153 | } | |
| 164 | 154 | return base; | |
@@ -238,7 +228,7 @@ const decode = function(input) { | |||
| 238 | 228 | // which gets added to `i`. The overflow checking is easier | |
| 239 | 229 | // if we increase `i` as we go, then subtract off its starting | |
| 240 | 230 | // value at the end to obtain `delta`. | |
| 241 | - let oldi = i; | ||
| 231 | + const oldi = i; | ||
| 242 | 232 | for (let w = 1, k = base; /* no condition */; k += base) { | |
| 243 | 233 | ||
| 244 | 234 | if (index >= inputLength) { | |
@@ -247,7 +237,10 @@ const decode = function(input) { | |||
| 247 | 237 | ||
| 248 | 238 | const digit = basicToDigit(input.charCodeAt(index++)); | |
| 249 | 239 | ||
| 250 | - if (digit >= base || digit > floor((maxInt - i) / w)) { | ||
| 240 | + if (digit >= base) { | ||
| 241 | + error('invalid-input'); | ||
| 242 | + } | ||
| 243 | + if (digit > floor((maxInt - i) / w)) { | ||
| 251 | 244 | error('overflow'); | |
| 252 | 245 | } | |
| 253 | 246 | ||
@@ -301,7 +294,7 @@ const encode = function(input) { | |||
| 301 | 294 | input = ucs2decode(input); | |
| 302 | 295 | ||
| 303 | 296 | // Cache the length. | |
| 304 | - let inputLength = input.length; | ||
| 297 | + const inputLength = input.length; | ||
| 305 | 298 | ||
| 306 | 299 | // Initialize the state. | |
| 307 | 300 | let n = initialN; | |
@@ -315,7 +308,7 @@ const encode = function(input) { | |||
| 315 | 308 | } | |
| 316 | 309 | } | |
| 317 | 310 | ||
| 318 | - let basicLength = output.length; | ||
| 311 | + const basicLength = output.length; | ||
| 319 | 312 | let handledCPCount = basicLength; | |
| 320 | 313 | ||
| 321 | 314 | // `handledCPCount` is the number of code points that have been handled; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -63,7 +63,7 @@ assert.throws(() => { | |||
| 63 | 63 | }, /^RangeError: Illegal input >= 0x80 \(not a basic code point\)$/); | |
| 64 | 64 | assert.throws(() => { | |
| 65 | 65 | punycode.decode('あ'); | |
| 66 | - }, /^RangeError: Overflow: input needs wider integers to process$/); | ||
| 66 | + }, /^RangeError: Invalid input$/); | ||
| 67 | 67 | ||
| 68 | 68 | // http://tools.ietf.org/html/rfc3492#section-7.1 | |
| 69 | 69 | const tests = [ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments