Skip to content

Commit 11e7080

Browse files
committed
refactor(sri): extract sriOf(bytes) helper for sha384 prefix
1 parent 9046b99 commit 11e7080

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

scripts/walkthrough.mts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ function applyBasePath(html: string, basePath: string, slug: string): string {
152152
return out
153153
}
154154

155+
/** Build the `sha384-<base64>` SRI attribute value for a byte stream. */
156+
function sriOf(bytes: Uint8Array): string {
157+
return `sha384-${cryptoHash('sha384', bytes, 'base64')}`
158+
}
159+
155160
/**
156161
* Compute / look up the SRI hash for a CDN URL. Disk-cached under
157162
* `.cache/sri/<base64url(url)>.txt` so repeat builds don't refetch.
@@ -170,8 +175,7 @@ async function sriForUrl(url: string, cacheDir: string): Promise<string> {
170175
if (!res.ok) {
171176
throw new Error(`SRI fetch ${url} → HTTP ${res.status}`)
172177
}
173-
const bytes = new Uint8Array(await res.arrayBuffer())
174-
const integrity = `sha384-${cryptoHash('sha384', bytes, 'base64')}`
178+
const integrity = sriOf(new Uint8Array(await res.arrayBuffer()))
175179
mkdirSync(cacheDir, { recursive: true })
176180
writeFileSync(cachePath, integrity + '\n')
177181
return integrity

0 commit comments

Comments
 (0)