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.
Files changed (95) hide show
  1. package/README.md +994 -994
  2. package/dist/cjs/fetchP.js +22 -0
  3. package/dist/cjs/index.js +197 -55
  4. package/dist/esm/fetchP.js +12 -0
  5. package/dist/esm/index.js +5 -18
  6. package/dist/index.d.ts +8 -394
  7. package/dist/mphttpx.cjs.js +6646 -0
  8. package/dist/mphttpx.cjs.min.js +1 -0
  9. package/dist/mphttpx.esm.js +6595 -0
  10. package/dist/mphttpx.esm.min.js +1 -0
  11. package/package.json +28 -9
  12. package/dist/cjs/encoding/TextDecoderP.js +0 -174
  13. package/dist/cjs/encoding/TextEncoderP.js +0 -121
  14. package/dist/cjs/event-system/AbortControllerP.js +0 -28
  15. package/dist/cjs/event-system/AbortSignalP.js +0 -120
  16. package/dist/cjs/event-system/CloseEventP.js +0 -36
  17. package/dist/cjs/event-system/CustomEventP.js +0 -35
  18. package/dist/cjs/event-system/EventP.js +0 -127
  19. package/dist/cjs/event-system/EventTargetP.js +0 -155
  20. package/dist/cjs/event-system/MessageEventP.js +0 -61
  21. package/dist/cjs/event-system/ProgressEventP.js +0 -42
  22. package/dist/cjs/fetch-api/BodyImpl.js +0 -167
  23. package/dist/cjs/fetch-api/HeadersP.js +0 -155
  24. package/dist/cjs/fetch-api/RequestP.js +0 -163
  25. package/dist/cjs/fetch-api/ResponseP.js +0 -93
  26. package/dist/cjs/fetch-api/fetchP.js +0 -118
  27. package/dist/cjs/file-system/BlobP.js +0 -111
  28. package/dist/cjs/file-system/FileP.js +0 -34
  29. package/dist/cjs/file-system/FileReaderP.js +0 -173
  30. package/dist/cjs/helpers/Payload.js +0 -96
  31. package/dist/cjs/helpers/decode.js +0 -11
  32. package/dist/cjs/helpers/emitEvent.js +0 -14
  33. package/dist/cjs/helpers/emitProgressEvent.js +0 -19
  34. package/dist/cjs/helpers/encode.js +0 -11
  35. package/dist/cjs/helpers/handlers.js +0 -23
  36. package/dist/cjs/helpers/isArrayBuffer.js +0 -11
  37. package/dist/cjs/helpers/isBlob.js +0 -20
  38. package/dist/cjs/helpers/isEventTarget.js +0 -18
  39. package/dist/cjs/helpers/isSequence.js +0 -13
  40. package/dist/cjs/helpers/isURLSearchParams.js +0 -10
  41. package/dist/cjs/helpers/normalizeMethod.js +0 -11
  42. package/dist/cjs/helpers/statusTextMap.js +0 -68
  43. package/dist/cjs/helpers/toBase64.js +0 -28
  44. package/dist/cjs/mini-program/WebSocketImpl.js +0 -197
  45. package/dist/cjs/mini-program/XMLHttpRequestImpl.js +0 -493
  46. package/dist/cjs/mini-program/connectSocket.js +0 -28
  47. package/dist/cjs/mini-program/platform.js +0 -29
  48. package/dist/cjs/mini-program/request.js +0 -36
  49. package/dist/cjs/network/FormDataP.js +0 -160
  50. package/dist/cjs/network/URLSearchParamsP.js +0 -226
  51. package/dist/cjs/network/XMLHttpRequestEventTargetP.js +0 -64
  52. package/dist/cjs/network/XMLHttpRequestUploadP.js +0 -28
  53. package/dist/cjs/utils.js +0 -56
  54. package/dist/esm/encoding/TextDecoderP.js +0 -171
  55. package/dist/esm/encoding/TextEncoderP.js +0 -118
  56. package/dist/esm/event-system/AbortControllerP.js +0 -25
  57. package/dist/esm/event-system/AbortSignalP.js +0 -115
  58. package/dist/esm/event-system/CloseEventP.js +0 -34
  59. package/dist/esm/event-system/CustomEventP.js +0 -32
  60. package/dist/esm/event-system/EventP.js +0 -122
  61. package/dist/esm/event-system/EventTargetP.js +0 -150
  62. package/dist/esm/event-system/MessageEventP.js +0 -59
  63. package/dist/esm/event-system/ProgressEventP.js +0 -40
  64. package/dist/esm/fetch-api/BodyImpl.js +0 -164
  65. package/dist/esm/fetch-api/HeadersP.js +0 -149
  66. package/dist/esm/fetch-api/RequestP.js +0 -160
  67. package/dist/esm/fetch-api/ResponseP.js +0 -90
  68. package/dist/esm/fetch-api/fetchP.js +0 -114
  69. package/dist/esm/file-system/BlobP.js +0 -108
  70. package/dist/esm/file-system/FileP.js +0 -31
  71. package/dist/esm/file-system/FileReaderP.js +0 -170
  72. package/dist/esm/helpers/Payload.js +0 -94
  73. package/dist/esm/helpers/decode.js +0 -9
  74. package/dist/esm/helpers/emitEvent.js +0 -12
  75. package/dist/esm/helpers/emitProgressEvent.js +0 -17
  76. package/dist/esm/helpers/encode.js +0 -9
  77. package/dist/esm/helpers/handlers.js +0 -20
  78. package/dist/esm/helpers/isArrayBuffer.js +0 -9
  79. package/dist/esm/helpers/isBlob.js +0 -18
  80. package/dist/esm/helpers/isEventTarget.js +0 -16
  81. package/dist/esm/helpers/isSequence.js +0 -11
  82. package/dist/esm/helpers/isURLSearchParams.js +0 -8
  83. package/dist/esm/helpers/normalizeMethod.js +0 -9
  84. package/dist/esm/helpers/statusTextMap.js +0 -66
  85. package/dist/esm/helpers/toBase64.js +0 -26
  86. package/dist/esm/mini-program/WebSocketImpl.js +0 -193
  87. package/dist/esm/mini-program/XMLHttpRequestImpl.js +0 -489
  88. package/dist/esm/mini-program/connectSocket.js +0 -26
  89. package/dist/esm/mini-program/platform.js +0 -27
  90. package/dist/esm/mini-program/request.js +0 -34
  91. package/dist/esm/network/FormDataP.js +0 -157
  92. package/dist/esm/network/URLSearchParamsP.js +0 -223
  93. package/dist/esm/network/XMLHttpRequestEventTargetP.js +0 -61
  94. package/dist/esm/network/XMLHttpRequestUploadP.js +0 -25
  95. package/dist/esm/utils.js +0 -48
