mphttpx 2.4.0 → 2.6.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # MPHTTPX
2
2
 
3
- 小程序中浏览器网络 API 的完整 polyfill 实现,一次安装即可使用 `fetch`、`XMLHttpRequest`、`WebSocket` 等标准接口,让小程序开发不再「小」。
3
+ 小程序中浏览器网络 API 的完整 polyfill 实现,一次安装即可使用 `fetch`、`XMLHttpRequest`、`WebSocket` 等符合 W3C 标准的应用程序编程接口。
4
4
 
5
5
  ---
6
6
 
@@ -33,11 +33,11 @@ fetch("https://api.example.com/data")
33
33
  | `XMLHttpRequest` | miniprogram-xmlhttprequest-shim |
34
34
  | `WebSocket` | miniprogram-websocket |
35
35
 
36
- 每个 API 都有对应的 `P` 后缀版本(如 `fetchP`、`BlobP`、`TextEncoderP`),始终返回 polyfill 实现,不会回退至浏览器原生 API。
36
+ 每个 API 都有对应的 `P` 后缀版本(如 `fetchP`、`BlobP`、`TextEncoderP`),始终返回 polyfill 实现,请按需使用。
37
37
 
38
38
  ```javascript
39
- import { fetch, Blob } from "mphttpx"; // 优先返回全局对象,不存在时返回 polyfill
40
- import { fetchP, BlobP } from "mphttpx"; // 始终返回 polyfill 实现
39
+ import { fetch, Blob } from "mphttpx"; // 优先返回全局对象,不存在时返回 polyfill
40
+ import { fetchP, BlobP } from "mphttpx"; // 始终返回 polyfill 实现
41
41
  ```
42
42
 
43
43
  ## 依赖库
