diff options
author | 2022-12-02 10:25:13 -0600 | |
---|---|---|
committer | 2022-12-02 08:25:13 -0800 | |
commit | b8586b33dad8ab66ebd5d1aa4d5a0df305266e7f (patch) | |
tree | 19c31806b3ae22d7c0f0f127fd3cb1b640f4bd59 /test/bun.js/spawned-child.js | |
parent | beaf91590acd319e4ac15c14c64c59f45b6a794b (diff) | |
download | bun-b8586b33dad8ab66ebd5d1aa4d5a0df305266e7f.tar.gz bun-b8586b33dad8ab66ebd5d1aa4d5a0df305266e7f.tar.zst bun-b8586b33dad8ab66ebd5d1aa4d5a0df305266e7f.zip |
feat(process): add process.{stdin, stdout, stderr} support (#1495)
* fix(stream): get Duplex working
* feat(process): add stdin,stdout,stderr in a semi-broken state (pipes??)
* test(NodeTestHelpers): fix test names
* test(NodeTestHelpers): add test for createDoneDotAll done called w error
* test(NodeTestHelpers): remove stray console.log
* fix(stream): fix bug in Duplex, Readable
* test(process.stdio): rename test
* fix(process.stdio): change onData listener to onReadable
* refactor(streams): add file-wide debug fn, destructure opts
* fix(child_process): check isCallable on promise
* fix: get stdio streams mostly working (mostly)
* fix(child_process): wait until stream is drained before calling end?
* fix(child_process): change to result?.then
* debug(child_process,streams): add EE id tracking, add shim for stdio after handle is dead
* test(child_process): fix double pipe test, temp fix for ChildProcess.kill() return val
* fix(child_process): remove immediate emit of exit on kill
* debug(streams): add more debug log
* debug(streams): add more debug logs part 2
* feat(streams,fs): add NativeWritable, adapt fs.WriteStream and fs.ReadStream to native
Diffstat (limited to '')
-rw-r--r-- | test/bun.js/spawned-child.js | 64 |
1 files changed, 54 insertions, 10 deletions
diff --git a/test/bun.js/spawned-child.js b/test/bun.js/spawned-child.js index c70aeab16..276930503 100644 --- a/test/bun.js/spawned-child.js +++ b/test/bun.js/spawned-child.js @@ -1,11 +1,55 @@ -if (process.argv[2] === "STDIN") { - let result = ""; - process.stdin.on("data", (data) => { - result += data; - }); - process.stdin.on("close", () => { - console.log(result); - }); -} else { - setTimeout(() => console.log("hello"), 150); +if (globalThis.Bun) { + const nodeStream = require("node:stream"); + const nodeFs = require("node:fs"); + + // TODO: Remove this polyfill once we have integrated polyfill into runtime init + const { + stdin: _stdinInit, + stdout: _stdoutInit, + stderr: _stderrInit, + } = require("../../src/bun.js/process-stdio-polyfill.js"); + + function _require(mod) { + if (mod === "node:stream") return nodeStream; + if (mod === "node:fs") return nodeFs; + throw new Error(`Unknown module: ${mod}`); + } + + process.stdin = _stdinInit({ require: _require }); + process.stdout = _stdoutInit({ require: _require }); + process.stderr = _stderrInit({ require: _require }); } + +const TARGET = process.argv[2]; +const MODE = process.argv[3]; + +async function main() { + if (TARGET === "STDIN") { + let data = ""; + process.stdin.setEncoding("utf8"); + if (MODE === "READABLE") { + process.stdin.on("readable", () => { + let chunk; + while ((chunk = process.stdin.read()) !== null) { + data += chunk; + } + }); + } else { + process.stdin.on("data", (chunk) => { + data += chunk; + }); + } + process.stdin.on("end", () => { + console.log("data:", data); + process.exit(0); + }); + } else if (TARGET === "STDOUT") { + process.stdout.write("stdout_test"); + } else if (TARGET === "TIMER") { + setTimeout(() => console.log("hello"), 150); + } else { + console.log("unknown target! you messed up..."); + } +} + +main(); |