mphttpx 2.3.0 → 2.4.0
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 +69 -953
- 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 -1
- package/dist/mphttpx.esm.js +6595 -0
- package/dist/mphttpx.esm.min.js +1 -1
- package/package.json +27 -9
- package/README.zh-CN.md +0 -983
- 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 -125
- 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 -121
- 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,160 +0,0 @@
|
|
|
1
|
-
import { HeadersP } from './HeadersP.js';
|
|
2
|
-
import { BodyImpl, Body_init } from './BodyImpl.js';
|
|
3
|
-
import { Payload } from '../helpers/Payload.js';
|
|
4
|
-
import { isEventTarget } from '../helpers/isEventTarget.js';
|
|
5
|
-
import { normalizeMethod } from '../helpers/normalizeMethod.js';
|
|
6
|
-
import { createAbortSignal } from '../event-system/AbortSignalP.js';
|
|
7
|
-
import { checkArgsLength, setState, SymbolP, isPolyfillType } from '../utils.js';
|
|
8
|
-
|
|
9
|
-
class RequestP extends BodyImpl {
|
|
10
|
-
constructor(input, init) {
|
|
11
|
-
checkArgsLength(arguments.length, 1, "Request");
|
|
12
|
-
super();
|
|
13
|
-
setState(this, "__Request__", new RequestState());
|
|
14
|
-
const s = state(this);
|
|
15
|
-
let _init = init !== null && init !== void 0 ? init : {};
|
|
16
|
-
if (typeof _init !== "object") {
|
|
17
|
-
throw new TypeError("Failed to construct 'Request': The provided value is not of type 'RequestInit'.");
|
|
18
|
-
}
|
|
19
|
-
let body = _init.body;
|
|
20
|
-
let bodyInited = false;
|
|
21
|
-
if (isRequest(input)) {
|
|
22
|
-
if (input.bodyUsed) {
|
|
23
|
-
throw new TypeError("Failed to construct 'Request': Cannot construct a Request with a Request object that has already been used.");
|
|
24
|
-
}
|
|
25
|
-
s.cache = input.cache;
|
|
26
|
-
s.credentials = input.credentials;
|
|
27
|
-
if (_init.headers === undefined) {
|
|
28
|
-
state(this).headers = new HeadersP(input.headers);
|
|
29
|
-
}
|
|
30
|
-
s.method = input.method;
|
|
31
|
-
s.mode = input.mode;
|
|
32
|
-
s.signal = input.signal;
|
|
33
|
-
s.url = input.url;
|
|
34
|
-
if (!body) {
|
|
35
|
-
this.__Body__.payload = createPhonyPayload(input.arrayBuffer());
|
|
36
|
-
bodyInited = true;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
s.url = "" + input;
|
|
41
|
-
}
|
|
42
|
-
if (_init.cache) {
|
|
43
|
-
s.cache = _init.cache;
|
|
44
|
-
}
|
|
45
|
-
if (_init.credentials) {
|
|
46
|
-
s.credentials = _init.credentials;
|
|
47
|
-
}
|
|
48
|
-
if (_init.headers !== undefined) {
|
|
49
|
-
state(this).headers = new HeadersP(_init.headers);
|
|
50
|
-
}
|
|
51
|
-
if (_init.method) {
|
|
52
|
-
s.method = normalizeMethod(_init.method);
|
|
53
|
-
}
|
|
54
|
-
if (_init.mode) {
|
|
55
|
-
s.mode = _init.mode;
|
|
56
|
-
}
|
|
57
|
-
if (_init.signal !== null && _init.signal !== undefined) {
|
|
58
|
-
if (isEventTarget(_init.signal))
|
|
59
|
-
s.signal = _init.signal;
|
|
60
|
-
else
|
|
61
|
-
throw new TypeError("Failed to construct 'Request': Failed to read the 'signal' property from 'RequestInit': Failed to convert value to 'AbortSignal'.");
|
|
62
|
-
}
|
|
63
|
-
if (this.method === "GET" || this.method === "HEAD") {
|
|
64
|
-
if (body !== null && body !== undefined) {
|
|
65
|
-
throw new TypeError("Failed to construct 'Request': Request with GET/HEAD method cannot have body.");
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
Body_init(this, bodyInited ? null : body);
|
|
69
|
-
let payload = this.__Body__.payload;
|
|
70
|
-
if (!this.headers.has("Content-Type") && payload && payload.type) {
|
|
71
|
-
this.headers.set("Content-Type", payload.type);
|
|
72
|
-
}
|
|
73
|
-
if (this.method === "GET" || this.method === "HEAD") {
|
|
74
|
-
if (this.cache === "no-cache" || this.cache === "no-store") {
|
|
75
|
-
clearCache(this);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
get cache() { return state(this).cache; }
|
|
80
|
-
get credentials() { return state(this).credentials; }
|
|
81
|
-
get destination() { return ""; }
|
|
82
|
-
get headers() {
|
|
83
|
-
if (!state(this).headers) {
|
|
84
|
-
state(this).headers = new HeadersP();
|
|
85
|
-
}
|
|
86
|
-
return state(this).headers;
|
|
87
|
-
}
|
|
88
|
-
get integrity() { return ""; }
|
|
89
|
-
get keepalive() { return false; }
|
|
90
|
-
get method() { return state(this).method; }
|
|
91
|
-
get mode() { return state(this).mode; }
|
|
92
|
-
get redirect() { return "follow"; }
|
|
93
|
-
get referrer() { return state(this).referrer; }
|
|
94
|
-
get referrerPolicy() { return ""; }
|
|
95
|
-
get signal() {
|
|
96
|
-
if (!state(this).signal) {
|
|
97
|
-
state(this).signal = createAbortSignal();
|
|
98
|
-
}
|
|
99
|
-
return state(this).signal;
|
|
100
|
-
}
|
|
101
|
-
get url() { return state(this).url; }
|
|
102
|
-
clone() {
|
|
103
|
-
if (!this.bodyUsed) {
|
|
104
|
-
let req = new RequestP(this);
|
|
105
|
-
this.__Body__.bodyUsed = false;
|
|
106
|
-
return req;
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
throw new TypeError("Failed to execute 'clone' on 'Request': Request body is already used");
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
/** @internal */ toString() { return "[object Request]"; }
|
|
113
|
-
/** @internal */ get [SymbolP.toStringTag]() { return "Request"; }
|
|
114
|
-
/** @internal */ get __MPHTTPX__() { return { chain: ["Request", "Body"] }; }
|
|
115
|
-
}
|
|
116
|
-
/** @internal */
|
|
117
|
-
class RequestState {
|
|
118
|
-
constructor() {
|
|
119
|
-
this.cache = "default";
|
|
120
|
-
this.credentials = "same-origin";
|
|
121
|
-
this.method = "GET";
|
|
122
|
-
this.mode = "cors";
|
|
123
|
-
this.referrer = "about:client";
|
|
124
|
-
this.url = "";
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
function state(target) {
|
|
128
|
-
return target.__Request__;
|
|
129
|
-
}
|
|
130
|
-
function isRequest(value) {
|
|
131
|
-
return isPolyfillType("Request", value) || isExternalRequest(value);
|
|
132
|
-
}
|
|
133
|
-
function isExternalRequest(value) {
|
|
134
|
-
let expect = "[object Request]";
|
|
135
|
-
return (Object.prototype.toString.call(value) === expect || String(value) === expect)
|
|
136
|
-
&& "arrayBuffer" in value
|
|
137
|
-
&& typeof value.arrayBuffer === "function";
|
|
138
|
-
}
|
|
139
|
-
function createPhonyPayload(promise) {
|
|
140
|
-
let payload = Object.create(Payload.prototype);
|
|
141
|
-
payload.promise = promise;
|
|
142
|
-
payload.type = "";
|
|
143
|
-
return payload;
|
|
144
|
-
}
|
|
145
|
-
function clearCache(req) {
|
|
146
|
-
// Search for a '_' parameter in the query string
|
|
147
|
-
let reParamSearch = /([?&])_=[^&]*/;
|
|
148
|
-
if (reParamSearch.test(req.url)) {
|
|
149
|
-
// If it already exists then set the value with the current time
|
|
150
|
-
state(req).url = req.url.replace(reParamSearch, "$1_=" + (new Date()).getTime());
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
// Otherwise add a new '_' parameter to the end with the current time
|
|
154
|
-
let reQueryString = /\?/;
|
|
155
|
-
state(req).url += (reQueryString.test(req.url) ? "&" : "?") + "_=" + (new Date()).getTime();
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
const RequestE = (typeof Request !== "undefined" && Request) || RequestP;
|
|
159
|
-
|
|
160
|
-
export { RequestE as Request, RequestP };
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { HeadersP } from './HeadersP.js';
|
|
2
|
-
import { BodyImpl, Body_init } from './BodyImpl.js';
|
|
3
|
-
import { checkArgsLength, setState, SymbolP } from '../utils.js';
|
|
4
|
-
|
|
5
|
-
class ResponseP extends BodyImpl {
|
|
6
|
-
static json(data, init) {
|
|
7
|
-
checkArgsLength(arguments.length, 1, "Response", "json");
|
|
8
|
-
let response = new ResponseP(typeof data === "string" ? data : JSON.stringify(data), init);
|
|
9
|
-
response.headers.set("Content-Type", "application/json");
|
|
10
|
-
return response;
|
|
11
|
-
}
|
|
12
|
-
static error() {
|
|
13
|
-
let response = new ResponseP(null, { status: 200, statusText: "" });
|
|
14
|
-
state(response).status = 0;
|
|
15
|
-
state(response).type = "error";
|
|
16
|
-
return response;
|
|
17
|
-
}
|
|
18
|
-
static redirect(url, status = 302) {
|
|
19
|
-
checkArgsLength(arguments.length, 1, "Response", "redirect");
|
|
20
|
-
if ([301, 302, 303, 307, 308].indexOf(status) === -1) {
|
|
21
|
-
throw new RangeError("Failed to execute 'redirect' on 'Response': Invalid status code");
|
|
22
|
-
}
|
|
23
|
-
return new ResponseP(null, { status, headers: { location: "" + url } });
|
|
24
|
-
}
|
|
25
|
-
constructor(body, init) {
|
|
26
|
-
super();
|
|
27
|
-
setState(this, "__Response__", new ResponseState());
|
|
28
|
-
const s = state(this);
|
|
29
|
-
let _init = init !== null && init !== void 0 ? init : {};
|
|
30
|
-
if (typeof _init !== "object") {
|
|
31
|
-
throw new TypeError("Failed to construct 'Response': The provided value is not of type 'ResponseInit'.");
|
|
32
|
-
}
|
|
33
|
-
if (_init.headers !== undefined) {
|
|
34
|
-
state(this).headers = new HeadersP(_init.headers);
|
|
35
|
-
}
|
|
36
|
-
let status = _init.status === undefined ? 200 : _init.status;
|
|
37
|
-
if (status < 200 || status > 599)
|
|
38
|
-
throw new RangeError(`Failed to construct 'Response': The status provided (${+status}) is outside the range [200, 599].`);
|
|
39
|
-
s.status = status;
|
|
40
|
-
s.statusText = _init.statusText === undefined ? "" : "" + _init.statusText;
|
|
41
|
-
Body_init(this, body);
|
|
42
|
-
let payload = this.__Body__.payload;
|
|
43
|
-
if (!this.headers.has("Content-Type") && payload && payload.type) {
|
|
44
|
-
this.headers.set("Content-Type", payload.type);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
get headers() {
|
|
48
|
-
if (!state(this).headers) {
|
|
49
|
-
state(this).headers = new HeadersP();
|
|
50
|
-
}
|
|
51
|
-
return state(this).headers;
|
|
52
|
-
}
|
|
53
|
-
get ok() { return this.status >= 200 && this.status < 300; }
|
|
54
|
-
get redirected() { return false; }
|
|
55
|
-
get status() { return state(this).status; }
|
|
56
|
-
get statusText() { return state(this).statusText; }
|
|
57
|
-
get type() { return state(this).type; }
|
|
58
|
-
get url() { return state(this).url; }
|
|
59
|
-
clone() {
|
|
60
|
-
if (this.bodyUsed) {
|
|
61
|
-
throw new TypeError("Failed to execute 'clone' on 'Response': Response body is already used");
|
|
62
|
-
}
|
|
63
|
-
let response = new ResponseP(null, {
|
|
64
|
-
headers: new HeadersP(this.headers),
|
|
65
|
-
status: this.status,
|
|
66
|
-
statusText: this.statusText,
|
|
67
|
-
});
|
|
68
|
-
state(response).url = this.url;
|
|
69
|
-
response.__Body__.payload = this.__Body__.payload;
|
|
70
|
-
return response;
|
|
71
|
-
}
|
|
72
|
-
/** @internal */ toString() { return "[object Response]"; }
|
|
73
|
-
/** @internal */ get [SymbolP.toStringTag]() { return "Response"; }
|
|
74
|
-
/** @internal */ get __MPHTTPX__() { return { chain: ["Response", "Body"] }; }
|
|
75
|
-
}
|
|
76
|
-
/** @internal */
|
|
77
|
-
class ResponseState {
|
|
78
|
-
constructor() {
|
|
79
|
-
this.status = 200;
|
|
80
|
-
this.statusText = "";
|
|
81
|
-
this.type = "default";
|
|
82
|
-
this.url = "";
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
function state(target) {
|
|
86
|
-
return target.__Response__;
|
|
87
|
-
}
|
|
88
|
-
const ResponseE = (typeof Response !== "undefined" && Response) || ResponseP;
|
|
89
|
-
|
|
90
|
-
export { ResponseE as Response, ResponseP };
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { RequestP } from './RequestP.js';
|
|
2
|
-
import { ResponseP } from './ResponseP.js';
|
|
3
|
-
import { checkArgsLength, DOMExceptionP } from '../utils.js';
|
|
4
|
-
import { XMLHttpRequest as XMLHttpRequestE } from '../mini-program/XMLHttpRequestImpl.js';
|
|
5
|
-
import { isHeaders, normalizeName, normalizeValue, HeadersP } from './HeadersP.js';
|
|
6
|
-
|
|
7
|
-
const mp = { XMLHttpRequest: XMLHttpRequestE };
|
|
8
|
-
function setXMLHttpRequest(XHR) { mp.XMLHttpRequest = XHR; }
|
|
9
|
-
function fetchP(input, init) {
|
|
10
|
-
checkArgsLength(arguments.length, 1, "Window", "fetch");
|
|
11
|
-
if (new.target === fetchP) {
|
|
12
|
-
throw new TypeError("fetch is not a constructor");
|
|
13
|
-
}
|
|
14
|
-
return new Promise((resolve, reject) => {
|
|
15
|
-
const request = new RequestP(input, init);
|
|
16
|
-
const signal = request.__Request__.signal;
|
|
17
|
-
if (signal && signal.aborted) {
|
|
18
|
-
return reject(signal.reason);
|
|
19
|
-
}
|
|
20
|
-
let xhr = new mp.XMLHttpRequest();
|
|
21
|
-
let aborted = false;
|
|
22
|
-
let payload = request.__Body__.payload;
|
|
23
|
-
xhr.onload = () => {
|
|
24
|
-
let options = {
|
|
25
|
-
headers: parseHeaders(xhr.getAllResponseHeaders() || ""),
|
|
26
|
-
status: xhr.status,
|
|
27
|
-
statusText: xhr.statusText,
|
|
28
|
-
};
|
|
29
|
-
// This check if specifically for when a user fetches a file locally from the file system
|
|
30
|
-
// Only if the status is out of a normal range
|
|
31
|
-
if (request.url.indexOf("file://") === 0 && (xhr.status < 200 || xhr.status > 599)) {
|
|
32
|
-
options.status = 200;
|
|
33
|
-
}
|
|
34
|
-
setTimeout(() => {
|
|
35
|
-
const response = new ResponseP("response" in xhr ? xhr.response : xhr.responseText, options);
|
|
36
|
-
response.__Response__.url = "responseURL" in xhr ? xhr.responseURL : (options.headers.get("X-Request-URL") || "");
|
|
37
|
-
const _payload = response.__Body__.payload;
|
|
38
|
-
const _contentType = response.headers.get("Content-Type") || "";
|
|
39
|
-
if (_payload && _contentType) {
|
|
40
|
-
_payload.type = _contentType;
|
|
41
|
-
}
|
|
42
|
-
resolve(response);
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
|
-
xhr.onerror = () => { setTimeout(() => { reject(new TypeError("Failed to fetch")); }); };
|
|
46
|
-
xhr.ontimeout = () => { setTimeout(() => { reject(new DOMExceptionP("request:fail timeout", "TimeoutError")); }); };
|
|
47
|
-
xhr.onabort = () => { setTimeout(() => { reject(new DOMExceptionP("The user aborted a request.", "AbortError")); }); };
|
|
48
|
-
xhr.open(request.method, request.url, true);
|
|
49
|
-
if (request.credentials === "include") {
|
|
50
|
-
xhr.withCredentials = true;
|
|
51
|
-
}
|
|
52
|
-
else if (request.credentials === "omit") {
|
|
53
|
-
xhr.withCredentials = false;
|
|
54
|
-
}
|
|
55
|
-
if ("responseType" in xhr) {
|
|
56
|
-
xhr.responseType = "arraybuffer";
|
|
57
|
-
}
|
|
58
|
-
if (init && typeof init === "object" && init.headers && typeof init.headers === "object" && !isHeaders(init.headers)) {
|
|
59
|
-
let headers = init.headers;
|
|
60
|
-
let names = [];
|
|
61
|
-
Object.getOwnPropertyNames(headers).forEach(name => {
|
|
62
|
-
names.push(normalizeName(name));
|
|
63
|
-
xhr.setRequestHeader(name, normalizeValue(headers[name]));
|
|
64
|
-
});
|
|
65
|
-
request.headers.forEach((value, name) => {
|
|
66
|
-
if (names.indexOf(name) === -1) {
|
|
67
|
-
xhr.setRequestHeader(name, value);
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
request.headers.forEach((value, name) => {
|
|
73
|
-
xhr.setRequestHeader(name, value);
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
if (signal) {
|
|
77
|
-
const abortFn = () => { aborted = true; xhr.abort(); removeFn(); };
|
|
78
|
-
const removeFn = () => { signal.removeEventListener("abort", abortFn); };
|
|
79
|
-
signal.addEventListener("abort", abortFn);
|
|
80
|
-
xhr.onreadystatechange = () => { if (xhr.readyState === 4)
|
|
81
|
-
removeFn(); };
|
|
82
|
-
}
|
|
83
|
-
if (!payload)
|
|
84
|
-
xhr.send();
|
|
85
|
-
else
|
|
86
|
-
payload.promise.then(body => {
|
|
87
|
-
if (!aborted)
|
|
88
|
-
xhr.send(body !== "" ? body : undefined);
|
|
89
|
-
else
|
|
90
|
-
reject(new DOMExceptionP("The user aborted a request.", "AbortError"));
|
|
91
|
-
})
|
|
92
|
-
.catch(e => {
|
|
93
|
-
console.error(e);
|
|
94
|
-
reject(new TypeError("Failed to fetch"));
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
function parseHeaders(rawHeaders) {
|
|
99
|
-
let headers = new HeadersP();
|
|
100
|
-
let preProcessedHeaders = rawHeaders.replace(/\r?\n[\t ]+/g, " ");
|
|
101
|
-
preProcessedHeaders
|
|
102
|
-
.split("\r")
|
|
103
|
-
.map(header => header.indexOf("\n") === 0 ? header.substring(1, header.length) : header)
|
|
104
|
-
.forEach(line => {
|
|
105
|
-
let parts = line.split(":");
|
|
106
|
-
let name = parts.shift().trim();
|
|
107
|
-
if (name) {
|
|
108
|
-
let value = parts.join(":").trim();
|
|
109
|
-
try {
|
|
110
|
-
headers.append(name, value);
|
|
111
|
-
}
|
|
112
|
-
catch (e) {
|
|
113
|
-
console.warn(`SyntaxError: Response.headers: '${name}' is not a valid HTTP header field name.`);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
return headers;
|
|
118
|
-
}
|
|
119
|
-
const fetchE = (typeof fetch !== "undefined" && fetch) || fetchP;
|
|
120
|
-
|
|
121
|
-
export { fetchE as fetch, fetchP, setXMLHttpRequest };
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { encode } from '../helpers/encode.js';
|
|
2
|
-
import { decode } from '../helpers/decode.js';
|
|
3
|
-
import { setState, className, SymbolP } from '../utils.js';
|
|
4
|
-
import { isBlob } from '../helpers/isBlob.js';
|
|
5
|
-
import { isSequence } from '../helpers/isSequence.js';
|
|
6
|
-
import { isArrayBuffer } from '../helpers/isArrayBuffer.js';
|
|
7
|
-
|
|
8
|
-
class BlobP {
|
|
9
|
-
constructor(blobParts = [], options) {
|
|
10
|
-
if (!isSequence(blobParts)) {
|
|
11
|
-
throw new TypeError(`Failed to construct 'Blob/File': The provided value cannot be converted to a sequence.`);
|
|
12
|
-
}
|
|
13
|
-
let _blobParts = Array.isArray(blobParts) ? blobParts : Array.from(blobParts);
|
|
14
|
-
let tasks = [];
|
|
15
|
-
let size = 0;
|
|
16
|
-
for (let i = 0; i < _blobParts.length; ++i) {
|
|
17
|
-
let chunk = _blobParts[i];
|
|
18
|
-
if (isBlob(chunk)) {
|
|
19
|
-
size += chunk.size;
|
|
20
|
-
tasks.push(chunk.arrayBuffer().then(r => new Uint8Array(r)));
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
let bytes = (isArrayBuffer(chunk) || ArrayBuffer.isView(chunk))
|
|
24
|
-
? BufferSource_toUint8Array(chunk)
|
|
25
|
-
: encode(chunk);
|
|
26
|
-
size += bytes.length;
|
|
27
|
-
tasks.push(Promise.resolve(bytes));
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
setState(this, "__Blob__", new BlobState(Promise.all(tasks).then(chunks => concat(chunks))));
|
|
31
|
-
state(this).size = size;
|
|
32
|
-
state(this).type = normalizeType(options === null || options === void 0 ? void 0 : options.type);
|
|
33
|
-
}
|
|
34
|
-
get size() { return state(this).size; }
|
|
35
|
-
get type() { return state(this).type; }
|
|
36
|
-
arrayBuffer() {
|
|
37
|
-
return state(this).promise.then(r => clone(r.buffer).buffer);
|
|
38
|
-
}
|
|
39
|
-
bytes() {
|
|
40
|
-
return state(this).promise.then(r => clone(r.buffer));
|
|
41
|
-
}
|
|
42
|
-
slice(start, end, contentType) {
|
|
43
|
-
let _start = start !== null && start !== void 0 ? start : 0, _end = end !== null && end !== void 0 ? end : this.size;
|
|
44
|
-
let blob = Object.create(BlobP.prototype);
|
|
45
|
-
setState(blob, "__Blob__", new BlobState(state(this).promise.then(r => clone(r.slice(_start, _end)))));
|
|
46
|
-
state(blob).size = calcSlicedSize(this.size, _start, _end);
|
|
47
|
-
state(blob).type = normalizeType(contentType);
|
|
48
|
-
return blob;
|
|
49
|
-
}
|
|
50
|
-
stream() {
|
|
51
|
-
throw new TypeError(`Failed to execute 'stream' on '${className(this)}': method not implemented.`);
|
|
52
|
-
}
|
|
53
|
-
text() {
|
|
54
|
-
return state(this).promise.then(r => decode(r));
|
|
55
|
-
}
|
|
56
|
-
/** @internal */ toString() { return "[object Blob]"; }
|
|
57
|
-
/** @internal */ get [SymbolP.toStringTag]() { return "Blob"; }
|
|
58
|
-
/** @internal */ get __MPHTTPX__() { return { chain: ["Blob"] }; }
|
|
59
|
-
}
|
|
60
|
-
class BlobState {
|
|
61
|
-
constructor(promise) {
|
|
62
|
-
this.size = 0;
|
|
63
|
-
this.type = "";
|
|
64
|
-
this.promise = promise;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
function state(target) {
|
|
68
|
-
return target.__Blob__;
|
|
69
|
-
}
|
|
70
|
-
function BufferSource_toUint8Array(buf) {
|
|
71
|
-
return isArrayBuffer(buf)
|
|
72
|
-
? new Uint8Array(buf)
|
|
73
|
-
: new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
74
|
-
}
|
|
75
|
-
function normalizeType(type) {
|
|
76
|
-
let rawType = "" + (type !== undefined ? type : "");
|
|
77
|
-
return /[^\u0020-\u007E]/.test(rawType) ? "" : rawType.toLowerCase();
|
|
78
|
-
}
|
|
79
|
-
function clone(buf) {
|
|
80
|
-
let sourceArray = BufferSource_toUint8Array(buf);
|
|
81
|
-
let cloneArray = new Uint8Array(new ArrayBuffer(sourceArray.byteLength));
|
|
82
|
-
cloneArray.set(sourceArray);
|
|
83
|
-
return cloneArray;
|
|
84
|
-
}
|
|
85
|
-
function concat(chunks) {
|
|
86
|
-
let totalByteLength = chunks.reduce((acc, cur) => acc + cur.byteLength, 0);
|
|
87
|
-
let result = new Uint8Array(totalByteLength);
|
|
88
|
-
chunks.reduce((offset, chunk) => { result.set(chunk, offset); return offset + chunk.byteLength; }, 0);
|
|
89
|
-
return result;
|
|
90
|
-
}
|
|
91
|
-
function calcSlicedSize(size, start, end) {
|
|
92
|
-
const normalizeNumer = (n) => {
|
|
93
|
-
let num = Number(n);
|
|
94
|
-
if (isNaN(num))
|
|
95
|
-
num = 0;
|
|
96
|
-
if (num >= 0) {
|
|
97
|
-
num = Math.min(num, size);
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
num = Math.max(0, num + size);
|
|
101
|
-
}
|
|
102
|
-
return num;
|
|
103
|
-
};
|
|
104
|
-
return Math.max(0, normalizeNumer(end) - normalizeNumer(start));
|
|
105
|
-
}
|
|
106
|
-
const BlobE = (typeof Blob !== "undefined" && Blob) || BlobP;
|
|
107
|
-
|
|
108
|
-
export { BlobE as Blob, BlobP };
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { BlobP } from './BlobP.js';
|
|
2
|
-
import { checkArgsLength, setState, SymbolP } from '../utils.js';
|
|
3
|
-
|
|
4
|
-
class FileP extends BlobP {
|
|
5
|
-
constructor(fileBits, fileName, options) {
|
|
6
|
-
checkArgsLength(arguments.length, 2, "File");
|
|
7
|
-
super(fileBits, options);
|
|
8
|
-
setState(this, "__File__", new FileState());
|
|
9
|
-
state(this).lastModified = +((options === null || options === void 0 ? void 0 : options.lastModified) ? new Date(options.lastModified) : new Date()) || 0;
|
|
10
|
-
state(this).name = "" + fileName;
|
|
11
|
-
}
|
|
12
|
-
get lastModified() { return state(this).lastModified; }
|
|
13
|
-
get name() { return state(this).name; }
|
|
14
|
-
get webkitRelativePath() { return ""; }
|
|
15
|
-
/** @internal */ toString() { return "[object File]"; }
|
|
16
|
-
/** @internal */ get [SymbolP.toStringTag]() { return "File"; }
|
|
17
|
-
/** @internal */ get __MPHTTPX__() { return { chain: ["File", "Blob"] }; }
|
|
18
|
-
}
|
|
19
|
-
/** @internal */
|
|
20
|
-
class FileState {
|
|
21
|
-
constructor() {
|
|
22
|
-
this.lastModified = 0;
|
|
23
|
-
this.name = "";
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
function state(target) {
|
|
27
|
-
return target.__File__;
|
|
28
|
-
}
|
|
29
|
-
const FileE = (typeof File !== "undefined" && File) || FileP;
|
|
30
|
-
|
|
31
|
-
export { FileE as File, FileP };
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import { isBlob } from '../helpers/isBlob.js';
|
|
2
|
-
import { attachFn, executeFn } from '../helpers/handlers.js';
|
|
3
|
-
import { Uint8Array_toBase64 } from '../helpers/toBase64.js';
|
|
4
|
-
import { EventTargetP } from '../event-system/EventTargetP.js';
|
|
5
|
-
import { emitProgressEvent } from '../helpers/emitProgressEvent.js';
|
|
6
|
-
import { setState, SymbolP, DOMExceptionP, checkArgsLength } from '../utils.js';
|
|
7
|
-
|
|
8
|
-
class FileReaderP extends EventTargetP {
|
|
9
|
-
static get EMPTY() { return 0; }
|
|
10
|
-
static get LOADING() { return 1; }
|
|
11
|
-
static get DONE() { return 2; }
|
|
12
|
-
constructor() {
|
|
13
|
-
super();
|
|
14
|
-
setState(this, "__FileReader__", new FileReaderState(this));
|
|
15
|
-
}
|
|
16
|
-
get EMPTY() { return 0; }
|
|
17
|
-
get LOADING() { return 1; }
|
|
18
|
-
get DONE() { return 2; }
|
|
19
|
-
get error() { return state(this).error; }
|
|
20
|
-
get readyState() { return state(this).readyState; }
|
|
21
|
-
get result() { return state(this).result; }
|
|
22
|
-
abort() {
|
|
23
|
-
if (state(this).pos === 2 /* FRCycle.LOADING */)
|
|
24
|
-
execAbort(this);
|
|
25
|
-
}
|
|
26
|
-
readAsArrayBuffer(blob) {
|
|
27
|
-
check(this, "readAsArrayBuffer", arguments.length, blob);
|
|
28
|
-
read(this, blob.size, () => blob.arrayBuffer());
|
|
29
|
-
}
|
|
30
|
-
readAsBinaryString(blob) {
|
|
31
|
-
check(this, "readAsBinaryString", arguments.length, blob);
|
|
32
|
-
read(this, blob.size, () => blob.arrayBuffer().then(res => {
|
|
33
|
-
let str = [];
|
|
34
|
-
let buf = new Uint8Array(res);
|
|
35
|
-
for (let i = 0; i < buf.length; ++i) {
|
|
36
|
-
str.push(String.fromCharCode(buf[i]));
|
|
37
|
-
}
|
|
38
|
-
return str.join("");
|
|
39
|
-
}));
|
|
40
|
-
}
|
|
41
|
-
readAsDataURL(blob) {
|
|
42
|
-
check(this, "readAsDataURL", arguments.length, blob);
|
|
43
|
-
read(this, blob.size, () => blob.arrayBuffer().then(res => {
|
|
44
|
-
return "data:" + (blob.type || "application/octet-stream") + ";base64," + Uint8Array_toBase64(new Uint8Array(res));
|
|
45
|
-
}));
|
|
46
|
-
}
|
|
47
|
-
readAsText(blob, encoding) {
|
|
48
|
-
check(this, "readAsText", arguments.length, blob);
|
|
49
|
-
if (encoding !== undefined) {
|
|
50
|
-
let _encoding = "" + encoding;
|
|
51
|
-
if (["utf-8", "utf8", "unicode-1-1-utf-8"].indexOf(_encoding.toLowerCase()) === -1) {
|
|
52
|
-
console.warn(`Ignoring the execution of 'readAsText' on 'FileReader': encoding ('${_encoding}') not implemented.`);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
read(this, blob.size, () => blob.text());
|
|
56
|
-
}
|
|
57
|
-
get onabort() { return state(this).onabort; }
|
|
58
|
-
set onabort(value) { state(this).onabort = value; state(this).attach("abort"); }
|
|
59
|
-
get onerror() { return state(this).onerror; }
|
|
60
|
-
set onerror(value) { state(this).onerror = value; state(this).attach("error"); }
|
|
61
|
-
get onload() { return state(this).onload; }
|
|
62
|
-
set onload(value) { state(this).onload = value; state(this).attach("load"); }
|
|
63
|
-
get onloadend() { return state(this).onloadend; }
|
|
64
|
-
set onloadend(value) { state(this).onloadend = value; state(this).attach("loadend"); }
|
|
65
|
-
get onloadstart() { return state(this).onloadstart; }
|
|
66
|
-
set onloadstart(value) { state(this).onloadstart = value; state(this).attach("loadstart"); }
|
|
67
|
-
get onprogress() { return state(this).onprogress; }
|
|
68
|
-
set onprogress(value) { state(this).onprogress = value; state(this).attach("progress"); }
|
|
69
|
-
/** @internal */ toString() { return "[object FileReader]"; }
|
|
70
|
-
/** @internal */ get [SymbolP.toStringTag]() { return "FileReader"; }
|
|
71
|
-
/** @internal */ get __MPHTTPX__() { return { chain: ["FileReader", "EventTarget"] }; }
|
|
72
|
-
}
|
|
73
|
-
/** @internal */
|
|
74
|
-
class FileReaderState {
|
|
75
|
-
constructor(target) {
|
|
76
|
-
this.pos = 0 /* FRCycle.EMPTY */;
|
|
77
|
-
this.readyState = 0 /* EMPTY */;
|
|
78
|
-
this.result = null;
|
|
79
|
-
this.error = null;
|
|
80
|
-
this.onabort = null;
|
|
81
|
-
this.onerror = null;
|
|
82
|
-
this.onload = null;
|
|
83
|
-
this.onloadend = null;
|
|
84
|
-
this.onloadstart = null;
|
|
85
|
-
this.onprogress = null;
|
|
86
|
-
this.attach = attachFn(target, getHandlers(target));
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
function getHandlers(t) {
|
|
90
|
-
return {
|
|
91
|
-
onabort: (ev) => { executeFn(t, t.onabort, ev); },
|
|
92
|
-
onerror: (ev) => { executeFn(t, t.onerror, ev); },
|
|
93
|
-
onload: (ev) => { executeFn(t, t.onload, ev); },
|
|
94
|
-
onloadend: (ev) => { executeFn(t, t.onloadend, ev); },
|
|
95
|
-
onloadstart: (ev) => { executeFn(t, t.onloadstart, ev); },
|
|
96
|
-
onprogress: (ev) => { executeFn(t, t.onprogress, ev); },
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
function state(target) {
|
|
100
|
-
return target.__FileReader__;
|
|
101
|
-
}
|
|
102
|
-
function check(reader, kind, actual, blob) {
|
|
103
|
-
checkArgsLength(actual, 1, "FileReader", kind);
|
|
104
|
-
if (!isBlob(blob))
|
|
105
|
-
throw new TypeError("Failed to execute '" + kind + "' on 'FileReader': parameter 1 is not of type 'Blob'.");
|
|
106
|
-
if (reader.readyState === 1 /* LOADING */)
|
|
107
|
-
throw new DOMExceptionP(`Failed to execute '${kind}' on 'FileReader': The object is already busy reading Blobs.`, "InvalidStateError");
|
|
108
|
-
}
|
|
109
|
-
function read(reader, size, launch) {
|
|
110
|
-
execLoadstart(reader, size, launch);
|
|
111
|
-
}
|
|
112
|
-
function execLoadstart(reader, size, launch) {
|
|
113
|
-
let s = state(reader);
|
|
114
|
-
s.pos = 1 /* FRCycle.LOADSTART */;
|
|
115
|
-
s.error = null;
|
|
116
|
-
s.result = null;
|
|
117
|
-
const callback = () => { emitProgressEvent(reader, "loadstart", 0, size); return launch(); };
|
|
118
|
-
return execLoading(reader, size, callback);
|
|
119
|
-
}
|
|
120
|
-
function execLoading(reader, size, launch) {
|
|
121
|
-
let s = state(reader);
|
|
122
|
-
s.pos = 2 /* FRCycle.LOADING */;
|
|
123
|
-
s.readyState = 1 /* LOADING */;
|
|
124
|
-
return Promise.resolve().then(() => {
|
|
125
|
-
if (s.pos !== 2 /* FRCycle.LOADING */)
|
|
126
|
-
return;
|
|
127
|
-
return launch().then(r => execDone(reader, size, r)).catch(err => execError(reader, err));
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
function execDone(reader, size, result) {
|
|
131
|
-
let s = state(reader);
|
|
132
|
-
if (s.pos !== 2 /* FRCycle.LOADING */)
|
|
133
|
-
return;
|
|
134
|
-
s.pos = 3 /* FRCycle.DONE */;
|
|
135
|
-
s.result = result;
|
|
136
|
-
s.readyState = 2; /* DONE */
|
|
137
|
-
execLoad(reader, size);
|
|
138
|
-
}
|
|
139
|
-
function execLoad(reader, size) {
|
|
140
|
-
state(reader).pos = 4 /* FRCycle.LOAD */;
|
|
141
|
-
emitProgressEvent(reader, "load", size, size);
|
|
142
|
-
execLoadend(reader, size);
|
|
143
|
-
}
|
|
144
|
-
function execAbort(reader) {
|
|
145
|
-
let s = state(reader);
|
|
146
|
-
s.pos = 5 /* FRCycle.ABORT */;
|
|
147
|
-
s.error = new DOMExceptionP("An ongoing operation was aborted, typically with a call to abort().", "AbortError");
|
|
148
|
-
s.result = null;
|
|
149
|
-
s.readyState = 2 /* DONE */;
|
|
150
|
-
emitProgressEvent(reader, "abort");
|
|
151
|
-
execLoadend(reader);
|
|
152
|
-
}
|
|
153
|
-
function execError(reader, err) {
|
|
154
|
-
let s = state(reader);
|
|
155
|
-
if (s.pos !== 2 /* FRCycle.LOADING */)
|
|
156
|
-
return;
|
|
157
|
-
s.pos = 6 /* FRCycle.ERROR */;
|
|
158
|
-
s.error = err;
|
|
159
|
-
s.result = null;
|
|
160
|
-
s.readyState = 2 /* DONE */;
|
|
161
|
-
emitProgressEvent(reader, "error");
|
|
162
|
-
execLoadend(reader);
|
|
163
|
-
}
|
|
164
|
-
function execLoadend(reader, size = 0) {
|
|
165
|
-
state(reader).pos = 7 /* FRCycle.LOADEND */;
|
|
166
|
-
emitProgressEvent(reader, "loadend", size, size);
|
|
167
|
-
}
|
|
168
|
-
const FileReaderE = (typeof FileReader !== "undefined" && FileReader) || FileReaderP;
|
|
169
|
-
|
|
170
|
-
export { FileReaderE as FileReader, FileReaderP };
|