source: node_modules/url-parse/dist/url-parse.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 20.6 KB
Line 
1(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.URLParse = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
2(function (global){(function (){
3'use strict';
4
5var required = require('requires-port')
6 , qs = require('querystringify')
7 , controlOrWhitespace = /^[\x00-\x20\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]+/
8 , CRHTLF = /[\n\r\t]/g
9 , slashes = /^[A-Za-z][A-Za-z0-9+-.]*:\/\//
10 , port = /:\d+$/
11 , protocolre = /^([a-z][a-z0-9.+-]*:)?(\/\/)?([\\/]+)?([\S\s]*)/i
12 , windowsDriveLetter = /^[a-zA-Z]:/;
13
14/**
15 * Remove control characters and whitespace from the beginning of a string.
16 *
17 * @param {Object|String} str String to trim.
18 * @returns {String} A new string representing `str` stripped of control
19 * characters and whitespace from its beginning.
20 * @public
21 */
22function trimLeft(str) {
23 return (str ? str : '').toString().replace(controlOrWhitespace, '');
24}
25
26/**
27 * These are the parse rules for the URL parser, it informs the parser
28 * about:
29 *
30 * 0. The char it Needs to parse, if it's a string it should be done using
31 * indexOf, RegExp using exec and NaN means set as current value.
32 * 1. The property we should set when parsing this value.
33 * 2. Indication if it's backwards or forward parsing, when set as number it's
34 * the value of extra chars that should be split off.
35 * 3. Inherit from location if non existing in the parser.
36 * 4. `toLowerCase` the resulting value.
37 */
38var rules = [
39 ['#', 'hash'], // Extract from the back.
40 ['?', 'query'], // Extract from the back.
41 function sanitize(address, url) { // Sanitize what is left of the address
42 return isSpecial(url.protocol) ? address.replace(/\\/g, '/') : address;
43 },
44 ['/', 'pathname'], // Extract from the back.
45 ['@', 'auth', 1], // Extract from the front.
46 [NaN, 'host', undefined, 1, 1], // Set left over value.
47 [/:(\d*)$/, 'port', undefined, 1], // RegExp the back.
48 [NaN, 'hostname', undefined, 1, 1] // Set left over.
49];
50
51/**
52 * These properties should not be copied or inherited from. This is only needed
53 * for all non blob URL's as a blob URL does not include a hash, only the
54 * origin.
55 *
56 * @type {Object}
57 * @private
58 */
59var ignore = { hash: 1, query: 1 };
60
61/**
62 * The location object differs when your code is loaded through a normal page,
63 * Worker or through a worker using a blob. And with the blobble begins the
64 * trouble as the location object will contain the URL of the blob, not the
65 * location of the page where our code is loaded in. The actual origin is
66 * encoded in the `pathname` so we can thankfully generate a good "default"
67 * location from it so we can generate proper relative URL's again.
68 *
69 * @param {Object|String} loc Optional default location object.
70 * @returns {Object} lolcation object.
71 * @public
72 */
73function lolcation(loc) {
74 var globalVar;
75
76 if (typeof window !== 'undefined') globalVar = window;
77 else if (typeof global !== 'undefined') globalVar = global;
78 else if (typeof self !== 'undefined') globalVar = self;
79 else globalVar = {};
80
81 var location = globalVar.location || {};
82 loc = loc || location;
83
84 var finaldestination = {}
85 , type = typeof loc
86 , key;
87
88 if ('blob:' === loc.protocol) {
89 finaldestination = new Url(unescape(loc.pathname), {});
90 } else if ('string' === type) {
91 finaldestination = new Url(loc, {});
92 for (key in ignore) delete finaldestination[key];
93 } else if ('object' === type) {
94 for (key in loc) {
95 if (key in ignore) continue;
96 finaldestination[key] = loc[key];
97 }
98
99 if (finaldestination.slashes === undefined) {
100 finaldestination.slashes = slashes.test(loc.href);
101 }
102 }
103
104 return finaldestination;
105}
106
107/**
108 * Check whether a protocol scheme is special.
109 *
110 * @param {String} The protocol scheme of the URL
111 * @return {Boolean} `true` if the protocol scheme is special, else `false`
112 * @private
113 */
114function isSpecial(scheme) {
115 return (
116 scheme === 'file:' ||
117 scheme === 'ftp:' ||
118 scheme === 'http:' ||
119 scheme === 'https:' ||
120 scheme === 'ws:' ||
121 scheme === 'wss:'
122 );
123}
124
125/**
126 * @typedef ProtocolExtract
127 * @type Object
128 * @property {String} protocol Protocol matched in the URL, in lowercase.
129 * @property {Boolean} slashes `true` if protocol is followed by "//", else `false`.
130 * @property {String} rest Rest of the URL that is not part of the protocol.
131 */
132
133/**
134 * Extract protocol information from a URL with/without double slash ("//").
135 *
136 * @param {String} address URL we want to extract from.
137 * @param {Object} location
138 * @return {ProtocolExtract} Extracted information.
139 * @private
140 */
141function extractProtocol(address, location) {
142 address = trimLeft(address);
143 address = address.replace(CRHTLF, '');
144 location = location || {};
145
146 var match = protocolre.exec(address);
147 var protocol = match[1] ? match[1].toLowerCase() : '';
148 var forwardSlashes = !!match[2];
149 var otherSlashes = !!match[3];
150 var slashesCount = 0;
151 var rest;
152
153 if (forwardSlashes) {
154 if (otherSlashes) {
155 rest = match[2] + match[3] + match[4];
156 slashesCount = match[2].length + match[3].length;
157 } else {
158 rest = match[2] + match[4];
159 slashesCount = match[2].length;
160 }
161 } else {
162 if (otherSlashes) {
163 rest = match[3] + match[4];
164 slashesCount = match[3].length;
165 } else {
166 rest = match[4]
167 }
168 }
169
170 if (protocol === 'file:') {
171 if (slashesCount >= 2) {
172 rest = rest.slice(2);
173 }
174 } else if (isSpecial(protocol)) {
175 rest = match[4];
176 } else if (protocol) {
177 if (forwardSlashes) {
178 rest = rest.slice(2);
179 }
180 } else if (slashesCount >= 2 && isSpecial(location.protocol)) {
181 rest = match[4];
182 }
183
184 return {
185 protocol: protocol,
186 slashes: forwardSlashes || isSpecial(protocol),
187 slashesCount: slashesCount,
188 rest: rest
189 };
190}
191
192/**
193 * Resolve a relative URL pathname against a base URL pathname.
194 *
195 * @param {String} relative Pathname of the relative URL.
196 * @param {String} base Pathname of the base URL.
197 * @return {String} Resolved pathname.
198 * @private
199 */
200function resolve(relative, base) {
201 if (relative === '') return base;
202
203 var path = (base || '/').split('/').slice(0, -1).concat(relative.split('/'))
204 , i = path.length
205 , last = path[i - 1]
206 , unshift = false
207 , up = 0;
208
209 while (i--) {
210 if (path[i] === '.') {
211 path.splice(i, 1);
212 } else if (path[i] === '..') {
213 path.splice(i, 1);
214 up++;
215 } else if (up) {
216 if (i === 0) unshift = true;
217 path.splice(i, 1);
218 up--;
219 }
220 }
221
222 if (unshift) path.unshift('');
223 if (last === '.' || last === '..') path.push('');
224
225 return path.join('/');
226}
227
228/**
229 * The actual URL instance. Instead of returning an object we've opted-in to
230 * create an actual constructor as it's much more memory efficient and
231 * faster and it pleases my OCD.
232 *
233 * It is worth noting that we should not use `URL` as class name to prevent
234 * clashes with the global URL instance that got introduced in browsers.
235 *
236 * @constructor
237 * @param {String} address URL we want to parse.
238 * @param {Object|String} [location] Location defaults for relative paths.
239 * @param {Boolean|Function} [parser] Parser for the query string.
240 * @private
241 */
242function Url(address, location, parser) {
243 address = trimLeft(address);
244 address = address.replace(CRHTLF, '');
245
246 if (!(this instanceof Url)) {
247 return new Url(address, location, parser);
248 }
249
250 var relative, extracted, parse, instruction, index, key
251 , instructions = rules.slice()
252 , type = typeof location
253 , url = this
254 , i = 0;
255
256 //
257 // The following if statements allows this module two have compatibility with
258 // 2 different API:
259 //
260 // 1. Node.js's `url.parse` api which accepts a URL, boolean as arguments
261 // where the boolean indicates that the query string should also be parsed.
262 //
263 // 2. The `URL` interface of the browser which accepts a URL, object as
264 // arguments. The supplied object will be used as default values / fall-back
265 // for relative paths.
266 //
267 if ('object' !== type && 'string' !== type) {
268 parser = location;
269 location = null;
270 }
271
272 if (parser && 'function' !== typeof parser) parser = qs.parse;
273
274 location = lolcation(location);
275
276 //
277 // Extract protocol information before running the instructions.
278 //
279 extracted = extractProtocol(address || '', location);
280 relative = !extracted.protocol && !extracted.slashes;
281 url.slashes = extracted.slashes || relative && location.slashes;
282 url.protocol = extracted.protocol || location.protocol || '';
283 address = extracted.rest;
284
285 //
286 // When the authority component is absent the URL starts with a path
287 // component.
288 //
289 if (
290 extracted.protocol === 'file:' && (
291 extracted.slashesCount !== 2 || windowsDriveLetter.test(address)) ||
292 (!extracted.slashes &&
293 (extracted.protocol ||
294 extracted.slashesCount < 2 ||
295 !isSpecial(url.protocol)))
296 ) {
297 instructions[3] = [/(.*)/, 'pathname'];
298 }
299
300 for (; i < instructions.length; i++) {
301 instruction = instructions[i];
302
303 if (typeof instruction === 'function') {
304 address = instruction(address, url);
305 continue;
306 }
307
308 parse = instruction[0];
309 key = instruction[1];
310
311 if (parse !== parse) {
312 url[key] = address;
313 } else if ('string' === typeof parse) {
314 index = parse === '@'
315 ? address.lastIndexOf(parse)
316 : address.indexOf(parse);
317
318 if (~index) {
319 if ('number' === typeof instruction[2]) {
320 url[key] = address.slice(0, index);
321 address = address.slice(index + instruction[2]);
322 } else {
323 url[key] = address.slice(index);
324 address = address.slice(0, index);
325 }
326 }
327 } else if ((index = parse.exec(address))) {
328 url[key] = index[1];
329 address = address.slice(0, index.index);
330 }
331
332 url[key] = url[key] || (
333 relative && instruction[3] ? location[key] || '' : ''
334 );
335
336 //
337 // Hostname, host and protocol should be lowercased so they can be used to
338 // create a proper `origin`.
339 //
340 if (instruction[4]) url[key] = url[key].toLowerCase();
341 }
342
343 //
344 // Also parse the supplied query string in to an object. If we're supplied
345 // with a custom parser as function use that instead of the default build-in
346 // parser.
347 //
348 if (parser) url.query = parser(url.query);
349
350 //
351 // If the URL is relative, resolve the pathname against the base URL.
352 //
353 if (
354 relative
355 && location.slashes
356 && url.pathname.charAt(0) !== '/'
357 && (url.pathname !== '' || location.pathname !== '')
358 ) {
359 url.pathname = resolve(url.pathname, location.pathname);
360 }
361
362 //
363 // Default to a / for pathname if none exists. This normalizes the URL
364 // to always have a /
365 //
366 if (url.pathname.charAt(0) !== '/' && isSpecial(url.protocol)) {
367 url.pathname = '/' + url.pathname;
368 }
369
370 //
371 // We should not add port numbers if they are already the default port number
372 // for a given protocol. As the host also contains the port number we're going
373 // override it with the hostname which contains no port number.
374 //
375 if (!required(url.port, url.protocol)) {
376 url.host = url.hostname;
377 url.port = '';
378 }
379
380 //
381 // Parse down the `auth` for the username and password.
382 //
383 url.username = url.password = '';
384
385 if (url.auth) {
386 index = url.auth.indexOf(':');
387
388 if (~index) {
389 url.username = url.auth.slice(0, index);
390 url.username = encodeURIComponent(decodeURIComponent(url.username));
391
392 url.password = url.auth.slice(index + 1);
393 url.password = encodeURIComponent(decodeURIComponent(url.password))
394 } else {
395 url.username = encodeURIComponent(decodeURIComponent(url.auth));
396 }
397
398 url.auth = url.password ? url.username +':'+ url.password : url.username;
399 }
400
401 url.origin = url.protocol !== 'file:' && isSpecial(url.protocol) && url.host
402 ? url.protocol +'//'+ url.host
403 : 'null';
404
405 //
406 // The href is just the compiled result.
407 //
408 url.href = url.toString();
409}
410
411/**
412 * This is convenience method for changing properties in the URL instance to
413 * insure that they all propagate correctly.
414 *
415 * @param {String} part Property we need to adjust.
416 * @param {Mixed} value The newly assigned value.
417 * @param {Boolean|Function} fn When setting the query, it will be the function
418 * used to parse the query.
419 * When setting the protocol, double slash will be
420 * removed from the final url if it is true.
421 * @returns {URL} URL instance for chaining.
422 * @public
423 */
424function set(part, value, fn) {
425 var url = this;
426
427 switch (part) {
428 case 'query':
429 if ('string' === typeof value && value.length) {
430 value = (fn || qs.parse)(value);
431 }
432
433 url[part] = value;
434 break;
435
436 case 'port':
437 url[part] = value;
438
439 if (!required(value, url.protocol)) {
440 url.host = url.hostname;
441 url[part] = '';
442 } else if (value) {
443 url.host = url.hostname +':'+ value;
444 }
445
446 break;
447
448 case 'hostname':
449 url[part] = value;
450
451 if (url.port) value += ':'+ url.port;
452 url.host = value;
453 break;
454
455 case 'host':
456 url[part] = value;
457
458 if (port.test(value)) {
459 value = value.split(':');
460 url.port = value.pop();
461 url.hostname = value.join(':');
462 } else {
463 url.hostname = value;
464 url.port = '';
465 }
466
467 break;
468
469 case 'protocol':
470 url.protocol = value.toLowerCase();
471 url.slashes = !fn;
472 break;
473
474 case 'pathname':
475 case 'hash':
476 if (value) {
477 var char = part === 'pathname' ? '/' : '#';
478 url[part] = value.charAt(0) !== char ? char + value : value;
479 } else {
480 url[part] = value;
481 }
482 break;
483
484 case 'username':
485 case 'password':
486 url[part] = encodeURIComponent(value);
487 break;
488
489 case 'auth':
490 var index = value.indexOf(':');
491
492 if (~index) {
493 url.username = value.slice(0, index);
494 url.username = encodeURIComponent(decodeURIComponent(url.username));
495
496 url.password = value.slice(index + 1);
497 url.password = encodeURIComponent(decodeURIComponent(url.password));
498 } else {
499 url.username = encodeURIComponent(decodeURIComponent(value));
500 }
501 }
502
503 for (var i = 0; i < rules.length; i++) {
504 var ins = rules[i];
505
506 if (ins[4]) url[ins[1]] = url[ins[1]].toLowerCase();
507 }
508
509 url.auth = url.password ? url.username +':'+ url.password : url.username;
510
511 url.origin = url.protocol !== 'file:' && isSpecial(url.protocol) && url.host
512 ? url.protocol +'//'+ url.host
513 : 'null';
514
515 url.href = url.toString();
516
517 return url;
518}
519
520/**
521 * Transform the properties back in to a valid and full URL string.
522 *
523 * @param {Function} stringify Optional query stringify function.
524 * @returns {String} Compiled version of the URL.
525 * @public
526 */
527function toString(stringify) {
528 if (!stringify || 'function' !== typeof stringify) stringify = qs.stringify;
529
530 var query
531 , url = this
532 , host = url.host
533 , protocol = url.protocol;
534
535 if (protocol && protocol.charAt(protocol.length - 1) !== ':') protocol += ':';
536
537 var result =
538 protocol +
539 ((url.protocol && url.slashes) || isSpecial(url.protocol) ? '//' : '');
540
541 if (url.username) {
542 result += url.username;
543 if (url.password) result += ':'+ url.password;
544 result += '@';
545 } else if (url.password) {
546 result += ':'+ url.password;
547 result += '@';
548 } else if (
549 url.protocol !== 'file:' &&
550 isSpecial(url.protocol) &&
551 !host &&
552 url.pathname !== '/'
553 ) {
554 //
555 // Add back the empty userinfo, otherwise the original invalid URL
556 // might be transformed into a valid one with `url.pathname` as host.
557 //
558 result += '@';
559 }
560
561 //
562 // Trailing colon is removed from `url.host` when it is parsed. If it still
563 // ends with a colon, then add back the trailing colon that was removed. This
564 // prevents an invalid URL from being transformed into a valid one.
565 //
566 if (host[host.length - 1] === ':' || (port.test(url.hostname) && !url.port)) {
567 host += ':';
568 }
569
570 result += host + url.pathname;
571
572 query = 'object' === typeof url.query ? stringify(url.query) : url.query;
573 if (query) result += '?' !== query.charAt(0) ? '?'+ query : query;
574
575 if (url.hash) result += url.hash;
576
577 return result;
578}
579
580Url.prototype = { set: set, toString: toString };
581
582//
583// Expose the URL parser and some additional properties that might be useful for
584// others or testing.
585//
586Url.extractProtocol = extractProtocol;
587Url.location = lolcation;
588Url.trimLeft = trimLeft;
589Url.qs = qs;
590
591module.exports = Url;
592
593}).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
594},{"querystringify":2,"requires-port":3}],2:[function(require,module,exports){
595'use strict';
596
597var has = Object.prototype.hasOwnProperty
598 , undef;
599
600/**
601 * Decode a URI encoded string.
602 *
603 * @param {String} input The URI encoded string.
604 * @returns {String|Null} The decoded string.
605 * @api private
606 */
607function decode(input) {
608 try {
609 return decodeURIComponent(input.replace(/\+/g, ' '));
610 } catch (e) {
611 return null;
612 }
613}
614
615/**
616 * Attempts to encode a given input.
617 *
618 * @param {String} input The string that needs to be encoded.
619 * @returns {String|Null} The encoded string.
620 * @api private
621 */
622function encode(input) {
623 try {
624 return encodeURIComponent(input);
625 } catch (e) {
626 return null;
627 }
628}
629
630/**
631 * Simple query string parser.
632 *
633 * @param {String} query The query string that needs to be parsed.
634 * @returns {Object}
635 * @api public
636 */
637function querystring(query) {
638 var parser = /([^=?#&]+)=?([^&]*)/g
639 , result = {}
640 , part;
641
642 while (part = parser.exec(query)) {
643 var key = decode(part[1])
644 , value = decode(part[2]);
645
646 //
647 // Prevent overriding of existing properties. This ensures that build-in
648 // methods like `toString` or __proto__ are not overriden by malicious
649 // querystrings.
650 //
651 // In the case if failed decoding, we want to omit the key/value pairs
652 // from the result.
653 //
654 if (key === null || value === null || key in result) continue;
655 result[key] = value;
656 }
657
658 return result;
659}
660
661/**
662 * Transform a query string to an object.
663 *
664 * @param {Object} obj Object that should be transformed.
665 * @param {String} prefix Optional prefix.
666 * @returns {String}
667 * @api public
668 */
669function querystringify(obj, prefix) {
670 prefix = prefix || '';
671
672 var pairs = []
673 , value
674 , key;
675
676 //
677 // Optionally prefix with a '?' if needed
678 //
679 if ('string' !== typeof prefix) prefix = '?';
680
681 for (key in obj) {
682 if (has.call(obj, key)) {
683 value = obj[key];
684
685 //
686 // Edge cases where we actually want to encode the value to an empty
687 // string instead of the stringified value.
688 //
689 if (!value && (value === null || value === undef || isNaN(value))) {
690 value = '';
691 }
692
693 key = encode(key);
694 value = encode(value);
695
696 //
697 // If we failed to encode the strings, we should bail out as we don't
698 // want to add invalid strings to the query.
699 //
700 if (key === null || value === null) continue;
701 pairs.push(key +'='+ value);
702 }
703 }
704
705 return pairs.length ? prefix + pairs.join('&') : '';
706}
707
708//
709// Expose the module.
710//
711exports.stringify = querystringify;
712exports.parse = querystring;
713
714},{}],3:[function(require,module,exports){
715'use strict';
716
717/**
718 * Check if we're required to add a port number.
719 *
720 * @see https://url.spec.whatwg.org/#default-port
721 * @param {Number|String} port Port number we need to check
722 * @param {String} protocol Protocol we need to check against.
723 * @returns {Boolean} Is it a default port for the given protocol
724 * @api private
725 */
726module.exports = function required(port, protocol) {
727 protocol = protocol.split(':')[0];
728 port = +port;
729
730 if (!port) return false;
731
732 switch (protocol) {
733 case 'http':
734 case 'ws':
735 return port !== 80;
736
737 case 'https':
738 case 'wss':
739 return port !== 443;
740
741 case 'ftp':
742 return port !== 21;
743
744 case 'gopher':
745 return port !== 70;
746
747 case 'file':
748 return false;
749 }
750
751 return port !== 0;
752};
753
754},{}]},{},[1])(1)
755});
Note: See TracBrowser for help on using the repository browser.