mphttpx 1.1.0 → 1.2.0-beta.2

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 (59) hide show
  1. package/README.zh-CN.md +982 -0
  2. package/dist/cjs/AbortControllerP.js +31 -0
  3. package/dist/cjs/AbortSignalP.js +126 -0
  4. package/dist/cjs/BlobP.js +131 -0
  5. package/dist/cjs/BodyImpl.js +142 -0
  6. package/dist/cjs/CloseEventP.js +38 -0
  7. package/dist/cjs/CustomEventP.js +35 -0
  8. package/dist/cjs/EventP.js +173 -0
  9. package/dist/cjs/EventTargetP.js +176 -0
  10. package/dist/cjs/FileP.js +36 -0
  11. package/dist/cjs/FileReaderP.js +151 -0
  12. package/dist/cjs/FormDataP.js +250 -0
  13. package/dist/cjs/HeadersP.js +176 -0
  14. package/dist/cjs/MessageEventP.js +64 -0
  15. package/dist/cjs/ProgressEventP.js +69 -0
  16. package/dist/cjs/RequestP.js +158 -0
  17. package/dist/cjs/ResponseP.js +102 -0
  18. package/dist/cjs/TextDecoderP.js +186 -0
  19. package/dist/cjs/TextEncoderP.js +122 -0
  20. package/dist/cjs/URLSearchParamsP.js +230 -0
  21. package/dist/cjs/WebSocketP.js +238 -0
  22. package/dist/cjs/XMLHttpRequestP.js +567 -0
  23. package/dist/cjs/convertor.js +83 -0
  24. package/dist/cjs/fetchP.js +115 -0
  25. package/dist/cjs/index.js +80 -0
  26. package/dist/cjs/isPolyfill.js +56 -0
  27. package/dist/cjs/platform.js +33 -0
  28. package/dist/esm/AbortControllerP.js +28 -0
  29. package/dist/esm/AbortSignalP.js +121 -0
  30. package/dist/esm/BlobP.js +124 -0
  31. package/dist/esm/BodyImpl.js +137 -0
  32. package/dist/esm/CloseEventP.js +35 -0
  33. package/dist/esm/CustomEventP.js +32 -0
  34. package/dist/esm/EventP.js +165 -0
  35. package/dist/esm/EventTargetP.js +168 -0
  36. package/dist/esm/FileP.js +33 -0
  37. package/dist/esm/FileReaderP.js +148 -0
  38. package/dist/esm/FormDataP.js +245 -0
  39. package/dist/esm/HeadersP.js +170 -0
  40. package/dist/esm/MessageEventP.js +61 -0
  41. package/dist/esm/ProgressEventP.js +65 -0
  42. package/dist/esm/RequestP.js +153 -0
  43. package/dist/esm/ResponseP.js +98 -0
  44. package/dist/esm/TextDecoderP.js +183 -0
  45. package/dist/esm/TextEncoderP.js +119 -0
  46. package/dist/esm/URLSearchParamsP.js +227 -0
  47. package/dist/esm/WebSocketP.js +234 -0
  48. package/dist/esm/XMLHttpRequestP.js +563 -0
  49. package/dist/esm/convertor.js +80 -0
  50. package/dist/esm/fetchP.js +111 -0
  51. package/dist/esm/index.js +25 -0
  52. package/dist/esm/isPolyfill.js +48 -0
  53. package/dist/esm/platform.js +31 -0
  54. package/dist/index.d.ts +33 -37
  55. package/package.json +7 -9
  56. package/dist/index.cjs.js +0 -3291
  57. package/dist/index.cjs.min.js +0 -1
  58. package/dist/index.esm.js +0 -3251
  59. package/dist/index.esm.min.js +0 -1
