chore(review): 1105 realtime·Service Worker 조치 브랜치를 합친다
This commit is contained in:
@@ -11,6 +11,7 @@ import path from "node:path";
|
||||
|
||||
import { getBuildConfig } from "../src/contracts/env.ts";
|
||||
import {
|
||||
BUILD_ASSET_DIRECTORY,
|
||||
HASHED_STATIC_ASSET_NAME,
|
||||
SERVICE_WORKER_BOUNDS,
|
||||
SERVICE_WORKER_SCRIPT_PATH,
|
||||
@@ -19,7 +20,8 @@ import {
|
||||
|
||||
/**
|
||||
* §17.2.2 step 4. Scans the completed app `dist` and emits the exact hashed
|
||||
* asset list the Service Worker will verify at install time.
|
||||
* asset list under `BUILD_ASSET_DIRECTORY` the Service Worker will verify at
|
||||
* install time.
|
||||
*
|
||||
* `service-worker.js` itself and `index.html` are excluded (§17.2.2), as are
|
||||
* the runtime config and release manifest, which are network-only (§18.4).
|
||||
@@ -41,7 +43,12 @@ const EXCLUDED_FILES: ReadonlySet<string> = new Set([
|
||||
"runtime-config.schema.json",
|
||||
]);
|
||||
|
||||
/** Vite emits content-hashed names; only those may be treated as immutable. */
|
||||
/**
|
||||
* Vite emits content-hashed names under its assets directory; only those may
|
||||
* be treated as immutable. A `public/` file copied to the dist root keeps its
|
||||
* own name even when it looks hashed (`android-chrome-192x192.png`), and a
|
||||
* product may change its bytes under that name (1105/06-01).
|
||||
*/
|
||||
const HASHED_NAME = HASHED_STATIC_ASSET_NAME;
|
||||
|
||||
/**
|
||||
@@ -59,7 +66,10 @@ export async function collectStaticAssets(
|
||||
|
||||
for (const relative of files.sort()) {
|
||||
const base = path.basename(relative);
|
||||
if (EXCLUDED_FILES.has(base) || relative.startsWith(".vite/")) continue;
|
||||
if (!relative.split(path.sep).join("/").startsWith(BUILD_ASSET_DIRECTORY)) {
|
||||
continue;
|
||||
}
|
||||
if (EXCLUDED_FILES.has(base)) continue;
|
||||
const contentType = CACHEABLE_EXTENSIONS[path.extname(base).toLowerCase()];
|
||||
if (!contentType || !HASHED_NAME.test(base)) continue;
|
||||
|
||||
|
||||
@@ -2,7 +2,10 @@ import { isIP } from "node:net";
|
||||
import path from "node:path";
|
||||
import { connect } from "node:tls";
|
||||
|
||||
import type { InstalledServiceWorkerSelection } from "../../src/contracts/service-worker.ts";
|
||||
import {
|
||||
BUILD_ASSET_DIRECTORY,
|
||||
type InstalledServiceWorkerSelection,
|
||||
} from "../../src/contracts/service-worker.ts";
|
||||
import { CACHEABLE_ASSET_CONTENT_TYPES } from "../../src/contracts/service-worker-static-manifest.ts";
|
||||
import { changedRuntimeConfigBuildFields } from "./runtime-config-switches.ts";
|
||||
|
||||
@@ -272,17 +275,26 @@ export function acceptedAssetContentTypes(extension: string): readonly string[]
|
||||
* 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.
|
||||
*
|
||||
* 1105/06-02. The install fetch also refuses any redirect (`redirect:
|
||||
* "error"`), so an asset answered only after one fails here too, whichever
|
||||
* redirect mode the fetcher uses.
|
||||
*
|
||||
* It runs for a build without a worker as well, on purpose: under `nosniff`
|
||||
* a script or style of another type breaks the page anyway, and a later
|
||||
* release that turns the worker on needs no new hosting check. A font or
|
||||
* image of another type fails this probe although such a page still loads it.
|
||||
*/
|
||||
export async function probeAssetContentTypes(
|
||||
base: URL,
|
||||
distFiles: readonly string[],
|
||||
fetcher: HostingFetcher = (url) => fetch(url, { redirect: "follow" }),
|
||||
fetcher: HostingFetcher = (url) => fetch(url, { redirect: "manual" }),
|
||||
): 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/") &&
|
||||
file.startsWith(BUILD_ASSET_DIRECTORY) &&
|
||||
acceptedAssetContentTypes(extension).length > 0 &&
|
||||
!samples.has(extension)
|
||||
) {
|
||||
@@ -293,11 +305,14 @@ export async function probeAssetContentTypes(
|
||||
for (const [extension, file] of [...samples].sort(([left], [right]) => left.localeCompare(right))) {
|
||||
const expected = acceptedAssetContentTypes(extension);
|
||||
let observed: string;
|
||||
let redirected = false;
|
||||
try {
|
||||
const response = await fetcher(new URL(file, base));
|
||||
await response.body?.cancel().catch(() => undefined);
|
||||
observed =
|
||||
response.status === 200
|
||||
redirected = response.redirected || response.type === "opaqueredirect";
|
||||
observed = redirected
|
||||
? "redirected"
|
||||
: response.status === 200
|
||||
? (response.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase() ?? "")
|
||||
: `HTTP ${response.status}`;
|
||||
} catch (error: unknown) {
|
||||
@@ -308,12 +323,39 @@ export async function probeAssetContentTypes(
|
||||
header: "content-type",
|
||||
expected,
|
||||
observed,
|
||||
passed: expected.includes(observed),
|
||||
...(redirected
|
||||
? { reason: "the worker's install fetch refuses a redirected asset" }
|
||||
: {}),
|
||||
passed: !redirected && expected.includes(observed),
|
||||
});
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1105/06-02. Everything live mode of `verify:hosting-headers` asks the
|
||||
* deployed host: the release surfaces (`probeLiveRelease`) and one built asset
|
||||
* per cacheable type (`probeAssetContentTypes`). Live mode refuses loopback
|
||||
* and plain http, so tests reach it only through this function and a fetcher
|
||||
* double; a check dropped from here fails a test.
|
||||
*/
|
||||
export async function probeLiveHosting(
|
||||
base: URL,
|
||||
paths: Readonly<Record<string, string>>,
|
||||
candidate: CandidateRelease,
|
||||
distFiles: readonly string[],
|
||||
transport: ProbeTransport = {},
|
||||
): Promise<{ responses: ResponseHeaders; results: ProbeResult[] }> {
|
||||
const probed = await probeLiveRelease(base, paths, candidate, transport);
|
||||
return {
|
||||
responses: probed.responses,
|
||||
results: [
|
||||
...probed.results,
|
||||
...(await probeAssetContentTypes(base, distFiles, transport.fetcher)),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
/** 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,8 +7,7 @@ import {
|
||||
checkPublicServiceWorkers,
|
||||
classifyLiveHostingBaseUrl,
|
||||
hostingVerdict,
|
||||
probeAssetContentTypes,
|
||||
probeLiveRelease,
|
||||
probeLiveHosting,
|
||||
releaseIdentity,
|
||||
shipsServiceWorker,
|
||||
type HostingMode,
|
||||
@@ -111,13 +110,12 @@ if (liveTarget?.passed && !slotEmpty) {
|
||||
runtimeConfig: await readFile(path.join(distRoot, "config.json"), "utf8"),
|
||||
indexHtml: await readFile(path.join(distRoot, "index.html"), "utf8"),
|
||||
};
|
||||
const probed = await probeLiveRelease(liveTarget.url, paths, candidate, {
|
||||
// 1103/06-03, 1105/06-02. Release surfaces and every precached asset type.
|
||||
const probed = await probeLiveHosting(liveTarget.url, paths, candidate, distFiles, {
|
||||
alpnProtocols: strings(recordValue(cachePolicy.transport).alpnProtocols),
|
||||
});
|
||||
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