msw 0.20.5 → 0.21.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/README.md +1 -1
- package/lib/esm/fetch-deps.js +26 -23
- package/lib/esm/graphql.js +92 -79
- package/lib/esm/index.js +90 -44
- package/lib/esm/{matchRequest-deps.js → matchRequestUrl-deps.js} +29 -17
- package/lib/esm/mockServiceWorker.js +8 -1
- package/lib/esm/rest-deps.js +27 -18
- package/lib/esm/rest.js +1 -1
- package/lib/esm/xml-deps.js +3 -3
- package/lib/types/LiveStorage.d.ts +17 -0
- package/lib/types/context/body.d.ts +2 -2
- package/lib/types/context/errors.d.ts +3 -1
- package/lib/types/context/fetch.d.ts +2 -2
- package/lib/types/context/json.d.ts +5 -3
- package/lib/types/context/text.d.ts +2 -2
- package/lib/types/context/xml.d.ts +2 -2
- package/lib/types/graphql.d.ts +8 -10
- package/lib/types/index.d.ts +3 -3
- package/lib/types/native/index.d.ts +7 -1
- package/lib/types/node/createSetupServer.d.ts +29 -0
- package/lib/types/node/setupServer.d.ts +4 -24
- package/lib/types/response.d.ts +10 -7
- package/lib/types/rest.d.ts +158 -16
- package/lib/types/setupWorker/glossary.d.ts +14 -4
- package/lib/types/setupWorker/start/utils/getWorkerByRegistration.d.ts +2 -1
- package/lib/types/setupWorker/start/utils/getWorkerInstance.d.ts +2 -2
- package/lib/types/sharedOptions.d.ts +1 -1
- package/lib/types/utils/getResponse.d.ts +2 -2
- package/lib/types/utils/handlers/requestHandler.d.ts +74 -0
- package/lib/types/utils/{requestHandlerUtils.d.ts → handlers/requestHandlerUtils.d.ts} +2 -2
- package/lib/types/utils/{isNodeProcess.d.ts → internal/isNodeProcess.d.ts} +1 -0
- package/lib/types/utils/{isStringEqual.d.ts → internal/isStringEqual.d.ts} +0 -0
- package/lib/types/utils/{jsonParse.d.ts → internal/jsonParse.d.ts} +0 -0
- package/lib/types/utils/internal/mergeRight.d.ts +5 -0
- package/lib/types/utils/{logger → logging}/getStatusCodeColor.d.ts +0 -0
- package/lib/types/utils/{logger → logging}/getTimestamp.d.ts +0 -0
- package/lib/types/utils/{logger → logging}/prepareRequest.d.ts +3 -3
- package/lib/types/utils/{logger → logging}/prepareResponse.d.ts +2 -2
- package/lib/types/utils/matching/{matchRequest.d.ts → matchRequestUrl.d.ts} +0 -0
- package/lib/types/utils/request/getPublicUrlFromRequest.d.ts +6 -0
- package/lib/types/utils/request/getRequestCookies.d.ts +1 -1
- package/lib/types/{onUnhandledRequest.d.ts → utils/request/onUnhandledRequest.d.ts} +1 -1
- package/lib/types/utils/request/parseBody.d.ts +5 -0
- package/lib/types/utils/url/getAbsoluteUrl.d.ts +6 -0
- package/lib/types/utils/{getAbsoluteWorkerUrl.d.ts → url/getAbsoluteWorkerUrl.d.ts} +0 -0
- package/lib/types/utils/url/getUrlByMask.d.ts +5 -0
- package/lib/umd/index.js +696 -613
- package/lib/umd/mockServiceWorker.js +8 -1
- package/native/index.js +1508 -133
- package/node/context/delay.d.ts +11 -0
- package/node/context/fetch.d.ts +8 -0
- package/node/context/set.d.ts +2 -0
- package/node/context/status.d.ts +2 -0
- package/node/index.js +1507 -133
- package/node/node/createSetupServer.d.ts +29 -0
- package/node/node/index.d.ts +5 -0
- package/node/node/setupServer.d.ts +7 -0
- package/node/response.d.ts +25 -0
- package/node/utils/NetworkError.d.ts +3 -0
- package/node/utils/getResponse.d.ts +14 -0
- package/{lib/types → node/utils}/handlers/requestHandler.d.ts +13 -12
- package/node/utils/handlers/requestHandlerUtils.d.ts +4 -0
- package/node/utils/internal/compose.d.ts +5 -0
- package/node/utils/internal/isNodeProcess.d.ts +5 -0
- package/node/utils/internal/jsonParse.d.ts +5 -0
- package/node/utils/request/getPublicUrlFromRequest.d.ts +6 -0
- package/node/utils/request/onUnhandledRequest.d.ts +5 -0
- package/node/utils/request/parseBody.d.ts +5 -0
- package/package.json +34 -29
- package/lib/types/utils/getJsonBody.d.ts +0 -5
- package/lib/types/utils/request/parseRequestBody.d.ts +0 -2
- package/lib/types/utils/resolveMask.d.ts +0 -6
- package/lib/types/utils/resolveRelativeUrl.d.ts +0 -6
package/node/index.js
CHANGED
|
@@ -2,22 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var ClientRequest = require('node-request-interceptor/lib/interceptors/ClientRequest');
|
|
6
|
+
var XMLHttpRequest = require('node-request-interceptor/lib/interceptors/XMLHttpRequest');
|
|
5
7
|
var timers = require('timers');
|
|
6
8
|
var nodeRequestInterceptor = require('node-request-interceptor');
|
|
7
9
|
|
|
8
10
|
/*! *****************************************************************************
|
|
9
|
-
Copyright (c) Microsoft Corporation.
|
|
10
|
-
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
11
|
-
this file except in compliance with the License. You may obtain a copy of the
|
|
12
|
-
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
Copyright (c) Microsoft Corporation.
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
|
17
|
-
MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
13
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
14
|
+
purpose with or without fee is hereby granted.
|
|
18
15
|
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
17
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
18
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
19
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
20
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
21
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
22
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
21
23
|
***************************************************************************** */
|
|
22
24
|
|
|
23
25
|
function __awaiter(thisArg, _arguments, P, generator) {
|
|
@@ -116,13 +118,15 @@ function tryDecode(str, decode) {
|
|
|
116
118
|
}
|
|
117
119
|
}
|
|
118
120
|
|
|
121
|
+
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
122
|
+
|
|
119
123
|
function createCommonjsModule(fn, basedir, module) {
|
|
120
124
|
return module = {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
125
|
+
path: basedir,
|
|
126
|
+
exports: {},
|
|
127
|
+
require: function (path, base) {
|
|
128
|
+
return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
|
|
129
|
+
}
|
|
126
130
|
}, fn(module, module.exports), module.exports;
|
|
127
131
|
}
|
|
128
132
|
|
|
@@ -466,16 +470,16 @@ function set(...args) {
|
|
|
466
470
|
|
|
467
471
|
/**
|
|
468
472
|
* Returns a boolean indicating if the current process is running in NodeJS environment.
|
|
473
|
+
* @see https://github.com/mswjs/msw/pull/255
|
|
469
474
|
*/
|
|
470
|
-
// Please see https://github.com/mswjs/msw/pull/255
|
|
471
475
|
function isNodeProcess() {
|
|
476
|
+
// Check browser environment.
|
|
472
477
|
if (typeof global !== 'object') {
|
|
473
|
-
// check browser environment
|
|
474
478
|
return false;
|
|
475
479
|
}
|
|
480
|
+
// Check nodejs or React Native environment.
|
|
476
481
|
if (Object.prototype.toString.call(global.process) === '[object process]' ||
|
|
477
482
|
navigator.product === 'ReactNative') {
|
|
478
|
-
// check nodejs or react native environment
|
|
479
483
|
return true;
|
|
480
484
|
}
|
|
481
485
|
}
|
|
@@ -504,20 +508,21 @@ const delay = (durationMs) => {
|
|
|
504
508
|
};
|
|
505
509
|
|
|
506
510
|
const useFetch = isNodeProcess() ? require('node-fetch') : window.fetch;
|
|
507
|
-
const gracefully = (promise) => {
|
|
508
|
-
return promise.then((res) => {
|
|
509
|
-
var _a;
|
|
510
|
-
if ((_a = res.headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.includes('json')) {
|
|
511
|
-
return res.json();
|
|
512
|
-
}
|
|
513
|
-
return res.text();
|
|
514
|
-
});
|
|
515
|
-
};
|
|
516
511
|
const augmentRequestInit = (requestInit) => {
|
|
517
512
|
const headers = new lib.Headers(requestInit.headers);
|
|
518
513
|
headers.set('x-msw-bypass', 'true');
|
|
519
514
|
return Object.assign(Object.assign({}, requestInit), { headers: headers.getAllHeaders() });
|
|
520
515
|
};
|
|
516
|
+
const createFetchRequestParameters = (input) => {
|
|
517
|
+
const { body, method } = input;
|
|
518
|
+
const requestParameters = Object.assign(Object.assign({}, input), { body: undefined });
|
|
519
|
+
if (['GET', 'HEAD'].includes(method)) {
|
|
520
|
+
return requestParameters;
|
|
521
|
+
}
|
|
522
|
+
requestParameters.body =
|
|
523
|
+
typeof body === 'object' ? JSON.stringify(body) : body;
|
|
524
|
+
return requestParameters;
|
|
525
|
+
};
|
|
521
526
|
/**
|
|
522
527
|
* Wrapper around the native `window.fetch()` function that performs
|
|
523
528
|
* a request bypassing MSW. Requests performed using
|
|
@@ -526,11 +531,11 @@ const augmentRequestInit = (requestInit) => {
|
|
|
526
531
|
const fetch = (input, requestInit = {}) => {
|
|
527
532
|
// Keep the default `window.fetch()` call signature
|
|
528
533
|
if (typeof input === 'string') {
|
|
529
|
-
return
|
|
534
|
+
return useFetch(input, augmentRequestInit(requestInit));
|
|
530
535
|
}
|
|
531
|
-
const
|
|
532
|
-
const
|
|
533
|
-
return
|
|
536
|
+
const requestParameters = createFetchRequestParameters(input);
|
|
537
|
+
const compliantRequest = augmentRequestInit(requestParameters);
|
|
538
|
+
return useFetch(input.url.href, compliantRequest);
|
|
534
539
|
};
|
|
535
540
|
|
|
536
541
|
const defaultContext = {
|
|
@@ -672,21 +677,16 @@ function jsonParse(str) {
|
|
|
672
677
|
}
|
|
673
678
|
|
|
674
679
|
/**
|
|
675
|
-
*
|
|
676
|
-
* otherwise returns a given body string as-is.
|
|
680
|
+
* Parses a given request/response body based on the `Content-Type` header.
|
|
677
681
|
*/
|
|
678
|
-
function
|
|
679
|
-
return jsonParse(body) || body;
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
function parseRequestBody(body, headers) {
|
|
682
|
+
function parseBody(body, headers) {
|
|
683
683
|
var _a;
|
|
684
684
|
if (body) {
|
|
685
685
|
// If the intercepted request's body has a JSON Content-Type
|
|
686
686
|
// parse it into an object, otherwise leave as-is.
|
|
687
687
|
const hasJsonContent = (_a = headers === null || headers === void 0 ? void 0 : headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.includes('json');
|
|
688
688
|
if (hasJsonContent && typeof body !== 'object') {
|
|
689
|
-
return
|
|
689
|
+
return jsonParse(body) || body;
|
|
690
690
|
}
|
|
691
691
|
return body;
|
|
692
692
|
}
|
|
@@ -708,12 +708,1374 @@ function resetHandlers(initialHandlers, ...nextHandlers) {
|
|
|
708
708
|
return nextHandlers.length > 0 ? [...nextHandlers] : [...initialHandlers];
|
|
709
709
|
}
|
|
710
710
|
|
|
711
|
+
var punycode = createCommonjsModule(function (module, exports) {
|
|
712
|
+
(function(root) {
|
|
713
|
+
|
|
714
|
+
/** Detect free variables */
|
|
715
|
+
var freeExports = exports &&
|
|
716
|
+
!exports.nodeType && exports;
|
|
717
|
+
var freeModule = module &&
|
|
718
|
+
!module.nodeType && module;
|
|
719
|
+
var freeGlobal = typeof commonjsGlobal == 'object' && commonjsGlobal;
|
|
720
|
+
if (
|
|
721
|
+
freeGlobal.global === freeGlobal ||
|
|
722
|
+
freeGlobal.window === freeGlobal ||
|
|
723
|
+
freeGlobal.self === freeGlobal
|
|
724
|
+
) {
|
|
725
|
+
root = freeGlobal;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* The `punycode` object.
|
|
730
|
+
* @name punycode
|
|
731
|
+
* @type Object
|
|
732
|
+
*/
|
|
733
|
+
var punycode,
|
|
734
|
+
|
|
735
|
+
/** Highest positive signed 32-bit float value */
|
|
736
|
+
maxInt = 2147483647, // aka. 0x7FFFFFFF or 2^31-1
|
|
737
|
+
|
|
738
|
+
/** Bootstring parameters */
|
|
739
|
+
base = 36,
|
|
740
|
+
tMin = 1,
|
|
741
|
+
tMax = 26,
|
|
742
|
+
skew = 38,
|
|
743
|
+
damp = 700,
|
|
744
|
+
initialBias = 72,
|
|
745
|
+
initialN = 128, // 0x80
|
|
746
|
+
delimiter = '-', // '\x2D'
|
|
747
|
+
|
|
748
|
+
/** Regular expressions */
|
|
749
|
+
regexPunycode = /^xn--/,
|
|
750
|
+
regexNonASCII = /[^\x20-\x7E]/, // unprintable ASCII chars + non-ASCII chars
|
|
751
|
+
regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g, // RFC 3490 separators
|
|
752
|
+
|
|
753
|
+
/** Error messages */
|
|
754
|
+
errors = {
|
|
755
|
+
'overflow': 'Overflow: input needs wider integers to process',
|
|
756
|
+
'not-basic': 'Illegal input >= 0x80 (not a basic code point)',
|
|
757
|
+
'invalid-input': 'Invalid input'
|
|
758
|
+
},
|
|
759
|
+
|
|
760
|
+
/** Convenience shortcuts */
|
|
761
|
+
baseMinusTMin = base - tMin,
|
|
762
|
+
floor = Math.floor,
|
|
763
|
+
stringFromCharCode = String.fromCharCode,
|
|
764
|
+
|
|
765
|
+
/** Temporary variable */
|
|
766
|
+
key;
|
|
767
|
+
|
|
768
|
+
/*--------------------------------------------------------------------------*/
|
|
769
|
+
|
|
770
|
+
/**
|
|
771
|
+
* A generic error utility function.
|
|
772
|
+
* @private
|
|
773
|
+
* @param {String} type The error type.
|
|
774
|
+
* @returns {Error} Throws a `RangeError` with the applicable error message.
|
|
775
|
+
*/
|
|
776
|
+
function error(type) {
|
|
777
|
+
throw RangeError(errors[type]);
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* A generic `Array#map` utility function.
|
|
782
|
+
* @private
|
|
783
|
+
* @param {Array} array The array to iterate over.
|
|
784
|
+
* @param {Function} callback The function that gets called for every array
|
|
785
|
+
* item.
|
|
786
|
+
* @returns {Array} A new array of values returned by the callback function.
|
|
787
|
+
*/
|
|
788
|
+
function map(array, fn) {
|
|
789
|
+
var length = array.length;
|
|
790
|
+
var result = [];
|
|
791
|
+
while (length--) {
|
|
792
|
+
result[length] = fn(array[length]);
|
|
793
|
+
}
|
|
794
|
+
return result;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* A simple `Array#map`-like wrapper to work with domain name strings or email
|
|
799
|
+
* addresses.
|
|
800
|
+
* @private
|
|
801
|
+
* @param {String} domain The domain name or email address.
|
|
802
|
+
* @param {Function} callback The function that gets called for every
|
|
803
|
+
* character.
|
|
804
|
+
* @returns {Array} A new string of characters returned by the callback
|
|
805
|
+
* function.
|
|
806
|
+
*/
|
|
807
|
+
function mapDomain(string, fn) {
|
|
808
|
+
var parts = string.split('@');
|
|
809
|
+
var result = '';
|
|
810
|
+
if (parts.length > 1) {
|
|
811
|
+
// In email addresses, only the domain name should be punycoded. Leave
|
|
812
|
+
// the local part (i.e. everything up to `@`) intact.
|
|
813
|
+
result = parts[0] + '@';
|
|
814
|
+
string = parts[1];
|
|
815
|
+
}
|
|
816
|
+
// Avoid `split(regex)` for IE8 compatibility. See #17.
|
|
817
|
+
string = string.replace(regexSeparators, '\x2E');
|
|
818
|
+
var labels = string.split('.');
|
|
819
|
+
var encoded = map(labels, fn).join('.');
|
|
820
|
+
return result + encoded;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
/**
|
|
824
|
+
* Creates an array containing the numeric code points of each Unicode
|
|
825
|
+
* character in the string. While JavaScript uses UCS-2 internally,
|
|
826
|
+
* this function will convert a pair of surrogate halves (each of which
|
|
827
|
+
* UCS-2 exposes as separate characters) into a single code point,
|
|
828
|
+
* matching UTF-16.
|
|
829
|
+
* @see `punycode.ucs2.encode`
|
|
830
|
+
* @see <https://mathiasbynens.be/notes/javascript-encoding>
|
|
831
|
+
* @memberOf punycode.ucs2
|
|
832
|
+
* @name decode
|
|
833
|
+
* @param {String} string The Unicode input string (UCS-2).
|
|
834
|
+
* @returns {Array} The new array of code points.
|
|
835
|
+
*/
|
|
836
|
+
function ucs2decode(string) {
|
|
837
|
+
var output = [],
|
|
838
|
+
counter = 0,
|
|
839
|
+
length = string.length,
|
|
840
|
+
value,
|
|
841
|
+
extra;
|
|
842
|
+
while (counter < length) {
|
|
843
|
+
value = string.charCodeAt(counter++);
|
|
844
|
+
if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
|
|
845
|
+
// high surrogate, and there is a next character
|
|
846
|
+
extra = string.charCodeAt(counter++);
|
|
847
|
+
if ((extra & 0xFC00) == 0xDC00) { // low surrogate
|
|
848
|
+
output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
|
|
849
|
+
} else {
|
|
850
|
+
// unmatched surrogate; only append this code unit, in case the next
|
|
851
|
+
// code unit is the high surrogate of a surrogate pair
|
|
852
|
+
output.push(value);
|
|
853
|
+
counter--;
|
|
854
|
+
}
|
|
855
|
+
} else {
|
|
856
|
+
output.push(value);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
return output;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
/**
|
|
863
|
+
* Creates a string based on an array of numeric code points.
|
|
864
|
+
* @see `punycode.ucs2.decode`
|
|
865
|
+
* @memberOf punycode.ucs2
|
|
866
|
+
* @name encode
|
|
867
|
+
* @param {Array} codePoints The array of numeric code points.
|
|
868
|
+
* @returns {String} The new Unicode string (UCS-2).
|
|
869
|
+
*/
|
|
870
|
+
function ucs2encode(array) {
|
|
871
|
+
return map(array, function(value) {
|
|
872
|
+
var output = '';
|
|
873
|
+
if (value > 0xFFFF) {
|
|
874
|
+
value -= 0x10000;
|
|
875
|
+
output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);
|
|
876
|
+
value = 0xDC00 | value & 0x3FF;
|
|
877
|
+
}
|
|
878
|
+
output += stringFromCharCode(value);
|
|
879
|
+
return output;
|
|
880
|
+
}).join('');
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
/**
|
|
884
|
+
* Converts a basic code point into a digit/integer.
|
|
885
|
+
* @see `digitToBasic()`
|
|
886
|
+
* @private
|
|
887
|
+
* @param {Number} codePoint The basic numeric code point value.
|
|
888
|
+
* @returns {Number} The numeric value of a basic code point (for use in
|
|
889
|
+
* representing integers) in the range `0` to `base - 1`, or `base` if
|
|
890
|
+
* the code point does not represent a value.
|
|
891
|
+
*/
|
|
892
|
+
function basicToDigit(codePoint) {
|
|
893
|
+
if (codePoint - 48 < 10) {
|
|
894
|
+
return codePoint - 22;
|
|
895
|
+
}
|
|
896
|
+
if (codePoint - 65 < 26) {
|
|
897
|
+
return codePoint - 65;
|
|
898
|
+
}
|
|
899
|
+
if (codePoint - 97 < 26) {
|
|
900
|
+
return codePoint - 97;
|
|
901
|
+
}
|
|
902
|
+
return base;
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/**
|
|
906
|
+
* Converts a digit/integer into a basic code point.
|
|
907
|
+
* @see `basicToDigit()`
|
|
908
|
+
* @private
|
|
909
|
+
* @param {Number} digit The numeric value of a basic code point.
|
|
910
|
+
* @returns {Number} The basic code point whose value (when used for
|
|
911
|
+
* representing integers) is `digit`, which needs to be in the range
|
|
912
|
+
* `0` to `base - 1`. If `flag` is non-zero, the uppercase form is
|
|
913
|
+
* used; else, the lowercase form is used. The behavior is undefined
|
|
914
|
+
* if `flag` is non-zero and `digit` has no uppercase form.
|
|
915
|
+
*/
|
|
916
|
+
function digitToBasic(digit, flag) {
|
|
917
|
+
// 0..25 map to ASCII a..z or A..Z
|
|
918
|
+
// 26..35 map to ASCII 0..9
|
|
919
|
+
return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5);
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
/**
|
|
923
|
+
* Bias adaptation function as per section 3.4 of RFC 3492.
|
|
924
|
+
* http://tools.ietf.org/html/rfc3492#section-3.4
|
|
925
|
+
* @private
|
|
926
|
+
*/
|
|
927
|
+
function adapt(delta, numPoints, firstTime) {
|
|
928
|
+
var k = 0;
|
|
929
|
+
delta = firstTime ? floor(delta / damp) : delta >> 1;
|
|
930
|
+
delta += floor(delta / numPoints);
|
|
931
|
+
for (/* no initialization */; delta > baseMinusTMin * tMax >> 1; k += base) {
|
|
932
|
+
delta = floor(delta / baseMinusTMin);
|
|
933
|
+
}
|
|
934
|
+
return floor(k + (baseMinusTMin + 1) * delta / (delta + skew));
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
/**
|
|
938
|
+
* Converts a Punycode string of ASCII-only symbols to a string of Unicode
|
|
939
|
+
* symbols.
|
|
940
|
+
* @memberOf punycode
|
|
941
|
+
* @param {String} input The Punycode string of ASCII-only symbols.
|
|
942
|
+
* @returns {String} The resulting string of Unicode symbols.
|
|
943
|
+
*/
|
|
944
|
+
function decode(input) {
|
|
945
|
+
// Don't use UCS-2
|
|
946
|
+
var output = [],
|
|
947
|
+
inputLength = input.length,
|
|
948
|
+
out,
|
|
949
|
+
i = 0,
|
|
950
|
+
n = initialN,
|
|
951
|
+
bias = initialBias,
|
|
952
|
+
basic,
|
|
953
|
+
j,
|
|
954
|
+
index,
|
|
955
|
+
oldi,
|
|
956
|
+
w,
|
|
957
|
+
k,
|
|
958
|
+
digit,
|
|
959
|
+
t,
|
|
960
|
+
/** Cached calculation results */
|
|
961
|
+
baseMinusT;
|
|
962
|
+
|
|
963
|
+
// Handle the basic code points: let `basic` be the number of input code
|
|
964
|
+
// points before the last delimiter, or `0` if there is none, then copy
|
|
965
|
+
// the first basic code points to the output.
|
|
966
|
+
|
|
967
|
+
basic = input.lastIndexOf(delimiter);
|
|
968
|
+
if (basic < 0) {
|
|
969
|
+
basic = 0;
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
for (j = 0; j < basic; ++j) {
|
|
973
|
+
// if it's not a basic code point
|
|
974
|
+
if (input.charCodeAt(j) >= 0x80) {
|
|
975
|
+
error('not-basic');
|
|
976
|
+
}
|
|
977
|
+
output.push(input.charCodeAt(j));
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
// Main decoding loop: start just after the last delimiter if any basic code
|
|
981
|
+
// points were copied; start at the beginning otherwise.
|
|
982
|
+
|
|
983
|
+
for (index = basic > 0 ? basic + 1 : 0; index < inputLength; /* no final expression */) {
|
|
984
|
+
|
|
985
|
+
// `index` is the index of the next character to be consumed.
|
|
986
|
+
// Decode a generalized variable-length integer into `delta`,
|
|
987
|
+
// which gets added to `i`. The overflow checking is easier
|
|
988
|
+
// if we increase `i` as we go, then subtract off its starting
|
|
989
|
+
// value at the end to obtain `delta`.
|
|
990
|
+
for (oldi = i, w = 1, k = base; /* no condition */; k += base) {
|
|
991
|
+
|
|
992
|
+
if (index >= inputLength) {
|
|
993
|
+
error('invalid-input');
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
digit = basicToDigit(input.charCodeAt(index++));
|
|
997
|
+
|
|
998
|
+
if (digit >= base || digit > floor((maxInt - i) / w)) {
|
|
999
|
+
error('overflow');
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
i += digit * w;
|
|
1003
|
+
t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
|
|
1004
|
+
|
|
1005
|
+
if (digit < t) {
|
|
1006
|
+
break;
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
baseMinusT = base - t;
|
|
1010
|
+
if (w > floor(maxInt / baseMinusT)) {
|
|
1011
|
+
error('overflow');
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
w *= baseMinusT;
|
|
1015
|
+
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
out = output.length + 1;
|
|
1019
|
+
bias = adapt(i - oldi, out, oldi == 0);
|
|
1020
|
+
|
|
1021
|
+
// `i` was supposed to wrap around from `out` to `0`,
|
|
1022
|
+
// incrementing `n` each time, so we'll fix that now:
|
|
1023
|
+
if (floor(i / out) > maxInt - n) {
|
|
1024
|
+
error('overflow');
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
n += floor(i / out);
|
|
1028
|
+
i %= out;
|
|
1029
|
+
|
|
1030
|
+
// Insert `n` at position `i` of the output
|
|
1031
|
+
output.splice(i++, 0, n);
|
|
1032
|
+
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
return ucs2encode(output);
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
/**
|
|
1039
|
+
* Converts a string of Unicode symbols (e.g. a domain name label) to a
|
|
1040
|
+
* Punycode string of ASCII-only symbols.
|
|
1041
|
+
* @memberOf punycode
|
|
1042
|
+
* @param {String} input The string of Unicode symbols.
|
|
1043
|
+
* @returns {String} The resulting Punycode string of ASCII-only symbols.
|
|
1044
|
+
*/
|
|
1045
|
+
function encode(input) {
|
|
1046
|
+
var n,
|
|
1047
|
+
delta,
|
|
1048
|
+
handledCPCount,
|
|
1049
|
+
basicLength,
|
|
1050
|
+
bias,
|
|
1051
|
+
j,
|
|
1052
|
+
m,
|
|
1053
|
+
q,
|
|
1054
|
+
k,
|
|
1055
|
+
t,
|
|
1056
|
+
currentValue,
|
|
1057
|
+
output = [],
|
|
1058
|
+
/** `inputLength` will hold the number of code points in `input`. */
|
|
1059
|
+
inputLength,
|
|
1060
|
+
/** Cached calculation results */
|
|
1061
|
+
handledCPCountPlusOne,
|
|
1062
|
+
baseMinusT,
|
|
1063
|
+
qMinusT;
|
|
1064
|
+
|
|
1065
|
+
// Convert the input in UCS-2 to Unicode
|
|
1066
|
+
input = ucs2decode(input);
|
|
1067
|
+
|
|
1068
|
+
// Cache the length
|
|
1069
|
+
inputLength = input.length;
|
|
1070
|
+
|
|
1071
|
+
// Initialize the state
|
|
1072
|
+
n = initialN;
|
|
1073
|
+
delta = 0;
|
|
1074
|
+
bias = initialBias;
|
|
1075
|
+
|
|
1076
|
+
// Handle the basic code points
|
|
1077
|
+
for (j = 0; j < inputLength; ++j) {
|
|
1078
|
+
currentValue = input[j];
|
|
1079
|
+
if (currentValue < 0x80) {
|
|
1080
|
+
output.push(stringFromCharCode(currentValue));
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
handledCPCount = basicLength = output.length;
|
|
1085
|
+
|
|
1086
|
+
// `handledCPCount` is the number of code points that have been handled;
|
|
1087
|
+
// `basicLength` is the number of basic code points.
|
|
1088
|
+
|
|
1089
|
+
// Finish the basic string - if it is not empty - with a delimiter
|
|
1090
|
+
if (basicLength) {
|
|
1091
|
+
output.push(delimiter);
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
// Main encoding loop:
|
|
1095
|
+
while (handledCPCount < inputLength) {
|
|
1096
|
+
|
|
1097
|
+
// All non-basic code points < n have been handled already. Find the next
|
|
1098
|
+
// larger one:
|
|
1099
|
+
for (m = maxInt, j = 0; j < inputLength; ++j) {
|
|
1100
|
+
currentValue = input[j];
|
|
1101
|
+
if (currentValue >= n && currentValue < m) {
|
|
1102
|
+
m = currentValue;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
// Increase `delta` enough to advance the decoder's <n,i> state to <m,0>,
|
|
1107
|
+
// but guard against overflow
|
|
1108
|
+
handledCPCountPlusOne = handledCPCount + 1;
|
|
1109
|
+
if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) {
|
|
1110
|
+
error('overflow');
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
delta += (m - n) * handledCPCountPlusOne;
|
|
1114
|
+
n = m;
|
|
1115
|
+
|
|
1116
|
+
for (j = 0; j < inputLength; ++j) {
|
|
1117
|
+
currentValue = input[j];
|
|
1118
|
+
|
|
1119
|
+
if (currentValue < n && ++delta > maxInt) {
|
|
1120
|
+
error('overflow');
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
if (currentValue == n) {
|
|
1124
|
+
// Represent delta as a generalized variable-length integer
|
|
1125
|
+
for (q = delta, k = base; /* no condition */; k += base) {
|
|
1126
|
+
t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
|
|
1127
|
+
if (q < t) {
|
|
1128
|
+
break;
|
|
1129
|
+
}
|
|
1130
|
+
qMinusT = q - t;
|
|
1131
|
+
baseMinusT = base - t;
|
|
1132
|
+
output.push(
|
|
1133
|
+
stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0))
|
|
1134
|
+
);
|
|
1135
|
+
q = floor(qMinusT / baseMinusT);
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
output.push(stringFromCharCode(digitToBasic(q, 0)));
|
|
1139
|
+
bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength);
|
|
1140
|
+
delta = 0;
|
|
1141
|
+
++handledCPCount;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
++delta;
|
|
1146
|
+
++n;
|
|
1147
|
+
|
|
1148
|
+
}
|
|
1149
|
+
return output.join('');
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
/**
|
|
1153
|
+
* Converts a Punycode string representing a domain name or an email address
|
|
1154
|
+
* to Unicode. Only the Punycoded parts of the input will be converted, i.e.
|
|
1155
|
+
* it doesn't matter if you call it on a string that has already been
|
|
1156
|
+
* converted to Unicode.
|
|
1157
|
+
* @memberOf punycode
|
|
1158
|
+
* @param {String} input The Punycoded domain name or email address to
|
|
1159
|
+
* convert to Unicode.
|
|
1160
|
+
* @returns {String} The Unicode representation of the given Punycode
|
|
1161
|
+
* string.
|
|
1162
|
+
*/
|
|
1163
|
+
function toUnicode(input) {
|
|
1164
|
+
return mapDomain(input, function(string) {
|
|
1165
|
+
return regexPunycode.test(string)
|
|
1166
|
+
? decode(string.slice(4).toLowerCase())
|
|
1167
|
+
: string;
|
|
1168
|
+
});
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
/**
|
|
1172
|
+
* Converts a Unicode string representing a domain name or an email address to
|
|
1173
|
+
* Punycode. Only the non-ASCII parts of the domain name will be converted,
|
|
1174
|
+
* i.e. it doesn't matter if you call it with a domain that's already in
|
|
1175
|
+
* ASCII.
|
|
1176
|
+
* @memberOf punycode
|
|
1177
|
+
* @param {String} input The domain name or email address to convert, as a
|
|
1178
|
+
* Unicode string.
|
|
1179
|
+
* @returns {String} The Punycode representation of the given domain name or
|
|
1180
|
+
* email address.
|
|
1181
|
+
*/
|
|
1182
|
+
function toASCII(input) {
|
|
1183
|
+
return mapDomain(input, function(string) {
|
|
1184
|
+
return regexNonASCII.test(string)
|
|
1185
|
+
? 'xn--' + encode(string)
|
|
1186
|
+
: string;
|
|
1187
|
+
});
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
/*--------------------------------------------------------------------------*/
|
|
1191
|
+
|
|
1192
|
+
/** Define the public API */
|
|
1193
|
+
punycode = {
|
|
1194
|
+
/**
|
|
1195
|
+
* A string representing the current Punycode.js version number.
|
|
1196
|
+
* @memberOf punycode
|
|
1197
|
+
* @type String
|
|
1198
|
+
*/
|
|
1199
|
+
'version': '1.3.2',
|
|
1200
|
+
/**
|
|
1201
|
+
* An object of methods to convert from JavaScript's internal character
|
|
1202
|
+
* representation (UCS-2) to Unicode code points, and back.
|
|
1203
|
+
* @see <https://mathiasbynens.be/notes/javascript-encoding>
|
|
1204
|
+
* @memberOf punycode
|
|
1205
|
+
* @type Object
|
|
1206
|
+
*/
|
|
1207
|
+
'ucs2': {
|
|
1208
|
+
'decode': ucs2decode,
|
|
1209
|
+
'encode': ucs2encode
|
|
1210
|
+
},
|
|
1211
|
+
'decode': decode,
|
|
1212
|
+
'encode': encode,
|
|
1213
|
+
'toASCII': toASCII,
|
|
1214
|
+
'toUnicode': toUnicode
|
|
1215
|
+
};
|
|
1216
|
+
|
|
1217
|
+
/** Expose `punycode` */
|
|
1218
|
+
// Some AMD build optimizers, like r.js, check for specific condition patterns
|
|
1219
|
+
// like the following:
|
|
1220
|
+
if (freeExports && freeModule) {
|
|
1221
|
+
if (module.exports == freeExports) { // in Node.js or RingoJS v0.8.0+
|
|
1222
|
+
freeModule.exports = punycode;
|
|
1223
|
+
} else { // in Narwhal or RingoJS v0.7.0-
|
|
1224
|
+
for (key in punycode) {
|
|
1225
|
+
punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]);
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
} else { // in Rhino or a web browser
|
|
1229
|
+
root.punycode = punycode;
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
}(commonjsGlobal));
|
|
1233
|
+
});
|
|
1234
|
+
|
|
1235
|
+
var util = {
|
|
1236
|
+
isString: function(arg) {
|
|
1237
|
+
return typeof(arg) === 'string';
|
|
1238
|
+
},
|
|
1239
|
+
isObject: function(arg) {
|
|
1240
|
+
return typeof(arg) === 'object' && arg !== null;
|
|
1241
|
+
},
|
|
1242
|
+
isNull: function(arg) {
|
|
1243
|
+
return arg === null;
|
|
1244
|
+
},
|
|
1245
|
+
isNullOrUndefined: function(arg) {
|
|
1246
|
+
return arg == null;
|
|
1247
|
+
}
|
|
1248
|
+
};
|
|
1249
|
+
|
|
1250
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
|
1251
|
+
|
|
1252
|
+
// If obj.hasOwnProperty has been overridden, then calling
|
|
1253
|
+
// obj.hasOwnProperty(prop) will break.
|
|
1254
|
+
// See: https://github.com/joyent/node/issues/1707
|
|
1255
|
+
function hasOwnProperty(obj, prop) {
|
|
1256
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
var decode$1 = function(qs, sep, eq, options) {
|
|
1260
|
+
sep = sep || '&';
|
|
1261
|
+
eq = eq || '=';
|
|
1262
|
+
var obj = {};
|
|
1263
|
+
|
|
1264
|
+
if (typeof qs !== 'string' || qs.length === 0) {
|
|
1265
|
+
return obj;
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
var regexp = /\+/g;
|
|
1269
|
+
qs = qs.split(sep);
|
|
1270
|
+
|
|
1271
|
+
var maxKeys = 1000;
|
|
1272
|
+
if (options && typeof options.maxKeys === 'number') {
|
|
1273
|
+
maxKeys = options.maxKeys;
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
var len = qs.length;
|
|
1277
|
+
// maxKeys <= 0 means that we should not limit keys count
|
|
1278
|
+
if (maxKeys > 0 && len > maxKeys) {
|
|
1279
|
+
len = maxKeys;
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
for (var i = 0; i < len; ++i) {
|
|
1283
|
+
var x = qs[i].replace(regexp, '%20'),
|
|
1284
|
+
idx = x.indexOf(eq),
|
|
1285
|
+
kstr, vstr, k, v;
|
|
1286
|
+
|
|
1287
|
+
if (idx >= 0) {
|
|
1288
|
+
kstr = x.substr(0, idx);
|
|
1289
|
+
vstr = x.substr(idx + 1);
|
|
1290
|
+
} else {
|
|
1291
|
+
kstr = x;
|
|
1292
|
+
vstr = '';
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
k = decodeURIComponent(kstr);
|
|
1296
|
+
v = decodeURIComponent(vstr);
|
|
1297
|
+
|
|
1298
|
+
if (!hasOwnProperty(obj, k)) {
|
|
1299
|
+
obj[k] = v;
|
|
1300
|
+
} else if (Array.isArray(obj[k])) {
|
|
1301
|
+
obj[k].push(v);
|
|
1302
|
+
} else {
|
|
1303
|
+
obj[k] = [obj[k], v];
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
return obj;
|
|
1308
|
+
};
|
|
1309
|
+
|
|
1310
|
+
// Copyright Joyent, Inc. and other Node contributors.
|
|
1311
|
+
|
|
1312
|
+
var stringifyPrimitive = function(v) {
|
|
1313
|
+
switch (typeof v) {
|
|
1314
|
+
case 'string':
|
|
1315
|
+
return v;
|
|
1316
|
+
|
|
1317
|
+
case 'boolean':
|
|
1318
|
+
return v ? 'true' : 'false';
|
|
1319
|
+
|
|
1320
|
+
case 'number':
|
|
1321
|
+
return isFinite(v) ? v : '';
|
|
1322
|
+
|
|
1323
|
+
default:
|
|
1324
|
+
return '';
|
|
1325
|
+
}
|
|
1326
|
+
};
|
|
1327
|
+
|
|
1328
|
+
var encode = function(obj, sep, eq, name) {
|
|
1329
|
+
sep = sep || '&';
|
|
1330
|
+
eq = eq || '=';
|
|
1331
|
+
if (obj === null) {
|
|
1332
|
+
obj = undefined;
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
if (typeof obj === 'object') {
|
|
1336
|
+
return Object.keys(obj).map(function(k) {
|
|
1337
|
+
var ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
|
|
1338
|
+
if (Array.isArray(obj[k])) {
|
|
1339
|
+
return obj[k].map(function(v) {
|
|
1340
|
+
return ks + encodeURIComponent(stringifyPrimitive(v));
|
|
1341
|
+
}).join(sep);
|
|
1342
|
+
} else {
|
|
1343
|
+
return ks + encodeURIComponent(stringifyPrimitive(obj[k]));
|
|
1344
|
+
}
|
|
1345
|
+
}).join(sep);
|
|
1346
|
+
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
if (!name) return '';
|
|
1350
|
+
return encodeURIComponent(stringifyPrimitive(name)) + eq +
|
|
1351
|
+
encodeURIComponent(stringifyPrimitive(obj));
|
|
1352
|
+
};
|
|
1353
|
+
|
|
1354
|
+
var querystring = createCommonjsModule(function (module, exports) {
|
|
1355
|
+
|
|
1356
|
+
exports.decode = exports.parse = decode$1;
|
|
1357
|
+
exports.encode = exports.stringify = encode;
|
|
1358
|
+
});
|
|
1359
|
+
|
|
1360
|
+
var format = urlFormat;
|
|
1361
|
+
|
|
1362
|
+
function Url() {
|
|
1363
|
+
this.protocol = null;
|
|
1364
|
+
this.slashes = null;
|
|
1365
|
+
this.auth = null;
|
|
1366
|
+
this.host = null;
|
|
1367
|
+
this.port = null;
|
|
1368
|
+
this.hostname = null;
|
|
1369
|
+
this.hash = null;
|
|
1370
|
+
this.search = null;
|
|
1371
|
+
this.query = null;
|
|
1372
|
+
this.pathname = null;
|
|
1373
|
+
this.path = null;
|
|
1374
|
+
this.href = null;
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
// Reference: RFC 3986, RFC 1808, RFC 2396
|
|
1378
|
+
|
|
1379
|
+
// define these here so at least they only have to be
|
|
1380
|
+
// compiled once on the first module load.
|
|
1381
|
+
var protocolPattern = /^([a-z0-9.+-]+:)/i,
|
|
1382
|
+
portPattern = /:[0-9]*$/,
|
|
1383
|
+
|
|
1384
|
+
// Special case for a simple path URL
|
|
1385
|
+
simplePathPattern = /^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/,
|
|
1386
|
+
|
|
1387
|
+
// RFC 2396: characters reserved for delimiting URLs.
|
|
1388
|
+
// We actually just auto-escape these.
|
|
1389
|
+
delims = ['<', '>', '"', '`', ' ', '\r', '\n', '\t'],
|
|
1390
|
+
|
|
1391
|
+
// RFC 2396: characters not allowed for various reasons.
|
|
1392
|
+
unwise = ['{', '}', '|', '\\', '^', '`'].concat(delims),
|
|
1393
|
+
|
|
1394
|
+
// Allowed by RFCs, but cause of XSS attacks. Always escape these.
|
|
1395
|
+
autoEscape = ['\''].concat(unwise),
|
|
1396
|
+
// Characters that are never ever allowed in a hostname.
|
|
1397
|
+
// Note that any invalid chars are also handled, but these
|
|
1398
|
+
// are the ones that are *expected* to be seen, so we fast-path
|
|
1399
|
+
// them.
|
|
1400
|
+
nonHostChars = ['%', '/', '?', ';', '#'].concat(autoEscape),
|
|
1401
|
+
hostEndingChars = ['/', '?', '#'],
|
|
1402
|
+
hostnameMaxLen = 255,
|
|
1403
|
+
hostnamePartPattern = /^[+a-z0-9A-Z_-]{0,63}$/,
|
|
1404
|
+
hostnamePartStart = /^([+a-z0-9A-Z_-]{0,63})(.*)$/,
|
|
1405
|
+
// protocols that can allow "unsafe" and "unwise" chars.
|
|
1406
|
+
unsafeProtocol = {
|
|
1407
|
+
'javascript': true,
|
|
1408
|
+
'javascript:': true
|
|
1409
|
+
},
|
|
1410
|
+
// protocols that never have a hostname.
|
|
1411
|
+
hostlessProtocol = {
|
|
1412
|
+
'javascript': true,
|
|
1413
|
+
'javascript:': true
|
|
1414
|
+
},
|
|
1415
|
+
// protocols that always contain a // bit.
|
|
1416
|
+
slashedProtocol = {
|
|
1417
|
+
'http': true,
|
|
1418
|
+
'https': true,
|
|
1419
|
+
'ftp': true,
|
|
1420
|
+
'gopher': true,
|
|
1421
|
+
'file': true,
|
|
1422
|
+
'http:': true,
|
|
1423
|
+
'https:': true,
|
|
1424
|
+
'ftp:': true,
|
|
1425
|
+
'gopher:': true,
|
|
1426
|
+
'file:': true
|
|
1427
|
+
};
|
|
1428
|
+
|
|
1429
|
+
function urlParse(url, parseQueryString, slashesDenoteHost) {
|
|
1430
|
+
if (url && util.isObject(url) && url instanceof Url) return url;
|
|
1431
|
+
|
|
1432
|
+
var u = new Url;
|
|
1433
|
+
u.parse(url, parseQueryString, slashesDenoteHost);
|
|
1434
|
+
return u;
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) {
|
|
1438
|
+
if (!util.isString(url)) {
|
|
1439
|
+
throw new TypeError("Parameter 'url' must be a string, not " + typeof url);
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
// Copy chrome, IE, opera backslash-handling behavior.
|
|
1443
|
+
// Back slashes before the query string get converted to forward slashes
|
|
1444
|
+
// See: https://code.google.com/p/chromium/issues/detail?id=25916
|
|
1445
|
+
var queryIndex = url.indexOf('?'),
|
|
1446
|
+
splitter =
|
|
1447
|
+
(queryIndex !== -1 && queryIndex < url.indexOf('#')) ? '?' : '#',
|
|
1448
|
+
uSplit = url.split(splitter),
|
|
1449
|
+
slashRegex = /\\/g;
|
|
1450
|
+
uSplit[0] = uSplit[0].replace(slashRegex, '/');
|
|
1451
|
+
url = uSplit.join(splitter);
|
|
1452
|
+
|
|
1453
|
+
var rest = url;
|
|
1454
|
+
|
|
1455
|
+
// trim before proceeding.
|
|
1456
|
+
// This is to support parse stuff like " http://foo.com \n"
|
|
1457
|
+
rest = rest.trim();
|
|
1458
|
+
|
|
1459
|
+
if (!slashesDenoteHost && url.split('#').length === 1) {
|
|
1460
|
+
// Try fast path regexp
|
|
1461
|
+
var simplePath = simplePathPattern.exec(rest);
|
|
1462
|
+
if (simplePath) {
|
|
1463
|
+
this.path = rest;
|
|
1464
|
+
this.href = rest;
|
|
1465
|
+
this.pathname = simplePath[1];
|
|
1466
|
+
if (simplePath[2]) {
|
|
1467
|
+
this.search = simplePath[2];
|
|
1468
|
+
if (parseQueryString) {
|
|
1469
|
+
this.query = querystring.parse(this.search.substr(1));
|
|
1470
|
+
} else {
|
|
1471
|
+
this.query = this.search.substr(1);
|
|
1472
|
+
}
|
|
1473
|
+
} else if (parseQueryString) {
|
|
1474
|
+
this.search = '';
|
|
1475
|
+
this.query = {};
|
|
1476
|
+
}
|
|
1477
|
+
return this;
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
var proto = protocolPattern.exec(rest);
|
|
1482
|
+
if (proto) {
|
|
1483
|
+
proto = proto[0];
|
|
1484
|
+
var lowerProto = proto.toLowerCase();
|
|
1485
|
+
this.protocol = lowerProto;
|
|
1486
|
+
rest = rest.substr(proto.length);
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
// figure out if it's got a host
|
|
1490
|
+
// user@server is *always* interpreted as a hostname, and url
|
|
1491
|
+
// resolution will treat //foo/bar as host=foo,path=bar because that's
|
|
1492
|
+
// how the browser resolves relative URLs.
|
|
1493
|
+
if (slashesDenoteHost || proto || rest.match(/^\/\/[^@\/]+@[^@\/]+/)) {
|
|
1494
|
+
var slashes = rest.substr(0, 2) === '//';
|
|
1495
|
+
if (slashes && !(proto && hostlessProtocol[proto])) {
|
|
1496
|
+
rest = rest.substr(2);
|
|
1497
|
+
this.slashes = true;
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
if (!hostlessProtocol[proto] &&
|
|
1502
|
+
(slashes || (proto && !slashedProtocol[proto]))) {
|
|
1503
|
+
|
|
1504
|
+
// there's a hostname.
|
|
1505
|
+
// the first instance of /, ?, ;, or # ends the host.
|
|
1506
|
+
//
|
|
1507
|
+
// If there is an @ in the hostname, then non-host chars *are* allowed
|
|
1508
|
+
// to the left of the last @ sign, unless some host-ending character
|
|
1509
|
+
// comes *before* the @-sign.
|
|
1510
|
+
// URLs are obnoxious.
|
|
1511
|
+
//
|
|
1512
|
+
// ex:
|
|
1513
|
+
// http://a@b@c/ => user:a@b host:c
|
|
1514
|
+
// http://a@b?@c => user:a host:c path:/?@c
|
|
1515
|
+
|
|
1516
|
+
// v0.12 TODO(isaacs): This is not quite how Chrome does things.
|
|
1517
|
+
// Review our test case against browsers more comprehensively.
|
|
1518
|
+
|
|
1519
|
+
// find the first instance of any hostEndingChars
|
|
1520
|
+
var hostEnd = -1;
|
|
1521
|
+
for (var i = 0; i < hostEndingChars.length; i++) {
|
|
1522
|
+
var hec = rest.indexOf(hostEndingChars[i]);
|
|
1523
|
+
if (hec !== -1 && (hostEnd === -1 || hec < hostEnd))
|
|
1524
|
+
hostEnd = hec;
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
// at this point, either we have an explicit point where the
|
|
1528
|
+
// auth portion cannot go past, or the last @ char is the decider.
|
|
1529
|
+
var auth, atSign;
|
|
1530
|
+
if (hostEnd === -1) {
|
|
1531
|
+
// atSign can be anywhere.
|
|
1532
|
+
atSign = rest.lastIndexOf('@');
|
|
1533
|
+
} else {
|
|
1534
|
+
// atSign must be in auth portion.
|
|
1535
|
+
// http://a@b/c@d => host:b auth:a path:/c@d
|
|
1536
|
+
atSign = rest.lastIndexOf('@', hostEnd);
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
// Now we have a portion which is definitely the auth.
|
|
1540
|
+
// Pull that off.
|
|
1541
|
+
if (atSign !== -1) {
|
|
1542
|
+
auth = rest.slice(0, atSign);
|
|
1543
|
+
rest = rest.slice(atSign + 1);
|
|
1544
|
+
this.auth = decodeURIComponent(auth);
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
// the host is the remaining to the left of the first non-host char
|
|
1548
|
+
hostEnd = -1;
|
|
1549
|
+
for (var i = 0; i < nonHostChars.length; i++) {
|
|
1550
|
+
var hec = rest.indexOf(nonHostChars[i]);
|
|
1551
|
+
if (hec !== -1 && (hostEnd === -1 || hec < hostEnd))
|
|
1552
|
+
hostEnd = hec;
|
|
1553
|
+
}
|
|
1554
|
+
// if we still have not hit it, then the entire thing is a host.
|
|
1555
|
+
if (hostEnd === -1)
|
|
1556
|
+
hostEnd = rest.length;
|
|
1557
|
+
|
|
1558
|
+
this.host = rest.slice(0, hostEnd);
|
|
1559
|
+
rest = rest.slice(hostEnd);
|
|
1560
|
+
|
|
1561
|
+
// pull out port.
|
|
1562
|
+
this.parseHost();
|
|
1563
|
+
|
|
1564
|
+
// we've indicated that there is a hostname,
|
|
1565
|
+
// so even if it's empty, it has to be present.
|
|
1566
|
+
this.hostname = this.hostname || '';
|
|
1567
|
+
|
|
1568
|
+
// if hostname begins with [ and ends with ]
|
|
1569
|
+
// assume that it's an IPv6 address.
|
|
1570
|
+
var ipv6Hostname = this.hostname[0] === '[' &&
|
|
1571
|
+
this.hostname[this.hostname.length - 1] === ']';
|
|
1572
|
+
|
|
1573
|
+
// validate a little.
|
|
1574
|
+
if (!ipv6Hostname) {
|
|
1575
|
+
var hostparts = this.hostname.split(/\./);
|
|
1576
|
+
for (var i = 0, l = hostparts.length; i < l; i++) {
|
|
1577
|
+
var part = hostparts[i];
|
|
1578
|
+
if (!part) continue;
|
|
1579
|
+
if (!part.match(hostnamePartPattern)) {
|
|
1580
|
+
var newpart = '';
|
|
1581
|
+
for (var j = 0, k = part.length; j < k; j++) {
|
|
1582
|
+
if (part.charCodeAt(j) > 127) {
|
|
1583
|
+
// we replace non-ASCII char with a temporary placeholder
|
|
1584
|
+
// we need this to make sure size of hostname is not
|
|
1585
|
+
// broken by replacing non-ASCII by nothing
|
|
1586
|
+
newpart += 'x';
|
|
1587
|
+
} else {
|
|
1588
|
+
newpart += part[j];
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
// we test again with ASCII char only
|
|
1592
|
+
if (!newpart.match(hostnamePartPattern)) {
|
|
1593
|
+
var validParts = hostparts.slice(0, i);
|
|
1594
|
+
var notHost = hostparts.slice(i + 1);
|
|
1595
|
+
var bit = part.match(hostnamePartStart);
|
|
1596
|
+
if (bit) {
|
|
1597
|
+
validParts.push(bit[1]);
|
|
1598
|
+
notHost.unshift(bit[2]);
|
|
1599
|
+
}
|
|
1600
|
+
if (notHost.length) {
|
|
1601
|
+
rest = '/' + notHost.join('.') + rest;
|
|
1602
|
+
}
|
|
1603
|
+
this.hostname = validParts.join('.');
|
|
1604
|
+
break;
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
if (this.hostname.length > hostnameMaxLen) {
|
|
1611
|
+
this.hostname = '';
|
|
1612
|
+
} else {
|
|
1613
|
+
// hostnames are always lower case.
|
|
1614
|
+
this.hostname = this.hostname.toLowerCase();
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
if (!ipv6Hostname) {
|
|
1618
|
+
// IDNA Support: Returns a punycoded representation of "domain".
|
|
1619
|
+
// It only converts parts of the domain name that
|
|
1620
|
+
// have non-ASCII characters, i.e. it doesn't matter if
|
|
1621
|
+
// you call it with a domain that already is ASCII-only.
|
|
1622
|
+
this.hostname = punycode.toASCII(this.hostname);
|
|
1623
|
+
}
|
|
1624
|
+
|
|
1625
|
+
var p = this.port ? ':' + this.port : '';
|
|
1626
|
+
var h = this.hostname || '';
|
|
1627
|
+
this.host = h + p;
|
|
1628
|
+
this.href += this.host;
|
|
1629
|
+
|
|
1630
|
+
// strip [ and ] from the hostname
|
|
1631
|
+
// the host field still retains them, though
|
|
1632
|
+
if (ipv6Hostname) {
|
|
1633
|
+
this.hostname = this.hostname.substr(1, this.hostname.length - 2);
|
|
1634
|
+
if (rest[0] !== '/') {
|
|
1635
|
+
rest = '/' + rest;
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1640
|
+
// now rest is set to the post-host stuff.
|
|
1641
|
+
// chop off any delim chars.
|
|
1642
|
+
if (!unsafeProtocol[lowerProto]) {
|
|
1643
|
+
|
|
1644
|
+
// First, make 100% sure that any "autoEscape" chars get
|
|
1645
|
+
// escaped, even if encodeURIComponent doesn't think they
|
|
1646
|
+
// need to be.
|
|
1647
|
+
for (var i = 0, l = autoEscape.length; i < l; i++) {
|
|
1648
|
+
var ae = autoEscape[i];
|
|
1649
|
+
if (rest.indexOf(ae) === -1)
|
|
1650
|
+
continue;
|
|
1651
|
+
var esc = encodeURIComponent(ae);
|
|
1652
|
+
if (esc === ae) {
|
|
1653
|
+
esc = escape(ae);
|
|
1654
|
+
}
|
|
1655
|
+
rest = rest.split(ae).join(esc);
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
|
|
1660
|
+
// chop off from the tail first.
|
|
1661
|
+
var hash = rest.indexOf('#');
|
|
1662
|
+
if (hash !== -1) {
|
|
1663
|
+
// got a fragment string.
|
|
1664
|
+
this.hash = rest.substr(hash);
|
|
1665
|
+
rest = rest.slice(0, hash);
|
|
1666
|
+
}
|
|
1667
|
+
var qm = rest.indexOf('?');
|
|
1668
|
+
if (qm !== -1) {
|
|
1669
|
+
this.search = rest.substr(qm);
|
|
1670
|
+
this.query = rest.substr(qm + 1);
|
|
1671
|
+
if (parseQueryString) {
|
|
1672
|
+
this.query = querystring.parse(this.query);
|
|
1673
|
+
}
|
|
1674
|
+
rest = rest.slice(0, qm);
|
|
1675
|
+
} else if (parseQueryString) {
|
|
1676
|
+
// no query string, but parseQueryString still requested
|
|
1677
|
+
this.search = '';
|
|
1678
|
+
this.query = {};
|
|
1679
|
+
}
|
|
1680
|
+
if (rest) this.pathname = rest;
|
|
1681
|
+
if (slashedProtocol[lowerProto] &&
|
|
1682
|
+
this.hostname && !this.pathname) {
|
|
1683
|
+
this.pathname = '/';
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
//to support http.request
|
|
1687
|
+
if (this.pathname || this.search) {
|
|
1688
|
+
var p = this.pathname || '';
|
|
1689
|
+
var s = this.search || '';
|
|
1690
|
+
this.path = p + s;
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
// finally, reconstruct the href based on what has been validated.
|
|
1694
|
+
this.href = this.format();
|
|
1695
|
+
return this;
|
|
1696
|
+
};
|
|
1697
|
+
|
|
1698
|
+
// format a parsed object into a url string
|
|
1699
|
+
function urlFormat(obj) {
|
|
1700
|
+
// ensure it's an object, and not a string url.
|
|
1701
|
+
// If it's an obj, this is a no-op.
|
|
1702
|
+
// this way, you can call url_format() on strings
|
|
1703
|
+
// to clean up potentially wonky urls.
|
|
1704
|
+
if (util.isString(obj)) obj = urlParse(obj);
|
|
1705
|
+
if (!(obj instanceof Url)) return Url.prototype.format.call(obj);
|
|
1706
|
+
return obj.format();
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
Url.prototype.format = function() {
|
|
1710
|
+
var auth = this.auth || '';
|
|
1711
|
+
if (auth) {
|
|
1712
|
+
auth = encodeURIComponent(auth);
|
|
1713
|
+
auth = auth.replace(/%3A/i, ':');
|
|
1714
|
+
auth += '@';
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
var protocol = this.protocol || '',
|
|
1718
|
+
pathname = this.pathname || '',
|
|
1719
|
+
hash = this.hash || '',
|
|
1720
|
+
host = false,
|
|
1721
|
+
query = '';
|
|
1722
|
+
|
|
1723
|
+
if (this.host) {
|
|
1724
|
+
host = auth + this.host;
|
|
1725
|
+
} else if (this.hostname) {
|
|
1726
|
+
host = auth + (this.hostname.indexOf(':') === -1 ?
|
|
1727
|
+
this.hostname :
|
|
1728
|
+
'[' + this.hostname + ']');
|
|
1729
|
+
if (this.port) {
|
|
1730
|
+
host += ':' + this.port;
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1734
|
+
if (this.query &&
|
|
1735
|
+
util.isObject(this.query) &&
|
|
1736
|
+
Object.keys(this.query).length) {
|
|
1737
|
+
query = querystring.stringify(this.query);
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
var search = this.search || (query && ('?' + query)) || '';
|
|
1741
|
+
|
|
1742
|
+
if (protocol && protocol.substr(-1) !== ':') protocol += ':';
|
|
1743
|
+
|
|
1744
|
+
// only the slashedProtocols get the //. Not mailto:, xmpp:, etc.
|
|
1745
|
+
// unless they had them to begin with.
|
|
1746
|
+
if (this.slashes ||
|
|
1747
|
+
(!protocol || slashedProtocol[protocol]) && host !== false) {
|
|
1748
|
+
host = '//' + (host || '');
|
|
1749
|
+
if (pathname && pathname.charAt(0) !== '/') pathname = '/' + pathname;
|
|
1750
|
+
} else if (!host) {
|
|
1751
|
+
host = '';
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1754
|
+
if (hash && hash.charAt(0) !== '#') hash = '#' + hash;
|
|
1755
|
+
if (search && search.charAt(0) !== '?') search = '?' + search;
|
|
1756
|
+
|
|
1757
|
+
pathname = pathname.replace(/[?#]/g, function(match) {
|
|
1758
|
+
return encodeURIComponent(match);
|
|
1759
|
+
});
|
|
1760
|
+
search = search.replace('#', '%23');
|
|
1761
|
+
|
|
1762
|
+
return protocol + host + pathname + search + hash;
|
|
1763
|
+
};
|
|
1764
|
+
|
|
1765
|
+
Url.prototype.resolve = function(relative) {
|
|
1766
|
+
return this.resolveObject(urlParse(relative, false, true)).format();
|
|
1767
|
+
};
|
|
1768
|
+
|
|
1769
|
+
Url.prototype.resolveObject = function(relative) {
|
|
1770
|
+
if (util.isString(relative)) {
|
|
1771
|
+
var rel = new Url();
|
|
1772
|
+
rel.parse(relative, false, true);
|
|
1773
|
+
relative = rel;
|
|
1774
|
+
}
|
|
1775
|
+
|
|
1776
|
+
var result = new Url();
|
|
1777
|
+
var tkeys = Object.keys(this);
|
|
1778
|
+
for (var tk = 0; tk < tkeys.length; tk++) {
|
|
1779
|
+
var tkey = tkeys[tk];
|
|
1780
|
+
result[tkey] = this[tkey];
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
// hash is always overridden, no matter what.
|
|
1784
|
+
// even href="" will remove it.
|
|
1785
|
+
result.hash = relative.hash;
|
|
1786
|
+
|
|
1787
|
+
// if the relative url is empty, then there's nothing left to do here.
|
|
1788
|
+
if (relative.href === '') {
|
|
1789
|
+
result.href = result.format();
|
|
1790
|
+
return result;
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
// hrefs like //foo/bar always cut to the protocol.
|
|
1794
|
+
if (relative.slashes && !relative.protocol) {
|
|
1795
|
+
// take everything except the protocol from relative
|
|
1796
|
+
var rkeys = Object.keys(relative);
|
|
1797
|
+
for (var rk = 0; rk < rkeys.length; rk++) {
|
|
1798
|
+
var rkey = rkeys[rk];
|
|
1799
|
+
if (rkey !== 'protocol')
|
|
1800
|
+
result[rkey] = relative[rkey];
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
//urlParse appends trailing / to urls like http://www.example.com
|
|
1804
|
+
if (slashedProtocol[result.protocol] &&
|
|
1805
|
+
result.hostname && !result.pathname) {
|
|
1806
|
+
result.path = result.pathname = '/';
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
result.href = result.format();
|
|
1810
|
+
return result;
|
|
1811
|
+
}
|
|
1812
|
+
|
|
1813
|
+
if (relative.protocol && relative.protocol !== result.protocol) {
|
|
1814
|
+
// if it's a known url protocol, then changing
|
|
1815
|
+
// the protocol does weird things
|
|
1816
|
+
// first, if it's not file:, then we MUST have a host,
|
|
1817
|
+
// and if there was a path
|
|
1818
|
+
// to begin with, then we MUST have a path.
|
|
1819
|
+
// if it is file:, then the host is dropped,
|
|
1820
|
+
// because that's known to be hostless.
|
|
1821
|
+
// anything else is assumed to be absolute.
|
|
1822
|
+
if (!slashedProtocol[relative.protocol]) {
|
|
1823
|
+
var keys = Object.keys(relative);
|
|
1824
|
+
for (var v = 0; v < keys.length; v++) {
|
|
1825
|
+
var k = keys[v];
|
|
1826
|
+
result[k] = relative[k];
|
|
1827
|
+
}
|
|
1828
|
+
result.href = result.format();
|
|
1829
|
+
return result;
|
|
1830
|
+
}
|
|
1831
|
+
|
|
1832
|
+
result.protocol = relative.protocol;
|
|
1833
|
+
if (!relative.host && !hostlessProtocol[relative.protocol]) {
|
|
1834
|
+
var relPath = (relative.pathname || '').split('/');
|
|
1835
|
+
while (relPath.length && !(relative.host = relPath.shift()));
|
|
1836
|
+
if (!relative.host) relative.host = '';
|
|
1837
|
+
if (!relative.hostname) relative.hostname = '';
|
|
1838
|
+
if (relPath[0] !== '') relPath.unshift('');
|
|
1839
|
+
if (relPath.length < 2) relPath.unshift('');
|
|
1840
|
+
result.pathname = relPath.join('/');
|
|
1841
|
+
} else {
|
|
1842
|
+
result.pathname = relative.pathname;
|
|
1843
|
+
}
|
|
1844
|
+
result.search = relative.search;
|
|
1845
|
+
result.query = relative.query;
|
|
1846
|
+
result.host = relative.host || '';
|
|
1847
|
+
result.auth = relative.auth;
|
|
1848
|
+
result.hostname = relative.hostname || relative.host;
|
|
1849
|
+
result.port = relative.port;
|
|
1850
|
+
// to support http.request
|
|
1851
|
+
if (result.pathname || result.search) {
|
|
1852
|
+
var p = result.pathname || '';
|
|
1853
|
+
var s = result.search || '';
|
|
1854
|
+
result.path = p + s;
|
|
1855
|
+
}
|
|
1856
|
+
result.slashes = result.slashes || relative.slashes;
|
|
1857
|
+
result.href = result.format();
|
|
1858
|
+
return result;
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
var isSourceAbs = (result.pathname && result.pathname.charAt(0) === '/'),
|
|
1862
|
+
isRelAbs = (
|
|
1863
|
+
relative.host ||
|
|
1864
|
+
relative.pathname && relative.pathname.charAt(0) === '/'
|
|
1865
|
+
),
|
|
1866
|
+
mustEndAbs = (isRelAbs || isSourceAbs ||
|
|
1867
|
+
(result.host && relative.pathname)),
|
|
1868
|
+
removeAllDots = mustEndAbs,
|
|
1869
|
+
srcPath = result.pathname && result.pathname.split('/') || [],
|
|
1870
|
+
relPath = relative.pathname && relative.pathname.split('/') || [],
|
|
1871
|
+
psychotic = result.protocol && !slashedProtocol[result.protocol];
|
|
1872
|
+
|
|
1873
|
+
// if the url is a non-slashed url, then relative
|
|
1874
|
+
// links like ../.. should be able
|
|
1875
|
+
// to crawl up to the hostname, as well. This is strange.
|
|
1876
|
+
// result.protocol has already been set by now.
|
|
1877
|
+
// Later on, put the first path part into the host field.
|
|
1878
|
+
if (psychotic) {
|
|
1879
|
+
result.hostname = '';
|
|
1880
|
+
result.port = null;
|
|
1881
|
+
if (result.host) {
|
|
1882
|
+
if (srcPath[0] === '') srcPath[0] = result.host;
|
|
1883
|
+
else srcPath.unshift(result.host);
|
|
1884
|
+
}
|
|
1885
|
+
result.host = '';
|
|
1886
|
+
if (relative.protocol) {
|
|
1887
|
+
relative.hostname = null;
|
|
1888
|
+
relative.port = null;
|
|
1889
|
+
if (relative.host) {
|
|
1890
|
+
if (relPath[0] === '') relPath[0] = relative.host;
|
|
1891
|
+
else relPath.unshift(relative.host);
|
|
1892
|
+
}
|
|
1893
|
+
relative.host = null;
|
|
1894
|
+
}
|
|
1895
|
+
mustEndAbs = mustEndAbs && (relPath[0] === '' || srcPath[0] === '');
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
if (isRelAbs) {
|
|
1899
|
+
// it's absolute.
|
|
1900
|
+
result.host = (relative.host || relative.host === '') ?
|
|
1901
|
+
relative.host : result.host;
|
|
1902
|
+
result.hostname = (relative.hostname || relative.hostname === '') ?
|
|
1903
|
+
relative.hostname : result.hostname;
|
|
1904
|
+
result.search = relative.search;
|
|
1905
|
+
result.query = relative.query;
|
|
1906
|
+
srcPath = relPath;
|
|
1907
|
+
// fall through to the dot-handling below.
|
|
1908
|
+
} else if (relPath.length) {
|
|
1909
|
+
// it's relative
|
|
1910
|
+
// throw away the existing file, and take the new path instead.
|
|
1911
|
+
if (!srcPath) srcPath = [];
|
|
1912
|
+
srcPath.pop();
|
|
1913
|
+
srcPath = srcPath.concat(relPath);
|
|
1914
|
+
result.search = relative.search;
|
|
1915
|
+
result.query = relative.query;
|
|
1916
|
+
} else if (!util.isNullOrUndefined(relative.search)) {
|
|
1917
|
+
// just pull out the search.
|
|
1918
|
+
// like href='?foo'.
|
|
1919
|
+
// Put this after the other two cases because it simplifies the booleans
|
|
1920
|
+
if (psychotic) {
|
|
1921
|
+
result.hostname = result.host = srcPath.shift();
|
|
1922
|
+
//occationaly the auth can get stuck only in host
|
|
1923
|
+
//this especially happens in cases like
|
|
1924
|
+
//url.resolveObject('mailto:local1@domain1', 'local2@domain2')
|
|
1925
|
+
var authInHost = result.host && result.host.indexOf('@') > 0 ?
|
|
1926
|
+
result.host.split('@') : false;
|
|
1927
|
+
if (authInHost) {
|
|
1928
|
+
result.auth = authInHost.shift();
|
|
1929
|
+
result.host = result.hostname = authInHost.shift();
|
|
1930
|
+
}
|
|
1931
|
+
}
|
|
1932
|
+
result.search = relative.search;
|
|
1933
|
+
result.query = relative.query;
|
|
1934
|
+
//to support http.request
|
|
1935
|
+
if (!util.isNull(result.pathname) || !util.isNull(result.search)) {
|
|
1936
|
+
result.path = (result.pathname ? result.pathname : '') +
|
|
1937
|
+
(result.search ? result.search : '');
|
|
1938
|
+
}
|
|
1939
|
+
result.href = result.format();
|
|
1940
|
+
return result;
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1943
|
+
if (!srcPath.length) {
|
|
1944
|
+
// no path at all. easy.
|
|
1945
|
+
// we've already handled the other stuff above.
|
|
1946
|
+
result.pathname = null;
|
|
1947
|
+
//to support http.request
|
|
1948
|
+
if (result.search) {
|
|
1949
|
+
result.path = '/' + result.search;
|
|
1950
|
+
} else {
|
|
1951
|
+
result.path = null;
|
|
1952
|
+
}
|
|
1953
|
+
result.href = result.format();
|
|
1954
|
+
return result;
|
|
1955
|
+
}
|
|
1956
|
+
|
|
1957
|
+
// if a url ENDs in . or .., then it must get a trailing slash.
|
|
1958
|
+
// however, if it ends in anything else non-slashy,
|
|
1959
|
+
// then it must NOT get a trailing slash.
|
|
1960
|
+
var last = srcPath.slice(-1)[0];
|
|
1961
|
+
var hasTrailingSlash = (
|
|
1962
|
+
(result.host || relative.host || srcPath.length > 1) &&
|
|
1963
|
+
(last === '.' || last === '..') || last === '');
|
|
1964
|
+
|
|
1965
|
+
// strip single dots, resolve double dots to parent dir
|
|
1966
|
+
// if the path tries to go above the root, `up` ends up > 0
|
|
1967
|
+
var up = 0;
|
|
1968
|
+
for (var i = srcPath.length; i >= 0; i--) {
|
|
1969
|
+
last = srcPath[i];
|
|
1970
|
+
if (last === '.') {
|
|
1971
|
+
srcPath.splice(i, 1);
|
|
1972
|
+
} else if (last === '..') {
|
|
1973
|
+
srcPath.splice(i, 1);
|
|
1974
|
+
up++;
|
|
1975
|
+
} else if (up) {
|
|
1976
|
+
srcPath.splice(i, 1);
|
|
1977
|
+
up--;
|
|
1978
|
+
}
|
|
1979
|
+
}
|
|
1980
|
+
|
|
1981
|
+
// if the path is allowed to go above the root, restore leading ..s
|
|
1982
|
+
if (!mustEndAbs && !removeAllDots) {
|
|
1983
|
+
for (; up--; up) {
|
|
1984
|
+
srcPath.unshift('..');
|
|
1985
|
+
}
|
|
1986
|
+
}
|
|
1987
|
+
|
|
1988
|
+
if (mustEndAbs && srcPath[0] !== '' &&
|
|
1989
|
+
(!srcPath[0] || srcPath[0].charAt(0) !== '/')) {
|
|
1990
|
+
srcPath.unshift('');
|
|
1991
|
+
}
|
|
1992
|
+
|
|
1993
|
+
if (hasTrailingSlash && (srcPath.join('/').substr(-1) !== '/')) {
|
|
1994
|
+
srcPath.push('');
|
|
1995
|
+
}
|
|
1996
|
+
|
|
1997
|
+
var isAbsolute = srcPath[0] === '' ||
|
|
1998
|
+
(srcPath[0] && srcPath[0].charAt(0) === '/');
|
|
1999
|
+
|
|
2000
|
+
// put the host back
|
|
2001
|
+
if (psychotic) {
|
|
2002
|
+
result.hostname = result.host = isAbsolute ? '' :
|
|
2003
|
+
srcPath.length ? srcPath.shift() : '';
|
|
2004
|
+
//occationaly the auth can get stuck only in host
|
|
2005
|
+
//this especially happens in cases like
|
|
2006
|
+
//url.resolveObject('mailto:local1@domain1', 'local2@domain2')
|
|
2007
|
+
var authInHost = result.host && result.host.indexOf('@') > 0 ?
|
|
2008
|
+
result.host.split('@') : false;
|
|
2009
|
+
if (authInHost) {
|
|
2010
|
+
result.auth = authInHost.shift();
|
|
2011
|
+
result.host = result.hostname = authInHost.shift();
|
|
2012
|
+
}
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
mustEndAbs = mustEndAbs || (result.host && srcPath.length);
|
|
2016
|
+
|
|
2017
|
+
if (mustEndAbs && !isAbsolute) {
|
|
2018
|
+
srcPath.unshift('');
|
|
2019
|
+
}
|
|
2020
|
+
|
|
2021
|
+
if (!srcPath.length) {
|
|
2022
|
+
result.pathname = null;
|
|
2023
|
+
result.path = null;
|
|
2024
|
+
} else {
|
|
2025
|
+
result.pathname = srcPath.join('/');
|
|
2026
|
+
}
|
|
2027
|
+
|
|
2028
|
+
//to support request.http
|
|
2029
|
+
if (!util.isNull(result.pathname) || !util.isNull(result.search)) {
|
|
2030
|
+
result.path = (result.pathname ? result.pathname : '') +
|
|
2031
|
+
(result.search ? result.search : '');
|
|
2032
|
+
}
|
|
2033
|
+
result.auth = relative.auth || result.auth;
|
|
2034
|
+
result.slashes = result.slashes || relative.slashes;
|
|
2035
|
+
result.href = result.format();
|
|
2036
|
+
return result;
|
|
2037
|
+
};
|
|
2038
|
+
|
|
2039
|
+
Url.prototype.parseHost = function() {
|
|
2040
|
+
var host = this.host;
|
|
2041
|
+
var port = portPattern.exec(host);
|
|
2042
|
+
if (port) {
|
|
2043
|
+
port = port[0];
|
|
2044
|
+
if (port !== ':') {
|
|
2045
|
+
this.port = port.substr(1);
|
|
2046
|
+
}
|
|
2047
|
+
host = host.substr(0, host.length - port.length);
|
|
2048
|
+
}
|
|
2049
|
+
if (host) this.hostname = host;
|
|
2050
|
+
};
|
|
2051
|
+
|
|
2052
|
+
/**
|
|
2053
|
+
* Returns a relative URL if the given request URL is relative to the current origin.
|
|
2054
|
+
* Otherwise returns an absolute URL.
|
|
2055
|
+
*/
|
|
2056
|
+
const getPublicUrlFromRequest = (request) => {
|
|
2057
|
+
return request.referrer.startsWith(request.url.origin)
|
|
2058
|
+
? request.url.pathname
|
|
2059
|
+
: format({
|
|
2060
|
+
protocol: request.url.protocol,
|
|
2061
|
+
host: request.url.host,
|
|
2062
|
+
pathname: request.url.pathname,
|
|
2063
|
+
});
|
|
2064
|
+
};
|
|
2065
|
+
|
|
711
2066
|
function onUnhandledRequest(request, onUnhandledRequest = 'bypass') {
|
|
712
2067
|
if (typeof onUnhandledRequest === 'function') {
|
|
713
2068
|
onUnhandledRequest(request);
|
|
714
2069
|
return;
|
|
715
2070
|
}
|
|
716
|
-
const
|
|
2071
|
+
const publicUrl = getPublicUrlFromRequest(request);
|
|
2072
|
+
const message = `captured a ${request.method} ${request.url} request without a corresponding request handler.
|
|
2073
|
+
|
|
2074
|
+
If you wish to intercept this request, consider creating a request handler for it:
|
|
2075
|
+
|
|
2076
|
+
rest.${request.method.toLowerCase()}('${publicUrl}', (req, res, ctx) => {
|
|
2077
|
+
return res(ctx.text('body'))
|
|
2078
|
+
})`;
|
|
717
2079
|
switch (onUnhandledRequest) {
|
|
718
2080
|
case 'error': {
|
|
719
2081
|
throw new Error(`[MSW] Error: ${message}`);
|
|
@@ -730,100 +2092,112 @@ const DEFAULT_LISTEN_OPTIONS = {
|
|
|
730
2092
|
onUnhandledRequest: 'bypass',
|
|
731
2093
|
};
|
|
732
2094
|
/**
|
|
733
|
-
*
|
|
2095
|
+
* Creates a `setupServer` API using given request interceptors.
|
|
2096
|
+
* Useful to generate identical API using different patches to request issuing modules.
|
|
734
2097
|
*/
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
2098
|
+
function createSetupServer(...interceptors) {
|
|
2099
|
+
/**
|
|
2100
|
+
* Sets up a server-side requests interception with the given mock definition.
|
|
2101
|
+
*/
|
|
2102
|
+
return function setupServer(...requestHandlers) {
|
|
2103
|
+
requestHandlers.forEach((handler) => {
|
|
2104
|
+
if (Array.isArray(handler))
|
|
2105
|
+
throw new Error(`[MSW] Failed to call "setupServer" given an Array of request handlers (setupServer([a, b])), expected to receive each handler individually: setupServer(a, b).`);
|
|
2106
|
+
});
|
|
2107
|
+
const interceptor = new nodeRequestInterceptor.RequestInterceptor(interceptors);
|
|
2108
|
+
// Error when attempting to run this function in a browser environment.
|
|
2109
|
+
if (!isNodeProcess()) {
|
|
2110
|
+
throw new Error('[MSW] Failed to execute `setupServer` in the environment that is not NodeJS (i.e. a browser). Consider using `setupWorker` instead.');
|
|
2111
|
+
}
|
|
2112
|
+
// Store the list of request handlers for the current server instance,
|
|
2113
|
+
// so it could be modified at a runtime.
|
|
2114
|
+
let currentHandlers = [...requestHandlers];
|
|
2115
|
+
return {
|
|
2116
|
+
/**
|
|
2117
|
+
* Enables requests interception based on the previously provided mock definition.
|
|
2118
|
+
*/
|
|
2119
|
+
listen(options) {
|
|
2120
|
+
const resolvedOptions = Object.assign({}, DEFAULT_LISTEN_OPTIONS, options);
|
|
2121
|
+
interceptor.use((req) => __awaiter(this, void 0, void 0, function* () {
|
|
2122
|
+
const requestHeaders = new lib.Headers(lib.flattenHeadersObject(req.headers || {}));
|
|
2123
|
+
const requestCookieString = requestHeaders.get('cookie');
|
|
2124
|
+
const mockedRequest = {
|
|
2125
|
+
url: req.url,
|
|
2126
|
+
method: req.method,
|
|
2127
|
+
// Parse the request's body based on the "Content-Type" header.
|
|
2128
|
+
body: parseBody(req.body, requestHeaders),
|
|
2129
|
+
headers: requestHeaders,
|
|
2130
|
+
cookies: {},
|
|
2131
|
+
params: {},
|
|
2132
|
+
redirect: 'manual',
|
|
2133
|
+
referrer: '',
|
|
2134
|
+
keepalive: false,
|
|
2135
|
+
cache: 'default',
|
|
2136
|
+
mode: 'cors',
|
|
2137
|
+
referrerPolicy: 'no-referrer',
|
|
2138
|
+
integrity: '',
|
|
2139
|
+
destination: 'document',
|
|
2140
|
+
bodyUsed: false,
|
|
2141
|
+
credentials: 'same-origin',
|
|
2142
|
+
};
|
|
2143
|
+
if (requestCookieString) {
|
|
2144
|
+
// Set mocked request cookies from the `cookie` header of the original request.
|
|
2145
|
+
// No need to take `credentials` into account, because in NodeJS requests are intercepted
|
|
2146
|
+
// _after_ they happen. Request issuer should have already taken care of sending relevant cookies.
|
|
2147
|
+
// Unlike browser, where interception is on the worker level, _before_ the request happens.
|
|
2148
|
+
mockedRequest.cookies = parse_1(requestCookieString);
|
|
2149
|
+
}
|
|
2150
|
+
if (mockedRequest.headers.get('x-msw-bypass')) {
|
|
2151
|
+
return;
|
|
2152
|
+
}
|
|
2153
|
+
const { response } = yield getResponse(mockedRequest, currentHandlers);
|
|
2154
|
+
if (!response) {
|
|
2155
|
+
onUnhandledRequest(mockedRequest, resolvedOptions.onUnhandledRequest);
|
|
2156
|
+
return;
|
|
2157
|
+
}
|
|
2158
|
+
return new Promise((resolve) => {
|
|
2159
|
+
var _a;
|
|
2160
|
+
// the node build will use the timers module to ensure @sinon/fake-timers or jest fake timers
|
|
2161
|
+
// don't affect this timeout.
|
|
2162
|
+
timers.setTimeout(() => {
|
|
2163
|
+
resolve({
|
|
2164
|
+
status: response.status,
|
|
2165
|
+
statusText: response.statusText,
|
|
2166
|
+
headers: response.headers.getAllHeaders(),
|
|
2167
|
+
body: response.body,
|
|
2168
|
+
});
|
|
2169
|
+
}, (_a = response.delay) !== null && _a !== void 0 ? _a : 0);
|
|
2170
|
+
});
|
|
2171
|
+
}));
|
|
2172
|
+
},
|
|
2173
|
+
/**
|
|
2174
|
+
* Prepends given request handlers to the list of existing handlers.
|
|
2175
|
+
*/
|
|
2176
|
+
use(...handlers) {
|
|
2177
|
+
use(currentHandlers, ...handlers);
|
|
2178
|
+
},
|
|
2179
|
+
/**
|
|
2180
|
+
* Marks all request handlers that respond using `res.once()` as unused.
|
|
2181
|
+
*/
|
|
2182
|
+
restoreHandlers() {
|
|
2183
|
+
restoreHandlers(currentHandlers);
|
|
2184
|
+
},
|
|
2185
|
+
/**
|
|
2186
|
+
* Resets request handlers to the initial list given to the `setupServer` call, or to the explicit next request handlers list, if given.
|
|
2187
|
+
*/
|
|
2188
|
+
resetHandlers(...nextHandlers) {
|
|
2189
|
+
currentHandlers = resetHandlers(requestHandlers, ...nextHandlers);
|
|
2190
|
+
},
|
|
2191
|
+
/**
|
|
2192
|
+
* Stops requests interception by restoring all augmented modules.
|
|
2193
|
+
*/
|
|
2194
|
+
close() {
|
|
2195
|
+
interceptor.restore();
|
|
2196
|
+
},
|
|
2197
|
+
};
|
|
826
2198
|
};
|
|
827
|
-
}
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
const setupServer = createSetupServer(ClientRequest.interceptClientRequest, XMLHttpRequest.interceptXMLHttpRequest);
|
|
828
2202
|
|
|
829
2203
|
exports.setupServer = setupServer;
|