refactor: 도메인이 100개 1000개씩 늘어나도 유지보수 쉬운 구조로 변경
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
import { assertSafeConfigNames, getBuildConfig } from "../contracts/env.ts";
|
||||
import { checkReleaseCoherence } from "../platform/release/release-coherence.ts";
|
||||
import {
|
||||
BOOT_JSON_POLICIES,
|
||||
readBoundedBootJson,
|
||||
type BootLoadFailure,
|
||||
} from "./read-bounded-boot-json.ts";
|
||||
import {
|
||||
validateRuntimeConfig,
|
||||
type RuntimeConfig,
|
||||
} from "./runtime-config-schema.ts";
|
||||
|
||||
/**
|
||||
* §6.6. A safe boot error carries the failure kind, the build identity and a
|
||||
* support reference. It never carries a URL, a response body, a validation
|
||||
* value, an endpoint hostname or a stack trace.
|
||||
*/
|
||||
export type BootConfigSafe = Readonly<{
|
||||
kind: "BOOT_CONFIG_FAILURE";
|
||||
code: string;
|
||||
buildId: string;
|
||||
configSchemaVersion?: string;
|
||||
releaseId?: string;
|
||||
supportReference: string;
|
||||
}>;
|
||||
|
||||
type BootConfigSafeInput = Readonly<{
|
||||
buildId: string;
|
||||
configSchemaVersion?: string;
|
||||
releaseId?: string;
|
||||
}>;
|
||||
|
||||
export class BootConfigError extends Error {
|
||||
readonly kind = "BOOT_CONFIG_FAILURE" as const;
|
||||
readonly code: string;
|
||||
readonly safe: BootConfigSafe;
|
||||
|
||||
constructor(code: string, safe: BootConfigSafeInput) {
|
||||
super("Runtime configuration could not be loaded");
|
||||
this.name = "BootConfigError";
|
||||
this.code = code;
|
||||
this.safe = Object.freeze({
|
||||
kind: this.kind,
|
||||
code,
|
||||
buildId: safe.buildId,
|
||||
configSchemaVersion: safe.configSchemaVersion,
|
||||
releaseId: safe.releaseId,
|
||||
supportReference: `${safe.buildId}:${code}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export type RuntimeConfigLoadOptions = Readonly<{
|
||||
fetcher?: typeof fetch;
|
||||
buildConfig?: ReturnType<typeof getBuildConfig>;
|
||||
now?: () => number;
|
||||
signal?: AbortSignal;
|
||||
}>;
|
||||
|
||||
export type RuntimeConfigLoadResult = Readonly<{
|
||||
config: RuntimeConfig;
|
||||
configSchema: "V1" | "V2";
|
||||
build: ReturnType<typeof getBuildConfig>;
|
||||
validationDurationMs: number;
|
||||
}>;
|
||||
|
||||
const READ_FAILURE_CODE: Readonly<Record<BootLoadFailure, string>> =
|
||||
Object.freeze({
|
||||
FETCH_FAILED: "CONFIG_FETCH_FAILED",
|
||||
TIMEOUT: "CONFIG_TIMEOUT",
|
||||
HTTP_STATUS_INVALID: "CONFIG_HTTP_FAILED",
|
||||
CONTENT_TYPE_INVALID: "CONFIG_CONTENT_TYPE_INVALID",
|
||||
BODY_TOO_LARGE: "CONFIG_BODY_TOO_LARGE",
|
||||
UTF8_INVALID: "CONFIG_UTF8_INVALID",
|
||||
JSON_INVALID: "CONFIG_JSON_INVALID",
|
||||
SHAPE_INVALID: "CONFIG_SHAPE_INVALID",
|
||||
SECRET_NAME_REJECTED: "CONFIG_SECRET_NAME_REJECTED",
|
||||
SCHEMA_INVALID: "CONFIG_SCHEMA_INVALID",
|
||||
BUILD_MISMATCH: "CONFIG_BUILD_MISMATCH",
|
||||
RELEASE_MISMATCH: "CONFIG_RELEASE_MISMATCH",
|
||||
ASSET_MISMATCH: "CONFIG_ASSET_MISMATCH",
|
||||
CONTRACT_SET_MISMATCH: "CONFIG_CONTRACT_SET_MISMATCH",
|
||||
});
|
||||
|
||||
export async function loadRuntimeConfig(
|
||||
options: RuntimeConfigLoadOptions = {},
|
||||
): Promise<RuntimeConfigLoadResult> {
|
||||
const buildConfig = options.buildConfig ?? getBuildConfig();
|
||||
const now = options.now ?? performance.now.bind(performance);
|
||||
|
||||
const outcome = await readBoundedBootJson(
|
||||
buildConfig.runtimeConfigUrl,
|
||||
BOOT_JSON_POLICIES.RUNTIME_CONFIG,
|
||||
{
|
||||
...(options.fetcher ? { fetcher: options.fetcher } : {}),
|
||||
...(options.signal ? { signal: options.signal } : {}),
|
||||
},
|
||||
);
|
||||
|
||||
if (!outcome.ok) {
|
||||
throw new BootConfigError(READ_FAILURE_CODE[outcome.failure], {
|
||||
buildId: buildConfig.buildId,
|
||||
});
|
||||
}
|
||||
|
||||
const startedAt = now();
|
||||
const rawConfig = outcome.value;
|
||||
|
||||
try {
|
||||
assertSafeConfigNames(rawConfig);
|
||||
} catch {
|
||||
throw new BootConfigError("CONFIG_SECRET_NAME_REJECTED", {
|
||||
buildId: buildConfig.buildId,
|
||||
});
|
||||
}
|
||||
|
||||
const validated = validateRuntimeConfig(rawConfig);
|
||||
if (!validated.success) {
|
||||
throw new BootConfigError("CONFIG_SCHEMA_INVALID", {
|
||||
buildId: buildConfig.buildId,
|
||||
configSchemaVersion:
|
||||
typeof rawConfig.CONFIG_SCHEMA_VERSION === "string"
|
||||
? rawConfig.CONFIG_SCHEMA_VERSION
|
||||
: undefined,
|
||||
releaseId:
|
||||
typeof rawConfig.RELEASE_ID === "string"
|
||||
? rawConfig.RELEASE_ID
|
||||
: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
// Spec §45. HTML/JS build vs runtime config: delegated to the one pure
|
||||
// release-coherence validator so config and manifest checks cannot drift.
|
||||
if (
|
||||
!checkReleaseCoherence(
|
||||
{
|
||||
html: { buildId: buildConfig.buildId },
|
||||
config: validated.data.BUILD_ID ? { buildId: validated.data.BUILD_ID } : {},
|
||||
},
|
||||
["buildId"],
|
||||
).coherent
|
||||
) {
|
||||
throw new BootConfigError("CONFIG_BUILD_MISMATCH", {
|
||||
buildId: buildConfig.buildId,
|
||||
configSchemaVersion: validated.data.CONFIG_SCHEMA_VERSION,
|
||||
releaseId: validated.data.RELEASE_ID,
|
||||
});
|
||||
}
|
||||
|
||||
// §6.10: the validated snapshot is frozen. Nothing re-reads or mutates it;
|
||||
// a kill-switch change only takes effect on a new boot.
|
||||
return Object.freeze({
|
||||
config: validated.data,
|
||||
configSchema: validated.schema,
|
||||
build: buildConfig,
|
||||
validationDurationMs: now() - startedAt,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user