diff --git a/docs/bundler/index.mdx b/docs/bundler/index.mdx index b9a1837b9a10..3792dc2c05d0 100644 --- a/docs/bundler/index.mdx +++ b/docs/bundler/index.mdx @@ -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 + } } } ``` diff --git a/docs/runtime/child-process.mdx b/docs/runtime/child-process.mdx index c4774c34db7d..75fb3c4cc36a 100644 --- a/docs/runtime/child-process.mdx +++ b/docs/runtime/child-process.mdx @@ -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; diff --git a/docs/runtime/hashing.mdx b/docs/runtime/hashing.mdx index a9b0f7141791..024573224ddf 100644 --- a/docs/runtime/hashing.mdx +++ b/docs/runtime/hashing.mdx @@ -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); diff --git a/docs/runtime/networking/tcp.mdx b/docs/runtime/networking/tcp.mdx index b23eef720176..2c6f85058995 100644 --- a/docs/runtime/networking/tcp.mdx +++ b/docs/runtime/networking/tcp.mdx @@ -170,8 +170,10 @@ server.reload({ const socket = await Bun.connect({/* config */}); socket.reload({ - data() { - // new 'data' handler + socket: { + data() { + // new 'data' handler + }, }, }); ``` @@ -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, }); @@ -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 diff --git a/docs/runtime/yaml.mdx b/docs/runtime/yaml.mdx index 631d4ef41c38..9449a59b6cd5 100644 --- a/docs/runtime/yaml.mdx +++ b/docs/runtime/yaml.mdx @@ -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), ), ); } diff --git a/packages/bun-types/bun.d.ts b/packages/bun-types/bun.d.ts index 8279a41e75f7..5d6cd3a620b8 100644 --- a/packages/bun-types/bun.d.ts +++ b/packages/bun-types/bun.d.ts @@ -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"}' * ``` * @@ -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" } * ``` * @@ -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} @@ -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 { @@ -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(); * ``` @@ -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; * @@ -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 * } * ``` diff --git a/packages/bun-types/ffi.d.ts b/packages/bun-types/ffi.d.ts index 7af21e4dde7a..de36220d0c66 100644 --- a/packages/bun-types/ffi.d.ts +++ b/packages/bun-types/ffi.d.ts @@ -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", * } diff --git a/packages/bun-types/serve.d.ts b/packages/bun-types/serve.d.ts index 5cad6313351f..c1b2d47868bf 100644 --- a/packages/bun-types/serve.d.ts +++ b/packages/bun-types/serve.d.ts @@ -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[]; @@ -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), * }, * // ... * }); diff --git a/packages/bun-types/shell.d.ts b/packages/bun-types/shell.d.ts index 6f7d33e95973..8cc82e3585d9 100644 --- a/packages/bun-types/shell.d.ts +++ b/packages/bun-types/shell.d.ts @@ -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; @@ -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; @@ -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; @@ -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" * ``` */ text(encoding?: BufferEncoding): string; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/packages/bun-types/sqlite.d.ts b/packages/bun-types/sqlite.d.ts index 971ecddeba58..002f59bdadda 100644 --- a/packages/bun-types/sqlite.d.ts +++ b/packages/bun-types/sqlite.d.ts @@ -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>; diff --git a/packages/bun-types/test.d.ts b/packages/bun-types/test.d.ts index 5802792ca63a..3d26b062d2dd 100644 --- a/packages/bun-types/test.d.ts +++ b/packages/bun-types/test.d.ts @@ -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 @@ -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 @@ -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, + * "c": Any, * } * `); * @@ -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;