From 57a06745a48093c25d0f4729ccea41a918d6427d Mon Sep 17 00:00:00 2001 From: dave caruso Date: Thu, 7 Sep 2023 04:58:44 -0700 Subject: Progress for Next.js (#4468) * L * ipc * asdfghjkl * dfghjk * it works! * types * patches for next.js * sdfghj * wsdfgn,./ * this * yolo * okay loser * asdfghjk * add some more APIs * MESS * sdfghjkl * remove native events from streams * stuff * remove lazy(primordials) test * debugging * okay * less fake extensions object * fix `Buffer.toString()` args logic * fix deserialize * make tests work * add test for `Buffer.toString` args * Update server.zig * remove test * update test * Update spawn-streaming-stdin.test.ts * fix linux build * Update fs.test.ts * cli message improvements * dfshaj * Fix fs.watch bug maybe? * remove --------- Co-authored-by: Dylan Conway --- src/js/node/child_process.js | 89 ++++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 36 deletions(-) (limited to 'src/js/node/child_process.js') diff --git a/src/js/node/child_process.js b/src/js/node/child_process.js index 6878f3ca1..859e01aa7 100644 --- a/src/js/node/child_process.js +++ b/src/js/node/child_process.js @@ -1158,6 +1158,9 @@ class ChildProcess extends EventEmitter { const stdio = options.stdio || ["pipe", "pipe", "pipe"]; const bunStdio = getBunStdioFromOptions(stdio); + // TODO: better ipc support + const ipc = $isArray(stdio) && stdio[3] === "ipc"; + var env = options.envPairs || undefined; const detachedOption = options.detached; this.#encoding = options.encoding || undefined; @@ -1182,53 +1185,67 @@ class ChildProcess extends EventEmitter { ); }, lazy: true, + ipc: ipc ? this.#emitIpcMessage.bind(this) : undefined, }); this.pid = this.#handle.pid; onSpawnNT(this); - // const ipc = stdio.ipc; - // const ipcFd = stdio.ipcFd; - // stdio = options.stdio = stdio.stdio; - - // for (i = 0; i < stdio.length; i++) { - // const stream = stdio[i]; - // if (stream.type === "ignore") continue; - - // if (stream.ipc) { - // this._closesNeeded++; - // continue; - // } + if (ipc) { + this.send = this.#send; + this.disconnect = this.#disconnect; + } + } - // // The stream is already cloned and piped, thus stop its readable side, - // // otherwise we might attempt to read from the stream when at the same time - // // the child process does. - // if (stream.type === "wrap") { - // stream.handle.reading = false; - // stream.handle.readStop(); - // stream._stdio.pause(); - // stream._stdio.readableFlowing = false; - // stream._stdio._readableState.reading = false; - // stream._stdio[kIsUsedAsStdio] = true; - // continue; - // } + #emitIpcMessage(message) { + this.emit("message", message); + } - // if (stream.handle) { - // stream.socket = createSocket( - // this.pid !== 0 ? stream.handle : null, - // i > 0 - // ); + #send(message, handle, options, callback) { + if (typeof handle === "function") { + callback = handle; + handle = undefined; + options = undefined; + } else if (typeof options === "function") { + callback = options; + options = undefined; + } else if (options !== undefined) { + if (typeof options !== "object" || options === null) { + throw new ERR_INVALID_ARG_TYPE("options", "Object", options); + } + } - // // Add .send() method and start listening for IPC data - // if (ipc !== undefined) setupChannel(this, ipc, serialization); - } + if (!this.#handle) { + if (callback) { + process.nextTick(callback, new TypeError("Process was closed while trying to send message")); + } else { + this.emit("error", new TypeError("Process was closed while trying to send message")); + } + return false; + } - send() { - console.log("ChildProcess.prototype.send() - Sorry! Not implemented yet"); + // Bun does not handle handles yet + try { + this.#handle.send(message); + if (callback) process.nextTick(callback); + return true; + } catch (error) { + if (callback) { + process.nextTick(callback, error); + } else { + this.emit("error", error); + } + return false; + } } - disconnect() { - console.log("ChildProcess.prototype.disconnect() - Sorry! Not implemented yet"); + #disconnect() { + if (!this.connected) { + this.emit("error", new TypeError("Process was closed while trying to send message")); + return; + } + this.connected = false; + this.#handle.disconnect(); } kill(sig) { -- cgit v1.2.3