You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1443 lines
56 KiB

4 years ago
  1. /** @license URI.js v4.4.0 (c) 2011 Gary Court. License: http://github.com/garycourt/uri-js */
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (factory((global.URI = global.URI || {})));
  6. }(this, (function (exports) { 'use strict';
  7. function merge() {
  8. for (var _len = arguments.length, sets = Array(_len), _key = 0; _key < _len; _key++) {
  9. sets[_key] = arguments[_key];
  10. }
  11. if (sets.length > 1) {
  12. sets[0] = sets[0].slice(0, -1);
  13. var xl = sets.length - 1;
  14. for (var x = 1; x < xl; ++x) {
  15. sets[x] = sets[x].slice(1, -1);
  16. }
  17. sets[xl] = sets[xl].slice(1);
  18. return sets.join('');
  19. } else {
  20. return sets[0];
  21. }
  22. }
  23. function subexp(str) {
  24. return "(?:" + str + ")";
  25. }
  26. function typeOf(o) {
  27. return o === undefined ? "undefined" : o === null ? "null" : Object.prototype.toString.call(o).split(" ").pop().split("]").shift().toLowerCase();
  28. }
  29. function toUpperCase(str) {
  30. return str.toUpperCase();
  31. }
  32. function toArray(obj) {
  33. return obj !== undefined && obj !== null ? obj instanceof Array ? obj : typeof obj.length !== "number" || obj.split || obj.setInterval || obj.call ? [obj] : Array.prototype.slice.call(obj) : [];
  34. }
  35. function assign(target, source) {
  36. var obj = target;
  37. if (source) {
  38. for (var key in source) {
  39. obj[key] = source[key];
  40. }
  41. }
  42. return obj;
  43. }
  44. function buildExps(isIRI) {
  45. var ALPHA$$ = "[A-Za-z]",
  46. CR$ = "[\\x0D]",
  47. DIGIT$$ = "[0-9]",
  48. DQUOTE$$ = "[\\x22]",
  49. HEXDIG$$ = merge(DIGIT$$, "[A-Fa-f]"),
  50. //case-insensitive
  51. LF$$ = "[\\x0A]",
  52. SP$$ = "[\\x20]",
  53. PCT_ENCODED$ = subexp(subexp("%[EFef]" + HEXDIG$$ + "%" + HEXDIG$$ + HEXDIG$$ + "%" + HEXDIG$$ + HEXDIG$$) + "|" + subexp("%[89A-Fa-f]" + HEXDIG$$ + "%" + HEXDIG$$ + HEXDIG$$) + "|" + subexp("%" + HEXDIG$$ + HEXDIG$$)),
  54. //expanded
  55. GEN_DELIMS$$ = "[\\:\\/\\?\\#\\[\\]\\@]",
  56. SUB_DELIMS$$ = "[\\!\\$\\&\\'\\(\\)\\*\\+\\,\\;\\=]",
  57. RESERVED$$ = merge(GEN_DELIMS$$, SUB_DELIMS$$),
  58. UCSCHAR$$ = isIRI ? "[\\xA0-\\u200D\\u2010-\\u2029\\u202F-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]" : "[]",
  59. //subset, excludes bidi control characters
  60. IPRIVATE$$ = isIRI ? "[\\uE000-\\uF8FF]" : "[]",
  61. //subset
  62. UNRESERVED$$ = merge(ALPHA$$, DIGIT$$, "[\\-\\.\\_\\~]", UCSCHAR$$),
  63. SCHEME$ = subexp(ALPHA$$ + merge(ALPHA$$, DIGIT$$, "[\\+\\-\\.]") + "*"),
  64. USERINFO$ = subexp(subexp(PCT_ENCODED$ + "|" + merge(UNRESERVED$$, SUB_DELIMS$$, "[\\:]")) + "*"),
  65. DEC_OCTET$ = subexp(subexp("25[0-5]") + "|" + subexp("2[0-4]" + DIGIT$$) + "|" + subexp("1" + DIGIT$$ + DIGIT$$) + "|" + subexp("[1-9]" + DIGIT$$) + "|" + DIGIT$$),
  66. DEC_OCTET_RELAXED$ = subexp(subexp("25[0-5]") + "|" + subexp("2[0-4]" + DIGIT$$) + "|" + subexp("1" + DIGIT$$ + DIGIT$$) + "|" + subexp("0?[1-9]" + DIGIT$$) + "|0?0?" + DIGIT$$),
  67. //relaxed parsing rules
  68. IPV4ADDRESS$ = subexp(DEC_OCTET_RELAXED$ + "\\." + DEC_OCTET_RELAXED$ + "\\." + DEC_OCTET_RELAXED$ + "\\." + DEC_OCTET_RELAXED$),
  69. H16$ = subexp(HEXDIG$$ + "{1,4}"),
  70. LS32$ = subexp(subexp(H16$ + "\\:" + H16$) + "|" + IPV4ADDRESS$),
  71. IPV6ADDRESS1$ = subexp(subexp(H16$ + "\\:") + "{6}" + LS32$),
  72. // 6( h16 ":" ) ls32
  73. IPV6ADDRESS2$ = subexp("\\:\\:" + subexp(H16$ + "\\:") + "{5}" + LS32$),
  74. // "::" 5( h16 ":" ) ls32
  75. IPV6ADDRESS3$ = subexp(subexp(H16$) + "?\\:\\:" + subexp(H16$ + "\\:") + "{4}" + LS32$),
  76. //[ h16 ] "::" 4( h16 ":" ) ls32
  77. IPV6ADDRESS4$ = subexp(subexp(subexp(H16$ + "\\:") + "{0,1}" + H16$) + "?\\:\\:" + subexp(H16$ + "\\:") + "{3}" + LS32$),
  78. //[ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
  79. IPV6ADDRESS5$ = subexp(subexp(subexp(H16$ + "\\:") + "{0,2}" + H16$) + "?\\:\\:" + subexp(H16$ + "\\:") + "{2}" + LS32$),
  80. //[ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
  81. IPV6ADDRESS6$ = subexp(subexp(subexp(H16$ + "\\:") + "{0,3}" + H16$) + "?\\:\\:" + H16$ + "\\:" + LS32$),
  82. //[ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
  83. IPV6ADDRESS7$ = subexp(subexp(subexp(H16$ + "\\:") + "{0,4}" + H16$) + "?\\:\\:" + LS32$),
  84. //[ *4( h16 ":" ) h16 ] "::" ls32
  85. IPV6ADDRESS8$ = subexp(subexp(subexp(H16$ + "\\:") + "{0,5}" + H16$) + "?\\:\\:" + H16$),
  86. //[ *5( h16 ":" ) h16 ] "::" h16
  87. IPV6ADDRESS9$ = subexp(subexp(subexp(H16$ + "\\:") + "{0,6}" + H16$) + "?\\:\\:"),
  88. //[ *6( h16 ":" ) h16 ] "::"
  89. IPV6ADDRESS$ = subexp([IPV6ADDRESS1$, IPV6ADDRESS2$, IPV6ADDRESS3$, IPV6ADDRESS4$, IPV6ADDRESS5$, IPV6ADDRESS6$, IPV6ADDRESS7$, IPV6ADDRESS8$, IPV6ADDRESS9$].join("|")),
  90. ZONEID$ = subexp(subexp(UNRESERVED$$ + "|" + PCT_ENCODED$) + "+"),
  91. //RFC 6874
  92. IPV6ADDRZ$ = subexp(IPV6ADDRESS$ + "\\%25" + ZONEID$),
  93. //RFC 6874
  94. IPV6ADDRZ_RELAXED$ = subexp(IPV6ADDRESS$ + subexp("\\%25|\\%(?!" + HEXDIG$$ + "{2})") + ZONEID$),
  95. //RFC 6874, with relaxed parsing rules
  96. IPVFUTURE$ = subexp("[vV]" + HEXDIG$$ + "+\\." + merge(UNRESERVED$$, SUB_DELIMS$$, "[\\:]") + "+"),
  97. IP_LITERAL$ = subexp("\\[" + subexp(IPV6ADDRZ_RELAXED$ + "|" + IPV6ADDRESS$ + "|" + IPVFUTURE$) + "\\]"),
  98. //RFC 6874
  99. REG_NAME$ = subexp(subexp(PCT_ENCODED$ + "|" + merge(UNRESERVED$$, SUB_DELIMS$$)) + "*"),
  100. HOST$ = subexp(IP_LITERAL$ + "|" + IPV4ADDRESS$ + "(?!" + REG_NAME$ + ")" + "|" + REG_NAME$),
  101. PORT$ = subexp(DIGIT$$ + "*"),
  102. AUTHORITY$ = subexp(subexp(USERINFO$ + "@") + "?" + HOST$ + subexp("\\:" + PORT$) + "?"),
  103. PCHAR$ = subexp(PCT_ENCODED$ + "|" + merge(UNRESERVED$$, SUB_DELIMS$$, "[\\:\\@]")),
  104. SEGMENT$ = subexp(PCHAR$ + "*"),
  105. SEGMENT_NZ$ = subexp(PCHAR$ + "+"),
  106. SEGMENT_NZ_NC$ = subexp(subexp(PCT_ENCODED$ + "|" + merge(UNRESERVED$$, SUB_DELIMS$$, "[\\@]")) + "+"),
  107. PATH_ABEMPTY$ = subexp(subexp("\\/" + SEGMENT$) + "*"),
  108. PATH_ABSOLUTE$ = subexp("\\/" + subexp(SEGMENT_NZ$ + PATH_ABEMPTY$) + "?"),
  109. //simplified
  110. PATH_NOSCHEME$ = subexp(SEGMENT_NZ_NC$ + PATH_ABEMPTY$),
  111. //simplified
  112. PATH_ROOTLESS$ = subexp(SEGMENT_NZ$ + PATH_ABEMPTY$),
  113. //simplified
  114. PATH_EMPTY$ = "(?!" + PCHAR$ + ")",
  115. PATH$ = subexp(PATH_ABEMPTY$ + "|" + PATH_ABSOLUTE$ + "|" + PATH_NOSCHEME$ + "|" + PATH_ROOTLESS$ + "|" + PATH_EMPTY$),
  116. QUERY$ = subexp(subexp(PCHAR$ + "|" + merge("[\\/\\?]", IPRIVATE$$)) + "*"),
  117. FRAGMENT$ = subexp(subexp(PCHAR$ + "|[\\/\\?]") + "*"),
  118. HIER_PART$ = subexp(subexp("\\/\\/" + AUTHORITY$ + PATH_ABEMPTY$) + "|" + PATH_ABSOLUTE$ + "|" + PATH_ROOTLESS$ + "|" + PATH_EMPTY$),
  119. URI$ = subexp(SCHEME$ + "\\:" + HIER_PART$ + subexp("\\?" + QUERY$) + "?" + subexp("\\#" + FRAGMENT$) + "?"),
  120. RELATIVE_PART$ = subexp(subexp("\\/\\/" + AUTHORITY$ + PATH_ABEMPTY$) + "|" + PATH_ABSOLUTE$ + "|" + PATH_NOSCHEME$ + "|" + PATH_EMPTY$),
  121. RELATIVE$ = subexp(RELATIVE_PART$ + subexp("\\?" + QUERY$) + "?" + subexp("\\#" + FRAGMENT$) + "?"),
  122. URI_REFERENCE$ = subexp(URI$ + "|" + RELATIVE$),
  123. ABSOLUTE_URI$ = subexp(SCHEME$ + "\\:" + HIER_PART$ + subexp("\\?" + QUERY$) + "?"),
  124. GENERIC_REF$ = "^(" + SCHEME$ + ")\\:" + subexp(subexp("\\/\\/(" + subexp("(" + USERINFO$ + ")@") + "?(" + HOST$ + ")" + subexp("\\:(" + PORT$ + ")") + "?)") + "?(" + PATH_ABEMPTY$ + "|" + PATH_ABSOLUTE$ + "|" + PATH_ROOTLESS$ + "|" + PATH_EMPTY$ + ")") + subexp("\\?(" + QUERY$ + ")") + "?" + subexp("\\#(" + FRAGMENT$ + ")") + "?$",
  125. RELATIVE_REF$ = "^(){0}" + subexp(subexp("\\/\\/(" + subexp("(" + USERINFO$ + ")@") + "?(" + HOST$ + ")" + subexp("\\:(" + PORT$ + ")") + "?)") + "?(" + PATH_ABEMPTY$ + "|" + PATH_ABSOLUTE$ + "|" + PATH_NOSCHEME$ + "|" + PATH_EMPTY$ + ")") + subexp("\\?(" + QUERY$ + ")") + "?" + subexp("\\#(" + FRAGMENT$ + ")") + "?$",
  126. ABSOLUTE_REF$ = "^(" + SCHEME$ + ")\\:" + subexp(subexp("\\/\\/(" + subexp("(" + USERINFO$ + ")@") + "?(" + HOST$ + ")" + subexp("\\:(" + PORT$ + ")") + "?)") + "?(" + PATH_ABEMPTY$ + "|" + PATH_ABSOLUTE$ + "|" + PATH_ROOTLESS$ + "|" + PATH_EMPTY$ + ")") + subexp("\\?(" + QUERY$ + ")") + "?$",
  127. SAMEDOC_REF$ = "^" + subexp("\\#(" + FRAGMENT$ + ")") + "?$",
  128. AUTHORITY_REF$ = "^" + subexp("(" + USERINFO$ + ")@") + "?(" + HOST$ + ")" + subexp("\\:(" + PORT$ + ")") + "?$";
  129. return {
  130. NOT_SCHEME: new RegExp(merge("[^]", ALPHA$$, DIGIT$$, "[\\+\\-\\.]"), "g"),
  131. NOT_USERINFO: new RegExp(merge("[^\\%\\:]", UNRESERVED$$, SUB_DELIMS$$), "g"),
  132. NOT_HOST: new RegExp(merge("[^\\%\\[\\]\\:]", UNRESERVED$$, SUB_DELIMS$$), "g"),
  133. NOT_PATH: new RegExp(merge("[^\\%\\/\\:\\@]", UNRESERVED$$, SUB_DELIMS$$), "g"),
  134. NOT_PATH_NOSCHEME: new RegExp(merge("[^\\%\\/\\@]", UNRESERVED$$, SUB_DELIMS$$), "g"),
  135. NOT_QUERY: new RegExp(merge("[^\\%]", UNRESERVED$$, SUB_DELIMS$$, "[\\:\\@\\/\\?]", IPRIVATE$$), "g"),
  136. NOT_FRAGMENT: new RegExp(merge("[^\\%]", UNRESERVED$$, SUB_DELIMS$$, "[\\:\\@\\/\\?]"), "g"),
  137. ESCAPE: new RegExp(merge("[^]", UNRESERVED$$, SUB_DELIMS$$), "g"),
  138. UNRESERVED: new RegExp(UNRESERVED$$, "g"),
  139. OTHER_CHARS: new RegExp(merge("[^\\%]", UNRESERVED$$, RESERVED$$), "g"),
  140. PCT_ENCODED: new RegExp(PCT_ENCODED$, "g"),
  141. IPV4ADDRESS: new RegExp("^(" + IPV4ADDRESS$ + ")$"),
  142. IPV6ADDRESS: new RegExp("^\\[?(" + IPV6ADDRESS$ + ")" + subexp(subexp("\\%25|\\%(?!" + HEXDIG$$ + "{2})") + "(" + ZONEID$ + ")") + "?\\]?$") //RFC 6874, with relaxed parsing rules
  143. };
  144. }
  145. var URI_PROTOCOL = buildExps(false);
  146. var IRI_PROTOCOL = buildExps(true);
  147. var slicedToArray = function () {
  148. function sliceIterator(arr, i) {
  149. var _arr = [];
  150. var _n = true;
  151. var _d = false;
  152. var _e = undefined;
  153. try {
  154. for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
  155. _arr.push(_s.value);
  156. if (i && _arr.length === i) break;
  157. }
  158. } catch (err) {
  159. _d = true;
  160. _e = err;
  161. } finally {
  162. try {
  163. if (!_n && _i["return"]) _i["return"]();
  164. } finally {
  165. if (_d) throw _e;
  166. }
  167. }
  168. return _arr;
  169. }
  170. return function (arr, i) {
  171. if (Array.isArray(arr)) {
  172. return arr;
  173. } else if (Symbol.iterator in Object(arr)) {
  174. return sliceIterator(arr, i);
  175. } else {
  176. throw new TypeError("Invalid attempt to destructure non-iterable instance");
  177. }
  178. };
  179. }();
  180. var toConsumableArray = function (arr) {
  181. if (Array.isArray(arr)) {
  182. for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
  183. return arr2;
  184. } else {
  185. return Array.from(arr);
  186. }
  187. };
  188. /** Highest positive signed 32-bit float value */
  189. var maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1
  190. /** Bootstring parameters */
  191. var base = 36;
  192. var tMin = 1;
  193. var tMax = 26;
  194. var skew = 38;
  195. var damp = 700;
  196. var initialBias = 72;
  197. var initialN = 128; // 0x80
  198. var delimiter = '-'; // '\x2D'
  199. /** Regular expressions */
  200. var regexPunycode = /^xn--/;
  201. var regexNonASCII = /[^\0-\x7E]/; // non-ASCII chars
  202. var regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g; // RFC 3490 separators
  203. /** Error messages */
  204. var errors = {
  205. 'overflow': 'Overflow: input needs wider integers to process',
  206. 'not-basic': 'Illegal input >= 0x80 (not a basic code point)',
  207. 'invalid-input': 'Invalid input'
  208. };
  209. /** Convenience shortcuts */
  210. var baseMinusTMin = base - tMin;
  211. var floor = Math.floor;
  212. var stringFromCharCode = String.fromCharCode;
  213. /*--------------------------------------------------------------------------*/
  214. /**
  215. * A generic error utility function.
  216. * @private
  217. * @param {String} type The error type.
  218. * @returns {Error} Throws a `RangeError` with the applicable error message.
  219. */
  220. function error$1(type) {
  221. throw new RangeError(errors[type]);
  222. }
  223. /**
  224. * A generic `Array#map` utility function.
  225. * @private
  226. * @param {Array} array The array to iterate over.
  227. * @param {Function} callback The function that gets called for every array
  228. * item.
  229. * @returns {Array} A new array of values returned by the callback function.
  230. */
  231. function map(array, fn) {
  232. var result = [];
  233. var length = array.length;
  234. while (length--) {
  235. result[length] = fn(array[length]);
  236. }
  237. return result;
  238. }
  239. /**
  240. * A simple `Array#map`-like wrapper to work with domain name strings or email
  241. * addresses.
  242. * @private
  243. * @param {String} domain The domain name or email address.
  244. * @param {Function} callback The function that gets called for every
  245. * character.
  246. * @returns {Array} A new string of characters returned by the callback
  247. * function.
  248. */
  249. function mapDomain(string, fn) {
  250. var parts = string.split('@');
  251. var result = '';
  252. if (parts.length > 1) {
  253. // In email addresses, only the domain name should be punycoded. Leave
  254. // the local part (i.e. everything up to `@`) intact.
  255. result = parts[0] + '@';
  256. string = parts[1];
  257. }
  258. // Avoid `split(regex)` for IE8 compatibility. See #17.
  259. string = string.replace(regexSeparators, '\x2E');
  260. var labels = string.split('.');
  261. var encoded = map(labels, fn).join('.');
  262. return result + encoded;
  263. }
  264. /**
  265. * Creates an array containing the numeric code points of each Unicode
  266. * character in the string. While JavaScript uses UCS-2 internally,
  267. * this function will convert a pair of surrogate halves (each of which
  268. * UCS-2 exposes as separate characters) into a single code point,
  269. * matching UTF-16.
  270. * @see `punycode.ucs2.encode`
  271. * @see <https://mathiasbynens.be/notes/javascript-encoding>
  272. * @memberOf punycode.ucs2
  273. * @name decode
  274. * @param {String} string The Unicode input string (UCS-2).
  275. * @returns {Array} The new array of code points.
  276. */
  277. function ucs2decode(string) {
  278. var output = [];
  279. var counter = 0;
  280. var length = string.length;
  281. while (counter < length) {
  282. var value = string.charCodeAt(counter++);
  283. if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
  284. // It's a high surrogate, and there is a next character.
  285. var extra = string.charCodeAt(counter++);
  286. if ((extra & 0xFC00) == 0xDC00) {
  287. // Low surrogate.
  288. output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
  289. } else {
  290. // It's an unmatched surrogate; only append this code unit, in case the
  291. // next code unit is the high surrogate of a surrogate pair.
  292. output.push(value);
  293. counter--;
  294. }
  295. } else {
  296. output.push(value);
  297. }
  298. }
  299. return output;
  300. }
  301. /**
  302. * Creates a string based on an array of numeric code points.
  303. * @see `punycode.ucs2.decode`
  304. * @memberOf punycode.ucs2
  305. * @name encode
  306. * @param {Array} codePoints The array of numeric code points.
  307. * @returns {String} The new Unicode string (UCS-2).
  308. */
  309. var ucs2encode = function ucs2encode(array) {
  310. return String.fromCodePoint.apply(String, toConsumableArray(array));
  311. };
  312. /**
  313. * Converts a basic code point into a digit/integer.
  314. * @see `digitToBasic()`
  315. * @private
  316. * @param {Number} codePoint The basic numeric code point value.
  317. * @returns {Number} The numeric value of a basic code point (for use in
  318. * representing integers) in the range `0` to `base - 1`, or `base` if
  319. * the code point does not represent a value.
  320. */
  321. var basicToDigit = function basicToDigit(codePoint) {
  322. if (codePoint - 0x30 < 0x0A) {
  323. return codePoint - 0x16;
  324. }
  325. if (codePoint - 0x41 < 0x1A) {
  326. return codePoint - 0x41;
  327. }
  328. if (codePoint - 0x61 < 0x1A) {
  329. return codePoint - 0x61;
  330. }
  331. return base;
  332. };
  333. /**
  334. * Converts a digit/integer into a basic code point.
  335. * @see `basicToDigit()`
  336. * @private
  337. * @param {Number} digit The numeric value of a basic code point.
  338. * @returns {Number} The basic code point whose value (when used for
  339. * representing integers) is `digit`, which needs to be in the range
  340. * `0` to `base - 1`. If `flag` is non-zero, the uppercase form is
  341. * used; else, the lowercase form is used. The behavior is undefined
  342. * if `flag` is non-zero and `digit` has no uppercase form.
  343. */
  344. var digitToBasic = function digitToBasic(digit, flag) {
  345. // 0..25 map to ASCII a..z or A..Z
  346. // 26..35 map to ASCII 0..9
  347. return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5);
  348. };
  349. /**
  350. * Bias adaptation function as per section 3.4 of RFC 3492.
  351. * https://tools.ietf.org/html/rfc3492#section-3.4
  352. * @private
  353. */
  354. var adapt = function adapt(delta, numPoints, firstTime) {
  355. var k = 0;
  356. delta = firstTime ? floor(delta / damp) : delta >> 1;
  357. delta += floor(delta / numPoints);
  358. for (; /* no initialization */delta > baseMinusTMin * tMax >> 1; k += base) {
  359. delta = floor(delta / baseMinusTMin);
  360. }
  361. return floor(k + (baseMinusTMin + 1) * delta / (delta + skew));
  362. };
  363. /**
  364. * Converts a Punycode string of ASCII-only symbols to a string of Unicode
  365. * symbols.
  366. * @memberOf punycode
  367. * @param {String} input The Punycode string of ASCII-only symbols.
  368. * @returns {String} The resulting string of Unicode symbols.
  369. */
  370. var decode = function decode(input) {
  371. // Don't use UCS-2.
  372. var output = [];
  373. var inputLength = input.length;
  374. var i = 0;
  375. var n = initialN;
  376. var bias = initialBias;
  377. // Handle the basic code points: let `basic` be the number of input code
  378. // points before the last delimiter, or `0` if there is none, then copy
  379. // the first basic code points to the output.
  380. var basic = input.lastIndexOf(delimiter);
  381. if (basic < 0) {
  382. basic = 0;
  383. }
  384. for (var j = 0; j < basic; ++j) {
  385. // if it's not a basic code point
  386. if (input.charCodeAt(j) >= 0x80) {
  387. error$1('not-basic');
  388. }
  389. output.push(input.charCodeAt(j));
  390. }
  391. // Main decoding loop: start just after the last delimiter if any basic code
  392. // points were copied; start at the beginning otherwise.
  393. for (var index = basic > 0 ? basic + 1 : 0; index < inputLength;) /* no final expression */{
  394. // `index` is the index of the next character to be consumed.
  395. // Decode a generalized variable-length integer into `delta`,
  396. // which gets added to `i`. The overflow checking is easier
  397. // if we increase `i` as we go, then subtract off its starting
  398. // value at the end to obtain `delta`.
  399. var oldi = i;
  400. for (var w = 1, k = base;; /* no condition */k += base) {
  401. if (index >= inputLength) {
  402. error$1('invalid-input');
  403. }
  404. var digit = basicToDigit(input.charCodeAt(index++));
  405. if (digit >= base || digit > floor((maxInt - i) / w)) {
  406. error$1('overflow');
  407. }
  408. i += digit * w;
  409. var t = k <= bias ? tMin : k >= bias + tMax ? tMax : k - bias;
  410. if (digit < t) {
  411. break;
  412. }
  413. var baseMinusT = base - t;
  414. if (w > floor(maxInt / baseMinusT)) {
  415. error$1('overflow');
  416. }
  417. w *= baseMinusT;
  418. }
  419. var out = output.length + 1;
  420. bias = adapt(i - oldi, out, oldi == 0);
  421. // `i` was supposed to wrap around from `out` to `0`,
  422. // incrementing `n` each time, so we'll fix that now:
  423. if (floor(i / out) > maxInt - n) {
  424. error$1('overflow');
  425. }
  426. n += floor(i / out);
  427. i %= out;
  428. // Insert `n` at position `i` of the output.
  429. output.splice(i++, 0, n);
  430. }
  431. return String.fromCodePoint.apply(String, output);
  432. };
  433. /**
  434. * Converts a string of Unicode symbols (e.g. a domain name label) to a
  435. * Punycode string of ASCII-only symbols.
  436. * @memberOf punycode
  437. * @param {String} input The string of Unicode symbols.
  438. * @returns {String} The resulting Punycode string of ASCII-only symbols.
  439. */
  440. var encode = function encode(input) {
  441. var output = [];
  442. // Convert the input in UCS-2 to an array of Unicode code points.
  443. input = ucs2decode(input);
  444. // Cache the length.
  445. var inputLength = input.length;
  446. // Initialize the state.
  447. var n = initialN;
  448. var delta = 0;
  449. var bias = initialBias;
  450. // Handle the basic code points.
  451. var _iteratorNormalCompletion = true;
  452. var _didIteratorError = false;
  453. var _iteratorError = undefined;
  454. try {
  455. for (var _iterator = input[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  456. var _currentValue2 = _step.value;
  457. if (_currentValue2 < 0x80) {
  458. output.push(stringFromCharCode(_currentValue2));
  459. }
  460. }
  461. } catch (err) {
  462. _didIteratorError = true;
  463. _iteratorError = err;
  464. } finally {
  465. try {
  466. if (!_iteratorNormalCompletion && _iterator.return) {
  467. _iterator.return();
  468. }
  469. } finally {
  470. if (_didIteratorError) {
  471. throw _iteratorError;
  472. }
  473. }
  474. }
  475. var basicLength = output.length;
  476. var handledCPCount = basicLength;
  477. // `handledCPCount` is the number of code points that have been handled;
  478. // `basicLength` is the number of basic code points.
  479. // Finish the basic string with a delimiter unless it's empty.
  480. if (basicLength) {
  481. output.push(delimiter);
  482. }
  483. // Main encoding loop:
  484. while (handledCPCount < inputLength) {
  485. // All non-basic code points < n have been handled already. Find the next
  486. // larger one:
  487. var m = maxInt;
  488. var _iteratorNormalCompletion2 = true;
  489. var _didIteratorError2 = false;
  490. var _iteratorError2 = undefined;
  491. try {
  492. for (var _iterator2 = input[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  493. var currentValue = _step2.value;
  494. if (currentValue >= n && currentValue < m) {
  495. m = currentValue;
  496. }
  497. }
  498. // Increase `delta` enough to advance the decoder's <n,i> state to <m,0>,
  499. // but guard against overflow.
  500. } catch (err) {
  501. _didIteratorError2 = true;
  502. _iteratorError2 = err;
  503. } finally {
  504. try {
  505. if (!_iteratorNormalCompletion2 && _iterator2.return) {
  506. _iterator2.return();
  507. }
  508. } finally {
  509. if (_didIteratorError2) {
  510. throw _iteratorError2;
  511. }
  512. }
  513. }
  514. var handledCPCountPlusOne = handledCPCount + 1;
  515. if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) {
  516. error$1('overflow');
  517. }
  518. delta += (m - n) * handledCPCountPlusOne;
  519. n = m;
  520. var _iteratorNormalCompletion3 = true;
  521. var _didIteratorError3 = false;
  522. var _iteratorError3 = undefined;
  523. try {
  524. for (var _iterator3 = input[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
  525. var _currentValue = _step3.value;
  526. if (_currentValue < n && ++delta > maxInt) {
  527. error$1('overflow');
  528. }
  529. if (_currentValue == n) {
  530. // Represent delta as a generalized variable-length integer.
  531. var q = delta;
  532. for (var k = base;; /* no condition */k += base) {
  533. var t = k <= bias ? tMin : k >= bias + tMax ? tMax : k - bias;
  534. if (q < t) {
  535. break;
  536. }
  537. var qMinusT = q - t;
  538. var baseMinusT = base - t;
  539. output.push(stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0)));
  540. q = floor(qMinusT / baseMinusT);
  541. }
  542. output.push(stringFromCharCode(digitToBasic(q, 0)));
  543. bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength);
  544. delta = 0;
  545. ++handledCPCount;
  546. }
  547. }
  548. } catch (err) {
  549. _didIteratorError3 = true;
  550. _iteratorError3 = err;
  551. } finally {
  552. try {
  553. if (!_iteratorNormalCompletion3 && _iterator3.return) {
  554. _iterator3.return();
  555. }
  556. } finally {
  557. if (_didIteratorError3) {
  558. throw _iteratorError3;
  559. }
  560. }
  561. }
  562. ++delta;
  563. ++n;
  564. }
  565. return output.join('');
  566. };
  567. /**
  568. * Converts a Punycode string representing a domain name or an email address
  569. * to Unicode. Only the Punycoded parts of the input will be converted, i.e.
  570. * it doesn't matter if you call it on a string that has already been
  571. * converted to Unicode.
  572. * @memberOf punycode
  573. * @param {String} input The Punycoded domain name or email address to
  574. * convert to Unicode.
  575. * @returns {String} The Unicode representation of the given Punycode
  576. * string.
  577. */
  578. var toUnicode = function toUnicode(input) {
  579. return mapDomain(input, function (string) {
  580. return regexPunycode.test(string) ? decode(string.slice(4).toLowerCase()) : string;
  581. });
  582. };
  583. /**
  584. * Converts a Unicode string representing a domain name or an email address to
  585. * Punycode. Only the non-ASCII parts of the domain name will be converted,
  586. * i.e. it doesn't matter if you call it with a domain that's already in
  587. * ASCII.
  588. * @memberOf punycode
  589. * @param {String} input The domain name or email address to convert, as a
  590. * Unicode string.
  591. * @returns {String} The Punycode representation of the given domain name or
  592. * email address.
  593. */
  594. var toASCII = function toASCII(input) {
  595. return mapDomain(input, function (string) {
  596. return regexNonASCII.test(string) ? 'xn--' + encode(string) : string;
  597. });
  598. };
  599. /*--------------------------------------------------------------------------*/
  600. /** Define the public API */
  601. var punycode = {
  602. /**
  603. * A string representing the current Punycode.js version number.
  604. * @memberOf punycode
  605. * @type String
  606. */
  607. 'version': '2.1.0',
  608. /**
  609. * An object of methods to convert from JavaScript's internal character
  610. * representation (UCS-2) to Unicode code points, and back.
  611. * @see <https://mathiasbynens.be/notes/javascript-encoding>
  612. * @memberOf punycode
  613. * @type Object
  614. */
  615. 'ucs2': {
  616. 'decode': ucs2decode,
  617. 'encode': ucs2encode
  618. },
  619. 'decode': decode,
  620. 'encode': encode,
  621. 'toASCII': toASCII,
  622. 'toUnicode': toUnicode
  623. };
  624. /**
  625. * URI.js
  626. *
  627. * @fileoverview An RFC 3986 compliant, scheme extendable URI parsing/validating/resolving library for JavaScript.
  628. * @author <a href="mailto:gary.court@gmail.com">Gary Court</a>
  629. * @see http://github.com/garycourt/uri-js
  630. */
  631. /**
  632. * Copyright 2011 Gary Court. All rights reserved.
  633. *
  634. * Redistribution and use in source and binary forms, with or without modification, are
  635. * permitted provided that the following conditions are met:
  636. *
  637. * 1. Redistributions of source code must retain the above copyright notice, this list of
  638. * conditions and the following disclaimer.
  639. *
  640. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  641. * of conditions and the following disclaimer in the documentation and/or other materials
  642. * provided with the distribution.
  643. *
  644. * THIS SOFTWARE IS PROVIDED BY GARY COURT ``AS IS'' AND ANY EXPRESS OR IMPLIED
  645. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  646. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARY COURT OR
  647. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  648. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  649. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  650. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  651. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  652. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  653. *
  654. * The views and conclusions contained in the software and documentation are those of the
  655. * authors and should not be interpreted as representing official policies, either expressed
  656. * or implied, of Gary Court.
  657. */
  658. var SCHEMES = {};
  659. function pctEncChar(chr) {
  660. var c = chr.charCodeAt(0);
  661. var e = void 0;
  662. if (c < 16) e = "%0" + c.toString(16).toUpperCase();else if (c < 128) e = "%" + c.toString(16).toUpperCase();else if (c < 2048) e = "%" + (c >> 6 | 192).toString(16).toUpperCase() + "%" + (c & 63 | 128).toString(16).toUpperCase();else e = "%" + (c >> 12 | 224).toString(16).toUpperCase() + "%" + (c >> 6 & 63 | 128).toString(16).toUpperCase() + "%" + (c & 63 | 128).toString(16).toUpperCase();
  663. return e;
  664. }
  665. function pctDecChars(str) {
  666. var newStr = "";
  667. var i = 0;
  668. var il = str.length;
  669. while (i < il) {
  670. var c = parseInt(str.substr(i + 1, 2), 16);
  671. if (c < 128) {
  672. newStr += String.fromCharCode(c);
  673. i += 3;
  674. } else if (c >= 194 && c < 224) {
  675. if (il - i >= 6) {
  676. var c2 = parseInt(str.substr(i + 4, 2), 16);
  677. newStr += String.fromCharCode((c & 31) << 6 | c2 & 63);
  678. } else {
  679. newStr += str.substr(i, 6);
  680. }
  681. i += 6;
  682. } else if (c >= 224) {
  683. if (il - i >= 9) {
  684. var _c = parseInt(str.substr(i + 4, 2), 16);
  685. var c3 = parseInt(str.substr(i + 7, 2), 16);
  686. newStr += String.fromCharCode((c & 15) << 12 | (_c & 63) << 6 | c3 & 63);
  687. } else {
  688. newStr += str.substr(i, 9);
  689. }
  690. i += 9;
  691. } else {
  692. newStr += str.substr(i, 3);
  693. i += 3;
  694. }
  695. }
  696. return newStr;
  697. }
  698. function _normalizeComponentEncoding(components, protocol) {
  699. function decodeUnreserved(str) {
  700. var decStr = pctDecChars(str);
  701. return !decStr.match(protocol.UNRESERVED) ? str : decStr;
  702. }
  703. if (components.scheme) components.scheme = String(components.scheme).replace(protocol.PCT_ENCODED, decodeUnreserved).toLowerCase().replace(protocol.NOT_SCHEME, "");
  704. if (components.userinfo !== undefined) components.userinfo = String(components.userinfo).replace(protocol.PCT_ENCODED, decodeUnreserved).replace(protocol.NOT_USERINFO, pctEncChar).replace(protocol.PCT_ENCODED, toUpperCase);
  705. if (components.host !== undefined) components.host = String(components.host).replace(protocol.PCT_ENCODED, decodeUnreserved).toLowerCase().replace(protocol.NOT_HOST, pctEncChar).replace(protocol.PCT_ENCODED, toUpperCase);
  706. if (components.path !== undefined) components.path = String(components.path).replace(protocol.PCT_ENCODED, decodeUnreserved).replace(components.scheme ? protocol.NOT_PATH : protocol.NOT_PATH_NOSCHEME, pctEncChar).replace(protocol.PCT_ENCODED, toUpperCase);
  707. if (components.query !== undefined) components.query = String(components.query).replace(protocol.PCT_ENCODED, decodeUnreserved).replace(protocol.NOT_QUERY, pctEncChar).replace(protocol.PCT_ENCODED, toUpperCase);
  708. if (components.fragment !== undefined) components.fragment = String(components.fragment).replace(protocol.PCT_ENCODED, decodeUnreserved).replace(protocol.NOT_FRAGMENT, pctEncChar).replace(protocol.PCT_ENCODED, toUpperCase);
  709. return components;
  710. }
  711. function _stripLeadingZeros(str) {
  712. return str.replace(/^0*(.*)/, "$1") || "0";
  713. }
  714. function _normalizeIPv4(host, protocol) {
  715. var matches = host.match(protocol.IPV4ADDRESS) || [];
  716. var _matches = slicedToArray(matches, 2),
  717. address = _matches[1];
  718. if (address) {
  719. return address.split(".").map(_stripLeadingZeros).join(".");
  720. } else {
  721. return host;
  722. }
  723. }
  724. function _normalizeIPv6(host, protocol) {
  725. var matches = host.match(protocol.IPV6ADDRESS) || [];
  726. var _matches2 = slicedToArray(matches, 3),
  727. address = _matches2[1],
  728. zone = _matches2[2];
  729. if (address) {
  730. var _address$toLowerCase$ = address.toLowerCase().split('::').reverse(),
  731. _address$toLowerCase$2 = slicedToArray(_address$toLowerCase$, 2),
  732. last = _address$toLowerCase$2[0],
  733. first = _address$toLowerCase$2[1];
  734. var firstFields = first ? first.split(":").map(_stripLeadingZeros) : [];
  735. var lastFields = last.split(":").map(_stripLeadingZeros);
  736. var isLastFieldIPv4Address = protocol.IPV4ADDRESS.test(lastFields[lastFields.length - 1]);
  737. var fieldCount = isLastFieldIPv4Address ? 7 : 8;
  738. var lastFieldsStart = lastFields.length - fieldCount;
  739. var fields = Array(fieldCount);
  740. for (var x = 0; x < fieldCount; ++x) {
  741. fields[x] = firstFields[x] || lastFields[lastFieldsStart + x] || '';
  742. }
  743. if (isLastFieldIPv4Address) {
  744. fields[fieldCount - 1] = _normalizeIPv4(fields[fieldCount - 1], protocol);
  745. }
  746. var allZeroFields = fields.reduce(function (acc, field, index) {
  747. if (!field || field === "0") {
  748. var lastLongest = acc[acc.length - 1];
  749. if (lastLongest && lastLongest.index + lastLongest.length === index) {
  750. lastLongest.length++;
  751. } else {
  752. acc.push({ index: index, length: 1 });
  753. }
  754. }
  755. return acc;
  756. }, []);
  757. var longestZeroFields = allZeroFields.sort(function (a, b) {
  758. return b.length - a.length;
  759. })[0];
  760. var newHost = void 0;
  761. if (longestZeroFields && longestZeroFields.length > 1) {
  762. var newFirst = fields.slice(0, longestZeroFields.index);
  763. var newLast = fields.slice(longestZeroFields.index + longestZeroFields.length);
  764. newHost = newFirst.join(":") + "::" + newLast.join(":");
  765. } else {
  766. newHost = fields.join(":");
  767. }
  768. if (zone) {
  769. newHost += "%" + zone;
  770. }
  771. return newHost;
  772. } else {
  773. return host;
  774. }
  775. }
  776. var URI_PARSE = /^(?:([^:\/?#]+):)?(?:\/\/((?:([^\/?#@]*)@)?(\[[^\/?#\]]+\]|[^\/?#:]*)(?:\:(\d*))?))?([^?#]*)(?:\?([^#]*))?(?:#((?:.|\n|\r)*))?/i;
  777. var NO_MATCH_IS_UNDEFINED = "".match(/(){0}/)[1] === undefined;
  778. function parse(uriString) {
  779. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  780. var components = {};
  781. var protocol = options.iri !== false ? IRI_PROTOCOL : URI_PROTOCOL;
  782. if (options.reference === "suffix") uriString = (options.scheme ? options.scheme + ":" : "") + "//" + uriString;
  783. var matches = uriString.match(URI_PARSE);
  784. if (matches) {
  785. if (NO_MATCH_IS_UNDEFINED) {
  786. //store each component
  787. components.scheme = matches[1];
  788. components.userinfo = matches[3];
  789. components.host = matches[4];
  790. components.port = parseInt(matches[5], 10);
  791. components.path = matches[6] || "";
  792. components.query = matches[7];
  793. components.fragment = matches[8];
  794. //fix port number
  795. if (isNaN(components.port)) {
  796. components.port = matches[5];
  797. }
  798. } else {
  799. //IE FIX for improper RegExp matching
  800. //store each component
  801. components.scheme = matches[1] || undefined;
  802. components.userinfo = uriString.indexOf("@") !== -1 ? matches[3] : undefined;
  803. components.host = uriString.indexOf("//") !== -1 ? matches[4] : undefined;
  804. components.port = parseInt(matches[5], 10);
  805. components.path = matches[6] || "";
  806. components.query = uriString.indexOf("?") !== -1 ? matches[7] : undefined;
  807. components.fragment = uriString.indexOf("#") !== -1 ? matches[8] : undefined;
  808. //fix port number
  809. if (isNaN(components.port)) {
  810. components.port = uriString.match(/\/\/(?:.|\n)*\:(?:\/|\?|\#|$)/) ? matches[4] : undefined;
  811. }
  812. }
  813. if (components.host) {
  814. //normalize IP hosts
  815. components.host = _normalizeIPv6(_normalizeIPv4(components.host, protocol), protocol);
  816. }
  817. //determine reference type
  818. if (components.scheme === undefined && components.userinfo === undefined && components.host === undefined && components.port === undefined && !components.path && components.query === undefined) {
  819. components.reference = "same-document";
  820. } else if (components.scheme === undefined) {
  821. components.reference = "relative";
  822. } else if (components.fragment === undefined) {
  823. components.reference = "absolute";
  824. } else {
  825. components.reference = "uri";
  826. }
  827. //check for reference errors
  828. if (options.reference && options.reference !== "suffix" && options.reference !== components.reference) {
  829. components.error = components.error || "URI is not a " + options.reference + " reference.";
  830. }
  831. //find scheme handler
  832. var schemeHandler = SCHEMES[(options.scheme || components.scheme || "").toLowerCase()];
  833. //check if scheme can't handle IRIs
  834. if (!options.unicodeSupport && (!schemeHandler || !schemeHandler.unicodeSupport)) {
  835. //if host component is a domain name
  836. if (components.host && (options.domainHost || schemeHandler && schemeHandler.domainHost)) {
  837. //convert Unicode IDN -> ASCII IDN
  838. try {
  839. components.host = punycode.toASCII(components.host.replace(protocol.PCT_ENCODED, pctDecChars).toLowerCase());
  840. } catch (e) {
  841. components.error = components.error || "Host's domain name can not be converted to ASCII via punycode: " + e;
  842. }
  843. }
  844. //convert IRI -> URI
  845. _normalizeComponentEncoding(components, URI_PROTOCOL);
  846. } else {
  847. //normalize encodings
  848. _normalizeComponentEncoding(components, protocol);
  849. }
  850. //perform scheme specific parsing
  851. if (schemeHandler && schemeHandler.parse) {
  852. schemeHandler.parse(components, options);
  853. }
  854. } else {
  855. components.error = components.error || "URI can not be parsed.";
  856. }
  857. return components;
  858. }
  859. function _recomposeAuthority(components, options) {
  860. var protocol = options.iri !== false ? IRI_PROTOCOL : URI_PROTOCOL;
  861. var uriTokens = [];
  862. if (components.userinfo !== undefined) {
  863. uriTokens.push(components.userinfo);
  864. uriTokens.push("@");
  865. }
  866. if (components.host !== undefined) {
  867. //normalize IP hosts, add brackets and escape zone separator for IPv6
  868. uriTokens.push(_normalizeIPv6(_normalizeIPv4(String(components.host), protocol), protocol).replace(protocol.IPV6ADDRESS, function (_, $1, $2) {
  869. return "[" + $1 + ($2 ? "%25" + $2 : "") + "]";
  870. }));
  871. }
  872. if (typeof components.port === "number" || typeof components.port === "string") {
  873. uriTokens.push(":");
  874. uriTokens.push(String(components.port));
  875. }
  876. return uriTokens.length ? uriTokens.join("") : undefined;
  877. }
  878. var RDS1 = /^\.\.?\//;
  879. var RDS2 = /^\/\.(\/|$)/;
  880. var RDS3 = /^\/\.\.(\/|$)/;
  881. var RDS5 = /^\/?(?:.|\n)*?(?=\/|$)/;
  882. function removeDotSegments(input) {
  883. var output = [];
  884. while (input.length) {
  885. if (input.match(RDS1)) {
  886. input = input.replace(RDS1, "");
  887. } else if (input.match(RDS2)) {
  888. input = input.replace(RDS2, "/");
  889. } else if (input.match(RDS3)) {
  890. input = input.replace(RDS3, "/");
  891. output.pop();
  892. } else if (input === "." || input === "..") {
  893. input = "";
  894. } else {
  895. var im = input.match(RDS5);
  896. if (im) {
  897. var s = im[0];
  898. input = input.slice(s.length);
  899. output.push(s);
  900. } else {
  901. throw new Error("Unexpected dot segment condition");
  902. }
  903. }
  904. }
  905. return output.join("");
  906. }
  907. function serialize(components) {
  908. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  909. var protocol = options.iri ? IRI_PROTOCOL : URI_PROTOCOL;
  910. var uriTokens = [];
  911. //find scheme handler
  912. var schemeHandler = SCHEMES[(options.scheme || components.scheme || "").toLowerCase()];
  913. //perform scheme specific serialization
  914. if (schemeHandler && schemeHandler.serialize) schemeHandler.serialize(components, options);
  915. if (components.host) {
  916. //if host component is an IPv6 address
  917. if (protocol.IPV6ADDRESS.test(components.host)) {}
  918. //TODO: normalize IPv6 address as per RFC 5952
  919. //if host component is a domain name
  920. else if (options.domainHost || schemeHandler && schemeHandler.domainHost) {
  921. //convert IDN via punycode
  922. try {
  923. components.host = !options.iri ? punycode.toASCII(components.host.replace(protocol.PCT_ENCODED, pctDecChars).toLowerCase()) : punycode.toUnicode(components.host);
  924. } catch (e) {
  925. components.error = components.error || "Host's domain name can not be converted to " + (!options.iri ? "ASCII" : "Unicode") + " via punycode: " + e;
  926. }
  927. }
  928. }
  929. //normalize encoding
  930. _normalizeComponentEncoding(components, protocol);
  931. if (options.reference !== "suffix" && components.scheme) {
  932. uriTokens.push(components.scheme);
  933. uriTokens.push(":");
  934. }
  935. var authority = _recomposeAuthority(components, options);
  936. if (authority !== undefined) {
  937. if (options.reference !== "suffix") {
  938. uriTokens.push("//");
  939. }
  940. uriTokens.push(authority);
  941. if (components.path && components.path.charAt(0) !== "/") {
  942. uriTokens.push("/");
  943. }
  944. }
  945. if (components.path !== undefined) {
  946. var s = components.path;
  947. if (!options.absolutePath && (!schemeHandler || !schemeHandler.absolutePath)) {
  948. s = removeDotSegments(s);
  949. }
  950. if (authority === undefined) {
  951. s = s.replace(/^\/\//, "/%2F"); //don't allow the path to start with "//"
  952. }
  953. uriTokens.push(s);
  954. }
  955. if (components.query !== undefined) {
  956. uriTokens.push("?");
  957. uriTokens.push(components.query);
  958. }
  959. if (components.fragment !== undefined) {
  960. uriTokens.push("#");
  961. uriTokens.push(components.fragment);
  962. }
  963. return uriTokens.join(""); //merge tokens into a string
  964. }
  965. function resolveComponents(base, relative) {
  966. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  967. var skipNormalization = arguments[3];
  968. var target = {};
  969. if (!skipNormalization) {
  970. base = parse(serialize(base, options), options); //normalize base components
  971. relative = parse(serialize(relative, options), options); //normalize relative components
  972. }
  973. options = options || {};
  974. if (!options.tolerant && relative.scheme) {
  975. target.scheme = relative.scheme;
  976. //target.authority = relative.authority;
  977. target.userinfo = relative.userinfo;
  978. target.host = relative.host;
  979. target.port = relative.port;
  980. target.path = removeDotSegments(relative.path || "");
  981. target.query = relative.query;
  982. } else {
  983. if (relative.userinfo !== undefined || relative.host !== undefined || relative.port !== undefined) {
  984. //target.authority = relative.authority;
  985. target.userinfo = relative.userinfo;
  986. target.host = relative.host;
  987. target.port = relative.port;
  988. target.path = removeDotSegments(relative.path || "");
  989. target.query = relative.query;
  990. } else {
  991. if (!relative.path) {
  992. target.path = base.path;
  993. if (relative.query !== undefined) {
  994. target.query = relative.query;
  995. } else {
  996. target.query = base.query;
  997. }
  998. } else {
  999. if (relative.path.charAt(0) === "/") {
  1000. target.path = removeDotSegments(relative.path);
  1001. } else {
  1002. if ((base.userinfo !== undefined || base.host !== undefined || base.port !== undefined) && !base.path) {
  1003. target.path = "/" + relative.path;
  1004. } else if (!base.path) {
  1005. target.path = relative.path;
  1006. } else {
  1007. target.path = base.path.slice(0, base.path.lastIndexOf("/") + 1) + relative.path;
  1008. }
  1009. target.path = removeDotSegments(target.path);
  1010. }
  1011. target.query = relative.query;
  1012. }
  1013. //target.authority = base.authority;
  1014. target.userinfo = base.userinfo;
  1015. target.host = base.host;
  1016. target.port = base.port;
  1017. }
  1018. target.scheme = base.scheme;
  1019. }
  1020. target.fragment = relative.fragment;
  1021. return target;
  1022. }
  1023. function resolve(baseURI, relativeURI, options) {
  1024. var schemelessOptions = assign({ scheme: 'null' }, options);
  1025. return serialize(resolveComponents(parse(baseURI, schemelessOptions), parse(relativeURI, schemelessOptions), schemelessOptions, true), schemelessOptions);
  1026. }
  1027. function normalize(uri, options) {
  1028. if (typeof uri === "string") {
  1029. uri = serialize(parse(uri, options), options);
  1030. } else if (typeOf(uri) === "object") {
  1031. uri = parse(serialize(uri, options), options);
  1032. }
  1033. return uri;
  1034. }
  1035. function equal(uriA, uriB, options) {
  1036. if (typeof uriA === "string") {
  1037. uriA = serialize(parse(uriA, options), options);
  1038. } else if (typeOf(uriA) === "object") {
  1039. uriA = serialize(uriA, options);
  1040. }
  1041. if (typeof uriB === "string") {
  1042. uriB = serialize(parse(uriB, options), options);
  1043. } else if (typeOf(uriB) === "object") {
  1044. uriB = serialize(uriB, options);
  1045. }
  1046. return uriA === uriB;
  1047. }
  1048. function escapeComponent(str, options) {
  1049. return str && str.toString().replace(!options || !options.iri ? URI_PROTOCOL.ESCAPE : IRI_PROTOCOL.ESCAPE, pctEncChar);
  1050. }
  1051. function unescapeComponent(str, options) {
  1052. return str && str.toString().replace(!options || !options.iri ? URI_PROTOCOL.PCT_ENCODED : IRI_PROTOCOL.PCT_ENCODED, pctDecChars);
  1053. }
  1054. var handler = {
  1055. scheme: "http",
  1056. domainHost: true,
  1057. parse: function parse(components, options) {
  1058. //report missing host
  1059. if (!components.host) {
  1060. components.error = components.error || "HTTP URIs must have a host.";
  1061. }
  1062. return components;
  1063. },
  1064. serialize: function serialize(components, options) {
  1065. var secure = String(components.scheme).toLowerCase() === "https";
  1066. //normalize the default port
  1067. if (components.port === (secure ? 443 : 80) || components.port === "") {
  1068. components.port = undefined;
  1069. }
  1070. //normalize the empty path
  1071. if (!components.path) {
  1072. components.path = "/";
  1073. }
  1074. //NOTE: We do not parse query strings for HTTP URIs
  1075. //as WWW Form Url Encoded query strings are part of the HTML4+ spec,
  1076. //and not the HTTP spec.
  1077. return components;
  1078. }
  1079. };
  1080. var handler$1 = {
  1081. scheme: "https",
  1082. domainHost: handler.domainHost,
  1083. parse: handler.parse,
  1084. serialize: handler.serialize
  1085. };
  1086. function isSecure(wsComponents) {
  1087. return typeof wsComponents.secure === 'boolean' ? wsComponents.secure : String(wsComponents.scheme).toLowerCase() === "wss";
  1088. }
  1089. //RFC 6455
  1090. var handler$2 = {
  1091. scheme: "ws",
  1092. domainHost: true,
  1093. parse: function parse(components, options) {
  1094. var wsComponents = components;
  1095. //indicate if the secure flag is set
  1096. wsComponents.secure = isSecure(wsComponents);
  1097. //construct resouce name
  1098. wsComponents.resourceName = (wsComponents.path || '/') + (wsComponents.query ? '?' + wsComponents.query : '');
  1099. wsComponents.path = undefined;
  1100. wsComponents.query = undefined;
  1101. return wsComponents;
  1102. },
  1103. serialize: function serialize(wsComponents, options) {
  1104. //normalize the default port
  1105. if (wsComponents.port === (isSecure(wsComponents) ? 443 : 80) || wsComponents.port === "") {
  1106. wsComponents.port = undefined;
  1107. }
  1108. //ensure scheme matches secure flag
  1109. if (typeof wsComponents.secure === 'boolean') {
  1110. wsComponents.scheme = wsComponents.secure ? 'wss' : 'ws';
  1111. wsComponents.secure = undefined;
  1112. }
  1113. //reconstruct path from resource name
  1114. if (wsComponents.resourceName) {
  1115. var _wsComponents$resourc = wsComponents.resourceName.split('?'),
  1116. _wsComponents$resourc2 = slicedToArray(_wsComponents$resourc, 2),
  1117. path = _wsComponents$resourc2[0],
  1118. query = _wsComponents$resourc2[1];
  1119. wsComponents.path = path && path !== '/' ? path : undefined;
  1120. wsComponents.query = query;
  1121. wsComponents.resourceName = undefined;
  1122. }
  1123. //forbid fragment component
  1124. wsComponents.fragment = undefined;
  1125. return wsComponents;
  1126. }
  1127. };
  1128. var handler$3 = {
  1129. scheme: "wss",
  1130. domainHost: handler$2.domainHost,
  1131. parse: handler$2.parse,
  1132. serialize: handler$2.serialize
  1133. };
  1134. var O = {};
  1135. var isIRI = true;
  1136. //RFC 3986
  1137. var UNRESERVED$$ = "[A-Za-z0-9\\-\\.\\_\\~" + (isIRI ? "\\xA0-\\u200D\\u2010-\\u2029\\u202F-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF" : "") + "]";
  1138. var HEXDIG$$ = "[0-9A-Fa-f]"; //case-insensitive
  1139. var PCT_ENCODED$ = subexp(subexp("%[EFef]" + HEXDIG$$ + "%" + HEXDIG$$ + HEXDIG$$ + "%" + HEXDIG$$ + HEXDIG$$) + "|" + subexp("%[89A-Fa-f]" + HEXDIG$$ + "%" + HEXDIG$$ + HEXDIG$$) + "|" + subexp("%" + HEXDIG$$ + HEXDIG$$)); //expanded
  1140. //RFC 5322, except these symbols as per RFC 6068: @ : / ? # [ ] & ; =
  1141. //const ATEXT$$ = "[A-Za-z0-9\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\_\\`\\{\\|\\}\\~]";
  1142. //const WSP$$ = "[\\x20\\x09]";
  1143. //const OBS_QTEXT$$ = "[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x7F]"; //(%d1-8 / %d11-12 / %d14-31 / %d127)
  1144. //const QTEXT$$ = merge("[\\x21\\x23-\\x5B\\x5D-\\x7E]", OBS_QTEXT$$); //%d33 / %d35-91 / %d93-126 / obs-qtext
  1145. //const VCHAR$$ = "[\\x21-\\x7E]";
  1146. //const WSP$$ = "[\\x20\\x09]";
  1147. //const OBS_QP$ = subexp("\\\\" + merge("[\\x00\\x0D\\x0A]", OBS_QTEXT$$)); //%d0 / CR / LF / obs-qtext
  1148. //const FWS$ = subexp(subexp(WSP$$ + "*" + "\\x0D\\x0A") + "?" + WSP$$ + "+");
  1149. //const QUOTED_PAIR$ = subexp(subexp("\\\\" + subexp(VCHAR$$ + "|" + WSP$$)) + "|" + OBS_QP$);
  1150. //const QUOTED_STRING$ = subexp('\\"' + subexp(FWS$ + "?" + QCONTENT$) + "*" + FWS$ + "?" + '\\"');
  1151. var ATEXT$$ = "[A-Za-z0-9\\!\\$\\%\\'\\*\\+\\-\\^\\_\\`\\{\\|\\}\\~]";
  1152. var QTEXT$$ = "[\\!\\$\\%\\'\\(\\)\\*\\+\\,\\-\\.0-9\\<\\>A-Z\\x5E-\\x7E]";
  1153. var VCHAR$$ = merge(QTEXT$$, "[\\\"\\\\]");
  1154. var SOME_DELIMS$$ = "[\\!\\$\\'\\(\\)\\*\\+\\,\\;\\:\\@]";
  1155. var UNRESERVED = new RegExp(UNRESERVED$$, "g");
  1156. var PCT_ENCODED = new RegExp(PCT_ENCODED$, "g");
  1157. var NOT_LOCAL_PART = new RegExp(merge("[^]", ATEXT$$, "[\\.]", '[\\"]', VCHAR$$), "g");
  1158. var NOT_HFNAME = new RegExp(merge("[^]", UNRESERVED$$, SOME_DELIMS$$), "g");
  1159. var NOT_HFVALUE = NOT_HFNAME;
  1160. function decodeUnreserved(str) {
  1161. var decStr = pctDecChars(str);
  1162. return !decStr.match(UNRESERVED) ? str : decStr;
  1163. }
  1164. var handler$4 = {
  1165. scheme: "mailto",
  1166. parse: function parse$$1(components, options) {
  1167. var mailtoComponents = components;
  1168. var to = mailtoComponents.to = mailtoComponents.path ? mailtoComponents.path.split(",") : [];
  1169. mailtoComponents.path = undefined;
  1170. if (mailtoComponents.query) {
  1171. var unknownHeaders = false;
  1172. var headers = {};
  1173. var hfields = mailtoComponents.query.split("&");
  1174. for (var x = 0, xl = hfields.length; x < xl; ++x) {
  1175. var hfield = hfields[x].split("=");
  1176. switch (hfield[0]) {
  1177. case "to":
  1178. var toAddrs = hfield[1].split(",");
  1179. for (var _x = 0, _xl = toAddrs.length; _x < _xl; ++_x) {
  1180. to.push(toAddrs[_x]);
  1181. }
  1182. break;
  1183. case "subject":
  1184. mailtoComponents.subject = unescapeComponent(hfield[1], options);
  1185. break;
  1186. case "body":
  1187. mailtoComponents.body = unescapeComponent(hfield[1], options);
  1188. break;
  1189. default:
  1190. unknownHeaders = true;
  1191. headers[unescapeComponent(hfield[0], options)] = unescapeComponent(hfield[1], options);
  1192. break;
  1193. }
  1194. }
  1195. if (unknownHeaders) mailtoComponents.headers = headers;
  1196. }
  1197. mailtoComponents.query = undefined;
  1198. for (var _x2 = 0, _xl2 = to.length; _x2 < _xl2; ++_x2) {
  1199. var addr = to[_x2].split("@");
  1200. addr[0] = unescapeComponent(addr[0]);
  1201. if (!options.unicodeSupport) {
  1202. //convert Unicode IDN -> ASCII IDN
  1203. try {
  1204. addr[1] = punycode.toASCII(unescapeComponent(addr[1], options).toLowerCase());
  1205. } catch (e) {
  1206. mailtoComponents.error = mailtoComponents.error || "Email address's domain name can not be converted to ASCII via punycode: " + e;
  1207. }
  1208. } else {
  1209. addr[1] = unescapeComponent(addr[1], options).toLowerCase();
  1210. }
  1211. to[_x2] = addr.join("@");
  1212. }
  1213. return mailtoComponents;
  1214. },
  1215. serialize: function serialize$$1(mailtoComponents, options) {
  1216. var components = mailtoComponents;
  1217. var to = toArray(mailtoComponents.to);
  1218. if (to) {
  1219. for (var x = 0, xl = to.length; x < xl; ++x) {
  1220. var toAddr = String(to[x]);
  1221. var atIdx = toAddr.lastIndexOf("@");
  1222. var localPart = toAddr.slice(0, atIdx).replace(PCT_ENCODED, decodeUnreserved).replace(PCT_ENCODED, toUpperCase).replace(NOT_LOCAL_PART, pctEncChar);
  1223. var domain = toAddr.slice(atIdx + 1);
  1224. //convert IDN via punycode
  1225. try {
  1226. domain = !options.iri ? punycode.toASCII(unescapeComponent(domain, options).toLowerCase()) : punycode.toUnicode(domain);
  1227. } catch (e) {
  1228. components.error = components.error || "Email address's domain name can not be converted to " + (!options.iri ? "ASCII" : "Unicode") + " via punycode: " + e;
  1229. }
  1230. to[x] = localPart + "@" + domain;
  1231. }
  1232. components.path = to.join(",");
  1233. }
  1234. var headers = mailtoComponents.headers = mailtoComponents.headers || {};
  1235. if (mailtoComponents.subject) headers["subject"] = mailtoComponents.subject;
  1236. if (mailtoComponents.body) headers["body"] = mailtoComponents.body;
  1237. var fields = [];
  1238. for (var name in headers) {
  1239. if (headers[name] !== O[name]) {
  1240. fields.push(name.replace(PCT_ENCODED, decodeUnreserved).replace(PCT_ENCODED, toUpperCase).replace(NOT_HFNAME, pctEncChar) + "=" + headers[name].replace(PCT_ENCODED, decodeUnreserved).replace(PCT_ENCODED, toUpperCase).replace(NOT_HFVALUE, pctEncChar));
  1241. }
  1242. }
  1243. if (fields.length) {
  1244. components.query = fields.join("&");
  1245. }
  1246. return components;
  1247. }
  1248. };
  1249. var URN_PARSE = /^([^\:]+)\:(.*)/;
  1250. //RFC 2141
  1251. var handler$5 = {
  1252. scheme: "urn",
  1253. parse: function parse$$1(components, options) {
  1254. var matches = components.path && components.path.match(URN_PARSE);
  1255. var urnComponents = components;
  1256. if (matches) {
  1257. var scheme = options.scheme || urnComponents.scheme || "urn";
  1258. var nid = matches[1].toLowerCase();
  1259. var nss = matches[2];
  1260. var urnScheme = scheme + ":" + (options.nid || nid);
  1261. var schemeHandler = SCHEMES[urnScheme];
  1262. urnComponents.nid = nid;
  1263. urnComponents.nss = nss;
  1264. urnComponents.path = undefined;
  1265. if (schemeHandler) {
  1266. urnComponents = schemeHandler.parse(urnComponents, options);
  1267. }
  1268. } else {
  1269. urnComponents.error = urnComponents.error || "URN can not be parsed.";
  1270. }
  1271. return urnComponents;
  1272. },
  1273. serialize: function serialize$$1(urnComponents, options) {
  1274. var scheme = options.scheme || urnComponents.scheme || "urn";
  1275. var nid = urnComponents.nid;
  1276. var urnScheme = scheme + ":" + (options.nid || nid);
  1277. var schemeHandler = SCHEMES[urnScheme];
  1278. if (schemeHandler) {
  1279. urnComponents = schemeHandler.serialize(urnComponents, options);
  1280. }
  1281. var uriComponents = urnComponents;
  1282. var nss = urnComponents.nss;
  1283. uriComponents.path = (nid || options.nid) + ":" + nss;
  1284. return uriComponents;
  1285. }
  1286. };
  1287. var UUID = /^[0-9A-Fa-f]{8}(?:\-[0-9A-Fa-f]{4}){3}\-[0-9A-Fa-f]{12}$/;
  1288. //RFC 4122
  1289. var handler$6 = {
  1290. scheme: "urn:uuid",
  1291. parse: function parse(urnComponents, options) {
  1292. var uuidComponents = urnComponents;
  1293. uuidComponents.uuid = uuidComponents.nss;
  1294. uuidComponents.nss = undefined;
  1295. if (!options.tolerant && (!uuidComponents.uuid || !uuidComponents.uuid.match(UUID))) {
  1296. uuidComponents.error = uuidComponents.error || "UUID is not valid.";
  1297. }
  1298. return uuidComponents;
  1299. },
  1300. serialize: function serialize(uuidComponents, options) {
  1301. var urnComponents = uuidComponents;
  1302. //normalize UUID
  1303. urnComponents.nss = (uuidComponents.uuid || "").toLowerCase();
  1304. return urnComponents;
  1305. }
  1306. };
  1307. SCHEMES[handler.scheme] = handler;
  1308. SCHEMES[handler$1.scheme] = handler$1;
  1309. SCHEMES[handler$2.scheme] = handler$2;
  1310. SCHEMES[handler$3.scheme] = handler$3;
  1311. SCHEMES[handler$4.scheme] = handler$4;
  1312. SCHEMES[handler$5.scheme] = handler$5;
  1313. SCHEMES[handler$6.scheme] = handler$6;
  1314. exports.SCHEMES = SCHEMES;
  1315. exports.pctEncChar = pctEncChar;
  1316. exports.pctDecChars = pctDecChars;
  1317. exports.parse = parse;
  1318. exports.removeDotSegments = removeDotSegments;
  1319. exports.serialize = serialize;
  1320. exports.resolveComponents = resolveComponents;
  1321. exports.resolve = resolve;
  1322. exports.normalize = normalize;
  1323. exports.equal = equal;
  1324. exports.escapeComponent = escapeComponent;
  1325. exports.unescapeComponent = unescapeComponent;
  1326. Object.defineProperty(exports, '__esModule', { value: true });
  1327. })));
  1328. //# sourceMappingURL=uri.all.js.map