aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/spawned-child.js
blob: 276930503c441e2cea151122d4cd463d4b630b0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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();