@wevu/web-apis 1.4.3 → 1.5.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
@@ -155,6 +155,8 @@ pnpm --filter @wevu/web-apis typecheck
155
155
  - 新项目建议使用 `installWebRuntimeGlobals()`
156
156
  - `installRequestGlobals()` 仍保留为兼容别名,后续会逐步淡出文档主叙事
157
157
 
158
+ `crypto.getRandomValues()` 同步填充并返回原 TypedArray,优先使用原生 Web Crypto。微信等小程序宿主的同名接口接收 options 并异步返回结果,兼容层不会调用它来完成同步填充。缺少原生 Web Crypto 时沿用 `Math.random()` fallback;该 fallback 不提供密码学安全保证,不能用于密钥或认证令牌。
159
+
158
160
  ## 11. Body 与 Blob 契约
159
161
 
160
162
  `RequestPolyfill` 和 `ResponsePolyfill` 提供可读 `body`,以及 `bytes()`、`arrayBuffer()`、`text()`、`json()`、`blob()`。非 null body 只能消费一次;调用消费方法或开始读取、取消 body 流时,`bodyUsed` 会变成 `true`。仅获取 reader 会锁定流,但不改变 `bodyUsed`。body 已消费或锁定时,重复读取和 `clone()` 都会以 `TypeError` 失败。需要多种读取方式时,请在消费前 clone:
package/dist/index.d.mts CHANGED
@@ -14,8 +14,13 @@ export declare function btoaPolyfill(input?: string): string;
14
14
  export declare function atobPolyfill(input?: string): string;
15
15
  type IntegerTypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | BigInt64Array | BigUint64Array;
16
16
  declare function getRandomValuesPolyfill<T extends IntegerTypedArray>(typedArray: T): (T & Int8Array<ArrayBufferLike>) | (T & Uint8Array<ArrayBufferLike>) | (T & Uint8ClampedArray<ArrayBufferLike>) | (T & Int16Array<ArrayBufferLike>) | (T & Uint16Array<ArrayBufferLike>) | (T & Int32Array<ArrayBufferLike>) | (T & Uint32Array<ArrayBufferLike>) | (T & BigInt64Array<ArrayBufferLike>) | (T & BigUint64Array<ArrayBufferLike>);
17
+ /**
18
+ * @description 基于 `getRandomValues` 生成符合 RFC 4122 version 4 的 UUID。
19
+ */
20
+ export declare function randomUUIDPolyfill(): string;
17
21
  export declare const cryptoPolyfill: {
18
22
  getRandomValues: typeof getRandomValuesPolyfill;
23
+ randomUUID: typeof randomUUIDPolyfill;
19
24
  };