@@ -48,7 +48,7 @@ import { fetchP, BlobP } from "mphttpx"; // 始终返回 polyfill 实现
48
48
  | [miniprogram-xmlhttprequest-shim](https://www.npmjs.com/package/miniprogram-xmlhttprequest-shim) | XMLHttpRequest、<br>Cookie、enableCookie |
49
49
  | [miniprogram-websocket](https://www.npmjs.com/package/miniprogram-websocket) | WebSocket |
50
50
 
51
- 各 API 的详细文档和兼容性说明及更多功能请参阅对应库的 README。
51
+ 各 API 的详细文档及兼容性说明请参阅对应库的自述文档。
52
52
 
53
53
  ## 兼容性
54
54
 
@@ -56,34 +56,11 @@ import { fetchP, BlobP } from "mphttpx"; // 始终返回 polyfill 实现
56
56
  | :---: | :----: | :---: | :---: | :---: | :---: | :---: | :----: |
57
57
  | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
58
58
 
59
- 在 Chrome、Firefox、Edge、Safari 等浏览器中,所有导出均直接使用浏览器原生实现,无额外开销。
60
-
61
- ## 自动导入
62
-
63
- 配合 [unplugin-auto-import](https://www.npmjs.com/package/unplugin-auto-import) 使用:
64
-
65
- ```javascript
66
- AutoImport({
67
- imports: [{
68
- "mphttpx": [
69
- "fetch", "Headers", "Request", "Response",
70
- "Blob", "File", "FileReader",
71
- "URLSearchParams", "FormData",
72
- "AbortController", "AbortSignal",
73
- "EventTarget", "Event", "CustomEvent",
74
- "TextEncoder", "TextDecoder",
75
-
76
- "XMLHttpRequest", // mini-programs
77
- "WebSocket", // mini-programs
78
- ],
79
- }],
80
- });
81
- ```
59
+ 在 Chrome、Firefox、Edge、Safari 等浏览器中,所有导出均直接使用浏览器原生实现,无性能损耗。
82
60
 
83
61
  ## 注意事项
84
62
 
85
- - **UniApp (HBuilderX Vue2)**:可能需要安装较低版本的 `unplugin-auto-import`(如 `0.16.7`)以兼容 CMD 模块格式。
86
- - **支付宝小程序**:`globalThis`、`window`、`XMLHttpRequest` 等为保留字,导入时建议重命名:`import { XMLHttpRequest as myXHR } from "mphttpx";`。
63
+ - **支付宝小程序**:`fetch`、`XMLHttpRequest`、`WebSocket` 等为保留字,导入时建议重命名:`import { fetch as fetchPolyfill } from "mphttpx";`。
87
64
 
88
65
  ## 开源协议
89
66
 
@@ -174,6 +174,14 @@ function setState(target, name, value) {
174
174
  value: value
175
175
  });
176
176
  }
177
+ // Safe wrapper around String() for type-like representations (e.g. "[object Blob]"). Axios
178
+ function typeString(value) {
179
+ try {
180
+ return String(value);
181
+ } catch (e) {
182
+ return "[object Unknown]";
183
+ }
184
+ }
177
185
  function isObjectType(name, value) {
178
186
  return Object.prototype.toString.call(value) === "[object ".concat(name, "]");
179
187
  }
@@ -572,7 +580,7 @@ function concatString(val) {
572
580
  }
573
581
  // Mini Program
574
582
  function isOtherArrayBuffer(value) {
575
- return (isObjectType("ArrayBuffer", value) || String(value) === "[object ArrayBuffer]") && !!value && _typeof(value) === "object" && "byteLength" in value && typeof value.byteLength === "number";
583
+ return !!value && _typeof(value) === "object" && "byteLength" in value && typeof value.byteLength === "number" && (isObjectType("ArrayBuffer", value) || typeString(value) === "[object ArrayBuffer]");
576
584
  }
577
585
  function isArrayBuffer(value) {
578
586
  return !!value && _typeof(value) === "object" && ArrayBuffer.prototype.isPrototypeOf(value) || isOtherArrayBuffer(value);
@@ -923,7 +931,7 @@ function isEvent(value) {
923
931
  var predicate = function predicate(str) {
924
932
  return "[object " === str.slice(0, 8) && str.slice(-6) === "Event]";
925
933
  };
926
- return !!value && _typeof(value) === "object" && (predicate(Object.prototype.toString.call(value)) || predicate(String(value))) && "type" in value && typeof value.type === "string";
934
+ return !!value && _typeof(value) === "object" && "type" in value && typeof value.type === "string" && (predicate(Object.prototype.toString.call(value)) || predicate(typeString(value)));
927
935
  }
928
936
  function whenAbort(target, executor, signal) {
929
937
  var _onAbort = function onAbort() {
@@ -1475,7 +1483,7 @@ function isExternalBlob(value) {
1475
1483
  var strict = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
1476
1484
  var expects = ["[object Blob]"];
1477
1485
  if (!strict) expects.push("[object File]");
1478
- return (expects.indexOf(Object.prototype.toString.call(value)) > -1 || expects.indexOf(String(value)) > -1) && !!value && _typeof(value) === "object" && "size" in value && typeof value.size === "number" && "arrayBuffer" in value && typeof value.arrayBuffer === "function";
1486
+ return !!value && _typeof(value) === "object" && "size" in value && typeof value.size === "number" && "arrayBuffer" in value && typeof value.arrayBuffer === "function" && (expects.indexOf(Object.prototype.toString.call(value)) > -1 || expects.indexOf(typeString(value)) > -1);
1479
1487
  }
1480
1488
  var BlobE = function () {
1481
1489
  try {
@@ -2231,10 +2239,10 @@ function decode(str) {
2231
2239
  });
2232
2240
  }
2233
2241
  function isURLSearchParams(value) {
2234
- return isPolyfillType("URLSearchParams", value) || isGlobalURLSearchParams(value);
2242
+ return isPolyfillType("URLSearchParams", value) || isExternalURLSearchParams(value);
2235
2243
  }
2236
- function isGlobalURLSearchParams(value) {
2237
- return isObjectType("URLSearchParams", value) || !!value && _typeof(value) === "object" && typeof URLSearchParams !== "undefined" && URLSearchParams && URLSearchParams.prototype.isPrototypeOf(value);
2244
+ function isExternalURLSearchParams(value) {
2245
+ return isObjectType("URLSearchParams", value) || !!value && _typeof(value) === "object" && typeof URLSearchParams === "function" && value instanceof URLSearchParams;
2238
2246
  }
2239
2247
  var URLSearchParamsE = typeof URLSearchParams !== "undefined" && URLSearchParams || URLSearchParamsP;
2240
2248
 
@@ -2485,7 +2493,7 @@ function isFormData(value) {
2485
2493
  }
2486
2494
  function isExternalFormData(value) {
2487
2495
  var expect = "[object FormData]";
2488
- return (Object.prototype.toString.call(value) === expect || String(value) === expect) && !!value && _typeof(value) === "object" && "forEach" in value && typeof value.forEach === "function";
2496
+ return !!value && _typeof(value) === "object" && "forEach" in value && typeof value.forEach === "function" && (Object.prototype.toString.call(value) === expect || typeString(value) === expect);
2489
2497
  }
2490
2498
  function FormData_toBlob(formData, externalBoundary) {
2491
2499
  var boundary = externalBoundary || "----formdata-mphttpx-" + Math.random();
@@ -2762,7 +2770,7 @@ function state$8(target) {
2762
2770
  }
2763
2771
  function initBody(instance, body) {
2764
2772
  var b = instance;
2765
- if (isGlobalReadableStream(body) || isOtherReadableStream(body)) {
2773
+ if (isReadableStream(body)) {
2766
2774
  throw new TypeError("Failed to construct '".concat(className(b), "': ReadableStream not implemented."));
2767
2775
  }
2768
2776
  if (body !== null && body !== undefined) {
@@ -2803,11 +2811,12 @@ function consumed(body, kind) {
2803
2811
  }
2804
2812
  return Promise.reject(new TypeError("Failed to execute '".concat(kind, "' on '").concat(className(body), "': body stream already read")));
2805
2813
  }
2806
- function isGlobalReadableStream(value) {
2807
- return !!value && _typeof(value) === "object" && typeof ReadableStream !== "undefined" && ReadableStream && ReadableStream.prototype.isPrototypeOf(value);
2814
+ function isReadableStream(value) {
2815
+ return isExternalReadableStream(value);
2808
2816
  }
2809
- function isOtherReadableStream(value) {
2810
- return (isObjectType("ReadableStream", value) || String(value) === "[object ReadableStream]") && !!value && _typeof(value) === "object" && "getReader" in value && typeof value.getReader === "function";
2817
+ function isExternalReadableStream(value) {
2818
+ var expect = "[object ReadableStream]";
2819
+ return !!value && _typeof(value) === "object" && "getReader" in value && typeof value.getReader === "function" && (Object.prototype.toString.call(value) === expect || typeString(value) === expect);
2811
2820
  }
2812
2821
 
2813
2822
  var HeadersP = /*#__PURE__*/function (_Symbol$iterator, _Symbol$toStringTag) {
@@ -2985,7 +2994,7 @@ function isHeaders(value) {
2985
2994
  }
2986
2995
  function isExternalHeaders(value) {
2987
2996
  var expect = "[object Headers]";
2988
- return (Object.prototype.toString.call(value) === expect || String(value) === expect) && !!value && _typeof(value) === "object" && "forEach" in value && typeof value.forEach === "function";
2997
+ return !!value && _typeof(value) === "object" && "forEach" in value && typeof value.forEach === "function" && (Object.prototype.toString.call(value) === expect || typeString(value) === expect);
2989
2998
  }
2990
2999
  function normalizeName(name, throwError) {
2991
3000
  if (typeof name !== "string") {
@@ -3224,7 +3233,7 @@ function isRequest(value) {
3224
3233
  }
3225
3234
  function isExternalRequest(value) {
3226
3235
  var expect = "[object Request]";
3227
- return (Object.prototype.toString.call(value) === expect || String(value) === expect) && !!value && _typeof(value) === "object" && "arrayBuffer" in value && typeof value.arrayBuffer === "function";
3236
+ return !!value && _typeof(value) === "object" && "arrayBuffer" in value && typeof value.arrayBuffer === "function" && (Object.prototype.toString.call(value) === expect || typeString(value) === expect);
3228
3237
  }
3229
3238
  function createPhonyPayload(request) {
3230
3239
  var payload = Object.create(Payload.prototype);
@@ -6308,7 +6317,7 @@ var WebSocketP = /*#__PURE__*/function (_EventTargetP, _Symbol$toStringTag) {
6308
6317
  onError(_this);
6309
6318
  onMessage(_this);
6310
6319
  } else {
6311
- throw new Error("connectSocket can't establish a connection to the server at ".concat("" + url, "."));
6320
+ throw new Error("connectSocket can't establish a connection to the server at ".concat(targetURL, "."));
6312
6321
  }
6313
6322
  return _this;
6314
6323
  }