fix(artifacts): honor cancellation across source validation

This commit is contained in:
64579525
2026-10-12 22:06:50 +08:00
parent 78820dd69a
commit e47b667670
5 changed files with 296 additions and 15 deletions
+11 -5
View File
@@ -41,12 +41,12 @@ export class InesArtifactReader implements ArtifactReader {
async *entries(signal?: AbortSignal): AsyncIterable<ArtifactEntry> {
abortIfNeeded(signal);
await this.#verifySource();
await this.#verifySource(signal);
const prefix = Buffer.alloc(16);
const { bytesRead } = await this.source.handle.read(prefix, 0, 16, 0);
abortIfNeeded(signal);
// Concurrent source changes take precedence over malformed header diagnostics.
await this.#verifySource();
await this.#verifySource(signal);
const parsed = parseInesLayout(
prefix.subarray(0, bytesRead),
this.source.initial.size,
@@ -82,7 +82,7 @@ export class InesArtifactReader implements ArtifactReader {
"integrity",
"iNES region does not match this reader's inventory",
);
await this.#verifySource();
await this.#verifySource(signal);
const offset = produced.byteOffset;
const size = produced.declaredSize;
if (offset === null || size === null || size <= 0)
@@ -92,7 +92,7 @@ export class InesArtifactReader implements ArtifactReader {
end: offset + size - 1,
signal,
});
const verify = () => this.#verifySource();
const verify = () => this.#verifySource(signal);
return Readable.from(
(async function* () {
let bytes = 0;
@@ -126,6 +126,7 @@ export class InesArtifactReader implements ArtifactReader {
expectedSha256: string,
signal?: AbortSignal,
): Promise<InesInventory> {
abortIfNeeded(signal);
const layout = this.#layout;
if (layout === undefined || this.#entries.size !== layout.regions.length)
throw new ArtifactReaderFailure("integrity", "Incomplete iNES inventory");
@@ -146,6 +147,7 @@ export class InesArtifactReader implements ArtifactReader {
bytes += chunk.length;
}
}
abortIfNeeded(signal);
const sha256 = hash.digest("hex");
if (bytes !== this.source.initial.size || sha256 !== expectedSha256)
throw new ArtifactReaderFailure(
@@ -174,12 +176,14 @@ export class InesArtifactReader implements ArtifactReader {
return this.ownedSource?.close() ?? Promise.resolve();
}
async #verifySource(): Promise<void> {
async #verifySource(signal?: AbortSignal): Promise<void> {
abortIfNeeded(signal);
if (this.#closed)
throw new ArtifactReaderFailure("unavailable", "iNES reader is closed");
try {
await verifyRegularFileState(this.path, this.source);
} catch (cause: unknown) {
abortIfNeeded(signal);
if (cause instanceof RegularFileChangedError)
throw new ArtifactReaderFailure(
"integrity",
@@ -188,5 +192,7 @@ export class InesArtifactReader implements ArtifactReader {
);
throw cause;
}
// State validation is asynchronous even after the final byte was consumed.
abortIfNeeded(signal);
}
}
+26 -7
View File
@@ -13,7 +13,10 @@ import {
nesContainerFormat,
parseInesLayout,
} from "../../domain/inesContainer.js";
import { ARTIFACT_CLASSIFICATION_PREFIX_BYTES } from "../ArtifactHash.js";
import {
abortIfNeeded,
ARTIFACT_CLASSIFICATION_PREFIX_BYTES,
} from "../ArtifactHash.js";
import type { ArtifactOccurrence } from "../../domain/artifactGraph.js";
import {
hasZipSignature,
@@ -107,12 +110,18 @@ export const classifyAndHashRootForInventory = async (
| { readonly kind: "failed"; readonly cause: unknown };
try {
const initial = await handle.stat();
abortIfNeeded(options.signal);
if (!sameRegularFileState(expectedMetadata, initial))
throw new ArtifactReaderFailure(
"integrity",
`Root artifact changed before inventory: ${path}`,
);
const format = await classifyRootFormat(path, handle, initial);
const format = await classifyRootFormat(
path,
handle,
initial,
options.signal,
);
const digest = await hashStableRootArtifactHandle(
path,
handle,
@@ -137,13 +146,19 @@ export const classifyAndHashRootForInventory = async (
outcome = {
kind: "failed",
cause:
cause instanceof RegularFileChangedError
options.signal?.aborted === true
? new ArtifactReaderFailure(
"integrity",
`Root artifact changed during inventory: ${path}`,
{ cause: cause.cause ?? cause },
"cancelled",
"Artifact inventory cancelled",
{ cause },
)
: cause,
: cause instanceof RegularFileChangedError
? new ArtifactReaderFailure(
"integrity",
`Root artifact changed during inventory: ${path}`,
{ cause: cause.cause ?? cause },
)
: cause,
};
}
if (outcome.kind === "failed" || outcome.value.rootSource === undefined) {
@@ -165,10 +180,14 @@ const classifyRootFormat = async (
path: string,
handle: FileHandle,
initial: Stats,
signal?: AbortSignal,
): Promise<ArtifactOccurrence["artifact_format"]> => {
abortIfNeeded(signal);
const magic = Buffer.alloc(ARTIFACT_CLASSIFICATION_PREFIX_BYTES);
const observed = await handle.read(magic, 0, magic.length, 0);
abortIfNeeded(signal);
await verifyRegularFileState(path, { handle, initial });
abortIfNeeded(signal);
const prefix = magic.subarray(0, observed.bytesRead);
if (nesContainerFormat(prefix) !== undefined) {
const parsed = parseInesLayout(prefix, initial.size);
@@ -130,6 +130,7 @@ export const hashStableRootArtifactHandle = async (
try {
await verifyRegularFileState(path, { handle, initial });
} catch (cause: unknown) {
abortIfNeeded(signal);
if (cause instanceof RegularFileChangedError)
throw new ArtifactReaderFailure(
"integrity",
@@ -138,6 +139,7 @@ export const hashStableRootArtifactHandle = async (
);
throw cause;
}
abortIfNeeded(signal);
if (digest.bytes !== initial.size)
throw new ArtifactReaderFailure(
"integrity",