문제: 사전 캐시 수집기(`collectStaticAssets`)는 dist 전체를 돌고 파일 이름만 해시 정규식으로 본다. `public/` 파일은 이름 그대로 dist 루트에 복사되는데, `apple-touch-icon.png`, `android-chrome-192x192.png`, `safari-pinned-tab.svg`는 정규식에 걸려 immutable 자산으로 들어간다(리뷰 06-R1). 제품이 같은 이름으로 아이콘을 바꾸고 CDN이 옛 바이트를 주면 새 릴리스 install이 INTEGRITY_MISMATCH로 통째로 거절된다. 이전 릴리스 fallback, 호스팅 정책 `/assets/*`, MIME probe, 레시피 문서는 모두 `assets/`만 가정한다. 고른 방법: 리뷰 06-01의 (a). 계약 모듈에 `BUILD_ASSET_DIRECTORY = "assets/"` 하나를 두고, 수집기는 그 아래 파일만 받는다. 워커 fallback (`service-worker-static-assets.ts`)과 probe(`hosting-probe.ts`)도 같은 상수를 읽는다. `build.assetsDir`를 바꿀 때(0925/06-08) 이 상수 한 곳만 고치면 된다. 수집기의 `.vite/` 제외는 새 조건이 덮으므로 지웠다. 레시피 문서에 루트 파일이 빠진다는 문장을 더했다. 버린 대안: - 정규식을 Vite 해시 형식(8자 고정)으로 좁히기. 우연히 맞는 이름이 남는다. - 문서만 고치기. 아이콘 문제가 그대로다. 위험·한계: 루트에 해시 이름 파일을 일부러 두는 제품은 그 파일이 사전 캐시에서 빠진다. Vite는 그런 파일을 만들지 않는다. 상수는 vite 설정에서 읽지 않는다. `build.assetsDir`를 바꾸는 제품은 상수도 같이 바꿔야 한다 (0925/06-08은 그대로 남는다). 검증: desktop batch에서 확인. - node scripts/run-vitest.ts run tests/unit/service-worker-web-push-remediation.test.ts tests/unit/service-worker-runtime.test.ts tests/unit/hosting-probe.test.ts tests/unit/service-worker-build-output.test.ts - RED: "precaches only the assets directory, not public files at the dist root"가 옛 수집기에서 루트 아이콘 세 개를 받아 실패한다. Refs: 1105/06-01
242 lines
8.2 KiB
TypeScript
242 lines
8.2 KiB
TypeScript
import { afterEach, describe, expect, it } from "vitest";
|
|
|
|
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
|
|
import {
|
|
CACHEABLE_ASSET_CONTENT_TYPES,
|
|
decodeStaticAssetManifest,
|
|
isCanonicalStaticAssetUrl,
|
|
} from "../../src/contracts/service-worker-static-manifest.ts";
|
|
|
|
/**
|
|
* SW-RR-03. The build generator and the shared runtime decoder must agree on
|
|
* exactly which asset kinds exist. A generator that emits `.mjs` or `.png` while
|
|
* the decoder refuses them turns a correct build into a runtime contract
|
|
* failure, and the reverse admits a kind no build produces.
|
|
*/
|
|
describe("SW-RR-03 one authoritative cacheable asset table", () => {
|
|
it("covers every extension the generator emits", async () => {
|
|
const generator = await import(
|
|
"../../scripts/generate-service-worker-assets.ts"
|
|
);
|
|
expect(generator).toBeDefined();
|
|
for (const extension of [
|
|
".js",
|
|
".mjs",
|
|
".css",
|
|
".woff2",
|
|
".svg",
|
|
".png",
|
|
".webp",
|
|
]) {
|
|
expect(CACHEABLE_ASSET_CONTENT_TYPES[extension]).toBeDefined();
|
|
}
|
|
});
|
|
|
|
it("decodes a manifest row for every table entry", () => {
|
|
const assets = Object.entries(CACHEABLE_ASSET_CONTENT_TYPES).map(
|
|
([extension, contentType], index) => ({
|
|
url: `/assets/name-abcdefgh${index}${extension}`,
|
|
sha256: `sha256:${"a".repeat(64)}`,
|
|
bytes: 16,
|
|
contentType,
|
|
}),
|
|
);
|
|
const decoded = decodeStaticAssetManifest({
|
|
schemaVersion: 1,
|
|
buildId: "build-1",
|
|
releaseId: "release-1",
|
|
assets,
|
|
setDigest: `sha256:${"b".repeat(64)}`,
|
|
});
|
|
expect(decoded).toMatchObject({ ok: true });
|
|
});
|
|
|
|
it("refuses a row whose extension is not in the table", () => {
|
|
const decoded = decodeStaticAssetManifest({
|
|
schemaVersion: 1,
|
|
buildId: "build-1",
|
|
releaseId: "release-1",
|
|
setDigest: `sha256:${"b".repeat(64)}`,
|
|
assets: [
|
|
{
|
|
url: "/assets/control-abcdefgh.json",
|
|
sha256: `sha256:${"a".repeat(64)}`,
|
|
bytes: 16,
|
|
contentType: "application/json",
|
|
},
|
|
],
|
|
});
|
|
expect(decoded.ok).toBe(false);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* SW-02. Sharing only the extension table left the generator and the decoder
|
|
* with different path grammars: the generator emitted a URL for a directory
|
|
* containing an `@` or a space, and the decoder then refused the manifest it
|
|
* had just produced, failing the release build at install time.
|
|
*/
|
|
describe("SW-02 the generator and the decoder share one path grammar", () => {
|
|
// 00-03. The dist fixtures used to stay in /tmp.
|
|
const temporaryRoots: string[] = [];
|
|
afterEach(async () => {
|
|
await Promise.all(
|
|
temporaryRoots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
|
|
);
|
|
});
|
|
|
|
async function distWith(
|
|
files: Readonly<Record<string, string>>,
|
|
): Promise<string> {
|
|
const root = await mkdtemp(path.join(tmpdir(), "sw-assets-"));
|
|
temporaryRoots.push(root);
|
|
for (const [relative, content] of Object.entries(files)) {
|
|
const absolute = path.join(root, relative);
|
|
await mkdir(path.dirname(absolute), { recursive: true });
|
|
await writeFile(absolute, content);
|
|
}
|
|
return root;
|
|
}
|
|
|
|
it("emits a manifest the runtime decoder accepts for every table entry", async () => {
|
|
const { collectStaticAssets } = await import(
|
|
"../../scripts/generate-service-worker-assets.ts"
|
|
);
|
|
const files: Record<string, string> = {};
|
|
for (const [index, extension] of Object.keys(
|
|
CACHEABLE_ASSET_CONTENT_TYPES,
|
|
).entries()) {
|
|
files[`assets/name-abcdefg${index}${extension}`] = `content-${index}`;
|
|
}
|
|
files["assets/nested/deep/name-abcdefgz.js"] = "nested";
|
|
const root = await distWith(files);
|
|
|
|
const manifest = await collectStaticAssets(root, "build-1", "release-1");
|
|
|
|
expect(manifest.assets.length).toBe(
|
|
Object.keys(CACHEABLE_ASSET_CONTENT_TYPES).length + 1,
|
|
);
|
|
expect(decodeStaticAssetManifest(manifest)).toMatchObject({ ok: true });
|
|
for (const asset of manifest.assets) {
|
|
expect(isCanonicalStaticAssetUrl(asset.url)).toBe(true);
|
|
}
|
|
});
|
|
|
|
// 1105/06-01. `public/` files are copied to the dist root under their own
|
|
// names. A PWA icon such as `android-chrome-192x192.png` matches the hashed
|
|
// name pattern, but a product may change its bytes under the same name; a
|
|
// CDN that still serves the old bytes then fails every install with
|
|
// INTEGRITY_MISMATCH. Only Vite's assets directory holds hashed files.
|
|
it("precaches only the assets directory, not public files at the dist root", async () => {
|
|
const { collectStaticAssets } = await import(
|
|
"../../scripts/generate-service-worker-assets.ts"
|
|
);
|
|
const root = await distWith({
|
|
"index.html": "page",
|
|
"config.json": "{}",
|
|
"release-manifest.json": "{}",
|
|
"apple-touch-icon.png": "icon",
|
|
"android-chrome-192x192.png": "icon",
|
|
"safari-pinned-tab.svg": "icon",
|
|
"favicon.svg": "icon",
|
|
"robots.txt": "robots",
|
|
"icons/maskable-512x512.png": "icon",
|
|
"assets/index-AbCdEf12.js": "entry",
|
|
"assets/index-AbCdEf12.css": "style",
|
|
});
|
|
|
|
const manifest = await collectStaticAssets(root, "build-1", "release-1");
|
|
|
|
expect(manifest.assets.map((asset) => asset.url)).toEqual([
|
|
"/assets/index-AbCdEf12.css",
|
|
"/assets/index-AbCdEf12.js",
|
|
]);
|
|
});
|
|
|
|
// 0929/06-02. dist is served under the router base path; a root URL made
|
|
// the worker's install request /assets/... and fail under /app/.
|
|
it("puts every asset URL under the router base path", async () => {
|
|
const { collectStaticAssets } = await import(
|
|
"../../scripts/generate-service-worker-assets.ts"
|
|
);
|
|
const root = await distWith({
|
|
"assets/index-abcdefgh.js": "entry",
|
|
"assets/nested/chunk-abcdefgz.css": "style",
|
|
});
|
|
|
|
const manifest = await collectStaticAssets(
|
|
root,
|
|
"build-1",
|
|
"release-1",
|
|
"/app/",
|
|
);
|
|
|
|
expect(manifest.assets.map((asset) => asset.url)).toEqual([
|
|
"/app/assets/index-abcdefgh.js",
|
|
"/app/assets/nested/chunk-abcdefgz.css",
|
|
]);
|
|
expect(decodeStaticAssetManifest(manifest)).toMatchObject({ ok: true });
|
|
});
|
|
|
|
it.each([
|
|
{ label: "an at sign", name: "bad@name-abcdefgh.js" },
|
|
{ label: "a space", name: "bad name-abcdefgh.js" },
|
|
{ label: "a percent escape", name: "bad%20name-abcdefgh.js" },
|
|
{ label: "a hash", name: "bad#name-abcdefgh.js" },
|
|
])(
|
|
"stops the build rather than emitting a path the decoder refuses ($label)",
|
|
async ({ name }) => {
|
|
const { collectStaticAssets } = await import(
|
|
"../../scripts/generate-service-worker-assets.ts"
|
|
);
|
|
const root = await distWith({
|
|
"assets/good-abcdefgh.js": "ok",
|
|
[`assets/${name}`]: "bad",
|
|
});
|
|
|
|
await expect(
|
|
collectStaticAssets(root, "build-1", "release-1"),
|
|
).rejects.toThrow(/not canonical/u);
|
|
},
|
|
);
|
|
|
|
it("agrees with the decoder on the whole path policy table", () => {
|
|
const cases: readonly (readonly [string, boolean])[] = [
|
|
["/assets/name-abcdefgh.js", true],
|
|
["/assets/nested/name-abcdefgh.js", true],
|
|
["/assets/bad@name-abcdefgh.js", false],
|
|
["/assets/bad name-abcdefgh.js", false],
|
|
["/assets/bad%20name-abcdefgh.js", false],
|
|
["/assets/bad#name-abcdefgh.js", false],
|
|
["/assets/../name-abcdefgh.js", false],
|
|
["/assets/./name-abcdefgh.js", false],
|
|
["assets/name-abcdefgh.js", false],
|
|
["/assets/\\name-abcdefgh.js", false],
|
|
["/assets/naïve-abcdefgh.js", false],
|
|
];
|
|
for (const [url, canonical] of cases) {
|
|
expect(isCanonicalStaticAssetUrl(url), url).toBe(canonical);
|
|
const decoded = decodeStaticAssetManifest({
|
|
schemaVersion: 1,
|
|
buildId: "build-1",
|
|
releaseId: "release-1",
|
|
setDigest: `sha256:${"a".repeat(64)}`,
|
|
assets: [
|
|
{
|
|
url,
|
|
sha256: `sha256:${"a".repeat(64)}`,
|
|
bytes: 4,
|
|
contentType: "text/javascript",
|
|
},
|
|
],
|
|
});
|
|
// The decoder still owns the digest check, so a canonical path may fail
|
|
// for other reasons; a non-canonical one must always fail.
|
|
if (!canonical) expect(decoded.ok, url).toBe(false);
|
|
}
|
|
});
|
|
});
|