11'use strict' ;
22
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-
133/** Highest positive signed 32-bit float value */
144const maxInt = 2147483647 ; // aka. 0x7FFFFFFF or 2^31-1
155
@@ -25,7 +15,7 @@ const delimiter = '-'; // '\x2D'
2515
2616/** Regular expressions */
2717const regexPunycode = / ^ x n - - / ;
28- const regexNonASCII = / [ ^ \0 - \x7E ] / ; // non-ASCII chars
18+ const regexNonASCII = / [ ^ \0 - \x7F ] / ; // Note: U+007F DEL is excluded too.
2919const regexSeparators = / [ \x2E \u3002 \uFF0E \uFF61 ] / g; // RFC 3490 separators
3020
3121/** Error messages */
@@ -60,11 +50,11 @@ function error(type) {
6050 * item.
6151 * @returns {Array } A new array of values returned by the callback function.
6252 */
63- function map ( array , fn ) {
53+ function map ( array , callback ) {
6454 const result = [ ] ;
6555 let length = array . length ;
6656 while ( length -- ) {
67- result [ length ] = fn ( array [ length ] ) ;
57+ result [ length ] = callback ( array [ length ] ) ;
6858 }
6959 return result ;
7060}
@@ -76,22 +66,22 @@ function map(array, fn) {
7666 * @param {String } domain The domain name or email address.
7767 * @param {Function } callback The function that gets called for every
7868 * 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
8070 * function.
8171 */
82- function mapDomain ( string , fn ) {
83- const parts = string . split ( '@' ) ;
72+ function mapDomain ( domain , callback ) {
73+ const parts = domain . split ( '@' ) ;
8474 let result = '' ;
8575 if ( parts . length > 1 ) {
8676 // In email addresses, only the domain name should be punycoded. Leave
8777 // the local part (i.e. everything up to `@`) intact.
8878 result = parts [ 0 ] + '@' ;
89- string = parts [ 1 ] ;
79+ domain = parts [ 1 ] ;
9080 }
9181 // 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 ( '.' ) ;
9585 return result + encoded ;
9686}
9787
@@ -140,7 +130,7 @@ function ucs2decode(string) {
140130 * @param {Array } codePoints The array of numeric code points.
141131 * @returns {String } The new Unicode string (UCS-2).
142132 */
143- const ucs2encode = array => String . fromCodePoint ( ...array ) ;
133+ const ucs2encode = codePoints => String . fromCodePoint ( ...codePoints ) ;
144134
145135/**
146136 * Converts a basic code point into a digit/integer.
@@ -152,13 +142,13 @@ const ucs2encode = array => String.fromCodePoint(...array);
152142 * the code point does not represent a value.
153143 */
154144const basicToDigit = function ( codePoint ) {
155- if ( codePoint - 0x30 < 0x0A ) {
156- return codePoint - 0x16 ;
145+ if ( codePoint >= 0x30 && codePoint < 0x3A ) {
146+ return 26 + ( codePoint - 0x30 ) ;
157147 }
158- if ( codePoint - 0x41 < 0x1A ) {
148+ if ( codePoint >= 0x41 && codePoint < 0x5B ) {
159149 return codePoint - 0x41 ;
160150 }
161- if ( codePoint - 0x61 < 0x1A ) {
151+ if ( codePoint >= 0x61 && codePoint < 0x7B ) {
162152 return codePoint - 0x61 ;
163153 }
164154 return base ;
@@ -238,7 +228,7 @@ const decode = function(input) {
238228 // which gets added to `i`. The overflow checking is easier
239229 // if we increase `i` as we go, then subtract off its starting
240230 // value at the end to obtain `delta`.
241- let oldi = i ;
231+ const oldi = i ;
242232 for ( let w = 1 , k = base ; /* no condition */ ; k += base ) {
243233
244234 if ( index >= inputLength ) {
@@ -247,7 +237,10 @@ const decode = function(input) {
247237
248238 const digit = basicToDigit ( input . charCodeAt ( index ++ ) ) ;
249239
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 ) ) {
251244 error ( 'overflow' ) ;
252245 }
253246
@@ -301,7 +294,7 @@ const encode = function(input) {
301294 input = ucs2decode ( input ) ;
302295
303296 // Cache the length.
304- let inputLength = input . length ;
297+ const inputLength = input . length ;
305298
306299 // Initialize the state.
307300 let n = initialN ;
@@ -315,7 +308,7 @@ const encode = function(input) {
315308 }
316309 }
317310
318- let basicLength = output . length ;
311+ const basicLength = output . length ;
319312 let handledCPCount = basicLength ;
320313
321314 // `handledCPCount` is the number of code points that have been handled;
0 commit comments