aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Derrick Farris <mr.dcfarris@gmail.com> 2022-11-09 20:10:29 -0600
committerGravatar GitHub <noreply@github.com> 2022-11-09 18:10:29 -0800
commit7f5022db0ca839262a862f8e2c8012145c4f90f5 (patch)
tree21a63b36a0bfca79830a3a4891af7a38de85cb13
parent9ea025c5430d2ed227d61b5f92cd3d6f46d209bd (diff)
downloadbun-7f5022db0ca839262a862f8e2c8012145c4f90f5.tar.gz
bun-7f5022db0ca839262a862f8e2c8012145c4f90f5.tar.zst
bun-7f5022db0ca839262a862f8e2c8012145c4f90f5.zip
fix(child_process): fix execFileSync options.input (#1479)
* fix(child_process): fix execFileSync options.input * fix(child_process): debug err, check for Uint8Array too * fix(child_process): fix ArrayBufferIsView call * test(child_process): fix missing toString() call on test result * refactor(child_process): change options.input to input to getter calls
-rw-r--r--src/bun.js/child_process.exports.js67
-rw-r--r--test/bun.js/child_process.test.ts8
-rw-r--r--test/bun.js/spawned-child.js12
3 files changed, 49 insertions, 38 deletions
diff --git a/src/bun.js/child_process.exports.js b/src/bun.js/child_process.exports.js
index 5295152dc..49750878c 100644
--- a/src/bun.js/child_process.exports.js
+++ b/src/bun.js/child_process.exports.js
@@ -6,13 +6,11 @@ const {
constants: { signals },
} = import.meta.require("node:os");
+const { ArrayBuffer } = import.meta.primordials;
+
const MAX_BUFFER = 1024 * 1024;
const debug = process.env.DEBUG ? console.log : () => {};
-const platformTmpDir = `${process.platform === "darwin" ? "/private" : ""}${
- process.env.TMPDIR
-}`.slice(0, -1); // remove trailing slash
-
// Sections:
// 1. Exported child_process functions
// 2. child_process helpers
@@ -493,32 +491,24 @@ export function spawnSync(file, args, options) {
// Validate and translate the kill signal, if present.
options.killSignal = sanitizeKillSignal(options.killSignal);
- // options.stdio = getValidStdio(options.stdio || "pipe", true).stdio;
- // if (options.input) {
- // const stdin = (options.stdio[0] = { ...options.stdio[0] });
- // stdin.input = options.input;
- // }
- // // We may want to pass data in on any given fd, ensure it is a valid buffer
- // for (let i = 0; i < options.stdio.length; i++) {
- // const input = options.stdio[i] && options.stdio[i].input;
- // if (input != null) {
- // const pipe = (options.stdio[i] = { ...options.stdio[i] });
- // if (isArrayBufferView(input)) {
- // pipe.input = input;
- // } else if (typeof input === "string") {
- // pipe.input = Buffer.from(input, options.encoding);
- // } else {
- // throw new ERR_INVALID_ARG_TYPE(
- // `options.stdio[${i}]`,
- // ["Buffer", "TypedArray", "DataView", "string"],
- // input
- // );
- // }
- // }
- // }
-
const stdio = options.stdio || "pipe";
- const bunStdio = getBunStdioOptions(stdio);
+ const bunStdio = getBunStdioFromOptions(stdio);
+
+ var { input } = options;
+ if (input) {
+ if (ArrayBufferIsView(input)) {
+ bunStdio[0] = input;
+ } else if (typeof input === "string") {
+ bunStdio[0] = Buffer.from(input, encoding || "utf8");
+ } else {
+ throw new ERR_INVALID_ARG_TYPE(
+ `options.stdio[0]`,
+ ["Buffer", "TypedArray", "DataView", "string"],
+ input,
+ );
+ }
+ }
+
const { stdout, stderr, success, exitCode } = Bun.spawnSync({
cmd: options.args,
env: options.env || undefined,
@@ -577,10 +567,10 @@ export function spawnSync(file, args, options) {
export function execFileSync(file, args, options) {
({ file, args, options } = normalizeExecFileArgs(file, args, options));
- const inheritStderr = !options.stdio;
+ // const inheritStderr = !options.stdio;
const ret = spawnSync(file, args, options);
- if (inheritStderr && ret.stderr) process.stderr.write(ret.stderr);
+ // if (inheritStderr && ret.stderr) process.stderr.write(ret.stderr);
const errArgs = [options.argv0 || file];
ArrayPrototypePush.apply(errArgs, args);
@@ -612,11 +602,11 @@ export function execFileSync(file, args, options) {
*/
export function execSync(command, options) {
const opts = normalizeExecArgs(command, options, null);
- const inheritStderr = !opts.options.stdio;
+ // const inheritStderr = !opts.options.stdio;
const ret = spawnSync(opts.file, opts.options);
- if (inheritStderr && ret.stderr) process.stderr.write(ret.stderr);
+ // if (inheritStderr && ret.stderr) process.stderr.write(ret.stderr); // TODO: Uncomment when we have process.stderr
const err = checkExecSyncError(ret, undefined, command);
@@ -977,7 +967,7 @@ export class ChildProcess extends EventEmitter {
}
const stdio = options.stdio || "pipe";
- const bunStdio = getBunStdioOptions(stdio);
+ const bunStdio = getBunStdioFromOptions(stdio);
const cmd = options.args;
this.#handle = Bun.spawn({
@@ -1138,7 +1128,7 @@ function fdToStdioName(fd) {
}
}
-function getBunStdioOptions(stdio) {
+function getBunStdioFromOptions(stdio) {
const normalizedStdio = normalizeStdio(stdio);
// Node options:
// pipe: just a pipe
@@ -1162,8 +1152,8 @@ function getBunStdioOptions(stdio) {
// ignore -> null
// inherit -> inherit (stdin/stdout/stderr)
// Stream -> throw err for now
-
- return normalizedStdio.map((item) => nodeToBun(item));
+ const bunStdio = normalizedStdio.map((item) => nodeToBun(item));
+ return bunStdio;
}
function normalizeStdio(stdio) {
@@ -1467,6 +1457,9 @@ var ArrayPrototypeSlice = Array.prototype.slice;
var ArrayPrototypeUnshift = Array.prototype.unshift;
var ArrayIsArray = Array.isArray;
+// var ArrayBuffer = ArrayBuffer;
+var ArrayBufferIsView = ArrayBuffer.isView;
+
var NumberIsInteger = Number.isInteger;
var MathAbs = Math.abs;
diff --git a/test/bun.js/child_process.test.ts b/test/bun.js/child_process.test.ts
index fecc5b961..f39629169 100644
--- a/test/bun.js/child_process.test.ts
+++ b/test/bun.js/child_process.test.ts
@@ -259,6 +259,14 @@ describe("execFileSync()", () => {
const result = execFileSync("bun", ["-v"], { encoding: "utf8" });
expect(SEMVER_REGEX.test(result.trim())).toBe(true);
});
+
+ it("should allow us to pass input to the command", () => {
+ const result = execFileSync("node", ["spawned-child.js", "STDIN"], {
+ input: "hello world!",
+ encoding: "utf8",
+ });
+ expect(result.trim()).toBe("hello world!");
+ });
});
describe("execSync()", () => {
diff --git a/test/bun.js/spawned-child.js b/test/bun.js/spawned-child.js
index 757aacd5c..c70aeab16 100644
--- a/test/bun.js/spawned-child.js
+++ b/test/bun.js/spawned-child.js
@@ -1 +1,11 @@
-setTimeout(() => console.log("hello"), 150);
+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);
+}