merge: integrate main and retain per-slice FAT alignment
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { lstat, realpath } from "node:fs/promises";
|
||||
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
||||
import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
|
||||
import { ArtifactResourceScope } from "../../artifacts/ArtifactResourceScope.js";
|
||||
import { readStableArtifact } from "../../artifacts/readStableArtifact.js";
|
||||
import { normalizeArtifactPath } from "../../artifacts/ArtifactPaths.js";
|
||||
import type { WebScriptArtifactPort } from "../../application/WebScriptArtifactPort.js";
|
||||
@@ -19,10 +20,36 @@ import {
|
||||
|
||||
/** Read one digest-verified captured source under the manifest's current location. */
|
||||
export class LocalWebScriptArtifacts implements WebScriptArtifactPort {
|
||||
constructor(readonly operation: WebArtifactReadContext["operation"]) {}
|
||||
constructor(
|
||||
readonly operation: WebArtifactReadContext["operation"],
|
||||
readonly resources = new ArtifactResourceScope(),
|
||||
) {}
|
||||
|
||||
close(): Promise<void> {
|
||||
return this.resources.close();
|
||||
}
|
||||
|
||||
/** Read the selected source; relocated exports retain their reported original root. */
|
||||
async load(input: SelectedWebScriptInput, options?: ExecutionOptions) {
|
||||
try {
|
||||
return await this.resources.run(() => this.#load(input, options));
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
webArtifactReadError(
|
||||
cause,
|
||||
{
|
||||
operation: this.operation,
|
||||
field: ["manifest_path"],
|
||||
targetPath: input.manifest_path,
|
||||
callerSelected: true,
|
||||
},
|
||||
options?.signal,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async #load(input: SelectedWebScriptInput, options?: ExecutionOptions) {
|
||||
let field: readonly (string | number)[] = ["manifest_path"];
|
||||
let targetPath = input.manifest_path;
|
||||
let callerSelected = true;
|
||||
@@ -31,6 +58,7 @@ export class LocalWebScriptArtifacts implements WebScriptArtifactPort {
|
||||
input.manifest_path,
|
||||
32 * 1024 * 1024,
|
||||
options?.signal,
|
||||
this.resources,
|
||||
);
|
||||
const manifest = webScriptExportManifestSchema.parse(manifestBytes.value);
|
||||
field = ["script_index"];
|
||||
@@ -65,6 +93,8 @@ export class LocalWebScriptArtifacts implements WebScriptArtifactPort {
|
||||
sourcePath,
|
||||
16 * 1024 * 1024,
|
||||
options?.signal,
|
||||
undefined,
|
||||
this.resources,
|
||||
);
|
||||
await assertContained(root, sourcePath);
|
||||
if (
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { z } from "zod";
|
||||
import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
|
||||
import type { ArtifactResourceScope } from "../../artifacts/ArtifactResourceScope.js";
|
||||
import { readStableArtifact } from "../../artifacts/readStableArtifact.js";
|
||||
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
|
||||
import {
|
||||
@@ -9,6 +10,8 @@ import {
|
||||
} from "../../domain/analysisErrorCore.js";
|
||||
import { ArtifactOperationError } from "../../domain/artifactOperationError.js";
|
||||
import { ProviderAdapterError } from "../../domain/providerAdapterError.js";
|
||||
import { ProviderCleanupError } from "../../domain/providerCleanupError.js";
|
||||
import { analysisErrorWithCleanupFailure } from "../../domain/analysisErrorCleanup.js";
|
||||
import { safeParseJson } from "../../domain/safeJson.js";
|
||||
|
||||
/** The selected artifact and input field owning a reader failure. */
|
||||
@@ -42,8 +45,15 @@ export const readWebArtifactJson = async (
|
||||
path: string,
|
||||
maximumBytes: number,
|
||||
signal?: AbortSignal,
|
||||
resourceScope?: ArtifactResourceScope,
|
||||
) => {
|
||||
const data = await readStableArtifact(path, maximumBytes, signal);
|
||||
const data = await readStableArtifact(
|
||||
path,
|
||||
maximumBytes,
|
||||
signal,
|
||||
undefined,
|
||||
resourceScope,
|
||||
);
|
||||
const value = safeParseJson(decodeWebArtifact(data.bytes));
|
||||
if (!value.ok) throw new WebArtifactFormatFailure(value.error);
|
||||
return { ...data, value: value.value };
|
||||
@@ -55,8 +65,30 @@ export const webArtifactReadError = (
|
||||
context: WebArtifactReadContext,
|
||||
signal?: AbortSignal,
|
||||
): AnalysisError => {
|
||||
if (cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined) {
|
||||
const primary =
|
||||
signal?.aborted === true
|
||||
? new AnalysisCancelledError(context.operation, { cause })
|
||||
: new ArtifactOperationError(
|
||||
context.operation,
|
||||
cause.reason,
|
||||
cause.details,
|
||||
cause.message,
|
||||
{ cause },
|
||||
);
|
||||
return analysisErrorWithCleanupFailure(
|
||||
primary,
|
||||
new ProviderCleanupError(
|
||||
"captured-web-artifacts",
|
||||
cause.cleanup.resources,
|
||||
{ reason: cause.cleanup.reason },
|
||||
{ cause, operation: context.operation },
|
||||
),
|
||||
context.operation,
|
||||
);
|
||||
}
|
||||
if (signal?.aborted === true)
|
||||
return new AnalysisCancelledError(context.operation);
|
||||
return new AnalysisCancelledError(context.operation, { cause });
|
||||
const selection = context.callerSelected
|
||||
? selectedFileError(cause, context)
|
||||
: undefined;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { WebSourceLocationArtifactPort } from "../../application/WebSourceLocationPorts.js";
|
||||
import type { WebScriptArtifactPort } from "../../application/WebScriptArtifactPort.js";
|
||||
import type { ExecutionOptions } from "../../application/AnalysisProvider.js";
|
||||
import { ArtifactResourceScope } from "../../artifacts/ArtifactResourceScope.js";
|
||||
import { readStableArtifact } from "../../artifacts/readStableArtifact.js";
|
||||
import {
|
||||
WEB_SOURCE_MAP_LIMITS,
|
||||
@@ -12,14 +13,64 @@ import { decodeWebArtifact, webArtifactReadError } from "./WebArtifactReads.js";
|
||||
|
||||
/** Read one verified capture source and the caller-selected map, without fetching. */
|
||||
export class LocalWebSourceLocationArtifacts implements WebSourceLocationArtifactPort {
|
||||
readonly scripts: WebScriptArtifactPort;
|
||||
readonly #resources: ArtifactResourceScope;
|
||||
|
||||
constructor(
|
||||
readonly scripts: WebScriptArtifactPort = new LocalWebScriptArtifacts(
|
||||
"trace_web_source_location",
|
||||
),
|
||||
) {}
|
||||
scripts?: WebScriptArtifactPort,
|
||||
resources = scripts instanceof LocalWebScriptArtifacts
|
||||
? scripts.resources
|
||||
: new ArtifactResourceScope(),
|
||||
) {
|
||||
this.#resources = resources;
|
||||
this.scripts =
|
||||
scripts ??
|
||||
new LocalWebScriptArtifacts("trace_web_source_location", resources);
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
const closeScript =
|
||||
this.scripts instanceof LocalWebScriptArtifacts &&
|
||||
this.scripts.resources === this.#resources
|
||||
? undefined
|
||||
: this.scripts.close?.bind(this.scripts);
|
||||
const results = await Promise.allSettled([
|
||||
Promise.resolve().then(() => this.#resources.close()),
|
||||
...(closeScript === undefined
|
||||
? []
|
||||
: [Promise.resolve().then(closeScript)]),
|
||||
]);
|
||||
const failures = results.flatMap((result) =>
|
||||
result.status === "rejected" ? [result.reason] : [],
|
||||
);
|
||||
if (failures.length > 0)
|
||||
throw new AggregateError(
|
||||
failures,
|
||||
"Web source-location artifact cleanup failed",
|
||||
);
|
||||
}
|
||||
|
||||
/** Preserve actual map identity separately from the selected deployment URL. */
|
||||
async load(input: WebSourceLocationInput, options?: ExecutionOptions) {
|
||||
try {
|
||||
return await this.#resources.run(() => this.#load(input, options));
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
webArtifactReadError(
|
||||
cause,
|
||||
{
|
||||
operation: "trace_web_source_location",
|
||||
field: ["source_map", "path"],
|
||||
targetPath: input.source_map.path,
|
||||
callerSelected: true,
|
||||
},
|
||||
options?.signal,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async #load(input: WebSourceLocationInput, options?: ExecutionOptions) {
|
||||
const loaded = await this.scripts.load(input, options);
|
||||
if (!loaded.ok) return loaded;
|
||||
try {
|
||||
@@ -27,6 +78,8 @@ export class LocalWebSourceLocationArtifacts implements WebSourceLocationArtifac
|
||||
input.source_map.path,
|
||||
WEB_SOURCE_MAP_LIMITS.mapBytes,
|
||||
options?.signal,
|
||||
undefined,
|
||||
this.#resources,
|
||||
);
|
||||
return ok({
|
||||
...loaded.value,
|
||||
|
||||
@@ -17,6 +17,7 @@ import { z } from "zod";
|
||||
import type { ExecutionOptions } from "../../application/AnalysisProvider.js";
|
||||
import { readStableArtifact } from "../../artifacts/readStableArtifact.js";
|
||||
import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
|
||||
import { ArtifactResourceScope } from "../../artifacts/ArtifactResourceScope.js";
|
||||
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
|
||||
import {
|
||||
AnalysisCapabilityUnavailableError,
|
||||
@@ -26,6 +27,7 @@ import {
|
||||
} from "../../domain/analysisErrorCore.js";
|
||||
import { ProviderAdapterError } from "../../domain/providerAdapterError.js";
|
||||
import { ProviderCleanupError } from "../../domain/providerCleanupError.js";
|
||||
import { analysisErrorWithCleanupFailure } from "../../domain/analysisErrorCleanup.js";
|
||||
import { err, ok, type Result } from "../../domain/result.js";
|
||||
import {
|
||||
WEB_NETWORK_CAPTURE_LIMITS,
|
||||
@@ -60,18 +62,52 @@ const replySchema = z.discriminatedUnion("ok", [
|
||||
|
||||
/** One owned snapshot/process/root lifecycle for replaceable historical format decoders. */
|
||||
export class HistoricalCaptureDecoder {
|
||||
readonly #resources: ArtifactResourceScope;
|
||||
|
||||
constructor(
|
||||
readonly adapters: readonly HistoricalCaptureFormatAdapter[],
|
||||
readonly environment: Readonly<NodeJS.ProcessEnv>,
|
||||
readonly createRuntime: () => Promise<
|
||||
Pick<PrivateRuntimeRoot, "path" | "close">
|
||||
> = () => PrivateRuntimeRoot.create({ prefix: "rea-web-capture-" }),
|
||||
) {}
|
||||
resources = new ArtifactResourceScope(),
|
||||
) {
|
||||
this.#resources = resources;
|
||||
}
|
||||
|
||||
/** Retry retained readers and private roots at service shutdown. */
|
||||
close(): Promise<void> {
|
||||
return this.#resources.close();
|
||||
}
|
||||
|
||||
/** Snapshot the selected artifact, decode it offline, and release private credentials and processes. */
|
||||
async inspect(
|
||||
input: InspectWebNetworkCaptureInput,
|
||||
options?: ExecutionOptions,
|
||||
): Promise<Result<WebNetworkCapture, AnalysisError>> {
|
||||
try {
|
||||
return await this.#resources.run(() =>
|
||||
this.inspectAdmitted(input, options),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
const error = historicalCaptureFailure(
|
||||
input,
|
||||
cause,
|
||||
"capture-read",
|
||||
options,
|
||||
);
|
||||
return err(
|
||||
redactExplicitFailure(
|
||||
this.withReaderCleanup(error, cause),
|
||||
input.sensitive_values,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async inspectAdmitted(
|
||||
input: InspectWebNetworkCaptureInput,
|
||||
options?: ExecutionOptions,
|
||||
): Promise<Result<WebNetworkCapture, AnalysisError>> {
|
||||
let runtime: Pick<PrivateRuntimeRoot, "path" | "close"> | undefined;
|
||||
let result: Result<WebNetworkCapture, AnalysisError>;
|
||||
@@ -90,6 +126,8 @@ export class HistoricalCaptureDecoder {
|
||||
input.capture_path,
|
||||
WEB_NETWORK_CAPTURE_LIMITS.inputBytes,
|
||||
options?.signal,
|
||||
undefined,
|
||||
this.#resources,
|
||||
);
|
||||
phase = "decoder";
|
||||
runtime = await this.createRuntime();
|
||||
@@ -139,9 +177,16 @@ export class HistoricalCaptureDecoder {
|
||||
replyPath,
|
||||
WEB_NETWORK_CAPTURE_LIMITS.outputBytes,
|
||||
options?.signal,
|
||||
undefined,
|
||||
this.#resources,
|
||||
);
|
||||
reply = replySchema.parse(JSON.parse(replyFile.bytes.toString("utf8")));
|
||||
} catch (cause: unknown) {
|
||||
if (
|
||||
cause instanceof ArtifactReaderFailure &&
|
||||
cause.cleanup !== undefined
|
||||
)
|
||||
throw cause;
|
||||
if (options?.signal?.aborted)
|
||||
throw new AnalysisCancelledError(OPERATION);
|
||||
throw new AnalysisOutputError(
|
||||
@@ -230,17 +275,21 @@ export class HistoricalCaptureDecoder {
|
||||
result = ok({ ...reply.value, ...common, format: input.format });
|
||||
}
|
||||
} catch (cause: unknown) {
|
||||
const error = historicalCaptureFailure(input, cause, phase, options);
|
||||
result = err(
|
||||
redactExplicitFailure(
|
||||
historicalCaptureFailure(input, cause, phase, options),
|
||||
this.withReaderCleanup(error, cause),
|
||||
input.sensitive_values,
|
||||
),
|
||||
);
|
||||
}
|
||||
if (runtime !== undefined) {
|
||||
try {
|
||||
await runtime.close();
|
||||
} catch (cause: unknown) {
|
||||
const cleanup = await this.#resources.release({
|
||||
kind: "private-runtime-root",
|
||||
resource: runtime.path,
|
||||
root: runtime,
|
||||
});
|
||||
if (cleanup.kind === "failed") {
|
||||
return err(
|
||||
redactExplicitFailure(
|
||||
new ProviderCleanupError(
|
||||
@@ -251,9 +300,12 @@ export class HistoricalCaptureDecoder {
|
||||
previous_error: result.ok
|
||||
? null
|
||||
: projectAnalysisError(result.error),
|
||||
reason: cause instanceof Error ? cause.message : String(cause),
|
||||
reason:
|
||||
cleanup.cause instanceof Error
|
||||
? cleanup.cause.message
|
||||
: String(cleanup.cause),
|
||||
},
|
||||
{ operation: OPERATION },
|
||||
{ operation: OPERATION, cause: cleanup.cause },
|
||||
),
|
||||
input.sensitive_values,
|
||||
),
|
||||
@@ -264,6 +316,34 @@ export class HistoricalCaptureDecoder {
|
||||
? err(new AnalysisCancelledError(OPERATION))
|
||||
: result;
|
||||
}
|
||||
|
||||
private withReaderCleanup(
|
||||
primary: AnalysisError,
|
||||
cause: unknown,
|
||||
): AnalysisError {
|
||||
if (
|
||||
!(cause instanceof ArtifactReaderFailure) ||
|
||||
cause.cleanup === undefined
|
||||
)
|
||||
return primary;
|
||||
if (cause.reason === "unavailable")
|
||||
return new ProviderCleanupError(
|
||||
"web-network-capture",
|
||||
cause.cleanup.resources,
|
||||
{ reason: cause.cleanup.reason },
|
||||
{ operation: OPERATION, cause },
|
||||
);
|
||||
const cleanup = new ProviderCleanupError(
|
||||
"web-network-capture",
|
||||
cause.cleanup.resources,
|
||||
{
|
||||
reason: cause.cleanup.reason,
|
||||
previous_error: projectAnalysisError(primary),
|
||||
},
|
||||
{ operation: OPERATION, cause },
|
||||
);
|
||||
return analysisErrorWithCleanupFailure(primary, cleanup, OPERATION);
|
||||
}
|
||||
}
|
||||
|
||||
/** The Node capture decoder runs on this executable; only Node.js enforces its V8 heap flags. */
|
||||
|
||||
@@ -4,6 +4,7 @@ import type {
|
||||
} from "../../application/WebModulePorts.js";
|
||||
import type { WebScriptArtifactPort } from "../../application/WebScriptArtifactPort.js";
|
||||
import type { ExecutionOptions } from "../../application/AnalysisProvider.js";
|
||||
import { ArtifactResourceScope } from "../../artifacts/ArtifactResourceScope.js";
|
||||
import { jsonObjectSchema } from "../../domain/jsonValue.js";
|
||||
import { err, ok } from "../../domain/result.js";
|
||||
import type { WebModuleTraceInput } from "../../domain/webModuleTrace.js";
|
||||
@@ -15,14 +16,64 @@ import {
|
||||
|
||||
/** Add an explicitly selected import map to a verified captured-script source. */
|
||||
export class LocalWebModuleArtifacts implements WebModuleArtifactPort {
|
||||
readonly scripts: WebScriptArtifactPort;
|
||||
readonly #resources: ArtifactResourceScope;
|
||||
|
||||
constructor(
|
||||
readonly scripts: WebScriptArtifactPort = new LocalWebScriptArtifacts(
|
||||
"trace_web_module_imports",
|
||||
),
|
||||
) {}
|
||||
scripts?: WebScriptArtifactPort,
|
||||
resources = scripts instanceof LocalWebScriptArtifacts
|
||||
? scripts.resources
|
||||
: new ArtifactResourceScope(),
|
||||
) {
|
||||
this.#resources = resources;
|
||||
this.scripts =
|
||||
scripts ??
|
||||
new LocalWebScriptArtifacts("trace_web_module_imports", resources);
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
const closeScript =
|
||||
this.scripts instanceof LocalWebScriptArtifacts &&
|
||||
this.scripts.resources === this.#resources
|
||||
? undefined
|
||||
: this.scripts.close?.bind(this.scripts);
|
||||
const results = await Promise.allSettled([
|
||||
Promise.resolve().then(() => this.#resources.close()),
|
||||
...(closeScript === undefined
|
||||
? []
|
||||
: [Promise.resolve().then(closeScript)]),
|
||||
]);
|
||||
const failures = results.flatMap((result) =>
|
||||
result.status === "rejected" ? [result.reason] : [],
|
||||
);
|
||||
if (failures.length > 0)
|
||||
throw new AggregateError(failures, "Web module artifact cleanup failed");
|
||||
}
|
||||
|
||||
/** Read selected source and optional map without website requests. */
|
||||
async load(input: WebModuleTraceInput, options?: ExecutionOptions) {
|
||||
try {
|
||||
return await this.#resources.run(() => this.#load(input, options));
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
webArtifactReadError(
|
||||
cause,
|
||||
{
|
||||
operation: "trace_web_module_imports",
|
||||
field:
|
||||
input.import_map === undefined
|
||||
? ["manifest_path"]
|
||||
: ["import_map", "path"],
|
||||
targetPath: input.import_map?.path ?? input.manifest_path,
|
||||
callerSelected: input.import_map !== undefined,
|
||||
},
|
||||
options?.signal,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async #load(input: WebModuleTraceInput, options?: ExecutionOptions) {
|
||||
const loaded = await this.scripts.load(input, options);
|
||||
if (!loaded.ok) return loaded;
|
||||
if (input.import_map === undefined)
|
||||
@@ -33,6 +84,7 @@ export class LocalWebModuleArtifacts implements WebModuleArtifactPort {
|
||||
selected.path,
|
||||
4 * 1024 * 1024,
|
||||
options?.signal,
|
||||
this.#resources,
|
||||
);
|
||||
const importMap: WebModuleArtifacts["importMap"] = {
|
||||
file: {
|
||||
|
||||
Reference in New Issue
Block a user