@@ -1,489 +0,0 @@
1
- import { BlobP } from '../file-system/BlobP.js';
2
- import { HeadersP } from '../fetch-api/HeadersP.js';
3
- import { encode } from '../helpers/encode.js';
4
- import { decode } from '../helpers/decode.js';
5
- import { Payload } from '../helpers/Payload.js';
6
- import { isArrayBuffer } from '../helpers/isArrayBuffer.js';
7
- import { statusTextMap } from '../helpers/statusTextMap.js';
8
- import { Uint8Array_toBase64 } from '../helpers/toBase64.js';
9
- import { attachFn, executeFn } from '../helpers/handlers.js';
10
- import { normalizeMethod } from '../helpers/normalizeMethod.js';
11
- import { emitEvent } from '../helpers/emitEvent.js';
12
- import { emitProgressEvent } from '../helpers/emitProgressEvent.js';
13
- import { getRequest } from './request.js';
14
- import { setState, DOMExceptionP, checkArgsLength, SymbolP } from '../utils.js';
15
- import { createXMLHttpRequestUpload } from '../network/XMLHttpRequestUploadP.js';
16
- import { XMLHttpRequestEventTargetP } from '../network/XMLHttpRequestEventTargetP.js';
17
-
18
- const mp = { request: getRequest() };
19
- function setRequest(request) { mp.request = request; }
20
- class XMLHttpRequestImpl extends XMLHttpRequestEventTargetP {
21
- static get UNSENT() { return 0; }
22
- static get OPENED() { return 1; }
23
- static get HEADERS_RECEIVED() { return 2; }
24
- static get LOADING() { return 3; }
25
- static get DONE() { return 4; }
26
- constructor() {
27
- super();
28
- setState(this, "__XMLHttpRequest__", new XMLHttpRequestState(this));
29
- }
30
- get UNSENT() { return 0; }
31
- get OPENED() { return 1; }
32
- get HEADERS_RECEIVED() { return 2; }
33
- get LOADING() { return 3; }
34
- get DONE() { return 4; }
35
- get readyState() { return state(this).readyState; }
36
- get response() { return state(this).response; }
37
- get responseText() { return state(this).responseText; }
38
- get responseType() { return state(this).responseType; }
39
- set responseType(value) {
40
- if (state(this).pos > 9 /* XHRCycle.HEADERS_RECEIVED */)
41
- throw new DOMExceptionP("Failed to set the 'responseType' property on 'XMLHttpRequest': The response type cannot be set if the object's state is LOADING or DONE.", "InvalidStateError");
42
- if (responseTypes.indexOf(value) > -1)
43
- state(this).responseType = value;
44
- }
45
- get responseURL() { return state(this).responseURL; }
46
- get responseXML() { return null; }
47
- get status() { return state(this).status; }
48
- get statusText() {
49
- if (state(this).pos < 9 /* XHRCycle.HEADERS_RECEIVED */)
50
- return "";
51
- return state(this).statusText || statusTextMap[this.status] || "unknown";
52
- }
53
- get timeout() { return state(this).timeout; }
54
- set timeout(value) { state(this).timeout = value >= 0 ? value : 0; }
55
- get upload() {
56
- if (!state(this).upload)
57
- state(this).upload = createXMLHttpRequestUpload();
58
- return state(this).upload;
59
- }
60
- get withCredentials() { return state(this).withCredentials; }
61
- set withCredentials(value) {
62
- if (state(this).pos < 2 /* XHRCycle.LOADSTART */)
63
- state(this).withCredentials = !!value;
64
- else
65
- throw new TypeError("Failed to set the 'withCredentials' property on 'XMLHttpRequest': The value may only be set if the object's state is UNSENT or OPENED.");
66
- }
67
- abort() {
68
- const s = state(this);
69
- const requestTask = s.requestTask;
70
- switch (s.pos) {
71
- case 1 /* XHRCycle.OPENED */:
72
- s.requestHeaders = new HeadersP();
73
- break;
74
- case 2 /* XHRCycle.LOADSTART */:
75
- case 3 /* XHRCycle.UPLOAD_LOADSTART */:
76
- execUploadAbort(this);
77
- execUnset(this);
78
- break;
79
- case 4 /* XHRCycle.UPLOAD_LOAD */:
80
- case 8 /* XHRCycle.UPLOAD_LOADEND */:
81
- case 9 /* XHRCycle.HEADERS_RECEIVED */:
82
- case 10 /* XHRCycle.LOADING */:
83
- execAbort(this);
84
- case 11 /* XHRCycle.DONE */:
85
- execUnset(this);
86
- break;
87
- }
88
- clearRequest(s);
89
- safeAbort(requestTask);
90
- }
91
- getAllResponseHeaders() {
92
- const headers = state(this).responseHeaders;
93
- if (!headers)
94
- return "";
95
- let result = [];
96
- headers.forEach((value, name) => { result.push(`${name}: ${value}\r\n`); });
97
- return result.join("");
98
- }
99
- getResponseHeader(name) {
100
- checkArgsLength(arguments.length, 1, "XMLHttpRequest", "getResponseHeader");
101
- if (!state(this).responseHeaders)
102
- return null;
103
- return state(this).responseHeaders.get(name);
104
- }
105
- open(method, url, async = true, username = null, password = null) {
106
- checkArgsLength(arguments.length, 2, "XMLHttpRequest", "open");
107
- if (!async) {
108
- console.warn("Synchronous XMLHttpRequest is not supported because of its detrimental effects to the end user's experience.");
109
- }
110
- let _method = normalizeMethod(method);
111
- let _url = "" + url;
112
- let _username = "" + (username !== null && username !== void 0 ? username : "");
113
- let _password = "" + (password !== null && password !== void 0 ? password : "");
114
- const s = state(this);
115
- clearRequest(s);
116
- s.method = _method;
117
- s.requestURL = _url;
118
- s.requestHeaders = new HeadersP();
119
- if (_username.length > 0 || _password.length > 0) {
120
- let auth = `Basic ${Uint8Array_toBase64(encode(_username + ":" + _password))}`;
121
- this.setRequestHeader("Authorization", auth);
122
- }
123
- execOpened(this);
124
- }
125
- overrideMimeType(mime) {
126
- checkArgsLength(arguments.length, 1, "XMLHttpRequest", "overrideMimeType");
127
- if (state(this).pos === 1 /* XHRCycle.OPENED */) {
128
- console.error(`TypeError: Failed to execute 'overrideMimeType' on 'XMLHttpRequest': mimeType ('${mime}') not implemented.`);
129
- }
130
- }
131
- send(body) {
132
- const s = state(this);
133
- const requestId = s.requestId;
134
- if (s.pos !== 1 /* XHRCycle.OPENED */) {
135
- throw new DOMExceptionP("Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED.", "InvalidStateError");
136
- }
137
- const options = {
138
- url: s.requestURL,
139
- method: s.method,
140
- header: Headers_toDict(s.requestHeaders),
141
- dataType: this.responseType === "json" ? "json" : normalizeDataType(this.responseType),
142
- responseType: normalizeDataType(this.responseType),
143
- withCredentials: this.withCredentials,
144
- success: requestSuccess.bind(this, requestId),
145
- fail: requestFail.bind(this, requestId),
146
- };
147
- const payload = (s.method !== "GET" && s.method !== "HEAD" && body !== null && body !== undefined) && new Payload(body);
148
- if (payload && payload.type && !s.requestHeaders.has("Content-Type")) {
149
- options.header["Content-Type"] = payload.type;
150
- }
151
- const request = (function (data) {
152
- if (requestId !== s.requestId)
153
- return;
154
- if (s.pos !== 2 /* XHRCycle.LOADSTART */ && s.pos !== 8 /* XHRCycle.UPLOAD_LOADEND */)
155
- return;
156
- options.data = data !== "" ? data : undefined;
157
- options.headers = options.header; // Alipay Mini Program
158
- s.requestTask = mp.request(options);
159
- }).bind(this);
160
- checkRequestTimeout(this);
161
- const task = execLoadstart(this, payload || undefined);
162
- if (!task)
163
- request();
164
- else
165
- task.then(data => { if (data !== undefined)
166
- request(data); });
167
- }
168
- setRequestHeader(name, value) {
169
- checkArgsLength(arguments.length, 2, "XMLHttpRequest", "setRequestHeader");
170
- if (state(this).pos !== 1 /* XHRCycle.OPENED */) {
171
- throw new DOMExceptionP("Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.", "InvalidStateError");
172
- }
173
- let _name = "" + name;
174
- let _value = "" + value;
175
- try {
176
- state(this).requestHeaders.append(_name, _value);
177
- }
178
- catch (e) {
179
- throw new SyntaxError(`Failed to execute 'setRequestHeader' on 'XMLHttpRequest': '${_name}' is not a valid HTTP header field name.`);
180
- }
181
- }
182
- get onreadystatechange() { return state(this).onreadystatechange; }
183
- set onreadystatechange(value) { state(this).onreadystatechange = value; state(this).attach("readystatechange"); }
184
- /** @internal */ toString() { return "[object XMLHttpRequest]"; }
185
- /** @internal */ get [SymbolP.toStringTag]() { return "XMLHttpRequest"; }
186
- /** @internal */ get __MPHTTPX__() { return { chain: ["XMLHttpRequest", "XMLHttpRequestEventTarget", "EventTarget"] }; }
187
- }
188
- class XMLHttpRequestState {
189
- constructor(target) {
190
- this.pos = 0 /* XHRCycle.UNSET */;
191
- this.readyState = 0 /* UNSENT */;
192
- this.response = "";
193
- this.responseText = "";
194
- this.responseType = "";
195
- this.responseURL = "";
196
- this.status = 0;
197
- this.statusText = "";
198
- this.timeout = 0;
199
- this.withCredentials = false;
200
- this.method = "GET";
201
- this.requestURL = "";
202
- this.requestHeaders = null;
203
- this.responseHeaders = null;
204
- this.timeoutId = 0;
205
- this.requestId = 0;
206
- this.requestTask = null;
207
- this.onreadystatechange = null;
208
- this.attach = attachFn(target, getHandlers(target));
209
- }
210
- }
211
- function getHandlers(t) {
212
- return {
213
- onreadystatechange: (ev) => { executeFn(t, t.onreadystatechange, ev); },
214
- };
215
- }
216
- function state(target) {
217
- return target.__XMLHttpRequest__;
218
- }
219
- function Headers_toDict(headers) {
220
- let dict = {};
221
- headers.forEach((value, name) => { dict[name] = value; });
222
- return dict;
223
- }
224
- const responseTypes = ["", "arraybuffer", "blob", "document", "json", "text"];
225
- function normalizeDataType(responseType) {
226
- return (responseType === "blob" || responseType === "arraybuffer") ? "arraybuffer" : "text";
227
- }
228
- function clearRequest(state) {
229
- state.requestTask = null;
230
- clearRequestTimeout(state);
231
- state.response = "";
232
- state.responseText = "";
233
- state.responseURL = "";
234
- state.status = 0;
235
- state.statusText = "";
236
- state.requestHeaders = null;
237
- state.responseHeaders = null;
238
- }
239
- function clearRequestTimeout(state) {
240
- if (state.timeoutId) {
241
- clearTimeout(state.timeoutId);
242
- state.timeoutId = 0;
243
- }
244
- }
245
- function checkRequestTimeout(xhr) {
246
- if (!xhr.timeout)
247
- return;
248
- const s = state(xhr);
249
- const whenTimeout = () => {
250
- switch (s.pos) {
251
- case 2 /* XHRCycle.LOADSTART */:
252
- case 3 /* XHRCycle.UPLOAD_LOADSTART */:
253
- execUploadTimeout(xhr);
254
- break;
255
- case 8 /* XHRCycle.UPLOAD_LOADEND */:
256
- execTimeout(xhr);
257
- break;
258
- }
259
- };
260
- s.timeoutId = setTimeout(() => { let task = s.requestTask; whenTimeout(); safeAbort(task); }, xhr.timeout);
261
- }
262
- function requestSuccess(requestId, res) {
263
- if (requestId !== state(this).requestId)
264
- return;
265
- execHeadersReceived(this, res);
266
- }
267
- function requestFail(requestId, err) {
268
- // Alipay Mini Program
269
- if (("header" in err && "statusCode" in err) || ("headers" in err && "status" in err)) {
270
- return requestSuccess.call(this, requestId, {
271
- statusCode: "statusCode" in err ? err.statusCode : err.status || 0,
272
- header: "header" in err ? err.header : err.headers || {},
273
- data: "data" in err ? err.data || "" : "",
274
- });
275
- }
276
- if (requestId !== state(this).requestId)
277
- return;
278
- execError(this, err);
279
- }
280
- function setReadyStateAndNotify(xhr, value) {
281
- if (xhr.readyState !== value) {
282
- state(xhr).readyState = value;
283
- emitEvent(xhr, "readystatechange");
284
- }
285
- }
286
- function getResponse(type, data) {
287
- let temp = (typeof data === "string" || isArrayBuffer(data))
288
- ? data
289
- : JSON.stringify(data);
290
- switch (type) {
291
- case "":
292
- case "text":
293
- return typeof temp === "string" ? temp : decode(temp);
294
- case "arraybuffer":
295
- return isArrayBuffer(temp) ? temp : encode(temp).buffer;
296
- case "json":
297
- return typeof data === "string"
298
- ? JSON.parse(data)
299
- : isArrayBuffer(data)
300
- ? JSON.parse(decode(data))
301
- : data;
302
- case "blob":
303
- return new BlobP([temp]);
304
- default:
305
- return temp;
306
- }
307
- }
308
- // Alipay Mini Program
309
- function safeAbort(task) {
310
- if (task && typeof task === "object") {
311
- if ("abort" in task && typeof task.abort === "function")
312
- task.abort();
313
- }
314
- }
315
- function execUnset(xhr) {
316
- state(xhr).pos = 0 /* XHRCycle.UNSET */;
317
- state(xhr).readyState = 0 /* UNSENT */;
318
- }
319
- function execOpened(xhr) {
320
- state(xhr).pos = 1 /* XHRCycle.OPENED */;
321
- state(xhr).requestId += 1;
322
- setReadyStateAndNotify(xhr, 1 /* OPENED */);
323
- }
324
- function execLoadstart(xhr, payload) {
325
- state(xhr).pos = 2 /* XHRCycle.LOADSTART */;
326
- emitProgressEvent(xhr, "loadstart");
327
- return payload && execUploadLoadstart(xhr, payload);
328
- }
329
- function execUploadLoadstart(xhr, payload) {
330
- let s = state(xhr);
331
- let requestId = s.requestId;
332
- if (s.pos !== 2 /* XHRCycle.LOADSTART */)
333
- return;
334
- s.pos = 3 /* XHRCycle.UPLOAD_LOADSTART */;
335
- if (s.upload)
336
- emitProgressEvent(xhr.upload, "loadstart", 0, payload.size);
337
- return payload.promise
338
- .then(r => { if (requestId === s.requestId) {
339
- return execUploadLoad(xhr, payload, r);
340
- } })
341
- .catch(e => { if (requestId === s.requestId) {
342
- execUploadError(xhr);
343
- console.error(e);
344
- } });
345
- }
346
- function execUploadLoad(xhr, payload, data) {
347
- let s = state(xhr);
348
- if (s.pos !== 3 /* XHRCycle.UPLOAD_LOADSTART */)
349
- return;
350
- s.pos = 4 /* XHRCycle.UPLOAD_LOAD */;
351
- if (s.upload && payload.size > 0)
352
- emitProgressEvent(xhr.upload, "load", payload.size, payload.size);
353
- return execUploadLoadend(xhr, "load", { payload, data });
354
- }
355
- function execUploadAbort(xhr) {
356
- state(xhr).pos = 5 /* XHRCycle.UPLOAD_ABORT */;
357
- setReadyStateAndNotify(xhr, 4 /* DONE */);
358
- if (state(xhr).upload)
359
- emitProgressEvent(xhr.upload, "abort");
360
- execUploadLoadend(xhr, "abort");
361
- }
362
- function execUploadError(xhr) {
363
- let s = state(xhr);
364
- if (s.pos !== 3 /* XHRCycle.UPLOAD_LOADSTART */)
365
- return;
366
- s.pos = 6 /* XHRCycle.UPLOAD_ERROR */;
367
- setReadyStateAndNotify(xhr, 4 /* DONE */);
368
- if (s.upload)
369
- emitProgressEvent(xhr.upload, "error");
370
- execUploadLoadend(xhr, "error");
371
- }
372
- function execUploadTimeout(xhr) {
373
- state(xhr).pos = 7 /* XHRCycle.UPLOAD_TIMEOUT */;
374
- setReadyStateAndNotify(xhr, 4 /* DONE */);
375
- if (state(xhr).upload)
376
- emitProgressEvent(xhr.upload, "timeout");
377
- execUploadLoadend(xhr, "timeout");
378
- }
379
- function execUploadLoadend(xhr, type, ctx) {
380
- let s = state(xhr);
381
- if ((type === "load" && s.pos === 4 /* XHRCycle.UPLOAD_LOAD */) || type !== "load")
382
- s.pos = 8 /* XHRCycle.UPLOAD_LOADEND */;
383
- if (s.upload) {
384
- let l = ctx ? ctx.payload.size : 0;
385
- emitProgressEvent(xhr.upload, "loadend", l, l);
386
- }
387
- switch (type) {
388
- case "load": return ctx && ctx.data;
389
- case "abort":
390
- execAbort(xhr);
391
- break;
392
- case "error":
393
- execError(xhr);
394
- break;
395
- case "timeout":
396
- execTimeout(xhr);
397
- break;
398
- }
399
- }
400
- function execHeadersReceived(xhr, res) {
401
- let s = state(xhr);
402
- if (s.pos !== 2 /* XHRCycle.LOADSTART */ && s.pos !== 8 /* XHRCycle.UPLOAD_LOADEND */)
403
- return;
404
- s.pos = 9 /* XHRCycle.HEADERS_RECEIVED */;
405
- s.responseURL = s.requestURL;
406
- s.status = "statusCode" in res ? res.statusCode : "status" in res ? res.status : 200;
407
- s.responseHeaders = new HeadersP(("header" in res ? res.header : "headers" in res ? res.headers : {}));
408
- setReadyStateAndNotify(xhr, 2 /* HEADERS_RECEIVED */);
409
- execLoading(xhr, res);
410
- }
411
- function execLoading(xhr, res) {
412
- if (state(xhr).pos !== 9 /* XHRCycle.HEADERS_RECEIVED */)
413
- return;
414
- state(xhr).pos = 10 /* XHRCycle.LOADING */;
415
- setReadyStateAndNotify(xhr, 3 /* LOADING */);
416
- execDone(xhr, res);
417
- }
418
- function execDone(xhr, res) {
419
- let s = state(xhr);
420
- if (s.pos !== 10 /* XHRCycle.LOADING */)
421
- return;
422
- s.pos = 11 /* XHRCycle.DONE */;
423
- const setResponse = () => {
424
- var _a;
425
- s.response = getResponse(xhr.responseType, (_a = res.data) !== null && _a !== void 0 ? _a : "");
426
- if (!xhr.responseType || xhr.responseType === "text")
427
- s.responseText = xhr.response;
428
- setReadyStateAndNotify(xhr, 4 /* DONE */);
429
- execLoad(xhr);
430
- };
431
- try {
432
- setResponse();
433
- }
434
- catch (e) {
435
- execError(xhr);
436
- console.error(e);
437
- }
438
- }
439
- function execLoad(xhr) {
440
- let s = state(xhr);
441
- if (s.pos !== 11 /* XHRCycle.DONE */)
442
- return;
443
- s.pos = 12 /* XHRCycle.LOAD */;
444
- let lstr = s.responseHeaders.get("Content-Length");
445
- let contentLength = lstr ? (parseInt(lstr) || 0) : 0;
446
- emitProgressEvent(xhr, "load", contentLength, contentLength);
447
- execLoadend(xhr, contentLength);
448
- }
449
- function execAbort(xhr) {
450
- state(xhr).pos = 13 /* XHRCycle.ABORT */;
451
- setReadyStateAndNotify(xhr, 4 /* DONE */);
452
- emitProgressEvent(xhr, "abort");
453
- execLoadend(xhr);
454
- }
455
- function execError(xhr, err) {
456
- let s = state(xhr);
457
- switch (s.pos) {
458
- case 2 /* XHRCycle.LOADSTART */:
459
- case 8 /* XHRCycle.UPLOAD_LOADEND */:
460
- case 11 /* XHRCycle.DONE */:
461
- break;
462
- default:
463
- return;
464
- }
465
- s.pos = 14 /* XHRCycle.ERROR */;
466
- if (err) {
467
- s.status = 0;
468
- s.statusText = "errMsg" in err ? err.errMsg : "errorMessage" in err ? err.errorMessage : "";
469
- }
470
- setReadyStateAndNotify(xhr, 4 /* DONE */);
471
- emitProgressEvent(xhr, "error");
472
- execLoadend(xhr);
473
- }
474
- function execTimeout(xhr) {
475
- state(xhr).pos = 15 /* XHRCycle.TIMEOUT */;
476
- setReadyStateAndNotify(xhr, 4 /* DONE */);
477
- emitProgressEvent(xhr, "timeout");
478
- execLoadend(xhr);
479
- }
480
- function execLoadend(xhr, contentLength = 0) {
481
- let s = state(xhr);
482
- s.pos = 16 /* XHRCycle.LOADEND */;
483
- s.requestTask = null;
484
- clearRequestTimeout(s);
485
- emitProgressEvent(xhr, "loadend", contentLength, contentLength);
486
- }
487
- const XMLHttpRequestE = (typeof XMLHttpRequest !== "undefined" && XMLHttpRequest) || XMLHttpRequestImpl;
488
-
489
- export { XMLHttpRequestE as XMLHttpRequest, XMLHttpRequestImpl, setRequest };
@@ -1,26 +0,0 @@
1
- import { getPlatform } from './platform.js';
2
-
3
- /** @internal */
4
- function getConnectSocket() {
5
- let mp = getPlatform();
6
- return mp ? mp.connectSocket : function errorConnectSocket(options) {
7
- return {
8
- send(obj) { },
9
- close(obj) { },
10
- onOpen(listener) { },
11
- onMessage(listener) { },
12
- onError(listener) {
13
- if (typeof listener === "function") {
14
- listener({ errMsg: "NOT_SUPPORTED_ERR" });
15
- }
16
- },
17
- onClose(listener) {
18
- if (typeof listener === "function") {
19
- setTimeout(() => { listener({ code: 3009, reason: "NOT_SUPPORTED_ERR" }); });
20
- }
21
- },
22
- };
23
- };
24
- }
25
-
26
- export { getConnectSocket };
@@ -1,27 +0,0 @@
1
- /** @internal */
2
- function getPlatform() {
3
- let u = "undefined", r = "request", f = "function";
4
- let mp;
5
- mp =
6
- (typeof wx !== u && typeof (wx === null || wx === void 0 ? void 0 : wx[r]) === f && wx) || // 微信
7
- (typeof my !== u && typeof (my === null || my === void 0 ? void 0 : my[r]) === f && my) || // 支付宝
8
- (typeof qq !== u && typeof (qq === null || qq === void 0 ? void 0 : qq[r]) === f && qq) || // QQ
9
- (typeof jd !== u && typeof (jd === null || jd === void 0 ? void 0 : jd[r]) === f && jd) || // 京东
10
- (typeof swan !== u && typeof (swan === null || swan === void 0 ? void 0 : swan[r]) === f && swan) || // 百度
11
- (typeof tt !== u && typeof (tt === null || tt === void 0 ? void 0 : tt[r]) === f && tt) || // 抖音 | 飞书
12
- (typeof ks !== u && typeof (ks === null || ks === void 0 ? void 0 : ks[r]) === f && ks) || // 快手
13
- (typeof qh !== u && typeof (qh === null || qh === void 0 ? void 0 : qh[r]) === f && qh) || // 360
14
- (typeof xhs !== u && typeof (xhs === null || xhs === void 0 ? void 0 : xhs[r]) === f && xhs) || // 小红书
15
- undefined;
16
- if (typeof XMLHttpRequest === f && typeof WebSocket === f) {
17
- return;
18
- }
19
- if (mp === undefined)
20
- mp =
21
- (typeof uni !== u && typeof (uni === null || uni === void 0 ? void 0 : uni[r]) === f && uni) || // UniApp
22
- (typeof Taro !== u && typeof (Taro === null || Taro === void 0 ? void 0 : Taro[r]) === f && Taro) || // Taro
23
- undefined;
24
- return mp;
25
- }
26
-
27
- export { getPlatform };
@@ -1,34 +0,0 @@
1
- import { getPlatform } from './platform.js';
2
-
3
- /** @internal */
4
- function getRequest() {
5
- let mp = getPlatform();
6
- return mp ? mp.request : function errorRequest(options) {
7
- const errMsg = "NOT_SUPPORTED_ERR";
8
- const errno = 9;
9
- const err = {
10
- errMsg,
11
- errno,
12
- exception: {
13
- retryCount: 0,
14
- reasons: [{ errMsg, errno }],
15
- },
16
- useHttpDNS: false,
17
- };
18
- Promise.resolve(err)
19
- .then(err => { try {
20
- if (options.fail) {
21
- options.fail(err);
22
- }
23
- }
24
- catch (e) {
25
- console.error(e);
26
- } })
27
- .then(() => { if (options.complete) {
28
- options.complete(err);
29
- } });
30
- throw new ReferenceError("request is not defined");
31
- };
32
- }
33
-
34
- export { getRequest };