@wiajs/req 1.7.7
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 +1023 -0
- package/LICENSE +7 -0
- package/MIGRATION_GUIDE.md +3 -0
- package/README.md +1645 -0
- package/SECURITY.md +6 -0
- package/dist/node/req.cjs +4397 -0
- package/dist/node/req.mjs +3551 -0
- package/dist/web/req.mjs +2609 -0
- package/index.d.cts +545 -0
- package/index.d.ts +565 -0
- package/index.js +43 -0
- package/lib/adapters/README.md +37 -0
- package/lib/adapters/adapters.js +57 -0
- package/lib/adapters/fetch.js +229 -0
- package/lib/adapters/http.js +552 -0
- package/lib/adapters/xhr.js +200 -0
- package/lib/axios.js +89 -0
- package/lib/cancel/CancelToken.js +106 -0
- package/lib/cancel/CanceledError.js +20 -0
- package/lib/cancel/isCancel.js +4 -0
- package/lib/core/Axios.js +359 -0
- package/lib/core/AxiosError.js +89 -0
- package/lib/core/AxiosHeaders.js +243 -0
- package/lib/core/InterceptorManager.js +59 -0
- package/lib/core/README.md +8 -0
- package/lib/core/buildFullPath.js +18 -0
- package/lib/core/dispatchRequest.js +72 -0
- package/lib/core/mergeConfig.js +98 -0
- package/lib/core/settle.js +21 -0
- package/lib/core/transformData.js +22 -0
- package/lib/defaults/index.js +136 -0
- package/lib/defaults/transitional.js +6 -0
- package/lib/env/README.md +3 -0
- package/lib/env/classes/FormData.js +2 -0
- package/lib/env/data.js +1 -0
- package/lib/helpers/AxiosTransformStream.js +116 -0
- package/lib/helpers/AxiosURLSearchParams.js +50 -0
- package/lib/helpers/HttpStatusCode.js +69 -0
- package/lib/helpers/README.md +7 -0
- package/lib/helpers/ZlibHeaderTransformStream.js +22 -0
- package/lib/helpers/bind.js +6 -0
- package/lib/helpers/buildURL.js +42 -0
- package/lib/helpers/callbackify.js +14 -0
- package/lib/helpers/combineURLs.js +11 -0
- package/lib/helpers/composeSignals.js +37 -0
- package/lib/helpers/cookies.js +29 -0
- package/lib/helpers/deprecatedMethod.js +18 -0
- package/lib/helpers/formDataToJSON.js +78 -0
- package/lib/helpers/formDataToStream.js +77 -0
- package/lib/helpers/fromDataURI.js +44 -0
- package/lib/helpers/isAbsoluteURL.js +13 -0
- package/lib/helpers/isAxiosError.js +11 -0
- package/lib/helpers/isURLSameOrigin.js +50 -0
- package/lib/helpers/null.js +2 -0
- package/lib/helpers/parseHeaders.js +61 -0
- package/lib/helpers/parseProtocol.js +5 -0
- package/lib/helpers/progressEventReducer.js +54 -0
- package/lib/helpers/readBlob.js +13 -0
- package/lib/helpers/resolveConfig.js +45 -0
- package/lib/helpers/speedometer.js +40 -0
- package/lib/helpers/spread.js +26 -0
- package/lib/helpers/throttle.js +41 -0
- package/lib/helpers/toFormData.js +175 -0
- package/lib/helpers/toURLEncodedForm.js +15 -0
- package/lib/helpers/trackStream.js +75 -0
- package/lib/helpers/validator.js +84 -0
- package/lib/platform/browser/classes/Blob.js +2 -0
- package/lib/platform/browser/classes/FormData.js +2 -0
- package/lib/platform/browser/classes/URLSearchParams.js +3 -0
- package/lib/platform/browser/index.js +19 -0
- package/lib/platform/common/utils.js +37 -0
- package/lib/platform/index.js +6 -0
- package/lib/platform/node/classes/FormData.js +2 -0
- package/lib/platform/node/classes/URLSearchParams.js +3 -0
- package/lib/platform/node/index.js +16 -0
- package/lib/req.js +67 -0
- package/lib/utils.js +635 -0
- package/package.json +214 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const HttpStatusCode = {
|
|
2
|
+
Continue: 100,
|
|
3
|
+
SwitchingProtocols: 101,
|
|
4
|
+
Processing: 102,
|
|
5
|
+
EarlyHints: 103,
|
|
6
|
+
Ok: 200,
|
|
7
|
+
Created: 201,
|
|
8
|
+
Accepted: 202,
|
|
9
|
+
NonAuthoritativeInformation: 203,
|
|
10
|
+
NoContent: 204,
|
|
11
|
+
ResetContent: 205,
|
|
12
|
+
PartialContent: 206,
|
|
13
|
+
MultiStatus: 207,
|
|
14
|
+
AlreadyReported: 208,
|
|
15
|
+
ImUsed: 226,
|
|
16
|
+
MultipleChoices: 300,
|
|
17
|
+
MovedPermanently: 301,
|
|
18
|
+
Found: 302,
|
|
19
|
+
SeeOther: 303,
|
|
20
|
+
NotModified: 304,
|
|
21
|
+
UseProxy: 305,
|
|
22
|
+
Unused: 306,
|
|
23
|
+
TemporaryRedirect: 307,
|
|
24
|
+
PermanentRedirect: 308,
|
|
25
|
+
BadRequest: 400,
|
|
26
|
+
Unauthorized: 401,
|
|
27
|
+
PaymentRequired: 402,
|
|
28
|
+
Forbidden: 403,
|
|
29
|
+
NotFound: 404,
|
|
30
|
+
MethodNotAllowed: 405,
|
|
31
|
+
NotAcceptable: 406,
|
|
32
|
+
ProxyAuthenticationRequired: 407,
|
|
33
|
+
RequestTimeout: 408,
|
|
34
|
+
Conflict: 409,
|
|
35
|
+
Gone: 410,
|
|
36
|
+
LengthRequired: 411,
|
|
37
|
+
PreconditionFailed: 412,
|
|
38
|
+
PayloadTooLarge: 413,
|
|
39
|
+
UriTooLong: 414,
|
|
40
|
+
UnsupportedMediaType: 415,
|
|
41
|
+
RangeNotSatisfiable: 416,
|
|
42
|
+
ExpectationFailed: 417,
|
|
43
|
+
ImATeapot: 418,
|
|
44
|
+
MisdirectedRequest: 421,
|
|
45
|
+
UnprocessableEntity: 422,
|
|
46
|
+
Locked: 423,
|
|
47
|
+
FailedDependency: 424,
|
|
48
|
+
TooEarly: 425,
|
|
49
|
+
UpgradeRequired: 426,
|
|
50
|
+
PreconditionRequired: 428,
|
|
51
|
+
TooManyRequests: 429,
|
|
52
|
+
RequestHeaderFieldsTooLarge: 431,
|
|
53
|
+
UnavailableForLegalReasons: 451,
|
|
54
|
+
InternalServerError: 500,
|
|
55
|
+
NotImplemented: 501,
|
|
56
|
+
BadGateway: 502,
|
|
57
|
+
ServiceUnavailable: 503,
|
|
58
|
+
GatewayTimeout: 504,
|
|
59
|
+
HttpVersionNotSupported: 505,
|
|
60
|
+
VariantAlsoNegotiates: 506,
|
|
61
|
+
InsufficientStorage: 507,
|
|
62
|
+
LoopDetected: 508,
|
|
63
|
+
NotExtended: 510,
|
|
64
|
+
NetworkAuthenticationRequired: 511
|
|
65
|
+
};
|
|
66
|
+
Object.entries(HttpStatusCode).forEach(([key, value])=>{
|
|
67
|
+
HttpStatusCode[value] = key;
|
|
68
|
+
});
|
|
69
|
+
export default HttpStatusCode;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# axios // helpers
|
|
2
|
+
|
|
3
|
+
The modules found in `helpers/` should be generic modules that are _not_ specific to the domain logic of axios. These modules could theoretically be published to npm on their own and consumed by other modules or apps. Some examples of generic modules are things like:
|
|
4
|
+
|
|
5
|
+
- Browser polyfills
|
|
6
|
+
- Managing cookies
|
|
7
|
+
- Parsing HTTP headers
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import stream from "stream";
|
|
3
|
+
let ZlibHeaderTransformStream = class ZlibHeaderTransformStream extends stream.Transform {
|
|
4
|
+
__transform(chunk, encoding, callback) {
|
|
5
|
+
this.push(chunk);
|
|
6
|
+
callback();
|
|
7
|
+
}
|
|
8
|
+
_transform(chunk, encoding, callback) {
|
|
9
|
+
if (chunk.length !== 0) {
|
|
10
|
+
this._transform = this.__transform;
|
|
11
|
+
// Add Default Compression headers if no zlib headers are present
|
|
12
|
+
if (chunk[0] !== 120) {
|
|
13
|
+
const header = Buffer.alloc(2);
|
|
14
|
+
header[0] = 120; // Hex: 78
|
|
15
|
+
header[1] = 156; // Hex: 9C
|
|
16
|
+
this.push(header, encoding);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
this.__transform(chunk, encoding, callback);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
export default ZlibHeaderTransformStream;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
import utils from '../utils.js';
|
|
3
|
+
import AxiosURLSearchParams from '../helpers/AxiosURLSearchParams.js';
|
|
4
|
+
/**
|
|
5
|
+
* It replaces all instances of the characters `:`, `$`, `,`, `+`, `[`, and `]` with their
|
|
6
|
+
* URI encoded counterparts
|
|
7
|
+
*
|
|
8
|
+
* @param {string} val The value to be encoded.
|
|
9
|
+
*
|
|
10
|
+
* @returns {string} The encoded value.
|
|
11
|
+
*/ function encode(val) {
|
|
12
|
+
return encodeURIComponent(val).replace(/%3A/gi, ':').replace(/%24/g, '$').replace(/%2C/gi, ',').replace(/%20/g, '+').replace(/%5B/gi, '[').replace(/%5D/gi, ']');
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Build a URL by appending params to the end
|
|
16
|
+
*
|
|
17
|
+
* @param {string} url The base of the url (e.g., http://www.google.com)
|
|
18
|
+
* @param {object} [params] The params to be appended
|
|
19
|
+
* @param {?object} options
|
|
20
|
+
*
|
|
21
|
+
* @returns {string} The formatted url
|
|
22
|
+
*/ export default function buildURL(url, params, options) {
|
|
23
|
+
/*eslint no-param-reassign:0*/ if (!params) {
|
|
24
|
+
return url;
|
|
25
|
+
}
|
|
26
|
+
const _encode = options && options.encode || encode;
|
|
27
|
+
const serializeFn = options && options.serialize;
|
|
28
|
+
let serializedParams;
|
|
29
|
+
if (serializeFn) {
|
|
30
|
+
serializedParams = serializeFn(params, options);
|
|
31
|
+
} else {
|
|
32
|
+
serializedParams = utils.isURLSearchParams(params) ? params.toString() : new AxiosURLSearchParams(params, options).toString(_encode);
|
|
33
|
+
}
|
|
34
|
+
if (serializedParams) {
|
|
35
|
+
const hashmarkIndex = url.indexOf("#");
|
|
36
|
+
if (hashmarkIndex !== -1) {
|
|
37
|
+
url = url.slice(0, hashmarkIndex);
|
|
38
|
+
}
|
|
39
|
+
url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams;
|
|
40
|
+
}
|
|
41
|
+
return url;
|
|
42
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import utils from "../utils.js";
|
|
2
|
+
const callbackify = (fn, reducer)=>{
|
|
3
|
+
return utils.isAsyncFn(fn) ? function(...args) {
|
|
4
|
+
const cb = args.pop();
|
|
5
|
+
fn.apply(this, args).then((value)=>{
|
|
6
|
+
try {
|
|
7
|
+
reducer ? cb(null, ...reducer(value)) : cb(null, value);
|
|
8
|
+
} catch (err) {
|
|
9
|
+
cb(err);
|
|
10
|
+
}
|
|
11
|
+
}, cb);
|
|
12
|
+
} : fn;
|
|
13
|
+
};
|
|
14
|
+
export default callbackify;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a new URL by combining the specified URLs
|
|
4
|
+
*
|
|
5
|
+
* @param {string} baseURL The base URL
|
|
6
|
+
* @param {string} relativeURL The relative URL
|
|
7
|
+
*
|
|
8
|
+
* @returns {string} The combined URL
|
|
9
|
+
*/ export default function combineURLs(baseURL, relativeURL) {
|
|
10
|
+
return relativeURL ? baseURL.replace(/\/?\/$/, '') + '/' + relativeURL.replace(/^\/+/, '') : baseURL;
|
|
11
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import CanceledError from "../cancel/CanceledError.js";
|
|
2
|
+
import AxiosError from "../core/AxiosError.js";
|
|
3
|
+
import utils from '../utils.js';
|
|
4
|
+
const composeSignals = (signals, timeout)=>{
|
|
5
|
+
const { length } = signals = signals ? signals.filter(Boolean) : [];
|
|
6
|
+
if (timeout || length) {
|
|
7
|
+
let controller = new AbortController();
|
|
8
|
+
let aborted;
|
|
9
|
+
const onabort = function(reason) {
|
|
10
|
+
if (!aborted) {
|
|
11
|
+
aborted = true;
|
|
12
|
+
unsubscribe();
|
|
13
|
+
const err = reason instanceof Error ? reason : this.reason;
|
|
14
|
+
controller.abort(err instanceof AxiosError ? err : new CanceledError(err instanceof Error ? err.message : err));
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
let timer = timeout && setTimeout(()=>{
|
|
18
|
+
timer = null;
|
|
19
|
+
onabort(new AxiosError(`timeout ${timeout} of ms exceeded`, AxiosError.ETIMEDOUT));
|
|
20
|
+
}, timeout);
|
|
21
|
+
const unsubscribe = ()=>{
|
|
22
|
+
if (signals) {
|
|
23
|
+
timer && clearTimeout(timer);
|
|
24
|
+
timer = null;
|
|
25
|
+
signals.forEach((signal)=>{
|
|
26
|
+
signal.unsubscribe ? signal.unsubscribe(onabort) : signal.removeEventListener('abort', onabort);
|
|
27
|
+
});
|
|
28
|
+
signals = null;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
signals.forEach((signal)=>signal.addEventListener('abort', onabort));
|
|
32
|
+
const { signal } = controller;
|
|
33
|
+
signal.unsubscribe = ()=>utils.asap(unsubscribe);
|
|
34
|
+
return signal;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
export default composeSignals;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import utils from './../utils.js';
|
|
2
|
+
import platform from '../platform/index.js';
|
|
3
|
+
export default platform.hasStandardBrowserEnv ? // Standard browser envs support document.cookie
|
|
4
|
+
{
|
|
5
|
+
write (name, value, expires, path, domain, secure) {
|
|
6
|
+
const cookie = [
|
|
7
|
+
name + '=' + encodeURIComponent(value)
|
|
8
|
+
];
|
|
9
|
+
utils.isNumber(expires) && cookie.push('expires=' + new Date(expires).toGMTString());
|
|
10
|
+
utils.isString(path) && cookie.push('path=' + path);
|
|
11
|
+
utils.isString(domain) && cookie.push('domain=' + domain);
|
|
12
|
+
secure === true && cookie.push('secure');
|
|
13
|
+
document.cookie = cookie.join('; ');
|
|
14
|
+
},
|
|
15
|
+
read (name) {
|
|
16
|
+
const match = document.cookie.match(new RegExp('(^|;\\s*)(' + name + ')=([^;]*)'));
|
|
17
|
+
return match ? decodeURIComponent(match[3]) : null;
|
|
18
|
+
},
|
|
19
|
+
remove (name) {
|
|
20
|
+
this.write(name, '', Date.now() - 86400000);
|
|
21
|
+
}
|
|
22
|
+
} : // Non-standard browser env (web workers, react-native) lack needed support.
|
|
23
|
+
{
|
|
24
|
+
write () {},
|
|
25
|
+
read () {
|
|
26
|
+
return null;
|
|
27
|
+
},
|
|
28
|
+
remove () {}
|
|
29
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/*eslint no-console:0*/ /**
|
|
3
|
+
* Supply a warning to the developer that a method they are using
|
|
4
|
+
* has been deprecated.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} method The name of the deprecated method
|
|
7
|
+
* @param {string} [instead] The alternate method to use if applicable
|
|
8
|
+
* @param {string} [docs] The documentation URL to get further details
|
|
9
|
+
*
|
|
10
|
+
* @returns {void}
|
|
11
|
+
*/ export default function deprecatedMethod(method, instead, docs) {
|
|
12
|
+
try {
|
|
13
|
+
console.warn('DEPRECATED method `' + method + '`.' + (instead ? ' Use `' + instead + '` instead.' : '') + ' This method will be removed in a future release.');
|
|
14
|
+
if (docs) {
|
|
15
|
+
console.warn('For more information about usage see ' + docs);
|
|
16
|
+
}
|
|
17
|
+
} catch (e) {}
|
|
18
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
import utils from '../utils.js';
|
|
3
|
+
/**
|
|
4
|
+
* It takes a string like `foo[x][y][z]` and returns an array like `['foo', 'x', 'y', 'z']
|
|
5
|
+
*
|
|
6
|
+
* @param {string} name - The name of the property to get.
|
|
7
|
+
*
|
|
8
|
+
* @returns An array of strings.
|
|
9
|
+
*/ function parsePropPath(name) {
|
|
10
|
+
// foo[x][y][z]
|
|
11
|
+
// foo.x.y.z
|
|
12
|
+
// foo-x-y-z
|
|
13
|
+
// foo x y z
|
|
14
|
+
return utils.matchAll(/\w+|\[(\w*)]/g, name).map((match)=>{
|
|
15
|
+
return match[0] === '[]' ? '' : match[1] || match[0];
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Convert an array to an object.
|
|
20
|
+
*
|
|
21
|
+
* @param {Array<any>} arr - The array to convert to an object.
|
|
22
|
+
*
|
|
23
|
+
* @returns An object with the same keys and values as the array.
|
|
24
|
+
*/ function arrayToObject(arr) {
|
|
25
|
+
const obj = {};
|
|
26
|
+
const keys = Object.keys(arr);
|
|
27
|
+
let i;
|
|
28
|
+
const len = keys.length;
|
|
29
|
+
let key;
|
|
30
|
+
for(i = 0; i < len; i++){
|
|
31
|
+
key = keys[i];
|
|
32
|
+
obj[key] = arr[key];
|
|
33
|
+
}
|
|
34
|
+
return obj;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* It takes a FormData object and returns a JavaScript object
|
|
38
|
+
*
|
|
39
|
+
* @param {string} formData The FormData object to convert to JSON.
|
|
40
|
+
*
|
|
41
|
+
* @returns {Object<string, any> | null} The converted object.
|
|
42
|
+
*/ function formDataToJSON(formData) {
|
|
43
|
+
function buildPath(path, value, target, index) {
|
|
44
|
+
let name = path[index++];
|
|
45
|
+
if (name === '__proto__') return true;
|
|
46
|
+
const isNumericKey = Number.isFinite(+name);
|
|
47
|
+
const isLast = index >= path.length;
|
|
48
|
+
name = !name && utils.isArray(target) ? target.length : name;
|
|
49
|
+
if (isLast) {
|
|
50
|
+
if (utils.hasOwnProp(target, name)) {
|
|
51
|
+
target[name] = [
|
|
52
|
+
target[name],
|
|
53
|
+
value
|
|
54
|
+
];
|
|
55
|
+
} else {
|
|
56
|
+
target[name] = value;
|
|
57
|
+
}
|
|
58
|
+
return !isNumericKey;
|
|
59
|
+
}
|
|
60
|
+
if (!target[name] || !utils.isObject(target[name])) {
|
|
61
|
+
target[name] = [];
|
|
62
|
+
}
|
|
63
|
+
const result = buildPath(path, value, target[name], index);
|
|
64
|
+
if (result && utils.isArray(target[name])) {
|
|
65
|
+
target[name] = arrayToObject(target[name]);
|
|
66
|
+
}
|
|
67
|
+
return !isNumericKey;
|
|
68
|
+
}
|
|
69
|
+
if (utils.isFormData(formData) && utils.isFunction(formData.entries)) {
|
|
70
|
+
const obj = {};
|
|
71
|
+
utils.forEachEntry(formData, (name, value)=>{
|
|
72
|
+
buildPath(parsePropPath(name), value, obj, 0);
|
|
73
|
+
});
|
|
74
|
+
return obj;
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
export default formDataToJSON;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { TextEncoder } from 'util';
|
|
2
|
+
import { Readable } from 'stream';
|
|
3
|
+
import utils from "../utils.js";
|
|
4
|
+
import readBlob from "./readBlob.js";
|
|
5
|
+
const BOUNDARY_ALPHABET = utils.ALPHABET.ALPHA_DIGIT + '-_';
|
|
6
|
+
const textEncoder = new TextEncoder();
|
|
7
|
+
const CRLF = '\r\n';
|
|
8
|
+
const CRLF_BYTES = textEncoder.encode(CRLF);
|
|
9
|
+
const CRLF_BYTES_COUNT = 2;
|
|
10
|
+
let FormDataPart = class FormDataPart {
|
|
11
|
+
constructor(name, value){
|
|
12
|
+
const { escapeName } = this.constructor;
|
|
13
|
+
const isStringValue = utils.isString(value);
|
|
14
|
+
let headers = `Content-Disposition: form-data; name="${escapeName(name)}"${!isStringValue && value.name ? `; filename="${escapeName(value.name)}"` : ''}${CRLF}`;
|
|
15
|
+
if (isStringValue) {
|
|
16
|
+
value = textEncoder.encode(String(value).replace(/\r?\n|\r\n?/g, CRLF));
|
|
17
|
+
} else {
|
|
18
|
+
headers += `Content-Type: ${value.type || "application/octet-stream"}${CRLF}`;
|
|
19
|
+
}
|
|
20
|
+
this.headers = textEncoder.encode(headers + CRLF);
|
|
21
|
+
this.contentLength = isStringValue ? value.byteLength : value.size;
|
|
22
|
+
this.size = this.headers.byteLength + this.contentLength + CRLF_BYTES_COUNT;
|
|
23
|
+
this.name = name;
|
|
24
|
+
this.value = value;
|
|
25
|
+
}
|
|
26
|
+
async *encode() {
|
|
27
|
+
yield this.headers;
|
|
28
|
+
const { value } = this;
|
|
29
|
+
if (utils.isTypedArray(value)) {
|
|
30
|
+
yield value;
|
|
31
|
+
} else {
|
|
32
|
+
yield* readBlob(value);
|
|
33
|
+
}
|
|
34
|
+
yield CRLF_BYTES;
|
|
35
|
+
}
|
|
36
|
+
static escapeName(name) {
|
|
37
|
+
return String(name).replace(/[\r\n"]/g, (match)=>({
|
|
38
|
+
'\r': '%0D',
|
|
39
|
+
'\n': '%0A',
|
|
40
|
+
'"': '%22'
|
|
41
|
+
})[match]);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const formDataToStream = (form, headersHandler, options)=>{
|
|
45
|
+
const { tag = 'form-data-boundary', size = 25, boundary = tag + '-' + utils.generateString(size, BOUNDARY_ALPHABET) } = options || {};
|
|
46
|
+
if (!utils.isFormData(form)) {
|
|
47
|
+
throw TypeError('FormData instance required');
|
|
48
|
+
}
|
|
49
|
+
if (boundary.length < 1 || boundary.length > 70) {
|
|
50
|
+
throw Error('boundary must be 10-70 characters long');
|
|
51
|
+
}
|
|
52
|
+
const boundaryBytes = textEncoder.encode('--' + boundary + CRLF);
|
|
53
|
+
const footerBytes = textEncoder.encode('--' + boundary + '--' + CRLF + CRLF);
|
|
54
|
+
let contentLength = footerBytes.byteLength;
|
|
55
|
+
const parts = Array.from(form.entries()).map(([name, value])=>{
|
|
56
|
+
const part = new FormDataPart(name, value);
|
|
57
|
+
contentLength += part.size;
|
|
58
|
+
return part;
|
|
59
|
+
});
|
|
60
|
+
contentLength += boundaryBytes.byteLength * parts.length;
|
|
61
|
+
contentLength = utils.toFiniteNumber(contentLength);
|
|
62
|
+
const computedHeaders = {
|
|
63
|
+
'Content-Type': `multipart/form-data; boundary=${boundary}`
|
|
64
|
+
};
|
|
65
|
+
if (Number.isFinite(contentLength)) {
|
|
66
|
+
computedHeaders['Content-Length'] = contentLength;
|
|
67
|
+
}
|
|
68
|
+
headersHandler && headersHandler(computedHeaders);
|
|
69
|
+
return Readable.from(async function*() {
|
|
70
|
+
for (const part of parts){
|
|
71
|
+
yield boundaryBytes;
|
|
72
|
+
yield* part.encode();
|
|
73
|
+
}
|
|
74
|
+
yield footerBytes;
|
|
75
|
+
}());
|
|
76
|
+
};
|
|
77
|
+
export default formDataToStream;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
import AxiosError from '../core/AxiosError.js';
|
|
3
|
+
import parseProtocol from './parseProtocol.js';
|
|
4
|
+
import platform from '../platform/index.js';
|
|
5
|
+
const DATA_URL_PATTERN = /^(?:([^;]+);)?(?:[^;]+;)?(base64|),([\s\S]*)$/;
|
|
6
|
+
/**
|
|
7
|
+
* Parse data uri to a Buffer or Blob
|
|
8
|
+
*
|
|
9
|
+
* @param {String} uri
|
|
10
|
+
* @param {?Boolean} asBlob
|
|
11
|
+
* @param {?Object} options
|
|
12
|
+
* @param {?Function} options.Blob
|
|
13
|
+
*
|
|
14
|
+
* @returns {Buffer|Blob}
|
|
15
|
+
*/ export default function fromDataURI(uri, asBlob, options) {
|
|
16
|
+
const _Blob = options && options.Blob || platform.classes.Blob;
|
|
17
|
+
const protocol = parseProtocol(uri);
|
|
18
|
+
if (asBlob === undefined && _Blob) {
|
|
19
|
+
asBlob = true;
|
|
20
|
+
}
|
|
21
|
+
if (protocol === 'data') {
|
|
22
|
+
uri = protocol.length ? uri.slice(protocol.length + 1) : uri;
|
|
23
|
+
const match = DATA_URL_PATTERN.exec(uri);
|
|
24
|
+
if (!match) {
|
|
25
|
+
throw new AxiosError('Invalid URL', AxiosError.ERR_INVALID_URL);
|
|
26
|
+
}
|
|
27
|
+
const mime = match[1];
|
|
28
|
+
const isBase64 = match[2];
|
|
29
|
+
const body = match[3];
|
|
30
|
+
const buffer = Buffer.from(decodeURIComponent(body), isBase64 ? 'base64' : 'utf8');
|
|
31
|
+
if (asBlob) {
|
|
32
|
+
if (!_Blob) {
|
|
33
|
+
throw new AxiosError('Blob is not supported', AxiosError.ERR_NOT_SUPPORT);
|
|
34
|
+
}
|
|
35
|
+
return new _Blob([
|
|
36
|
+
buffer
|
|
37
|
+
], {
|
|
38
|
+
type: mime
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return buffer;
|
|
42
|
+
}
|
|
43
|
+
throw new AxiosError('Unsupported protocol ' + protocol, AxiosError.ERR_NOT_SUPPORT);
|
|
44
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* Determines whether the specified URL is absolute
|
|
4
|
+
*
|
|
5
|
+
* @param {string} url The URL to test
|
|
6
|
+
*
|
|
7
|
+
* @returns {boolean} True if the specified URL is absolute, otherwise false
|
|
8
|
+
*/ export default function isAbsoluteURL(url) {
|
|
9
|
+
// A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
|
|
10
|
+
// RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
|
|
11
|
+
// by any combination of letters, digits, plus, period, or hyphen.
|
|
12
|
+
return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(url);
|
|
13
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
import utils from './../utils.js';
|
|
3
|
+
/**
|
|
4
|
+
* Determines whether the payload is an error thrown by Axios
|
|
5
|
+
*
|
|
6
|
+
* @param {*} payload The value to test
|
|
7
|
+
*
|
|
8
|
+
* @returns {boolean} True if the payload is an error thrown by Axios, otherwise false
|
|
9
|
+
*/ export default function isAxiosError(payload) {
|
|
10
|
+
return utils.isObject(payload) && payload.isAxiosError === true;
|
|
11
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
import utils from './../utils.js';
|
|
3
|
+
import platform from '../platform/index.js';
|
|
4
|
+
export default platform.hasStandardBrowserEnv ? // Standard browser envs have full support of the APIs needed to test
|
|
5
|
+
// whether the request URL is of the same origin as current location.
|
|
6
|
+
function standardBrowserEnv() {
|
|
7
|
+
const msie = platform.navigator && /(msie|trident)/i.test(platform.navigator.userAgent);
|
|
8
|
+
const urlParsingNode = document.createElement('a');
|
|
9
|
+
let originURL;
|
|
10
|
+
/**
|
|
11
|
+
* Parse a URL to discover its components
|
|
12
|
+
*
|
|
13
|
+
* @param {String} url The URL to be parsed
|
|
14
|
+
* @returns {Object}
|
|
15
|
+
*/ function resolveURL(url) {
|
|
16
|
+
let href = url;
|
|
17
|
+
if (msie) {
|
|
18
|
+
// IE needs attribute set twice to normalize properties
|
|
19
|
+
urlParsingNode.setAttribute('href', href);
|
|
20
|
+
href = urlParsingNode.href;
|
|
21
|
+
}
|
|
22
|
+
urlParsingNode.setAttribute('href', href);
|
|
23
|
+
// urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
|
|
24
|
+
return {
|
|
25
|
+
href: urlParsingNode.href,
|
|
26
|
+
protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '',
|
|
27
|
+
host: urlParsingNode.host,
|
|
28
|
+
search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '',
|
|
29
|
+
hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '',
|
|
30
|
+
hostname: urlParsingNode.hostname,
|
|
31
|
+
port: urlParsingNode.port,
|
|
32
|
+
pathname: urlParsingNode.pathname.charAt(0) === '/' ? urlParsingNode.pathname : '/' + urlParsingNode.pathname
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
originURL = resolveURL(window.location.href);
|
|
36
|
+
/**
|
|
37
|
+
* Determine if a URL shares the same origin as the current location
|
|
38
|
+
*
|
|
39
|
+
* @param {String} requestURL The URL to test
|
|
40
|
+
* @returns {boolean} True if URL shares the same origin, otherwise false
|
|
41
|
+
*/ return function isURLSameOrigin(requestURL) {
|
|
42
|
+
const parsed = utils.isString(requestURL) ? resolveURL(requestURL) : requestURL;
|
|
43
|
+
return parsed.protocol === originURL.protocol && parsed.host === originURL.host;
|
|
44
|
+
};
|
|
45
|
+
}() : // Non standard browser envs (web workers, react-native) lack needed support.
|
|
46
|
+
function nonStandardBrowserEnv() {
|
|
47
|
+
return function isURLSameOrigin() {
|
|
48
|
+
return true;
|
|
49
|
+
};
|
|
50
|
+
}();
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import utils from '../utils.js';
|
|
2
|
+
// RawAxiosHeaders whose duplicates are ignored by node
|
|
3
|
+
// c.f. https://nodejs.org/api/http.html#http_message_headers
|
|
4
|
+
const ignoreDuplicateOf = utils.toObjectSet([
|
|
5
|
+
'age',
|
|
6
|
+
'authorization',
|
|
7
|
+
'content-length',
|
|
8
|
+
'content-type',
|
|
9
|
+
'etag',
|
|
10
|
+
'expires',
|
|
11
|
+
'from',
|
|
12
|
+
'host',
|
|
13
|
+
'if-modified-since',
|
|
14
|
+
'if-unmodified-since',
|
|
15
|
+
'last-modified',
|
|
16
|
+
'location',
|
|
17
|
+
'max-forwards',
|
|
18
|
+
'proxy-authorization',
|
|
19
|
+
'referer',
|
|
20
|
+
'retry-after',
|
|
21
|
+
'user-agent'
|
|
22
|
+
]);
|
|
23
|
+
/**
|
|
24
|
+
* Parse headers into an object
|
|
25
|
+
*
|
|
26
|
+
* ```
|
|
27
|
+
* Date: Wed, 27 Aug 2014 08:58:49 GMT
|
|
28
|
+
* Content-Type: application/json
|
|
29
|
+
* Connection: keep-alive
|
|
30
|
+
* Transfer-Encoding: chunked
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @param {String} rawHeaders Headers needing to be parsed
|
|
34
|
+
*
|
|
35
|
+
* @returns {Object} Headers parsed into an object
|
|
36
|
+
*/ export default ((rawHeaders)=>{
|
|
37
|
+
const parsed = {};
|
|
38
|
+
let key;
|
|
39
|
+
let val;
|
|
40
|
+
let i;
|
|
41
|
+
rawHeaders && rawHeaders.split('\n').forEach(function parser(line) {
|
|
42
|
+
i = line.indexOf(':');
|
|
43
|
+
key = line.substring(0, i).trim().toLowerCase();
|
|
44
|
+
val = line.substring(i + 1).trim();
|
|
45
|
+
if (!key || parsed[key] && ignoreDuplicateOf[key]) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (key === 'set-cookie') {
|
|
49
|
+
if (parsed[key]) {
|
|
50
|
+
parsed[key].push(val);
|
|
51
|
+
} else {
|
|
52
|
+
parsed[key] = [
|
|
53
|
+
val
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
return parsed;
|
|
61
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import speedometer from './speedometer.js';
|
|
2
|
+
import throttle from './throttle.js';
|
|
3
|
+
import utils from '../utils.js';
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param {*} listener
|
|
7
|
+
* @param {*} isDownloadStream
|
|
8
|
+
* @param {*} freq
|
|
9
|
+
* @returns
|
|
10
|
+
*/ export const progressEventReducer = (listener, isDownloadStream, freq = 3)=>{
|
|
11
|
+
let bytesNotified = 0;
|
|
12
|
+
const _speedometer = speedometer(50, 250);
|
|
13
|
+
return throttle((e)=>{
|
|
14
|
+
const loaded = e.loaded;
|
|
15
|
+
const total = e.lengthComputable ? e.total : undefined;
|
|
16
|
+
const progressBytes = loaded - bytesNotified;
|
|
17
|
+
const rate = _speedometer(progressBytes);
|
|
18
|
+
const inRange = loaded <= total;
|
|
19
|
+
bytesNotified = loaded;
|
|
20
|
+
const data = {
|
|
21
|
+
loaded,
|
|
22
|
+
total,
|
|
23
|
+
progress: total ? loaded / total : undefined,
|
|
24
|
+
bytes: progressBytes,
|
|
25
|
+
rate: rate ? rate : undefined,
|
|
26
|
+
estimated: rate && total && inRange ? (total - loaded) / rate : undefined,
|
|
27
|
+
event: e,
|
|
28
|
+
lengthComputable: total != null,
|
|
29
|
+
[isDownloadStream ? 'download' : 'upload']: true
|
|
30
|
+
};
|
|
31
|
+
listener(data);
|
|
32
|
+
}, freq);
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* @param {*} total
|
|
37
|
+
* @param {*} throttled
|
|
38
|
+
* @returns
|
|
39
|
+
*/ export const progressEventDecorator = (total, throttled)=>{
|
|
40
|
+
const lengthComputable = total != null;
|
|
41
|
+
return [
|
|
42
|
+
(loaded)=>throttled[0]({
|
|
43
|
+
lengthComputable,
|
|
44
|
+
total,
|
|
45
|
+
loaded
|
|
46
|
+
}),
|
|
47
|
+
throttled[1]
|
|
48
|
+
];
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
*
|
|
52
|
+
* @param {*} fn
|
|
53
|
+
* @returns
|
|
54
|
+
*/ export const asyncDecorator = (fn)=>(...args)=>utils.asap(()=>fn(...args));
|