Skip to content

Commit

Permalink
reflect review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
haxibami committed Jun 19, 2024
1 parent ba669b9 commit 5ac7fd8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
17 changes: 9 additions & 8 deletions scripts/unused-asset.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<string>}
*/
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));
});
}
5 changes: 3 additions & 2 deletions src/lib/AssetFileNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]";
};
Expand Down
15 changes: 3 additions & 12 deletions src/lib/CopyAssetIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5ac7fd8

Please sign in to comment.