refactor(web): centralize isolated script paths

This commit is contained in:
morluto committed 2026-10-27 10:05:33 +08:00
1 parent 8dc4421325
commit bfdde7ee4c
2 files changed
+9 -10

No files matched your search

+2 -7
View File
@@ -18,6 +18,7 @@ import {
type WebScriptExportResult,
} from "../../domain/webScriptExport.js";
import {
isolatedWebScriptPath,
planWebScriptExport,
type PlannedWebScript,
} from "../../domain/webScriptExportPlan.js";
@@ -212,7 +213,7 @@ const publishPlannedScript = async (
record.content.layout !== "url-path"
)
throw cause;
const relativePath = isolatedScriptPath(script, index);
const relativePath = isolatedWebScriptPath(script.source, index);
await write(relativePath);
return {
...record,
@@ -226,11 +227,5 @@ const publishPlannedScript = async (
}
};
const isolatedScriptPath = (
script: PlannedWebScript["script"],
index: number,
): string =>
`isolated/source-${index + 1}-${createHash("sha256").update(JSON.stringify(script.source)).digest("hex")}.js`;
const message = (cause: unknown): string =>
cause instanceof Error ? cause.message : "Unknown publication failure";
+7 -3
View File
@@ -17,6 +17,12 @@ export interface PlannedWebScript {
readonly record: ExportedWebScript;
}
/** Stable isolated identity shared by planning and destination collision fallback. */
export const isolatedWebScriptPath = (
source: CapturedWebScript["source"],
index: number,
): string => `isolated/source-${index + 1}-${hash(JSON.stringify(source))}.js`;
/** Preserve safe URL layouts only when every captured path is unambiguous. */
export const planWebScriptExport = (
scripts: readonly CapturedWebScript[],
@@ -45,9 +51,7 @@ export const planWebScriptExport = (
url: script.url,
content: {
state: "exported",
relative_path:
path ??
`isolated/source-${index + 1}-${hash(JSON.stringify(script.source))}.js`,
relative_path: path ?? isolatedWebScriptPath(script.source, index),
layout: path === undefined ? "isolated" : "url-path",
layout_reason: reason,
sha256: script.content.sha256,