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,94 +0,0 @@
1
- import { encode } from './encode.js';
2
- import { decode } from './decode.js';
3
- import { BlobP } from '../file-system/BlobP.js';
4
- import { isBlob } from './isBlob.js';
5
- import { isPolyfillType } from '../utils.js';
6
- import { isArrayBuffer } from './isArrayBuffer.js';
7
- import { isURLSearchParams } from './isURLSearchParams.js';
8
-
9
- /** @internal */
10
- class Payload {
11
- constructor(body) {
12
- this.type = "";
13
- if (typeof body === "string") {
14
- this.promise = Promise.resolve(body);
15
- this.type = "text/plain;charset=UTF-8";
16
- this.calcLength = () => encode(body).length;
17
- }
18
- else if (isURLSearchParams(body)) {
19
- let _body = body.toString();
20
- this.promise = Promise.resolve(_body);
21
- this.type = "application/x-www-form-urlencoded;charset=UTF-8";
22
- this.calcLength = () => encode(_body).length;
23
- }
24
- else if (isArrayBuffer(body)) {
25
- this.promise = Promise.resolve(body.slice(0));
26
- this.length = body.byteLength;
27
- }
28
- else if (ArrayBuffer.isView(body)) {
29
- let _body = body.buffer.slice(body.byteOffset, body.byteOffset + body.byteLength);
30
- this.promise = Promise.resolve(_body);
31
- this.length = _body.byteLength;
32
- }
33
- else if (isBlob(body)) {
34
- this.promise = body.arrayBuffer();
35
- this.type = body.type;
36
- this.length = body.size;
37
- }
38
- else if (isFormData(body)) {
39
- let _body = FormData_toBlob(body);
40
- this.promise = _body.arrayBuffer();
41
- this.type = _body.type;
42
- this.length = _body.size;
43
- }
44
- else if (body === null || body === undefined) {
45
- this.promise = Promise.resolve("");
46
- this.length = 0;
47
- }
48
- else {
49
- let _body = "" + body;
50
- this.promise = Promise.resolve(_body);
51
- this.type = "text/plain;charset=UTF-8";
52
- this.calcLength = () => encode(_body).length;
53
- }
54
- }
55
- get size() {
56
- var _a;
57
- if (typeof this.length !== "number" && this.calcLength)
58
- this.length = this.calcLength();
59
- return (_a = this.length) !== null && _a !== void 0 ? _a : 0;
60
- }
61
- text() { return this.promise.then(r => typeof r === "string" ? r : decode(r)); }
62
- arrayBuffer() { return this.promise.then(r => isArrayBuffer(r) ? r : encode(r).buffer); }
63
- }
64
- function isFormData(value) {
65
- return isPolyfillType("FormData", value) || isExternalFormData(value);
66
- }
67
- function isExternalFormData(value) {
68
- let expect = "[object FormData]";
69
- return (Object.prototype.toString.call(value) === expect || String(value) === expect)
70
- && "forEach" in value
71
- && typeof value.forEach === "function";
72
- }
73
- function FormData_toBlob(formData) {
74
- const boundary = "----formdata-mphttpx-" + Math.random();
75
- const p = `--${boundary}\r\nContent-Disposition: form-data; name="`;
76
- let chunks = [];
77
- formData.forEach((value, name) => {
78
- typeof value === "string"
79
- ? chunks.push(p + escape(normalizeLinefeeds(name)) + `"\r\n\r\n${normalizeLinefeeds(value)}\r\n`)
80
- : chunks.push(p + escape(normalizeLinefeeds(name)) + `"; filename="${escape(value.name)}"\r\nContent-Type: ${value.type || "application/octet-stream"}\r\n\r\n`, value, `\r\n`);
81
- });
82
- chunks.push(`--${boundary}--`);
83
- return new BlobP(chunks, { type: "multipart/form-data; boundary=" + boundary });
84
- }
85
- // normalize line feeds for textarea
86
- // https://html.spec.whatwg.org/multipage/form-elements.html#textarea-line-break-normalisation-transformation
87
- function normalizeLinefeeds(value) {
88
- return value.replace(/\r?\n|\r/g, "\r\n");
89
- }
90
- function escape(str) {
91
- return str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22');
92
- }
93
-
94
- export { Payload };
@@ -1,9 +0,0 @@
1
- import { TextDecoderP } from '../encoding/TextDecoderP.js';
2
-
3
- /** @internal */
4
- function decode(buf) {
5
- let decoder = new TextDecoderP();
6
- return decoder.decode(buf);
7
- }
8
-
9
- export { decode };
@@ -1,12 +0,0 @@
1
- import { EventTarget_fire } from '../event-system/EventTargetP.js';
2
- import { EventP, Event_setTrusted } from '../event-system/EventP.js';
3
-
4
- /** @internal */
5
- function emitEvent(target, type) {
6
- let event = new EventP(type);
7
- event.__Event__.target = target;
8
- Event_setTrusted(event, true);
9
- EventTarget_fire(target, event);
10
- }
11
-
12
- export { emitEvent };
@@ -1,17 +0,0 @@
1
- import { Event_setTrusted } from '../event-system/EventP.js';
2
- import { EventTarget_fire } from '../event-system/EventTargetP.js';
3
- import { ProgressEventP } from '../event-system/ProgressEventP.js';
4
-
5
- /** @internal */
6
- function emitProgressEvent(target, type, loaded = 0, total = 0) {
7
- let event = new ProgressEventP(type, {
8
- lengthComputable: total > 0,
9
- loaded,
10
- total,
11
- });
12
- event.__Event__.target = target;
13
- Event_setTrusted(event, true);
14
- EventTarget_fire(target, event);
15
- }
16
-
17
- export { emitProgressEvent };
@@ -1,9 +0,0 @@
1
- import { TextEncoderP } from '../encoding/TextEncoderP.js';
2
-
3
- /** @internal */
4
- function encode(str) {
5
- let encoder = new TextEncoderP();
6
- return encoder.encode(str);
7
- }
8
-
9
- export { encode };
@@ -1,20 +0,0 @@
1
- import { EventTargetP } from '../event-system/EventTargetP.js';
2
-
3
- /** @internal */
4
- function attachFn(target, handlers) {
5
- return function attach(type) {
6
- const fnName = ("on" + type);
7
- const callback = target[fnName];
8
- const listener = handlers[fnName];
9
- typeof callback === "function"
10
- ? EventTargetP.prototype.addEventListener.call(target, type, listener)
11
- : EventTargetP.prototype.removeEventListener.call(target, type, listener);
12
- };
13
- }
14
- /** @internal */
15
- function executeFn(target, cb, ev) {
16
- if (typeof cb === "function")
17
- cb.call(target, ev);
18
- }
19
-
20
- export { attachFn, executeFn };
@@ -1,9 +0,0 @@
1
- import { isObjectType } from '../utils.js';
2
-
3
- /** @internal */
4
- function isArrayBuffer(value) {
5
- // Mini Program
6
- return isObjectType("ArrayBuffer", value) || (!!value && typeof value === "object" && ArrayBuffer.prototype.isPrototypeOf(value));
7
- }
8
-
9
- export { isArrayBuffer };
@@ -1,18 +0,0 @@
1
- import { isPolyfillType } from '../utils.js';
2
-
3
- /** @internal */
4
- function isBlob(value, strict = false) {
5
- return isPolyfillType("Blob", value, strict) || isExternalBlob(value, strict);
6
- }
7
- function isExternalBlob(value, strict = false) {
8
- let expects = ["[object Blob]"];
9
- if (!strict)
10
- expects.push("[object File]");
11
- return (expects.indexOf(Object.prototype.toString.call(value)) > -1 || expects.indexOf(String(value)) > -1)
12
- && "size" in value
13
- && typeof value.size === "number"
14
- && "arrayBuffer" in value
15
- && typeof value.arrayBuffer === "function";
16
- }
17
-
18
- export { isBlob };
@@ -1,16 +0,0 @@
1
- import { isPolyfillType } from '../utils.js';
2
-
3
- /** @internal */
4
- function isEventTarget(value) {
5
- return isPolyfillType("EventTarget", value) || isExternalEventTarget(value);
6
- }
7
- function isExternalEventTarget(value) {
8
- return !!value
9
- && typeof value === "object"
10
- && "addEventListener" in value
11
- && typeof value.addEventListener === "function"
12
- && "removeEventListener" in value
13
- && typeof value.removeEventListener === "function";
14
- }
15
-
16
- export { isEventTarget };
@@ -1,11 +0,0 @@
1
- /** @internal */
2
- function isSequence(value) {
3
- return Array.isArray(value) || (!!value
4
- && typeof value === "object"
5
- && typeof Symbol === "function"
6
- && Symbol.iterator
7
- && Symbol.iterator in value
8
- && typeof value[Symbol.iterator] === "function");
9
- }
10
-
11
- export { isSequence };
@@ -1,8 +0,0 @@
1
- import { isPolyfillType, isObjectType } from '../utils.js';
2
-
3
- /** @internal */
4
- function isURLSearchParams(value) {
5
- return isPolyfillType("URLSearchParams", value) || isObjectType("URLSearchParams", value);
6
- }
7
-
8
- export { isURLSearchParams };
@@ -1,9 +0,0 @@
1
- // HTTP methods whose capitalization should be normalized
2
- const methods = ["CONNECT", "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT", "TRACE"];
3
- /** @internal */
4
- function normalizeMethod(method) {
5
- let upcased = method.toUpperCase();
6
- return methods.indexOf(upcased) > -1 ? upcased : method;
7
- }
8
-
9
- export { normalizeMethod };
@@ -1,66 +0,0 @@
1
- /** @internal */
2
- const statusTextMap = {
3
- 100: "Continue",
4
- 101: "Switching Protocols",
5
- 102: "Processing",
6
- 103: "Early Hints",
7
- 200: "OK",
8
- 201: "Created",
9
- 202: "Accepted",
10
- 203: "Non-Authoritative Information",
11
- 204: "No Content",
12
- 205: "Reset Content",
13
- 206: "Partial Content",
14
- 207: "Multi-Status",
15
- 208: "Already Reported",
16
- 226: "IM Used",
17
- 300: "Multiple Choices",
18
- 301: "Moved Permanently",
19
- 302: "Found",
20
- 303: "See Other",
21
- 304: "Not Modified",
22
- 307: "Temporary Redirect",
23
- 308: "Permanent Redirect",
24
- 400: "Bad Request",
25
- 401: "Unauthorized",
26
- 402: "Payment Required",
27
- 403: "Forbidden",
28
- 404: "Not Found",
29
- 405: "Method Not Allowed",
30
- 406: "Not Acceptable",
31
- 407: "Proxy Authentication Required",
32
- 408: "Request Timeout",
33
- 409: "Conflict",
34
- 410: "Gone",
35
- 411: "Length Required",
36
- 412: "Precondition Failed",
37
- 413: "Content Too Large",
38
- 414: "URI Too Long",
39
- 415: "Unsupported Media Type",
40
- 416: "Range Not Satisfiable",
41
- 417: "Expectation Failed",
42
- 418: "I'm a teapot",
43
- 421: "Misdirected Request",
44
- 422: "Unprocessable Entity",
45
- 423: "Locked",
46
- 424: "Failed Dependency",
47
- 425: "Too Early",
48
- 426: "Upgrade Required",
49
- 428: "Precondition Required",
50
- 429: "Too Many Requests",
51
- 431: "Request Header Fields Too Large",
52
- 451: "Unavailable For Legal Reasons",
53
- 500: "Internal Server Error",
54
- 501: "Not Implemented",
55
- 502: "Bad Gateway",
56
- 503: "Service Unavailable",
57
- 504: "Gateway Timeout",
58
- 505: "HTTP Version Not Supported",
59
- 506: "Variant Also Negotiates",
60
- 507: "Insufficient Storage",
61
- 508: "Loop Detected",
62
- 510: "Not Extended",
63
- 511: "Network Authentication Required"
64
- };
65
-
66
- export { statusTextMap };
@@ -1,26 +0,0 @@
1
- /** @internal */
2
- function Uint8Array_toBase64(input) {
3
- let byteToCharMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
4
- let output = [];
5
- for (var i = 0; i < input.length; i += 3) {
6
- let byte1 = input[i];
7
- let haveByte2 = i + 1 < input.length;
8
- let byte2 = haveByte2 ? input[i + 1] : 0;
9
- let haveByte3 = i + 2 < input.length;
10
- let byte3 = haveByte3 ? input[i + 2] : 0;
11
- let outByte1 = byte1 >> 2;
12
- let outByte2 = ((byte1 & 0x03) << 4) | (byte2 >> 4);
13
- let outByte3 = ((byte2 & 0x0F) << 2) | (byte3 >> 6);
14
- let outByte4 = byte3 & 0x3F;
15
- if (!haveByte3) {
16
- outByte4 = 64;
17
- if (!haveByte2) {
18
- outByte3 = 64;
19
- }
20
- }
21
- output.push(byteToCharMap[outByte1], byteToCharMap[outByte2], byteToCharMap[outByte3], byteToCharMap[outByte4]);
22
- }
23
- return output.join("");
24
- }
25
-
26
- export { Uint8Array_toBase64 };
@@ -1,193 +0,0 @@
1
- import { BlobP } from '../file-system/BlobP.js';
2
- import { Payload } from '../helpers/Payload.js';
3
- import { HeadersP } from '../fetch-api/HeadersP.js';
4
- import { CloseEventP } from '../event-system/CloseEventP.js';
5
- import { MessageEventP } from '../event-system/MessageEventP.js';
6
- import { emitEvent } from '../helpers/emitEvent.js';
7
- import { attachFn, executeFn } from '../helpers/handlers.js';
8
- import { Event_setTrusted } from '../event-system/EventP.js';
9
- import { EventTargetP, EventTarget_fire } from '../event-system/EventTargetP.js';
10
- import { isSequence } from '../helpers/isSequence.js';
11
- import { isArrayBuffer } from '../helpers/isArrayBuffer.js';
12
- import { getConnectSocket } from './connectSocket.js';
13
- import { checkArgsLength, setState, DOMExceptionP, SymbolP } from '../utils.js';
14
-
15
- const mp = { connectSocket: getConnectSocket() };
16
- function setConnectSocket(connectSocket) { mp.connectSocket = connectSocket; }
17
- class WebSocketImpl extends EventTargetP {
18
- static get CONNECTING() { return 0; }
19
- static get OPEN() { return 1; }
20
- static get CLOSING() { return 2; }
21
- static get CLOSED() { return 3; }
22
- constructor(url, protocols) {
23
- checkArgsLength(arguments.length, 1, "WebSocket");
24
- super();
25
- const targetURL = "" + url;
26
- const socketTask = mp.connectSocket({
27
- url: targetURL,
28
- protocols: protocols !== undefined
29
- ? isSequence(protocols)
30
- ? Array.isArray(protocols) ? protocols : Array.from(protocols)
31
- : ["" + protocols]
32
- : [],
33
- multiple: true, // Alipay Mini Program
34
- fail(err) { console.error(err); },
35
- });
36
- setState(this, "__WebSocket__", new WebSocketState(this, socketTask));
37
- state(this).url = targetURL;
38
- if (socketTask && typeof socketTask === "object") {
39
- onOpen(this);
40
- onClose(this);
41
- onError(this);
42
- onMessage(this);
43
- }
44
- else {
45
- throw new Error(`connectSocket can't establish a connection to the server at ${"" + url}.`);
46
- }
47
- }
48
- get CONNECTING() { return 0; }
49
- get OPEN() { return 1; }
50
- get CLOSING() { return 2; }
51
- get CLOSED() { return 3; }
52
- get binaryType() { return state(this).binaryType; }
53
- set binaryType(value) { if (value === "blob" || value === "arraybuffer") {
54
- state(this).binaryType = value;
55
- } }
56
- get bufferedAmount() { return state(this).bufferedAmount; }
57
- get extensions() { return ""; }
58
- get protocol() { return state(this).protocol; }
59
- get readyState() { return state(this).readyState; }
60
- get url() { return state(this).url; }
61
- close(code, reason) {
62
- let s = state(this);
63
- if (this.readyState === 2 /* CLOSING */ || this.readyState === 3 /* CLOSED */)
64
- return;
65
- s.readyState = 2 /* CLOSING */;
66
- s.socketTask.close({
67
- code: code,
68
- reason: reason,
69
- fail(err) { console.error(err); },
70
- complete: () => { s.readyState = 3 /* CLOSED */; },
71
- });
72
- }
73
- send(data) {
74
- checkArgsLength(arguments.length, 1, "WebSocket", "send");
75
- if (this.readyState === 0 /* CONNECTING */) {
76
- throw new DOMExceptionP("Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.", "InvalidStateError");
77
- }
78
- if (this.readyState === 2 /* CLOSING */ || this.readyState === 3 /* CLOSED */) {
79
- return console.error("WebSocket is already in CLOSING or CLOSED state.");
80
- }
81
- const transfer = (function (data) {
82
- if (this.readyState !== 1 /* OPEN */)
83
- return;
84
- state(this).socketTask.send({ data, fail(err) { console.error(err); } });
85
- }).bind(this);
86
- let payload = new Payload(data !== null && data !== void 0 ? data : "" + data);
87
- payload.promise.then(transfer);
88
- }
89
- get onclose() { return state(this).onclose; }
90
- set onclose(value) { state(this).onclose = value; state(this).attach("close"); }
91
- get onerror() { return state(this).onerror; }
92
- set onerror(value) { state(this).onerror = value; state(this).attach("error"); }
93
- get onmessage() { return state(this).onmessage; }
94
- set onmessage(value) { state(this).onmessage = value; state(this).attach("message"); }
95
- get onopen() { return state(this).onopen; }
96
- set onopen(value) { state(this).onopen = value; state(this).attach("open"); }
97
- /** @internal */ toString() { return "[object WebSocket]"; }
98
- /** @internal */ get [SymbolP.toStringTag]() { return "WebSocket"; }
99
- /** @internal */ get __MPHTTPX__() { return { chain: ["WebSocket", "EventTarget"] }; }
100
- }
101
- /** @internal */
102
- class WebSocketState {
103
- constructor(target, socketTask) {
104
- this.binaryType = "blob";
105
- this.bufferedAmount = 0;
106
- this.protocol = "";
107
- this.readyState = 0;
108
- this.url = "";
109
- this.error = null;
110
- this.onclose = null;
111
- this.onerror = null;
112
- this.onmessage = null;
113
- this.onopen = null;
114
- this.socketTask = socketTask;
115
- this.attach = attachFn(target, getHandlers(target));
116
- }
117
- }
118
- function getHandlers(t) {
119
- return {
120
- onclose: (ev) => { executeFn(t, t.onclose, ev); },
121
- onerror: (ev) => { executeFn(t, t.onerror, ev); },
122
- onmessage: (ev) => { executeFn(t, t.onmessage, ev); },
123
- onopen: (ev) => { executeFn(t, t.onopen, ev); },
124
- };
125
- }
126
- function state(target) {
127
- return target.__WebSocket__;
128
- }
129
- function onOpen(socket) {
130
- let s = state(socket);
131
- s.socketTask.onOpen(res => {
132
- if ("header" in res && res.header && typeof res.header === "object") {
133
- let headers = new HeadersP(res.header);
134
- s.protocol = headers.get("Sec-WebSocket-Protocol") || "";
135
- }
136
- s.readyState = 1 /* OPEN */;
137
- emitEvent(socket, "open");
138
- });
139
- }
140
- function onClose(socket) {
141
- let s = state(socket);
142
- s.socketTask.onClose(res => {
143
- s.readyState = 3 /* CLOSED */;
144
- let event = new CloseEventP("close", {
145
- wasClean: !s.error,
146
- code: res.code,
147
- reason: res.reason,
148
- });
149
- Event_setTrusted(event, true);
150
- EventTarget_fire(socket, event);
151
- });
152
- }
153
- function onError(socket) {
154
- let s = state(socket);
155
- s.socketTask.onError(res => {
156
- console.error(res);
157
- s.error = res;
158
- s.readyState = 3 /* CLOSED */;
159
- emitEvent(socket, "error");
160
- });
161
- }
162
- function onMessage(socket) {
163
- state(socket).socketTask.onMessage(res => {
164
- let data = res.data;
165
- let _data;
166
- // Alipay Mini Program
167
- if (data && typeof data === "object" && "data" in data) {
168
- _data = data.data;
169
- if ("isBuffer" in data && data.isBuffer && typeof _data === "string") {
170
- // @ts-ignore
171
- try {
172
- _data = my.base64ToArrayBuffer(_data);
173
- }
174
- catch (e) { }
175
- }
176
- }
177
- else {
178
- _data = data;
179
- }
180
- if (isArrayBuffer(_data) && socket.binaryType === "blob") {
181
- _data = new BlobP([_data]);
182
- }
183
- let event = new MessageEventP("message", {
184
- data: _data,
185
- origin: socket.url,
186
- });
187
- Event_setTrusted(event, true);
188
- EventTarget_fire(socket, event);
189
- });
190
- }
191
- const WebSocketE = (typeof WebSocket !== "undefined" && WebSocket) || WebSocketImpl;
192
-
193
- export { WebSocketE as WebSocket, WebSocketImpl, setConnectSocket };