@@ -0,0 +1,111 @@
1
+ import { XMLHttpRequest as XMLHttpRequestE } from './XMLHttpRequestP.js';
2
+ import { parseHeaders, normalizeName, normalizeValue } from './HeadersP.js';
3
+ import { Body_toPayload } from './BodyImpl.js';
4
+ import { RequestP, requestState as state } from './RequestP.js';
5
+ import { ResponseP, responseState as state$1 } from './ResponseP.js';
6
+ import { g, checkArgsLength, MPException, isObjectType, isPolyfillType } from './isPolyfill.js';
7
+ import './BlobP.js';
8
+ import './TextEncoderP.js';
9
+ import './TextDecoderP.js';
10
+ import './convertor.js';
11
+ import './FormDataP.js';
12
+ import './FileP.js';
13
+ import './EventTargetP.js';
14
+ import './EventP.js';
15
+ import './ProgressEventP.js';
16
+ import './platform.js';
17
+ import './AbortControllerP.js';
18
+ import './AbortSignalP.js';
19
+
20
+ const mp = { XMLHttpRequest: XMLHttpRequestE };
21
+ const setXMLHttpRequest = (XHR) => { mp.XMLHttpRequest = XHR; };
22
+ function fetchP(...args) {
23
+ if (new.target === fetchP) {
24
+ throw new TypeError("fetch is not a constructor");
25
+ }
26
+ const [input, init] = args;
27
+ checkArgsLength(args, 1, "Window", "fetch");
28
+ return new Promise((resolve, reject) => {
29
+ const request = new RequestP(input, init);
30
+ const signal = request[state].signal;
31
+ if (signal && signal.aborted) {
32
+ return reject(signal.reason);
33
+ }
34
+ let xhr = new mp.XMLHttpRequest();
35
+ xhr.onload = function () {
36
+ let options = {
37
+ headers: parseHeaders(xhr.getAllResponseHeaders() || ""),
38
+ status: xhr.status,
39
+ statusText: xhr.statusText,
40
+ };
41
+ // This check if specifically for when a user fetches a file locally from the file system
42
+ // Only if the status is out of a normal range
43
+ if (request.url.indexOf("file://") === 0 && (xhr.status < 200 || xhr.status > 599)) {
44
+ options.status = 200;
45
+ }
46
+ setTimeout(() => {
47
+ let response = new ResponseP("response" in xhr ? xhr.response : xhr.responseText, options);
48
+ response[state$1].url = "responseURL" in xhr ? xhr.responseURL : (options.headers.get("X-Request-URL") || "");
49
+ resolve(response);
50
+ });
51
+ };
52
+ xhr.onerror = function () {
53
+ setTimeout(function () {
54
+ reject(new TypeError("Failed to fetch"));
55
+ });
56
+ };
57
+ xhr.ontimeout = function () {
58
+ setTimeout(function () {
59
+ reject(new MPException("request:fail timeout", "TimeoutError"));
60
+ });
61
+ };
62
+ xhr.onabort = function () {
63
+ setTimeout(function () {
64
+ reject(new MPException("request:fail abort", "AbortError"));
65
+ });
66
+ };
67
+ xhr.open(request.method, request.url, true);
68
+ if (request.credentials === "include") {
69
+ xhr.withCredentials = true;
70
+ }
71
+ else if (request.credentials === "omit") {
72
+ xhr.withCredentials = false;
73
+ }
74
+ if ("responseType" in xhr) {
75
+ xhr.responseType = "arraybuffer";
76
+ }
77
+ if (init && typeof init === "object" && typeof init.headers === "object" && !(isObjectType("Headers", init.headers) || isPolyfillType("Headers", init.headers))) {
78
+ let headers = init.headers;
79
+ let names = [];
80
+ Object.getOwnPropertyNames(headers).forEach(name => {
81
+ names.push(normalizeName(name));
82
+ xhr.setRequestHeader(name, normalizeValue(headers[name]));
83
+ });
84
+ request.headers.forEach((value, name) => {
85
+ if (names.indexOf(name) === -1) {
86
+ xhr.setRequestHeader(name, value);
87
+ }
88
+ });
89
+ }
90
+ else {
91
+ request.headers.forEach((value, name) => {
92
+ xhr.setRequestHeader(name, value);
93
+ });
94
+ }
95
+ if (signal) {
96
+ const abortXHR = () => { xhr.abort(); };
97
+ signal.addEventListener("abort", abortXHR);
98
+ xhr.onreadystatechange = function () {
99
+ // success or failure
100
+ if (xhr.readyState === 4 /* DONE */) {
101
+ signal.removeEventListener("abort", abortXHR);
102
+ }
103
+ };
104
+ }
105
+ let body = Body_toPayload(request);
106
+ xhr.send(body !== "" ? body : void 0);
107
+ });
108
+ }
109
+ const fetchE = g["fetch"] || fetchP;
110
+
111
+ export { fetchE as fetch, fetchP, setXMLHttpRequest };
@@ -0,0 +1,25 @@
1
+ export { TextEncoder, TextEncoderP } from './TextEncoderP.js';
2
+ export { TextDecoder, TextDecoderP } from './TextDecoderP.js';
3
+ export { Blob, BlobP } from './BlobP.js';
4
+ export { File, FileP } from './FileP.js';
5
+ export { FileReader, FileReaderP } from './FileReaderP.js';
6
+ export { URLSearchParams, URLSearchParamsP } from './URLSearchParamsP.js';
7
+ export { FormData, FormDataP } from './FormDataP.js';
8
+ export { fetch, fetchP, setXMLHttpRequest } from './fetchP.js';
9
+ export { Headers, HeadersP } from './HeadersP.js';
10
+ export { Request, RequestP } from './RequestP.js';
11
+ export { Response, ResponseP } from './ResponseP.js';
12
+ export { AbortController, AbortControllerP } from './AbortControllerP.js';
13
+ export { AbortSignal, AbortSignalP } from './AbortSignalP.js';
14
+ export { EventTarget, EventTargetP } from './EventTargetP.js';
15
+ export { Event, EventP } from './EventP.js';
16
+ export { CustomEvent, CustomEventP } from './CustomEventP.js';
17
+ export { XMLHttpRequest, XMLHttpRequestImpl as XMLHttpRequestP, setRequest } from './XMLHttpRequestP.js';
18
+ export { WebSocket, WebSocketImpl as WebSocketP, setConnectSocket } from './WebSocketP.js';
19
+ export { g } from './isPolyfill.js';
20
+ export { mp } from './platform.js';
21
+ export { convert, convertBack } from './convertor.js';
22
+ export { BodyImpl } from './BodyImpl.js';
23
+ export { ProgressEvent, ProgressEventP } from './ProgressEventP.js';
24
+ export { CloseEvent, CloseEventP } from './CloseEventP.js';
25
+ export { MessageEvent, MessageEventP } from './MessageEventP.js';
@@ -0,0 +1,48 @@
1
+ /* eslint-disable no-prototype-builtins */
2
+ /** @internal */ const g = (typeof globalThis !== "undefined" && globalThis) ||
3
+ (typeof self !== "undefined" && self) ||
4
+ // @ts-ignore eslint-disable-next-line no-undef
5
+ (typeof global !== "undefined" && global) ||
6
+ {};
7
+ /** @internal */
8
+ const polyfill = "MPHTTPX";
9
+ /** @internal */
10
+ function checkArgsLength(args, required, className, funcName) {
11
+ if (args.length < required) {
12
+ throw new TypeError(`Failed to ${funcName ? ("execute '" + funcName + "' on") : "construct"} '${className}': ${required} argument${required > 1 ? "s" : ""} required, but only ${args.length} present.`);
13
+ }
14
+ }
15
+ /** @internal */
16
+ class MPException extends Error {
17
+ constructor(message, name) {
18
+ super(message);
19
+ this.name = "Error";
20
+ if (name) {
21
+ this.name = name;
22
+ }
23
+ }
24
+ }
25
+ /** @internal */
26
+ function isObjectType(name, value) {
27
+ return Object.prototype.toString.call(value) === `[object ${name}]`;
28
+ }
29
+ /** @internal */
30
+ function isPolyfillType(name, value, strict = false) {
31
+ return !!value
32
+ && typeof value === "object"
33
+ && "isPolyfill" in value
34
+ && !!value.isPolyfill
35
+ && typeof value.isPolyfill === "object"
36
+ && "symbol" in value.isPolyfill
37
+ && value.isPolyfill.symbol === polyfill
38
+ && "hierarchy" in value.isPolyfill
39
+ && Array.isArray(value.isPolyfill.hierarchy)
40
+ && ((index) => strict ? index === 0 : index > -1)(value.isPolyfill.hierarchy.indexOf(name));
41
+ }
42
+ /** @internal */
43
+ function isArrayBuffer(value) {
44
+ // Mini Program
45
+ return isObjectType("ArrayBuffer", value) || (!!value && typeof value === "object" && ArrayBuffer.prototype.isPrototypeOf(value));
46
+ }
47
+
48
+ export { MPException, checkArgsLength, g, isArrayBuffer, isObjectType, isPolyfillType, polyfill };
@@ -0,0 +1,31 @@
1
+ import { g } from './isPolyfill.js';
2
+
3
+ // @ts-nocheck
4
+ function getPlatform() {
5
+ let u = "undefined", r = "request", f = "function";
6
+ let mp;
7
+ mp =
8
+ (typeof wx !== u && typeof (wx === null || wx === void 0 ? void 0 : wx[r]) === f && wx) || // 微信
9
+ (typeof my !== u && typeof (my === null || my === void 0 ? void 0 : my[r]) === f && my) || // 支付宝
10
+ (typeof qq !== u && typeof (qq === null || qq === void 0 ? void 0 : qq[r]) === f && qq) || // QQ
11
+ (typeof jd !== u && typeof (jd === null || jd === void 0 ? void 0 : jd[r]) === f && jd) || // 京东
12
+ (typeof swan !== u && typeof (swan === null || swan === void 0 ? void 0 : swan[r]) === f && swan) || // 百度
13
+ (typeof tt !== u && typeof (tt === null || tt === void 0 ? void 0 : tt[r]) === f && tt) || // 抖音 | 飞书
14
+ (typeof ks !== u && typeof (ks === null || ks === void 0 ? void 0 : ks[r]) === f && ks) || // 快手
15
+ (typeof qh !== u && typeof (qh === null || qh === void 0 ? void 0 : qh[r]) === f && qh) || // 360
16
+ (typeof xhs !== u && typeof (xhs === null || xhs === void 0 ? void 0 : xhs[r]) === f && xhs) || // 小红书
17
+ undefined;
18
+ if (typeof g["XMLHttpRequest"] === f) {
19
+ return;
20
+ }
21
+ if (mp === undefined)
22
+ mp =
23
+ (typeof uni !== u && typeof (uni === null || uni === void 0 ? void 0 : uni[r]) === f && uni) || // UniApp
24
+ (typeof Taro !== u && typeof (Taro === null || Taro === void 0 ? void 0 : Taro[r]) === f && Taro) || // Taro
25
+ undefined;
26
+ return mp;
27
+ }
28
+ /** @internal */
29
+ const mp = getPlatform();
30
+
31
+ export { mp };
package/dist/index.d.ts CHANGED
@@ -63,15 +63,15 @@ declare const EventTargetE: {
63
63
  };
