11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst { getOptionValue } = require('internal/options'); 41cb0ef41Sopenharmony_ciif (getOptionValue('--pending-deprecation')){ 51cb0ef41Sopenharmony_ci process.emitWarning( 61cb0ef41Sopenharmony_ci 'The `punycode` module is deprecated. Please use a userland ' + 71cb0ef41Sopenharmony_ci 'alternative instead.', 81cb0ef41Sopenharmony_ci 'DeprecationWarning', 91cb0ef41Sopenharmony_ci 'DEP0040', 101cb0ef41Sopenharmony_ci ); 111cb0ef41Sopenharmony_ci} 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ci/** Highest positive signed 32-bit float value */ 141cb0ef41Sopenharmony_ciconst maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci/** Bootstring parameters */ 171cb0ef41Sopenharmony_ciconst base = 36; 181cb0ef41Sopenharmony_ciconst tMin = 1; 191cb0ef41Sopenharmony_ciconst tMax = 26; 201cb0ef41Sopenharmony_ciconst skew = 38; 211cb0ef41Sopenharmony_ciconst damp = 700; 221cb0ef41Sopenharmony_ciconst initialBias = 72; 231cb0ef41Sopenharmony_ciconst initialN = 128; // 0x80 241cb0ef41Sopenharmony_ciconst delimiter = '-'; // '\x2D' 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci/** Regular expressions */ 271cb0ef41Sopenharmony_ciconst regexPunycode = /^xn--/; 281cb0ef41Sopenharmony_ciconst regexNonASCII = /[^\0-\x7F]/; // Note: U+007F DEL is excluded too. 291cb0ef41Sopenharmony_ciconst regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g; // RFC 3490 separators 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci/** Error messages */ 321cb0ef41Sopenharmony_ciconst errors = { 331cb0ef41Sopenharmony_ci 'overflow': 'Overflow: input needs wider integers to process', 341cb0ef41Sopenharmony_ci 'not-basic': 'Illegal input >= 0x80 (not a basic code point)', 351cb0ef41Sopenharmony_ci 'invalid-input': 'Invalid input' 361cb0ef41Sopenharmony_ci}; 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci/** Convenience shortcuts */ 391cb0ef41Sopenharmony_ciconst baseMinusTMin = base - tMin; 401cb0ef41Sopenharmony_ciconst floor = Math.floor; 411cb0ef41Sopenharmony_ciconst stringFromCharCode = String.fromCharCode; 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci/*--------------------------------------------------------------------------*/ 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci/** 461cb0ef41Sopenharmony_ci * A generic error utility function. 471cb0ef41Sopenharmony_ci * @private 481cb0ef41Sopenharmony_ci * @param {String} type The error type. 491cb0ef41Sopenharmony_ci * @returns {Error} Throws a `RangeError` with the applicable error message. 501cb0ef41Sopenharmony_ci */ 511cb0ef41Sopenharmony_cifunction error(type) { 521cb0ef41Sopenharmony_ci throw new RangeError(errors[type]); 531cb0ef41Sopenharmony_ci} 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci/** 561cb0ef41Sopenharmony_ci * A generic `Array#map` utility function. 571cb0ef41Sopenharmony_ci * @private 581cb0ef41Sopenharmony_ci * @param {Array} array The array to iterate over. 591cb0ef41Sopenharmony_ci * @param {Function} callback The function that gets called for every array 601cb0ef41Sopenharmony_ci * item. 611cb0ef41Sopenharmony_ci * @returns {Array} A new array of values returned by the callback function. 621cb0ef41Sopenharmony_ci */ 631cb0ef41Sopenharmony_cifunction map(array, callback) { 641cb0ef41Sopenharmony_ci const result = []; 651cb0ef41Sopenharmony_ci let length = array.length; 661cb0ef41Sopenharmony_ci while (length--) { 671cb0ef41Sopenharmony_ci result[length] = callback(array[length]); 681cb0ef41Sopenharmony_ci } 691cb0ef41Sopenharmony_ci return result; 701cb0ef41Sopenharmony_ci} 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci/** 731cb0ef41Sopenharmony_ci * A simple `Array#map`-like wrapper to work with domain name strings or email 741cb0ef41Sopenharmony_ci * addresses. 751cb0ef41Sopenharmony_ci * @private 761cb0ef41Sopenharmony_ci * @param {String} domain The domain name or email address. 771cb0ef41Sopenharmony_ci * @param {Function} callback The function that gets called for every 781cb0ef41Sopenharmony_ci * character. 791cb0ef41Sopenharmony_ci * @returns {String} A new string of characters returned by the callback 801cb0ef41Sopenharmony_ci * function. 811cb0ef41Sopenharmony_ci */ 821cb0ef41Sopenharmony_cifunction mapDomain(domain, callback) { 831cb0ef41Sopenharmony_ci const parts = domain.split('@'); 841cb0ef41Sopenharmony_ci let result = ''; 851cb0ef41Sopenharmony_ci if (parts.length > 1) { 861cb0ef41Sopenharmony_ci // In email addresses, only the domain name should be punycoded. Leave 871cb0ef41Sopenharmony_ci // the local part (i.e. everything up to `@`) intact. 881cb0ef41Sopenharmony_ci result = parts[0] + '@'; 891cb0ef41Sopenharmony_ci domain = parts[1]; 901cb0ef41Sopenharmony_ci } 911cb0ef41Sopenharmony_ci // Avoid `split(regex)` for IE8 compatibility. See #17. 921cb0ef41Sopenharmony_ci domain = domain.replace(regexSeparators, '\x2E'); 931cb0ef41Sopenharmony_ci const labels = domain.split('.'); 941cb0ef41Sopenharmony_ci const encoded = map(labels, callback).join('.'); 951cb0ef41Sopenharmony_ci return result + encoded; 961cb0ef41Sopenharmony_ci} 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci/** 991cb0ef41Sopenharmony_ci * Creates an array containing the numeric code points of each Unicode 1001cb0ef41Sopenharmony_ci * character in the string. While JavaScript uses UCS-2 internally, 1011cb0ef41Sopenharmony_ci * this function will convert a pair of surrogate halves (each of which 1021cb0ef41Sopenharmony_ci * UCS-2 exposes as separate characters) into a single code point, 1031cb0ef41Sopenharmony_ci * matching UTF-16. 1041cb0ef41Sopenharmony_ci * @see `punycode.ucs2.encode` 1051cb0ef41Sopenharmony_ci * @see <https://mathiasbynens.be/notes/javascript-encoding> 1061cb0ef41Sopenharmony_ci * @memberOf punycode.ucs2 1071cb0ef41Sopenharmony_ci * @name decode 1081cb0ef41Sopenharmony_ci * @param {String} string The Unicode input string (UCS-2). 1091cb0ef41Sopenharmony_ci * @returns {Array} The new array of code points. 1101cb0ef41Sopenharmony_ci */ 1111cb0ef41Sopenharmony_cifunction ucs2decode(string) { 1121cb0ef41Sopenharmony_ci const output = []; 1131cb0ef41Sopenharmony_ci let counter = 0; 1141cb0ef41Sopenharmony_ci const length = string.length; 1151cb0ef41Sopenharmony_ci while (counter < length) { 1161cb0ef41Sopenharmony_ci const value = string.charCodeAt(counter++); 1171cb0ef41Sopenharmony_ci if (value >= 0xD800 && value <= 0xDBFF && counter < length) { 1181cb0ef41Sopenharmony_ci // It's a high surrogate, and there is a next character. 1191cb0ef41Sopenharmony_ci const extra = string.charCodeAt(counter++); 1201cb0ef41Sopenharmony_ci if ((extra & 0xFC00) == 0xDC00) { // Low surrogate. 1211cb0ef41Sopenharmony_ci output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); 1221cb0ef41Sopenharmony_ci } else { 1231cb0ef41Sopenharmony_ci // It's an unmatched surrogate; only append this code unit, in case the 1241cb0ef41Sopenharmony_ci // next code unit is the high surrogate of a surrogate pair. 1251cb0ef41Sopenharmony_ci output.push(value); 1261cb0ef41Sopenharmony_ci counter--; 1271cb0ef41Sopenharmony_ci } 1281cb0ef41Sopenharmony_ci } else { 1291cb0ef41Sopenharmony_ci output.push(value); 1301cb0ef41Sopenharmony_ci } 1311cb0ef41Sopenharmony_ci } 1321cb0ef41Sopenharmony_ci return output; 1331cb0ef41Sopenharmony_ci} 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci/** 1361cb0ef41Sopenharmony_ci * Creates a string based on an array of numeric code points. 1371cb0ef41Sopenharmony_ci * @see `punycode.ucs2.decode` 1381cb0ef41Sopenharmony_ci * @memberOf punycode.ucs2 1391cb0ef41Sopenharmony_ci * @name encode 1401cb0ef41Sopenharmony_ci * @param {Array} codePoints The array of numeric code points. 1411cb0ef41Sopenharmony_ci * @returns {String} The new Unicode string (UCS-2). 1421cb0ef41Sopenharmony_ci */ 1431cb0ef41Sopenharmony_ciconst ucs2encode = codePoints => String.fromCodePoint(...codePoints); 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ci/** 1461cb0ef41Sopenharmony_ci * Converts a basic code point into a digit/integer. 1471cb0ef41Sopenharmony_ci * @see `digitToBasic()` 1481cb0ef41Sopenharmony_ci * @private 1491cb0ef41Sopenharmony_ci * @param {Number} codePoint The basic numeric code point value. 1501cb0ef41Sopenharmony_ci * @returns {Number} The numeric value of a basic code point (for use in 1511cb0ef41Sopenharmony_ci * representing integers) in the range `0` to `base - 1`, or `base` if 1521cb0ef41Sopenharmony_ci * the code point does not represent a value. 1531cb0ef41Sopenharmony_ci */ 1541cb0ef41Sopenharmony_ciconst basicToDigit = function(codePoint) { 1551cb0ef41Sopenharmony_ci if (codePoint >= 0x30 && codePoint < 0x3A) { 1561cb0ef41Sopenharmony_ci return 26 + (codePoint - 0x30); 1571cb0ef41Sopenharmony_ci } 1581cb0ef41Sopenharmony_ci if (codePoint >= 0x41 && codePoint < 0x5B) { 1591cb0ef41Sopenharmony_ci return codePoint - 0x41; 1601cb0ef41Sopenharmony_ci } 1611cb0ef41Sopenharmony_ci if (codePoint >= 0x61 && codePoint < 0x7B) { 1621cb0ef41Sopenharmony_ci return codePoint - 0x61; 1631cb0ef41Sopenharmony_ci } 1641cb0ef41Sopenharmony_ci return base; 1651cb0ef41Sopenharmony_ci}; 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci/** 1681cb0ef41Sopenharmony_ci * Converts a digit/integer into a basic code point. 1691cb0ef41Sopenharmony_ci * @see `basicToDigit()` 1701cb0ef41Sopenharmony_ci * @private 1711cb0ef41Sopenharmony_ci * @param {Number} digit The numeric value of a basic code point. 1721cb0ef41Sopenharmony_ci * @returns {Number} The basic code point whose value (when used for 1731cb0ef41Sopenharmony_ci * representing integers) is `digit`, which needs to be in the range 1741cb0ef41Sopenharmony_ci * `0` to `base - 1`. If `flag` is non-zero, the uppercase form is 1751cb0ef41Sopenharmony_ci * used; else, the lowercase form is used. The behavior is undefined 1761cb0ef41Sopenharmony_ci * if `flag` is non-zero and `digit` has no uppercase form. 1771cb0ef41Sopenharmony_ci */ 1781cb0ef41Sopenharmony_ciconst digitToBasic = function(digit, flag) { 1791cb0ef41Sopenharmony_ci // 0..25 map to ASCII a..z or A..Z 1801cb0ef41Sopenharmony_ci // 26..35 map to ASCII 0..9 1811cb0ef41Sopenharmony_ci return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5); 1821cb0ef41Sopenharmony_ci}; 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci/** 1851cb0ef41Sopenharmony_ci * Bias adaptation function as per section 3.4 of RFC 3492. 1861cb0ef41Sopenharmony_ci * https://tools.ietf.org/html/rfc3492#section-3.4 1871cb0ef41Sopenharmony_ci * @private 1881cb0ef41Sopenharmony_ci */ 1891cb0ef41Sopenharmony_ciconst adapt = function(delta, numPoints, firstTime) { 1901cb0ef41Sopenharmony_ci let k = 0; 1911cb0ef41Sopenharmony_ci delta = firstTime ? floor(delta / damp) : delta >> 1; 1921cb0ef41Sopenharmony_ci delta += floor(delta / numPoints); 1931cb0ef41Sopenharmony_ci for (/* no initialization */; delta > baseMinusTMin * tMax >> 1; k += base) { 1941cb0ef41Sopenharmony_ci delta = floor(delta / baseMinusTMin); 1951cb0ef41Sopenharmony_ci } 1961cb0ef41Sopenharmony_ci return floor(k + (baseMinusTMin + 1) * delta / (delta + skew)); 1971cb0ef41Sopenharmony_ci}; 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_ci/** 2001cb0ef41Sopenharmony_ci * Converts a Punycode string of ASCII-only symbols to a string of Unicode 2011cb0ef41Sopenharmony_ci * symbols. 2021cb0ef41Sopenharmony_ci * @memberOf punycode 2031cb0ef41Sopenharmony_ci * @param {String} input The Punycode string of ASCII-only symbols. 2041cb0ef41Sopenharmony_ci * @returns {String} The resulting string of Unicode symbols. 2051cb0ef41Sopenharmony_ci */ 2061cb0ef41Sopenharmony_ciconst decode = function(input) { 2071cb0ef41Sopenharmony_ci // Don't use UCS-2. 2081cb0ef41Sopenharmony_ci const output = []; 2091cb0ef41Sopenharmony_ci const inputLength = input.length; 2101cb0ef41Sopenharmony_ci let i = 0; 2111cb0ef41Sopenharmony_ci let n = initialN; 2121cb0ef41Sopenharmony_ci let bias = initialBias; 2131cb0ef41Sopenharmony_ci 2141cb0ef41Sopenharmony_ci // Handle the basic code points: let `basic` be the number of input code 2151cb0ef41Sopenharmony_ci // points before the last delimiter, or `0` if there is none, then copy 2161cb0ef41Sopenharmony_ci // the first basic code points to the output. 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci let basic = input.lastIndexOf(delimiter); 2191cb0ef41Sopenharmony_ci if (basic < 0) { 2201cb0ef41Sopenharmony_ci basic = 0; 2211cb0ef41Sopenharmony_ci } 2221cb0ef41Sopenharmony_ci 2231cb0ef41Sopenharmony_ci for (let j = 0; j < basic; ++j) { 2241cb0ef41Sopenharmony_ci // if it's not a basic code point 2251cb0ef41Sopenharmony_ci if (input.charCodeAt(j) >= 0x80) { 2261cb0ef41Sopenharmony_ci error('not-basic'); 2271cb0ef41Sopenharmony_ci } 2281cb0ef41Sopenharmony_ci output.push(input.charCodeAt(j)); 2291cb0ef41Sopenharmony_ci } 2301cb0ef41Sopenharmony_ci 2311cb0ef41Sopenharmony_ci // Main decoding loop: start just after the last delimiter if any basic code 2321cb0ef41Sopenharmony_ci // points were copied; start at the beginning otherwise. 2331cb0ef41Sopenharmony_ci 2341cb0ef41Sopenharmony_ci for (let index = basic > 0 ? basic + 1 : 0; index < inputLength; /* no final expression */) { 2351cb0ef41Sopenharmony_ci 2361cb0ef41Sopenharmony_ci // `index` is the index of the next character to be consumed. 2371cb0ef41Sopenharmony_ci // Decode a generalized variable-length integer into `delta`, 2381cb0ef41Sopenharmony_ci // which gets added to `i`. The overflow checking is easier 2391cb0ef41Sopenharmony_ci // if we increase `i` as we go, then subtract off its starting 2401cb0ef41Sopenharmony_ci // value at the end to obtain `delta`. 2411cb0ef41Sopenharmony_ci const oldi = i; 2421cb0ef41Sopenharmony_ci for (let w = 1, k = base; /* no condition */; k += base) { 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_ci if (index >= inputLength) { 2451cb0ef41Sopenharmony_ci error('invalid-input'); 2461cb0ef41Sopenharmony_ci } 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ci const digit = basicToDigit(input.charCodeAt(index++)); 2491cb0ef41Sopenharmony_ci 2501cb0ef41Sopenharmony_ci if (digit >= base) { 2511cb0ef41Sopenharmony_ci error('invalid-input'); 2521cb0ef41Sopenharmony_ci } 2531cb0ef41Sopenharmony_ci if (digit > floor((maxInt - i) / w)) { 2541cb0ef41Sopenharmony_ci error('overflow'); 2551cb0ef41Sopenharmony_ci } 2561cb0ef41Sopenharmony_ci 2571cb0ef41Sopenharmony_ci i += digit * w; 2581cb0ef41Sopenharmony_ci const t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias); 2591cb0ef41Sopenharmony_ci 2601cb0ef41Sopenharmony_ci if (digit < t) { 2611cb0ef41Sopenharmony_ci break; 2621cb0ef41Sopenharmony_ci } 2631cb0ef41Sopenharmony_ci 2641cb0ef41Sopenharmony_ci const baseMinusT = base - t; 2651cb0ef41Sopenharmony_ci if (w > floor(maxInt / baseMinusT)) { 2661cb0ef41Sopenharmony_ci error('overflow'); 2671cb0ef41Sopenharmony_ci } 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_ci w *= baseMinusT; 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ci } 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_ci const out = output.length + 1; 2741cb0ef41Sopenharmony_ci bias = adapt(i - oldi, out, oldi == 0); 2751cb0ef41Sopenharmony_ci 2761cb0ef41Sopenharmony_ci // `i` was supposed to wrap around from `out` to `0`, 2771cb0ef41Sopenharmony_ci // incrementing `n` each time, so we'll fix that now: 2781cb0ef41Sopenharmony_ci if (floor(i / out) > maxInt - n) { 2791cb0ef41Sopenharmony_ci error('overflow'); 2801cb0ef41Sopenharmony_ci } 2811cb0ef41Sopenharmony_ci 2821cb0ef41Sopenharmony_ci n += floor(i / out); 2831cb0ef41Sopenharmony_ci i %= out; 2841cb0ef41Sopenharmony_ci 2851cb0ef41Sopenharmony_ci // Insert `n` at position `i` of the output. 2861cb0ef41Sopenharmony_ci output.splice(i++, 0, n); 2871cb0ef41Sopenharmony_ci 2881cb0ef41Sopenharmony_ci } 2891cb0ef41Sopenharmony_ci 2901cb0ef41Sopenharmony_ci return String.fromCodePoint(...output); 2911cb0ef41Sopenharmony_ci}; 2921cb0ef41Sopenharmony_ci 2931cb0ef41Sopenharmony_ci/** 2941cb0ef41Sopenharmony_ci * Converts a string of Unicode symbols (e.g. a domain name label) to a 2951cb0ef41Sopenharmony_ci * Punycode string of ASCII-only symbols. 2961cb0ef41Sopenharmony_ci * @memberOf punycode 2971cb0ef41Sopenharmony_ci * @param {String} input The string of Unicode symbols. 2981cb0ef41Sopenharmony_ci * @returns {String} The resulting Punycode string of ASCII-only symbols. 2991cb0ef41Sopenharmony_ci */ 3001cb0ef41Sopenharmony_ciconst encode = function(input) { 3011cb0ef41Sopenharmony_ci const output = []; 3021cb0ef41Sopenharmony_ci 3031cb0ef41Sopenharmony_ci // Convert the input in UCS-2 to an array of Unicode code points. 3041cb0ef41Sopenharmony_ci input = ucs2decode(input); 3051cb0ef41Sopenharmony_ci 3061cb0ef41Sopenharmony_ci // Cache the length. 3071cb0ef41Sopenharmony_ci const inputLength = input.length; 3081cb0ef41Sopenharmony_ci 3091cb0ef41Sopenharmony_ci // Initialize the state. 3101cb0ef41Sopenharmony_ci let n = initialN; 3111cb0ef41Sopenharmony_ci let delta = 0; 3121cb0ef41Sopenharmony_ci let bias = initialBias; 3131cb0ef41Sopenharmony_ci 3141cb0ef41Sopenharmony_ci // Handle the basic code points. 3151cb0ef41Sopenharmony_ci for (const currentValue of input) { 3161cb0ef41Sopenharmony_ci if (currentValue < 0x80) { 3171cb0ef41Sopenharmony_ci output.push(stringFromCharCode(currentValue)); 3181cb0ef41Sopenharmony_ci } 3191cb0ef41Sopenharmony_ci } 3201cb0ef41Sopenharmony_ci 3211cb0ef41Sopenharmony_ci const basicLength = output.length; 3221cb0ef41Sopenharmony_ci let handledCPCount = basicLength; 3231cb0ef41Sopenharmony_ci 3241cb0ef41Sopenharmony_ci // `handledCPCount` is the number of code points that have been handled; 3251cb0ef41Sopenharmony_ci // `basicLength` is the number of basic code points. 3261cb0ef41Sopenharmony_ci 3271cb0ef41Sopenharmony_ci // Finish the basic string with a delimiter unless it's empty. 3281cb0ef41Sopenharmony_ci if (basicLength) { 3291cb0ef41Sopenharmony_ci output.push(delimiter); 3301cb0ef41Sopenharmony_ci } 3311cb0ef41Sopenharmony_ci 3321cb0ef41Sopenharmony_ci // Main encoding loop: 3331cb0ef41Sopenharmony_ci while (handledCPCount < inputLength) { 3341cb0ef41Sopenharmony_ci 3351cb0ef41Sopenharmony_ci // All non-basic code points < n have been handled already. Find the next 3361cb0ef41Sopenharmony_ci // larger one: 3371cb0ef41Sopenharmony_ci let m = maxInt; 3381cb0ef41Sopenharmony_ci for (const currentValue of input) { 3391cb0ef41Sopenharmony_ci if (currentValue >= n && currentValue < m) { 3401cb0ef41Sopenharmony_ci m = currentValue; 3411cb0ef41Sopenharmony_ci } 3421cb0ef41Sopenharmony_ci } 3431cb0ef41Sopenharmony_ci 3441cb0ef41Sopenharmony_ci // Increase `delta` enough to advance the decoder's <n,i> state to <m,0>, 3451cb0ef41Sopenharmony_ci // but guard against overflow. 3461cb0ef41Sopenharmony_ci const handledCPCountPlusOne = handledCPCount + 1; 3471cb0ef41Sopenharmony_ci if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) { 3481cb0ef41Sopenharmony_ci error('overflow'); 3491cb0ef41Sopenharmony_ci } 3501cb0ef41Sopenharmony_ci 3511cb0ef41Sopenharmony_ci delta += (m - n) * handledCPCountPlusOne; 3521cb0ef41Sopenharmony_ci n = m; 3531cb0ef41Sopenharmony_ci 3541cb0ef41Sopenharmony_ci for (const currentValue of input) { 3551cb0ef41Sopenharmony_ci if (currentValue < n && ++delta > maxInt) { 3561cb0ef41Sopenharmony_ci error('overflow'); 3571cb0ef41Sopenharmony_ci } 3581cb0ef41Sopenharmony_ci if (currentValue === n) { 3591cb0ef41Sopenharmony_ci // Represent delta as a generalized variable-length integer. 3601cb0ef41Sopenharmony_ci let q = delta; 3611cb0ef41Sopenharmony_ci for (let k = base; /* no condition */; k += base) { 3621cb0ef41Sopenharmony_ci const t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias); 3631cb0ef41Sopenharmony_ci if (q < t) { 3641cb0ef41Sopenharmony_ci break; 3651cb0ef41Sopenharmony_ci } 3661cb0ef41Sopenharmony_ci const qMinusT = q - t; 3671cb0ef41Sopenharmony_ci const baseMinusT = base - t; 3681cb0ef41Sopenharmony_ci output.push( 3691cb0ef41Sopenharmony_ci stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0)) 3701cb0ef41Sopenharmony_ci ); 3711cb0ef41Sopenharmony_ci q = floor(qMinusT / baseMinusT); 3721cb0ef41Sopenharmony_ci } 3731cb0ef41Sopenharmony_ci 3741cb0ef41Sopenharmony_ci output.push(stringFromCharCode(digitToBasic(q, 0))); 3751cb0ef41Sopenharmony_ci bias = adapt(delta, handledCPCountPlusOne, handledCPCount === basicLength); 3761cb0ef41Sopenharmony_ci delta = 0; 3771cb0ef41Sopenharmony_ci ++handledCPCount; 3781cb0ef41Sopenharmony_ci } 3791cb0ef41Sopenharmony_ci } 3801cb0ef41Sopenharmony_ci 3811cb0ef41Sopenharmony_ci ++delta; 3821cb0ef41Sopenharmony_ci ++n; 3831cb0ef41Sopenharmony_ci 3841cb0ef41Sopenharmony_ci } 3851cb0ef41Sopenharmony_ci return output.join(''); 3861cb0ef41Sopenharmony_ci}; 3871cb0ef41Sopenharmony_ci 3881cb0ef41Sopenharmony_ci/** 3891cb0ef41Sopenharmony_ci * Converts a Punycode string representing a domain name or an email address 3901cb0ef41Sopenharmony_ci * to Unicode. Only the Punycoded parts of the input will be converted, i.e. 3911cb0ef41Sopenharmony_ci * it doesn't matter if you call it on a string that has already been 3921cb0ef41Sopenharmony_ci * converted to Unicode. 3931cb0ef41Sopenharmony_ci * @memberOf punycode 3941cb0ef41Sopenharmony_ci * @param {String} input The Punycoded domain name or email address to 3951cb0ef41Sopenharmony_ci * convert to Unicode. 3961cb0ef41Sopenharmony_ci * @returns {String} The Unicode representation of the given Punycode 3971cb0ef41Sopenharmony_ci * string. 3981cb0ef41Sopenharmony_ci */ 3991cb0ef41Sopenharmony_ciconst toUnicode = function(input) { 4001cb0ef41Sopenharmony_ci return mapDomain(input, function(string) { 4011cb0ef41Sopenharmony_ci return regexPunycode.test(string) 4021cb0ef41Sopenharmony_ci ? decode(string.slice(4).toLowerCase()) 4031cb0ef41Sopenharmony_ci : string; 4041cb0ef41Sopenharmony_ci }); 4051cb0ef41Sopenharmony_ci}; 4061cb0ef41Sopenharmony_ci 4071cb0ef41Sopenharmony_ci/** 4081cb0ef41Sopenharmony_ci * Converts a Unicode string representing a domain name or an email address to 4091cb0ef41Sopenharmony_ci * Punycode. Only the non-ASCII parts of the domain name will be converted, 4101cb0ef41Sopenharmony_ci * i.e. it doesn't matter if you call it with a domain that's already in 4111cb0ef41Sopenharmony_ci * ASCII. 4121cb0ef41Sopenharmony_ci * @memberOf punycode 4131cb0ef41Sopenharmony_ci * @param {String} input The domain name or email address to convert, as a 4141cb0ef41Sopenharmony_ci * Unicode string. 4151cb0ef41Sopenharmony_ci * @returns {String} The Punycode representation of the given domain name or 4161cb0ef41Sopenharmony_ci * email address. 4171cb0ef41Sopenharmony_ci */ 4181cb0ef41Sopenharmony_ciconst toASCII = function(input) { 4191cb0ef41Sopenharmony_ci return mapDomain(input, function(string) { 4201cb0ef41Sopenharmony_ci return regexNonASCII.test(string) 4211cb0ef41Sopenharmony_ci ? 'xn--' + encode(string) 4221cb0ef41Sopenharmony_ci : string; 4231cb0ef41Sopenharmony_ci }); 4241cb0ef41Sopenharmony_ci}; 4251cb0ef41Sopenharmony_ci 4261cb0ef41Sopenharmony_ci/*--------------------------------------------------------------------------*/ 4271cb0ef41Sopenharmony_ci 4281cb0ef41Sopenharmony_ci/** Define the public API */ 4291cb0ef41Sopenharmony_ciconst punycode = { 4301cb0ef41Sopenharmony_ci /** 4311cb0ef41Sopenharmony_ci * A string representing the current Punycode.js version number. 4321cb0ef41Sopenharmony_ci * @memberOf punycode 4331cb0ef41Sopenharmony_ci * @type String 4341cb0ef41Sopenharmony_ci */ 4351cb0ef41Sopenharmony_ci 'version': '2.1.0', 4361cb0ef41Sopenharmony_ci /** 4371cb0ef41Sopenharmony_ci * An object of methods to convert from JavaScript's internal character 4381cb0ef41Sopenharmony_ci * representation (UCS-2) to Unicode code points, and back. 4391cb0ef41Sopenharmony_ci * @see <https://mathiasbynens.be/notes/javascript-encoding> 4401cb0ef41Sopenharmony_ci * @memberOf punycode 4411cb0ef41Sopenharmony_ci * @type Object 4421cb0ef41Sopenharmony_ci */ 4431cb0ef41Sopenharmony_ci 'ucs2': { 4441cb0ef41Sopenharmony_ci 'decode': ucs2decode, 4451cb0ef41Sopenharmony_ci 'encode': ucs2encode 4461cb0ef41Sopenharmony_ci }, 4471cb0ef41Sopenharmony_ci 'decode': decode, 4481cb0ef41Sopenharmony_ci 'encode': encode, 4491cb0ef41Sopenharmony_ci 'toASCII': toASCII, 4501cb0ef41Sopenharmony_ci 'toUnicode': toUnicode 4511cb0ef41Sopenharmony_ci}; 4521cb0ef41Sopenharmony_ci 4531cb0ef41Sopenharmony_cimodule.exports = punycode; 454