fix(artifacts): retain ASAR resources through cleanup failures
Give snapshot capture, header admission, packed members, and container streams one file-handle owner. Serialize adoption with reader shutdown, retain failed closes for retry, and remove snapshots only after descriptor cleanup succeeds. Cover unread members, pending opens, zero-byte members, and paired format/cleanup failures. Keep intentionally unrecoverable test fixtures hygienic and strengthen managed identity and process cleanup-authority expectations. Validation: full npm run check; 99 targeted tests and temporary-directory hygiene; exact discovery of 877 test files. No real vendor-provider verification was performed.
This commit is contained in:
+238
-110
@@ -1,10 +1,4 @@
|
|||||||
import {
|
import { constants, createWriteStream, type Stats } from "node:fs";
|
||||||
constants,
|
|
||||||
createReadStream,
|
|
||||||
createWriteStream,
|
|
||||||
type ReadStream,
|
|
||||||
type Stats,
|
|
||||||
} from "node:fs";
|
|
||||||
import {
|
import {
|
||||||
lstat,
|
lstat,
|
||||||
mkdtemp,
|
mkdtemp,
|
||||||
@@ -46,51 +40,40 @@ export class AsarArtifactReader implements ArtifactReader {
|
|||||||
#archiveSize: number | undefined;
|
#archiveSize: number | undefined;
|
||||||
#headerSize: number | undefined;
|
#headerSize: number | undefined;
|
||||||
readonly #entries = new Map<string, AsarEntryState>();
|
readonly #entries = new Map<string, AsarEntryState>();
|
||||||
readonly #unpackedHandles = new Map<
|
readonly #ownedHandles = new Map<SharedAsarFileHandle, OwnedAsarHandle>();
|
||||||
UnpackedFileHandle,
|
|
||||||
{
|
|
||||||
readonly path: string;
|
|
||||||
readonly owner: OwnedFileHandle<UnpackedFileHandle>;
|
|
||||||
}
|
|
||||||
>();
|
|
||||||
#snapshot: { readonly path: string; readonly sha256: string } | undefined;
|
#snapshot: { readonly path: string; readonly sha256: string } | undefined;
|
||||||
#snapshotRoot: string | undefined;
|
#snapshotRoot: string | undefined;
|
||||||
#snapshotOperation: Promise<void> = Promise.resolve();
|
#snapshotOperation: Promise<void> = Promise.resolve();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly path: string,
|
private readonly path: string,
|
||||||
private readonly openUnpackedFile: OpenAsarUnpackedFile = open,
|
private readonly options: AsarArtifactReaderOptions = {},
|
||||||
private readonly removeSnapshot: (path: string) => Promise<void> = (path) =>
|
|
||||||
rm(path, { recursive: true, force: true }),
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async *entries(signal?: AbortSignal): AsyncIterable<ArtifactEntry> {
|
async *entries(signal?: AbortSignal): AsyncIterable<ArtifactEntry> {
|
||||||
let paths: readonly AsarListedPath[];
|
let paths: readonly AsarListedPath[];
|
||||||
try {
|
try {
|
||||||
await this.prepareContainer(undefined, signal);
|
await this.prepareContainer(undefined, signal);
|
||||||
const snapshotPath = this.#snapshotPath();
|
paths = await this.#withSnapshotOperation(async () => {
|
||||||
this.#resetArchiveState();
|
const snapshotPath = this.#snapshotPath();
|
||||||
this.#entries.clear();
|
this.#resetArchiveState();
|
||||||
const headerHandle = await open(
|
this.#entries.clear();
|
||||||
snapshotPath,
|
const admitted = await this.#admitHeader(snapshotPath);
|
||||||
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
|
|
||||||
);
|
|
||||||
try {
|
|
||||||
const admitted = await admitAsarHeader(headerHandle);
|
|
||||||
this.#archiveSize = admitted.archiveBytes;
|
this.#archiveSize = admitted.archiveBytes;
|
||||||
this.#headerSize = admitted.headerBytes;
|
this.#headerSize = admitted.headerBytes;
|
||||||
} finally {
|
abortIfNeeded(signal);
|
||||||
await headerHandle.close();
|
const ordered = orderAsarListing(
|
||||||
}
|
listPackage(snapshotPath, { isPack: false }),
|
||||||
abortIfNeeded(signal);
|
|
||||||
paths = orderAsarListing(listPackage(snapshotPath, { isPack: false }));
|
|
||||||
const archiveMetadata = await lstat(snapshotPath);
|
|
||||||
if (!archiveMetadata.isFile() || archiveMetadata.isSymbolicLink())
|
|
||||||
throw new ArtifactReaderFailure(
|
|
||||||
"format",
|
|
||||||
"ASAR container is not a regular file",
|
|
||||||
);
|
);
|
||||||
this.#archiveSize = archiveMetadata.size;
|
const archiveMetadata = await lstat(snapshotPath);
|
||||||
|
if (!archiveMetadata.isFile() || archiveMetadata.isSymbolicLink())
|
||||||
|
throw new ArtifactReaderFailure(
|
||||||
|
"format",
|
||||||
|
"ASAR container is not a regular file",
|
||||||
|
);
|
||||||
|
this.#archiveSize = archiveMetadata.size;
|
||||||
|
return ordered;
|
||||||
|
}, signal);
|
||||||
} catch (cause: unknown) {
|
} catch (cause: unknown) {
|
||||||
throw asarFailure(this.path, "inventory", cause);
|
throw asarFailure(this.path, "inventory", cause);
|
||||||
}
|
}
|
||||||
@@ -142,7 +125,47 @@ export class AsarArtifactReader implements ArtifactReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async #admitHeader(snapshotPath: string) {
|
||||||
|
const headerHandle = await this.#openOwned(
|
||||||
|
snapshotPath,
|
||||||
|
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
|
||||||
|
);
|
||||||
|
let admission:
|
||||||
|
| {
|
||||||
|
readonly ok: true;
|
||||||
|
readonly value: Awaited<ReturnType<typeof admitAsarHeader>>;
|
||||||
|
}
|
||||||
|
| { readonly ok: false; readonly cause: unknown };
|
||||||
|
try {
|
||||||
|
admission = { ok: true, value: await admitAsarHeader(headerHandle) };
|
||||||
|
} catch (cause: unknown) {
|
||||||
|
admission = { ok: false, cause };
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await this.#closeOwnedHandle(headerHandle);
|
||||||
|
} catch (cleanupCause: unknown) {
|
||||||
|
if (!admission.ok)
|
||||||
|
throw ArtifactReaderFailure.withCleanup(
|
||||||
|
admission.cause,
|
||||||
|
ArtifactReaderFailure.cleanupObservation(cleanupCause, snapshotPath),
|
||||||
|
);
|
||||||
|
throw cleanupCause;
|
||||||
|
}
|
||||||
|
if (!admission.ok) throw admission.cause;
|
||||||
|
return admission.value;
|
||||||
|
}
|
||||||
|
|
||||||
async open(entry: ArtifactEntry, signal?: AbortSignal): Promise<Readable> {
|
async open(entry: ArtifactEntry, signal?: AbortSignal): Promise<Readable> {
|
||||||
|
return this.#withSnapshotOperation(
|
||||||
|
() => this.#openEntry(entry, signal),
|
||||||
|
signal,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async #openEntry(
|
||||||
|
entry: ArtifactEntry,
|
||||||
|
signal?: AbortSignal,
|
||||||
|
): Promise<Readable> {
|
||||||
abortIfNeeded(signal);
|
abortIfNeeded(signal);
|
||||||
const state = this.#entries.get(entry.adapterKey);
|
const state = this.#entries.get(entry.adapterKey);
|
||||||
if (state === undefined && entry.kind !== "file")
|
if (state === undefined && entry.kind !== "file")
|
||||||
@@ -160,10 +183,16 @@ export class AsarArtifactReader implements ArtifactReader {
|
|||||||
"integrity",
|
"integrity",
|
||||||
`ASAR entry metadata changed since inventory: ${state.entry.path}`,
|
`ASAR entry metadata changed since inventory: ${state.entry.path}`,
|
||||||
);
|
);
|
||||||
const { metadata } = state;
|
if (state.entry.unpacked)
|
||||||
const producedEntry = state.entry;
|
return this.#openUnpackedStream(state.entry, signal);
|
||||||
if (producedEntry.unpacked)
|
return this.#openPackedStream(state, signal);
|
||||||
return this.#openUnpackedStream(producedEntry, signal);
|
}
|
||||||
|
|
||||||
|
async #openPackedStream(
|
||||||
|
state: AsarEntryState,
|
||||||
|
signal?: AbortSignal,
|
||||||
|
): Promise<Readable> {
|
||||||
|
const { metadata, entry: producedEntry } = state;
|
||||||
const archiveSize = this.#archiveSize;
|
const archiveSize = this.#archiveSize;
|
||||||
const headerSize = this.#headerSize;
|
const headerSize = this.#headerSize;
|
||||||
const offset =
|
const offset =
|
||||||
@@ -187,10 +216,11 @@ export class AsarArtifactReader implements ArtifactReader {
|
|||||||
"format",
|
"format",
|
||||||
`ASAR entry range is outside its container: ${producedEntry.path}`,
|
`ASAR entry range is outside its container: ${producedEntry.path}`,
|
||||||
);
|
);
|
||||||
let handle: FileHandle | undefined;
|
const snapshotPath = this.#snapshotPath();
|
||||||
|
let handle: SharedAsarFileHandle | undefined;
|
||||||
try {
|
try {
|
||||||
const openedHandle = await open(
|
const openedHandle = await this.#openOwned(
|
||||||
this.#snapshotPath(),
|
snapshotPath,
|
||||||
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
|
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
|
||||||
);
|
);
|
||||||
handle = openedHandle;
|
handle = openedHandle;
|
||||||
@@ -202,25 +232,48 @@ export class AsarArtifactReader implements ArtifactReader {
|
|||||||
`ASAR container changed before read: ${producedEntry.path}`,
|
`ASAR container changed before read: ${producedEntry.path}`,
|
||||||
);
|
);
|
||||||
if (metadata.size === 0) {
|
if (metadata.size === 0) {
|
||||||
await closeAsarHandle(() => openedHandle.close(), signal);
|
|
||||||
handle = undefined;
|
handle = undefined;
|
||||||
|
await closeAsarHandle(
|
||||||
|
() => this.#closeOwnedHandle(openedHandle),
|
||||||
|
signal,
|
||||||
|
);
|
||||||
return Readable.from([]);
|
return Readable.from([]);
|
||||||
}
|
}
|
||||||
const source = openedHandle.createReadStream({
|
const source = this.#ownedReadStream(openedHandle, {
|
||||||
start,
|
start,
|
||||||
end: start + metadata.size - 1,
|
end: start + metadata.size - 1,
|
||||||
autoClose: true,
|
signal,
|
||||||
});
|
});
|
||||||
return readValidatedAsarEntry(
|
handle = undefined;
|
||||||
|
const output = readValidatedAsarEntry(
|
||||||
source,
|
source,
|
||||||
metadata.size,
|
metadata.size,
|
||||||
producedEntry.path,
|
producedEntry.path,
|
||||||
this.path,
|
this.path,
|
||||||
signal,
|
signal,
|
||||||
);
|
);
|
||||||
|
this.#setOwnedOutput(openedHandle, output);
|
||||||
|
return output;
|
||||||
} catch (cause: unknown) {
|
} catch (cause: unknown) {
|
||||||
await handle?.close().catch(() => undefined);
|
const failure = asarFailure(
|
||||||
throw asarFailure(this.path, `read ${producedEntry.path}`, cause);
|
this.path,
|
||||||
|
`read ${producedEntry.path}`,
|
||||||
|
cause,
|
||||||
|
);
|
||||||
|
if (handle !== undefined) {
|
||||||
|
try {
|
||||||
|
await this.#closeOwnedHandle(handle);
|
||||||
|
} catch (cleanupCause: unknown) {
|
||||||
|
throw ArtifactReaderFailure.withCleanup(
|
||||||
|
failure,
|
||||||
|
ArtifactReaderFailure.cleanupObservation(
|
||||||
|
cleanupCause,
|
||||||
|
snapshotPath,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw failure;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,49 +299,37 @@ export class AsarArtifactReader implements ArtifactReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Read the same captured container that supplies headers and packed members. */
|
/** Read the same captured container that supplies headers and packed members. */
|
||||||
async openContainer(signal?: AbortSignal): Promise<ReadStream> {
|
async openContainer(signal?: AbortSignal): Promise<Readable> {
|
||||||
await this.prepareContainer(undefined, signal);
|
await this.prepareContainer(undefined, signal);
|
||||||
return createReadStream(
|
return this.#withSnapshotOperation(async () => {
|
||||||
this.#snapshotPath(),
|
abortIfNeeded(signal);
|
||||||
signal === undefined ? {} : { signal },
|
const snapshotPath = this.#snapshotPath();
|
||||||
);
|
const handle = await this.#openOwned(
|
||||||
|
snapshotPath,
|
||||||
|
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
|
||||||
|
);
|
||||||
|
return this.#ownedReadStream(handle, { signal });
|
||||||
|
}, signal);
|
||||||
}
|
}
|
||||||
|
|
||||||
async close(): Promise<void> {
|
async close(): Promise<void> {
|
||||||
await this.#withSnapshotOperation(async () => {
|
await this.#withSnapshotOperation(() => this.#closeSnapshot());
|
||||||
let cleanupFailure: ArtifactReaderFailure | undefined;
|
|
||||||
for (const [handle, { path }] of this.#unpackedHandles) {
|
|
||||||
try {
|
|
||||||
await this.#closeUnpackedHandle(handle);
|
|
||||||
} catch (cause: unknown) {
|
|
||||||
cleanupFailure = ArtifactReaderFailure.withCleanup(
|
|
||||||
cleanupFailure ?? cause,
|
|
||||||
ArtifactReaderFailure.cleanupObservation(cause, path),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
await this.#closeSnapshot();
|
|
||||||
} catch (cause: unknown) {
|
|
||||||
cleanupFailure = ArtifactReaderFailure.withCleanup(
|
|
||||||
cleanupFailure ?? cause,
|
|
||||||
ArtifactReaderFailure.cleanupObservation(cause, this.path),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (cleanupFailure !== undefined) throw cleanupFailure;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async #captureSnapshot(signal?: AbortSignal): Promise<void> {
|
async #captureSnapshot(signal?: AbortSignal): Promise<void> {
|
||||||
|
let inputHandle: SharedAsarFileHandle | undefined;
|
||||||
try {
|
try {
|
||||||
this.#snapshotRoot = await mkdtemp(join(tmpdir(), "rea-asar-snapshot-"));
|
this.#snapshotRoot = await mkdtemp(join(tmpdir(), "rea-asar-snapshot-"));
|
||||||
const snapshotPath = join(this.#snapshotRoot, "container.asar");
|
const snapshotPath = join(this.#snapshotRoot, "container.asar");
|
||||||
const hash = createHash("sha256");
|
const hash = createHash("sha256");
|
||||||
const handle = await open(
|
inputHandle = await this.#openOwned(
|
||||||
this.path,
|
this.path,
|
||||||
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
|
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
|
||||||
);
|
);
|
||||||
const source = handle.createReadStream();
|
const source = this.#ownedReadStream(inputHandle, {
|
||||||
|
signal,
|
||||||
|
releaseOnClose: false,
|
||||||
|
});
|
||||||
const digesting = new Transform({
|
const digesting = new Transform({
|
||||||
transform(chunk: Buffer, _encoding, callback) {
|
transform(chunk: Buffer, _encoding, callback) {
|
||||||
hash.update(chunk);
|
hash.update(chunk);
|
||||||
@@ -301,34 +342,50 @@ export class AsarArtifactReader implements ArtifactReader {
|
|||||||
createWriteStream(snapshotPath, { flags: "wx", mode: 0o600 }),
|
createWriteStream(snapshotPath, { flags: "wx", mode: 0o600 }),
|
||||||
signal === undefined ? {} : { signal },
|
signal === undefined ? {} : { signal },
|
||||||
);
|
);
|
||||||
|
const completedInput = inputHandle;
|
||||||
|
inputHandle = undefined;
|
||||||
|
await this.#closeOwnedHandle(completedInput);
|
||||||
this.#snapshot = { path: snapshotPath, sha256: hash.digest("hex") };
|
this.#snapshot = { path: snapshotPath, sha256: hash.digest("hex") };
|
||||||
} catch (cause: unknown) {
|
} catch (cause: unknown) {
|
||||||
const primary =
|
let failure =
|
||||||
signal?.aborted === true
|
signal?.aborted === true
|
||||||
? new ArtifactReaderFailure("cancelled", "ASAR operation cancelled", {
|
? new ArtifactReaderFailure("cancelled", "ASAR operation cancelled", {
|
||||||
cause,
|
cause,
|
||||||
})
|
})
|
||||||
: asarFailure(this.path, "snapshot", cause);
|
: asarFailure(this.path, "snapshot", cause);
|
||||||
|
if (inputHandle !== undefined) {
|
||||||
|
const failedInput = inputHandle;
|
||||||
|
inputHandle = undefined;
|
||||||
|
try {
|
||||||
|
await this.#closeOwnedHandle(failedInput);
|
||||||
|
} catch (cleanupCause: unknown) {
|
||||||
|
failure = ArtifactReaderFailure.withCleanup(
|
||||||
|
failure,
|
||||||
|
ArtifactReaderFailure.cleanupObservation(cleanupCause, this.path),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
await this.#closeSnapshot();
|
if (this.#ownedHandles.size === 0) await this.#closeSnapshot();
|
||||||
} catch (cleanupCause: unknown) {
|
} catch (cleanupCause: unknown) {
|
||||||
throw ArtifactReaderFailure.withCleanup(
|
throw ArtifactReaderFailure.withCleanup(
|
||||||
primary,
|
failure,
|
||||||
ArtifactReaderFailure.cleanupObservation(
|
ArtifactReaderFailure.cleanupObservation(
|
||||||
cleanupCause,
|
cleanupCause,
|
||||||
this.#snapshotRoot ?? this.path,
|
this.#snapshotRoot ?? this.path,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
throw primary;
|
throw failure;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async #closeSnapshot(): Promise<void> {
|
async #closeSnapshot(): Promise<void> {
|
||||||
|
await this.#closeOwnedHandles();
|
||||||
const root = this.#snapshotRoot;
|
const root = this.#snapshotRoot;
|
||||||
if (root !== undefined) {
|
if (root !== undefined) {
|
||||||
try {
|
try {
|
||||||
await this.removeSnapshot(root);
|
await (this.options.removeSnapshot ?? defaultRemoveSnapshot)(root);
|
||||||
if (this.#snapshotRoot === root) this.#snapshotRoot = undefined;
|
if (this.#snapshotRoot === root) this.#snapshotRoot = undefined;
|
||||||
} catch (cause: unknown) {
|
} catch (cause: unknown) {
|
||||||
throw ArtifactReaderFailure.withCleanup(cause, {
|
throw ArtifactReaderFailure.withCleanup(cause, {
|
||||||
@@ -386,7 +443,7 @@ export class AsarArtifactReader implements ArtifactReader {
|
|||||||
signal?: AbortSignal,
|
signal?: AbortSignal,
|
||||||
): Promise<Readable> {
|
): Promise<Readable> {
|
||||||
const unpackedRoot = `${this.path}.unpacked`;
|
const unpackedRoot = `${this.path}.unpacked`;
|
||||||
let handle: UnpackedFileHandle | undefined;
|
let handle: SharedAsarFileHandle | undefined;
|
||||||
try {
|
try {
|
||||||
const rootMetadata = await lstat(unpackedRoot);
|
const rootMetadata = await lstat(unpackedRoot);
|
||||||
if (!rootMetadata.isDirectory() || rootMetadata.isSymbolicLink())
|
if (!rootMetadata.isDirectory() || rootMetadata.isSymbolicLink())
|
||||||
@@ -413,15 +470,12 @@ export class AsarArtifactReader implements ArtifactReader {
|
|||||||
"path",
|
"path",
|
||||||
`ASAR unpacked entry is not a regular file: ${entry.path}`,
|
`ASAR unpacked entry is not a regular file: ${entry.path}`,
|
||||||
);
|
);
|
||||||
handle = await this.openUnpackedFile(
|
handle = await (this.options.openUnpackedFile ?? open)(
|
||||||
canonical,
|
canonical,
|
||||||
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
|
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
|
||||||
);
|
);
|
||||||
const openedHandle = handle;
|
const openedHandle = handle;
|
||||||
this.#unpackedHandles.set(openedHandle, {
|
this.#adoptHandle(openedHandle, canonical);
|
||||||
path: canonical,
|
|
||||||
owner: new OwnedFileHandle(openedHandle),
|
|
||||||
});
|
|
||||||
const openedMetadata = await openedHandle.stat();
|
const openedMetadata = await openedHandle.stat();
|
||||||
if (
|
if (
|
||||||
!openedMetadata.isFile() ||
|
!openedMetadata.isFile() ||
|
||||||
@@ -441,30 +495,28 @@ export class AsarArtifactReader implements ArtifactReader {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
abortIfNeeded(signal);
|
abortIfNeeded(signal);
|
||||||
const source = readFileHandleChunks(openedHandle, {
|
const source = this.#ownedReadStream(openedHandle, { start: 0 });
|
||||||
start: 0,
|
handle = undefined;
|
||||||
});
|
const output = readValidatedAsarEntry(
|
||||||
source.once("close", () => {
|
|
||||||
void this.#closeUnpackedHandle(openedHandle).catch(() => undefined);
|
|
||||||
});
|
|
||||||
return readValidatedAsarEntry(
|
|
||||||
source,
|
source,
|
||||||
undefined,
|
undefined,
|
||||||
entry.path,
|
entry.path,
|
||||||
this.path,
|
this.path,
|
||||||
signal,
|
signal,
|
||||||
);
|
);
|
||||||
|
this.#setOwnedOutput(openedHandle, output);
|
||||||
|
return output;
|
||||||
} catch (cause: unknown) {
|
} catch (cause: unknown) {
|
||||||
let failure = asarUnpackedEntryFailure(this.path, entry, cause);
|
let failure = asarUnpackedEntryFailure(this.path, entry, cause);
|
||||||
if (handle !== undefined && this.#unpackedHandles.has(handle)) {
|
if (handle !== undefined && this.#ownedHandles.has(handle)) {
|
||||||
try {
|
try {
|
||||||
await this.#closeUnpackedHandle(handle);
|
await this.#closeOwnedHandle(handle);
|
||||||
} catch (cleanupCause: unknown) {
|
} catch (cleanupCause: unknown) {
|
||||||
failure = ArtifactReaderFailure.withCleanup(
|
failure = ArtifactReaderFailure.withCleanup(
|
||||||
failure,
|
failure,
|
||||||
ArtifactReaderFailure.cleanupObservation(
|
ArtifactReaderFailure.cleanupObservation(
|
||||||
cleanupCause,
|
cleanupCause,
|
||||||
this.#unpackedHandles.get(handle)?.path ?? entry.path,
|
this.#ownedHandles.get(handle)?.path ?? entry.path,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -473,11 +525,76 @@ export class AsarArtifactReader implements ArtifactReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async #closeUnpackedHandle(handle: UnpackedFileHandle): Promise<void> {
|
async #closeOwnedHandle(handle: SharedAsarFileHandle): Promise<void> {
|
||||||
const owned = this.#unpackedHandles.get(handle);
|
const owned = this.#ownedHandles.get(handle);
|
||||||
if (owned === undefined) return;
|
if (owned === undefined) return;
|
||||||
await owned.owner.close();
|
owned.output?.destroy();
|
||||||
this.#unpackedHandles.delete(handle);
|
owned.source?.destroy();
|
||||||
|
try {
|
||||||
|
await owned.owner.close();
|
||||||
|
} catch (cause: unknown) {
|
||||||
|
throw ArtifactReaderFailure.withCleanup(
|
||||||
|
cause,
|
||||||
|
ArtifactReaderFailure.cleanupObservation(cause, owned.path),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
this.#ownedHandles.delete(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
async #closeOwnedHandles(): Promise<void> {
|
||||||
|
let failure: ArtifactReaderFailure | undefined;
|
||||||
|
for (const [handle, owned] of this.#ownedHandles) {
|
||||||
|
try {
|
||||||
|
await this.#closeOwnedHandle(handle);
|
||||||
|
} catch (cause: unknown) {
|
||||||
|
failure = ArtifactReaderFailure.withCleanup(
|
||||||
|
failure ?? cause,
|
||||||
|
ArtifactReaderFailure.cleanupObservation(cause, owned.path),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (failure !== undefined) throw failure;
|
||||||
|
}
|
||||||
|
|
||||||
|
async #openOwned(path: string, flags: number): Promise<SharedAsarFileHandle> {
|
||||||
|
const handle = await (this.options.openFile ?? open)(path, flags);
|
||||||
|
this.#adoptHandle(handle, path);
|
||||||
|
return handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
#adoptHandle(handle: SharedAsarFileHandle, path: string): void {
|
||||||
|
this.#ownedHandles.set(handle, {
|
||||||
|
path,
|
||||||
|
owner: new OwnedFileHandle(handle),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#ownedReadStream(
|
||||||
|
handle: SharedAsarFileHandle,
|
||||||
|
options: {
|
||||||
|
readonly start?: number;
|
||||||
|
readonly end?: number;
|
||||||
|
readonly signal?: AbortSignal | undefined;
|
||||||
|
readonly releaseOnClose?: boolean;
|
||||||
|
},
|
||||||
|
): Readable {
|
||||||
|
const { releaseOnClose, ...readOptions } = options;
|
||||||
|
const source = readFileHandleChunks(handle, readOptions);
|
||||||
|
const owned = this.#ownedHandles.get(handle);
|
||||||
|
if (owned === undefined)
|
||||||
|
throw new ArtifactReaderFailure("integrity", "ASAR handle has no owner");
|
||||||
|
owned.source = source;
|
||||||
|
owned.output = source;
|
||||||
|
if (releaseOnClose !== false)
|
||||||
|
source.once("close", () => {
|
||||||
|
void this.#closeOwnedHandle(handle).catch(() => undefined);
|
||||||
|
});
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
#setOwnedOutput(handle: SharedAsarFileHandle, output: Readable): void {
|
||||||
|
const owned = this.#ownedHandles.get(handle);
|
||||||
|
if (owned !== undefined) owned.output = output;
|
||||||
}
|
}
|
||||||
|
|
||||||
#resetArchiveState(): void {
|
#resetArchiveState(): void {
|
||||||
@@ -491,16 +608,27 @@ type AsarEntryState = {
|
|||||||
readonly metadata: AsarFileMetadata;
|
readonly metadata: AsarFileMetadata;
|
||||||
readonly entry: ArtifactEntry;
|
readonly entry: ArtifactEntry;
|
||||||
};
|
};
|
||||||
type UnpackedFileHandle = {
|
type SharedAsarFileHandle = Pick<FileHandle, "fd" | "read" | "close"> & {
|
||||||
readonly fd: number;
|
|
||||||
stat(): Promise<Stats>;
|
stat(): Promise<Stats>;
|
||||||
close(): Promise<void>;
|
|
||||||
read: FileHandle["read"];
|
|
||||||
};
|
};
|
||||||
type OpenAsarUnpackedFile = (
|
type OpenAsarFile = (
|
||||||
path: string,
|
path: string,
|
||||||
flags: number,
|
flags: number,
|
||||||
) => Promise<UnpackedFileHandle>;
|
) => Promise<SharedAsarFileHandle>;
|
||||||
|
type OwnedAsarHandle = {
|
||||||
|
readonly path: string;
|
||||||
|
readonly owner: OwnedFileHandle<SharedAsarFileHandle>;
|
||||||
|
source?: Readable;
|
||||||
|
output?: Readable;
|
||||||
|
};
|
||||||
|
type AsarArtifactReaderOptions = {
|
||||||
|
readonly openFile?: OpenAsarFile;
|
||||||
|
readonly openUnpackedFile?: OpenAsarFile;
|
||||||
|
readonly removeSnapshot?: (path: string) => Promise<void>;
|
||||||
|
};
|
||||||
|
|
||||||
|
const defaultRemoveSnapshot = (path: string): Promise<void> =>
|
||||||
|
rm(path, { recursive: true, force: true });
|
||||||
|
|
||||||
const asarUnpackedEntryFailure = (
|
const asarUnpackedEntryFailure = (
|
||||||
archivePath: string,
|
archivePath: string,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import type { Stats } from "node:fs";
|
||||||
import type { FileHandle } from "node:fs/promises";
|
import type { FileHandle } from "node:fs/promises";
|
||||||
|
|
||||||
import { ArtifactReaderFailure } from "./ArtifactReader.js";
|
import { ArtifactReaderFailure } from "./ArtifactReader.js";
|
||||||
@@ -7,7 +8,7 @@ export const MAX_ASAR_HEADER_BYTES = 16 * 1024 * 1024;
|
|||||||
|
|
||||||
/** Admit both Chromium pickle extents before an upstream decoder allocates them. */
|
/** Admit both Chromium pickle extents before an upstream decoder allocates them. */
|
||||||
export const admitAsarHeader = async (
|
export const admitAsarHeader = async (
|
||||||
handle: FileHandle,
|
handle: Pick<FileHandle, "read"> & { stat(): Promise<Stats> },
|
||||||
): Promise<{
|
): Promise<{
|
||||||
readonly archiveBytes: number;
|
readonly archiveBytes: number;
|
||||||
readonly headerBytes: number;
|
readonly headerBytes: number;
|
||||||
|
|||||||
@@ -280,7 +280,11 @@ describe("managed native boundary inspection", () => {
|
|||||||
|
|
||||||
describe("independent managed metadata facets", () => {
|
describe("independent managed metadata facets", () => {
|
||||||
it("preserves identity, members, and imports when embedded resources are malformed", () => {
|
it("preserves identity, members, and imports when embedded resources are malformed", () => {
|
||||||
const bytes = buildManagedPeFixture({ pinvoke: {}, readyToRun: true });
|
const bytes = buildManagedPeFixture({
|
||||||
|
mvid: Buffer.from("3aebc60edc4a544b1f458b4ed40b33b1", "hex"),
|
||||||
|
pinvoke: {},
|
||||||
|
readyToRun: true,
|
||||||
|
});
|
||||||
bytes.writeUInt32LE(0x7fff_0000, 0x0200 + 24);
|
bytes.writeUInt32LE(0x7fff_0000, 0x0200 + 24);
|
||||||
const target = managedPeFixtureTarget(bytes);
|
const target = managedPeFixtureTarget(bytes);
|
||||||
const artifact = inspectManagedArtifactBytes(bytes, target);
|
const artifact = inspectManagedArtifactBytes(bytes, target);
|
||||||
@@ -288,7 +292,7 @@ describe("independent managed metadata facets", () => {
|
|||||||
const native = inspectManagedNativeBoundariesBytes(bytes, target);
|
const native = inspectManagedNativeBoundariesBytes(bytes, target);
|
||||||
|
|
||||||
for (const result of [artifact, members, native]) {
|
for (const result of [artifact, members, native]) {
|
||||||
expect(result.module?.mvid).toBeTruthy();
|
expect(result.module?.mvid).toBe("0ec6eb3a-4adc-4b54-1f45-8b4ed40b33b1");
|
||||||
expect(result.metadata.status).toBe("partial");
|
expect(result.metadata.status).toBe("partial");
|
||||||
expect(result.coverage).toMatchObject({
|
expect(result.coverage).toMatchObject({
|
||||||
state: "partial",
|
state: "partial",
|
||||||
|
|||||||
@@ -182,15 +182,13 @@ describe("ASAR snapshot operation ownership", () => {
|
|||||||
const removalStartedPromise = new Promise<void>((resolve) => {
|
const removalStartedPromise = new Promise<void>((resolve) => {
|
||||||
removalStarted = resolve;
|
removalStarted = resolve;
|
||||||
});
|
});
|
||||||
const reader = new AsarArtifactReader(
|
const reader = new AsarArtifactReader(fixture.archive, {
|
||||||
fixture.archive,
|
removeSnapshot: async (path) => {
|
||||||
undefined,
|
|
||||||
async (path) => {
|
|
||||||
removalStarted?.();
|
removalStarted?.();
|
||||||
await removalGate;
|
await removalGate;
|
||||||
await rm(path, { recursive: true, force: true });
|
await rm(path, { recursive: true, force: true });
|
||||||
},
|
},
|
||||||
);
|
});
|
||||||
let prepared = false;
|
let prepared = false;
|
||||||
const preparing = reader.prepareContainer().then(() => {
|
const preparing = reader.prepareContainer().then(() => {
|
||||||
prepared = true;
|
prepared = true;
|
||||||
@@ -302,15 +300,13 @@ describe("ASAR captured-byte identity", () => {
|
|||||||
it("retains a completed inventory and snapshot ownership when cleanup fails", async () => {
|
it("retains a completed inventory and snapshot ownership when cleanup fails", async () => {
|
||||||
const fixture = await archiveFixture();
|
const fixture = await archiveFixture();
|
||||||
const attempts: string[] = [];
|
const attempts: string[] = [];
|
||||||
const reader = new AsarArtifactReader(
|
const reader = new AsarArtifactReader(fixture.archive, {
|
||||||
fixture.archive,
|
removeSnapshot: async (path) => {
|
||||||
undefined,
|
|
||||||
async (path) => {
|
|
||||||
attempts.push(path);
|
attempts.push(path);
|
||||||
if (attempts.length === 1) throw new Error("snapshot removal denied");
|
if (attempts.length === 1) throw new Error("snapshot removal denied");
|
||||||
await rm(path, { recursive: true, force: true });
|
await rm(path, { recursive: true, force: true });
|
||||||
},
|
},
|
||||||
);
|
});
|
||||||
const resourceScope = new ArtifactResourceScope();
|
const resourceScope = new ArtifactResourceScope();
|
||||||
onTestFinished(() => resourceScope.close());
|
onTestFinished(() => resourceScope.close());
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -0,0 +1,266 @@
|
|||||||
|
import { fstatSync } from "node:fs";
|
||||||
|
import {
|
||||||
|
lstat,
|
||||||
|
mkdir,
|
||||||
|
open as openFile,
|
||||||
|
type FileHandle,
|
||||||
|
writeFile,
|
||||||
|
} from "node:fs/promises";
|
||||||
|
import { dirname, join } from "node:path";
|
||||||
|
|
||||||
|
import { createPackage } from "@electron/asar";
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { AsarArtifactReader } from "../../../src/artifacts/AsarArtifactReader.js";
|
||||||
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||||
|
|
||||||
|
const CONTENT = "module.exports = 'owned';\n";
|
||||||
|
|
||||||
|
const createArchive = async (content = CONTENT) => {
|
||||||
|
const root = await createTestTempDirectory("rea-asar-reader-cleanup-");
|
||||||
|
const source = join(root, "source");
|
||||||
|
const archive = join(root, "fixture.asar");
|
||||||
|
await mkdir(source);
|
||||||
|
await writeFile(join(source, "main.js"), content);
|
||||||
|
await createPackage(source, archive);
|
||||||
|
return { archive };
|
||||||
|
};
|
||||||
|
|
||||||
|
type OpenedHandle = { readonly path: string; readonly fd: number };
|
||||||
|
type ReaderHooks = {
|
||||||
|
readonly beforeAdoption?: (path: string, handle: FileHandle) => Promise<void>;
|
||||||
|
readonly close?: (handle: FileHandle) => Promise<void>;
|
||||||
|
};
|
||||||
|
|
||||||
|
const createTrackedReader = (archive: string, hooks: ReaderHooks = {}) => {
|
||||||
|
const opened: OpenedHandle[] = [];
|
||||||
|
const reader = new AsarArtifactReader(archive, {
|
||||||
|
openFile: async (path, flags) => {
|
||||||
|
const handle = await openFile(path, flags);
|
||||||
|
opened.push({ path, fd: handle.fd });
|
||||||
|
await hooks.beforeAdoption?.(path, handle);
|
||||||
|
return {
|
||||||
|
get fd() {
|
||||||
|
return handle.fd;
|
||||||
|
},
|
||||||
|
stat: () => handle.stat(),
|
||||||
|
read: handle.read.bind(handle),
|
||||||
|
close: () => hooks.close?.(handle) ?? handle.close(),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return { reader, opened };
|
||||||
|
};
|
||||||
|
|
||||||
|
const packedMember = async (reader: AsarArtifactReader) => {
|
||||||
|
for await (const entry of reader.entries())
|
||||||
|
if (entry.path === "main.js") return entry;
|
||||||
|
throw new Error("Expected packed ASAR member");
|
||||||
|
};
|
||||||
|
|
||||||
|
const latestHandle = (opened: readonly OpenedHandle[]): OpenedHandle => {
|
||||||
|
const handle = opened.at(-1);
|
||||||
|
if (handle === undefined)
|
||||||
|
throw new Error("Expected a descriptor for the packed member");
|
||||||
|
return handle;
|
||||||
|
};
|
||||||
|
|
||||||
|
const openPackedMember = async (
|
||||||
|
reader: AsarArtifactReader,
|
||||||
|
opened: readonly OpenedHandle[],
|
||||||
|
) => {
|
||||||
|
const output = await reader.open(await packedMember(reader));
|
||||||
|
const handle = latestHandle(opened);
|
||||||
|
return { output, handle, root: dirname(handle.path) };
|
||||||
|
};
|
||||||
|
|
||||||
|
const expectDescriptorOpen = (fd: number): void => {
|
||||||
|
expect(fstatSync(fd).isFile()).toBe(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const expectDescriptorClosed = (fd: number): void => {
|
||||||
|
expect(() => fstatSync(fd)).toThrow();
|
||||||
|
};
|
||||||
|
|
||||||
|
const expectSnapshotRemoved = async (
|
||||||
|
fd: number,
|
||||||
|
root: string,
|
||||||
|
): Promise<void> => {
|
||||||
|
expectDescriptorClosed(fd);
|
||||||
|
await expect(lstat(root)).rejects.toMatchObject({ code: "ENOENT" });
|
||||||
|
};
|
||||||
|
|
||||||
|
describe("ASAR reader cleanup", () => {
|
||||||
|
it("reaps an unread packed member and its snapshot when the reader closes", async () => {
|
||||||
|
const { archive } = await createArchive();
|
||||||
|
const { reader, opened } = createTrackedReader(archive);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const {
|
||||||
|
output,
|
||||||
|
handle: memberHandle,
|
||||||
|
root,
|
||||||
|
} = await openPackedMember(reader, opened);
|
||||||
|
expect(memberHandle.path).toContain("rea-asar-snapshot-");
|
||||||
|
expectDescriptorOpen(memberHandle.fd);
|
||||||
|
expect(output.destroyed).toBe(false);
|
||||||
|
|
||||||
|
await reader.close();
|
||||||
|
|
||||||
|
await expectSnapshotRemoved(memberHandle.fd, root);
|
||||||
|
} finally {
|
||||||
|
await reader.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("retains the snapshot and facts after a failed close, then retries cleanup", async () => {
|
||||||
|
const { archive } = await createArchive();
|
||||||
|
let failNextClose = false;
|
||||||
|
const closeFailure = Object.assign(new Error("injected close failure"), {
|
||||||
|
code: "EIO",
|
||||||
|
errno: -5,
|
||||||
|
syscall: "close",
|
||||||
|
});
|
||||||
|
const { reader, opened } = createTrackedReader(archive, {
|
||||||
|
close: async (handle) => {
|
||||||
|
if (failNextClose) {
|
||||||
|
failNextClose = false;
|
||||||
|
throw closeFailure;
|
||||||
|
}
|
||||||
|
await handle.close();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { handle: memberHandle, root } = await openPackedMember(
|
||||||
|
reader,
|
||||||
|
opened,
|
||||||
|
);
|
||||||
|
failNextClose = true;
|
||||||
|
|
||||||
|
await expect(reader.close()).rejects.toMatchObject({
|
||||||
|
cleanup: { resources: [memberHandle.path] },
|
||||||
|
});
|
||||||
|
expectDescriptorOpen(memberHandle.fd);
|
||||||
|
expect((await lstat(root)).isDirectory()).toBe(true);
|
||||||
|
expect(await packedMember(reader)).toMatchObject({ path: "main.js" });
|
||||||
|
|
||||||
|
await reader.close();
|
||||||
|
|
||||||
|
await expectSnapshotRemoved(memberHandle.fd, root);
|
||||||
|
} finally {
|
||||||
|
await reader.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("closes a member handle adopted while reader shutdown is queued", async () => {
|
||||||
|
const { archive } = await createArchive();
|
||||||
|
let pauseMemberOpen = false;
|
||||||
|
let memberOpenReached!: () => void;
|
||||||
|
let releaseMemberOpen!: () => void;
|
||||||
|
const memberOpenStarted = new Promise<void>((resolve) => {
|
||||||
|
memberOpenReached = resolve;
|
||||||
|
});
|
||||||
|
const memberOpenGate = new Promise<void>((resolve) => {
|
||||||
|
releaseMemberOpen = resolve;
|
||||||
|
});
|
||||||
|
const { reader, opened } = createTrackedReader(archive, {
|
||||||
|
beforeAdoption: async (path) => {
|
||||||
|
if (pauseMemberOpen && path.includes("rea-asar-snapshot-")) {
|
||||||
|
pauseMemberOpen = false;
|
||||||
|
memberOpenReached();
|
||||||
|
await memberOpenGate;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const entry = await packedMember(reader);
|
||||||
|
pauseMemberOpen = true;
|
||||||
|
const opening = reader.open(entry);
|
||||||
|
await memberOpenStarted;
|
||||||
|
const closing = reader.close();
|
||||||
|
releaseMemberOpen();
|
||||||
|
const output = await opening;
|
||||||
|
const memberHandle = latestHandle(opened);
|
||||||
|
const root = dirname(memberHandle.path);
|
||||||
|
|
||||||
|
await closing;
|
||||||
|
|
||||||
|
expect(output.destroyed).toBe(true);
|
||||||
|
await expectSnapshotRemoved(memberHandle.fd, root);
|
||||||
|
} finally {
|
||||||
|
releaseMemberOpen();
|
||||||
|
await reader.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("ASAR preparation cleanup", () => {
|
||||||
|
it("retains a zero-byte member handle when its first close fails", async () => {
|
||||||
|
const { archive } = await createArchive("");
|
||||||
|
let failNextClose = false;
|
||||||
|
const { reader, opened } = createTrackedReader(archive, {
|
||||||
|
close: async (handle) => {
|
||||||
|
if (failNextClose) {
|
||||||
|
failNextClose = false;
|
||||||
|
throw new Error("empty member close denied");
|
||||||
|
}
|
||||||
|
await handle.close();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
const entry = await packedMember(reader);
|
||||||
|
failNextClose = true;
|
||||||
|
await expect(reader.open(entry)).rejects.toMatchObject({
|
||||||
|
cleanup: {
|
||||||
|
reason: "empty member close denied",
|
||||||
|
resources: [expect.any(String)],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const handle = latestHandle(opened);
|
||||||
|
expectDescriptorOpen(handle.fd);
|
||||||
|
expect((await lstat(dirname(handle.path))).isDirectory()).toBe(true);
|
||||||
|
await reader.close();
|
||||||
|
await expectSnapshotRemoved(handle.fd, dirname(handle.path));
|
||||||
|
} finally {
|
||||||
|
await reader.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves a malformed header failure when closing its handle also fails", async () => {
|
||||||
|
const { archive } = await createArchive();
|
||||||
|
await writeFile(archive, Buffer.alloc(16));
|
||||||
|
let failHeaderClose = false;
|
||||||
|
const closeFailure = new Error("header close denied");
|
||||||
|
const { reader, opened } = createTrackedReader(archive, {
|
||||||
|
beforeAdoption: async (path) => {
|
||||||
|
if (path !== archive) failHeaderClose = true;
|
||||||
|
},
|
||||||
|
close: async (handle) => {
|
||||||
|
if (failHeaderClose) {
|
||||||
|
failHeaderClose = false;
|
||||||
|
throw closeFailure;
|
||||||
|
}
|
||||||
|
await handle.close();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
await expect(packedMember(reader)).rejects.toMatchObject({
|
||||||
|
reason: "format",
|
||||||
|
message: "Truncated or invalid ASAR size pickle",
|
||||||
|
cleanup: {
|
||||||
|
reason: closeFailure.message,
|
||||||
|
resources: [expect.any(String)],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const handle = latestHandle(opened);
|
||||||
|
expectDescriptorOpen(handle.fd);
|
||||||
|
expect((await lstat(dirname(handle.path))).isDirectory()).toBe(true);
|
||||||
|
await reader.close();
|
||||||
|
await expectSnapshotRemoved(handle.fd, dirname(handle.path));
|
||||||
|
} finally {
|
||||||
|
await reader.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -9,11 +9,11 @@ import {
|
|||||||
writeFile,
|
writeFile,
|
||||||
truncate,
|
truncate,
|
||||||
} from "node:fs/promises";
|
} from "node:fs/promises";
|
||||||
import { join } from "node:path";
|
import { dirname, join } from "node:path";
|
||||||
import { Readable } from "node:stream";
|
import { Readable } from "node:stream";
|
||||||
import { buffer } from "node:stream/consumers";
|
import { buffer } from "node:stream/consumers";
|
||||||
import { createPackageWithOptions } from "@electron/asar";
|
import { createPackageWithOptions } from "@electron/asar";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it, onTestFinished } from "vitest";
|
||||||
import { scanArtifactInventory } from "../../fixtures/artifactInventory.js";
|
import { scanArtifactInventory } from "../../fixtures/artifactInventory.js";
|
||||||
import { AsarArtifactReader } from "../../../src/artifacts/AsarArtifactReader.js";
|
import { AsarArtifactReader } from "../../../src/artifacts/AsarArtifactReader.js";
|
||||||
import {
|
import {
|
||||||
@@ -105,7 +105,13 @@ describe("ASAR entry streaming", () => {
|
|||||||
await writeFile(join(source, "small.js"), "module.exports = 1;\n");
|
await writeFile(join(source, "small.js"), "module.exports = 1;\n");
|
||||||
await createPackageWithOptions(source, archive, {});
|
await createPackageWithOptions(source, archive, {});
|
||||||
|
|
||||||
const reader = new AsarArtifactReader(archive);
|
let snapshotPath: string | undefined;
|
||||||
|
const reader = new AsarArtifactReader(archive, {
|
||||||
|
openFile: async (path, flags) => {
|
||||||
|
if (path !== archive) snapshotPath = path;
|
||||||
|
return openFile(path, flags);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const entries = [];
|
const entries = [];
|
||||||
@@ -115,9 +121,11 @@ describe("ASAR entry streaming", () => {
|
|||||||
const entry = entries.find(({ path }) => path === "small.js");
|
const entry = entries.find(({ path }) => path === "small.js");
|
||||||
if (entry === undefined) throw new Error("Expected packed ASAR member");
|
if (entry === undefined) throw new Error("Expected packed ASAR member");
|
||||||
const snapshot = await reader.openContainer();
|
const snapshot = await reader.openContainer();
|
||||||
const identity = await lstat(snapshot.path);
|
if (snapshotPath === undefined)
|
||||||
|
throw new Error("Expected snapshot acquisition");
|
||||||
|
const identity = await lstat(snapshotPath);
|
||||||
await buffer(snapshot);
|
await buffer(snapshot);
|
||||||
const probe = await openFile(snapshot.path, "r");
|
const probe = await openFile(snapshotPath, "r");
|
||||||
expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);
|
expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);
|
||||||
await probe.close();
|
await probe.close();
|
||||||
expect(await matchingDescriptorCount(identity)).toBe(0);
|
expect(await matchingDescriptorCount(identity)).toBe(0);
|
||||||
@@ -145,7 +153,13 @@ describe("ASAR entry streaming", () => {
|
|||||||
await writeFile(join(source, "small.js"), "module.exports = 1;\n");
|
await writeFile(join(source, "small.js"), "module.exports = 1;\n");
|
||||||
await createPackageWithOptions(source, archive, {});
|
await createPackageWithOptions(source, archive, {});
|
||||||
|
|
||||||
const reader = new AsarArtifactReader(archive);
|
let snapshotPath: string | undefined;
|
||||||
|
const reader = new AsarArtifactReader(archive, {
|
||||||
|
openFile: async (path, flags) => {
|
||||||
|
if (path !== archive) snapshotPath = path;
|
||||||
|
return openFile(path, flags);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const entries = [];
|
const entries = [];
|
||||||
@@ -153,7 +167,9 @@ describe("ASAR entry streaming", () => {
|
|||||||
const entry = entries.find(({ path }) => path === "small.js");
|
const entry = entries.find(({ path }) => path === "small.js");
|
||||||
if (entry === undefined) throw new Error("Expected packed ASAR member");
|
if (entry === undefined) throw new Error("Expected packed ASAR member");
|
||||||
const snapshot = await reader.openContainer();
|
const snapshot = await reader.openContainer();
|
||||||
const identity = await lstat(snapshot.path);
|
if (snapshotPath === undefined)
|
||||||
|
throw new Error("Expected snapshot acquisition");
|
||||||
|
const identity = await lstat(snapshotPath);
|
||||||
await buffer(snapshot);
|
await buffer(snapshot);
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const opening = reader.open(entry, controller.signal);
|
const opening = reader.open(entry, controller.signal);
|
||||||
@@ -340,23 +356,25 @@ describe("ASAR read failures", () => {
|
|||||||
syscall: "close",
|
syscall: "close",
|
||||||
});
|
});
|
||||||
let closeCount = 0;
|
let closeCount = 0;
|
||||||
const reader = new AsarArtifactReader(archive, async (path, flags) => {
|
const reader = new AsarArtifactReader(archive, {
|
||||||
const handle = await openFile(path, flags);
|
openUnpackedFile: async (path, flags) => {
|
||||||
expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);
|
const handle = await openFile(path, flags);
|
||||||
return {
|
expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);
|
||||||
get fd() {
|
return {
|
||||||
return handle.fd;
|
get fd() {
|
||||||
},
|
return handle.fd;
|
||||||
async stat() {
|
},
|
||||||
throw ioError;
|
async stat() {
|
||||||
},
|
throw ioError;
|
||||||
async close() {
|
},
|
||||||
closeCount += 1;
|
async close() {
|
||||||
if (closeCount === 1) throw closeError;
|
closeCount += 1;
|
||||||
await handle.close();
|
if (closeCount === 1) throw closeError;
|
||||||
},
|
await handle.close();
|
||||||
read: handle.read.bind(handle),
|
},
|
||||||
};
|
read: handle.read.bind(handle),
|
||||||
|
};
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -396,24 +414,37 @@ it.skipIf(process.platform === "win32")(
|
|||||||
});
|
});
|
||||||
const closeFailure = new Error("native close outcome unavailable");
|
const closeFailure = new Error("native close outcome unavailable");
|
||||||
let closeCount = 0;
|
let closeCount = 0;
|
||||||
const reader = new AsarArtifactReader(archive, async (path, flags) => {
|
let snapshotRoot: string | undefined;
|
||||||
const handle = await openFile(path, flags);
|
const reader = new AsarArtifactReader(archive, {
|
||||||
return {
|
openFile: async (path, flags) => {
|
||||||
get fd() {
|
if (path !== archive && snapshotRoot === undefined) {
|
||||||
return handle.fd;
|
const root = dirname(path);
|
||||||
},
|
snapshotRoot = root;
|
||||||
async stat() {
|
// This fixture deliberately reports an unrecoverable close outcome.
|
||||||
throw statFailure;
|
// Remove its retained snapshot only after testing reader ownership.
|
||||||
},
|
onTestFinished(() => rm(root, { recursive: true, force: true }));
|
||||||
async close() {
|
}
|
||||||
closeCount += 1;
|
return openFile(path, flags);
|
||||||
// Model Node's invalidated-descriptor rejection without leaking a
|
},
|
||||||
// native descriptor in the test process.
|
openUnpackedFile: async (path, flags) => {
|
||||||
await handle.close();
|
const handle = await openFile(path, flags);
|
||||||
throw closeFailure;
|
return {
|
||||||
},
|
get fd() {
|
||||||
read: handle.read.bind(handle),
|
return handle.fd;
|
||||||
};
|
},
|
||||||
|
async stat() {
|
||||||
|
throw statFailure;
|
||||||
|
},
|
||||||
|
async close() {
|
||||||
|
closeCount += 1;
|
||||||
|
// Model Node's invalidated-descriptor rejection without leaking a
|
||||||
|
// native descriptor in the test process.
|
||||||
|
await handle.close();
|
||||||
|
throw closeFailure;
|
||||||
|
},
|
||||||
|
read: handle.read.bind(handle),
|
||||||
|
};
|
||||||
|
},
|
||||||
});
|
});
|
||||||
const entry = await unpackedMember(reader);
|
const entry = await unpackedMember(reader);
|
||||||
await expect(reader.open(entry)).rejects.toMatchObject({
|
await expect(reader.open(entry)).rejects.toMatchObject({
|
||||||
@@ -430,6 +461,8 @@ it.skipIf(process.platform === "win32")(
|
|||||||
});
|
});
|
||||||
expect(closeCount).toBe(1);
|
expect(closeCount).toBe(1);
|
||||||
await waitForNoMatchingDescriptor(identity);
|
await waitForNoMatchingDescriptor(identity);
|
||||||
|
if (snapshotRoot === undefined) throw new Error("Expected ASAR snapshot");
|
||||||
|
expect((await lstat(snapshotRoot)).isDirectory()).toBe(true);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -439,24 +472,26 @@ it.skipIf(process.platform === "win32")(
|
|||||||
const { archive, unpackedPath, identity } = await createUnpackedFixture();
|
const { archive, unpackedPath, identity } = await createUnpackedFixture();
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
let closeCount = 0;
|
let closeCount = 0;
|
||||||
const reader = new AsarArtifactReader(archive, async (path, flags) => {
|
const reader = new AsarArtifactReader(archive, {
|
||||||
const handle = await openFile(path, flags);
|
openUnpackedFile: async (path, flags) => {
|
||||||
return {
|
const handle = await openFile(path, flags);
|
||||||
get fd() {
|
return {
|
||||||
return handle.fd;
|
get fd() {
|
||||||
},
|
return handle.fd;
|
||||||
async stat() {
|
},
|
||||||
const observed = await handle.stat();
|
async stat() {
|
||||||
controller.abort();
|
const observed = await handle.stat();
|
||||||
return observed;
|
controller.abort();
|
||||||
},
|
return observed;
|
||||||
async close() {
|
},
|
||||||
closeCount += 1;
|
async close() {
|
||||||
if (closeCount === 1) throw new Error("first close failed");
|
closeCount += 1;
|
||||||
await handle.close();
|
if (closeCount === 1) throw new Error("first close failed");
|
||||||
},
|
await handle.close();
|
||||||
read: handle.read.bind(handle),
|
},
|
||||||
};
|
read: handle.read.bind(handle),
|
||||||
|
};
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -484,25 +519,27 @@ it.skipIf(process.platform === "win32")(
|
|||||||
const { archive, unpackedPath, identity } = await createUnpackedFixture();
|
const { archive, unpackedPath, identity } = await createUnpackedFixture();
|
||||||
const closeError = new Error("first close failed");
|
const closeError = new Error("first close failed");
|
||||||
let closeCount = 0;
|
let closeCount = 0;
|
||||||
const reader = new AsarArtifactReader(archive, async (path, flags) => {
|
const reader = new AsarArtifactReader(archive, {
|
||||||
const handle = await openFile(path, flags);
|
openUnpackedFile: async (path, flags) => {
|
||||||
return {
|
const handle = await openFile(path, flags);
|
||||||
get fd() {
|
return {
|
||||||
return handle.fd;
|
get fd() {
|
||||||
},
|
return handle.fd;
|
||||||
async stat() {
|
},
|
||||||
const observed = await handle.stat();
|
async stat() {
|
||||||
return Object.assign(Object.create(observed), {
|
const observed = await handle.stat();
|
||||||
ino: observed.ino + 1,
|
return Object.assign(Object.create(observed), {
|
||||||
});
|
ino: observed.ino + 1,
|
||||||
},
|
});
|
||||||
async close() {
|
},
|
||||||
closeCount += 1;
|
async close() {
|
||||||
if (closeCount === 1) throw closeError;
|
closeCount += 1;
|
||||||
await handle.close();
|
if (closeCount === 1) throw closeError;
|
||||||
},
|
await handle.close();
|
||||||
read: handle.read.bind(handle),
|
},
|
||||||
};
|
read: handle.read.bind(handle),
|
||||||
|
};
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -527,16 +564,15 @@ it.skipIf(process.platform === "win32")(
|
|||||||
);
|
);
|
||||||
|
|
||||||
it.skipIf(process.platform === "win32")(
|
it.skipIf(process.platform === "win32")(
|
||||||
"retains unpacked handles and snapshots across combined cleanup failures",
|
"retains unpacked handles before retrying snapshot cleanup failures",
|
||||||
async () => {
|
async () => {
|
||||||
const { archive, unpackedPath, identity } = await createUnpackedFixture();
|
const { archive, unpackedPath, identity } = await createUnpackedFixture();
|
||||||
const closeError = new Error("unpacked close denied");
|
const closeError = new Error("unpacked close denied");
|
||||||
const snapshotError = new Error("snapshot removal denied");
|
const snapshotError = new Error("snapshot removal denied");
|
||||||
let closeCount = 0;
|
let closeCount = 0;
|
||||||
const snapshotPaths: string[] = [];
|
const snapshotPaths: string[] = [];
|
||||||
const reader = new AsarArtifactReader(
|
const reader = new AsarArtifactReader(archive, {
|
||||||
archive,
|
openUnpackedFile: async (path, flags) => {
|
||||||
async (path, flags) => {
|
|
||||||
const handle = await openFile(path, flags);
|
const handle = await openFile(path, flags);
|
||||||
return {
|
return {
|
||||||
get fd() {
|
get fd() {
|
||||||
@@ -557,12 +593,12 @@ it.skipIf(process.platform === "win32")(
|
|||||||
read: handle.read.bind(handle),
|
read: handle.read.bind(handle),
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
async (path) => {
|
removeSnapshot: async (path) => {
|
||||||
snapshotPaths.push(path);
|
snapshotPaths.push(path);
|
||||||
if (snapshotPaths.length === 1) throw snapshotError;
|
if (snapshotPaths.length === 1) throw snapshotError;
|
||||||
await rm(path, { recursive: true, force: true });
|
await rm(path, { recursive: true, force: true });
|
||||||
},
|
},
|
||||||
);
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const entry = await unpackedMember(reader);
|
const entry = await unpackedMember(reader);
|
||||||
@@ -579,19 +615,24 @@ it.skipIf(process.platform === "win32")(
|
|||||||
);
|
);
|
||||||
expect(cleanupFailure).toMatchObject({
|
expect(cleanupFailure).toMatchObject({
|
||||||
cleanup: {
|
cleanup: {
|
||||||
resources: [unpackedPath, expect.any(String)],
|
resources: [unpackedPath],
|
||||||
reason: expect.stringContaining("unpacked close denied"),
|
reason: expect.stringContaining("unpacked close denied"),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
expect(closeCount).toBe(2);
|
||||||
|
expect(snapshotPaths).toEqual([]);
|
||||||
|
expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);
|
||||||
|
|
||||||
|
await expect(reader.close()).rejects.toMatchObject({
|
||||||
|
message: snapshotError.message,
|
||||||
|
cleanup: { resources: [expect.any(String)] },
|
||||||
|
});
|
||||||
|
expect(closeCount).toBe(3);
|
||||||
|
await waitForNoMatchingDescriptor(identity);
|
||||||
const snapshotRoot = snapshotPaths[0];
|
const snapshotRoot = snapshotPaths[0];
|
||||||
if (snapshotRoot === undefined)
|
if (snapshotRoot === undefined)
|
||||||
throw new Error("Expected owned ASAR snapshot root");
|
throw new Error("Expected owned ASAR snapshot root");
|
||||||
expect(snapshotPaths).toEqual([snapshotRoot]);
|
expect(snapshotPaths).toEqual([snapshotRoot]);
|
||||||
expect(cleanupFailure).toMatchObject({
|
|
||||||
cleanup: { resources: [unpackedPath, snapshotRoot] },
|
|
||||||
});
|
|
||||||
expect(closeCount).toBe(2);
|
|
||||||
expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);
|
|
||||||
expect((await lstat(snapshotRoot)).isDirectory()).toBe(true);
|
expect((await lstat(snapshotRoot)).isDirectory()).toBe(true);
|
||||||
|
|
||||||
await reader.close();
|
await reader.close();
|
||||||
|
|||||||
@@ -285,12 +285,17 @@ describe("provider process output and cleanup primitives", () => {
|
|||||||
await stopProviderProcessFixture(child);
|
await stopProviderProcessFixture(child);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("provider process cleanup authority", () => {
|
||||||
it("honors verified group cleanup instead of direct process signaling", async () => {
|
it("honors verified group cleanup instead of direct process signaling", async () => {
|
||||||
const child = spawnProviderProcessFixture("stubborn");
|
const child = spawnProviderProcessFixture("stubborn");
|
||||||
await waitForProviderProcessReady(child);
|
const signalFromCleanup = child.kill.bind(child);
|
||||||
|
const directSignal = vi.spyOn(child, "kill");
|
||||||
const cleanup = vi.fn(async () => {
|
const cleanup = vi.fn(async () => {
|
||||||
child.kill("SIGKILL");
|
// Keep the injected cleanup signal separate from the supervisor's
|
||||||
|
// direct method, while retaining the child handle's lifetime checks.
|
||||||
|
signalFromCleanup("SIGKILL");
|
||||||
return { cleaned: true, signaled: true } as const;
|
return { cleaned: true, signaled: true } as const;
|
||||||
});
|
});
|
||||||
const supervisor = new ProviderProcessSupervisor({
|
const supervisor = new ProviderProcessSupervisor({
|
||||||
@@ -299,12 +304,19 @@ describe("provider process output and cleanup primitives", () => {
|
|||||||
cleanup,
|
cleanup,
|
||||||
});
|
});
|
||||||
|
|
||||||
await expect(supervisor.stop({ killGraceMs: 500 })).resolves.toEqual({
|
try {
|
||||||
status: "verified-cleanup",
|
await waitForProviderProcessReady(child);
|
||||||
});
|
await expect(supervisor.stop({ killGraceMs: 500 })).resolves.toEqual({
|
||||||
// Cleanup must be attempted. The exact attempt count is an internal retry
|
status: "verified-cleanup",
|
||||||
// ladder, so it is deliberately not pinned here.
|
});
|
||||||
expect(cleanup).toHaveBeenCalled();
|
// Cleanup must be attempted. The exact attempt count is an internal
|
||||||
|
// retry ladder, so it is deliberately not pinned here.
|
||||||
|
expect(cleanup).toHaveBeenCalled();
|
||||||
|
expect(directSignal).not.toHaveBeenCalled();
|
||||||
|
} finally {
|
||||||
|
directSignal.mockRestore();
|
||||||
|
await stopProviderProcessFixture(child);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("cleans an owned group even after its launcher leader has exited", async () => {
|
it("cleans an owned group even after its launcher leader has exited", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user