@wevu/web-apis 1.2.1 → 1.2.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/index.mjs +28 -15
- package/dist/shared.mjs +3 -2
- package/package.json +4 -1
package/dist/index.mjs
CHANGED
|
@@ -6,6 +6,7 @@ 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";
|
|
9
|
+
import { REQUEST_GLOBAL_ACTUALS_KEY, REQUEST_GLOBAL_PLACEHOLDER_KEY } from "@weapp-core/constants";
|
|
9
10
|
//#region src/index.ts
|
|
10
11
|
function resolveActualBindingTargets(targets) {
|
|
11
12
|
const bindingTargets = [...targets];
|
|
@@ -13,7 +14,7 @@ function resolveActualBindingTargets(targets) {
|
|
|
13
14
|
return [...new Set(bindingTargets)];
|
|
14
15
|
}
|
|
15
16
|
function isPlaceholderRequestGlobal(value) {
|
|
16
|
-
return Boolean(value && typeof value === "function" && value
|
|
17
|
+
return Boolean(value && typeof value === "function" && value[REQUEST_GLOBAL_PLACEHOLDER_KEY] === true);
|
|
17
18
|
}
|
|
18
19
|
function hasUsableConstructor(value, args = []) {
|
|
19
20
|
if (typeof value !== "function" || isPlaceholderRequestGlobal(value)) return false;
|
|
@@ -24,42 +25,50 @@ function hasUsableConstructor(value, args = []) {
|
|
|
24
25
|
return false;
|
|
25
26
|
}
|
|
26
27
|
}
|
|
28
|
+
function assignHostGlobal(host, key, value) {
|
|
29
|
+
try {
|
|
30
|
+
host[key] = value;
|
|
31
|
+
return true;
|
|
32
|
+
} catch {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
27
36
|
function installSingleTarget(host, target) {
|
|
28
37
|
if (target === "fetch") {
|
|
29
|
-
if (typeof host.fetch !== "function" || isPlaceholderRequestGlobal(host.fetch)) host
|
|
38
|
+
if (typeof host.fetch !== "function" || isPlaceholderRequestGlobal(host.fetch)) assignHostGlobal(host, "fetch", fetch);
|
|
30
39
|
return;
|
|
31
40
|
}
|
|
32
41
|
if (target === "Headers") {
|
|
33
|
-
if (typeof host.Headers !== "function" || isPlaceholderRequestGlobal(host.Headers)) host
|
|
42
|
+
if (typeof host.Headers !== "function" || isPlaceholderRequestGlobal(host.Headers)) assignHostGlobal(host, "Headers", HeadersPolyfill);
|
|
34
43
|
return;
|
|
35
44
|
}
|
|
36
45
|
if (target === "Request") {
|
|
37
|
-
if (typeof host.Request !== "function" || isPlaceholderRequestGlobal(host.Request)) host
|
|
46
|
+
if (typeof host.Request !== "function" || isPlaceholderRequestGlobal(host.Request)) assignHostGlobal(host, "Request", RequestPolyfill);
|
|
38
47
|
return;
|
|
39
48
|
}
|
|
40
49
|
if (target === "Response") {
|
|
41
|
-
if (typeof host.Response !== "function" || isPlaceholderRequestGlobal(host.Response)) host
|
|
50
|
+
if (typeof host.Response !== "function" || isPlaceholderRequestGlobal(host.Response)) assignHostGlobal(host, "Response", ResponsePolyfill);
|
|
42
51
|
return;
|
|
43
52
|
}
|
|
44
53
|
if (target === "AbortController") {
|
|
45
|
-
if (typeof host.AbortController !== "function" || isPlaceholderRequestGlobal(host.AbortController)) host
|
|
54
|
+
if (typeof host.AbortController !== "function" || isPlaceholderRequestGlobal(host.AbortController)) assignHostGlobal(host, "AbortController", AbortControllerPolyfill);
|
|
46
55
|
return;
|
|
47
56
|
}
|
|
48
57
|
if (target === "AbortSignal") {
|
|
49
|
-
if (typeof host.AbortSignal !== "function" || isPlaceholderRequestGlobal(host.AbortSignal)) host
|
|
58
|
+
if (typeof host.AbortSignal !== "function" || isPlaceholderRequestGlobal(host.AbortSignal)) assignHostGlobal(host, "AbortSignal", AbortSignalPolyfill);
|
|
50
59
|
return;
|
|
51
60
|
}
|
|
52
61
|
if (target === "WebSocket") {
|
|
53
|
-
if (typeof host.WebSocket !== "function" || isPlaceholderRequestGlobal(host.WebSocket)) host
|
|
62
|
+
if (typeof host.WebSocket !== "function" || isPlaceholderRequestGlobal(host.WebSocket)) assignHostGlobal(host, "WebSocket", WebSocketPolyfill);
|
|
54
63
|
return;
|
|
55
64
|
}
|
|
56
|
-
if (target === "XMLHttpRequest" && (typeof host.XMLHttpRequest !== "function" || isPlaceholderRequestGlobal(host.XMLHttpRequest))) host
|
|
65
|
+
if (target === "XMLHttpRequest" && (typeof host.XMLHttpRequest !== "function" || isPlaceholderRequestGlobal(host.XMLHttpRequest))) assignHostGlobal(host, "XMLHttpRequest", XMLHttpRequestPolyfill);
|
|
57
66
|
}
|
|
58
67
|
function installUrlGlobals(host) {
|
|
59
|
-
if (!hasUsableConstructor(host.URL, ["https://request-globals.invalid"])) host
|
|
60
|
-
if (!hasUsableConstructor(host.URLSearchParams, ["client=graphql-request"])) host
|
|
61
|
-
if (!hasUsableConstructor(host.Blob)) host
|
|
62
|
-
if (!hasUsableConstructor(host.FormData)) host
|
|
68
|
+
if (!hasUsableConstructor(host.URL, ["https://request-globals.invalid"])) assignHostGlobal(host, "URL", URLPolyfill);
|
|
69
|
+
if (!hasUsableConstructor(host.URLSearchParams, ["client=graphql-request"])) assignHostGlobal(host, "URLSearchParams", URLSearchParamsPolyfill);
|
|
70
|
+
if (!hasUsableConstructor(host.Blob)) assignHostGlobal(host, "Blob", BlobPolyfill);
|
|
71
|
+
if (!hasUsableConstructor(host.FormData)) assignHostGlobal(host, "FormData", FormDataPolyfill);
|
|
63
72
|
}
|
|
64
73
|
function installGlobalBindingIfNeeded(host, target) {
|
|
65
74
|
const value = host[target];
|
|
@@ -79,8 +88,12 @@ function ensureRuntimeHostAliases(host) {
|
|
|
79
88
|
}
|
|
80
89
|
}
|
|
81
90
|
function syncWeappViteRequestGlobalsActuals(host, targets) {
|
|
82
|
-
const globalObject = resolveRequestGlobalsHost();
|
|
83
|
-
|
|
91
|
+
const globalObject = host != null && (typeof host === "object" || typeof host === "function") ? host : resolveRequestGlobalsHost();
|
|
92
|
+
let actuals = globalObject[REQUEST_GLOBAL_ACTUALS_KEY];
|
|
93
|
+
if (!actuals || typeof actuals !== "object") {
|
|
94
|
+
actuals = Object.create(null);
|
|
95
|
+
assignHostGlobal(globalObject, REQUEST_GLOBAL_ACTUALS_KEY, actuals);
|
|
96
|
+
}
|
|
84
97
|
for (const target of resolveActualBindingTargets(targets)) {
|
|
85
98
|
const value = host[target];
|
|
86
99
|
if (value != null) actuals[target] = value;
|
package/dist/shared.mjs
CHANGED
|
@@ -27,7 +27,7 @@ function resolveRequestGlobalsHost() {
|
|
|
27
27
|
return {};
|
|
28
28
|
}
|
|
29
29
|
function isRequestGlobalsHostCandidate(value) {
|
|
30
|
-
return typeof value === "object" || typeof value === "function";
|
|
30
|
+
return value != null && (typeof value === "object" || typeof value === "function");
|
|
31
31
|
}
|
|
32
32
|
function pushRequestGlobalsHost(hosts, candidate) {
|
|
33
33
|
if (!isRequestGlobalsHostCandidate(candidate)) return;
|
|
@@ -52,7 +52,8 @@ function resolveRequestGlobalsHosts() {
|
|
|
52
52
|
function installRequestGlobalBinding(name, value) {
|
|
53
53
|
if (!name) return;
|
|
54
54
|
try {
|
|
55
|
-
|
|
55
|
+
const host = resolveRequestGlobalsHost();
|
|
56
|
+
host[name] = value;
|
|
56
57
|
} catch {}
|
|
57
58
|
}
|
|
58
59
|
function cloneArrayBuffer(buffer) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wevu/web-apis",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.2.
|
|
4
|
+
"version": "1.2.3",
|
|
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,6 +70,7 @@
|
|
|
70
70
|
"node": "^20.19.0 || >=22.12.0"
|
|
71
71
|
},
|
|
72
72
|
"dependencies": {
|
|
73
|
+
"@weapp-core/constants": "0.1.0",
|
|
73
74
|
"@wevu/api": "0.2.3"
|
|
74
75
|
},
|
|
75
76
|
"publishConfig": {
|
|
@@ -81,6 +82,8 @@
|
|
|
81
82
|
"build": "tsdown",
|
|
82
83
|
"test": "vitest run",
|
|
83
84
|
"test:dev": "vitest",
|
|
85
|
+
"pretest:types": "pnpm build",
|
|
86
|
+
"test:types": "tsd",
|
|
84
87
|
"typecheck": "tsc --noEmit"
|
|
85
88
|
}
|
|
86
89
|
}
|