better stub child_process channel

This commit is contained in:
Alistair Smith
2025-05-29 11:45:18 -07:00
parent b6d4a15496
commit 3bc5724ff4

View File

@@ -1,3 +1,5 @@
import type { Pipe as NodeStreamPipe } from "node:stream";
// Hardcoded module "node:child_process"
const EventEmitter = require("node:events");
const OsModule = require("node:os");
@@ -1041,13 +1043,15 @@ class ChildProcess extends EventEmitter {
#handle;
#closesNeeded = 1;
#closesGot = 0;
disconnect: undefined | (() => void);
signalCode = null;
exitCode = null;
spawnfile;
spawnargs;
pid;
channel;
channel: NodeStreamPipe | undefined;
killed = false;
[Symbol.dispose]() {
@@ -1371,7 +1375,7 @@ class ChildProcess extends EventEmitter {
if (has_ipc) {
this.send = this.#send;
this.disconnect = this.#disconnect;
this.channel = new Control();
this.channel = new SubprocessChannel(this);
Object.defineProperty(this, "_channel", {
get() {
return this.channel;
@@ -1730,9 +1734,53 @@ function abortChildProcess(child, killSignal, reason) {
}
}
class Control extends EventEmitter {
constructor() {
class SubprocessChannel extends EventEmitter implements NodeStreamPipe {
#hasRef: boolean = true;
#setRef: (enabled: boolean) => void;
#closed: boolean = false;
#childProcess: ChildProcess | undefined;
public constructor(childProcess?: ChildProcess) {
super();
this.#setRef = $newZigFunction("node_cluster_binding.zig", "setRef", 1);
this.#childProcess = childProcess;
}
public close(): void {
if (this.#closed) return;
this.#closed = true;
if (this.#hasRef) {
this.#hasRef = false;
this.#setRef(false);
}
if (this.#childProcess) {
this.#childProcess.disconnect?.();
}
process.nextTick(() => {
this.emit("close");
});
}
public hasRef(): boolean {
return this.#hasRef && !this.#closed;
}
public ref(): void {
if (!this.#hasRef && !this.#closed) {
this.#hasRef = true;
this.#setRef(true);
}
}
public unref(): void {
if (this.#hasRef) {
this.#hasRef = false;
this.#setRef(false);
}
}
}