mphttpx 2.2.1 → 2.3.1
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/README.md +994 -994
- package/dist/cjs/fetchP.js +22 -0
- package/dist/cjs/index.js +197 -55
- package/dist/esm/fetchP.js +12 -0
- package/dist/esm/index.js +5 -18
- package/dist/index.d.ts +8 -394
- package/dist/mphttpx.cjs.js +6646 -0
- package/dist/mphttpx.cjs.min.js +1 -0
- package/dist/mphttpx.esm.js +6595 -0
- package/dist/mphttpx.esm.min.js +1 -0
- package/package.json +28 -9
- package/dist/cjs/encoding/TextDecoderP.js +0 -174
- package/dist/cjs/encoding/TextEncoderP.js +0 -121
- package/dist/cjs/event-system/AbortControllerP.js +0 -28
- package/dist/cjs/event-system/AbortSignalP.js +0 -120
- package/dist/cjs/event-system/CloseEventP.js +0 -36
- package/dist/cjs/event-system/CustomEventP.js +0 -35
- package/dist/cjs/event-system/EventP.js +0 -127
- package/dist/cjs/event-system/EventTargetP.js +0 -155
- package/dist/cjs/event-system/MessageEventP.js +0 -61
- package/dist/cjs/event-system/ProgressEventP.js +0 -42
- package/dist/cjs/fetch-api/BodyImpl.js +0 -167
- package/dist/cjs/fetch-api/HeadersP.js +0 -155
- package/dist/cjs/fetch-api/RequestP.js +0 -163
- package/dist/cjs/fetch-api/ResponseP.js +0 -93
- package/dist/cjs/fetch-api/fetchP.js +0 -118
- package/dist/cjs/file-system/BlobP.js +0 -111
- package/dist/cjs/file-system/FileP.js +0 -34
- package/dist/cjs/file-system/FileReaderP.js +0 -173
- package/dist/cjs/helpers/Payload.js +0 -96
- package/dist/cjs/helpers/decode.js +0 -11
- package/dist/cjs/helpers/emitEvent.js +0 -14
- package/dist/cjs/helpers/emitProgressEvent.js +0 -19
- package/dist/cjs/helpers/encode.js +0 -11
- package/dist/cjs/helpers/handlers.js +0 -23
- package/dist/cjs/helpers/isArrayBuffer.js +0 -11
- package/dist/cjs/helpers/isBlob.js +0 -20
- package/dist/cjs/helpers/isEventTarget.js +0 -18
- package/dist/cjs/helpers/isSequence.js +0 -13
- package/dist/cjs/helpers/isURLSearchParams.js +0 -10
- package/dist/cjs/helpers/normalizeMethod.js +0 -11
- package/dist/cjs/helpers/statusTextMap.js +0 -68
- package/dist/cjs/helpers/toBase64.js +0 -28
- package/dist/cjs/mini-program/WebSocketImpl.js +0 -197
- package/dist/cjs/mini-program/XMLHttpRequestImpl.js +0 -493
- package/dist/cjs/mini-program/connectSocket.js +0 -28
- package/dist/cjs/mini-program/platform.js +0 -29
- package/dist/cjs/mini-program/request.js +0 -36
- package/dist/cjs/network/FormDataP.js +0 -160
- package/dist/cjs/network/URLSearchParamsP.js +0 -226
- package/dist/cjs/network/XMLHttpRequestEventTargetP.js +0 -64
- package/dist/cjs/network/XMLHttpRequestUploadP.js +0 -28
- package/dist/cjs/utils.js +0 -56
- package/dist/esm/encoding/TextDecoderP.js +0 -171
- package/dist/esm/encoding/TextEncoderP.js +0 -118
- package/dist/esm/event-system/AbortControllerP.js +0 -25
- package/dist/esm/event-system/AbortSignalP.js +0 -115
- package/dist/esm/event-system/CloseEventP.js +0 -34
- package/dist/esm/event-system/CustomEventP.js +0 -32
- package/dist/esm/event-system/EventP.js +0 -122
- package/dist/esm/event-system/EventTargetP.js +0 -150
- package/dist/esm/event-system/MessageEventP.js +0 -59
- package/dist/esm/event-system/ProgressEventP.js +0 -40
- package/dist/esm/fetch-api/BodyImpl.js +0 -164
- package/dist/esm/fetch-api/HeadersP.js +0 -149
- package/dist/esm/fetch-api/RequestP.js +0 -160
- package/dist/esm/fetch-api/ResponseP.js +0 -90
- package/dist/esm/fetch-api/fetchP.js +0 -114
- package/dist/esm/file-system/BlobP.js +0 -108
- package/dist/esm/file-system/FileP.js +0 -31
- package/dist/esm/file-system/FileReaderP.js +0 -170
- package/dist/esm/helpers/Payload.js +0 -94
- package/dist/esm/helpers/decode.js +0 -9
- package/dist/esm/helpers/emitEvent.js +0 -12
- package/dist/esm/helpers/emitProgressEvent.js +0 -17
- package/dist/esm/helpers/encode.js +0 -9
- package/dist/esm/helpers/handlers.js +0 -20
- package/dist/esm/helpers/isArrayBuffer.js +0 -9
- package/dist/esm/helpers/isBlob.js +0 -18
- package/dist/esm/helpers/isEventTarget.js +0 -16
- package/dist/esm/helpers/isSequence.js +0 -11
- package/dist/esm/helpers/isURLSearchParams.js +0 -8
- package/dist/esm/helpers/normalizeMethod.js +0 -9
- package/dist/esm/helpers/statusTextMap.js +0 -66
- package/dist/esm/helpers/toBase64.js +0 -26
- package/dist/esm/mini-program/WebSocketImpl.js +0 -193
- package/dist/esm/mini-program/XMLHttpRequestImpl.js +0 -489
- package/dist/esm/mini-program/connectSocket.js +0 -26
- package/dist/esm/mini-program/platform.js +0 -27
- package/dist/esm/mini-program/request.js +0 -34
- package/dist/esm/network/FormDataP.js +0 -157
- package/dist/esm/network/URLSearchParamsP.js +0 -223
- package/dist/esm/network/XMLHttpRequestEventTargetP.js +0 -61
- package/dist/esm/network/XMLHttpRequestUploadP.js +0 -25
- package/dist/esm/utils.js +0 -48
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var Payload = require('../helpers/Payload.js');
|
|
4
|
-
var BlobP = require('../file-system/BlobP.js');
|
|
5
|
-
var FileP = require('../file-system/FileP.js');
|
|
6
|
-
var FormDataP = require('../network/FormDataP.js');
|
|
7
|
-
var utils = require('../utils.js');
|
|
8
|
-
|
|
9
|
-
class BodyImpl {
|
|
10
|
-
/** @internal */
|
|
11
|
-
constructor() {
|
|
12
|
-
if (new.target === BodyImpl) {
|
|
13
|
-
throw new TypeError("Failed to construct 'Body': Illegal constructor");
|
|
14
|
-
}
|
|
15
|
-
utils.setState(this, "__Body__", new BodyState());
|
|
16
|
-
}
|
|
17
|
-
get bodyUsed() { return state(this).bodyUsed; }
|
|
18
|
-
;
|
|
19
|
-
get body() {
|
|
20
|
-
throw new TypeError(`Failed to access 'body' on '${utils.className(this)}': property not implemented.`);
|
|
21
|
-
}
|
|
22
|
-
arrayBuffer() {
|
|
23
|
-
const kind = "arrayBuffer";
|
|
24
|
-
return consumed(this, kind) || read(this, kind);
|
|
25
|
-
}
|
|
26
|
-
blob() {
|
|
27
|
-
const kind = "blob";
|
|
28
|
-
return consumed(this, kind) || read(this, kind);
|
|
29
|
-
}
|
|
30
|
-
bytes() {
|
|
31
|
-
const kind = "bytes";
|
|
32
|
-
return consumed(this, kind) || read(this, kind);
|
|
33
|
-
}
|
|
34
|
-
formData() {
|
|
35
|
-
const kind = "formData";
|
|
36
|
-
return consumed(this, kind) || read(this, kind);
|
|
37
|
-
}
|
|
38
|
-
json() {
|
|
39
|
-
const kind = "json";
|
|
40
|
-
return consumed(this, kind) || read(this, kind);
|
|
41
|
-
}
|
|
42
|
-
text() {
|
|
43
|
-
const kind = "text";
|
|
44
|
-
return consumed(this, kind) || read(this, kind);
|
|
45
|
-
}
|
|
46
|
-
/** @internal */ toString() { return "[object Body]"; }
|
|
47
|
-
/** @internal */ get [utils.SymbolP.toStringTag]() { return "Body"; }
|
|
48
|
-
/** @internal */ get __MPHTTPX__() { return { chain: ["Body"] }; }
|
|
49
|
-
}
|
|
50
|
-
/** @internal */
|
|
51
|
-
class BodyState {
|
|
52
|
-
constructor() {
|
|
53
|
-
this.bodyUsed = false;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
function state(target) {
|
|
57
|
-
return target.__Body__;
|
|
58
|
-
}
|
|
59
|
-
/** @internal */
|
|
60
|
-
function Body_init(instance, body) {
|
|
61
|
-
const b = instance;
|
|
62
|
-
if (utils.isObjectType("ReadableStream", body)) {
|
|
63
|
-
throw new TypeError(`Failed to construct '${utils.className(b)}': ReadableStream not implemented.`);
|
|
64
|
-
}
|
|
65
|
-
if (body !== null && body !== undefined) {
|
|
66
|
-
state(b).payload = new Payload.Payload(body);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
function read(body, kind) {
|
|
70
|
-
let payload = state(body).payload || new Payload.Payload();
|
|
71
|
-
if (kind === "json") {
|
|
72
|
-
return payload.text().then(r => JSON.parse(r));
|
|
73
|
-
}
|
|
74
|
-
else if (kind === "text") {
|
|
75
|
-
return payload.text();
|
|
76
|
-
}
|
|
77
|
-
else if (kind === "arrayBuffer") {
|
|
78
|
-
return payload.arrayBuffer();
|
|
79
|
-
}
|
|
80
|
-
else if (kind === "bytes") {
|
|
81
|
-
return payload.arrayBuffer().then(r => new Uint8Array(r));
|
|
82
|
-
}
|
|
83
|
-
else if (kind === "blob") {
|
|
84
|
-
return payload.promise.then(r => new BlobP.BlobP([r]));
|
|
85
|
-
}
|
|
86
|
-
else if (kind === "formData") {
|
|
87
|
-
return payload.text().then(r => createFormDataFromBinaryText(r, extractBoundary(payload.type)));
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
return payload.promise;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
function consumed(body, kind) {
|
|
94
|
-
if (!state(body).payload)
|
|
95
|
-
return;
|
|
96
|
-
if (!body.bodyUsed) {
|
|
97
|
-
state(body).bodyUsed = true;
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
return Promise.reject(new TypeError(`Failed to execute '${kind}' on '${utils.className(body)}': body stream already read`));
|
|
101
|
-
}
|
|
102
|
-
function extractBoundary(contentType) {
|
|
103
|
-
if (!contentType)
|
|
104
|
-
return;
|
|
105
|
-
if (!/multipart\/form-data/i.test(contentType))
|
|
106
|
-
return;
|
|
107
|
-
let boundaryMatch = contentType.match(/boundary\s*=\s*([^;]+)/i);
|
|
108
|
-
if (boundaryMatch && boundaryMatch[1]) {
|
|
109
|
-
let boundary = boundaryMatch[1].trim();
|
|
110
|
-
return boundary.replace(/^["']|["']$/g, "");
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
function createFormDataFromBinaryText(text, boundary) {
|
|
114
|
-
const throwParseError = () => {
|
|
115
|
-
throw new TypeError("Could not parse content as FormData.");
|
|
116
|
-
};
|
|
117
|
-
if (typeof text !== "string" || text.trim() === "") {
|
|
118
|
-
throwParseError();
|
|
119
|
-
}
|
|
120
|
-
let firstLineEnd = text.indexOf("\r\n");
|
|
121
|
-
if (firstLineEnd === -1) {
|
|
122
|
-
throwParseError();
|
|
123
|
-
}
|
|
124
|
-
let _boundary = text.substring(2, firstLineEnd).trim();
|
|
125
|
-
if (!_boundary) {
|
|
126
|
-
throwParseError();
|
|
127
|
-
}
|
|
128
|
-
if (boundary !== undefined && boundary !== _boundary) {
|
|
129
|
-
throwParseError();
|
|
130
|
-
}
|
|
131
|
-
let parts = text.split(`--${_boundary}`).filter(part => {
|
|
132
|
-
let trimmed = part.trim();
|
|
133
|
-
return trimmed !== "" && trimmed !== "--";
|
|
134
|
-
});
|
|
135
|
-
if (parts.length === 0) {
|
|
136
|
-
throwParseError();
|
|
137
|
-
}
|
|
138
|
-
let formData = new FormDataP.FormDataP();
|
|
139
|
-
parts.forEach(part => {
|
|
140
|
-
let separatorIndex = part.indexOf("\r\n\r\n");
|
|
141
|
-
if (separatorIndex === -1) {
|
|
142
|
-
throwParseError();
|
|
143
|
-
}
|
|
144
|
-
let headerRaw = part.substring(0, separatorIndex).trim();
|
|
145
|
-
let nameMatch = headerRaw.match(/name="([^"]*)"/);
|
|
146
|
-
if (!nameMatch || nameMatch.length < 2) {
|
|
147
|
-
throwParseError();
|
|
148
|
-
}
|
|
149
|
-
let fieldName = nameMatch[1];
|
|
150
|
-
let filenameMatch = headerRaw.match(/filename="([^"]*)"/);
|
|
151
|
-
let contentRaw = part.substring(separatorIndex + 4);
|
|
152
|
-
if (!filenameMatch) {
|
|
153
|
-
formData.append(fieldName, contentRaw.replace(/^[\r\n]+|[\r\n]+$/g, ""));
|
|
154
|
-
}
|
|
155
|
-
else {
|
|
156
|
-
let filename = filenameMatch[1] || "";
|
|
157
|
-
let contentTypeMatch = headerRaw.match(/Content-Type: ([^\r\n]+)/);
|
|
158
|
-
let mimeType = contentTypeMatch ? (contentTypeMatch[1] || "").trim() : "text/plain";
|
|
159
|
-
let content = contentRaw.replace(/\r\n/g, "");
|
|
160
|
-
formData.append(fieldName, new FileP.FileP([content], filename, { type: mimeType }));
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
return formData;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
exports.BodyImpl = BodyImpl;
|
|
167
|
-
exports.Body_init = Body_init;
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var isSequence = require('../helpers/isSequence.js');
|
|
4
|
-
var utils = require('../utils.js');
|
|
5
|
-
|
|
6
|
-
class HeadersP {
|
|
7
|
-
constructor(init) {
|
|
8
|
-
utils.setState(this, "__Headers__", new HeadersState());
|
|
9
|
-
if (init !== undefined) {
|
|
10
|
-
if (isHeaders(init)) {
|
|
11
|
-
init.forEach((value, name) => { Headers_append(this, name, value, ""); }, this);
|
|
12
|
-
}
|
|
13
|
-
else if (isSequence.isSequence(init)) {
|
|
14
|
-
let _init = Array.isArray(init) ? init : Array.from(init);
|
|
15
|
-
for (let i = 0; i < _init.length; ++i) {
|
|
16
|
-
let item = _init[i];
|
|
17
|
-
if (isSequence.isSequence(item)) {
|
|
18
|
-
let pair = Array.isArray(item) ? item : Array.from(item);
|
|
19
|
-
if (pair.length === 2) {
|
|
20
|
-
Headers_append(this, pair[0], pair[1]);
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
throw new TypeError("Failed to construct 'Headers': Invalid value");
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
throw new TypeError("Failed to construct 'Headers': The provided value cannot be converted to a sequence.");
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
if (init && typeof init === "object") {
|
|
33
|
-
Object.getOwnPropertyNames(init).forEach(name => { Headers_append(this, name, init[name]); }, this);
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
throw new TypeError("Failed to construct 'Headers': The provided value is not of type '(record<ByteString, ByteString> or sequence<sequence<ByteString>>)'.");
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
append(name, value) {
|
|
42
|
-
checkArgsFn(arguments.length, 2, "append");
|
|
43
|
-
Headers_append(this, name, value, "append");
|
|
44
|
-
}
|
|
45
|
-
delete(name) {
|
|
46
|
-
checkArgsFn(arguments.length, 1, "delete");
|
|
47
|
-
delete state(this).dict[normalizeName(name, throwsFn("delete"))];
|
|
48
|
-
}
|
|
49
|
-
get(name) {
|
|
50
|
-
var _a;
|
|
51
|
-
checkArgsFn(arguments.length, 1, "get");
|
|
52
|
-
return (_a = state(this).dict[normalizeName(name, throwsFn("get"))]) !== null && _a !== void 0 ? _a : null;
|
|
53
|
-
}
|
|
54
|
-
getSetCookie() {
|
|
55
|
-
let value = this.get("Set-Cookie");
|
|
56
|
-
return value ? value.split(", ") : [];
|
|
57
|
-
}
|
|
58
|
-
has(name) {
|
|
59
|
-
checkArgsFn(arguments.length, 1, "has");
|
|
60
|
-
return state(this).dict.hasOwnProperty(normalizeName(name, throwsFn("has")));
|
|
61
|
-
}
|
|
62
|
-
set(name, value) {
|
|
63
|
-
checkArgsFn(arguments.length, 2, "set");
|
|
64
|
-
state(this).dict[normalizeName(name, throwsFn("set"))] = normalizeValue(value);
|
|
65
|
-
}
|
|
66
|
-
forEach(callbackfn, thisArg) {
|
|
67
|
-
checkArgsFn(arguments.length, 1, "forEach");
|
|
68
|
-
if (typeof callbackfn !== "function") {
|
|
69
|
-
throw new TypeError("Failed to execute 'forEach' on 'Headers': parameter 1 is not of type 'Function'.");
|
|
70
|
-
}
|
|
71
|
-
let dict = state(this).dict;
|
|
72
|
-
let names = Object.getOwnPropertyNames(dict);
|
|
73
|
-
for (let i = 0; i < names.length; ++i) {
|
|
74
|
-
let name = names[i];
|
|
75
|
-
callbackfn.call(thisArg, dict[name], name, this);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
entries() {
|
|
79
|
-
let array = [];
|
|
80
|
-
this.forEach((value, name) => { array.push([name, value]); });
|
|
81
|
-
return array.values();
|
|
82
|
-
}
|
|
83
|
-
keys() {
|
|
84
|
-
let array = [];
|
|
85
|
-
this.forEach((value, name) => { array.push([name, value]); });
|
|
86
|
-
return array.map(x => x[0]).values();
|
|
87
|
-
}
|
|
88
|
-
values() {
|
|
89
|
-
let array = [];
|
|
90
|
-
this.forEach((value, name) => { array.push([name, value]); });
|
|
91
|
-
return array.map(x => x[1]).values();
|
|
92
|
-
}
|
|
93
|
-
// @ts-ignore
|
|
94
|
-
/** @internal */ [utils.SymbolP.iterator]() {
|
|
95
|
-
return this.entries();
|
|
96
|
-
}
|
|
97
|
-
/** @internal */ toString() { return "[object Headers]"; }
|
|
98
|
-
/** @internal */ get [utils.SymbolP.toStringTag]() { return "Headers"; }
|
|
99
|
-
/** @internal */ get __MPHTTPX__() { return { chain: ["Headers"] }; }
|
|
100
|
-
}
|
|
101
|
-
/** @internal */
|
|
102
|
-
class HeadersState {
|
|
103
|
-
constructor() {
|
|
104
|
-
this.dict = {};
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
function state(target) {
|
|
108
|
-
return target.__Headers__;
|
|
109
|
-
}
|
|
110
|
-
/** @internal */
|
|
111
|
-
function isHeaders(value) {
|
|
112
|
-
return utils.isPolyfillType("Headers", value) || isExternalHeaders(value);
|
|
113
|
-
}
|
|
114
|
-
function isExternalHeaders(value) {
|
|
115
|
-
let expect = "[object Headers]";
|
|
116
|
-
return (Object.prototype.toString.call(value) === expect || String(value) === expect)
|
|
117
|
-
&& "forEach" in value
|
|
118
|
-
&& typeof value.forEach === "function";
|
|
119
|
-
}
|
|
120
|
-
function Headers_append(headers, name, value, kind = "constructor") {
|
|
121
|
-
let _name = normalizeName(name, kind ? throwsFn(kind) : undefined);
|
|
122
|
-
let _value = normalizeValue(value);
|
|
123
|
-
let dict = state(headers).dict;
|
|
124
|
-
let oldValue = dict[_name];
|
|
125
|
-
dict[_name] = oldValue !== undefined ? `${oldValue}, ${_value}` : _value;
|
|
126
|
-
}
|
|
127
|
-
function throwsFn(kind) {
|
|
128
|
-
return () => {
|
|
129
|
-
throw new TypeError(`Failed to ${(kind && kind !== "constructor") ? ("execute '" + kind + "' on") : "construct"} 'Headers': Invalid name`);
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
function checkArgsFn(actual, expect, funcName) {
|
|
133
|
-
utils.checkArgsLength(actual, expect, "Headers", funcName);
|
|
134
|
-
}
|
|
135
|
-
/** @internal */
|
|
136
|
-
function normalizeName(name, throwError) {
|
|
137
|
-
if (typeof name !== "string") {
|
|
138
|
-
name = "" + name;
|
|
139
|
-
}
|
|
140
|
-
if (throwError && (/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(name) || name === "")) {
|
|
141
|
-
throwError();
|
|
142
|
-
}
|
|
143
|
-
return name.toLowerCase();
|
|
144
|
-
}
|
|
145
|
-
/** @internal */
|
|
146
|
-
function normalizeValue(value) {
|
|
147
|
-
return typeof value === "string" ? value : ("" + value);
|
|
148
|
-
}
|
|
149
|
-
const HeadersE = (typeof Headers !== "undefined" && Headers) || HeadersP;
|
|
150
|
-
|
|
151
|
-
exports.Headers = HeadersE;
|
|
152
|
-
exports.HeadersP = HeadersP;
|
|
153
|
-
exports.isHeaders = isHeaders;
|
|
154
|
-
exports.normalizeName = normalizeName;
|
|
155
|
-
exports.normalizeValue = normalizeValue;
|
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var HeadersP = require('./HeadersP.js');
|
|
4
|
-
var BodyImpl = require('./BodyImpl.js');
|
|
5
|
-
var Payload = require('../helpers/Payload.js');
|
|
6
|
-
var isEventTarget = require('../helpers/isEventTarget.js');
|
|
7
|
-
var normalizeMethod = require('../helpers/normalizeMethod.js');
|
|
8
|
-
var AbortSignalP = require('../event-system/AbortSignalP.js');
|
|
9
|
-
var utils = require('../utils.js');
|
|
10
|
-
|
|
11
|
-
class RequestP extends BodyImpl.BodyImpl {
|
|
12
|
-
constructor(input, init) {
|
|
13
|
-
utils.checkArgsLength(arguments.length, 1, "Request");
|
|
14
|
-
super();
|
|
15
|
-
utils.setState(this, "__Request__", new RequestState());
|
|
16
|
-
const s = state(this);
|
|
17
|
-
let _init = init !== null && init !== void 0 ? init : {};
|
|
18
|
-
if (typeof _init !== "object") {
|
|
19
|
-
throw new TypeError("Failed to construct 'Request': The provided value is not of type 'RequestInit'.");
|
|
20
|
-
}
|
|
21
|
-
let body = _init.body;
|
|
22
|
-
let bodyInited = false;
|
|
23
|
-
if (isRequest(input)) {
|
|
24
|
-
if (input.bodyUsed) {
|
|
25
|
-
throw new TypeError("Failed to construct 'Request': Cannot construct a Request with a Request object that has already been used.");
|
|
26
|
-
}
|
|
27
|
-
s.cache = input.cache;
|
|
28
|
-
s.credentials = input.credentials;
|
|
29
|
-
if (_init.headers === undefined) {
|
|
30
|
-
state(this).headers = new HeadersP.HeadersP(input.headers);
|
|
31
|
-
}
|
|
32
|
-
s.method = input.method;
|
|
33
|
-
s.mode = input.mode;
|
|
34
|
-
s.signal = input.signal;
|
|
35
|
-
s.url = input.url;
|
|
36
|
-
if (!body) {
|
|
37
|
-
this.__Body__.payload = createPhonyPayload(input.arrayBuffer());
|
|
38
|
-
bodyInited = true;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
s.url = "" + input;
|
|
43
|
-
}
|
|
44
|
-
if (_init.cache) {
|
|
45
|
-
s.cache = _init.cache;
|
|
46
|
-
}
|
|
47
|
-
if (_init.credentials) {
|
|
48
|
-
s.credentials = _init.credentials;
|
|
49
|
-
}
|
|
50
|
-
if (_init.headers !== undefined) {
|
|
51
|
-
state(this).headers = new HeadersP.HeadersP(_init.headers);
|
|
52
|
-
}
|
|
53
|
-
if (_init.method) {
|
|
54
|
-
s.method = normalizeMethod.normalizeMethod(_init.method);
|
|
55
|
-
}
|
|
56
|
-
if (_init.mode) {
|
|
57
|
-
s.mode = _init.mode;
|
|
58
|
-
}
|
|
59
|
-
if (_init.signal !== null && _init.signal !== undefined) {
|
|
60
|
-
if (isEventTarget.isEventTarget(_init.signal))
|
|
61
|
-
s.signal = _init.signal;
|
|
62
|
-
else
|
|
63
|
-
throw new TypeError("Failed to construct 'Request': Failed to read the 'signal' property from 'RequestInit': Failed to convert value to 'AbortSignal'.");
|
|
64
|
-
}
|
|
65
|
-
if (this.method === "GET" || this.method === "HEAD") {
|
|
66
|
-
if (body !== null && body !== undefined) {
|
|
67
|
-
throw new TypeError("Failed to construct 'Request': Request with GET/HEAD method cannot have body.");
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
BodyImpl.Body_init(this, bodyInited ? null : body);
|
|
71
|
-
let payload = this.__Body__.payload;
|
|
72
|
-
if (!this.headers.has("Content-Type") && payload && payload.type) {
|
|
73
|
-
this.headers.set("Content-Type", payload.type);
|
|
74
|
-
}
|
|
75
|
-
if (this.method === "GET" || this.method === "HEAD") {
|
|
76
|
-
if (this.cache === "no-cache" || this.cache === "no-store") {
|
|
77
|
-
clearCache(this);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
get cache() { return state(this).cache; }
|
|
82
|
-
get credentials() { return state(this).credentials; }
|
|
83
|
-
get destination() { return ""; }
|
|
84
|
-
get headers() {
|
|
85
|
-
if (!state(this).headers) {
|
|
86
|
-
state(this).headers = new HeadersP.HeadersP();
|
|
87
|
-
}
|
|
88
|
-
return state(this).headers;
|
|
89
|
-
}
|
|
90
|
-
get integrity() { return ""; }
|
|
91
|
-
get keepalive() { return false; }
|
|
92
|
-
get method() { return state(this).method; }
|
|
93
|
-
get mode() { return state(this).mode; }
|
|
94
|
-
get redirect() { return "follow"; }
|
|
95
|
-
get referrer() { return state(this).referrer; }
|
|
96
|
-
get referrerPolicy() { return ""; }
|
|
97
|
-
get signal() {
|
|
98
|
-
if (!state(this).signal) {
|
|
99
|
-
state(this).signal = AbortSignalP.createAbortSignal();
|
|
100
|
-
}
|
|
101
|
-
return state(this).signal;
|
|
102
|
-
}
|
|
103
|
-
get url() { return state(this).url; }
|
|
104
|
-
clone() {
|
|
105
|
-
if (!this.bodyUsed) {
|
|
106
|
-
let req = new RequestP(this);
|
|
107
|
-
this.__Body__.bodyUsed = false;
|
|
108
|
-
return req;
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
throw new TypeError("Failed to execute 'clone' on 'Request': Request body is already used");
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
/** @internal */ toString() { return "[object Request]"; }
|
|
115
|
-
/** @internal */ get [utils.SymbolP.toStringTag]() { return "Request"; }
|
|
116
|
-
/** @internal */ get __MPHTTPX__() { return { chain: ["Request", "Body"] }; }
|
|
117
|
-
}
|
|
118
|
-
/** @internal */
|
|
119
|
-
class RequestState {
|
|
120
|
-
constructor() {
|
|
121
|
-
this.cache = "default";
|
|
122
|
-
this.credentials = "same-origin";
|
|
123
|
-
this.method = "GET";
|
|
124
|
-
this.mode = "cors";
|
|
125
|
-
this.referrer = "about:client";
|
|
126
|
-
this.url = "";
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
function state(target) {
|
|
130
|
-
return target.__Request__;
|
|
131
|
-
}
|
|
132
|
-
function isRequest(value) {
|
|
133
|
-
return utils.isPolyfillType("Request", value) || isExternalRequest(value);
|
|
134
|
-
}
|
|
135
|
-
function isExternalRequest(value) {
|
|
136
|
-
let expect = "[object Request]";
|
|
137
|
-
return (Object.prototype.toString.call(value) === expect || String(value) === expect)
|
|
138
|
-
&& "arrayBuffer" in value
|
|
139
|
-
&& typeof value.arrayBuffer === "function";
|
|
140
|
-
}
|
|
141
|
-
function createPhonyPayload(promise) {
|
|
142
|
-
let payload = Object.create(Payload.Payload.prototype);
|
|
143
|
-
payload.promise = promise;
|
|
144
|
-
payload.type = "";
|
|
145
|
-
return payload;
|
|
146
|
-
}
|
|
147
|
-
function clearCache(req) {
|
|
148
|
-
// Search for a '_' parameter in the query string
|
|
149
|
-
let reParamSearch = /([?&])_=[^&]*/;
|
|
150
|
-
if (reParamSearch.test(req.url)) {
|
|
151
|
-
// If it already exists then set the value with the current time
|
|
152
|
-
state(req).url = req.url.replace(reParamSearch, "$1_=" + (new Date()).getTime());
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
// Otherwise add a new '_' parameter to the end with the current time
|
|
156
|
-
let reQueryString = /\?/;
|
|
157
|
-
state(req).url += (reQueryString.test(req.url) ? "&" : "?") + "_=" + (new Date()).getTime();
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
const RequestE = (typeof Request !== "undefined" && Request) || RequestP;
|
|
161
|
-
|
|
162
|
-
exports.Request = RequestE;
|
|
163
|
-
exports.RequestP = RequestP;
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var HeadersP = require('./HeadersP.js');
|
|
4
|
-
var BodyImpl = require('./BodyImpl.js');
|
|
5
|
-
var utils = require('../utils.js');
|
|
6
|
-
|
|
7
|
-
class ResponseP extends BodyImpl.BodyImpl {
|
|
8
|
-
static json(data, init) {
|
|
9
|
-
utils.checkArgsLength(arguments.length, 1, "Response", "json");
|
|
10
|
-
let response = new ResponseP(typeof data === "string" ? data : JSON.stringify(data), init);
|
|
11
|
-
response.headers.set("Content-Type", "application/json");
|
|
12
|
-
return response;
|
|
13
|
-
}
|
|
14
|
-
static error() {
|
|
15
|
-
let response = new ResponseP(null, { status: 200, statusText: "" });
|
|
16
|
-
state(response).status = 0;
|
|
17
|
-
state(response).type = "error";
|
|
18
|
-
return response;
|
|
19
|
-
}
|
|
20
|
-
static redirect(url, status = 302) {
|
|
21
|
-
utils.checkArgsLength(arguments.length, 1, "Response", "redirect");
|
|
22
|
-
if ([301, 302, 303, 307, 308].indexOf(status) === -1) {
|
|
23
|
-
throw new RangeError("Failed to execute 'redirect' on 'Response': Invalid status code");
|
|
24
|
-
}
|
|
25
|
-
return new ResponseP(null, { status, headers: { location: "" + url } });
|
|
26
|
-
}
|
|
27
|
-
constructor(body, init) {
|
|
28
|
-
super();
|
|
29
|
-
utils.setState(this, "__Response__", new ResponseState());
|
|
30
|
-
const s = state(this);
|
|
31
|
-
let _init = init !== null && init !== void 0 ? init : {};
|
|
32
|
-
if (typeof _init !== "object") {
|
|
33
|
-
throw new TypeError("Failed to construct 'Response': The provided value is not of type 'ResponseInit'.");
|
|
34
|
-
}
|
|
35
|
-
if (_init.headers !== undefined) {
|
|
36
|
-
state(this).headers = new HeadersP.HeadersP(_init.headers);
|
|
37
|
-
}
|
|
38
|
-
let status = _init.status === undefined ? 200 : _init.status;
|
|
39
|
-
if (status < 200 || status > 500)
|
|
40
|
-
throw new RangeError(`Failed to construct 'Response': The status provided (${+status}) is outside the range [200, 599].`);
|
|
41
|
-
s.status = status;
|
|
42
|
-
s.statusText = _init.statusText === undefined ? "" : "" + _init.statusText;
|
|
43
|
-
BodyImpl.Body_init(this, body);
|
|
44
|
-
let payload = this.__Body__.payload;
|
|
45
|
-
if (!this.headers.has("Content-Type") && payload && payload.type) {
|
|
46
|
-
this.headers.set("Content-Type", payload.type);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
get headers() {
|
|
50
|
-
if (!state(this).headers) {
|
|
51
|
-
state(this).headers = new HeadersP.HeadersP();
|
|
52
|
-
}
|
|
53
|
-
return state(this).headers;
|
|
54
|
-
}
|
|
55
|
-
get ok() { return this.status >= 200 && this.status < 300; }
|
|
56
|
-
get redirected() { return false; }
|
|
57
|
-
get status() { return state(this).status; }
|
|
58
|
-
get statusText() { return state(this).statusText; }
|
|
59
|
-
get type() { return state(this).type; }
|
|
60
|
-
get url() { return state(this).url; }
|
|
61
|
-
clone() {
|
|
62
|
-
if (this.bodyUsed) {
|
|
63
|
-
throw new TypeError("Failed to execute 'clone' on 'Response': Response body is already used");
|
|
64
|
-
}
|
|
65
|
-
let response = new ResponseP(null, {
|
|
66
|
-
headers: new HeadersP.HeadersP(this.headers),
|
|
67
|
-
status: this.status,
|
|
68
|
-
statusText: this.statusText,
|
|
69
|
-
});
|
|
70
|
-
state(response).url = this.url;
|
|
71
|
-
response.__Body__.payload = this.__Body__.payload;
|
|
72
|
-
return response;
|
|
73
|
-
}
|
|
74
|
-
/** @internal */ toString() { return "[object Response]"; }
|
|
75
|
-
/** @internal */ get [utils.SymbolP.toStringTag]() { return "Response"; }
|
|
76
|
-
/** @internal */ get __MPHTTPX__() { return { chain: ["Response", "Body"] }; }
|
|
77
|
-
}
|
|
78
|
-
/** @internal */
|
|
79
|
-
class ResponseState {
|
|
80
|
-
constructor() {
|
|
81
|
-
this.status = 200;
|
|
82
|
-
this.statusText = "";
|
|
83
|
-
this.type = "default";
|
|
84
|
-
this.url = "";
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
function state(target) {
|
|
88
|
-
return target.__Response__;
|
|
89
|
-
}
|
|
90
|
-
const ResponseE = (typeof Response !== "undefined" && Response) || ResponseP;
|
|
91
|
-
|
|
92
|
-
exports.Response = ResponseE;
|
|
93
|
-
exports.ResponseP = ResponseP;
|