Repository navigation
Expand file tree
/
Copy pathfetch-remote-assets.mjs
More file actions
257 lines (222 loc) · 8.36 KB
/
Copy pathfetch-remote-assets.mjs
File metadata and controls
257 lines (222 loc) · 8.36 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { createHash } from 'node:crypto';
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import { withRetries } from './retry.mjs';
const root = process.cwd();
const manifestPath = path.join(root, 'config', 'remote-assets.json');
const timeoutMs = 15000;
// CI starts without any fetched copies, so a failed download must fail the build there.
const allowLocalFallback = !process.env.CI;
const manifest = JSON.parse(await readFile(manifestPath, 'utf8'));
const seenIds = new Set();
const seenTargets = new Set();
const jsonKinds = ['string', 'number', 'boolean'];
function assertManifest(condition, message) {
if (!condition) throw new Error(`Remote asset manifest error: ${message}`);
}
function validateManifestAsset(asset) {
assertManifest(asset && typeof asset === 'object', 'each asset must be an object');
assertManifest(typeof asset.id === 'string' && /^[a-z0-9-]+$/.test(asset.id), 'invalid id');
assertManifest(!seenIds.has(asset.id), `duplicate id "${asset.id}"`);
seenIds.add(asset.id);
assertManifest(
typeof asset.description === 'string' && asset.description.length > 0,
`${asset.id} missing description`,
);
assertManifest(
typeof asset.source === 'string' && asset.source.startsWith('https://'),
`${asset.id} source must be HTTPS`,
);
assertManifest(
typeof asset.target === 'string' && asset.target.startsWith('public/'),
`${asset.id} target must be inside public/`,
);
assertManifest(!asset.target.includes('..'), `${asset.id} target must not traverse directories`);
assertManifest(!path.isAbsolute(asset.target), `${asset.id} target must be relative`);
assertManifest(!seenTargets.has(asset.target), `duplicate target "${asset.target}"`);
seenTargets.add(asset.target);
assertManifest(
['svg', 'asciinema-cast', 'pdf', 'json'].includes(asset.type),
`${asset.id} has unsupported type`,
);
if (asset.type === 'json') {
assertManifest(
asset.shape &&
typeof asset.shape === 'object' &&
Object.keys(asset.shape).length > 0 &&
Object.values(asset.shape).every((kind) => jsonKinds.includes(kind)),
`${asset.id} shape must map each required key to one of ${jsonKinds.join(', ')}`,
);
// `match` pins a string value's format, so a placeholder such as "unknown" or "N/A" fails.
for (const [key, pattern] of Object.entries(asset.match ?? {})) {
assertManifest(
asset.shape[key] === 'string',
`${asset.id} match key "${key}" needs a string shape`,
);
assertManifest(
typeof pattern === 'string',
`${asset.id} match for "${key}" must be a regex string`,
);
new RegExp(pattern); // throws on an invalid pattern
}
} else {
assertManifest(asset.shape === undefined, `${asset.id} shape applies only to json assets`);
assertManifest(asset.match === undefined, `${asset.id} match applies only to json assets`);
}
assertManifest(
Number.isInteger(asset.minBytes) && asset.minBytes > 0,
`${asset.id} minBytes must be positive`,
);
}
function validateSvg(asset, text) {
if (!text.includes('<svg') || !text.includes('</svg>')) {
throw new Error(`${asset.id}: expected an SVG document from ${asset.source}`);
}
}
function validateCast(asset, text) {
const firstLine = text.split(/\r?\n/, 1)[0];
let header;
try {
header = JSON.parse(firstLine);
} catch (error) {
throw new Error(`${asset.id}: expected a JSON Asciinema v2 header`, { cause: error });
}
if (header.version !== 2 || !Number.isFinite(header.width) || !Number.isFinite(header.height)) {
throw new Error(`${asset.id}: expected an Asciinema v2 header with width and height`);
}
}
function validateJson(asset, text) {
let document;
try {
document = JSON.parse(text);
} catch (error) {
throw new Error(`${asset.id}: expected a JSON document from ${asset.source}`, { cause: error });
}
if (!document || typeof document !== 'object' || Array.isArray(document)) {
throw new Error(`${asset.id}: expected a JSON object from ${asset.source}`);
}
for (const [key, kind] of Object.entries(asset.shape)) {
if (typeof document[key] !== kind) {
throw new Error(`${asset.id}: expected "${key}" to be a ${kind} in ${asset.source}`);
}
}
for (const [key, pattern] of Object.entries(asset.match ?? {})) {
if (!new RegExp(pattern).test(document[key])) {
throw new Error(
`${asset.id}: "${key}" is ${JSON.stringify(document[key])}, not /${pattern}/`,
);
}
}
}
function validatePdf(asset, bytes) {
if (Buffer.from(bytes.subarray(0, 5)).toString('latin1') !== '%PDF-') {
throw new Error(`${asset.id}: expected a PDF document from ${asset.source}`);
}
}
function validateAsset(asset, bytes) {
if (asset.type === 'pdf') {
validatePdf(asset, bytes);
return;
}
const text = new TextDecoder('utf8', { fatal: true }).decode(bytes);
if (asset.type === 'svg') validateSvg(asset, text);
if (asset.type === 'asciinema-cast') validateCast(asset, text);
if (asset.type === 'json') validateJson(asset, text);
}
async function fetchWithTimeout(url) {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
try {
return await fetch(url, {
signal: controller.signal,
headers: {
'user-agent': 'jackson-ferguson-portfolio-build',
},
});
} finally {
clearTimeout(timer);
}
}
async function fetchAsset(asset) {
return withRetries(`${asset.id} download`, async () => {
const response = await fetchWithTimeout(asset.source);
if (!response.ok) {
throw new Error(`${asset.id}: ${response.status} ${response.statusText}`);
}
const bytes = new Uint8Array(await response.arrayBuffer());
if (bytes.byteLength < asset.minBytes) {
throw new Error(
`${asset.id}: fetched ${bytes.byteLength} bytes, expected at least ${asset.minBytes}`,
);
}
validateAsset(asset, bytes);
return bytes;
});
}
async function writeIfChanged(targetPath, bytes) {
const current = await readFile(targetPath).catch(() => null);
if (current && Buffer.compare(current, Buffer.from(bytes)) === 0) return false;
await mkdir(path.dirname(targetPath), { recursive: true });
const temporaryPath = `${targetPath}.tmp-${process.pid}`;
try {
await writeFile(temporaryPath, bytes);
await rename(temporaryPath, targetPath);
return true;
} catch (error) {
await rm(temporaryPath, { force: true }).catch(() => {});
throw error;
}
}
// Offline local work can keep using the last copy that passed validation.
async function readFallback(asset, targetPath, error) {
if (!allowLocalFallback) throw error;
const bytes = await readFile(targetPath).catch(() => null);
if (!bytes) throw error;
try {
validateAsset(asset, bytes);
} catch {
throw error;
}
console.warn(`warning: ${error.message}; keeping the existing ${asset.target}`);
return bytes;
}
assertManifest(Array.isArray(manifest.assets), 'assets must be an array');
manifest.assets.forEach(validateManifestAsset);
// Assets download in parallel, so an outage costs one retry window rather than one per asset.
const results = await Promise.all(
manifest.assets.map(async (asset) => {
const targetPath = path.join(root, asset.target);
let bytes;
let status;
try {
bytes = await fetchAsset(asset);
status = (await writeIfChanged(targetPath, bytes)) ? 'updated' : 'current';
} catch (error) {
bytes = await readFallback(asset, targetPath, error);
status = 'kept';
}
return {
id: asset.id,
source: asset.source,
target: asset.target,
size: bytes.byteLength,
sha256: createHash('sha256').update(bytes).digest('hex'),
status,
};
}),
);
// Astro copies this alongside the assets, so the live site records its own snapshot.
// The commit lets the daily check redeploy a main commit whose own deployment never finished.
const snapshot = {
version: 1,
commit: process.env.GITHUB_SHA ?? null,
assets: results.map(({ id, source, target, sha256 }) => ({ id, source, target, sha256 })),
};
await writeIfChanged(
path.join(root, 'public', 'remote-assets-state.json'),
Buffer.from(`${JSON.stringify(snapshot, null, 2)}\n`),
);
for (const result of results) {
console.log(`${result.status} ${result.target} ${result.size} bytes sha256:${result.sha256}`);
}