Files
DongHyeonka ea6a5da997 fix(service-worker): manifest releaseId에 ref 모양 값을 받는다
manifest 검증이 releaseId를 buildId 규칙으로 봐서 `/`를 거절한다. CI와
verify.sh의 RELEASE_ID는 `refs/…`라서 Service Worker를 ACTIVE로 켠
제품의 build가 실패한다. worker를 켜는 e2e fixture는 RELEASE_ID에 `/`
없는 build id를 줘서 잡지 못했다.

releaseId는 제어문자 없는 1~512자로 따로 받고 buildId 규칙은 둔다.
worker는 releaseId로 cache 이름이나 URL을 만들지 않는다. worker fixture
build는 CI 모양 RELEASE_ID로 돌고 update spec이 그 값이 release
manifest에 남았는지 단언한다. CI 값에서 `/`를 빼는 안은 release 증거의
식별값이 바뀌고, `/`만 더하는 안은 `+`·`@`·한글 ref에서 다시 실패해
버렸다.

위험: worker bundle에 든 releaseId가 길어질 수 있다(최대 512자).

검증(노트북): 단위 시험 파일이 옛 규칙에서 9개 실패하고 고친 뒤 32개
통과한다. 규칙 경계를 바꾼 변이 여섯도 실패한다. types·lint 통과.
고치기 전 ACTIVE build는 desktop에서 refs/heads 값으로
`buildId or releaseId is invalid`로 실패했다.

Refs: REV-FE-0041
2026-11-23 03:21:11 +09:00

147 lines
5.4 KiB
TypeScript

import { spawnSync } from "node:child_process";
import { cp, readFile, rename, rm, writeFile } from "node:fs/promises";
import path from "node:path";
import { localBuildEnvironment } from "../../../scripts/lib/build-environment.ts";
import { linkFixtureNodeModules } from "../../../scripts/lib/fixture-node-modules.ts";
import { selectRepositoryPaths } from "../../../scripts/lib/repository-layout.ts";
import { sharedFixtureBuild } from "./shared-fixture-builds.ts";
/**
* Real production builds of an edited temp copy of the repository, in the
* template's default configuration (`serviceWorker: null`). `edit` changes the
* copy right before that build runs, and edits persist into later builds. The
* working tree and its `dist` (served by the e2e preview server) are never
* touched. Serve the result with `startBuildServer` from
* `service-worker-builds.ts`.
*/
const repoRoot = path.resolve(import.meta.dirname, "../../..");
const skipped = new Set(selectRepositoryPaths("checkout-copy-skip"));
export type FixtureBuild<Name extends string> = Readonly<{
name: Name;
edit?: (root: string) => Promise<void>;
/** Extra build variables for this build only (e.g. `APP_PROFILE`). */
env?: Readonly<Record<string, string>>;
}>;
export type FixtureBuilds<Name extends string> = Readonly<{
root: string;
dist: Readonly<Record<Name, string>>;
/** One copy for the whole e2e run (shared-fixture-builds.ts); the run removes it. */
shared: boolean;
}>;
/** Replaces the one occurrence of `search` in `file` of the copy. */
export async function editOnce(
file: string,
search: string,
replacement: string,
): Promise<void> {
const source = await readFile(file, "utf8");
if (source.split(search).length !== 2) {
throw new Error(`Expected exactly one "${search}" in ${file}.`);
}
await writeFile(file, source.replace(search, replacement));
}
function run(
cwd: string,
script: string,
buildId: string,
env: Readonly<Record<string, string>> = {},
): void {
const result = spawnSync("node", [script], {
cwd,
encoding: "utf8",
// 1103/11-08. A local build, not the CI job's release build.
env: { ...localBuildEnvironment(), RELEASE_ID: buildId, ...env, VITE_BUILD_ID: buildId },
});
if (result.status !== 0) {
throw new Error(
`${script} failed for ${buildId}:\n${result.stdout}\n${result.stderr}`,
);
}
}
export type EditedCopyBuild<Name extends string> = Readonly<{
name: Name;
/** VITE_BUILD_ID of this build, and its RELEASE_ID unless `env` names one. */
buildId: string;
edit?: (root: string) => Promise<void>;
env?: Readonly<Record<string, string>>;
}>;
/**
* 0929/11-07. The one way an e2e spec gets real production builds of an
* edited copy of the checkout; prepareFixtureBuilds and
* prepareServiceWorkerBuilds (service-worker-builds.ts) are its two shapes.
* It copies the checkout (without `checkout-copy-skip`), links node_modules,
* runs `prepare` once, then for each build runs `edit`, builds, moves `dist`
* to `dist-<name>` and asks `collect` what the spec needs from that build.
* `key` and `fingerprint` name the result in the e2e run
* (shared-fixture-builds.ts), which removes the copy when preparing fails.
*/
export async function buildEditedCopies<Name extends string, Collected = null>(
input: Readonly<{
key: string;
fingerprint: string;
prepare?: (root: string) => Promise<void>;
builds: readonly EditedCopyBuild<Name>[];
collect?: (name: Name, root: string) => Promise<Collected>;
}>,
): Promise<Readonly<{
root: string;
dist: Readonly<Record<Name, string>>;
collected: Readonly<Record<Name, Collected>>;
shared: boolean;
}>> {
const { root, value, shared } = await sharedFixtureBuild(
input.key,
input.fingerprint,
async (root) => {
await cp(repoRoot, root, {
recursive: true,
filter: (source) =>
!skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!),
});
await linkFixtureNodeModules(root, repoRoot);
await input.prepare?.(root);
const dist = {} as Record<Name, string>;
const collected = {} as Record<Name, Collected>;
for (const build of input.builds) {
await build.edit?.(root);
run(root, "scripts/generate-product-feature-index.ts", build.buildId, build.env);
run(root, "scripts/build-frontend.ts", build.buildId, build.env);
dist[build.name] = path.join(root, `dist-${build.name}`);
await rename(path.join(root, "dist"), dist[build.name]);
collected[build.name] = input.collect ? await input.collect(build.name, root) : (null as Collected);
}
return { dist, collected };
},
);
return { root, ...value, shared };
}
export async function prepareFixtureBuilds<Name extends string>(
prefix: string,
builds: readonly FixtureBuild<Name>[],
): Promise<FixtureBuilds<Name>> {
// An edit's source text names the build (two specs never share a prefix).
const { root, dist, shared } = await buildEditedCopies({
key: `fixture-${prefix}`,
fingerprint: JSON.stringify(
builds.map((build) => [build.name, build.env ?? {}, build.edit?.toString() ?? ""]),
),
builds: builds.map((build) => ({ ...build, buildId: `${prefix}-${build.name}` })),
});
return { root, dist, shared };
}
export async function removeFixtureBuilds(
builds: Readonly<{ root: string; shared: boolean }> | undefined,
): Promise<void> {
if (builds && !builds.shared) await rm(builds.root, { recursive: true, force: true });
}