-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow complete control over stdio (#17)
* Add results of stdio tests * Add ipc tests * Allow complete control over stdio * Add more debug info to test failure * Rename extension to ps1
- Loading branch information
1 parent
fda2064
commit 9d8b11c
Showing
16 changed files
with
959 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# ipc | ||
|
||
This folder contains a small script for seeing when `ipc` stdio value can be used. | ||
|
||
Conclusions: | ||
|
||
| Function | Result | | ||
| - | - | | ||
| `spawn` | works | | ||
| `spawnSync` | runtime error | | ||
| `fork` | works | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import process from "node:process"; | ||
|
||
console.log("Script started"); | ||
|
||
const tc = (msg, f) => { | ||
try { | ||
f(); | ||
console.log(`${msg}: Success`); | ||
} catch (e) { | ||
console.log(`${msg}: Failure - ${e}`); | ||
} | ||
}; | ||
|
||
process.on("message", (msg) => { | ||
console.log(`Child got parent message: ${msg}`); | ||
}); | ||
|
||
tc("Child -> Parent Message", () => { | ||
process.send("Hello from child"); | ||
}); | ||
|
||
setTimeout(() => { | ||
tc("Child disconnect from parent", () => { | ||
process.disconnect(); | ||
}); | ||
|
||
console.log("Script finished"); | ||
}, 100); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
|
||
Spawn - IPC usage: Success | ||
Parent receives message: Success | ||
Parent -> Child: Success | ||
Script started | ||
Child -> Parent Message: Success | ||
Parent received message: Hello from child | ||
Child got parent message: Hello from Parent | ||
Child disconnect from parent: Success | ||
Script finished | ||
|
||
|
||
SpawnSync - IPC usage: Failure - Error [ERR_IPC_SYNC_FORK]: IPC cannot be used with synchronous forks | ||
|
||
|
||
Fork - IPC usage: Success | ||
Parent receives message: Success | ||
Parent -> Child: Success | ||
Script started | ||
Child -> Parent Message: Success | ||
Parent received message: Hello from child | ||
Child got parent message: Hello from Parent | ||
Child disconnect from parent: Success | ||
Script finished | ||
Finished |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import child_process from "node:child_process"; | ||
|
||
const stdio = [ "ignore", "inherit", "inherit", "ipc"]; | ||
|
||
const tc = (msg, f) => { | ||
try { | ||
f(); | ||
console.log(`${msg}: Success`); | ||
} catch (e) { | ||
console.log(`${msg}: Failure - ${e}`); | ||
} | ||
}; | ||
|
||
const go = (idx) => { | ||
var child; | ||
if (idx === 1) { | ||
tc("\n\nSpawn - IPC usage", () => { | ||
child = child_process.spawn("node", ["child.mjs"], { stdio }); | ||
}); | ||
} else if (idx === 2) { | ||
tc("\n\nSpawnSync - IPC usage", () => { | ||
child = child_process.spawnSync("node", ["child.mjs"], { stdio }); | ||
}); | ||
} else if (idx === 3) { | ||
tc("\n\nFork - IPC usage", () => { | ||
child = child_process.fork("child.mjs", { stdio }); | ||
}); | ||
} | ||
if (!child) { | ||
if (idx !== 4) { | ||
go(idx+1); | ||
} else { | ||
console.log("Finished"); | ||
} | ||
} else { | ||
tc("Parent receives message", () => { | ||
child.on("message", (msg) => { | ||
console.log(`Parent received message: ${msg}`); | ||
}); | ||
}); | ||
child.on("spawn", () => { | ||
tc("Parent -> Child", () => { | ||
child.send("Hello from Parent"); | ||
}); | ||
}); | ||
child.on("exit", () => { | ||
go(idx + 1); | ||
}); | ||
} | ||
}; | ||
|
||
go(1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# stdio | ||
|
||
This folder contains a small script for seeing what is needed to terminate a child process with the different `stdio` options. It also helps verify when a `stream` will have a value and when it will be `null`. | ||
|
||
Conclusions: | ||
|
||
| stdio | Stream | Read | Write | Program terminates when | | ||
| - | - | - | - | - | | ||
| `inherit` | `stdin` | Error | Error | `Ctrl+D` pressed | | ||
| `ignore` | `stdin` | Error | Error | Event Loop finishes | | ||
| `pipe` | `stdin` | Error | Success | `stdin.end()` called | | ||
| `inherit` | `stdout` | Error | Error | Event Loop finishes | | ||
| `ignore` | `stdout` | Error | Error | Event Loop finishes | | ||
| `pipe` | `stdout` | Success | Error | Event Loop finishes | | ||
| `inherit` | `stderr` | Error | Error | Event Loop finishes | | ||
| `ignore` | `stderr` | Error | Error | Event Loop finishes | | ||
| `pipe` | `stderr` | Success | Error | Event Loop finishes | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import process from "node:process"; | ||
|
||
console.log("Script started"); | ||
process.stdin.once("data", (str) => { | ||
console.log(`Got data from stdin: ${str}`); | ||
}); | ||
|
||
const ms = 100; | ||
|
||
setTimeout(() => { | ||
console.log("log 1"); | ||
setTimeout(() => { | ||
console.error("error 1"); | ||
setTimeout(() => { | ||
console.log("log 2"); | ||
setTimeout(() => { | ||
console.error("error 2"); | ||
console.log("Script terminated"); | ||
}, ms); | ||
}, ms); | ||
}, ms); | ||
}, ms); |
Oops, something went wrong.