fix(android): support owned JADX stdio on Windows (#1276)
Support owned JADX stdio on Windows x64, preserve protocol input errors through cancellation, and verify native stdin and Android CLI/MCP lifecycle on the real Windows host. Closes #1262
This commit is contained in:
@@ -7,7 +7,7 @@ import {
|
||||
type WindowsNativeLoadHost,
|
||||
} from "./WindowsNativeLoader.js";
|
||||
|
||||
const fixture = () => {
|
||||
const fixture = (protocolStdin?: boolean) => {
|
||||
const bytes = Buffer.alloc(128);
|
||||
bytes.write("MZ");
|
||||
bytes.writeUInt32LE(64, 0x3c);
|
||||
@@ -51,6 +51,7 @@ const fixture = () => {
|
||||
privateDacl: true,
|
||||
atomicJobAssignment: true,
|
||||
killOnOwnerClose: true,
|
||||
...(protocolStdin === undefined ? {} : { protocolStdin }),
|
||||
}),
|
||||
};
|
||||
},
|
||||
@@ -59,6 +60,22 @@ const fixture = () => {
|
||||
};
|
||||
|
||||
describe("packaged Windows native admission", () => {
|
||||
it.each([true, false])(
|
||||
"preserves protocol stdin capability %s alongside existing job controls",
|
||||
(protocolStdin) => {
|
||||
const value = fixture(protocolStdin);
|
||||
expect(loadWindowsNativeAuthority(value.host)).toMatchObject({
|
||||
available: true,
|
||||
authority: {
|
||||
inspection: {
|
||||
protocolStdin,
|
||||
atomicJobAssignment: true,
|
||||
killOnOwnerClose: true,
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
it("loads only after compatibility and digest checks, preserving producer identity", () => {
|
||||
const value = fixture();
|
||||
expect(loadWindowsNativeAuthority(value.host)).toMatchObject({
|
||||
|
||||
@@ -43,6 +43,7 @@ const inspectionSchema = z.strictObject({
|
||||
privateDacl: z.literal(true),
|
||||
atomicJobAssignment: z.literal(true),
|
||||
killOnOwnerClose: z.literal(true),
|
||||
protocolStdin: z.boolean().optional(),
|
||||
});
|
||||
|
||||
type NativeCall = (
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { EventEmitter } from "node:events";
|
||||
import { PassThrough } from "node:stream";
|
||||
import { PassThrough, Writable } from "node:stream";
|
||||
|
||||
import { z } from "zod";
|
||||
|
||||
@@ -36,6 +36,7 @@ export const quoteWindowsProcessArgument = (value: string): string => {
|
||||
export class WindowsOwnedProcess extends EventEmitter {
|
||||
readonly stdout = new PassThrough();
|
||||
readonly stderr = new PassThrough();
|
||||
readonly stdin: Writable | null;
|
||||
readonly signalCode: NodeJS.Signals | null = null;
|
||||
exitCode: number | null = null;
|
||||
readonly pid: number;
|
||||
@@ -55,9 +56,14 @@ export class WindowsOwnedProcess extends EventEmitter {
|
||||
cwd: string | undefined,
|
||||
environment: NodeJS.ProcessEnv,
|
||||
verbatim: boolean,
|
||||
protocolStdin = false,
|
||||
) {
|
||||
super();
|
||||
this.#authority = requireWindowsNativeAuthority();
|
||||
if (protocolStdin && this.#authority.inspection.protocolStdin !== true)
|
||||
throw new Error(
|
||||
"The bundled Windows native artifact does not support owned protocol stdin; use a matching current Windows x64 REA package.",
|
||||
);
|
||||
const entries = Object.entries(environment)
|
||||
.filter((entry): entry is [string, string] => entry[1] !== undefined)
|
||||
.map(([key, value]) => `${key}=${value}`);
|
||||
@@ -75,16 +81,70 @@ export class WindowsOwnedProcess extends EventEmitter {
|
||||
line,
|
||||
cwd ?? "",
|
||||
entries,
|
||||
...(protocolStdin ? [true] : []),
|
||||
]),
|
||||
);
|
||||
this.pid = launched.pid;
|
||||
this.#handle = launched.handle;
|
||||
this.stdin = protocolStdin
|
||||
? new Writable({
|
||||
write: (chunk: unknown, _encoding, callback) => {
|
||||
void this.#writeInput(chunk).then(
|
||||
() => callback(),
|
||||
(cause: unknown) =>
|
||||
callback(
|
||||
cause instanceof Error ? cause : new Error(String(cause)),
|
||||
),
|
||||
);
|
||||
},
|
||||
final: (callback) => {
|
||||
try {
|
||||
this.#authority.call("process_stdin_close", [this.#handle]);
|
||||
callback();
|
||||
} catch (cause: unknown) {
|
||||
callback(
|
||||
cause instanceof Error ? cause : new Error(String(cause)),
|
||||
);
|
||||
}
|
||||
},
|
||||
destroy: (cause, callback) => {
|
||||
try {
|
||||
if (!this.#closed)
|
||||
this.#authority.call("process_stdin_close", [this.#handle]);
|
||||
callback(cause);
|
||||
} catch (failure: unknown) {
|
||||
callback(
|
||||
failure instanceof Error ? failure : new Error(String(failure)),
|
||||
);
|
||||
}
|
||||
},
|
||||
})
|
||||
: null;
|
||||
this.#settled = new Promise((resolve) => {
|
||||
this.#settle = resolve;
|
||||
});
|
||||
this.#schedule();
|
||||
}
|
||||
|
||||
async #writeInput(chunk: unknown): Promise<void> {
|
||||
const bytes = z.instanceof(Buffer).parse(chunk);
|
||||
const written = z
|
||||
.number()
|
||||
.int()
|
||||
.nonnegative()
|
||||
.max(Number.MAX_SAFE_INTEGER)
|
||||
.parse(
|
||||
await this.#authority.call("process_stdin_write", [
|
||||
this.#handle,
|
||||
bytes,
|
||||
]),
|
||||
);
|
||||
if (written !== bytes.length)
|
||||
throw new Error(
|
||||
"Owned Windows input write did not preserve all requested bytes",
|
||||
);
|
||||
}
|
||||
|
||||
/** Terminate the retained job, never a PID supplied by another caller. */
|
||||
kill(signal?: NodeJS.Signals | number): boolean {
|
||||
if (signal === 0)
|
||||
@@ -184,6 +244,7 @@ export class WindowsOwnedProcess extends EventEmitter {
|
||||
if (this.#timer !== undefined) clearTimeout(this.#timer);
|
||||
this.#authority.call("process_close", [this.#handle]);
|
||||
this.#closed = true;
|
||||
this.stdin?.destroy();
|
||||
this.stdout.end();
|
||||
this.stderr.end();
|
||||
this.#settle?.();
|
||||
|
||||
Reference in New Issue
Block a user