aboutsummaryrefslogtreecommitdiff
path: root/docs/api
diff options
context:
space:
mode:
Diffstat (limited to 'docs/api')
-rw-r--r--docs/api/ffi.md21
-rw-r--r--docs/api/file-io.md6
-rw-r--r--docs/api/globals.md6
-rw-r--r--docs/api/http.md6
-rw-r--r--docs/api/spawn.md21
-rw-r--r--docs/api/tcp.md6
-rw-r--r--docs/api/utils.md8
-rw-r--r--docs/api/websockets.md4
8 files changed, 32 insertions, 46 deletions
diff --git a/docs/api/ffi.md b/docs/api/ffi.md
index 13b50f9c7..d6ed23f47 100644
--- a/docs/api/ffi.md
+++ b/docs/api/ffi.md
@@ -1,5 +1,7 @@
Use the built-in `bun:ffi` module to efficiently call native libraries from JavaScript. It works with languages that support the C ABI (Zig, Rust, C/C++, C#, Nim, Kotlin, etc).
+## Usage (`bun:ffi`)
+
To print the version number of `sqlite3`:
```ts
@@ -227,11 +229,7 @@ const lib = linkSymbols({
},
});
-const [major, minor, patch] = [
- lib.symbols.getMajor(),
- lib.symbols.getMinor(),
- lib.symbols.getPatch(),
-];
+const [major, minor, patch] = [lib.symbols.getMajor(), lib.symbols.getMinor(), lib.symbols.getPatch()];
```
## Callbacks
@@ -251,13 +249,10 @@ const {
},
});
-const searchIterator = new JSCallback(
- (ptr, length) => /hello/.test(new CString(ptr, length)),
- {
- returns: "bool",
- args: ["ptr", "usize"],
- },
-);
+const searchIterator = new JSCallback((ptr, length) => /hello/.test(new CString(ptr, length)), {
+ returns: "bool",
+ args: ["ptr", "usize"],
+});
const str = Buffer.from("wwutwutwutwutwutwutwutwutwutwutut\0", "utf8");
if (search(ptr(str), searchIterator)) {
@@ -278,7 +273,7 @@ When you're done with a JSCallback, you should call `close()` to free the memory
**⚡️ Performance tip** — For a slight performance boost, directly pass `JSCallback.prototype.ptr` instead of the `JSCallback` object:
```ts
-const onResolve = new JSCallback((arg) => arg === 42, {
+const onResolve = new JSCallback(arg => arg === 42, {
returns: "bool",
args: ["i32"],
});
diff --git a/docs/api/file-io.md b/docs/api/file-io.md
index 35d639422..ca78338d2 100644
--- a/docs/api/file-io.md
+++ b/docs/api/file-io.md
@@ -1,10 +1,10 @@
{% callout %}
-**Note** — The `Bun.file` and `Bun.write` APIs documented on this page are heavily optimized and represent the recommended way to perform file-system tasks using Bun. Existing Node.js projects may use Bun's [nearly complete](/docs/ecosystem/nodejs#node_fs) implementation of the [`node:fs`](https://nodejs.org/api/fs.html) module.
+**Note** — The `Bun.file` and `Bun.write` APIs documented on this page are heavily optimized and represent the recommended way to perform file-system tasks using Bun. Existing Node.js projects may use Bun's [nearly complete](/docs/runtime/nodejs-apis#node_fs) implementation of the [`node:fs`](https://nodejs.org/api/fs.html) module.
{% /callout %}
Bun provides a set of optimized APIs for reading and writing files.
-## Reading files
+## Reading files (`Bun.file()`)
`Bun.file(path): BunFile`
@@ -56,7 +56,7 @@ Bun.stdout;
Bun.stderr;
```
-## Writing files
+## Writing files (`Bun.write()`)
`Bun.write(destination, data): Promise<number>`
diff --git a/docs/api/globals.md b/docs/api/globals.md
index d49069590..f5fc411dc 100644
--- a/docs/api/globals.md
+++ b/docs/api/globals.md
@@ -34,7 +34,7 @@ Bun implements the following globals.
- [`Buffer`](https://nodejs.org/api/buffer.html#class-buffer)
- Node.js
-- See [Node.js > `Buffer`](/docs/ecosystem/nodejs#node_buffer)
+- See [Node.js > `Buffer`](/docs/runtime/nodejs-apis#node_buffer)
---
@@ -172,7 +172,7 @@ Bun implements the following globals.
- [`global`](https://nodejs.org/api/globals.html#global)
- Node.js
-- See [Node.js > `global`](/docs/ecosystem/nodejs#node_global).
+- See [Node.js > `global`](/docs/runtime/nodejs-apis#node_global).
---
@@ -214,7 +214,7 @@ Bun implements the following globals.
- [`process`](https://nodejs.org/api/process.html)
- Node.js
-- See [Node.js > `process`](/docs/ecosystem/nodejs#node_process)
+- See [Node.js > `process`](/docs/runtime/nodejs-apis#node_process)
---
diff --git a/docs/api/http.md b/docs/api/http.md
index 3e11299dd..3ebdafab9 100644
--- a/docs/api/http.md
+++ b/docs/api/http.md
@@ -1,8 +1,8 @@
{% callout %}
-**Note** — This page documents the `Bun.serve` API. This API is heavily optimized and represents the recommended way to build HTTP servers in Bun. Existing Node.js projects may use Bun's [nearly complete](/docs/ecosystem/nodejs#node_http) implementation of the Node.js [`http`](https://nodejs.org/api/http.html) and [`https`](https://nodejs.org/api/https.html) modules.
+**Note** — This page documents the `Bun.serve` API. This API is heavily optimized and represents the recommended way to build HTTP servers in Bun. Existing Node.js projects may use Bun's [nearly complete](/docs/runtime/nodejs-apis#node_http) implementation of the Node.js [`http`](https://nodejs.org/api/http.html) and [`https`](https://nodejs.org/api/https.html) modules.
{% /callout %}
-## Send a request
+## Send a request (`fetch()`)
Bun implements the Web `fetch` API for making HTTP requests. The `fetch` function is available in the global scope.
@@ -13,7 +13,7 @@ console.log(result.icons[0].src);
// => "/logo-square.jpg"
```
-## Start a server
+## Start a server (`Bun.serve()`)
Start an HTTP server in Bun with `Bun.serve`.
diff --git a/docs/api/spawn.md b/docs/api/spawn.md
index 100969aa3..1ef81f1b7 100644
--- a/docs/api/spawn.md
+++ b/docs/api/spawn.md
@@ -1,6 +1,6 @@
Spawn child processes with `Bun.spawn` or `Bun.spawnSync`.
-## Spawn a process
+## Spawn a process (`Bun.spawn()`)
Provide a command as an array of strings. The result of `Bun.spawn()` is a `Bun.Subprocess` object.
@@ -28,9 +28,7 @@ By default, the input stream of the subprocess is undefined; it can be configure
```ts
const proc = Bun.spawn(["cat"], {
- stdin: await fetch(
- "https://raw.githubusercontent.com/oven-sh/bun/main/examples/hashing.js",
- ),
+ stdin: await fetch("https://raw.githubusercontent.com/oven-sh/bun/main/examples/hashing.js"),
});
const text = await new Response(proc.stdout).text();
@@ -183,7 +181,7 @@ const proc = Bun.spawn(["echo", "hello"]);
proc.unref();
```
-## Blocking API
+## Blocking API (`Bun.spawnSync()`)
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.
@@ -245,12 +243,7 @@ namespace SpawnOptions {
stdin?: SpawnOptions.Readable;
stdout?: SpawnOptions.Writable;
stderr?: SpawnOptions.Writable;
- onExit?: (
- proc: Subprocess,
- exitCode: number | null,
- signalCode: string | null,
- error: Error | null,
- ) => void;
+ onExit?: (proc: Subprocess, exitCode: number | null, signalCode: string | null, error: Error | null) => void;
}
type Readable =
@@ -262,7 +255,7 @@ namespace SpawnOptions {
| BunFile
| ArrayBufferView
| number;
-
+
type Writable =
| "pipe"
| "inherit"
@@ -307,10 +300,10 @@ interface SyncSubprocess<Stdout, Stderr> {
type ReadableSubprocess = Subprocess<any, "pipe", "pipe">;
type WritableSubprocess = Subprocess<"pipe", any, any>;
type PipedSubprocess = Subprocess<"pipe", "pipe", "pipe">;
-type NullSubprocess = Subprocess<null, null, null>
+type NullSubprocess = Subprocess<null, null, null>;
type ReadableSyncSubprocess = SyncSubprocess<"pipe", "pipe">;
-type NullSyncSubprocess = SyncSubprocess<null, null>
+type NullSyncSubprocess = SyncSubprocess<null, null>;
type Signal =
| "SIGABRT"
diff --git a/docs/api/tcp.md b/docs/api/tcp.md
index 5e04dd348..999464f3f 100644
--- a/docs/api/tcp.md
+++ b/docs/api/tcp.md
@@ -1,6 +1,6 @@
Use Bun's native TCP API implement performance sensitive systems like database clients, game servers, or anything that needs to communicate over TCP (instead of HTTP). This is a low-level API intended for library authors and for advanced use cases.
-## Start a server
+## Start a server (`Bun.listen()`)
To start a TCP server with `Bun.listen`:
@@ -90,7 +90,7 @@ server.stop(true);
server.unref();
```
-## Create a connection
+## Create a connection (`Bun.connect()`)
Use `Bun.connect` to connect to a TCP server. Specify the server to connect to with `hostname` and `port`. TCP clients can define the same set of handlers as `Bun.listen`, plus a couple client-specific handlers.
@@ -136,7 +136,7 @@ const server = Bun.listen({ /* config */ })
// reloads handlers for all active server-side sockets
server.reload({
- socket:
+ socket: {
data(){
// new 'data' handler
}
diff --git a/docs/api/utils.md b/docs/api/utils.md
index ce3d0d6f9..59167b2b7 100644
--- a/docs/api/utils.md
+++ b/docs/api/utils.md
@@ -94,9 +94,7 @@ test("peek", () => {
// If we peek a rejected promise, it:
// - returns the error
// - does not mark the promise as handled
- const rejected = Promise.reject(
- new Error("Successfully tested promise rejection"),
- );
+ const rejected = Promise.reject(new Error("Successfully tested promise rejection"));
expect(peek(rejected).message).toBe("Successfully tested promise rejection");
});
```
@@ -128,7 +126,7 @@ const currentFile = import.meta.url;
Bun.openInEditor(currentFile);
```
-You can override this via the `debug.editor` setting in your [`bunfig.toml`](/docs/project/configuration)
+You can override this via the `debug.editor` setting in your [`bunfig.toml`](/docs/runtime/configuration)
```toml-diff#bunfig.toml
+ [debug]
@@ -142,5 +140,5 @@ Bun.openInEditor(import.meta.url, {
editor: "vscode", // or "subl"
line: 10,
column: 5,
-})
+});
```
diff --git a/docs/api/websockets.md b/docs/api/websockets.md
index f04d10fc6..0c40a05c0 100644
--- a/docs/api/websockets.md
+++ b/docs/api/websockets.md
@@ -12,7 +12,7 @@
Internally Bun's WebSocket implementation is built on [uWebSockets](https://github.com/uNetworking/uWebSockets).
{% /callout %}
-## Create a client
+## Connect to a WebSocket server
To connect to an external socket server, create an instance of `WebSocket` with the constructor.
@@ -46,7 +46,7 @@ socket.addEventListener("close", event => {});
socket.addEventListener("error", event => {});
```
-## Create a server
+## Create a WebSocket server
Below is a simple WebSocket server built with `Bun.serve`, in which all incoming requests are [upgraded](https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism) to WebSocket connections in the `fetch` handler. The socket handlers are declared in the `websocket` parameter.