20
25
  declare const performancePolyfill: {
21
26
  __weappVitePerformancePolyfill: boolean;
package/dist/index.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  import { AbortControllerPolyfill, AbortSignalPolyfill } from "./abort.mjs";
2
- import { c as installRequestGlobalBinding, f as resolveRequestGlobalsHost, p as resolveRequestGlobalsHosts, t as RequestGlobalsEventTarget, v as getMiniProgramRuntimeGlobalKeys } from "./shared-CARmZrDA.mjs";
3
2
  import { i as EventPolyfill, r as CustomEventPolyfill, t as EventSourcePolyfill } from "./eventsource-cHN0nNB1.mjs";
4
3
  import { t as fetch } from "./fetch-Cw1K3y4n.mjs";
5
4
  import { n as RequestPolyfill, t as ResponsePolyfill, u as HeadersPolyfill } from "./http-BJOhNGfL.mjs";
6
5
  import { getMiniProgramNetworkDefaults, resetMiniProgramNetworkDefaults, setMiniProgramNetworkDefaults } from "./networkDefaults.mjs";
6
+ import { c as installRequestGlobalBinding, f as resolveRequestGlobalsHost, p as resolveRequestGlobalsHosts, t as RequestGlobalsEventTarget, v as getMiniProgramRuntimeGlobalKeys } from "./shared-CARmZrDA.mjs";
7
7
  import { a as TransformStreamPolyfill, i as TransformStream, n as ReadableStreamPolyfill, o as WritableStream, r as TextDecoderStreamPolyfill, s as WritableStreamPolyfill, t as ReadableStream } from "./streams-CcHd-MmG.mjs";
8
8
  import { n as TextEncoderPolyfill, r as hasUsableTextDecoderConstructor, t as TextDecoderPolyfill } from "./textCodec-muWByCAW.mjs";
9
9
  import { n as URLSearchParamsPolyfill, t as URLPolyfill } from "./url-DjMmeEth.mjs";
@@ -71,26 +71,34 @@ function fillArrayWithMathRandom(target) {
71
71
  const bytes = new Uint8Array(target.buffer, target.byteOffset, target.byteLength);
72
72
  for (let index = 0; index < bytes.length; index++) bytes[index] = Math.floor(Math.random() * 256);
73
73
  }
74
- function fillArrayWithHostRandomValues(target) {
75
- for (const hostKey of getMiniProgramRuntimeGlobalKeys()) {
76
- const host = globalThis[hostKey];
77
- if (!host || typeof host.getRandomValues !== "function") continue;
78
- try {
79
- host.getRandomValues(target);
80
- return true;
81
- } catch {}
82
- }
83
- return false;
84
- }
85
74
  function getRandomValuesPolyfill(typedArray) {
86
75
  if (!isIntegerTypedArray(typedArray)) throw new TypeError("Failed to execute 'getRandomValues': input must be an integer TypedArray");
87
76
  if (typedArray.byteLength > MAX_RANDOM_VALUES_BYTE_LENGTH) throw new TypeError("Failed to execute 'getRandomValues': byteLength exceeds 65536");
88
77
  const nativeCrypto = getNativeCrypto();
89
78
  if (nativeCrypto && nativeCrypto.getRandomValues !== getRandomValuesPolyfill) return nativeCrypto.getRandomValues(typedArray);
90
- if (!fillArrayWithHostRandomValues(typedArray)) fillArrayWithMathRandom(typedArray);
79
+ fillArrayWithMathRandom(typedArray);
91
80
  return typedArray;
92
81
  }
93
- const cryptoPolyfill = { getRandomValues: getRandomValuesPolyfill };
82
+ function bytesToHex(bytes) {
83
+ let result = "";
84
+ for (let index = 0; index < bytes.length; index++) result += bytes[index].toString(16).padStart(2, "0");
85
+ return result;
86
+ }
87
+ /**
88
+ * @description 基于 `getRandomValues` 生成符合 RFC 4122 version 4 的 UUID。
89
+ */
90
+ function randomUUIDPolyfill() {
91
+ const bytes = /* @__PURE__ */ new Uint8Array(16);
92
+ getRandomValuesPolyfill(bytes);
93
+ bytes[6] = bytes[6] & 15 | 64;
94
+ bytes[8] = bytes[8] & 63 | 128;
95
+ const hex = bytesToHex(bytes);
96
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20, 32)}`;
97
+ }
98
+ const cryptoPolyfill = {
99
+ getRandomValues: getRandomValuesPolyfill,
100
+ randomUUID: randomUUIDPolyfill
101
+ };
94
102
  // src/performance.ts
95
103
  const performanceTimeOrigin = Date.now();
96
104
  const PERFORMANCE_POLYFILL_MARKER = "__weappVitePerformancePolyfill";
@@ -416,4 +424,4 @@ function installRequestGlobals(options = {}) {
416
424
  function installAbortGlobals() {
417
425
  return installWebRuntimeGlobals({ targets: ["AbortController", "AbortSignal"] });
418
426
  }
419
- export { AbortControllerPolyfill, AbortSignalPolyfill, BlobPolyfill, CustomEventPolyfill, EventPolyfill, EventSourcePolyfill, FilePolyfill, FormDataPolyfill, HeadersPolyfill, ReadableStreamPolyfill, RequestGlobalsEventTarget, RequestPolyfill, ResponsePolyfill, TextDecoderPolyfill, TextDecoderStreamPolyfill, TextEncoderPolyfill, TransformStreamPolyfill, URLPolyfill, URLSearchParamsPolyfill, WebSocketPolyfill, WritableStreamPolyfill, XMLHttpRequestPolyfill, atobPolyfill, btoaPolyfill, cryptoPolyfill, fetch, getMiniProgramNetworkDefaults, installAbortGlobals, installRequestGlobals, installWebRuntimeGlobals, performancePolyfill, queueMicrotaskPolyfill, resetMiniProgramNetworkDefaults, setMiniProgramNetworkDefaults };
427
+ export { AbortControllerPolyfill, AbortSignalPolyfill, BlobPolyfill, CustomEventPolyfill, EventPolyfill, EventSourcePolyfill, FilePolyfill, FormDataPolyfill, HeadersPolyfill, ReadableStreamPolyfill, RequestGlobalsEventTarget, RequestPolyfill, ResponsePolyfill, TextDecoderPolyfill, TextDecoderStreamPolyfill, TextEncoderPolyfill, TransformStreamPolyfill, URLPolyfill, URLSearchParamsPolyfill, WebSocketPolyfill, WritableStreamPolyfill, XMLHttpRequestPolyfill, atobPolyfill, btoaPolyfill, cryptoPolyfill, fetch, getMiniProgramNetworkDefaults, installAbortGlobals, installRequestGlobals, installWebRuntimeGlobals, performancePolyfill, queueMicrotaskPolyfill, randomUUIDPolyfill, resetMiniProgramNetworkDefaults, setMiniProgramNetworkDefaults };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wevu/web-apis",
3
3
  "type": "module",
4
- "version": "1.4.3",
4
+ "version": "1.5.0",
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",
@@ -87,9 +87,9 @@
87
87
  "node": "^22.23.4 || ^24.22.1 || >=26.12.1"
88
88
  },
89
89
  "dependencies": {
90
- "@weapp-core/api": "0.4.2",
91
- "@weapp-core/constants": "0.5.3",
92
- "@wevu/api": "0.4.2",
90
+ "@weapp-core/api": "0.4.3",
91
+ "@weapp-core/constants": "0.5.4",
92
+ "@wevu/api": "0.4.3",
93
93
  "web-streams-polyfill": "4.5.1"
94
94
  },
95
95
  "publishConfig": {