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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
import {
FileSink,
NullSubprocess,
PipedSubprocess,
ReadableSubprocess,
SyncSubprocess,
WritableSubprocess,
} from "bun";
import * as tsd from "tsd";
Bun.spawn(["echo", "hello"]);
{
const proc = Bun.spawn(["echo", "hello"], {
cwd: "./path/to/subdir", // specify a working direcory
env: { ...process.env, FOO: "bar" }, // specify environment variables
onExit(proc, exitCode, signalCode, error) {
// exit handler
},
});
proc.pid; // process ID of subprocess
tsd.expectType<ReadableStream<Uint8Array>>(proc.stdout);
tsd.expectType<undefined>(proc.stderr);
tsd.expectType<undefined>(proc.stdin);
}
{
const proc = Bun.spawn(["cat"], {
stdin: await fetch(
"https://raw.githubusercontent.com/oven-sh/bun/main/examples/hashing.js",
),
});
const text = await new Response(proc.stdout).text();
console.log(text); // "const input = "hello world".repeat(400); ..."
}
{
const proc = Bun.spawn(["cat"], {
stdin: "pipe", // return a FileSink for writing
});
// enqueue string data
proc.stdin.write("hello");
// enqueue binary data
const enc = new TextEncoder();
proc.stdin.write(enc.encode(" world!"));
// send buffered data
proc.stdin.flush();
// close the input stream
proc.stdin.end();
}
{
const proc = Bun.spawn(["echo", "hello"]);
const text = await new Response(proc.stdout).text();
console.log(text); // => "hello"
}
{
const proc = Bun.spawn(["echo", "hello"], {
onExit(proc, exitCode, signalCode, error) {
// exit handler
},
});
await proc.exited; // resolves when process exit
proc.killed; // boolean — was the process killed?
proc.exitCode; // null | number
proc.signalCode; // null | "SIGABRT" | "SIGALRM" | ...
proc.kill();
proc.killed; // true
proc.kill(); // specify an exit code
proc.unref();
}
{
const proc = Bun.spawn(["echo", "hello"], {
stdio: ["pipe", "pipe", "pipe"],
});
tsd.expectType<FileSink>(proc.stdin);
tsd.expectType<ReadableStream<Uint8Array>>(proc.stdout);
tsd.expectType<ReadableStream<Uint8Array>>(proc.stderr);
}
{
const proc = Bun.spawn(["echo", "hello"], {
stdio: ["inherit", "inherit", "inherit"],
});
tsd.expectType<undefined>(proc.stdin);
tsd.expectType<undefined>(proc.stdout);
tsd.expectType<undefined>(proc.stderr);
}
{
const proc = Bun.spawn(["echo", "hello"], {
stdio: ["ignore", "ignore", "ignore"],
});
tsd.expectType<undefined>(proc.stdin);
tsd.expectType<undefined>(proc.stdout);
tsd.expectType<undefined>(proc.stderr);
}
{
const proc = Bun.spawn(["echo", "hello"], {
stdio: [null, null, null],
});
tsd.expectType<undefined>(proc.stdin);
tsd.expectType<undefined>(proc.stdout);
tsd.expectType<undefined>(proc.stderr);
}
{
const proc = Bun.spawn(["echo", "hello"], {
stdio: [new Request("1"), null, null],
});
tsd.expectType<number>(proc.stdin);
}
{
const proc = Bun.spawn(["echo", "hello"], {
stdio: [new Response("1"), null, null],
});
tsd.expectType<number>(proc.stdin);
}
{
const proc = Bun.spawn(["echo", "hello"], {
stdio: [new Uint8Array([]), null, null],
});
tsd.expectType<number>(proc.stdin);
}
tsd.expectAssignable<PipedSubprocess>(
Bun.spawn([], { stdio: ["pipe", "pipe", "pipe"] }),
);
tsd.expectNotAssignable<PipedSubprocess>(
Bun.spawn([], { stdio: ["inherit", "inherit", "inherit"] }),
);
tsd.expectAssignable<ReadableSubprocess>(
Bun.spawn([], { stdio: ["ignore", "pipe", "pipe"] }),
);
tsd.expectAssignable<ReadableSubprocess>(
Bun.spawn([], { stdio: ["pipe", "pipe", "pipe"] }),
);
tsd.expectNotAssignable<ReadableSubprocess>(
Bun.spawn([], { stdio: ["pipe", "ignore", "pipe"] }),
);
tsd.expectAssignable<WritableSubprocess>(
Bun.spawn([], { stdio: ["pipe", "pipe", "pipe"] }),
);
tsd.expectAssignable<WritableSubprocess>(
Bun.spawn([], { stdio: ["pipe", "ignore", "inherit"] }),
);
tsd.expectNotAssignable<WritableSubprocess>(
Bun.spawn([], { stdio: ["ignore", "pipe", "pipe"] }),
);
tsd.expectAssignable<NullSubprocess>(
Bun.spawn([], { stdio: ["ignore", "inherit", "ignore"] }),
);
tsd.expectAssignable<NullSubprocess>(
Bun.spawn([], { stdio: [null, null, null] }),
);
tsd.expectNotAssignable<ReadableSubprocess>(Bun.spawn([], {}));
tsd.expectNotAssignable<PipedSubprocess>(Bun.spawn([], {}));
tsd.expectAssignable<SyncSubprocess>(Bun.spawnSync([], {}));
tsd.expectAssignable<SyncSubprocess>(Bun.spawnSync([], {}));
|