mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 21:23:45 +00:00
* refactor(kernel): give error-to-message text one owner in a new src/kernel layer * fix(kernel): keep the regex worker's own error text inside its isolated source * refactor(kernel): exclude copies that run in another realm from the error-message guard by name The regex worker's source string and the page.evaluate fetch in PlaywrightScenarioRequest cannot import errorMessage. The guard now names them, so the counted baseline does not invite a later slice to rewrite them. * refactor(kernel): migrate the long tail of inline error-message copies to errorMessage (#11552) Every provider and adapter folder with six or fewer copies now calls errorMessage from src/kernel. Local helpers whose whole body was the copy (failureMessage, gdbErrorMessage, message, describe, reasonOf, recoveryFailureMessage and four local errorMessage definitions) are removed and their call sites call the owner. Refs #11548 * refactor(kernel): give byte-to-hex formatting one owner in src/kernel (#11560) byteToHex returns the two lowercase hexadecimal digits of one byte. All eight inline copies (native, contracts, domain firmware and desktop, pcap) call it, keeping their own 0x, % or \x notation, and a counted guard with an empty baseline keeps new copies out. Refs #11548 * refactor(kernel): give one-shot SHA-256 hex digests one owner in src/kernel (#11562) sha256Hex digests one in-memory value. A counted guard records the one-shot createHash("sha256").update(x).digest("hex") chains (115 in 101 files) and the first slice migrates domain, contracts and process (35 copies), removing local wrappers (sha256, sha256Text, hash) and the exported sha256Text. Incremental and streamed hashing is not counted. Refs #11548 * refactor(kernel): migrate the application layer's inline error-message copies to errorMessage (#11563) All 69 copies in src/application call errorMessage from src/kernel. Local helpers whose whole body was the copy (errorDetail, causeReason and five local errorMessage definitions) are removed and their call sites call the owner. Refs #11548 * refactor(kernel): migrate the native provider's inline error-message copies to errorMessage (#11567) All 49 copies in src/native call errorMessage from src/kernel. The local errorReason helpers in LldbCallTracer and LldbRetainedObservations, whose whole body was the copy, are removed and their call sites call the owner. Refs #11548 * refactor(kernel): migrate the remaining inline error-message copies to errorMessage (#11569)
137 lines
4.5 KiB
TypeScript
137 lines
4.5 KiB
TypeScript
import type {
|
|
CapturedWebScript,
|
|
ExportedWebScript,
|
|
} from "./webScriptExport.js";
|
|
import { urlDeclaresQueryOrFragment } from "./urlComponents.js";
|
|
import { sha256Hex } from "../kernel/sha256Hex.js";
|
|
|
|
// Literal URL path characters that are also portable filename characters.
|
|
// Percent escapes remain isolated: decoding them could change path identity.
|
|
const PORTABLE_SEGMENT = /^[A-Za-z0-9_~.!$&'()+,;=@-]+$/u;
|
|
const RESERVED_SEGMENT = /^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/iu;
|
|
|
|
/** One planned file, or an explicit missing source with no filesystem effect. */
|
|
export interface PlannedWebScript {
|
|
readonly script: CapturedWebScript;
|
|
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}-${sha256Hex(JSON.stringify(source))}.js`;
|
|
|
|
/** Preserve safe URL layouts only when every captured path is unambiguous. */
|
|
export const planWebScriptExport = (
|
|
scripts: readonly CapturedWebScript[],
|
|
): readonly PlannedWebScript[] => {
|
|
const candidates = scripts.map(candidatePath);
|
|
const conflicts = conflictingPaths(candidates);
|
|
return scripts.map((script, index) => {
|
|
if (script.content.state === "unavailable")
|
|
return {
|
|
script,
|
|
record: {
|
|
source: script.source,
|
|
url: script.url,
|
|
content: script.content,
|
|
},
|
|
};
|
|
const candidate = candidates[index];
|
|
const reason = conflicts.has(index)
|
|
? "Captured paths collide by content version or file/directory prefix."
|
|
: (candidate?.reason ?? null);
|
|
const path = reason === null ? candidate?.path : undefined;
|
|
return {
|
|
script,
|
|
record: {
|
|
source: script.source,
|
|
url: script.url,
|
|
content: {
|
|
state: "exported",
|
|
relative_path: path ?? isolatedWebScriptPath(script.source, index),
|
|
layout: path === undefined ? "isolated" : "url-path",
|
|
layout_reason: reason,
|
|
sha256: script.content.sha256,
|
|
bytes: script.content.bytes.length,
|
|
media_type: script.content.media_type,
|
|
redacted: script.content.redacted,
|
|
representation: script.content.representation,
|
|
},
|
|
},
|
|
};
|
|
});
|
|
};
|
|
|
|
interface CandidatePath {
|
|
readonly path?: string;
|
|
readonly reason: string | null;
|
|
}
|
|
|
|
const candidatePath = (script: CapturedWebScript): CandidatePath => {
|
|
let url: URL;
|
|
try {
|
|
url = new URL(script.url);
|
|
} catch {
|
|
return {
|
|
reason: "Source has no absolute HTTP URL (for example an inline script).",
|
|
};
|
|
}
|
|
if (url.protocol !== "http:" && url.protocol !== "https:")
|
|
return { reason: "Source URL scheme has no local module layout mapping." };
|
|
const parts = url.pathname.slice(1).split("/");
|
|
if (
|
|
!/\.(?:js|mjs|cjs)$/u.test(url.pathname) ||
|
|
parts.some(
|
|
(part) =>
|
|
!PORTABLE_SEGMENT.test(part) ||
|
|
part === "." ||
|
|
part === ".." ||
|
|
part.endsWith(".") ||
|
|
RESERVED_SEGMENT.test(part),
|
|
)
|
|
)
|
|
return {
|
|
reason: "URL path is not an ordinary portable JavaScript module path.",
|
|
};
|
|
return {
|
|
path: `origins/${sha256Hex(url.origin)}/${parts.join("/")}`,
|
|
reason:
|
|
urlDeclaresQueryOrFragment(url) ||
|
|
url.username !== "" ||
|
|
url.password !== ""
|
|
? "URL query, fragment, or credentials cannot select a canonical local module."
|
|
: null,
|
|
};
|
|
};
|
|
|
|
// Register every exact prefix so a repeated path, or a file that is also a
|
|
// directory prefix, isolates every affected source. Case-distinct spellings
|
|
// stay distinct until the destination filesystem reports a collision.
|
|
const conflictingPaths = (
|
|
candidates: readonly CandidatePath[],
|
|
): Set<number> => {
|
|
const prefixes = new Map<string, { indices: number[]; files: number[] }>();
|
|
candidates.forEach(({ path }, index) => {
|
|
if (path === undefined) return;
|
|
const parts = path.split("/");
|
|
parts.forEach((_, position) => {
|
|
const spelling = parts.slice(0, position + 1).join("/");
|
|
const entry = prefixes.get(spelling) ?? { indices: [], files: [] };
|
|
entry.indices.push(index);
|
|
if (position === parts.length - 1) entry.files.push(index);
|
|
prefixes.set(spelling, entry);
|
|
});
|
|
});
|
|
const conflicts = new Set<number>();
|
|
for (const entry of prefixes.values())
|
|
if (
|
|
entry.files.length > 1 ||
|
|
(entry.files.length > 0 && entry.indices.length > entry.files.length)
|
|
)
|
|
for (const index of entry.indices) conflicts.add(index);
|
|
return conflicts;
|
|
};
|