@wevu/web-apis 1.2.0 → 1.2.1
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 +39 -9
- package/dist/shared.mjs +14 -5
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -7,8 +7,16 @@ import { BlobPolyfill, FormDataPolyfill } from "./web.mjs";
|
|
|
7
7
|
import { WebSocketPolyfill } from "./websocket.mjs";
|
|
8
8
|
import { XMLHttpRequestPolyfill } from "./xhr.mjs";
|
|
9
9
|
//#region src/index.ts
|
|
10
|
+
function resolveActualBindingTargets(targets) {
|
|
11
|
+
const bindingTargets = [...targets];
|
|
12
|
+
if (targets.some((target) => target === "fetch" || target === "Request" || target === "Response" || target === "XMLHttpRequest")) bindingTargets.push("URL", "URLSearchParams", "Blob", "FormData");
|
|
13
|
+
return [...new Set(bindingTargets)];
|
|
14
|
+
}
|
|
15
|
+
function isPlaceholderRequestGlobal(value) {
|
|
16
|
+
return Boolean(value && typeof value === "function" && value.__weappViteRequestGlobalsPlaceholder__ === true);
|
|
17
|
+
}
|
|
10
18
|
function hasUsableConstructor(value, args = []) {
|
|
11
|
-
if (typeof value !== "function") return false;
|
|
19
|
+
if (typeof value !== "function" || isPlaceholderRequestGlobal(value)) return false;
|
|
12
20
|
try {
|
|
13
21
|
Reflect.construct(value, args);
|
|
14
22
|
return true;
|
|
@@ -18,34 +26,34 @@ function hasUsableConstructor(value, args = []) {
|
|
|
18
26
|
}
|
|
19
27
|
function installSingleTarget(host, target) {
|
|
20
28
|
if (target === "fetch") {
|
|
21
|
-
if (typeof host.fetch !== "function") host.fetch = fetch;
|
|
29
|
+
if (typeof host.fetch !== "function" || isPlaceholderRequestGlobal(host.fetch)) host.fetch = fetch;
|
|
22
30
|
return;
|
|
23
31
|
}
|
|
24
32
|
if (target === "Headers") {
|
|
25
|
-
if (typeof host.Headers !== "function") host.Headers = HeadersPolyfill;
|
|
33
|
+
if (typeof host.Headers !== "function" || isPlaceholderRequestGlobal(host.Headers)) host.Headers = HeadersPolyfill;
|
|
26
34
|
return;
|
|
27
35
|
}
|
|
28
36
|
if (target === "Request") {
|
|
29
|
-
if (typeof host.Request !== "function") host.Request = RequestPolyfill;
|
|
37
|
+
if (typeof host.Request !== "function" || isPlaceholderRequestGlobal(host.Request)) host.Request = RequestPolyfill;
|
|
30
38
|
return;
|
|
31
39
|
}
|
|
32
40
|
if (target === "Response") {
|
|
33
|
-
if (typeof host.Response !== "function") host.Response = ResponsePolyfill;
|
|
41
|
+
if (typeof host.Response !== "function" || isPlaceholderRequestGlobal(host.Response)) host.Response = ResponsePolyfill;
|
|
34
42
|
return;
|
|
35
43
|
}
|
|
36
44
|
if (target === "AbortController") {
|
|
37
|
-
if (typeof host.AbortController !== "function") host.AbortController = AbortControllerPolyfill;
|
|
45
|
+
if (typeof host.AbortController !== "function" || isPlaceholderRequestGlobal(host.AbortController)) host.AbortController = AbortControllerPolyfill;
|
|
38
46
|
return;
|
|
39
47
|
}
|
|
40
48
|
if (target === "AbortSignal") {
|
|
41
|
-
if (typeof host.AbortSignal !== "function") host.AbortSignal = AbortSignalPolyfill;
|
|
49
|
+
if (typeof host.AbortSignal !== "function" || isPlaceholderRequestGlobal(host.AbortSignal)) host.AbortSignal = AbortSignalPolyfill;
|
|
42
50
|
return;
|
|
43
51
|
}
|
|
44
52
|
if (target === "WebSocket") {
|
|
45
|
-
if (typeof host.WebSocket !== "function") host.WebSocket = WebSocketPolyfill;
|
|
53
|
+
if (typeof host.WebSocket !== "function" || isPlaceholderRequestGlobal(host.WebSocket)) host.WebSocket = WebSocketPolyfill;
|
|
46
54
|
return;
|
|
47
55
|
}
|
|
48
|
-
if (target === "XMLHttpRequest" && typeof host.XMLHttpRequest !== "function") host.XMLHttpRequest = XMLHttpRequestPolyfill;
|
|
56
|
+
if (target === "XMLHttpRequest" && (typeof host.XMLHttpRequest !== "function" || isPlaceholderRequestGlobal(host.XMLHttpRequest))) host.XMLHttpRequest = XMLHttpRequestPolyfill;
|
|
49
57
|
}
|
|
50
58
|
function installUrlGlobals(host) {
|
|
51
59
|
if (!hasUsableConstructor(host.URL, ["https://request-globals.invalid"])) host.URL = URLPolyfill;
|
|
@@ -58,6 +66,26 @@ function installGlobalBindingIfNeeded(host, target) {
|
|
|
58
66
|
if (value == null) return;
|
|
59
67
|
installRequestGlobalBinding(target, value);
|
|
60
68
|
}
|
|
69
|
+
function ensureRuntimeHostAliases(host) {
|
|
70
|
+
for (const alias of [
|
|
71
|
+
"global",
|
|
72
|
+
"self",
|
|
73
|
+
"window"
|
|
74
|
+
]) {
|
|
75
|
+
try {
|
|
76
|
+
host[alias] = host;
|
|
77
|
+
} catch {}
|
|
78
|
+
installRequestGlobalBinding(alias, host);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function syncWeappViteRequestGlobalsActuals(host, targets) {
|
|
82
|
+
const globalObject = resolveRequestGlobalsHost();
|
|
83
|
+
const actuals = globalObject.__weappViteRequestGlobalsActuals && typeof globalObject.__weappViteRequestGlobalsActuals === "object" ? globalObject.__weappViteRequestGlobalsActuals : globalObject.__weappViteRequestGlobalsActuals = Object.create(null);
|
|
84
|
+
for (const target of resolveActualBindingTargets(targets)) {
|
|
85
|
+
const value = host[target];
|
|
86
|
+
if (value != null) actuals[target] = value;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
61
89
|
/**
|
|
62
90
|
* @description 按需向小程序全局环境注入缺失的请求相关对象。
|
|
63
91
|
*/
|
|
@@ -75,6 +103,7 @@ function installRequestGlobals(options = {}) {
|
|
|
75
103
|
const hosts = resolveRequestGlobalsHosts();
|
|
76
104
|
const primaryHost = resolveRequestGlobalsHost();
|
|
77
105
|
const needsUrlGlobals = targets.some((target) => target === "fetch" || target === "Request" || target === "Response" || target === "XMLHttpRequest");
|
|
106
|
+
ensureRuntimeHostAliases(primaryHost);
|
|
78
107
|
for (const host of hosts) {
|
|
79
108
|
if (needsUrlGlobals) installUrlGlobals(host);
|
|
80
109
|
for (const target of targets) installSingleTarget(host, target);
|
|
@@ -86,6 +115,7 @@ function installRequestGlobals(options = {}) {
|
|
|
86
115
|
installGlobalBindingIfNeeded(primaryHost, "FormData");
|
|
87
116
|
}
|
|
88
117
|
for (const target of targets) installGlobalBindingIfNeeded(primaryHost, target);
|
|
118
|
+
syncWeappViteRequestGlobalsActuals(primaryHost, targets);
|
|
89
119
|
return hosts[0];
|
|
90
120
|
}
|
|
91
121
|
/**
|
package/dist/shared.mjs
CHANGED
|
@@ -26,18 +26,27 @@ function resolveRequestGlobalsHost() {
|
|
|
26
26
|
if (typeof globalThis !== "undefined") return globalThis;
|
|
27
27
|
return {};
|
|
28
28
|
}
|
|
29
|
+
function isRequestGlobalsHostCandidate(value) {
|
|
30
|
+
return 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
|
+
}
|
|
29
36
|
function resolveRequestGlobalsHosts() {
|
|
30
37
|
const hosts = [];
|
|
31
38
|
const primaryHost = resolveRequestGlobalsHost();
|
|
32
|
-
hosts
|
|
39
|
+
pushRequestGlobalsHost(hosts, primaryHost);
|
|
40
|
+
for (const name of [
|
|
41
|
+
"global",
|
|
42
|
+
"self",
|
|
43
|
+
"window"
|
|
44
|
+
]) pushRequestGlobalsHost(hosts, primaryHost[name]);
|
|
33
45
|
for (const key of [
|
|
34
46
|
"wx",
|
|
35
47
|
"my",
|
|
36
48
|
"tt"
|
|
37
|
-
])
|
|
38
|
-
const candidate = primaryHost[key];
|
|
39
|
-
if (candidate && typeof candidate === "object" && !hosts.includes(candidate)) hosts.push(candidate);
|
|
40
|
-
}
|
|
49
|
+
]) pushRequestGlobalsHost(hosts, primaryHost[key]);
|
|
41
50
|
return hosts;
|
|
42
51
|
}
|
|
43
52
|
function installRequestGlobalBinding(name, value) {
|
package/package.json
CHANGED