index.cjs.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. 'use strict';
  2. /* eslint-disable no-bitwise */
  3. const decodeCache = {};
  4. function getDecodeCache (exclude) {
  5. let cache = decodeCache[exclude];
  6. if (cache) { return cache }
  7. cache = decodeCache[exclude] = [];
  8. for (let i = 0; i < 128; i++) {
  9. const ch = String.fromCharCode(i);
  10. cache.push(ch);
  11. }
  12. for (let i = 0; i < exclude.length; i++) {
  13. const ch = exclude.charCodeAt(i);
  14. cache[ch] = '%' + ('0' + ch.toString(16).toUpperCase()).slice(-2);
  15. }
  16. return cache
  17. }
  18. // Decode percent-encoded string.
  19. //
  20. function decode (string, exclude) {
  21. if (typeof exclude !== 'string') {
  22. exclude = decode.defaultChars;
  23. }
  24. const cache = getDecodeCache(exclude);
  25. return string.replace(/(%[a-f0-9]{2})+/gi, function (seq) {
  26. let result = '';
  27. for (let i = 0, l = seq.length; i < l; i += 3) {
  28. const b1 = parseInt(seq.slice(i + 1, i + 3), 16);
  29. if (b1 < 0x80) {
  30. result += cache[b1];
  31. continue
  32. }
  33. if ((b1 & 0xE0) === 0xC0 && (i + 3 < l)) {
  34. // 110xxxxx 10xxxxxx
  35. const b2 = parseInt(seq.slice(i + 4, i + 6), 16);
  36. if ((b2 & 0xC0) === 0x80) {
  37. const chr = ((b1 << 6) & 0x7C0) | (b2 & 0x3F);
  38. if (chr < 0x80) {
  39. result += '\ufffd\ufffd';
  40. } else {
  41. result += String.fromCharCode(chr);
  42. }
  43. i += 3;
  44. continue
  45. }
  46. }
  47. if ((b1 & 0xF0) === 0xE0 && (i + 6 < l)) {
  48. // 1110xxxx 10xxxxxx 10xxxxxx
  49. const b2 = parseInt(seq.slice(i + 4, i + 6), 16);
  50. const b3 = parseInt(seq.slice(i + 7, i + 9), 16);
  51. if ((b2 & 0xC0) === 0x80 && (b3 & 0xC0) === 0x80) {
  52. const chr = ((b1 << 12) & 0xF000) | ((b2 << 6) & 0xFC0) | (b3 & 0x3F);
  53. if (chr < 0x800 || (chr >= 0xD800 && chr <= 0xDFFF)) {
  54. result += '\ufffd\ufffd\ufffd';
  55. } else {
  56. result += String.fromCharCode(chr);
  57. }
  58. i += 6;
  59. continue
  60. }
  61. }
  62. if ((b1 & 0xF8) === 0xF0 && (i + 9 < l)) {
  63. // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx
  64. const b2 = parseInt(seq.slice(i + 4, i + 6), 16);
  65. const b3 = parseInt(seq.slice(i + 7, i + 9), 16);
  66. const b4 = parseInt(seq.slice(i + 10, i + 12), 16);
  67. if ((b2 & 0xC0) === 0x80 && (b3 & 0xC0) === 0x80 && (b4 & 0xC0) === 0x80) {
  68. let chr = ((b1 << 18) & 0x1C0000) | ((b2 << 12) & 0x3F000) | ((b3 << 6) & 0xFC0) | (b4 & 0x3F);
  69. if (chr < 0x10000 || chr > 0x10FFFF) {
  70. result += '\ufffd\ufffd\ufffd\ufffd';
  71. } else {
  72. chr -= 0x10000;
  73. result += String.fromCharCode(0xD800 + (chr >> 10), 0xDC00 + (chr & 0x3FF));
  74. }
  75. i += 9;
  76. continue
  77. }
  78. }
  79. result += '\ufffd';
  80. }
  81. return result
  82. })
  83. }
  84. decode.defaultChars = ';/?:@&=+$,#';
  85. decode.componentChars = '';
  86. const encodeCache = {};
  87. // Create a lookup array where anything but characters in `chars` string
  88. // and alphanumeric chars is percent-encoded.
  89. //
  90. function getEncodeCache (exclude) {
  91. let cache = encodeCache[exclude];
  92. if (cache) { return cache }
  93. cache = encodeCache[exclude] = [];
  94. for (let i = 0; i < 128; i++) {
  95. const ch = String.fromCharCode(i);
  96. if (/^[0-9a-z]$/i.test(ch)) {
  97. // always allow unencoded alphanumeric characters
  98. cache.push(ch);
  99. } else {
  100. cache.push('%' + ('0' + i.toString(16).toUpperCase()).slice(-2));
  101. }
  102. }
  103. for (let i = 0; i < exclude.length; i++) {
  104. cache[exclude.charCodeAt(i)] = exclude[i];
  105. }
  106. return cache
  107. }
  108. // Encode unsafe characters with percent-encoding, skipping already
  109. // encoded sequences.
  110. //
  111. // - string - string to encode
  112. // - exclude - list of characters to ignore (in addition to a-zA-Z0-9)
  113. // - keepEscaped - don't encode '%' in a correct escape sequence (default: true)
  114. //
  115. function encode (string, exclude, keepEscaped) {
  116. if (typeof exclude !== 'string') {
  117. // encode(string, keepEscaped)
  118. keepEscaped = exclude;
  119. exclude = encode.defaultChars;
  120. }
  121. if (typeof keepEscaped === 'undefined') {
  122. keepEscaped = true;
  123. }
  124. const cache = getEncodeCache(exclude);
  125. let result = '';
  126. for (let i = 0, l = string.length; i < l; i++) {
  127. const code = string.charCodeAt(i);
  128. if (keepEscaped && code === 0x25 /* % */ && i + 2 < l) {
  129. if (/^[0-9a-f]{2}$/i.test(string.slice(i + 1, i + 3))) {
  130. result += string.slice(i, i + 3);
  131. i += 2;
  132. continue
  133. }
  134. }
  135. if (code < 128) {
  136. result += cache[code];
  137. continue
  138. }
  139. if (code >= 0xD800 && code <= 0xDFFF) {
  140. if (code >= 0xD800 && code <= 0xDBFF && i + 1 < l) {
  141. const nextCode = string.charCodeAt(i + 1);
  142. if (nextCode >= 0xDC00 && nextCode <= 0xDFFF) {
  143. result += encodeURIComponent(string[i] + string[i + 1]);
  144. i++;
  145. continue
  146. }
  147. }
  148. result += '%EF%BF%BD';
  149. continue
  150. }
  151. result += encodeURIComponent(string[i]);
  152. }
  153. return result
  154. }
  155. encode.defaultChars = ";/?:@&=+$,-_.!~*'()#";
  156. encode.componentChars = "-_.!~*'()";
  157. function format (url) {
  158. let result = '';
  159. result += url.protocol || '';
  160. result += url.slashes ? '//' : '';
  161. result += url.auth ? url.auth + '@' : '';
  162. if (url.hostname && url.hostname.indexOf(':') !== -1) {
  163. // ipv6 address
  164. result += '[' + url.hostname + ']';
  165. } else {
  166. result += url.hostname || '';
  167. }
  168. result += url.port ? ':' + url.port : '';
  169. result += url.pathname || '';
  170. result += url.search || '';
  171. result += url.hash || '';
  172. return result
  173. }
  174. // Copyright Joyent, Inc. and other Node contributors.
  175. //
  176. // Permission is hereby granted, free of charge, to any person obtaining a
  177. // copy of this software and associated documentation files (the
  178. // "Software"), to deal in the Software without restriction, including
  179. // without limitation the rights to use, copy, modify, merge, publish,
  180. // distribute, sublicense, and/or sell copies of the Software, and to permit
  181. // persons to whom the Software is furnished to do so, subject to the
  182. // following conditions:
  183. //
  184. // The above copyright notice and this permission notice shall be included
  185. // in all copies or substantial portions of the Software.
  186. //
  187. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  188. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  189. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  190. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  191. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  192. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  193. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  194. //
  195. // Changes from joyent/node:
  196. //
  197. // 1. No leading slash in paths,
  198. // e.g. in `url.parse('http://foo?bar')` pathname is ``, not `/`
  199. //
  200. // 2. Backslashes are not replaced with slashes,
  201. // so `http:\\example.org\` is treated like a relative path
  202. //
  203. // 3. Trailing colon is treated like a part of the path,
  204. // i.e. in `http://example.org:foo` pathname is `:foo`
  205. //
  206. // 4. Nothing is URL-encoded in the resulting object,
  207. // (in joyent/node some chars in auth and paths are encoded)
  208. //
  209. // 5. `url.parse()` does not have `parseQueryString` argument
  210. //
  211. // 6. Removed extraneous result properties: `host`, `path`, `query`, etc.,
  212. // which can be constructed using other parts of the url.
  213. //
  214. function Url () {
  215. this.protocol = null;
  216. this.slashes = null;
  217. this.auth = null;
  218. this.port = null;
  219. this.hostname = null;
  220. this.hash = null;
  221. this.search = null;
  222. this.pathname = null;
  223. }
  224. // Reference: RFC 3986, RFC 1808, RFC 2396
  225. // define these here so at least they only have to be
  226. // compiled once on the first module load.
  227. const protocolPattern = /^([a-z0-9.+-]+:)/i;
  228. const portPattern = /:[0-9]*$/;
  229. // Special case for a simple path URL
  230. /* eslint-disable-next-line no-useless-escape */
  231. const simplePathPattern = /^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/;
  232. // RFC 2396: characters reserved for delimiting URLs.
  233. // We actually just auto-escape these.
  234. const delims = ['<', '>', '"', '`', ' ', '\r', '\n', '\t'];
  235. // RFC 2396: characters not allowed for various reasons.
  236. const unwise = ['{', '}', '|', '\\', '^', '`'].concat(delims);
  237. // Allowed by RFCs, but cause of XSS attacks. Always escape these.
  238. const autoEscape = ['\''].concat(unwise);
  239. // Characters that are never ever allowed in a hostname.
  240. // Note that any invalid chars are also handled, but these
  241. // are the ones that are *expected* to be seen, so we fast-path
  242. // them.
  243. const nonHostChars = ['%', '/', '?', ';', '#'].concat(autoEscape);
  244. const hostEndingChars = ['/', '?', '#'];
  245. const hostnameMaxLen = 255;
  246. const hostnamePartPattern = /^[+a-z0-9A-Z_-]{0,63}$/;
  247. const hostnamePartStart = /^([+a-z0-9A-Z_-]{0,63})(.*)$/;
  248. // protocols that can allow "unsafe" and "unwise" chars.
  249. // protocols that never have a hostname.
  250. const hostlessProtocol = {
  251. javascript: true,
  252. 'javascript:': true
  253. };
  254. // protocols that always contain a // bit.
  255. const slashedProtocol = {
  256. http: true,
  257. https: true,
  258. ftp: true,
  259. gopher: true,
  260. file: true,
  261. 'http:': true,
  262. 'https:': true,
  263. 'ftp:': true,
  264. 'gopher:': true,
  265. 'file:': true
  266. };
  267. function urlParse (url, slashesDenoteHost) {
  268. if (url && url instanceof Url) return url
  269. const u = new Url();
  270. u.parse(url, slashesDenoteHost);
  271. return u
  272. }
  273. Url.prototype.parse = function (url, slashesDenoteHost) {
  274. let lowerProto, hec, slashes;
  275. let rest = url;
  276. // trim before proceeding.
  277. // This is to support parse stuff like " http://foo.com \n"
  278. rest = rest.trim();
  279. if (!slashesDenoteHost && url.split('#').length === 1) {
  280. // Try fast path regexp
  281. const simplePath = simplePathPattern.exec(rest);
  282. if (simplePath) {
  283. this.pathname = simplePath[1];
  284. if (simplePath[2]) {
  285. this.search = simplePath[2];
  286. }
  287. return this
  288. }
  289. }
  290. let proto = protocolPattern.exec(rest);
  291. if (proto) {
  292. proto = proto[0];
  293. lowerProto = proto.toLowerCase();
  294. this.protocol = proto;
  295. rest = rest.substr(proto.length);
  296. }
  297. // figure out if it's got a host
  298. // user@server is *always* interpreted as a hostname, and url
  299. // resolution will treat //foo/bar as host=foo,path=bar because that's
  300. // how the browser resolves relative URLs.
  301. /* eslint-disable-next-line no-useless-escape */
  302. if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) {
  303. slashes = rest.substr(0, 2) === '//';
  304. if (slashes && !(proto && hostlessProtocol[proto])) {
  305. rest = rest.substr(2);
  306. this.slashes = true;
  307. }
  308. }
  309. if (!hostlessProtocol[proto] &&
  310. (slashes || (proto && !slashedProtocol[proto]))) {
  311. // there's a hostname.
  312. // the first instance of /, ?, ;, or # ends the host.
  313. //
  314. // If there is an @ in the hostname, then non-host chars *are* allowed
  315. // to the left of the last @ sign, unless some host-ending character
  316. // comes *before* the @-sign.
  317. // URLs are obnoxious.
  318. //
  319. // ex:
  320. // http://a@b@c/ => user:a@b host:c
  321. // http://a@b?@c => user:a host:c path:/?@c
  322. // v0.12 TODO(isaacs): This is not quite how Chrome does things.
  323. // Review our test case against browsers more comprehensively.
  324. // find the first instance of any hostEndingChars
  325. let hostEnd = -1;
  326. for (let i = 0; i < hostEndingChars.length; i++) {
  327. hec = rest.indexOf(hostEndingChars[i]);
  328. if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) {
  329. hostEnd = hec;
  330. }
  331. }
  332. // at this point, either we have an explicit point where the
  333. // auth portion cannot go past, or the last @ char is the decider.
  334. let auth, atSign;
  335. if (hostEnd === -1) {
  336. // atSign can be anywhere.
  337. atSign = rest.lastIndexOf('@');
  338. } else {
  339. // atSign must be in auth portion.
  340. // http://a@b/c@d => host:b auth:a path:/c@d
  341. atSign = rest.lastIndexOf('@', hostEnd);
  342. }
  343. // Now we have a portion which is definitely the auth.
  344. // Pull that off.
  345. if (atSign !== -1) {
  346. auth = rest.slice(0, atSign);
  347. rest = rest.slice(atSign + 1);
  348. this.auth = auth;
  349. }
  350. // the host is the remaining to the left of the first non-host char
  351. hostEnd = -1;
  352. for (let i = 0; i < nonHostChars.length; i++) {
  353. hec = rest.indexOf(nonHostChars[i]);
  354. if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) {
  355. hostEnd = hec;
  356. }
  357. }
  358. // if we still have not hit it, then the entire thing is a host.
  359. if (hostEnd === -1) {
  360. hostEnd = rest.length;
  361. }
  362. if (rest[hostEnd - 1] === ':') { hostEnd--; }
  363. const host = rest.slice(0, hostEnd);
  364. rest = rest.slice(hostEnd);
  365. // pull out port.
  366. this.parseHost(host);
  367. // we've indicated that there is a hostname,
  368. // so even if it's empty, it has to be present.
  369. this.hostname = this.hostname || '';
  370. // if hostname begins with [ and ends with ]
  371. // assume that it's an IPv6 address.
  372. const ipv6Hostname = this.hostname[0] === '[' &&
  373. this.hostname[this.hostname.length - 1] === ']';
  374. // validate a little.
  375. if (!ipv6Hostname) {
  376. const hostparts = this.hostname.split(/\./);
  377. for (let i = 0, l = hostparts.length; i < l; i++) {
  378. const part = hostparts[i];
  379. if (!part) { continue }
  380. if (!part.match(hostnamePartPattern)) {
  381. let newpart = '';
  382. for (let j = 0, k = part.length; j < k; j++) {
  383. if (part.charCodeAt(j) > 127) {
  384. // we replace non-ASCII char with a temporary placeholder
  385. // we need this to make sure size of hostname is not
  386. // broken by replacing non-ASCII by nothing
  387. newpart += 'x';
  388. } else {
  389. newpart += part[j];
  390. }
  391. }
  392. // we test again with ASCII char only
  393. if (!newpart.match(hostnamePartPattern)) {
  394. const validParts = hostparts.slice(0, i);
  395. const notHost = hostparts.slice(i + 1);
  396. const bit = part.match(hostnamePartStart);
  397. if (bit) {
  398. validParts.push(bit[1]);
  399. notHost.unshift(bit[2]);
  400. }
  401. if (notHost.length) {
  402. rest = notHost.join('.') + rest;
  403. }
  404. this.hostname = validParts.join('.');
  405. break
  406. }
  407. }
  408. }
  409. }
  410. if (this.hostname.length > hostnameMaxLen) {
  411. this.hostname = '';
  412. }
  413. // strip [ and ] from the hostname
  414. // the host field still retains them, though
  415. if (ipv6Hostname) {
  416. this.hostname = this.hostname.substr(1, this.hostname.length - 2);
  417. }
  418. }
  419. // chop off from the tail first.
  420. const hash = rest.indexOf('#');
  421. if (hash !== -1) {
  422. // got a fragment string.
  423. this.hash = rest.substr(hash);
  424. rest = rest.slice(0, hash);
  425. }
  426. const qm = rest.indexOf('?');
  427. if (qm !== -1) {
  428. this.search = rest.substr(qm);
  429. rest = rest.slice(0, qm);
  430. }
  431. if (rest) { this.pathname = rest; }
  432. if (slashedProtocol[lowerProto] &&
  433. this.hostname && !this.pathname) {
  434. this.pathname = '';
  435. }
  436. return this
  437. };
  438. Url.prototype.parseHost = function (host) {
  439. let port = portPattern.exec(host);
  440. if (port) {
  441. port = port[0];
  442. if (port !== ':') {
  443. this.port = port.substr(1);
  444. }
  445. host = host.substr(0, host.length - port.length);
  446. }
  447. if (host) { this.hostname = host; }
  448. };
  449. exports.decode = decode;
  450. exports.encode = encode;
  451. exports.format = format;
  452. exports.parse = urlParse;