Node-Red configuration
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

string-punycode-to-ascii.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. 'use strict';
  2. // based on https://github.com/bestiejs/punycode.js/blob/master/punycode.js
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1
  5. var base = 36;
  6. var tMin = 1;
  7. var tMax = 26;
  8. var skew = 38;
  9. var damp = 700;
  10. var initialBias = 72;
  11. var initialN = 128; // 0x80
  12. var delimiter = '-'; // '\x2D'
  13. var regexNonASCII = /[^\0-\u007E]/; // non-ASCII chars
  14. var regexSeparators = /[.\u3002\uFF0E\uFF61]/g; // RFC 3490 separators
  15. var OVERFLOW_ERROR = 'Overflow: input needs wider integers to process';
  16. var baseMinusTMin = base - tMin;
  17. var $RangeError = RangeError;
  18. var exec = uncurryThis(regexSeparators.exec);
  19. var floor = Math.floor;
  20. var fromCharCode = String.fromCharCode;
  21. var charCodeAt = uncurryThis(''.charCodeAt);
  22. var join = uncurryThis([].join);
  23. var push = uncurryThis([].push);
  24. var replace = uncurryThis(''.replace);
  25. var split = uncurryThis(''.split);
  26. var toLowerCase = uncurryThis(''.toLowerCase);
  27. /**
  28. * Creates an array containing the numeric code points of each Unicode
  29. * character in the string. While JavaScript uses UCS-2 internally,
  30. * this function will convert a pair of surrogate halves (each of which
  31. * UCS-2 exposes as separate characters) into a single code point,
  32. * matching UTF-16.
  33. */
  34. var ucs2decode = function (string) {
  35. var output = [];
  36. var counter = 0;
  37. var length = string.length;
  38. while (counter < length) {
  39. var value = charCodeAt(string, counter++);
  40. if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
  41. // It's a high surrogate, and there is a next character.
  42. var extra = charCodeAt(string, counter++);
  43. if ((extra & 0xFC00) === 0xDC00) { // Low surrogate.
  44. push(output, ((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
  45. } else {
  46. // It's an unmatched surrogate; only append this code unit, in case the
  47. // next code unit is the high surrogate of a surrogate pair.
  48. push(output, value);
  49. counter--;
  50. }
  51. } else {
  52. push(output, value);
  53. }
  54. }
  55. return output;
  56. };
  57. /**
  58. * Converts a digit/integer into a basic code point.
  59. */
  60. var digitToBasic = function (digit) {
  61. // 0..25 map to ASCII a..z or A..Z
  62. // 26..35 map to ASCII 0..9
  63. return digit + 22 + 75 * (digit < 26);
  64. };
  65. /**
  66. * Bias adaptation function as per section 3.4 of RFC 3492.
  67. * https://tools.ietf.org/html/rfc3492#section-3.4
  68. */
  69. var adapt = function (delta, numPoints, firstTime) {
  70. var k = 0;
  71. delta = firstTime ? floor(delta / damp) : delta >> 1;
  72. delta += floor(delta / numPoints);
  73. while (delta > baseMinusTMin * tMax >> 1) {
  74. delta = floor(delta / baseMinusTMin);
  75. k += base;
  76. }
  77. return floor(k + (baseMinusTMin + 1) * delta / (delta + skew));
  78. };
  79. /**
  80. * Converts a string of Unicode symbols (e.g. a domain name label) to a
  81. * Punycode string of ASCII-only symbols.
  82. */
  83. var encode = function (input) {
  84. var output = [];
  85. // Convert the input in UCS-2 to an array of Unicode code points.
  86. input = ucs2decode(input);
  87. // Cache the length.
  88. var inputLength = input.length;
  89. // Initialize the state.
  90. var n = initialN;
  91. var delta = 0;
  92. var bias = initialBias;
  93. var i, currentValue;
  94. // Handle the basic code points.
  95. for (i = 0; i < input.length; i++) {
  96. currentValue = input[i];
  97. if (currentValue < 0x80) {
  98. push(output, fromCharCode(currentValue));
  99. }
  100. }
  101. var basicLength = output.length; // number of basic code points.
  102. var handledCPCount = basicLength; // number of code points that have been handled;
  103. // Finish the basic string with a delimiter unless it's empty.
  104. if (basicLength) {
  105. push(output, delimiter);
  106. }
  107. // Main encoding loop:
  108. while (handledCPCount < inputLength) {
  109. // All non-basic code points < n have been handled already. Find the next larger one:
  110. var m = maxInt;
  111. for (i = 0; i < input.length; i++) {
  112. currentValue = input[i];
  113. if (currentValue >= n && currentValue < m) {
  114. m = currentValue;
  115. }
  116. }
  117. // Increase `delta` enough to advance the decoder's <n,i> state to <m,0>, but guard against overflow.
  118. var handledCPCountPlusOne = handledCPCount + 1;
  119. if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) {
  120. throw new $RangeError(OVERFLOW_ERROR);
  121. }
  122. delta += (m - n) * handledCPCountPlusOne;
  123. n = m;
  124. for (i = 0; i < input.length; i++) {
  125. currentValue = input[i];
  126. if (currentValue < n && ++delta > maxInt) {
  127. throw new $RangeError(OVERFLOW_ERROR);
  128. }
  129. if (currentValue === n) {
  130. // Represent delta as a generalized variable-length integer.
  131. var q = delta;
  132. var k = base;
  133. while (true) {
  134. var t = k <= bias ? tMin : k >= bias + tMax ? tMax : k - bias;
  135. if (q < t) break;
  136. var qMinusT = q - t;
  137. var baseMinusT = base - t;
  138. push(output, fromCharCode(digitToBasic(t + qMinusT % baseMinusT)));
  139. q = floor(qMinusT / baseMinusT);
  140. k += base;
  141. }
  142. push(output, fromCharCode(digitToBasic(q)));
  143. bias = adapt(delta, handledCPCountPlusOne, handledCPCount === basicLength);
  144. delta = 0;
  145. handledCPCount++;
  146. }
  147. }
  148. delta++;
  149. n++;
  150. }
  151. return join(output, '');
  152. };
  153. module.exports = function (input) {
  154. var encoded = [];
  155. var labels = split(replace(toLowerCase(input), regexSeparators, '\u002E'), '.');
  156. var i, label;
  157. for (i = 0; i < labels.length; i++) {
  158. label = labels[i];
  159. push(encoded, exec(regexNonASCII, label) ? 'xn--' + encode(label) : label);
  160. }
  161. return join(encoded, '.');
  162. };