@wevu/web-apis 1.2.5 → 1.2.7

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
@@ -108,6 +108,7 @@ socket.onclose = (event) => {
108
108
  | `installAbortGlobals` | 仅安装 `AbortController` / `AbortSignal` |
109
109
  | `fetch` | 基于小程序请求能力适配的 `fetch` 实现 |
110
110
  | `HeadersPolyfill` / `RequestPolyfill` / `ResponsePolyfill` | HTTP 相关兼容类 |
111
+ | `TextEncoderPolyfill` / `TextDecoderPolyfill` | 文本编解码兼容类 |
111
112
  | `URLPolyfill` / `URLSearchParamsPolyfill` | URL 相关兼容类 |
112
113
  | `WebSocketPolyfill` | 基于小程序 `SocketTask` 的 `WebSocket` 兼容实现 |
113
114
  | `XMLHttpRequestPolyfill` | XHR 兼容实现 |
package/dist/abort.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { RequestGlobalsEventTarget } from "./shared.mjs";
1
+ import { t as RequestGlobalsEventTarget } from "./shared-ChAdqo6n.mjs";
2
2
  //#region src/abort.ts
3
3
  function createAbortError(reason) {
4
4
  if (reason instanceof Error) return reason;
package/dist/fetch.d.mts CHANGED
@@ -1,3 +1,5 @@
1
+ import { URLPolyfill } from "./url.mjs";
2
+
1
3
  //#region src/fetch.d.ts
2
4
  interface RequestLikeInput {
3
5
  url: string;
@@ -17,7 +19,7 @@ interface RequestGlobalsFetchInit {
17
19
  signal?: AbortSignal | null;
18
20
  [key: string]: unknown;
19
21
  }
20
- type RequestGlobalsFetchInput = string | URL | RequestLikeInput;
22
+ type RequestGlobalsFetchInput = string | URL | URLPolyfill | RequestLikeInput;
21
23
  /**
22
24
  * @description 使用 @wevu/api 的 request 能力实现 fetch 语义对齐。
23
25
  */
package/dist/fetch.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { cloneArrayBuffer, cloneArrayBufferView, normalizeHeaderName } from "./shared.mjs";
1
+ import { f as isUrlInstance, l as normalizeHeaderName, n as cloneArrayBuffer, p as isUrlSearchParamsInstance, r as cloneArrayBufferView } from "./shared-ChAdqo6n.mjs";
2
2
  import { HeadersPolyfill, ResponsePolyfill } from "./http.mjs";
3
3
  import { wpi } from "@wevu/api";
4
4
  //#region src/fetch.ts
@@ -16,9 +16,6 @@ const hasOwn = Object.prototype.hasOwnProperty;
16
16
  function isObject(value) {
17
17
  return typeof value === "object" && value !== null;
18
18
  }
19
- function isNativeUrlInstance(value) {
20
- return typeof URL !== "undefined" && value instanceof URL;
21
- }
22
19
  function createAbortError() {
23
20
  if (typeof DOMException === "function") return new DOMException("The operation was aborted.", "AbortError");
24
21
  const error = /* @__PURE__ */ new Error("The operation was aborted.");
@@ -86,7 +83,7 @@ async function normalizeRequestBody(body, headers) {
86
83
  if (!hasHeader(headers, "content-type")) headers["content-type"] = "text/plain;charset=UTF-8";
87
84
  return body;
88
85
  }
89
- if (typeof URLSearchParams !== "undefined" && body instanceof URLSearchParams) {
86
+ if (isUrlSearchParamsInstance(body)) {
90
87
  if (!hasHeader(headers, "content-type")) headers["content-type"] = "application/x-www-form-urlencoded;charset=UTF-8";
91
88
  return body.toString();
92
89
  }
@@ -101,7 +98,7 @@ async function normalizeRequestBody(body, headers) {
101
98
  }
102
99
  async function resolveRequestMeta(input, init = {}) {
103
100
  const requestInput = isRequestLikeInput(input) ? input : void 0;
104
- const url = typeof input === "string" ? input : isNativeUrlInstance(input) ? input.toString() : requestInput?.url;
101
+ const url = typeof input === "string" ? input : isUrlInstance(input) ? input.toString() : requestInput?.url;
105
102
  if (!url) throw new TypeError("Failed to execute fetch: invalid request url");
106
103
  const method = normalizeMethod(init.method ?? requestInput?.method);
107
104
  const headers = toHeaderMap(requestInput?.headers);
package/dist/http.d.mts CHANGED
@@ -1,3 +1,5 @@
1
+ import { URLPolyfill } from "./url.mjs";
2
+
1
3
  //#region src/http.d.ts
2
4
  type HeaderRecord = Record<string, string>;
3
5
  declare class HeadersPolyfill {
@@ -21,9 +23,9 @@ declare class RequestPolyfill {
21
23
  readonly headers: HeadersPolyfill;
22
24
  readonly signal: AbortSignal | null;
23
25
  readonly [Symbol.toStringTag] = "Request";
24
- private readonly bodyValue;
25
- bodyUsed: boolean;
26
- constructor(input: string | URL | RequestPolyfill, init?: Record<string, any>);
26
+ constructor(input: string | URL | URLPolyfill | RequestPolyfill, init?: Record<string, any>);
27
+ get body(): ReadableStream<Uint8Array> | null;
28
+ get bodyUsed(): boolean;
27
29
  arrayBuffer(): Promise<ArrayBuffer>;
28
30
  text(): Promise<string>;
29
31
  clone(): RequestPolyfill;
@@ -36,11 +38,10 @@ declare class ResponsePolyfill {
36
38
  readonly url: string;
37
39
  readonly redirected = false;
38
40
  readonly type: ResponseType;
39
- readonly body: ReadableStream<Uint8Array> | null;
40
41
  readonly [Symbol.toStringTag] = "Response";
41
- private readonly bodyValue;
42
- bodyUsed: boolean;
43
42
  constructor(body?: RequestBodyLike, init?: Record<string, any>);
43
+ get body(): ReadableStream<Uint8Array> | null;
44
+ get bodyUsed(): boolean;
44
45
  arrayBuffer(): Promise<ArrayBuffer>;
45
46
  blob(): Promise<Blob>;
46
47
  formData(): Promise<void>;
package/dist/http.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { cloneArrayBuffer, cloneArrayBufferView, decodeText, encodeText, normalizeHeaderName } from "./shared.mjs";
1
+ import { f as isUrlInstance, i as decodeText, l as normalizeHeaderName, n as cloneArrayBuffer, o as encodeText, r as cloneArrayBufferView } from "./shared-ChAdqo6n.mjs";
2
2
  //#region src/http.ts
3
3
  function isIterableHeaders(input) {
4
4
  return Boolean(input) && typeof input[Symbol.iterator] === "function";
@@ -6,9 +6,6 @@ function isIterableHeaders(input) {
6
6
  function isHeaderObject(input) {
7
7
  return typeof input === "object" && input !== null;
8
8
  }
9
- function isNativeUrlInstance(value) {
10
- return typeof URL !== "undefined" && value instanceof URL;
11
- }
12
9
  var HeadersPolyfill = class {
13
10
  store = /* @__PURE__ */ new Map();
14
11
  constructor(init) {
@@ -62,6 +59,10 @@ var HeadersPolyfill = class {
62
59
  return this.entries();
63
60
  }
64
61
  };
62
+ const requestBodyStore = /* @__PURE__ */ new WeakMap();
63
+ const requestBodyUsedStore = /* @__PURE__ */ new WeakMap();
64
+ const responseBodyStore = /* @__PURE__ */ new WeakMap();
65
+ const responseBodyUsedStore = /* @__PURE__ */ new WeakMap();
65
66
  function normalizeBody(body) {
66
67
  if (body == null || typeof body === "string" || body instanceof ArrayBuffer || ArrayBuffer.isView(body)) return body;
67
68
  if (typeof Blob !== "undefined" && body instanceof Blob) return body;
@@ -79,36 +80,47 @@ async function readBodyAsText(body) {
79
80
  if (typeof body === "string") return body;
80
81
  return decodeText(await readBodyAsArrayBuffer(body));
81
82
  }
83
+ function getRequestBodyValue(request) {
84
+ return request ? requestBodyStore.get(request) : void 0;
85
+ }
86
+ function getResponseBodyValue(response) {
87
+ return responseBodyStore.get(response);
88
+ }
82
89
  var RequestPolyfill = class RequestPolyfill {
83
90
  url;
84
91
  method;
85
92
  headers;
86
93
  signal;
87
94
  [Symbol.toStringTag] = "Request";
88
- bodyValue;
89
- bodyUsed = false;
90
95
  constructor(input, init = {}) {
91
96
  const request = input instanceof RequestPolyfill ? input : void 0;
92
- this.url = typeof input === "string" ? input : isNativeUrlInstance(input) ? input.toString() : request?.url ?? "";
97
+ this.url = typeof input === "string" ? input : isUrlInstance(input) ? input.toString() : request?.url ?? "";
93
98
  this.method = String(init.method ?? request?.method ?? "GET").toUpperCase();
94
99
  this.headers = new HeadersPolyfill(init.headers ?? request?.headers);
95
100
  this.signal = init.signal ?? request?.signal ?? null;
96
- this.bodyValue = normalizeBody(init.body ?? request?.bodyValue);
101
+ requestBodyStore.set(this, normalizeBody(init.body ?? getRequestBodyValue(request)));
102
+ requestBodyUsedStore.set(this, false);
103
+ }
104
+ get body() {
105
+ return null;
106
+ }
107
+ get bodyUsed() {
108
+ return requestBodyUsedStore.get(this) === true;
97
109
  }
98
110
  async arrayBuffer() {
99
- this.bodyUsed = true;
100
- return readBodyAsArrayBuffer(this.bodyValue);
111
+ requestBodyUsedStore.set(this, true);
112
+ return readBodyAsArrayBuffer(getRequestBodyValue(this));
101
113
  }
102
114
  async text() {
103
- this.bodyUsed = true;
104
- return readBodyAsText(this.bodyValue);
115
+ requestBodyUsedStore.set(this, true);
116
+ return readBodyAsText(getRequestBodyValue(this));
105
117
  }
106
118
  clone() {
107
119
  return new RequestPolyfill(this.url, {
108
120
  method: this.method,
109
121
  headers: this.headers,
110
122
  signal: this.signal,
111
- body: this.bodyValue
123
+ body: getRequestBodyValue(this)
112
124
  });
113
125
  }
114
126
  };
@@ -120,21 +132,25 @@ var ResponsePolyfill = class ResponsePolyfill {
120
132
  url;
121
133
  redirected = false;
122
134
  type = "basic";
123
- body = null;
124
135
  [Symbol.toStringTag] = "Response";
125
- bodyValue;
126
- bodyUsed = false;
127
136
  constructor(body, init = {}) {
128
- this.bodyValue = normalizeBody(body);
137
+ responseBodyStore.set(this, normalizeBody(body));
138
+ responseBodyUsedStore.set(this, false);
129
139
  this.status = Number.isFinite(init.status) ? init.status : 200;
130
140
  this.statusText = init.statusText ?? "";
131
141
  this.ok = this.status >= 200 && this.status < 300;
132
142
  this.headers = new HeadersPolyfill(init.headers);
133
143
  this.url = init.url ?? "";
134
144
  }
145
+ get body() {
146
+ return null;
147
+ }
148
+ get bodyUsed() {
149
+ return responseBodyUsedStore.get(this) === true;
150
+ }
135
151
  async arrayBuffer() {
136
- this.bodyUsed = true;
137
- return readBodyAsArrayBuffer(this.bodyValue);
152
+ responseBodyUsedStore.set(this, true);
153
+ return readBodyAsArrayBuffer(getResponseBodyValue(this));
138
154
  }
139
155
  async blob() {
140
156
  if (typeof Blob !== "function") throw new TypeError("Blob is unavailable in current runtime");
@@ -147,11 +163,11 @@ var ResponsePolyfill = class ResponsePolyfill {
147
163
  return JSON.parse(await this.text());
148
164
  }
149
165
  async text() {
150
- this.bodyUsed = true;
151
- return readBodyAsText(this.bodyValue);
166
+ responseBodyUsedStore.set(this, true);
167
+ return readBodyAsText(getResponseBodyValue(this));
152
168
  }
153
169
  clone() {
154
- return new ResponsePolyfill(this.bodyValue, {
170
+ return new ResponsePolyfill(getResponseBodyValue(this), {
155
171
  status: this.status,
156
172
  statusText: this.statusText,
157
173
  headers: this.headers,
package/dist/index.d.mts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { RequestGlobalsEventTarget } from "./shared.mjs";
2
2
  import { AbortControllerPolyfill, AbortSignalPolyfill } from "./abort.mjs";
3
+ import { URLPolyfill, URLSearchParamsPolyfill } from "./url.mjs";
3
4
  import { fetch } from "./fetch.mjs";
4
5
  import { HeadersPolyfill, RequestPolyfill, ResponsePolyfill } from "./http.mjs";
5
- import { URLPolyfill, URLSearchParamsPolyfill } from "./url.mjs";
6
6
  import { BlobPolyfill, FormDataPolyfill } from "./web.mjs";
7
7
  import { WebSocketPolyfill } from "./websocket.mjs";
8
8
  import { XMLHttpRequestPolyfill } from "./xhr.mjs";
@@ -59,6 +59,19 @@ declare const performancePolyfill: {
59
59
  //#region src/task.d.ts
60
60
  declare function queueMicrotaskPolyfill(callback: () => void): void;
61
61
  //#endregion
62
+ //#region src/textCodec.d.ts
63
+ type TextDecoderInput = ArrayBuffer | ArrayBufferView | null | undefined;
64
+ declare class TextEncoderPolyfill {
65
+ readonly encoding = "utf-8";
66
+ encode(input?: string): Uint8Array<ArrayBuffer>;
67
+ }
68
+ declare class TextDecoderPolyfill {
69
+ readonly encoding = "utf-8";
70
+ readonly fatal = false;
71
+ readonly ignoreBOM = false;
72
+ decode(input?: TextDecoderInput): string;
73
+ }
74
+ //#endregion
62
75
  //#region src/index.d.ts
63
76
  type WeappInjectWebRuntimeGlobalsTarget = 'fetch' | 'Headers' | 'Request' | 'Response' | 'TextEncoder' | 'TextDecoder' | 'AbortController' | 'AbortSignal' | 'XMLHttpRequest' | 'WebSocket' | 'atob' | 'btoa' | 'queueMicrotask' | 'performance' | 'crypto' | 'Event' | 'CustomEvent';
64
77
  type WeappInjectRequestGlobalsTarget = WeappInjectWebRuntimeGlobalsTarget;
@@ -79,4 +92,4 @@ declare function installRequestGlobals(options?: InstallRequestGlobalsOptions):
79
92
  */
80
93
  declare function installAbortGlobals(): Record<string, any>;
81
94
  //#endregion
82
- export { AbortControllerPolyfill, AbortSignalPolyfill, BlobPolyfill, CustomEventPolyfill, EventPolyfill, FormDataPolyfill, HeadersPolyfill, InstallRequestGlobalsOptions, InstallWebRuntimeGlobalsOptions, RequestGlobalsEventTarget, RequestPolyfill, ResponsePolyfill, URLPolyfill, URLSearchParamsPolyfill, WeappInjectRequestGlobalsTarget, WeappInjectWebRuntimeGlobalsTarget, WebSocketPolyfill, XMLHttpRequestPolyfill, atobPolyfill, btoaPolyfill, cryptoPolyfill, fetch, installAbortGlobals, installRequestGlobals, installWebRuntimeGlobals, performancePolyfill, queueMicrotaskPolyfill };
95
+ export { AbortControllerPolyfill, AbortSignalPolyfill, BlobPolyfill, CustomEventPolyfill, EventPolyfill, FormDataPolyfill, HeadersPolyfill, InstallRequestGlobalsOptions, InstallWebRuntimeGlobalsOptions, RequestGlobalsEventTarget, RequestPolyfill, ResponsePolyfill, TextDecoderPolyfill, TextEncoderPolyfill, URLPolyfill, URLSearchParamsPolyfill, WeappInjectRequestGlobalsTarget, WeappInjectWebRuntimeGlobalsTarget, WebSocketPolyfill, XMLHttpRequestPolyfill, atobPolyfill, btoaPolyfill, cryptoPolyfill, fetch, installAbortGlobals, installRequestGlobals, installWebRuntimeGlobals, performancePolyfill, queueMicrotaskPolyfill };
package/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
- import { RequestGlobalsEventTarget, decodeTextFallback, encodeTextFallback, installRequestGlobalBinding, resolveRequestGlobalsHost, resolveRequestGlobalsHosts } from "./shared.mjs";
1
+ import { URLPolyfill, URLSearchParamsPolyfill } from "./url.mjs";
2
+ import { a as decodeTextFallback, c as installRequestGlobalBinding, d as resolveRequestGlobalsHosts, s as encodeTextFallback, t as RequestGlobalsEventTarget, u as resolveRequestGlobalsHost } from "./shared-ChAdqo6n.mjs";
2
3
  import { AbortControllerPolyfill, AbortSignalPolyfill } from "./abort.mjs";
3
4
  import { HeadersPolyfill, RequestPolyfill, ResponsePolyfill } from "./http.mjs";
4
5
  import { fetch } from "./fetch.mjs";
5
- import { URLPolyfill, URLSearchParamsPolyfill } from "./url.mjs";
6
6
  import { BlobPolyfill, FormDataPolyfill } from "./web.mjs";
7
7
  import { WebSocketPolyfill } from "./websocket.mjs";
8
8
  import { XMLHttpRequestPolyfill } from "./xhr.mjs";
@@ -398,4 +398,4 @@ function installAbortGlobals() {
398
398
  return installWebRuntimeGlobals({ targets: ["AbortController", "AbortSignal"] });
399
399
  }
400
400
  //#endregion
401
- export { AbortControllerPolyfill, AbortSignalPolyfill, BlobPolyfill, CustomEventPolyfill, EventPolyfill, FormDataPolyfill, HeadersPolyfill, RequestGlobalsEventTarget, RequestPolyfill, ResponsePolyfill, URLPolyfill, URLSearchParamsPolyfill, WebSocketPolyfill, XMLHttpRequestPolyfill, atobPolyfill, btoaPolyfill, cryptoPolyfill, fetch, installAbortGlobals, installRequestGlobals, installWebRuntimeGlobals, performancePolyfill, queueMicrotaskPolyfill };
401
+ export { AbortControllerPolyfill, AbortSignalPolyfill, BlobPolyfill, CustomEventPolyfill, EventPolyfill, FormDataPolyfill, HeadersPolyfill, RequestGlobalsEventTarget, RequestPolyfill, ResponsePolyfill, TextDecoderPolyfill, TextEncoderPolyfill, URLPolyfill, URLSearchParamsPolyfill, WebSocketPolyfill, XMLHttpRequestPolyfill, atobPolyfill, btoaPolyfill, cryptoPolyfill, fetch, installAbortGlobals, installRequestGlobals, installWebRuntimeGlobals, performancePolyfill, queueMicrotaskPolyfill };
@@ -0,0 +1,120 @@
1
+ import { URLPolyfill, URLSearchParamsPolyfill } from "./url.mjs";
2
+ //#region src/constructors.ts
3
+ function resolveUrlConstructor() {
4
+ return typeof globalThis.URL === "function" ? globalThis.URL : void 0;
5
+ }
6
+ function isUrlInstance(value) {
7
+ const HostUrl = resolveUrlConstructor();
8
+ return Boolean(HostUrl && value instanceof HostUrl || value instanceof URLPolyfill);
9
+ }
10
+ function resolveUrlSearchParamsConstructor() {
11
+ return typeof globalThis.URLSearchParams === "function" ? globalThis.URLSearchParams : void 0;
12
+ }
13
+ function isUrlSearchParamsInstance(value) {
14
+ const HostUrlSearchParams = resolveUrlSearchParamsConstructor();
15
+ return Boolean(HostUrlSearchParams && value instanceof HostUrlSearchParams || value instanceof URLSearchParamsPolyfill);
16
+ }
17
+ function resolveTextEncoderConstructor() {
18
+ return typeof globalThis.TextEncoder === "function" ? globalThis.TextEncoder : void 0;
19
+ }
20
+ function resolveTextDecoderConstructor() {
21
+ return typeof globalThis.TextDecoder === "function" ? globalThis.TextDecoder : void 0;
22
+ }
23
+ //#endregion
24
+ //#region src/shared.ts
25
+ var RequestGlobalsEventTarget = class {
26
+ listeners = /* @__PURE__ */ new Map();
27
+ addEventListener(type, listener) {
28
+ const set = this.listeners.get(type) ?? /* @__PURE__ */ new Set();
29
+ set.add(listener);
30
+ this.listeners.set(type, set);
31
+ }
32
+ removeEventListener(type, listener) {
33
+ this.listeners.get(type)?.delete(listener);
34
+ }
35
+ dispatchEvent(event) {
36
+ const payload = {
37
+ ...event,
38
+ target: event.target ?? this,
39
+ currentTarget: event.currentTarget ?? this
40
+ };
41
+ for (const listener of this.listeners.get(event.type) ?? []) listener(payload);
42
+ const handlerKey = `on${event.type}`;
43
+ const handler = this[handlerKey];
44
+ if (typeof handler === "function") handler(payload);
45
+ return true;
46
+ }
47
+ };
48
+ function resolveRequestGlobalsHost() {
49
+ if (typeof globalThis !== "undefined") return globalThis;
50
+ return {};
51
+ }
52
+ function isRequestGlobalsHostCandidate(value) {
53
+ return value != null && (typeof value === "object" || typeof value === "function");
54
+ }
55
+ function pushRequestGlobalsHost(hosts, candidate) {
56
+ if (!isRequestGlobalsHostCandidate(candidate)) return;
57
+ if (!hosts.includes(candidate)) hosts.push(candidate);
58
+ }
59
+ function resolveRequestGlobalsHosts() {
60
+ const hosts = [];
61
+ const primaryHost = resolveRequestGlobalsHost();
62
+ pushRequestGlobalsHost(hosts, primaryHost);
63
+ for (const name of [
64
+ "global",
65
+ "self",
66
+ "window"
67
+ ]) pushRequestGlobalsHost(hosts, primaryHost[name]);
68
+ for (const key of [
69
+ "wx",
70
+ "my",
71
+ "tt"
72
+ ]) pushRequestGlobalsHost(hosts, primaryHost[key]);
73
+ return hosts;
74
+ }
75
+ function installRequestGlobalBinding(name, value) {
76
+ if (!name) return;
77
+ try {
78
+ const host = resolveRequestGlobalsHost();
79
+ host[name] = value;
80
+ } catch {}
81
+ }
82
+ function cloneArrayBuffer(buffer) {
83
+ return buffer.slice(0);
84
+ }
85
+ function cloneArrayBufferView(view) {
86
+ const copied = new Uint8Array(view.byteLength);
87
+ copied.set(new Uint8Array(view.buffer, view.byteOffset, view.byteLength));
88
+ return copied.buffer;
89
+ }
90
+ function encodeTextFallback(value) {
91
+ const binary = unescape(encodeURIComponent(String(value)));
92
+ const bytes = new Uint8Array(binary.length);
93
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
94
+ return bytes.buffer;
95
+ }
96
+ function encodeText(value) {
97
+ const TextEncoderConstructor = resolveTextEncoderConstructor();
98
+ if (TextEncoderConstructor) return new TextEncoderConstructor().encode(value).buffer;
99
+ return encodeTextFallback(value);
100
+ }
101
+ function decodeTextFallback(value) {
102
+ const bytes = new Uint8Array(value);
103
+ let binary = "";
104
+ for (const byte of bytes) binary += String.fromCharCode(byte);
105
+ try {
106
+ return decodeURIComponent(escape(binary));
107
+ } catch {
108
+ return binary;
109
+ }
110
+ }
111
+ function decodeText(value) {
112
+ const TextDecoderConstructor = resolveTextDecoderConstructor();
113
+ if (TextDecoderConstructor) return new TextDecoderConstructor().decode(value);
114
+ return decodeTextFallback(value);
115
+ }
116
+ function normalizeHeaderName(name) {
117
+ return name.trim().toLowerCase();
118
+ }
119
+ //#endregion
120
+ export { decodeTextFallback as a, installRequestGlobalBinding as c, resolveRequestGlobalsHosts as d, isUrlInstance as f, resolveUrlConstructor as h, decodeText as i, normalizeHeaderName as l, resolveTextEncoderConstructor as m, cloneArrayBuffer as n, encodeText as o, isUrlSearchParamsInstance as p, cloneArrayBufferView as r, encodeTextFallback as s, RequestGlobalsEventTarget as t, resolveRequestGlobalsHost as u };
package/dist/shared.mjs CHANGED
@@ -1,95 +1,2 @@
1
- //#region src/shared.ts
2
- var RequestGlobalsEventTarget = class {
3
- listeners = /* @__PURE__ */ new Map();
4
- addEventListener(type, listener) {
5
- const set = this.listeners.get(type) ?? /* @__PURE__ */ new Set();
6
- set.add(listener);
7
- this.listeners.set(type, set);
8
- }
9
- removeEventListener(type, listener) {
10
- this.listeners.get(type)?.delete(listener);
11
- }
12
- dispatchEvent(event) {
13
- const payload = {
14
- ...event,
15
- target: event.target ?? this,
16
- currentTarget: event.currentTarget ?? this
17
- };
18
- for (const listener of this.listeners.get(event.type) ?? []) listener(payload);
19
- const handlerKey = `on${event.type}`;
20
- const handler = this[handlerKey];
21
- if (typeof handler === "function") handler(payload);
22
- return true;
23
- }
24
- };
25
- function resolveRequestGlobalsHost() {
26
- if (typeof globalThis !== "undefined") return globalThis;
27
- return {};
28
- }
29
- function isRequestGlobalsHostCandidate(value) {
30
- return value != null && (typeof value === "object" || typeof value === "function");
31
- }
32
- function pushRequestGlobalsHost(hosts, candidate) {
33
- if (!isRequestGlobalsHostCandidate(candidate)) return;
34
- if (!hosts.includes(candidate)) hosts.push(candidate);
35
- }
36
- function resolveRequestGlobalsHosts() {
37
- const hosts = [];
38
- const primaryHost = resolveRequestGlobalsHost();
39
- pushRequestGlobalsHost(hosts, primaryHost);
40
- for (const name of [
41
- "global",
42
- "self",
43
- "window"
44
- ]) pushRequestGlobalsHost(hosts, primaryHost[name]);
45
- for (const key of [
46
- "wx",
47
- "my",
48
- "tt"
49
- ]) pushRequestGlobalsHost(hosts, primaryHost[key]);
50
- return hosts;
51
- }
52
- function installRequestGlobalBinding(name, value) {
53
- if (!name) return;
54
- try {
55
- const host = resolveRequestGlobalsHost();
56
- host[name] = value;
57
- } catch {}
58
- }
59
- function cloneArrayBuffer(buffer) {
60
- return buffer.slice(0);
61
- }
62
- function cloneArrayBufferView(view) {
63
- const copied = new Uint8Array(view.byteLength);
64
- copied.set(new Uint8Array(view.buffer, view.byteOffset, view.byteLength));
65
- return copied.buffer;
66
- }
67
- function encodeTextFallback(value) {
68
- const binary = unescape(encodeURIComponent(String(value)));
69
- const bytes = new Uint8Array(binary.length);
70
- for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
71
- return bytes.buffer;
72
- }
73
- function encodeText(value) {
74
- if (typeof TextEncoder === "function") return new TextEncoder().encode(value).buffer;
75
- return encodeTextFallback(value);
76
- }
77
- function decodeTextFallback(value) {
78
- const bytes = new Uint8Array(value);
79
- let binary = "";
80
- for (const byte of bytes) binary += String.fromCharCode(byte);
81
- try {
82
- return decodeURIComponent(escape(binary));
83
- } catch {
84
- return binary;
85
- }
86
- }
87
- function decodeText(value) {
88
- if (typeof TextDecoder === "function") return new TextDecoder().decode(value);
89
- return decodeTextFallback(value);
90
- }
91
- function normalizeHeaderName(name) {
92
- return name.trim().toLowerCase();
93
- }
94
- //#endregion
1
+ import { a as decodeTextFallback, c as installRequestGlobalBinding, d as resolveRequestGlobalsHosts, i as decodeText, l as normalizeHeaderName, n as cloneArrayBuffer, o as encodeText, r as cloneArrayBufferView, s as encodeTextFallback, t as RequestGlobalsEventTarget, u as resolveRequestGlobalsHost } from "./shared-ChAdqo6n.mjs";
95
2
  export { RequestGlobalsEventTarget, cloneArrayBuffer, cloneArrayBufferView, decodeText, decodeTextFallback, encodeText, encodeTextFallback, installRequestGlobalBinding, normalizeHeaderName, resolveRequestGlobalsHost, resolveRequestGlobalsHosts };
package/dist/web.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { cloneArrayBuffer, cloneArrayBufferView, decodeText, encodeText } from "./shared.mjs";
1
+ import { i as decodeText, n as cloneArrayBuffer, o as encodeText, r as cloneArrayBufferView } from "./shared-ChAdqo6n.mjs";
2
2
  //#region src/web.ts
3
3
  function normalizeBlobPart(part) {
4
4
  if (typeof part === "string") return Promise.resolve(encodeText(part));
@@ -1,5 +1,5 @@
1
- import { RequestGlobalsEventTarget, cloneArrayBuffer, cloneArrayBufferView } from "./shared.mjs";
2
1
  import { URLPolyfill } from "./url.mjs";
2
+ import { h as resolveUrlConstructor$1, m as resolveTextEncoderConstructor, n as cloneArrayBuffer, r as cloneArrayBufferView, t as RequestGlobalsEventTarget } from "./shared-ChAdqo6n.mjs";
3
3
  import { BlobPolyfill } from "./web.mjs";
4
4
  import { wpi } from "@wevu/api";
5
5
  //#region src/websocket.ts
@@ -17,7 +17,8 @@ function createDomLikeError(name, message) {
17
17
  return error;
18
18
  }
19
19
  function encodeReasonLength(reason) {
20
- if (typeof TextEncoder === "function") return new TextEncoder().encode(reason).byteLength;
20
+ const TextEncoderConstructor = resolveTextEncoderConstructor();
21
+ if (TextEncoderConstructor) return new TextEncoderConstructor().encode(reason).byteLength;
21
22
  return unescape(encodeURIComponent(reason)).length;
22
23
  }
23
24
  function normalizeCloseCode(code) {
@@ -32,7 +33,7 @@ function normalizeCloseReason(reason) {
32
33
  return normalized;
33
34
  }
34
35
  function resolveUrlConstructor() {
35
- return typeof URL === "function" ? URL : URLPolyfill;
36
+ return resolveUrlConstructor$1() ?? URLPolyfill;
36
37
  }
37
38
  function normalizeProtocols(protocols) {
38
39
  if (protocols == null) return;
package/dist/xhr.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { RequestGlobalsEventTarget, resolveRequestGlobalsHost } from "./shared.mjs";
1
+ import { t as RequestGlobalsEventTarget, u as resolveRequestGlobalsHost } from "./shared-ChAdqo6n.mjs";
2
2
  import { AbortControllerPolyfill } from "./abort.mjs";
3
3
  import { HeadersPolyfill, headersToObject } from "./http.mjs";
4
4
  import { fetch } from "./fetch.mjs";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wevu/web-apis",
3
3
  "type": "module",
4
- "version": "1.2.5",
4
+ "version": "1.2.7",
5
5
  "description": "Web API polyfills and global installers for mini-program runtimes",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -70,7 +70,7 @@
70
70
  "node": "^20.19.0 || >=22.12.0"
71
71
  },
72
72
  "dependencies": {
73
- "@weapp-core/constants": "0.1.0",
73
+ "@weapp-core/constants": "^0.1.1",
74
74
  "@wevu/api": "0.2.3"
75
75
  },
76
76
  "publishConfig": {