Merge lane/F: service worker / web push (06-01~06-10, F-8)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
donghyeon-ka
2026-09-27 13:03:51 +09:00
co-authored by Claude Opus 5.5
31 changed files with 1424 additions and 158 deletions
+16 -3
View File
@@ -1,9 +1,11 @@
import { spawnSync } from "node:child_process";
import { rm } from "node:fs/promises";
import { rm, writeFile } from "node:fs/promises";
import process from "node:process";
import { SERVICE_WORKER_SCRIPT_PATH } from "../src/contracts/service-worker.ts";
import { INSTALLED_RUNTIME_CAPABILITIES } from "../src/features/installed-runtime-capabilities.ts";
import { makeDistFilesPublic } from "./lib/dist-permissions.ts";
import { SELF_UNREGISTERING_SERVICE_WORKER } from "./lib/self-unregistering-service-worker.ts";
/**
* §17.2.2. Deterministic two-pass build.
@@ -17,17 +19,22 @@ import { makeDistFilesPublic } from "./lib/dist-permissions.ts";
* 4. materialize dist/config.json from the declared APP_PROFILE
* 5. scan app dist and generate the static asset source
* 6. ACTIVE only: Vite Service Worker build (emptyOutDir = false)
* REMOVE_REGISTRATION only: write the self-unregistering worker (06-03)
* 7. generate Release Manifest V2 and the build manifest
* 8. make every dist file readable by other users (13-02)
*
* Steps 5 and 6 are skipped for `REMOVE_REGISTRATION`, `PURGE_OWNED_RESOURCES`
* and `null`: those modes never run an active worker build.
* `REMOVE_REGISTRATION` still ships a `service-worker.js` so the browser's
* update check can replace a worker that stops the page from booting (D-5).
*
* Step 4 has to follow the Vite build. Vite copies `public/` verbatim, so
* without it every build — including a production one — ships the local
* runtime document. The Service Worker asset scan excludes `config.json`
* (`scripts/generate-service-worker-assets.ts`), so the order against step 5
* is not what keeps the profile out of the worker cache (13-15).
* is not what keeps the profile out of the worker cache (13-15). Step 6 reads
* the release manifest URL from the `dist/config.json` step 4 wrote, and fails
* if its `APP_ENV` is not `APP_PROFILE` (06-10).
*/
const selection = INSTALLED_RUNTIME_CAPABILITIES.serviceWorker;
@@ -62,13 +69,19 @@ if (buildsActiveWorker) {
run("node", ["scripts/generate-service-worker-assets.ts", "dist"]);
// 6. service worker build
run("npx", ["vite", "build", "--config", "vite.service-worker.config.ts"]);
} else if (selection?.mode === "REMOVE_REGISTRATION") {
// 6. self-unregistering worker in place of the active one
await writeFile(
`dist/${SERVICE_WORKER_SCRIPT_PATH}`,
SELF_UNREGISTERING_SERVICE_WORKER,
);
} else {
process.stdout.write(
`service worker mode ${selection?.mode ?? "null"}: skipping worker build\n`,
);
}
// 6. release + build manifest
// 7. release + build manifest
run("node", ["scripts/generate-build-manifest.ts"]);
// 8. publishable permissions: the validated writer creates 0600 files
+1
View File
@@ -33,6 +33,7 @@ const telemetryProducerFiles: Readonly<Record<string, string>> = {
"api.request.failed": "src/adapters/http/client.ts",
"ui.render.failed": "src/application/create-application.ts",
"release.mismatch.detected": "src/application/create-application.ts",
"service-worker.start.failed": "src/bootstrap/optional-runtime-host.ts",
"telemetry.delivery.dropped":
"src/adapters/telemetry/best-effort-telemetry.ts",
};
+5 -2
View File
@@ -32,7 +32,8 @@ const PUBLIC_SERVICE_WORKER = /(?:^|\/)(?:service-worker|sw)(?:[.-][^/]*)?\.js$/
/**
* A built `dist` may ship a worker script only when the static selection is
* `ACTIVE`, and then only the selected script at the app root.
* `ACTIVE` or `REMOVE_REGISTRATION` (the self-unregistering worker, 06-03), and
* then only the selected script at the app root.
*/
export function checkPublicServiceWorkers(
distFiles: readonly string[],
@@ -40,7 +41,9 @@ export function checkPublicServiceWorkers(
) {
const found = distFiles.filter((file) => PUBLIC_SERVICE_WORKER.test(file));
const allowed: string[] =
selection?.mode === "ACTIVE" ? [selection.scriptPath] : [];
selection?.mode === "ACTIVE" || selection?.mode === "REMOVE_REGISTRATION"
? [selection.scriptPath]
: [];
const passed =
found.length === allowed.length &&
found.every((file) => allowed.includes(file));
@@ -0,0 +1,17 @@
/**
* 06-03 (D-5). The emergency exit for a worker that stops the page from
* booting. A `REMOVE_REGISTRATION` release writes this as `service-worker.js`.
* The browser's own update check (navigation, `updateViaCache: "none"`) then
* installs it over any worker of this scope without a page: it skips waiting,
* and on activation unregisters the registration. It has no fetch handler, so
* every request goes to the network. Owned caches stay for the
* `PURGE_OWNED_RESOURCES` release, as with the page-side removal.
*/
export const SELF_UNREGISTERING_SERVICE_WORKER = `// Generated by scripts/build-frontend.ts (REMOVE_REGISTRATION).
self.addEventListener("install", (event) => {
event.waitUntil(self.skipWaiting());
});
self.addEventListener("activate", (event) => {
event.waitUntil(self.registration.unregister());
});
`;
+11 -1
View File
@@ -29,7 +29,10 @@ export function resolveServiceWorkerBuildInput(input: Readonly<{
selection: InstalledServiceWorkerSelection | null;
assets: unknown;
contractSet: unknown;
/** `dist/config.json`, materialized from the build's profile (06-10). */
runtimeConfig: unknown;
/** The build's `APP_PROFILE`; the runtime document must be for it. */
appProfile: string;
buildId: string;
releaseId: string;
}>): ServiceWorkerBuildInput {
@@ -70,7 +73,14 @@ export function resolveServiceWorkerBuildInput(input: Readonly<{
throw new TypeError("Generated contract set digest is invalid.");
}
const runtimeConfig = record(input.runtimeConfig);
const releaseManifestUrl = runtimeConfig?.RELEASE_MANIFEST_URL;
// 06-10 / F-8. `public/config.json` is the local profile; a worker built from
// it for another profile would carry that profile's URLs.
if (runtimeConfig?.APP_ENV !== input.appProfile) {
throw new TypeError(
`Service Worker runtime config APP_ENV ${String(runtimeConfig?.APP_ENV)} does not match APP_PROFILE ${input.appProfile}.`,
);
}
const releaseManifestUrl = runtimeConfig.RELEASE_MANIFEST_URL;
if (
typeof releaseManifestUrl !== "string" ||
releaseManifestUrl.length === 0 ||