Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions docs/bundler/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1701,19 +1701,17 @@ BuildArtifact (entry-point) {
path: "./index.js",
loader: "tsx",
kind: "entry-point",
hash: "824a039620219640",
Blob (74756 bytes) {
hash: "qhq9679t",
Blob (161 bytes) {
type: "text/javascript;charset=utf-8"
},
sourcemap: BuildArtifact (sourcemap) {
path: "./index.js.map",
loader: "file",
kind: "sourcemap",
hash: "e7178cda3e72e301",
Blob (24765 bytes) {
Blob (348 bytes) {
type: "application/json;charset=utf-8"
},
sourcemap: null
}
}
}
```
Expand Down
3 changes: 3 additions & 0 deletions docs/runtime/child-process.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ const proc = Bun.spawn(["bash"], {
// Write to the terminal
proc.terminal.write("echo hello\n");

// bash runs until it reads `exit`
proc.terminal.write("exit\n");

// Wait for the process to exit
await proc.exited;

Expand Down
7 changes: 6 additions & 1 deletion docs/runtime/hashing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -248,21 +248,26 @@ hasher.digest();
// => Uint8Array(32) [ 185, 77, 39, 185, 147, ... ]
```

`.digest()` resets the hasher. A later `.digest()` hashes only the data you pass to `.update()` after the reset.

To get the hash as a string, pass an encoding to `.digest()`:

```ts
hasher.update("hello world");
hasher.digest("base64");
// => "uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="

hasher.update("hello world");
hasher.digest("hex");
// => "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
```

Alternatively, `.digest()` can write the hash into an existing `TypedArray` instead of allocating a new one.
Alternatively, `.digest()` can write the hash into an existing `TypedArray` instead of allocating a new one. Pass a `TypedArray` of at least `hasher.byteLength` bytes.

```ts
const arr = new Uint8Array(32);

hasher.update("hello world");
hasher.digest(arr);

console.log(arr);
Expand Down
9 changes: 6 additions & 3 deletions docs/runtime/networking/tcp.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ server.reload({
const socket = await Bun.connect({/* config */});

socket.reload({
data() {
// new 'data' handler
socket: {
data() {
// new 'data' handler
},
},
});
```
Expand Down Expand Up @@ -206,6 +208,7 @@ import { ArrayBufferSink } from "bun";
const sink = new ArrayBufferSink();
sink.start({
stream: true, // [!code ++]
asUint8Array: true, // flush() returns a Uint8Array
highWaterMark: 1024,
});

Expand All @@ -216,7 +219,7 @@ sink.write("l");
sink.write("o");

queueMicrotask(() => {
const data = sink.flush();
const data = sink.flush() as Uint8Array;
const wrote = socket.write(data);
if (wrote < data.byteLength) {
// put it back in the sink if the socket is full
Expand Down
5 changes: 3 additions & 2 deletions docs/runtime/yaml.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,9 @@ import { createConnection } from "./database-driver";
function parseConfig(config: any) {
return JSON.parse(
JSON.stringify(config).replace(
/\${([^:-]+)(?::([^}]+))?}/g,
(_, key, defaultValue) => process.env[key] || defaultValue || "",
/\${([^:}]+)(?::-([^}]*))?}/g,
// The default is already JSON text. Escape the environment value the same way.
(_, key, defaultValue = "") => (process.env[key] ? JSON.stringify(process.env[key]).slice(1, -1) : defaultValue),
),
);
}
Expand Down
19 changes: 11 additions & 8 deletions packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2684,7 +2684,7 @@ declare module "bun" {
* @example
* ```js
* const file = Bun.file("./hello.json");
* console.log(file.type); // "application/json"
* console.log(file.type); // "application/json;charset=utf-8"
* console.log(await file.text()); // '{"hello":"world"}'
* ```
*
Expand Down Expand Up @@ -5179,7 +5179,7 @@ declare module "bun" {
* @example
* ```js
* const file = Bun.file("./hello.json");
* console.log(file.type); // "application/json"
* console.log(file.type); // "application/json;charset=utf-8"
* console.log(await file.json()); // { hello: "world" }
* ```
*
Expand Down Expand Up @@ -5228,7 +5228,7 @@ declare module "bun" {
* @example
* ```js
* const file = Bun.file(new TextEncoder().encode("./hello.json"));
* console.log(file.type); // "application/json"
* console.log(file.type); // "application/json;charset=utf-8"
* ```
*
* @param path The path to the file as a byte buffer (the buffer is copied). If the path starts with `s3://`, the file behaves like {@link S3File}
Expand Down Expand Up @@ -5470,14 +5470,13 @@ declare module "bun" {
*
* ws.addEventListener("open", () => {
* console.log("Connected to server");
* ws.send("Hello, server!");
* });
*
* ws.addEventListener("message", (event) => {
* console.log("Received message:", event.data);
* ws.terminate();
* });
*
* ws.send("Hello, server!");
* ws.terminate();
* ```
*/
interface WebSocket extends EventTarget {
Expand Down Expand Up @@ -8434,6 +8433,7 @@ declare module "bun" {
* });
*
* proc.terminal.write("echo hello\n");
* proc.terminal.write("exit\n"); // bash runs until it reads `exit`
* await proc.exited;
* proc.terminal.close();
* ```
Expand Down Expand Up @@ -9246,6 +9246,9 @@ declare module "bun" {
* // Write to the terminal
* terminal.write("echo hello\n");
*
* // bash runs until it reads `exit`
* terminal.write("exit\n");
*
* // Wait for process to exit
* await proc.exited;
*
Expand Down Expand Up @@ -9712,13 +9715,13 @@ declare module "bun" {
* @example
* ```js
* const glob = new Glob("*.{ts,tsx}");
* const scannedFiles = Array.from(glob.scan({ cwd: './src' }))
* const scannedFiles = Array.from(glob.scanSync({ cwd: './src' }))
* ```
*
* @example
* ```js
* const glob = new Glob("*.{ts,tsx}");
* for (const path of glob.scan()) {
* for (const path of glob.scanSync()) {
* // do something
* }
* ```
Expand Down
4 changes: 2 additions & 2 deletions packages/bun-types/ffi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,9 @@ declare module "bun:ffi" {
* @example
* From JavaScript:
* ```ts
* import { dlopen, CString } from "bun:ffi"
* import { dlopen, CString, suffix } from "bun:ffi"
*
* const lib = dlopen('z', {
* const lib = dlopen(`z.${suffix}`, {
* version: {
* returns: "ptr",
* }
Expand Down
5 changes: 3 additions & 2 deletions packages/bun-types/serve.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,13 @@ declare module "bun" {

/**
* Returns an array of all topics the client is currently subscribed to.
* The order of the topics is not specified.
*
* @example
* ```ts
* ws.subscribe("chat");
* ws.subscribe("notifications");
* console.log(ws.subscriptions); // ["chat", "notifications"]
* console.log(ws.subscriptions); // ["chat", "notifications"] or ["notifications", "chat"]
* ```
*/
readonly subscriptions: string[];
Expand Down Expand Up @@ -436,7 +437,7 @@ declare module "bun" {
* Bun.serve({
* websocket: {
* data: {} as { name: string }, // ← Specify the type of `ws.data` like this
* message: (ws, message) => console.log(ws.data.name, 'says:', message);
* message: (ws, message) => console.log(ws.data.name, 'says:', message),
* },
* // ...
* });
Expand Down
28 changes: 14 additions & 14 deletions packages/bun-types/shell.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ declare module "bun" {
*
* **Read as base64 string**
* ```ts
* const output = await $`echo ${atob("hello")}`.text("base64");
* console.log(output); // "hello\n"
* const output = await $`echo hello`.text("base64");
* console.log(output); // "aGVsbG8K"
* ```
*/
text(encoding?: BufferEncoding): Promise<string>;
Expand Down Expand Up @@ -171,7 +171,7 @@ declare module "bun" {
*
* ```ts
* const output = await $`echo hello`.arrayBuffer();
* console.log(output); // ArrayBuffer { byteLength: 6 }
* console.log(output); // ArrayBuffer(6) [ 104, 101, 108, 108, 111, 10 ]
* ```
*/
arrayBuffer(): Promise<ArrayBuffer>;
Expand All @@ -184,7 +184,7 @@ declare module "bun" {
* @example
* ```ts
* const output = await $`echo hello`.blob();
* console.log(output); // Blob { size: 6, type: "" }
* console.log(output); // Blob (6 bytes)
* ```
*/
blob(): Promise<Blob>;
Expand Down Expand Up @@ -238,8 +238,8 @@ declare module "bun" {
*
* **Read as base64 string**
* ```ts
* const output = await $`echo ${atob("hello")}`;
* console.log(output.text("base64")); // "hello\n"
* const output = await $`echo hello`;
* console.log(output.text("base64")); // "aGVsbG8K"
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
* ```
*/
text(encoding?: BufferEncoding): string;
Expand All @@ -266,7 +266,7 @@ declare module "bun" {
*
* ```ts
* const output = await $`echo hello`;
* console.log(output.arrayBuffer()); // ArrayBuffer { byteLength: 6 }
* console.log(output.arrayBuffer()); // ArrayBuffer(6) [ 104, 101, 108, 108, 111, 10 ]
* ```
*/
arrayBuffer(): ArrayBuffer;
Expand All @@ -278,7 +278,7 @@ declare module "bun" {
* @example
* ```ts
* const output = await $`echo hello`;
* console.log(output.blob()); // Blob { size: 6, type: "" }
* console.log(output.blob()); // Blob (6 bytes)
* ```
*/
blob(): Blob;
Expand All @@ -290,7 +290,7 @@ declare module "bun" {
* @example
* ```ts
* const output = await $`echo hello`;
* console.log(output.bytes()); // Uint8Array { byteLength: 6 }
* console.log(output.bytes()); // Uint8Array(6) [ 104, 101, 108, 108, 111, 10 ]
* ```
*/
bytes(): Uint8Array<ArrayBuffer>;
Expand All @@ -316,8 +316,8 @@ declare module "bun" {
*
* **Read as base64 string**
* ```ts
* const output = await $`echo ${atob("hello")}`;
* console.log(output.text("base64")); // "hello\n"
* const output = await $`echo hello`;
* console.log(output.text("base64")); // "aGVsbG8K"
* ```
*/
text(encoding?: BufferEncoding): string;
Expand All @@ -344,7 +344,7 @@ declare module "bun" {
*
* ```ts
* const output = await $`echo hello`;
* console.log(output.arrayBuffer()); // ArrayBuffer { byteLength: 6 }
* console.log(output.arrayBuffer()); // ArrayBuffer(6) [ 104, 101, 108, 108, 111, 10 ]
* ```
*/
arrayBuffer(): ArrayBuffer;
Expand All @@ -357,7 +357,7 @@ declare module "bun" {
*
* ```ts
* const output = await $`echo hello`;
* console.log(output.bytes()); // Uint8Array { byteLength: 6 }
* console.log(output.bytes()); // Uint8Array(6) [ 104, 101, 108, 108, 111, 10 ]
* ```
*/
bytes(): Uint8Array<ArrayBuffer>;
Expand All @@ -369,7 +369,7 @@ declare module "bun" {
* @example
* ```ts
* const output = await $`echo hello`;
* console.log(output.blob()); // Blob { size: 6, type: "" }
* console.log(output.blob()); // Blob (6 bytes)
* ```
*/
blob(): Blob;
Expand Down
4 changes: 2 additions & 2 deletions packages/bun-types/sqlite.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,10 +936,10 @@ declare module "bun:sqlite" {
* const stmt = db.prepare("SELECT * FROM foo WHERE bar = ?");
*
* stmt.raw("baz");
* // => [[Uint8Array(24)]]
* // => [[Uint8Array(3)]]
*
* stmt.raw();
* // => [[Uint8Array(24)]]
* // => [[Uint8Array(3)]]
* ```
*/
raw(...params: ParamsType): Array<Array<Uint8Array | null>>;
Expand Down
8 changes: 4 additions & 4 deletions packages/bun-types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1944,7 +1944,7 @@ declare module "bun:test" {
*
* @example
* expect(1).not.toBe(0);
* expect(null).not.toBeNull();
* expect(undefined).not.toBeNull();
*
* @example
* expect(42).toEqual(42); // will pass
Expand Down Expand Up @@ -2005,7 +2005,7 @@ declare module "bun:test" {
* expect(100 + 23).toBe(123);
* expect("d" + "og").toBe("dog");
* expect([123]).toBe([123]); // fail, use toEqual()
* expect(3 + 0.14).toBe(3.14); // fail, use toBeCloseTo()
* expect(0.1 + 0.2).toBe(0.3); // fail, use toBeCloseTo()
*
* // TypeScript errors:
* expect("hello").toBe(3.14); // typescript error + fail
Expand Down Expand Up @@ -2526,7 +2526,7 @@ declare module "bun:test" {
* expect({ c: new Date() }).toMatchInlineSnapshot({ c: expect.any(Date) });
* expect({ c: new Date() }).toMatchInlineSnapshot({ c: expect.any(Date) }, `
* {
* "v": Any<Date>,
* "c": Any<Date>,
* }
* `);
*
Expand Down Expand Up @@ -2797,7 +2797,7 @@ declare module "bun:test" {
*
* @example
* expect(new Date()).toBeValidDate();
* expect(new Date(null)).not.toBeValidDate();
* expect(new Date("invalid")).not.toBeValidDate();
* expect("2020-03-01").not.toBeValidDate();
*/
toBeValidDate(): R;
Expand Down
Loading