Repository navigation
Expand file tree
/
Copy pathverify-deployment.mjs
More file actions
69 lines (58 loc) · 2.1 KB
/
Copy pathverify-deployment.mjs
File metadata and controls
69 lines (58 loc) · 2.1 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
import { readdir } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
const deploymentUrl = process.argv[2];
if (!deploymentUrl) {
console.error('Usage: npm run verify:deployment -- https://talishar.net');
process.exit(2);
}
const assetsDirectory = fileURLToPath(
new URL('../build/assets/', import.meta.url)
);
const assetNames = (await readdir(assetsDirectory)).filter(
(assetName) => assetName.endsWith('.js') || assetName.endsWith('.css')
);
const baseUrl = new URL(
deploymentUrl.endsWith('/') ? deploymentUrl : `${deploymentUrl}/`
);
const expectedContentType = (assetName) =>
assetName.endsWith('.js') ? /javascript|ecmascript/i : /^text\/css/i;
const verifyAsset = async (assetName) => {
const url = new URL(`assets/${assetName}`, baseUrl);
try {
const response = await fetch(url, { method: 'HEAD', redirect: 'follow' });
const contentType = response.headers.get('content-type') ?? '';
if (!response.ok) return `${assetName}: HTTP ${response.status}`;
if (!expectedContentType(assetName).test(contentType)) {
return `${assetName}: received ${contentType || 'no Content-Type'}`;
}
return null;
} catch (error) {
return `${assetName}: ${error instanceof Error ? error.message : String(error)}`;
}
};
const failures = [];
const pending = [...assetNames];
const workerCount = Math.min(10, pending.length);
await Promise.all(
Array.from({ length: workerCount }, async () => {
while (pending.length > 0) {
const assetName = pending.shift();
if (!assetName) return;
const failure = await verifyAsset(assetName);
if (failure) failures.push(failure);
}
})
);
if (failures.length > 0) {
console.error(
`Deployment verification failed: ${failures.length} of ${assetNames.length} build assets are unavailable.`
);
failures.slice(0, 25).forEach((failure) => console.error(`- ${failure}`));
if (failures.length > 25) {
console.error(`- ...and ${failures.length - 25} more`);
}
process.exit(1);
}
console.log(
`Deployment verification passed: ${assetNames.length} build assets are available from ${baseUrl.origin}.`
);