From 5ac7fd841050ebc4893de87fd517d6af713a6729 Mon Sep 17 00:00:00 2001 From: haxibami Date: Wed, 19 Jun 2024 13:39:37 +0900 Subject: [PATCH] reflect review comments --- scripts/unused-asset.js | 17 +++++++++-------- src/lib/AssetFileNames.ts | 5 +++-- src/lib/CopyAssetIntegration.ts | 15 +++------------ 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/scripts/unused-asset.js b/scripts/unused-asset.js index f8436486f4..09b918284c 100644 --- a/scripts/unused-asset.js +++ b/scripts/unused-asset.js @@ -1,6 +1,6 @@ // @ts-check -import { subtle } from "node:crypto"; +import { createHash } from "node:crypto"; import { readFile } from "node:fs/promises"; import { extname, parse, relative, join } from "node:path"; import { glob } from "glob"; @@ -47,7 +47,7 @@ const [dists, srcs, optimizedImgs] = await Promise.all([ }), // Images that are optimized by Astro - // e.g. `(sha1sum).foo.(hash).webp` + // e.g. `(hash).foo.webp` glob(`**/*{${IMAGE_EXTS.join(",")}}`, { cwd: "dist/_astro", nodir: true, @@ -109,15 +109,16 @@ async function searchSrc(src) { */ async function isImgOptimized(src) { const buffer = await readFile(src); - const sum = await sha1Sum(buffer); - return optimizedImgs.some((optimizedImg) => optimizedImg.includes(sum)); + const sum = await getHash(buffer); + return optimizedImgs.some((optimizedImg) => optimizedImg.startsWith(sum)); } /** - * @param {BufferSource} buffer + * @param {import("node:crypto").BinaryLike} buffer * @returns {Promise} */ -async function sha1Sum(buffer) { - const hash = await subtle.digest("SHA-1", buffer); - return Buffer.from(hash).toString("base64url"); +async function getHash(buffer) { + return new Promise((resolve) => { + resolve(createHash("sha1").update(buffer).digest("base64url").slice(0, 8)); + }); } diff --git a/src/lib/AssetFileNames.ts b/src/lib/AssetFileNames.ts index f6f0858fdd..1e20aa350b 100644 --- a/src/lib/AssetFileNames.ts +++ b/src/lib/AssetFileNames.ts @@ -21,9 +21,10 @@ const assetFileNames = ({ name, source }: PreRenderedAsset): string => { IMAGE_EXTS.includes(parse(name).ext) && typeof source !== "string" ) { - return `_astro/${createHash("sha-1") + return `_astro/${createHash("sha1") .update(source) - .digest("base64url")}.[name].[hash][extname]`; + .digest("base64url") + .slice(0, 8)}.[name][extname]`; } return "_astro/[name].[hash][extname]"; }; diff --git a/src/lib/CopyAssetIntegration.ts b/src/lib/CopyAssetIntegration.ts index f8ac902cf8..ccf3b9adf7 100644 --- a/src/lib/CopyAssetIntegration.ts +++ b/src/lib/CopyAssetIntegration.ts @@ -2,7 +2,7 @@ import type { AstroIntegration } from "astro"; import { existsSync } from "node:fs"; import { copyFile, mkdir } from "node:fs/promises"; -import { dirname, join, parse, relative, resolve } from "node:path"; +import { dirname, join, parse, relative, resolve, sep } from "node:path"; import { fileURLToPath } from "node:url"; import { selectAll } from "hast-util-select"; import rehypeParse from "rehype-parse"; @@ -36,21 +36,12 @@ export default function CopyAssetIntegration(): AstroIntegration { for (const tag of selectAll("a", hast)) { const base = new URL( - relative("src/pages", dirname(component)), + join(relative("src/pages", dirname(component)), sep), ORIGINS[0], ); const href = tag.properties?.href; if (typeof href !== "string") continue; - const absoluteUrl = new URL( - URL.canParse(href) - ? href - : resolve( - "/", - relative("src/pages", dirname(component)), - href, - ), - base, - ); + const absoluteUrl = new URL(href, base); if (!ORIGINS.includes(absoluteUrl.origin)) continue; const assetPath = decodeURI(absoluteUrl.pathname);