-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.js
More file actions
110 lines (98 loc) · 3.13 KB
/
bootstrap.js
File metadata and controls
110 lines (98 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const BUILD_ID = "20260325c";
function mergeRuntimeConfig(patch) {
if (!patch || typeof patch !== "object") return;
const current = window.MONOBRICK_RUNTIME && typeof window.MONOBRICK_RUNTIME === "object" ? window.MONOBRICK_RUNTIME : {};
const currentEndpoints =
current.endpoints && typeof current.endpoints === "object" ? current.endpoints : {};
const patchEndpoints = patch.endpoints && typeof patch.endpoints === "object" ? patch.endpoints : {};
window.MONOBRICK_RUNTIME = {
...current,
...patch,
endpoints: {
...currentEndpoints,
...patchEndpoints
}
};
}
function withBuildVersion(urlLike) {
const url = new URL(urlLike, window.location.href);
if (!url.searchParams.has("v")) {
url.searchParams.set("v", BUILD_ID);
}
return url;
}
async function probeResource(url) {
try {
const response = await fetch(url, { method: "GET", cache: "no-store" });
if (!response.ok) return null;
return response;
} catch {
return null;
}
}
async function loadOptionalRuntimeJson() {
const jsonUrl = withBuildVersion(new URL("../runtime-config.json", import.meta.url));
const response = await probeResource(jsonUrl);
if (!response) return false;
try {
const parsed = await response.json();
mergeRuntimeConfig(parsed);
return true;
} catch {
return false;
}
}
async function injectScript(src, type = "text/javascript") {
return new Promise((resolve) => {
const script = document.createElement("script");
script.src = src;
script.type = type;
script.async = false;
script.onload = () => resolve(true);
script.onerror = () => resolve(false);
document.head.append(script);
});
}
async function loadOptionalScript(url, type = "text/javascript") {
return injectScript(url.toString(), type);
}
async function loadOptionalRuntimeScript() {
const scriptUrl = withBuildVersion(new URL("../runtime-config.js", import.meta.url));
const probe = await probeResource(scriptUrl);
if (!probe) return false;
return loadOptionalScript(scriptUrl);
}
function getWalletAdapterCandidates() {
const runtime = window.MONOBRICK_RUNTIME && typeof window.MONOBRICK_RUNTIME === "object" ? window.MONOBRICK_RUNTIME : {};
const candidates = [];
const runtimeUrl = typeof runtime.walletAdapterUrl === "string" ? runtime.walletAdapterUrl.trim() : "";
if (runtimeUrl) {
try {
candidates.push(withBuildVersion(new URL(runtimeUrl, window.location.href)));
} catch {
// Ignore invalid runtime wallet adapter URL.
}
}
return candidates;
}
async function loadOptionalWalletAdapter() {
const candidates = getWalletAdapterCandidates();
for (const url of candidates) {
const loaded = await loadOptionalScript(url);
if (loaded && window.MONOBRICK_WALLET_ADAPTER) return true;
}
try {
if (window.MONOBRICK_WALLET_ADAPTER) return true;
} catch {
return false;
}
return false;
}
async function bootstrap() {
window.MONOBRICK_RUNTIME = window.MONOBRICK_RUNTIME || {};
await loadOptionalRuntimeJson();
await loadOptionalRuntimeScript();
await loadOptionalWalletAdapter();
await import(`./app.js?v=${BUILD_ID}`);
}
bootstrap();