aboutsummaryrefslogtreecommitdiff
path: root/src/js/node/child_process.js
diff options
context:
space:
mode:
authorGravatar dave caruso <me@paperdave.net> 2023-09-07 04:58:44 -0700
committerGravatar GitHub <noreply@github.com> 2023-09-07 04:58:44 -0700
commit57a06745a48093c25d0f4729ccea41a918d6427d (patch)
treeac2568d5c98918d6364d2a9667c164cd3f3b3867 /src/js/node/child_process.js
parent4360ec83b4146e15344b304573795f084f86a7c2 (diff)
downloadbun-57a06745a48093c25d0f4729ccea41a918d6427d.tar.gz
bun-57a06745a48093c25d0f4729ccea41a918d6427d.tar.zst
bun-57a06745a48093c25d0f4729ccea41a918d6427d.zip
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 <dylan.conway567@gmail.com>
Diffstat (limited to 'src/js/node/child_process.js')
-rw-r--r--src/js/node/child_process.js89
1 files changed, 53 insertions, 36 deletions
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) {