bun-types 1.3.2-canary.20251106T140813 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bun.d.ts +102 -6
- package/docs/bundler/bytecode.mdx +465 -0
- package/docs/bundler/css.mdx +1024 -0
- package/docs/bundler/esbuild.mdx +253 -0
- package/docs/bundler/executables.mdx +535 -0
- package/docs/bundler/fullstack.mdx +1064 -0
- package/docs/bundler/hot-reloading.mdx +229 -0
- package/docs/bundler/html-static.mdx +386 -0
- package/docs/bundler/index.mdx +1499 -0
- package/docs/bundler/loaders.mdx +356 -0
- package/docs/bundler/macros.mdx +328 -0
- package/docs/bundler/minifier.mdx +1306 -0
- package/docs/bundler/plugins.mdx +411 -0
- package/docs/feedback.mdx +85 -0
- package/docs/guides/binary/arraybuffer-to-array.mdx +29 -0
- package/docs/guides/binary/arraybuffer-to-blob.mdx +26 -0
- package/docs/guides/binary/arraybuffer-to-buffer.mdx +27 -0
- package/docs/guides/binary/arraybuffer-to-string.mdx +17 -0
- package/docs/guides/binary/arraybuffer-to-typedarray.mdx +41 -0
- package/docs/guides/binary/blob-to-arraybuffer.mdx +16 -0
- package/docs/guides/binary/blob-to-dataview.mdx +16 -0
- package/docs/guides/binary/blob-to-stream.mdx +16 -0
- package/docs/guides/binary/blob-to-string.mdx +17 -0
- package/docs/guides/binary/blob-to-typedarray.mdx +16 -0
- package/docs/guides/binary/buffer-to-arraybuffer.mdx +16 -0
- package/docs/guides/binary/buffer-to-blob.mdx +16 -0
- package/docs/guides/binary/buffer-to-readablestream.mdx +43 -0
- package/docs/guides/binary/buffer-to-string.mdx +27 -0
- package/docs/guides/binary/buffer-to-typedarray.mdx +16 -0
- package/docs/guides/binary/dataview-to-string.mdx +17 -0
- package/docs/guides/binary/typedarray-to-arraybuffer.mdx +27 -0
- package/docs/guides/binary/typedarray-to-blob.mdx +18 -0
- package/docs/guides/binary/typedarray-to-buffer.mdx +16 -0
- package/docs/guides/binary/typedarray-to-dataview.mdx +16 -0
- package/docs/guides/binary/typedarray-to-readablestream.mdx +43 -0
- package/docs/guides/binary/typedarray-to-string.mdx +18 -0
- package/docs/guides/deployment/aws-lambda.mdx +204 -0
- package/docs/guides/deployment/digital-ocean.mdx +161 -0
- package/docs/guides/deployment/google-cloud-run.mdx +197 -0
- package/docs/guides/deployment/railway.mdx +145 -0
- package/docs/guides/deployment/render.mdx +82 -0
- package/docs/guides/deployment/vercel.mdx +99 -0
- package/docs/guides/ecosystem/astro.mdx +82 -0
- package/docs/guides/ecosystem/discordjs.mdx +80 -0
- package/docs/guides/ecosystem/docker.mdx +151 -0
- package/docs/guides/ecosystem/drizzle.mdx +195 -0
- package/docs/guides/ecosystem/edgedb.mdx +257 -0
- package/docs/guides/ecosystem/elysia.mdx +31 -0
- package/docs/guides/ecosystem/express.mdx +43 -0
- package/docs/guides/ecosystem/hono.mdx +47 -0
- package/docs/guides/ecosystem/mongoose.mdx +92 -0
- package/docs/guides/ecosystem/neon-drizzle.mdx +234 -0
- package/docs/guides/ecosystem/neon-serverless-postgres.mdx +60 -0
- package/docs/guides/ecosystem/nextjs.mdx +57 -0
- package/docs/guides/ecosystem/nuxt.mdx +90 -0
- package/docs/guides/ecosystem/pm2.mdx +55 -0
- package/docs/guides/ecosystem/prisma-postgres.mdx +169 -0
- package/docs/guides/ecosystem/prisma.mdx +164 -0
- package/docs/guides/ecosystem/qwik.mdx +114 -0
- package/docs/guides/ecosystem/react.mdx +52 -0
- package/docs/guides/ecosystem/remix.mdx +97 -0
- package/docs/guides/ecosystem/sentry.mdx +54 -0
- package/docs/guides/ecosystem/solidstart.mdx +66 -0
- package/docs/guides/ecosystem/ssr-react.mdx +49 -0
- package/docs/guides/ecosystem/stric.mdx +54 -0
- package/docs/guides/ecosystem/sveltekit.mdx +138 -0
- package/docs/guides/ecosystem/systemd.mdx +114 -0
- package/docs/guides/ecosystem/upstash.mdx +87 -0
- package/docs/guides/ecosystem/vite.mdx +77 -0
- package/docs/guides/html-rewriter/extract-links.mdx +72 -0
- package/docs/guides/html-rewriter/extract-social-meta.mdx +97 -0
- package/docs/guides/http/cluster.mdx +69 -0
- package/docs/guides/http/fetch-unix.mdx +35 -0
- package/docs/guides/http/fetch.mdx +26 -0
- package/docs/guides/http/file-uploads.mdx +97 -0
- package/docs/guides/http/hot.mdx +28 -0
- package/docs/guides/http/proxy.mdx +26 -0
- package/docs/guides/http/server.mdx +48 -0
- package/docs/guides/http/simple.mdx +20 -0
- package/docs/guides/http/stream-file.mdx +50 -0
- package/docs/guides/http/stream-iterator.mdx +49 -0
- package/docs/guides/http/stream-node-streams-in-bun.mdx +22 -0
- package/docs/guides/http/tls.mdx +32 -0
- package/docs/guides/index.mdx +10 -0
- package/docs/guides/install/add-dev.mdx +28 -0
- package/docs/guides/install/add-git.mdx +38 -0
- package/docs/guides/install/add-optional.mdx +27 -0
- package/docs/guides/install/add-peer.mdx +45 -0
- package/docs/guides/install/add-tarball.mdx +35 -0
- package/docs/guides/install/add.mdx +44 -0
- package/docs/guides/install/azure-artifacts.mdx +76 -0
- package/docs/guides/install/cicd.mdx +43 -0
- package/docs/guides/install/custom-registry.mdx +32 -0
- package/docs/guides/install/from-npm-install-to-bun-install.mdx +230 -0
- package/docs/guides/install/git-diff-bun-lockfile.mdx +47 -0
- package/docs/guides/install/jfrog-artifactory.mdx +28 -0
- package/docs/guides/install/npm-alias.mdx +25 -0
- package/docs/guides/install/registry-scope.mdx +40 -0
- package/docs/guides/install/trusted.mdx +50 -0
- package/docs/guides/install/workspaces.mdx +70 -0
- package/docs/guides/install/yarnlock.mdx +50 -0
- package/docs/guides/process/argv.mdx +66 -0
- package/docs/guides/process/ctrl-c.mdx +18 -0
- package/docs/guides/process/ipc.mdx +69 -0
- package/docs/guides/process/nanoseconds.mdx +15 -0
- package/docs/guides/process/os-signals.mdx +41 -0
- package/docs/guides/process/spawn-stderr.mdx +34 -0
- package/docs/guides/process/spawn-stdout.mdx +28 -0
- package/docs/guides/process/spawn.mdx +43 -0
- package/docs/guides/process/stdin.mdx +62 -0
- package/docs/guides/read-file/arraybuffer.mdx +30 -0
- package/docs/guides/read-file/buffer.mdx +21 -0
- package/docs/guides/read-file/exists.mdx +18 -0
- package/docs/guides/read-file/json.mdx +19 -0
- package/docs/guides/read-file/mime.mdx +22 -0
- package/docs/guides/read-file/stream.mdx +28 -0
- package/docs/guides/read-file/string.mdx +24 -0
- package/docs/guides/read-file/uint8array.mdx +23 -0
- package/docs/guides/read-file/watch.mdx +66 -0
- package/docs/guides/runtime/build-time-constants.mdx +295 -0
- package/docs/guides/runtime/cicd.mdx +45 -0
- package/docs/guides/runtime/codesign-macos-executable.mdx +61 -0
- package/docs/guides/runtime/define-constant.mdx +149 -0
- package/docs/guides/runtime/delete-directory.mdx +39 -0
- package/docs/guides/runtime/delete-file.mdx +21 -0
- package/docs/guides/runtime/heap-snapshot.mdx +28 -0
- package/docs/guides/runtime/import-html.mdx +17 -0
- package/docs/guides/runtime/import-json.mdx +46 -0
- package/docs/guides/runtime/import-toml.mdx +32 -0
- package/docs/guides/runtime/import-yaml.mdx +104 -0
- package/docs/guides/runtime/read-env.mdx +37 -0
- package/docs/guides/runtime/set-env.mdx +51 -0
- package/docs/guides/runtime/shell.mdx +42 -0
- package/docs/guides/runtime/timezone.mdx +38 -0
- package/docs/guides/runtime/tsconfig-paths.mdx +31 -0
- package/docs/guides/runtime/typescript.mdx +51 -0
- package/docs/guides/runtime/vscode-debugger.mdx +48 -0
- package/docs/guides/runtime/web-debugger.mdx +103 -0
- package/docs/guides/streams/node-readable-to-arraybuffer.mdx +13 -0
- package/docs/guides/streams/node-readable-to-blob.mdx +13 -0
- package/docs/guides/streams/node-readable-to-json.mdx +14 -0
- package/docs/guides/streams/node-readable-to-string.mdx +14 -0
- package/docs/guides/streams/node-readable-to-uint8array.mdx +13 -0
- package/docs/guides/streams/to-array.mdx +16 -0
- package/docs/guides/streams/to-arraybuffer.mdx +16 -0
- package/docs/guides/streams/to-blob.mdx +16 -0
- package/docs/guides/streams/to-buffer.mdx +17 -0
- package/docs/guides/streams/to-json.mdx +16 -0
- package/docs/guides/streams/to-string.mdx +16 -0
- package/docs/guides/streams/to-typedarray.mdx +24 -0
- package/docs/guides/test/bail.mdx +24 -0
- package/docs/guides/test/coverage-threshold.mdx +67 -0
- package/docs/guides/test/coverage.mdx +49 -0
- package/docs/guides/test/happy-dom.mdx +73 -0
- package/docs/guides/test/migrate-from-jest.mdx +125 -0
- package/docs/guides/test/mock-clock.mdx +50 -0
- package/docs/guides/test/mock-functions.mdx +70 -0
- package/docs/guides/test/rerun-each.mdx +16 -0
- package/docs/guides/test/run-tests.mdx +116 -0
- package/docs/guides/test/skip-tests.mdx +43 -0
- package/docs/guides/test/snapshot.mdx +102 -0
- package/docs/guides/test/spy-on.mdx +49 -0
- package/docs/guides/test/svelte-test.mdx +113 -0
- package/docs/guides/test/testing-library.mdx +93 -0
- package/docs/guides/test/timeout.mdx +17 -0
- package/docs/guides/test/todo-tests.mdx +74 -0
- package/docs/guides/test/update-snapshots.mdx +49 -0
- package/docs/guides/test/watch-mode.mdx +24 -0
- package/docs/guides/util/base64.mdx +17 -0
- package/docs/guides/util/deep-equals.mdx +41 -0
- package/docs/guides/util/deflate.mdx +20 -0
- package/docs/guides/util/detect-bun.mdx +25 -0
- package/docs/guides/util/entrypoint.mdx +19 -0
- package/docs/guides/util/escape-html.mdx +24 -0
- package/docs/guides/util/file-url-to-path.mdx +16 -0
- package/docs/guides/util/gzip.mdx +20 -0
- package/docs/guides/util/hash-a-password.mdx +56 -0
- package/docs/guides/util/import-meta-dir.mdx +15 -0
- package/docs/guides/util/import-meta-file.mdx +15 -0
- package/docs/guides/util/import-meta-path.mdx +15 -0
- package/docs/guides/util/javascript-uuid.mdx +25 -0
- package/docs/guides/util/main.mdx +43 -0
- package/docs/guides/util/path-to-file-url.mdx +16 -0
- package/docs/guides/util/sleep.mdx +24 -0
- package/docs/guides/util/version.mdx +23 -0
- package/docs/guides/util/which-path-to-executable-bin.mdx +17 -0
- package/docs/guides/websocket/compression.mdx +33 -0
- package/docs/guides/websocket/context.mdx +74 -0
- package/docs/guides/websocket/pubsub.mdx +40 -0
- package/docs/guides/websocket/simple.mdx +35 -0
- package/docs/guides/write-file/append.mdx +54 -0
- package/docs/guides/write-file/basic.mdx +46 -0
- package/docs/guides/write-file/blob.mdx +30 -0
- package/docs/guides/write-file/cat.mdx +19 -0
- package/docs/guides/write-file/file-cp.mdx +18 -0
- package/docs/guides/write-file/filesink.mdx +54 -0
- package/docs/guides/write-file/response.mdx +19 -0
- package/docs/guides/write-file/stdout.mdx +23 -0
- package/docs/guides/write-file/stream.mdx +19 -0
- package/docs/guides/write-file/unlink.mdx +18 -0
- package/docs/index.mdx +133 -0
- package/docs/installation.mdx +365 -0
- package/docs/pm/bunx.mdx +83 -0
- package/docs/pm/catalogs.mdx +292 -0
- package/docs/pm/cli/add.mdx +179 -0
- package/docs/pm/cli/audit.mdx +60 -0
- package/docs/pm/cli/install.mdx +471 -0
- package/docs/pm/cli/link.mdx +48 -0
- package/docs/pm/cli/outdated.mdx +197 -0
- package/docs/pm/cli/patch.mdx +69 -0
- package/docs/pm/cli/pm.mdx +319 -0
- package/docs/pm/cli/publish.mdx +123 -0
- package/docs/pm/cli/remove.mdx +16 -0
- package/docs/pm/cli/update.mdx +140 -0
- package/docs/pm/cli/why.mdx +84 -0
- package/docs/pm/filter.mdx +102 -0
- package/docs/pm/global-cache.mdx +72 -0
- package/docs/pm/isolated-installs.mdx +205 -0
- package/docs/pm/lifecycle.mdx +57 -0
- package/docs/pm/lockfile.mdx +64 -0
- package/docs/pm/npmrc.mdx +111 -0
- package/docs/pm/overrides.mdx +83 -0
- package/docs/pm/scopes-registries.mdx +35 -0
- package/docs/pm/security-scanner-api.mdx +95 -0
- package/docs/pm/workspaces.mdx +109 -0
- package/docs/project/benchmarking.mdx +218 -0
- package/docs/project/bindgen.mdx +223 -0
- package/docs/project/building-windows.mdx +133 -0
- package/docs/project/contributing.mdx +343 -0
- package/docs/project/feedback.mdx +20 -0
- package/docs/project/license.mdx +78 -0
- package/docs/project/roadmap.mdx +8 -0
- package/docs/quickstart.mdx +240 -0
- package/docs/runtime/auto-install.mdx +97 -0
- package/docs/runtime/binary-data.mdx +846 -0
- package/docs/runtime/bun-apis.mdx +59 -0
- package/docs/runtime/bunfig.mdx +642 -0
- package/docs/runtime/c-compiler.mdx +204 -0
- package/docs/runtime/child-process.mdx +532 -0
- package/docs/runtime/color.mdx +267 -0
- package/docs/runtime/console.mdx +67 -0
- package/docs/runtime/cookies.mdx +454 -0
- package/docs/runtime/debugger.mdx +335 -0
- package/docs/runtime/environment-variables.mdx +214 -0
- package/docs/runtime/ffi.mdx +565 -0
- package/docs/runtime/file-io.mdx +306 -0
- package/docs/runtime/file-system-router.mdx +118 -0
- package/docs/runtime/file-types.mdx +354 -0
- package/docs/runtime/glob.mdx +181 -0
- package/docs/runtime/globals.mdx +72 -0
- package/docs/runtime/hashing.mdx +315 -0
- package/docs/runtime/html-rewriter.mdx +340 -0
- package/docs/runtime/http/cookies.mdx +79 -0
- package/docs/runtime/http/error-handling.mdx +40 -0
- package/docs/runtime/http/metrics.mdx +36 -0
- package/docs/runtime/http/routing.mdx +289 -0
- package/docs/runtime/http/server.mdx +647 -0
- package/docs/runtime/http/tls.mdx +101 -0
- package/docs/runtime/http/websockets.mdx +404 -0
- package/docs/runtime/index.mdx +223 -0
- package/docs/runtime/jsx.mdx +115 -0
- package/docs/runtime/module-resolution.mdx +342 -0
- package/docs/runtime/networking/dns.mdx +111 -0
- package/docs/runtime/networking/fetch.mdx +468 -0
- package/docs/runtime/networking/tcp.mdx +239 -0
- package/docs/runtime/networking/udp.mdx +129 -0
- package/docs/runtime/node-api.mdx +19 -0
- package/docs/runtime/nodejs-compat.mdx +468 -0
- package/docs/runtime/plugins.mdx +405 -0
- package/docs/runtime/redis.mdx +582 -0
- package/docs/runtime/s3.mdx +863 -0
- package/docs/runtime/secrets.mdx +336 -0
- package/docs/runtime/semver.mdx +57 -0
- package/docs/runtime/shell.mdx +637 -0
- package/docs/runtime/sql.mdx +1404 -0
- package/docs/runtime/sqlite.mdx +699 -0
- package/docs/runtime/streams.mdx +232 -0
- package/docs/runtime/templating/create.mdx +269 -0
- package/docs/runtime/templating/init.mdx +58 -0
- package/docs/runtime/transpiler.mdx +288 -0
- package/docs/runtime/typescript.mdx +58 -0
- package/docs/runtime/utils.mdx +922 -0
- package/docs/runtime/watch-mode.mdx +161 -0
- package/docs/runtime/web-apis.mdx +29 -0
- package/docs/runtime/workers.mdx +328 -0
- package/docs/runtime/yaml.mdx +469 -0
- package/docs/snippets/cli/add.mdx +166 -0
- package/docs/snippets/cli/build.mdx +196 -0
- package/docs/snippets/cli/feedback.mdx +17 -0
- package/docs/snippets/cli/init.mdx +84 -0
- package/docs/snippets/cli/install.mdx +173 -0
- package/docs/snippets/cli/link.mdx +163 -0
- package/docs/snippets/cli/outdated.mdx +140 -0
- package/docs/snippets/cli/patch.mdx +171 -0
- package/docs/snippets/cli/publish.mdx +198 -0
- package/docs/snippets/cli/remove.mdx +146 -0
- package/docs/snippets/cli/run.mdx +293 -0
- package/docs/snippets/cli/test.mdx +100 -0
- package/docs/snippets/cli/update.mdx +144 -0
- package/docs/snippets/product-card.mdx +32 -0
- package/docs/snippets/product-tiles.mdx +94 -0
- package/docs/test/code-coverage.mdx +409 -0
- package/docs/test/configuration.mdx +467 -0
- package/docs/test/dates-times.mdx +129 -0
- package/docs/test/discovery.mdx +90 -0
- package/docs/test/dom.mdx +226 -0
- package/docs/test/index.mdx +380 -0
- package/docs/test/lifecycle.mdx +348 -0
- package/docs/test/mocks.mdx +637 -0
- package/docs/test/reporters.mdx +117 -0
- package/docs/test/runtime-behavior.mdx +342 -0
- package/docs/test/snapshots.mdx +434 -0
- package/docs/test/writing-tests.mdx +635 -0
- package/docs/typescript.mdx +54 -0
- package/package.json +8 -6
- package/test.d.ts +2 -2
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Spawn
|
|
3
|
+
description: Spawn child processes with `Bun.spawn` or `Bun.spawnSync`
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## Spawn a process (`Bun.spawn()`)
|
|
7
|
+
|
|
8
|
+
Provide a command as an array of strings. The result of `Bun.spawn()` is a `Bun.Subprocess` object.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
const proc = Bun.spawn(["bun", "--version"]);
|
|
12
|
+
console.log(await proc.exited); // 0
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The second argument to `Bun.spawn` is a parameters object that can be used to configure the subprocess.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
const proc = Bun.spawn(["bun", "--version"], {
|
|
19
|
+
cwd: "./path/to/subdir", // specify a working directory
|
|
20
|
+
env: { ...process.env, FOO: "bar" }, // specify environment variables
|
|
21
|
+
onExit(proc, exitCode, signalCode, error) {
|
|
22
|
+
// exit handler
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
proc.pid; // process ID of subprocess
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Input stream
|
|
30
|
+
|
|
31
|
+
By default, the input stream of the subprocess is undefined; it can be configured with the `stdin` parameter.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
const proc = Bun.spawn(["cat"], {
|
|
35
|
+
stdin: await fetch("https://raw.githubusercontent.com/oven-sh/bun/main/examples/hashing.js"),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const text = await proc.stdout.text();
|
|
39
|
+
console.log(text); // "const input = "hello world".repeat(400); ..."
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
| Value | Description |
|
|
43
|
+
| ------------------------ | ------------------------------------------------ |
|
|
44
|
+
| `null` | **Default.** Provide no input to the subprocess |
|
|
45
|
+
| `"pipe"` | Return a `FileSink` for fast incremental writing |
|
|
46
|
+
| `"inherit"` | Inherit the `stdin` of the parent process |
|
|
47
|
+
| `Bun.file()` | Read from the specified file |
|
|
48
|
+
| `TypedArray \| DataView` | Use a binary buffer as input |
|
|
49
|
+
| `Response` | Use the response `body` as input |
|
|
50
|
+
| `Request` | Use the request `body` as input |
|
|
51
|
+
| `ReadableStream` | Use a readable stream as input |
|
|
52
|
+
| `Blob` | Use a blob as input |
|
|
53
|
+
| `number` | Read from the file with a given file descriptor |
|
|
54
|
+
|
|
55
|
+
The `"pipe"` option lets incrementally write to the subprocess's input stream from the parent process.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
const proc = Bun.spawn(["cat"], {
|
|
59
|
+
stdin: "pipe", // return a FileSink for writing
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// enqueue string data
|
|
63
|
+
proc.stdin.write("hello");
|
|
64
|
+
|
|
65
|
+
// enqueue binary data
|
|
66
|
+
const enc = new TextEncoder();
|
|
67
|
+
proc.stdin.write(enc.encode(" world!"));
|
|
68
|
+
|
|
69
|
+
// send buffered data
|
|
70
|
+
proc.stdin.flush();
|
|
71
|
+
|
|
72
|
+
// close the input stream
|
|
73
|
+
proc.stdin.end();
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Passing a `ReadableStream` to `stdin` lets you pipe data from a JavaScript `ReadableStream` directly to the subprocess's input:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const stream = new ReadableStream({
|
|
80
|
+
start(controller) {
|
|
81
|
+
controller.enqueue("Hello from ");
|
|
82
|
+
controller.enqueue("ReadableStream!");
|
|
83
|
+
controller.close();
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const proc = Bun.spawn(["cat"], {
|
|
88
|
+
stdin: stream,
|
|
89
|
+
stdout: "pipe",
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const output = await proc.stdout.text();
|
|
93
|
+
console.log(output); // "Hello from ReadableStream!"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Output streams
|
|
97
|
+
|
|
98
|
+
You can read results from the subprocess via the `stdout` and `stderr` properties. By default these are instances of `ReadableStream`.
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
const proc = Bun.spawn(["bun", "--version"]);
|
|
102
|
+
const text = await proc.stdout.text();
|
|
103
|
+
console.log(text); // => "1.3.1\n"
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Configure the output stream by passing one of the following values to `stdout/stderr`:
|
|
107
|
+
|
|
108
|
+
| Value | Description |
|
|
109
|
+
| ------------ | --------------------------------------------------------------------------------------------------- |
|
|
110
|
+
| `"pipe"` | **Default for `stdout`.** Pipe the output to a `ReadableStream` on the returned `Subprocess` object |
|
|
111
|
+
| `"inherit"` | **Default for `stderr`.** Inherit from the parent process |
|
|
112
|
+
| `"ignore"` | Discard the output |
|
|
113
|
+
| `Bun.file()` | Write to the specified file |
|
|
114
|
+
| `number` | Write to the file with the given file descriptor |
|
|
115
|
+
|
|
116
|
+
## Exit handling
|
|
117
|
+
|
|
118
|
+
Use the `onExit` callback to listen for the process exiting or being killed.
|
|
119
|
+
|
|
120
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
121
|
+
const proc = Bun.spawn(["bun", "--version"], {
|
|
122
|
+
onExit(proc, exitCode, signalCode, error) {
|
|
123
|
+
// exit handler
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
For convenience, the `exited` property is a `Promise` that resolves when the process exits.
|
|
129
|
+
|
|
130
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
131
|
+
const proc = Bun.spawn(["bun", "--version"]);
|
|
132
|
+
|
|
133
|
+
await proc.exited; // resolves when process exit
|
|
134
|
+
proc.killed; // boolean — was the process killed?
|
|
135
|
+
proc.exitCode; // null | number
|
|
136
|
+
proc.signalCode; // null | "SIGABRT" | "SIGALRM" | ...
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
To kill a process:
|
|
140
|
+
|
|
141
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
142
|
+
const proc = Bun.spawn(["bun", "--version"]);
|
|
143
|
+
proc.kill();
|
|
144
|
+
proc.killed; // true
|
|
145
|
+
|
|
146
|
+
proc.kill(15); // specify a signal code
|
|
147
|
+
proc.kill("SIGTERM"); // specify a signal name
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
The parent `bun` process will not terminate until all child processes have exited. Use `proc.unref()` to detach the child process from the parent.
|
|
151
|
+
|
|
152
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
153
|
+
const proc = Bun.spawn(["bun", "--version"]);
|
|
154
|
+
proc.unref();
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Resource usage
|
|
158
|
+
|
|
159
|
+
You can get information about the process's resource usage after it has exited:
|
|
160
|
+
|
|
161
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
162
|
+
const proc = Bun.spawn(["bun", "--version"]);
|
|
163
|
+
await proc.exited;
|
|
164
|
+
|
|
165
|
+
const usage = proc.resourceUsage();
|
|
166
|
+
console.log(`Max memory used: ${usage.maxRSS} bytes`);
|
|
167
|
+
console.log(`CPU time (user): ${usage.cpuTime.user} µs`);
|
|
168
|
+
console.log(`CPU time (system): ${usage.cpuTime.system} µs`);
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Using AbortSignal
|
|
172
|
+
|
|
173
|
+
You can abort a subprocess using an `AbortSignal`:
|
|
174
|
+
|
|
175
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
176
|
+
const controller = new AbortController();
|
|
177
|
+
const { signal } = controller;
|
|
178
|
+
|
|
179
|
+
const proc = Bun.spawn({
|
|
180
|
+
cmd: ["sleep", "100"],
|
|
181
|
+
signal,
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// Later, to abort the process:
|
|
185
|
+
controller.abort();
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Using timeout and killSignal
|
|
189
|
+
|
|
190
|
+
You can set a timeout for a subprocess to automatically terminate after a specific duration:
|
|
191
|
+
|
|
192
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
193
|
+
// Kill the process after 5 seconds
|
|
194
|
+
const proc = Bun.spawn({
|
|
195
|
+
cmd: ["sleep", "10"],
|
|
196
|
+
timeout: 5000, // 5 seconds in milliseconds
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
await proc.exited; // Will resolve after 5 seconds
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
By default, timed-out processes are killed with the `SIGTERM` signal. You can specify a different signal with the `killSignal` option:
|
|
203
|
+
|
|
204
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
205
|
+
// Kill the process with SIGKILL after 5 seconds
|
|
206
|
+
const proc = Bun.spawn({
|
|
207
|
+
cmd: ["sleep", "10"],
|
|
208
|
+
timeout: 5000,
|
|
209
|
+
killSignal: "SIGKILL", // Can be string name or signal number
|
|
210
|
+
});
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
The `killSignal` option also controls which signal is sent when an AbortSignal is aborted.
|
|
214
|
+
|
|
215
|
+
## Using maxBuffer
|
|
216
|
+
|
|
217
|
+
For spawnSync, you can limit the maximum number of bytes of output before the process is killed:
|
|
218
|
+
|
|
219
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
220
|
+
// Kill 'yes' after it emits over 100 bytes of output
|
|
221
|
+
const result = Bun.spawnSync({
|
|
222
|
+
cmd: ["yes"], // or ["bun", "exec", "yes"] on Windows
|
|
223
|
+
maxBuffer: 100,
|
|
224
|
+
});
|
|
225
|
+
// process exits
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Inter-process communication (IPC)
|
|
229
|
+
|
|
230
|
+
Bun supports direct inter-process communication channel between two `bun` processes. To receive messages from a spawned Bun subprocess, specify an `ipc` handler.
|
|
231
|
+
|
|
232
|
+
```ts parent.ts icon="/icons/typescript.svg"
|
|
233
|
+
const child = Bun.spawn(["bun", "child.ts"], {
|
|
234
|
+
ipc(message) {
|
|
235
|
+
/**
|
|
236
|
+
* The message received from the sub process
|
|
237
|
+
**/
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
The parent process can send messages to the subprocess using the `.send()` method on the returned `Subprocess` instance. A reference to the sending subprocess is also available as the second argument in the `ipc` handler.
|
|
243
|
+
|
|
244
|
+
```ts parent.ts icon="/icons/typescript.svg"
|
|
245
|
+
const childProc = Bun.spawn(["bun", "child.ts"], {
|
|
246
|
+
ipc(message, childProc) {
|
|
247
|
+
/**
|
|
248
|
+
* The message received from the sub process
|
|
249
|
+
**/
|
|
250
|
+
childProc.send("Respond to child");
|
|
251
|
+
},
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
childProc.send("I am your father"); // The parent can send messages to the child as well
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Meanwhile the child process can send messages to its parent using with `process.send()` and receive messages with `process.on("message")`. This is the same API used for `child_process.fork()` in Node.js.
|
|
258
|
+
|
|
259
|
+
```ts child.ts
|
|
260
|
+
process.send("Hello from child as string");
|
|
261
|
+
process.send({ message: "Hello from child as object" });
|
|
262
|
+
|
|
263
|
+
process.on("message", message => {
|
|
264
|
+
// print message from parent
|
|
265
|
+
console.log(message);
|
|
266
|
+
});
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
```ts child.ts
|
|
270
|
+
// send a string
|
|
271
|
+
process.send("Hello from child as string");
|
|
272
|
+
|
|
273
|
+
// send an object
|
|
274
|
+
process.send({ message: "Hello from child as object" });
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
The `serialization` option controls the underlying communication format between the two processes:
|
|
278
|
+
|
|
279
|
+
- `advanced`: (default) Messages are serialized using the JSC `serialize` API, which supports cloning [everything `structuredClone` supports](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm). This does not support transferring ownership of objects.
|
|
280
|
+
- `json`: Messages are serialized using `JSON.stringify` and `JSON.parse`, which does not support as many object types as `advanced` does.
|
|
281
|
+
|
|
282
|
+
To disconnect the IPC channel from the parent process, call:
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
childProc.disconnect();
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### IPC between Bun & Node.js
|
|
289
|
+
|
|
290
|
+
To use IPC between a `bun` process and a Node.js process, set `serialization: "json"` in `Bun.spawn`. This is because Node.js and Bun use different JavaScript engines with different object serialization formats.
|
|
291
|
+
|
|
292
|
+
```ts bun-node-ipc.js icon="file-code"
|
|
293
|
+
if (typeof Bun !== "undefined") {
|
|
294
|
+
const prefix = `[bun ${process.versions.bun} 🐇]`;
|
|
295
|
+
const node = Bun.spawn({
|
|
296
|
+
cmd: ["node", __filename],
|
|
297
|
+
ipc({ message }) {
|
|
298
|
+
console.log(message);
|
|
299
|
+
node.send({ message: `${prefix} 👋 hey node` });
|
|
300
|
+
node.kill();
|
|
301
|
+
},
|
|
302
|
+
stdio: ["inherit", "inherit", "inherit"],
|
|
303
|
+
serialization: "json",
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
node.send({ message: `${prefix} 👋 hey node` });
|
|
307
|
+
} else {
|
|
308
|
+
const prefix = `[node ${process.version}]`;
|
|
309
|
+
process.on("message", ({ message }) => {
|
|
310
|
+
console.log(message);
|
|
311
|
+
process.send({ message: `${prefix} 👋 hey bun` });
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
---
|
|
317
|
+
|
|
318
|
+
## Blocking API (`Bun.spawnSync()`)
|
|
319
|
+
|
|
320
|
+
Bun provides a synchronous equivalent of `Bun.spawn` called `Bun.spawnSync`. This is a blocking API that supports the same inputs and parameters as `Bun.spawn`. It returns a `SyncSubprocess` object, which differs from `Subprocess` in a few ways.
|
|
321
|
+
|
|
322
|
+
1. It contains a `success` property that indicates whether the process exited with a zero exit code.
|
|
323
|
+
2. The `stdout` and `stderr` properties are instances of `Buffer` instead of `ReadableStream`.
|
|
324
|
+
3. There is no `stdin` property. Use `Bun.spawn` to incrementally write to the subprocess's input stream.
|
|
325
|
+
|
|
326
|
+
```ts
|
|
327
|
+
const proc = Bun.spawnSync(["echo", "hello"]);
|
|
328
|
+
|
|
329
|
+
console.log(proc.stdout.toString());
|
|
330
|
+
// => "hello\n"
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
As a rule of thumb, the asynchronous `Bun.spawn` API is better for HTTP servers and apps, and `Bun.spawnSync` is better for building command-line tools.
|
|
334
|
+
|
|
335
|
+
---
|
|
336
|
+
|
|
337
|
+
## Benchmarks
|
|
338
|
+
|
|
339
|
+
<Note>
|
|
340
|
+
⚡️ Under the hood, `Bun.spawn` and `Bun.spawnSync` use
|
|
341
|
+
[`posix_spawn(3)`](https://man7.org/linux/man-pages/man3/posix_spawn.3.html).
|
|
342
|
+
</Note>
|
|
343
|
+
|
|
344
|
+
Bun's `spawnSync` spawns processes 60% faster than the Node.js `child_process` module.
|
|
345
|
+
|
|
346
|
+
```bash terminal icon="terminal"
|
|
347
|
+
bun spawn.mjs
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
```txt
|
|
351
|
+
cpu: Apple M1 Max
|
|
352
|
+
runtime: bun 1.x (arm64-darwin)
|
|
353
|
+
|
|
354
|
+
benchmark time (avg) (min … max) p75 p99 p995
|
|
355
|
+
--------------------------------------------------------- -----------------------------
|
|
356
|
+
spawnSync echo hi 888.14 µs/iter (821.83 µs … 1.2 ms) 905.92 µs 1 ms 1.03 ms
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
```sh terminal icon="terminal"
|
|
360
|
+
node spawn.node.mjs
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
```txt
|
|
364
|
+
cpu: Apple M1 Max
|
|
365
|
+
runtime: node v18.9.1 (arm64-darwin)
|
|
366
|
+
|
|
367
|
+
benchmark time (avg) (min … max) p75 p99 p995
|
|
368
|
+
--------------------------------------------------------- -----------------------------
|
|
369
|
+
spawnSync echo hi 1.47 ms/iter (1.14 ms … 2.64 ms) 1.57 ms 2.37 ms 2.52 ms
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
---
|
|
373
|
+
|
|
374
|
+
## Reference
|
|
375
|
+
|
|
376
|
+
A reference of the Spawn API and types are shown below. The real types have complex generics to strongly type the `Subprocess` streams with the options passed to `Bun.spawn` and `Bun.spawnSync`. For full details, find these types as defined [bun.d.ts](https://github.com/oven-sh/bun/blob/main/packages/bun-types/bun.d.ts).
|
|
377
|
+
|
|
378
|
+
```ts See Typescript Definitions expandable
|
|
379
|
+
interface Bun {
|
|
380
|
+
spawn(command: string[], options?: SpawnOptions.OptionsObject): Subprocess;
|
|
381
|
+
spawnSync(command: string[], options?: SpawnOptions.OptionsObject): SyncSubprocess;
|
|
382
|
+
|
|
383
|
+
spawn(options: { cmd: string[] } & SpawnOptions.OptionsObject): Subprocess;
|
|
384
|
+
spawnSync(options: { cmd: string[] } & SpawnOptions.OptionsObject): SyncSubprocess;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
namespace SpawnOptions {
|
|
388
|
+
interface OptionsObject {
|
|
389
|
+
cwd?: string;
|
|
390
|
+
env?: Record<string, string | undefined>;
|
|
391
|
+
stdio?: [Writable, Readable, Readable];
|
|
392
|
+
stdin?: Writable;
|
|
393
|
+
stdout?: Readable;
|
|
394
|
+
stderr?: Readable;
|
|
395
|
+
onExit?(
|
|
396
|
+
subprocess: Subprocess,
|
|
397
|
+
exitCode: number | null,
|
|
398
|
+
signalCode: number | null,
|
|
399
|
+
error?: ErrorLike,
|
|
400
|
+
): void | Promise<void>;
|
|
401
|
+
ipc?(message: any, subprocess: Subprocess): void;
|
|
402
|
+
serialization?: "json" | "advanced";
|
|
403
|
+
windowsHide?: boolean;
|
|
404
|
+
windowsVerbatimArguments?: boolean;
|
|
405
|
+
argv0?: string;
|
|
406
|
+
signal?: AbortSignal;
|
|
407
|
+
timeout?: number;
|
|
408
|
+
killSignal?: string | number;
|
|
409
|
+
maxBuffer?: number;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
type Readable =
|
|
413
|
+
| "pipe"
|
|
414
|
+
| "inherit"
|
|
415
|
+
| "ignore"
|
|
416
|
+
| null // equivalent to "ignore"
|
|
417
|
+
| undefined // to use default
|
|
418
|
+
| BunFile
|
|
419
|
+
| ArrayBufferView
|
|
420
|
+
| number;
|
|
421
|
+
|
|
422
|
+
type Writable =
|
|
423
|
+
| "pipe"
|
|
424
|
+
| "inherit"
|
|
425
|
+
| "ignore"
|
|
426
|
+
| null // equivalent to "ignore"
|
|
427
|
+
| undefined // to use default
|
|
428
|
+
| BunFile
|
|
429
|
+
| ArrayBufferView
|
|
430
|
+
| number
|
|
431
|
+
| ReadableStream
|
|
432
|
+
| Blob
|
|
433
|
+
| Response
|
|
434
|
+
| Request;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
interface Subprocess extends AsyncDisposable {
|
|
438
|
+
readonly stdin: FileSink | number | undefined;
|
|
439
|
+
readonly stdout: ReadableStream<Uint8Array> | number | undefined;
|
|
440
|
+
readonly stderr: ReadableStream<Uint8Array> | number | undefined;
|
|
441
|
+
readonly readable: ReadableStream<Uint8Array> | number | undefined;
|
|
442
|
+
readonly pid: number;
|
|
443
|
+
readonly exited: Promise<number>;
|
|
444
|
+
readonly exitCode: number | null;
|
|
445
|
+
readonly signalCode: NodeJS.Signals | null;
|
|
446
|
+
readonly killed: boolean;
|
|
447
|
+
|
|
448
|
+
kill(exitCode?: number | NodeJS.Signals): void;
|
|
449
|
+
ref(): void;
|
|
450
|
+
unref(): void;
|
|
451
|
+
|
|
452
|
+
send(message: any): void;
|
|
453
|
+
disconnect(): void;
|
|
454
|
+
resourceUsage(): ResourceUsage | undefined;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
interface SyncSubprocess {
|
|
458
|
+
stdout: Buffer | undefined;
|
|
459
|
+
stderr: Buffer | undefined;
|
|
460
|
+
exitCode: number;
|
|
461
|
+
success: boolean;
|
|
462
|
+
resourceUsage: ResourceUsage;
|
|
463
|
+
signalCode?: string;
|
|
464
|
+
exitedDueToTimeout?: true;
|
|
465
|
+
pid: number;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
interface ResourceUsage {
|
|
469
|
+
contextSwitches: {
|
|
470
|
+
voluntary: number;
|
|
471
|
+
involuntary: number;
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
cpuTime: {
|
|
475
|
+
user: number;
|
|
476
|
+
system: number;
|
|
477
|
+
total: number;
|
|
478
|
+
};
|
|
479
|
+
maxRSS: number;
|
|
480
|
+
|
|
481
|
+
messages: {
|
|
482
|
+
sent: number;
|
|
483
|
+
received: number;
|
|
484
|
+
};
|
|
485
|
+
ops: {
|
|
486
|
+
in: number;
|
|
487
|
+
out: number;
|
|
488
|
+
};
|
|
489
|
+
shmSize: number;
|
|
490
|
+
signalCount: number;
|
|
491
|
+
swapCount: number;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
type Signal =
|
|
495
|
+
| "SIGABRT"
|
|
496
|
+
| "SIGALRM"
|
|
497
|
+
| "SIGBUS"
|
|
498
|
+
| "SIGCHLD"
|
|
499
|
+
| "SIGCONT"
|
|
500
|
+
| "SIGFPE"
|
|
501
|
+
| "SIGHUP"
|
|
502
|
+
| "SIGILL"
|
|
503
|
+
| "SIGINT"
|
|
504
|
+
| "SIGIO"
|
|
505
|
+
| "SIGIOT"
|
|
506
|
+
| "SIGKILL"
|
|
507
|
+
| "SIGPIPE"
|
|
508
|
+
| "SIGPOLL"
|
|
509
|
+
| "SIGPROF"
|
|
510
|
+
| "SIGPWR"
|
|
511
|
+
| "SIGQUIT"
|
|
512
|
+
| "SIGSEGV"
|
|
513
|
+
| "SIGSTKFLT"
|
|
514
|
+
| "SIGSTOP"
|
|
515
|
+
| "SIGSYS"
|
|
516
|
+
| "SIGTERM"
|
|
517
|
+
| "SIGTRAP"
|
|
518
|
+
| "SIGTSTP"
|
|
519
|
+
| "SIGTTIN"
|
|
520
|
+
| "SIGTTOU"
|
|
521
|
+
| "SIGUNUSED"
|
|
522
|
+
| "SIGURG"
|
|
523
|
+
| "SIGUSR1"
|
|
524
|
+
| "SIGUSR2"
|
|
525
|
+
| "SIGVTALRM"
|
|
526
|
+
| "SIGWINCH"
|
|
527
|
+
| "SIGXCPU"
|
|
528
|
+
| "SIGXFSZ"
|
|
529
|
+
| "SIGBREAK"
|
|
530
|
+
| "SIGLOST"
|
|
531
|
+
| "SIGINFO";
|
|
532
|
+
```
|