-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.mjs
More file actions
139 lines (130 loc) · 3.79 KB
/
main.mjs
File metadata and controls
139 lines (130 loc) · 3.79 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import integrityData from "./integrity.json" with { type: "json" };
const defaultUserAgent =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36";
const targets = new Set([
"es2015",
"es2016",
"es2017",
"es2018",
"es2019",
"es2020",
"es2021",
"es2022",
"es2023",
"es2024",
"esnext",
"deno",
"denonext",
"node",
]);
async function checkWebsite() {
const res = await fetchEsmsh("https://esm.sh");
if (res.headers.get("content-type") !== "text/html; charset=utf-8") {
throw new Error("Invalid content type");
}
const html = await res.text();
if (!html.includes("</html>")) {
throw new Error("Bad HTML");
}
}
async function checkServerStatus() {
const res = await fetchEsmsh("https://esm.sh/status.json?t=" + Date.now().toString(36));
try {
const json = await res.json();
if (json.disk !== "ok") {
throw new Error("Disk " + json.disk);
}
if (!json.uptime) {
throw new Error("Bad status.json");
}
} catch (error) {
throw new Error("Bad status.json");
}
}
async function checkModules() {
for (const item of integrityData.list) {
const url = new URL(item.url);
if (url.origin !== "https://esm.sh") {
throw new Error(`Invalid origin: ${url.origin}`);
}
if (!/@\d+\.\d+\.\d+/.test(url.pathname)) {
throw new Error(`Exact version required: ${url.pathname}`);
}
if (item.target) {
if (!targets.has(item.target)) {
throw new Error(`Invalid target: ${item.target}`);
}
url.searchParams.set("target", item.target);
}
const res = await fetchEsmsh(url, item.userAgent);
const [argorithm, hash] = item.integrity.split("-", 2);
if (!/^sha\d+$/i.test(argorithm)) {
throw new Error(`Invalid hash algorithm: ${argorithm}`);
}
const buf = await crypto.subtle.digest("SHA-" + argorithm.slice(3), await res.arrayBuffer());
const digest = Array.from(new Uint8Array(buf))
.map((x) => x.toString(16).padStart(2, "0"))
.join("");
if (digest !== hash) {
throw new Error(`Integrity check failed: ${item.url}, want ${hash}, got ${digest}`);
}
}
}
async function fetchEsmsh(url, userAgent) {
const doFetch = () =>
new Promise((resolve, reject) => {
const ac = new AbortController();
setTimeout(() => {
ac.abort();
reject(new Error(`Fetch ${url}: timeout`));
}, 10000); // 10s
fetch(url, { headers: { "User-Agent": userAgent ?? defaultUserAgent }, signal: ac.signal }).then(res => {
if (!res.ok) {
reject(new Error(`Fetch ${url}: ${res.status}`));
}
resolve(res);
}, reject);
});
try {
return await doFetch();
} catch (error) {
// retry once after 500ms
await new Promise(resolve => setTimeout(resolve, 500));
return await doFetch();
}
}
function reportError(monitorType, env) {
return function(error) {
console.error(`⚠️ [${monitorType}]`, error.message);
return error;
};
}
async function check(env) {
const errors = await Promise.all([
checkWebsite().catch(reportError("website", env)),
checkServerStatus().catch(reportError("server", env)),
checkModules().catch(reportError("CDN", env)),
]);
return errors.filter(Boolean);
}
if (import.meta.main) {
const errors = await check(Deno.env.toObject());
if (errors.length === 0) {
console.log("✅ All checks passed");
Deno.exit(0);
} else {
Deno.exit(1);
}
}
export default {
async scheduled(event, env, ctx) {
await check(env);
},
async fetch(request, env) {
const errors = await check(env);
if (errors.length === 0) {
return new Response("✅ All checks passed", { status: 200 });
}
return Response.json({ errors: errors.map(e => e.message) }, { status: 500 });
},
};