1
0
Fork 0
mirror of https://github.com/mastodon/mastodon.git synced 2025-04-17 22:32:17 +02:00

add asset integrity key to file

This commit is contained in:
ChaosExAnima 2025-04-01 12:51:45 +02:00
parent 612b7241ef
commit 48630518e1
No known key found for this signature in database
GPG key ID: 8F2B333100FB6117

View file

@ -18,6 +18,7 @@ export interface Options {
declare module 'vite' {
interface ManifestChunk {
integrity: string;
assetIntegrity: Record<string, string>;
}
}
@ -46,17 +47,30 @@ async function augmentManifest(
.readFile(manifestPath, 'utf-8')
.then((file) => JSON.parse(file) as Manifest);
if (manifest) {
await Promise.all(
Object.values(manifest).map(async (chunk) => {
chunk.integrity = integrityForAsset(
await fs.readFile(resolveInOutDir(chunk.file)),
algorithms,
);
}),
);
await fs.writeFile(manifestPath, JSON.stringify(manifest, null, 2));
if (!manifest) {
throw new Error(`Manifest file not found at ${manifestPath}`);
}
await Promise.all(
Object.values(manifest).map(async (chunk) => {
chunk.integrity = integrityForAsset(
await fs.readFile(resolveInOutDir(chunk.file)),
algorithms,
);
if (!chunk.assets && !chunk.css) {
return;
}
chunk.assetIntegrity = {};
await Promise.all(
(chunk.assets ?? []).concat(chunk.css ?? []).map(async (asset) => {
chunk.assetIntegrity[asset] = integrityForAsset(
await fs.readFile(resolveInOutDir(asset)),
algorithms,
);
}),
);
}),
);
await fs.writeFile(manifestPath, JSON.stringify(manifest, null, 2));
}
function integrityForAsset(source: Buffer, algorithms: string[]) {