64
64
 
65
65
  declare class FileReaderP extends EventTargetP implements FileReader {
66
- static readonly EMPTY: 0;
67
- static readonly LOADING: 1;
68
- static readonly DONE: 2;
66
+ static get EMPTY(): 0;
67
+ static get LOADING(): 1;
68
+ static get DONE(): 2;
69
69
  constructor();
70
70
  get readyState(): 0 | 1 | 2;
71
71
  get result(): string | ArrayBuffer | null;
72
- readonly EMPTY: 0;
73
- readonly LOADING: 1;
74
- readonly DONE: 2;
72
+ get EMPTY(): 0;
73
+ get LOADING(): 1;
74
+ get DONE(): 2;
75
75
  get error(): (DOMException | null);
76
76
  abort(): void;
77
77
  readAsArrayBuffer(...args: [Blob]): void;
@@ -247,10 +247,10 @@ declare const AbortSignalE: {
247
247
  };
248
248
 
249
249
  declare class EventP implements Event {
250
- static readonly NONE: 0;
251
- static readonly CAPTURING_PHASE: 1;
252
- static readonly AT_TARGET: 2;
253
- static readonly BUBBLING_PHASE: 3;
250
+ static get NONE(): 0;
251
+ static get CAPTURING_PHASE(): 1;
252
+ static get AT_TARGET(): 2;
253
+ static get BUBBLING_PHASE(): 3;
254
254
  constructor(...args: [string, EventInit?]);
255
255
  get type(): string;
256
256
  get bubbles(): boolean;
@@ -259,10 +259,10 @@ declare class EventP implements Event {
259
259
  get target(): EventTarget | null;
260
260
  get currentTarget(): EventTarget | null;
261
261
  get eventPhase(): number;
262
- readonly NONE: 0;
263
- readonly CAPTURING_PHASE: 1;
264
- readonly AT_TARGET: 2;
265
- readonly BUBBLING_PHASE: 3;
262
+ get NONE(): 0;
263
+ get CAPTURING_PHASE(): 1;
264
+ get AT_TARGET(): 2;
265
+ get BUBBLING_PHASE(): 3;
266
266
  get srcElement(): EventTarget | null;
267
267
  get cancelBubble(): boolean;
268
268
  set cancelBubble(value: boolean);
@@ -315,17 +315,17 @@ declare class XMLHttpRequestEventTargetP extends EventTargetP implements XMLHttp
315
315
 
316
316
  declare const setRequest: (request: unknown) => void;
317
317
  declare class XMLHttpRequestImpl extends XMLHttpRequestEventTargetP implements XMLHttpRequest {
318
- static readonly UNSENT: 0;
319
- static readonly OPENED: 1;
320
- static readonly HEADERS_RECEIVED: 2;
321
- static readonly LOADING: 3;
322
- static readonly DONE: 4;
318
+ static get UNSENT(): 0;
319
+ static get OPENED(): 1;
320
+ static get HEADERS_RECEIVED(): 2;
321
+ static get LOADING(): 3;
322
+ static get DONE(): 4;
323
323
  constructor();
324
- readonly UNSENT: 0;
325
- readonly OPENED: 1;
326
- readonly HEADERS_RECEIVED: 2;
327
- readonly LOADING: 3;
328
- readonly DONE: 4;
324
+ get UNSENT(): 0;
325
+ get OPENED(): 1;
326
+ get HEADERS_RECEIVED(): 2;
327
+ get LOADING(): 3;
328
+ get DONE(): 4;
329
329
  get readyState(): number;
330
330
  get response(): any;
331
331
  get responseText(): string;
@@ -350,8 +350,6 @@ declare class XMLHttpRequestImpl extends XMLHttpRequestEventTargetP implements X
350
350
  get onreadystatechange(): ((this: XMLHttpRequest, ev: Event) => any) | null;
351
351
  set onreadystatechange(value: ((this: XMLHttpRequest, ev: Event) => any) | null);
352
352
  }
353
-
354
- declare const XMLHttpRequestP: typeof XMLHttpRequestImpl;
355
353
  declare const XMLHttpRequestE: {
356
354
  new (): XMLHttpRequest;
357
355
  prototype: XMLHttpRequest;
@@ -364,15 +362,15 @@ declare const XMLHttpRequestE: {
364
362
 
365
363
  declare const setConnectSocket: (connectSocket: unknown) => void;
366
364
  declare class WebSocketImpl extends EventTargetP implements WebSocket {
367
- static readonly CONNECTING: 0;
368
- static readonly OPEN: 1;
369
- static readonly CLOSING: 2;
370
- static readonly CLOSED: 3;
365
+ static get CONNECTING(): 0;
366
+ static get OPEN(): 1;
367
+ static get CLOSING(): 2;
368
+ static get CLOSED(): 3;
371
369
  constructor(...args: [string | URL, (string | string[])?]);
372
- readonly CONNECTING: 0;
373
- readonly OPEN: 1;
374
- readonly CLOSING: 2;
375
- readonly CLOSED: 3;
370
+ get CONNECTING(): 0;
371
+ get OPEN(): 1;
372
+ get CLOSING(): 2;
373
+ get CLOSED(): 3;
376
374
  get binaryType(): BinaryType;
377
375
  set binaryType(value: BinaryType);
378
376
  get bufferedAmount(): number;
@@ -391,8 +389,6 @@ declare class WebSocketImpl extends EventTargetP implements WebSocket {
391
389
  get onopen(): ((this: WebSocket, ev: Event) => any) | null;
392
390
  set onopen(value: ((this: WebSocket, ev: Event) => any) | null);
393
391
  }
394
-
395
- declare const WebSocketP: typeof WebSocketImpl;
396
392
  declare const WebSocketE: {
397
393
  new (url: string | URL, protocols?: string | string[]): WebSocket;
398
394
  prototype: WebSocket;
@@ -402,4 +398,4 @@ declare const WebSocketE: {
402
398
  readonly CLOSED: 3;
403
399
  };
404
400
 
405
- export { AbortControllerE as AbortController, AbortControllerP, AbortSignalE as AbortSignal, AbortSignalP, BlobE as Blob, BlobP, CustomEventE as CustomEvent, CustomEventP, EventE as Event, EventP, EventTargetE as EventTarget, EventTargetP, FileE as File, FileP, FileReaderE as FileReader, FileReaderP, FormDataE as FormData, FormDataP, HeadersE as Headers, HeadersP, RequestE as Request, RequestP, ResponseE as Response, ResponseP, TextDecoderE as TextDecoder, TextDecoderP, TextEncoderE as TextEncoder, TextEncoderP, URLSearchParamsE as URLSearchParams, URLSearchParamsP, WebSocketE as WebSocket, WebSocketP, XMLHttpRequestE as XMLHttpRequest, XMLHttpRequestP, fetchE as fetch, fetchP, setConnectSocket, setRequest, setXMLHttpRequest };
401
+ export { AbortControllerE as AbortController, AbortControllerP, AbortSignalE as AbortSignal, AbortSignalP, BlobE as Blob, BlobP, CustomEventE as CustomEvent, CustomEventP, EventE as Event, EventP, EventTargetE as EventTarget, EventTargetP, FileE as File, FileP, FileReaderE as FileReader, FileReaderP, FormDataE as FormData, FormDataP, HeadersE as Headers, HeadersP, RequestE as Request, RequestP, ResponseE as Response, ResponseP, TextDecoderE as TextDecoder, TextDecoderP, TextEncoderE as TextEncoder, TextEncoderP, URLSearchParamsE as URLSearchParams, URLSearchParamsP, WebSocketE as WebSocket, WebSocketImpl as WebSocketP, XMLHttpRequestE as XMLHttpRequest, XMLHttpRequestImpl as XMLHttpRequestP, fetchE as fetch, fetchP, setConnectSocket, setRequest, setXMLHttpRequest };
package/package.json CHANGED
@@ -1,22 +1,21 @@
1
1
  {
2
2
  "name": "mphttpx",
3
- "version": "1.1.0",
3
+ "version": "1.2.0-beta.2",
4
4
  "type": "module",
5
- "main": "dist/index.cjs.js",
6
- "module": "dist/index.esm.js",
5
+ "main": "dist/cjs/index.js",
6
+ "module": "dist/esm/index.js",
7
7
  "types": "dist/index.d.ts",
8
8
  "files": [
9
9
  "README.md",
10
- "dist/index.d.ts",
11
- "dist/index.cjs.js",
12
- "dist/index.esm.js",
13
- "dist/index.cjs.min.js",
14
- "dist/index.esm.min.js"
10
+ "dist",
11
+ "!dist/cjs/types/**/*",
12
+ "!dist/esm/types/**/*"
15
13
  ],
16
14
  "scripts": {
17
15
  "build": "rollup --config",
18
16
  "test": "node tests/test.js"
19
17
  },
18
+ "sideEffects": false,
20
19
  "author": "Xingzeng",
21
20
  "license": "MIT",
22
21
  "description": "A polyfill for mini-program.",
@@ -37,7 +36,6 @@
37
36
  "WebSocket"
38
37
  ],
39
38
  "devDependencies": {
40
- "@rollup/plugin-terser": "^0.4.4",
41
39
  "@rollup/plugin-typescript": "^12.3.0",
42
40
  "cors": "^2.8.5",
43
41
  "express": "^5.2.1",