mphttpx 1.2.0-beta.1 → 1.2.0-beta.3
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/dist/cjs/AbortControllerP.js +31 -0
- package/dist/cjs/AbortSignalP.js +126 -0
- package/dist/cjs/BlobP.js +131 -0
- package/dist/cjs/BodyImpl.js +142 -0
- package/dist/cjs/CloseEventP.js +38 -0
- package/dist/cjs/CustomEventP.js +35 -0
- package/dist/cjs/EventP.js +173 -0
- package/dist/cjs/EventTargetP.js +176 -0
- package/dist/cjs/FileP.js +36 -0
- package/dist/cjs/FileReaderP.js +151 -0
- package/dist/cjs/FormDataP.js +250 -0
- package/dist/cjs/HeadersP.js +184 -0
- package/dist/cjs/MessageEventP.js +64 -0
- package/dist/cjs/ProgressEventP.js +69 -0
- package/dist/cjs/RequestP.js +150 -0
- package/dist/cjs/ResponseP.js +102 -0
- package/dist/cjs/TextDecoderP.js +186 -0
- package/dist/cjs/TextEncoderP.js +122 -0
- package/dist/cjs/URLSearchParamsP.js +230 -0
- package/dist/cjs/WebSocketP.js +238 -0
- package/dist/cjs/XMLHttpRequestP.js +563 -0
- package/dist/cjs/convertor.js +83 -0
- package/dist/cjs/fetchP.js +115 -0
- package/dist/cjs/index.js +80 -0
- package/dist/cjs/isPolyfill.js +56 -0
- package/dist/cjs/platform.js +33 -0
- package/dist/esm/AbortControllerP.js +28 -0
- package/dist/esm/AbortSignalP.js +121 -0
- package/dist/esm/BlobP.js +124 -0
- package/dist/esm/BodyImpl.js +137 -0
- package/dist/esm/CloseEventP.js +35 -0
- package/dist/esm/CustomEventP.js +32 -0
- package/dist/esm/EventP.js +165 -0
- package/dist/esm/EventTargetP.js +168 -0
- package/dist/esm/FileP.js +33 -0
- package/dist/esm/FileReaderP.js +148 -0
- package/dist/esm/FormDataP.js +245 -0
- package/dist/esm/HeadersP.js +177 -0
- package/dist/esm/MessageEventP.js +61 -0
- package/dist/esm/ProgressEventP.js +65 -0
- package/dist/esm/RequestP.js +146 -0
- package/dist/esm/ResponseP.js +98 -0
- package/dist/esm/TextDecoderP.js +183 -0
- package/dist/esm/TextEncoderP.js +119 -0
- package/dist/esm/URLSearchParamsP.js +227 -0
- package/dist/esm/WebSocketP.js +234 -0
- package/dist/esm/XMLHttpRequestP.js +559 -0
- package/dist/esm/convertor.js +80 -0
- package/dist/esm/fetchP.js +111 -0
- package/dist/esm/index.js +25 -0
- package/dist/esm/isPolyfill.js +48 -0
- package/dist/esm/platform.js +31 -0
- package/dist/index.d.ts +1 -5
- package/package.json +7 -10
- package/dist/index.cjs.js +0 -3252
- package/dist/index.cjs.min.js +0 -1
- package/dist/index.esm.js +0 -3212
- 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 './convertor.js';
|
|
8
|
+
import './BlobP.js';
|
|
9
|
+
import './TextEncoderP.js';
|
|
10
|
+
import './TextDecoderP.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
|
@@ -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;
|
|
@@ -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,23 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mphttpx",
|
|
3
|
-
"version": "1.2.0-beta.
|
|
3
|
+
"version": "1.2.0-beta.3",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"main": "dist/index.
|
|
6
|
-
"module": "dist/index.
|
|
5
|
+
"main": "dist/cjs/index.js",
|
|
6
|
+
"module": "dist/esm/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
-
"sideEffects": false,
|
|
9
8
|
"files": [
|
|
10
9
|
"README.md",
|
|
11
|
-
"dist
|
|
12
|
-
"dist/
|
|
13
|
-
"dist/
|
|
14
|
-
"dist/index.cjs.min.js",
|
|
15
|
-
"dist/index.esm.min.js"
|
|
10
|
+
"dist",
|
|
11
|
+
"!dist/cjs/types/**/*",
|
|
12
|
+
"!dist/esm/types/**/*"
|
|
16
13
|
],
|
|
17
14
|
"scripts": {
|
|
18
15
|
"build": "rollup --config",
|
|
19
16
|
"test": "node tests/test.js"
|
|
20
17
|
},
|
|
18
|
+
"sideEffects": false,
|
|
21
19
|
"author": "Xingzeng",
|
|
22
20
|
"license": "MIT",
|
|
23
21
|
"description": "A polyfill for mini-program.",
|
|
@@ -38,7 +36,6 @@
|
|
|
38
36
|
"WebSocket"
|
|
39
37
|
],
|
|
40
38
|
"devDependencies": {
|
|
41
|
-
"@rollup/plugin-terser": "^0.4.4",
|
|
42
39
|
"@rollup/plugin-typescript": "^12.3.0",
|
|
43
40
|
"cors": "^2.8.5",
|
|
44
41
|
"express": "^5.2.1",
|