Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
refactor(asar): centralize read failure and cancellation semantics
morluto5da3b7c39369
diff --git a/src/artifacts/AsarArtifactReader.ts b/src/artifacts/AsarArtifactReader.tsindex 7465ac36e..9904c3298 100644--- a/src/artifacts/AsarArtifactReader.ts+++ b/src/artifacts/AsarArtifactReader.ts@@ -28,6 +28,11 @@ import { admitAsarHeader } from "./AsarHeader.js";import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js";import type { FileHandleChunkStream } from "../filesystem/readFileHandleChunks.js";import { readArtifactFileChunks } from "./readArtifactFileChunks.js";+import {+ assertAsarReadActive,+ asarCancellationFailure,+ asarFailure,+} from "./AsarFailure.js";import {readAsarListing,type AsarListedEntry,@@ -67,7 +72,7 @@ export class AsarArtifactReader implements ArtifactReader {const admitted = await this.#admitHeader(snapshotPath, signal);this.#archiveSize = admitted.archiveBytes;this.#headerSize = admitted.headerBytes;- abortIfNeeded(signal);+ assertAsarReadActive(signal);const ordered = await readAsarListing(getRawHeader(snapshotPath).header,signal,@@ -85,7 +90,7 @@ export class AsarArtifactReader implements ArtifactReader {throw asarFailure(this.path, "inventory", cause);}for (const { metadata, path } of paths) {- abortIfNeeded(signal);+ assertAsarReadActive(signal);const providerPath = path.split("/").join(sep);const kind =metadata.files !== undefined@@ -167,7 +172,7 @@ export class AsarArtifactReader implements ArtifactReader {entry: ArtifactEntry,signal?: AbortSignal,): Promise<Readable> {- abortIfNeeded(signal);+ assertAsarReadActive(signal);const state = this.#entries.get(entry.adapterKey);if (state === undefined && entry.kind !== "file")throw new ArtifactReaderFailure(@@ -226,7 +231,7 @@ export class AsarArtifactReader implements ArtifactReader {);handle = openedHandle;const observed = await openedHandle.stat();- abortIfNeeded(signal);+ assertAsarReadActive(signal);if (!observed.isFile() || observed.size !== archiveSize)throw new ArtifactReaderFailure("integrity",@@ -284,7 +289,7 @@ export class AsarArtifactReader implements ArtifactReader {signal?: AbortSignal,): Promise<void> {await this.#withSnapshotOperation(async () => {- abortIfNeeded(signal);+ assertAsarReadActive(signal);if (this.#snapshot === undefined && this.#snapshotRoot !== undefined)await this.#closeSnapshot();if (this.#snapshot === undefined) await this.#captureSnapshot(signal);@@ -303,7 +308,7 @@ export class AsarArtifactReader implements ArtifactReader {async openContainer(signal?: AbortSignal): Promise<Readable> {await this.prepareContainer(undefined, signal);return this.#withSnapshotOperation(async () => {- abortIfNeeded(signal);+ assertAsarReadActive(signal);const snapshotPath = this.#snapshotPath();const handle = await this.#openOwned(snapshotPath,@@ -345,7 +350,7 @@ export class AsarArtifactReader implements ArtifactReader {const completedInput = inputHandle;inputHandle = undefined;await this.#closeOwnedHandle(completedInput);- abortIfNeeded(signal);+ assertAsarReadActive(signal);this.#snapshot = { path: snapshotPath, sha256: hash.digest("hex") };} catch (cause: unknown) {let failure = asarFailure(this.path, "snapshot", cause);@@ -414,7 +419,7 @@ export class AsarArtifactReader implements ArtifactReader {throw cause;}try {- abortIfNeeded(signal);+ assertAsarReadActive(signal);return await operation();} finally {release();@@ -490,7 +495,7 @@ export class AsarArtifactReader implements ArtifactReader {),);}- abortIfNeeded(signal);+ assertAsarReadActive(signal);const source = this.#ownedReadStream(openedHandle, { start: 0, signal });handle = undefined;const output = readValidatedAsarEntry(@@ -655,29 +660,18 @@ const parseArchiveOffset = (value: string): number | undefined => {return Number.isSafeInteger(offset) ? offset : undefined;};-const abortIfNeeded = (signal?: AbortSignal): void => {- if (signal?.aborted === true)- throw new ArtifactReaderFailure("cancelled", "ASAR operation cancelled", {- cause: signal.reason,- });-};-const waitForOperation = async (operation: Promise<void>,signal?: AbortSignal,): Promise<void> => {if (signal === undefined) return operation;- abortIfNeeded(signal);+ assertAsarReadActive(signal);await new Promise<void>((resolve, reject) => {const removeAbortListener = (): void =>signal.removeEventListener("abort", onAbort);const onAbort = (): void => {removeAbortListener();- reject(- new ArtifactReaderFailure("cancelled", "ASAR operation cancelled", {- cause: signal.reason,- }),- );+ reject(asarCancellationFailure(signal.reason));};signal.addEventListener("abort", onAbort, { once: true });operation.then(@@ -693,34 +687,6 @@ const waitForOperation = async (});};-const asarFailure = (- path: string,- operation: string,- cause: unknown,-): ArtifactReaderFailure => {- if (cause instanceof ArtifactReaderFailure) return cause;- if (isFilesystemFailure(cause))- return new ArtifactReaderFailure(- "io",- `Could not ${operation} ASAR at ${path}: ${cause.message}`,- { cause },- );- return new ArtifactReaderFailure(- "format",- `Malformed ASAR during ${operation}: ${path}`,- { cause },- );-};--const isFilesystemFailure = (- cause: unknown,-): cause is NodeJS.ErrnoException & Error =>- cause instanceof Error &&- "errno" in cause &&- typeof cause.errno === "number" &&- "syscall" in cause &&- typeof cause.syscall === "string";-const isMissingFile = (cause: unknown): boolean =>typeof cause === "object" &&cause !== null &&diff --git a/src/artifacts/AsarEntryStream.ts b/src/artifacts/AsarEntryStream.tsindex 12b524af9..47a018336 100644--- a/src/artifacts/AsarEntryStream.ts+++ b/src/artifacts/AsarEntryStream.ts@@ -2,6 +2,11 @@ import type { FileHandleChunkStream } from "../filesystem/readFileHandleChunks.jimport { Readable } from "node:stream";import { ArtifactReaderFailure } from "./ArtifactReader.js";+import {+ assertAsarReadActive,+ asarCancellationFailure,+ asarFailure,+} from "./AsarFailure.js";/** Close an ASAR file handle before honoring cancellation of an empty member. */export const closeAsarHandle = async (@@ -9,7 +14,7 @@ export const closeAsarHandle = async (signal?: AbortSignal,): Promise<void> => {await close();- abortIfNeeded(signal);+ assertAsarReadActive(signal);};/** Validate and contextualize bytes read from one ASAR member source. */@@ -34,12 +39,7 @@ export const readValidatedAsarEntry = ("integrity",`ASAR entry size disagrees with its header: ${entryPath}`,);- if (signal?.aborted)- return new ArtifactReaderFailure(- "cancelled",- "ASAR operation cancelled",- { cause: signal.reason },- );+ if (signal?.aborted) return asarCancellationFailure(signal.reason);return undefined;};const output = Readable.from(@@ -115,38 +115,3 @@ export const readValidatedAsarEntry = (if (signal?.aborted) onAbort();return output;};--const abortIfNeeded = (signal?: AbortSignal): void => {- if (signal?.aborted === true)- throw new ArtifactReaderFailure("cancelled", "ASAR operation cancelled", {- cause: signal.reason,- });-};--const asarFailure = (- path: string,- operation: string,- cause: unknown,-): ArtifactReaderFailure => {- if (cause instanceof ArtifactReaderFailure) return cause;- if (isFilesystemFailure(cause))- return new ArtifactReaderFailure(- "io",- `Could not ${operation} ASAR at ${path}: ${cause.message}`,- { cause },- );- return new ArtifactReaderFailure(- "format",- `Malformed ASAR during ${operation}: ${path}`,- { cause },- );-};--const isFilesystemFailure = (- cause: unknown,-): cause is NodeJS.ErrnoException & Error =>- cause instanceof Error &&- "errno" in cause &&- typeof cause.errno === "number" &&- "syscall" in cause &&- typeof cause.syscall === "string";diff --git a/src/artifacts/AsarFailure.ts b/src/artifacts/AsarFailure.tsnew file mode 100644index 000000000..49db12d97--- /dev/null+++ b/src/artifacts/AsarFailure.ts@@ -0,0 +1,42 @@+import { ArtifactReaderFailure } from "./ArtifactReader.js";++/** Preserve cancellation at the ASAR boundary with its original abort reason. */+export const asarCancellationFailure = (+ reason: unknown,+): ArtifactReaderFailure =>+ new ArtifactReaderFailure("cancelled", "ASAR operation cancelled", {+ cause: reason,+ });++export const assertAsarReadActive = (signal?: AbortSignal): void => {+ if (signal?.aborted === true) throw asarCancellationFailure(signal.reason);+};++/** Classify ASAR boundary failures without replacing ownership-bearing errors. */+export const asarFailure = (+ path: string,+ operation: string,+ cause: unknown,+): ArtifactReaderFailure => {+ if (cause instanceof ArtifactReaderFailure) return cause;+ if (isFilesystemFailure(cause))+ return new ArtifactReaderFailure(+ "io",+ `Could not ${operation} ASAR at ${path}: ${cause.message}`,+ { cause },+ );+ return new ArtifactReaderFailure(+ "format",+ `Malformed ASAR during ${operation}: ${path}`,+ { cause },+ );+};++const isFilesystemFailure = (+ cause: unknown,+): cause is NodeJS.ErrnoException & Error =>+ cause instanceof Error &&+ "errno" in cause &&+ typeof cause.errno === "number" &&+ "syscall" in cause &&+ typeof cause.syscall === "string";diff --git a/src/artifacts/AsarListing.ts b/src/artifacts/AsarListing.tsindex f8973dc95..998856701 100644--- a/src/artifacts/AsarListing.ts+++ b/src/artifacts/AsarListing.ts@@ -2,6 +2,7 @@ import { setImmediate } from "node:timers/promises";import { z } from "zod";import { ArtifactReaderFailure } from "./ArtifactReader.js";+import { assertAsarReadActive } from "./AsarFailure.js";import { compareUnicodeCodePoints } from "../domain/unicodeCodePointOrder.js";/** Capacity for expanded paths and listing records, independent of packed bytes. */@@ -48,12 +49,7 @@ export const readAsarListing = async (while (pending.length > 0) {if (examined++ % 128 === 0) {await setImmediate();- if (signal?.aborted)- throw new ArtifactReaderFailure(- "cancelled",- "ASAR operation cancelled",- { cause: signal.reason },- );+ assertAsarReadActive(signal);}const frame = pending.at(-1);if (frame === undefined) break;