fix(hosting): live probe가 사전 캐시 확장자마다 Content-Type을 확인한다
문제
워커 install은 사전 캐시 자산마다 응답 MIME이 manifest 값
(CACHEABLE_ASSET_CONTENT_TYPES: .css .js .mjs .png .svg .webp .woff2)과
정확히 같아야 하고, 하나라도 틀리면 release 전체를 거절한다. 호스팅
live probe는 index가 가리키는 entry script 하나만 요청하고
hashedAsset.contentTypes도 JavaScript뿐이다. 그래서 CSS를 text/plain,
글꼴을 octet-stream으로 보내는 호스트도 게이트는 PASS다. 페이지는
글꼴을 그대로 쓰므로 워커만 조용히 설치되지 않는다.
고른 방법
- probeAssetContentTypes를 더한다. 후보 dist의 assets/에서 확장자마다
첫 파일(이름순)을 요청하고, 그 확장자의 워커 표 MIME과 비교한다.
결과는 surface hashedAsset.<ext>, header content-type이다.
verify-hosting-headers live 모드에서만 부른다.
- 기대 MIME은 워커 표에서 바로 읽는다. .js 계열은 hashedAsset surface와
같이 application/javascript도 받는다. 두 목록이 같다는 것을 테스트가
지킨다(acceptedAssetContentTypes(".js") = cache-policy hashedAsset).
- 운영 문서(release-cache-rollback.md)에 새 검사를 적는다.
- 버린 대안: (b) 워커가 비JS 자산 MIME을 느슨하게 받기 — 캐시에서 꺼낸
응답이 틀린 MIME을 그대로 갖고 설계 §18 확인 순서 4와 어긋난다.
(c) 사전 캐시를 JS·CSS로 줄이기 — 글꼴 캐시 이득을 잃는다. (d) 문서만
— 검출이 없다(리뷰 5.2).
위험·한계
- live probe 요청이 확장자 수만큼 는다(지금 빌드는 .js·.css 2개).
- 확장자마다 파일 하나만 본다. 같은 확장자 안에서 경로마다 MIME이
다른 호스트는 잡지 못한다.
- cache-control은 이 자산들에서 다시 보지 않는다. entry script의
hashedAsset 검사가 그대로 본다.
- 실제 호스트로 live 모드를 돌리지 않았다. fetcher 대역으로만 확인했다.
- 하위 경로 base는 probe가 아직 거절한다(0929/13-06, 다른 영역). 새
함수는 base 기준 상대 경로로 요청하므로 그 수정 뒤에도 그대로 쓴다.
검증
- 새 테스트 4건을 옛 코드에 돌림: 4건 실패(RED). 새 코드:
hosting-probe·service-worker-build-output 테스트 통과.
- CSS를 text/plain으로 보내는 호스트 → hashedAsset.css FAIL.
- check:types:node·check:types:test 통과, 바꾼 파일 eslint 통과.
Refs: docs/reviews/2026-11-03-maturity-review/06-adapters-service-worker-push.md 06-03
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import path from "node:path";
|
||||
|
||||
import type { InstalledServiceWorkerSelection } from "../../src/contracts/service-worker.ts";
|
||||
import { CACHEABLE_ASSET_CONTENT_TYPES } from "../../src/contracts/service-worker-static-manifest.ts";
|
||||
|
||||
const LOOPBACK_IPV4 = /^127(?:\.\d{1,3}){3}$/;
|
||||
|
||||
@@ -148,6 +151,68 @@ export async function probeLiveRelease(
|
||||
return { responses, results };
|
||||
}
|
||||
|
||||
/**
|
||||
* 1103/06-03. The types the probe accepts for a hashed asset: the worker's
|
||||
* table (`CACHEABLE_ASSET_CONTENT_TYPES`), plus `application/javascript`,
|
||||
* which servers still send for `.js` (R-6.5) and the `hashedAsset` surface in
|
||||
* `config/hosting/cache-policy.json` also accepts.
|
||||
*/
|
||||
export function acceptedAssetContentTypes(extension: string): readonly string[] {
|
||||
const type = Object.hasOwn(CACHEABLE_ASSET_CONTENT_TYPES, extension)
|
||||
? CACHEABLE_ASSET_CONTENT_TYPES[extension]
|
||||
: undefined;
|
||||
if (type === undefined) return [];
|
||||
return type === "text/javascript" ? [type, "application/javascript"] : [type];
|
||||
}
|
||||
|
||||
/**
|
||||
* 1103/06-03. The worker's install compares every precached asset's
|
||||
* Content-Type with the manifest's and refuses the whole release on one
|
||||
* mismatch, while a page still uses, say, a font served as octet-stream. The
|
||||
* entry script alone does not show that, so the live probe requests the first
|
||||
* built `assets/` file of each cacheable extension.
|
||||
*/
|
||||
export async function probeAssetContentTypes(
|
||||
base: URL,
|
||||
distFiles: readonly string[],
|
||||
fetcher: HostingFetcher = (url) => fetch(url, { redirect: "follow" }),
|
||||
): Promise<ProbeResult[]> {
|
||||
const samples = new Map<string, string>();
|
||||
for (const file of distFiles.map((entry) => entry.split(path.sep).join("/")).sort()) {
|
||||
const extension = path.posix.extname(file).toLowerCase();
|
||||
if (
|
||||
file.startsWith("assets/") &&
|
||||
acceptedAssetContentTypes(extension).length > 0 &&
|
||||
!samples.has(extension)
|
||||
) {
|
||||
samples.set(extension, file);
|
||||
}
|
||||
}
|
||||
const results: ProbeResult[] = [];
|
||||
for (const [extension, file] of [...samples].sort(([left], [right]) => left.localeCompare(right))) {
|
||||
const expected = acceptedAssetContentTypes(extension);
|
||||
let observed: string;
|
||||
try {
|
||||
const response = await fetcher(new URL(file, base));
|
||||
await response.body?.cancel().catch(() => undefined);
|
||||
observed =
|
||||
response.status === 200
|
||||
? (response.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase() ?? "")
|
||||
: `HTTP ${response.status}`;
|
||||
} catch (error: unknown) {
|
||||
observed = error instanceof Error ? error.name : "UnknownError";
|
||||
}
|
||||
results.push({
|
||||
surface: `hashedAsset${extension}`,
|
||||
header: "content-type",
|
||||
expected,
|
||||
observed,
|
||||
passed: expected.includes(observed),
|
||||
});
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/** A build ships a worker script for these selections (06-03: REMOVE_REGISTRATION ships the self-unregistering one). */
|
||||
export function shipsServiceWorker(selection: InstalledServiceWorkerSelection | null): boolean {
|
||||
return selection?.mode === "ACTIVE" || selection?.mode === "REMOVE_REGISTRATION";
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
checkPublicServiceWorkers,
|
||||
classifyLiveHostingBaseUrl,
|
||||
hostingVerdict,
|
||||
probeAssetContentTypes,
|
||||
probeLiveRelease,
|
||||
releaseIdentity,
|
||||
shipsServiceWorker,
|
||||
@@ -106,6 +107,8 @@ if (liveTarget?.passed && !slotEmpty) {
|
||||
const probed = await probeLiveRelease(liveTarget.url, paths, candidate);
|
||||
responses = probed.responses;
|
||||
probeResults.push(...probed.results);
|
||||
// 1103/06-03. The worker needs every precached asset kind typed exactly.
|
||||
probeResults.push(...(await probeAssetContentTypes(liveTarget.url, distFiles)));
|
||||
} else if (liveTarget && !liveTarget.passed) {
|
||||
mode = "invalid-live";
|
||||
probeResults.push({
|
||||
|
||||
Reference in New Issue
Block a user