@wevu/web-apis 1.2.18 → 1.2.20

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/http.d.mts CHANGED
@@ -8,6 +8,7 @@ declare class HeadersPolyfill {
8
8
  append(key: string, value: string): void;
9
9
  set(key: string, value: string): void;
10
10
  get(key: string): string | null;
11
+ getSetCookie(): string[];
11
12
  has(key: string): boolean;
12
13
  delete(key: string): void;
13
14
  forEach(callback: (value: string, key: string) => void): void;
@@ -40,6 +41,8 @@ declare class ResponsePolyfill {
40
41
  readonly type: ResponseType;
41
42
  readonly [Symbol.toStringTag] = "Response";
42
43
  constructor(body?: RequestBodyLike, init?: Record<string, any>);
44
+ static error(): ResponsePolyfill;
45
+ static json(data: unknown, init?: Record<string, any>): ResponsePolyfill;
43
46
  get body(): ReadableStream<Uint8Array> | null;
44
47
  get bodyUsed(): boolean;
45
48
  arrayBuffer(): Promise<ArrayBuffer>;
package/dist/http.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  import { f as isUrlInstance, i as decodeText, l as normalizeHeaderName, n as cloneArrayBuffer, o as encodeText, r as cloneArrayBufferView } from "./shared-B_5_ZEFl.mjs";
2
2
  //#region src/http.ts
3
+ const SET_COOKIE_HEADER_NAME = "set-cookie";
3
4
  function isIterableHeaders(input) {
4
5
  return Boolean(input) && typeof input[Symbol.iterator] === "function";
5
6
  }
@@ -23,19 +24,31 @@ var HeadersPolyfill = class {
23
24
  if (isHeaderObject(init)) for (const [key, value] of Object.entries(init)) this.append(key, Array.isArray(value) ? value.join(", ") : String(value));
24
25
  }
25
26
  append(key, value) {
26
- const current = this.get(key);
27
- this.set(key, current ? `${current}, ${value}` : value);
27
+ const normalized = normalizeHeaderName(key);
28
+ if (!normalized) return;
29
+ const item = this.store.get(normalized);
30
+ if (item) {
31
+ item.values.push(String(value));
32
+ return;
33
+ }
34
+ this.store.set(normalized, {
35
+ key,
36
+ values: [String(value)]
37
+ });
28
38
  }
29
39
  set(key, value) {
30
40
  const normalized = normalizeHeaderName(key);
31
41
  if (!normalized) return;
32
42
  this.store.set(normalized, {
33
43
  key,
34
- value: String(value)
44
+ values: [String(value)]
35
45
  });
36
46
  }
37
47
  get(key) {
38
- return this.store.get(normalizeHeaderName(key))?.value ?? null;
48
+ return this.store.get(normalizeHeaderName(key))?.values.join(", ") ?? null;
49
+ }
50
+ getSetCookie() {
51
+ return this.store.get(SET_COOKIE_HEADER_NAME)?.values.slice() ?? [];
39
52
  }
40
53
  has(key) {
41
54
  return this.store.has(normalizeHeaderName(key));
@@ -44,16 +57,16 @@ var HeadersPolyfill = class {
44
57
  this.store.delete(normalizeHeaderName(key));
45
58
  }
46
59
  forEach(callback) {
47
- for (const { key, value } of this.store.values()) callback(value, key);
60
+ for (const { key, values } of this.store.values()) callback(values.join(", "), key);
48
61
  }
49
62
  entries() {
50
- return Array.from(this.store.values(), (item) => [item.key, item.value])[Symbol.iterator]();
63
+ return Array.from(this.store.values(), (item) => [item.key, item.values.join(", ")])[Symbol.iterator]();
51
64
  }
52
65
  keys() {
53
66
  return Array.from(this.store.values(), (item) => item.key)[Symbol.iterator]();
54
67
  }
55
68
  values() {
56
- return Array.from(this.store.values(), (item) => item.value)[Symbol.iterator]();
69
+ return Array.from(this.store.values(), (item) => item.values.join(", "))[Symbol.iterator]();
57
70
  }
58
71
  [Symbol.iterator]() {
59
72
  return this.entries();
@@ -131,7 +144,7 @@ var ResponsePolyfill = class ResponsePolyfill {
131
144
  ok;
132
145
  url;
133
146
  redirected = false;
134
- type = "basic";
147
+ type;
135
148
  [Symbol.toStringTag] = "Response";
136
149
  constructor(body, init = {}) {
137
150
  responseBodyStore.set(this, normalizeBody(body));
@@ -141,6 +154,23 @@ var ResponsePolyfill = class ResponsePolyfill {
141
154
  this.ok = this.status >= 200 && this.status < 300;
142
155
  this.headers = new HeadersPolyfill(init.headers);
143
156
  this.url = init.url ?? "";
157
+ this.type = init.type ?? "basic";
158
+ }
159
+ static error() {
160
+ return new ResponsePolyfill(null, {
161
+ status: 0,
162
+ type: "error"
163
+ });
164
+ }
165
+ static json(data, init = {}) {
166
+ const body = JSON.stringify(data);
167
+ if (body === void 0) throw new TypeError("Failed to execute 'json' on 'Response': data is not JSON serializable");
168
+ const headers = new HeadersPolyfill(init.headers);
169
+ if (!headers.has("content-type")) headers.set("content-type", "application/json");
170
+ return new ResponsePolyfill(body, {
171
+ ...init,
172
+ headers
173
+ });
144
174
  }
145
175
  get body() {
146
176
  return null;
package/dist/index.mjs CHANGED
@@ -235,13 +235,95 @@ function assignHostGlobal(host, key, value) {
235
235
  return false;
236
236
  }
237
237
  }
238
+ function defineHostGlobal(host, key, descriptor) {
239
+ try {
240
+ Object.defineProperty(host, key, {
241
+ configurable: true,
242
+ ...descriptor
243
+ });
244
+ return true;
245
+ } catch {
246
+ return false;
247
+ }
248
+ }
249
+ function createUrlParser(URLConstructor) {
250
+ return function parse(input, base) {
251
+ try {
252
+ return new URLConstructor(input, base);
253
+ } catch {
254
+ return null;
255
+ }
256
+ };
257
+ }
258
+ function createUrlCanParser(URLConstructor) {
259
+ return function canParse(input, base) {
260
+ try {
261
+ Reflect.construct(URLConstructor, base === void 0 ? [input] : [input, base]);
262
+ return true;
263
+ } catch {
264
+ return false;
265
+ }
266
+ };
267
+ }
268
+ function patchUrlConstructor(host) {
269
+ const URLConstructor = host.URL;
270
+ if (typeof URLConstructor !== "function" || isPlaceholderRequestGlobal(URLConstructor)) return false;
271
+ let patched = true;
272
+ if (typeof URLConstructor.parse !== "function") patched = assignHostGlobal(URLConstructor, "parse", createUrlParser(URLConstructor)) && patched;
273
+ if (typeof URLConstructor.canParse !== "function") patched = assignHostGlobal(URLConstructor, "canParse", createUrlCanParser(URLConstructor)) && patched;
274
+ return patched;
275
+ }
276
+ function compareUrlSearchParamKeys(leftKey, rightKey) {
277
+ if (leftKey < rightKey) return -1;
278
+ if (leftKey > rightKey) return 1;
279
+ return 0;
280
+ }
281
+ function sortUrlSearchParams() {
282
+ const entries = Array.from(this.entries()).sort(([leftKey], [rightKey]) => compareUrlSearchParamKeys(leftKey, rightKey));
283
+ const keys = [...new Set(entries.map(([key]) => key))];
284
+ for (const key of keys) this.delete(key);
285
+ for (const [key, value] of entries) this.append(key, value);
286
+ }
287
+ function patchUrlSearchParamsConstructor(host) {
288
+ const URLSearchParamsConstructor = host.URLSearchParams;
289
+ const prototype = URLSearchParamsConstructor?.prototype;
290
+ if (typeof URLSearchParamsConstructor !== "function" || isPlaceholderRequestGlobal(URLSearchParamsConstructor) || !prototype) return false;
291
+ let patched = true;
292
+ if (!("size" in prototype)) patched = defineHostGlobal(prototype, "size", { get() {
293
+ let size = 0;
294
+ this.forEach(() => {
295
+ size += 1;
296
+ });
297
+ return size;
298
+ } }) && patched;
299
+ if (typeof prototype.sort !== "function") patched = assignHostGlobal(prototype, "sort", sortUrlSearchParams) && patched;
300
+ return patched;
301
+ }
302
+ function patchHeadersConstructor(host) {
303
+ const HeadersConstructor = host.Headers;
304
+ const prototype = HeadersConstructor?.prototype;
305
+ if (typeof HeadersConstructor !== "function" || isPlaceholderRequestGlobal(HeadersConstructor) || !prototype) return false;
306
+ if (typeof prototype.getSetCookie === "function") return true;
307
+ return assignHostGlobal(prototype, "getSetCookie", function getSetCookie() {
308
+ const value = this.get("set-cookie");
309
+ return value == null ? [] : [value];
310
+ });
311
+ }
312
+ function patchResponseConstructor(host) {
313
+ const ResponseConstructor = host.Response;
314
+ if (typeof ResponseConstructor !== "function" || isPlaceholderRequestGlobal(ResponseConstructor)) return false;
315
+ let patched = true;
316
+ if (typeof ResponseConstructor.json !== "function") patched = assignHostGlobal(ResponseConstructor, "json", ResponsePolyfill.json) && patched;
317
+ if (typeof ResponseConstructor.error !== "function") patched = assignHostGlobal(ResponseConstructor, "error", ResponsePolyfill.error) && patched;
318
+ return patched;
319
+ }
238
320
  function installSingleTarget(host, target) {
239
321
  if (target === "fetch") {
240
322
  if (typeof host.fetch !== "function" || isPlaceholderRequestGlobal(host.fetch)) assignHostGlobal(host, "fetch", fetch);
241
323
  return;
242
324
  }
243
325
  if (target === "Headers") {
244
- if (typeof host.Headers !== "function" || isPlaceholderRequestGlobal(host.Headers)) assignHostGlobal(host, "Headers", HeadersPolyfill);
326
+ if (typeof host.Headers !== "function" || isPlaceholderRequestGlobal(host.Headers) || !patchHeadersConstructor(host)) assignHostGlobal(host, "Headers", HeadersPolyfill);
245
327
  return;
246
328
  }
247
329
  if (target === "Request") {
@@ -249,7 +331,7 @@ function installSingleTarget(host, target) {
249
331
  return;
250
332
  }
251
333
  if (target === "Response") {
252
- if (typeof host.Response !== "function" || isPlaceholderRequestGlobal(host.Response)) assignHostGlobal(host, "Response", ResponsePolyfill);
334
+ if (typeof host.Response !== "function" || isPlaceholderRequestGlobal(host.Response) || !patchResponseConstructor(host)) assignHostGlobal(host, "Response", ResponsePolyfill);
253
335
  return;
254
336
  }
255
337
  if (target === "TextEncoder") {
@@ -303,8 +385,8 @@ function installSingleTarget(host, target) {
303
385
  if (target === "XMLHttpRequest" && (typeof host.XMLHttpRequest !== "function" || isPlaceholderRequestGlobal(host.XMLHttpRequest))) assignHostGlobal(host, "XMLHttpRequest", XMLHttpRequestPolyfill);
304
386
  }
305
387
  function installUrlGlobals(host) {
306
- if (!hasUsableConstructor(host.URL, ["https://request-globals.invalid"])) assignHostGlobal(host, "URL", URLPolyfill);
307
- if (!hasUsableConstructor(host.URLSearchParams, ["client=graphql-request"])) assignHostGlobal(host, "URLSearchParams", URLSearchParamsPolyfill);
388
+ if (!hasUsableConstructor(host.URL, ["https://request-globals.invalid"]) || !patchUrlConstructor(host)) assignHostGlobal(host, "URL", URLPolyfill);
389
+ if (!hasUsableConstructor(host.URLSearchParams, ["client=graphql-request"]) || !patchUrlSearchParamsConstructor(host)) assignHostGlobal(host, "URLSearchParams", URLSearchParamsPolyfill);
308
390
  if (!hasUsableConstructor(host.Blob)) assignHostGlobal(host, "Blob", BlobPolyfill);
309
391
  if (!hasUsableConstructor(host.FormData)) assignHostGlobal(host, "FormData", FormDataPolyfill);
310
392
  }
package/dist/url.d.mts CHANGED
@@ -9,7 +9,9 @@ declare class URLSearchParamsPolyfill {
9
9
  get(key: string): string | null;
10
10
  getAll(key: string): string[];
11
11
  has(key: string): boolean;
12
+ get size(): number;
12
13
  set(key: string, value: string): void;
14
+ sort(): void;
13
15
  forEach(callback: (value: string, key: string) => void): void;
14
16
  entries(): ArrayIterator<[string, string]>;
15
17
  keys(): ArrayIterator<string>;
@@ -30,6 +32,8 @@ declare class URLPolyfill {
30
32
  protocol: string;
31
33
  username: string;
32
34
  readonly searchParams: URLSearchParamsPolyfill;
35
+ static canParse(input: string | URLPolyfill, base?: string | URLPolyfill): boolean;
36
+ static parse(input: string | URLPolyfill, base?: string | URLPolyfill): URLPolyfill | null;
33
37
  constructor(input: string | URLPolyfill, base?: string | URLPolyfill);
34
38
  get hash(): string;
35
39
  set hash(value: string);
package/dist/url.mjs CHANGED
@@ -112,10 +112,21 @@ var URLSearchParamsPolyfill = class {
112
112
  has(key) {
113
113
  return this.entriesStore.some(([entryKey]) => entryKey === String(key));
114
114
  }
115
+ get size() {
116
+ return this.entriesStore.length;
117
+ }
115
118
  set(key, value) {
116
119
  this.delete(key);
117
120
  this.append(key, value);
118
121
  }
122
+ sort() {
123
+ this.entriesStore.sort(([leftKey], [rightKey]) => {
124
+ if (leftKey < rightKey) return -1;
125
+ if (leftKey > rightKey) return 1;
126
+ return 0;
127
+ });
128
+ this.onChange?.();
129
+ }
119
130
  forEach(callback) {
120
131
  for (const [key, value] of this.entriesStore) callback(value, key);
121
132
  }
@@ -135,7 +146,7 @@ var URLSearchParamsPolyfill = class {
135
146
  return this.entries();
136
147
  }
137
148
  };
138
- var URLPolyfill = class {
149
+ var URLPolyfill = class URLPolyfill {
139
150
  hashValue = "";
140
151
  hrefValue = "";
141
152
  searchValue = "";
@@ -148,6 +159,16 @@ var URLPolyfill = class {
148
159
  protocol = "";
149
160
  username = "";
150
161
  searchParams;
162
+ static canParse(input, base) {
163
+ return URLPolyfill.parse(input, base) !== null;
164
+ }
165
+ static parse(input, base) {
166
+ try {
167
+ return new URLPolyfill(input, base);
168
+ } catch {
169
+ return null;
170
+ }
171
+ }
151
172
  constructor(input, base) {
152
173
  const inputString = typeof input === "string" ? input : input.toString();
153
174
  const baseString = typeof base === "string" ? base : base ? base.toString() : void 0;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wevu/web-apis",
3
3
  "type": "module",
4
- "version": "1.2.18",
4
+ "version": "1.2.20",
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",
@@ -74,7 +74,7 @@
74
74
  "node": "^20.19.0 || >=22.12.0"
75
75
  },
76
76
  "dependencies": {
77
- "@weapp-core/constants": "0.1.11",
77
+ "@weapp-core/constants": "0.1.12",
78
78
  "@wevu/api": "0.2.9"
79
79
  },
80
80
  "publishConfig": {