bun-types 1.3.4 → 1.3.5-canary.20251209T140747
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 +12 -18
- package/docs/bundler/executables.mdx +194 -19
- package/docs/guides/test/mock-functions.mdx +1 -1
- package/docs/runtime/http/server.mdx +4 -2
- package/docs/test/mocks.mdx +12 -12
- package/package.json +1 -1
- package/s3.d.ts +18 -0
package/bun.d.ts
CHANGED
|
@@ -1740,9 +1740,9 @@ declare module "bun" {
|
|
|
1740
1740
|
* @default "esm"
|
|
1741
1741
|
*/
|
|
1742
1742
|
format?: /**
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1743
|
+
* ECMAScript Module format
|
|
1744
|
+
*/
|
|
1745
|
+
| "esm"
|
|
1746
1746
|
/**
|
|
1747
1747
|
* CommonJS format
|
|
1748
1748
|
* **Experimental**
|
|
@@ -3316,10 +3316,10 @@ declare module "bun" {
|
|
|
3316
3316
|
function color(
|
|
3317
3317
|
input: ColorInput,
|
|
3318
3318
|
outputFormat?: /**
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
|
|
3319
|
+
* True color ANSI color string, for use in terminals
|
|
3320
|
+
* @example \x1b[38;2;100;200;200m
|
|
3321
|
+
*/
|
|
3322
|
+
| "ansi"
|
|
3323
3323
|
| "ansi-16"
|
|
3324
3324
|
| "ansi-16m"
|
|
3325
3325
|
/**
|
|
@@ -5650,17 +5650,11 @@ declare module "bun" {
|
|
|
5650
5650
|
maxBuffer?: number;
|
|
5651
5651
|
}
|
|
5652
5652
|
|
|
5653
|
-
interface SpawnSyncOptions<In extends Writable, Out extends Readable, Err extends Readable>
|
|
5654
|
-
In,
|
|
5655
|
-
|
|
5656
|
-
|
|
5657
|
-
|
|
5658
|
-
|
|
5659
|
-
interface SpawnOptions<In extends Writable, Out extends Readable, Err extends Readable> extends BaseOptions<
|
|
5660
|
-
In,
|
|
5661
|
-
Out,
|
|
5662
|
-
Err
|
|
5663
|
-
> {
|
|
5653
|
+
interface SpawnSyncOptions<In extends Writable, Out extends Readable, Err extends Readable>
|
|
5654
|
+
extends BaseOptions<In, Out, Err> {}
|
|
5655
|
+
|
|
5656
|
+
interface SpawnOptions<In extends Writable, Out extends Readable, Err extends Readable>
|
|
5657
|
+
extends BaseOptions<In, Out, Err> {
|
|
5664
5658
|
/**
|
|
5665
5659
|
* If true, stdout and stderr pipes will not automatically start reading
|
|
5666
5660
|
* data. Reading will only begin when you access the `stdout` or `stderr`
|
|
@@ -183,9 +183,35 @@ console.log(process.execArgv); // ["--smol", "--user-agent=MyBot"]
|
|
|
183
183
|
|
|
184
184
|
---
|
|
185
185
|
|
|
186
|
-
##
|
|
186
|
+
## Automatic config loading
|
|
187
187
|
|
|
188
|
-
|
|
188
|
+
Standalone executables can automatically load configuration files from the directory where they are run. By default:
|
|
189
|
+
|
|
190
|
+
- **`tsconfig.json`** and **`package.json`** loading is **disabled** — these are typically only needed at development time, and the bundler already uses them when compiling
|
|
191
|
+
- **`.env`** and **`bunfig.toml`** loading is **enabled** — these often contain runtime configuration that may vary per deployment
|
|
192
|
+
|
|
193
|
+
<Note>
|
|
194
|
+
In a future version of Bun, `.env` and `bunfig.toml` may also be disabled by default for more deterministic behavior.
|
|
195
|
+
</Note>
|
|
196
|
+
|
|
197
|
+
### Enabling config loading at runtime
|
|
198
|
+
|
|
199
|
+
If your executable needs to read `tsconfig.json` or `package.json` at runtime, you can opt in with the new CLI flags:
|
|
200
|
+
|
|
201
|
+
```bash icon="terminal" terminal
|
|
202
|
+
# Enable runtime loading of tsconfig.json
|
|
203
|
+
bun build --compile --compile-autoload-tsconfig ./app.ts --outfile myapp
|
|
204
|
+
|
|
205
|
+
# Enable runtime loading of package.json
|
|
206
|
+
bun build --compile --compile-autoload-package-json ./app.ts --outfile myapp
|
|
207
|
+
|
|
208
|
+
# Enable both
|
|
209
|
+
bun build --compile --compile-autoload-tsconfig --compile-autoload-package-json ./app.ts --outfile myapp
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Disabling config loading at runtime
|
|
213
|
+
|
|
214
|
+
To disable `.env` or `bunfig.toml` loading for deterministic execution:
|
|
189
215
|
|
|
190
216
|
```bash icon="terminal" terminal
|
|
191
217
|
# Disable .env loading
|
|
@@ -194,16 +220,23 @@ bun build --compile --no-compile-autoload-dotenv ./app.ts --outfile myapp
|
|
|
194
220
|
# Disable bunfig.toml loading
|
|
195
221
|
bun build --compile --no-compile-autoload-bunfig ./app.ts --outfile myapp
|
|
196
222
|
|
|
197
|
-
# Disable
|
|
223
|
+
# Disable all config loading
|
|
198
224
|
bun build --compile --no-compile-autoload-dotenv --no-compile-autoload-bunfig ./app.ts --outfile myapp
|
|
199
225
|
```
|
|
200
226
|
|
|
201
|
-
|
|
227
|
+
### JavaScript API
|
|
228
|
+
|
|
229
|
+
You can also configure autoloading via the JavaScript API:
|
|
202
230
|
|
|
203
231
|
```ts
|
|
204
232
|
await Bun.build({
|
|
205
233
|
entrypoints: ["./app.ts"],
|
|
206
234
|
compile: {
|
|
235
|
+
// tsconfig.json and package.json are disabled by default
|
|
236
|
+
autoloadTsconfig: true, // Enable tsconfig.json loading
|
|
237
|
+
autoloadPackageJson: true, // Enable package.json loading
|
|
238
|
+
|
|
239
|
+
// .env and bunfig.toml are enabled by default
|
|
207
240
|
autoloadDotenv: false, // Disable .env loading
|
|
208
241
|
autoloadBunfig: false, // Disable bunfig.toml loading
|
|
209
242
|
},
|
|
@@ -380,34 +413,141 @@ cd /home/me/Desktop
|
|
|
380
413
|
|
|
381
414
|
## Embed assets & files
|
|
382
415
|
|
|
383
|
-
Standalone executables support embedding files.
|
|
416
|
+
Standalone executables support embedding files directly into the binary. This lets you ship a single executable that contains images, JSON configs, templates, or any other assets your application needs.
|
|
417
|
+
|
|
418
|
+
### How it works
|
|
419
|
+
|
|
420
|
+
Use the `with { type: "file" }` [import attribute](https://github.com/tc39/proposal-import-attributes) to embed a file:
|
|
421
|
+
|
|
422
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
423
|
+
import icon from "./icon.png" with { type: "file" };
|
|
424
|
+
|
|
425
|
+
console.log(icon);
|
|
426
|
+
// During development: "./icon.png"
|
|
427
|
+
// After compilation: "$bunfs/icon-a1b2c3d4.png" (internal path)
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
The import returns a **path string** that points to the embedded file. At build time, Bun:
|
|
431
|
+
|
|
432
|
+
1. Reads the file contents
|
|
433
|
+
2. Embeds the data into the executable
|
|
434
|
+
3. Replaces the import with an internal path (prefixed with `$bunfs/`)
|
|
384
435
|
|
|
385
|
-
|
|
436
|
+
You can then read this embedded file using `Bun.file()` or Node.js `fs` APIs.
|
|
437
|
+
|
|
438
|
+
### Reading embedded files with Bun.file()
|
|
439
|
+
|
|
440
|
+
`Bun.file()` is the recommended way to read embedded files:
|
|
386
441
|
|
|
387
442
|
```ts index.ts icon="/icons/typescript.svg"
|
|
388
|
-
// this becomes an internal file path
|
|
389
443
|
import icon from "./icon.png" with { type: "file" };
|
|
390
444
|
import { file } from "bun";
|
|
391
445
|
|
|
446
|
+
// Get file contents as different types
|
|
447
|
+
const bytes = await file(icon).arrayBuffer(); // ArrayBuffer
|
|
448
|
+
const text = await file(icon).text(); // string (for text files)
|
|
449
|
+
const blob = file(icon); // Blob
|
|
450
|
+
|
|
451
|
+
// Stream the file in a response
|
|
392
452
|
export default {
|
|
393
453
|
fetch(req) {
|
|
394
|
-
|
|
395
|
-
|
|
454
|
+
return new Response(file(icon), {
|
|
455
|
+
headers: { "Content-Type": "image/png" },
|
|
456
|
+
});
|
|
396
457
|
},
|
|
397
458
|
};
|
|
398
459
|
```
|
|
399
460
|
|
|
400
|
-
|
|
461
|
+
### Reading embedded files with Node.js fs
|
|
401
462
|
|
|
402
|
-
|
|
463
|
+
Embedded files work seamlessly with Node.js file system APIs:
|
|
403
464
|
|
|
404
465
|
```ts index.ts icon="/icons/typescript.svg"
|
|
405
466
|
import icon from "./icon.png" with { type: "file" };
|
|
467
|
+
import config from "./config.json" with { type: "file" };
|
|
468
|
+
import { readFileSync, promises as fs } from "node:fs";
|
|
469
|
+
|
|
470
|
+
// Synchronous read
|
|
471
|
+
const iconBuffer = readFileSync(icon);
|
|
472
|
+
|
|
473
|
+
// Async read
|
|
474
|
+
const configData = await fs.readFile(config, "utf-8");
|
|
475
|
+
const parsed = JSON.parse(configData);
|
|
476
|
+
|
|
477
|
+
// Check file stats
|
|
478
|
+
const stats = await fs.stat(icon);
|
|
479
|
+
console.log(`Icon size: ${stats.size} bytes`);
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
### Practical examples
|
|
483
|
+
|
|
484
|
+
#### Embedding a JSON config file
|
|
485
|
+
|
|
486
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
487
|
+
import configPath from "./default-config.json" with { type: "file" };
|
|
406
488
|
import { file } from "bun";
|
|
407
489
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
490
|
+
// Load the embedded default configuration
|
|
491
|
+
const defaultConfig = await file(configPath).json();
|
|
492
|
+
|
|
493
|
+
// Merge with user config if it exists
|
|
494
|
+
const userConfig = await file("./user-config.json")
|
|
495
|
+
.json()
|
|
496
|
+
.catch(() => ({}));
|
|
497
|
+
const config = { ...defaultConfig, ...userConfig };
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
#### Serving static assets in an HTTP server
|
|
501
|
+
|
|
502
|
+
Use `static` routes in `Bun.serve()` for efficient static file serving:
|
|
503
|
+
|
|
504
|
+
```ts server.ts icon="/icons/typescript.svg"
|
|
505
|
+
import favicon from "./favicon.ico" with { type: "file" };
|
|
506
|
+
import logo from "./logo.png" with { type: "file" };
|
|
507
|
+
import styles from "./styles.css" with { type: "file" };
|
|
508
|
+
import { file, serve } from "bun";
|
|
509
|
+
|
|
510
|
+
serve({
|
|
511
|
+
static: {
|
|
512
|
+
"/favicon.ico": file(favicon),
|
|
513
|
+
"/logo.png": file(logo),
|
|
514
|
+
"/styles.css": file(styles),
|
|
515
|
+
},
|
|
516
|
+
fetch(req) {
|
|
517
|
+
return new Response("Not found", { status: 404 });
|
|
518
|
+
},
|
|
519
|
+
});
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
Bun automatically handles Content-Type headers and caching for static routes.
|
|
523
|
+
|
|
524
|
+
#### Embedding templates
|
|
525
|
+
|
|
526
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
527
|
+
import templatePath from "./email-template.html" with { type: "file" };
|
|
528
|
+
import { file } from "bun";
|
|
529
|
+
|
|
530
|
+
async function sendWelcomeEmail(user: { name: string; email: string }) {
|
|
531
|
+
const template = await file(templatePath).text();
|
|
532
|
+
const html = template.replace("{{name}}", user.name).replace("{{email}}", user.email);
|
|
533
|
+
|
|
534
|
+
// Send email with the rendered template...
|
|
535
|
+
}
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
#### Embedding binary files
|
|
539
|
+
|
|
540
|
+
```ts index.ts icon="/icons/typescript.svg"
|
|
541
|
+
import wasmPath from "./processor.wasm" with { type: "file" };
|
|
542
|
+
import fontPath from "./font.ttf" with { type: "file" };
|
|
543
|
+
import { file } from "bun";
|
|
544
|
+
|
|
545
|
+
// Load a WebAssembly module
|
|
546
|
+
const wasmBytes = await file(wasmPath).arrayBuffer();
|
|
547
|
+
const wasmModule = await WebAssembly.instantiate(wasmBytes);
|
|
548
|
+
|
|
549
|
+
// Read binary font data
|
|
550
|
+
const fontData = await file(fontPath).bytes();
|
|
411
551
|
```
|
|
412
552
|
|
|
413
553
|
### Embed SQLite databases
|
|
@@ -474,22 +614,57 @@ This is honestly a workaround, and we expect to improve this in the future with
|
|
|
474
614
|
|
|
475
615
|
### Listing embedded files
|
|
476
616
|
|
|
477
|
-
|
|
617
|
+
`Bun.embeddedFiles` gives you access to all embedded files as `Blob` objects:
|
|
478
618
|
|
|
479
619
|
```ts index.ts icon="/icons/typescript.svg"
|
|
480
620
|
import "./icon.png" with { type: "file" };
|
|
621
|
+
import "./data.json" with { type: "file" };
|
|
622
|
+
import "./template.html" with { type: "file" };
|
|
481
623
|
import { embeddedFiles } from "bun";
|
|
482
624
|
|
|
483
|
-
|
|
625
|
+
// List all embedded files
|
|
626
|
+
for (const blob of embeddedFiles) {
|
|
627
|
+
console.log(`${blob.name} - ${blob.size} bytes`);
|
|
628
|
+
}
|
|
629
|
+
// Output:
|
|
630
|
+
// icon-a1b2c3d4.png - 4096 bytes
|
|
631
|
+
// data-e5f6g7h8.json - 256 bytes
|
|
632
|
+
// template-i9j0k1l2.html - 1024 bytes
|
|
484
633
|
```
|
|
485
634
|
|
|
486
|
-
`Bun.embeddedFiles`
|
|
635
|
+
Each item in `Bun.embeddedFiles` is a `Blob` with a `name` property:
|
|
487
636
|
|
|
488
637
|
```ts
|
|
489
|
-
embeddedFiles: Blob
|
|
638
|
+
embeddedFiles: ReadonlyArray<Blob>;
|
|
639
|
+
```
|
|
640
|
+
|
|
641
|
+
This is useful for dynamically serving all embedded assets using `static` routes:
|
|
642
|
+
|
|
643
|
+
```ts server.ts icon="/icons/typescript.svg"
|
|
644
|
+
import "./public/favicon.ico" with { type: "file" };
|
|
645
|
+
import "./public/logo.png" with { type: "file" };
|
|
646
|
+
import "./public/styles.css" with { type: "file" };
|
|
647
|
+
import { embeddedFiles, serve } from "bun";
|
|
648
|
+
|
|
649
|
+
// Build static routes from all embedded files
|
|
650
|
+
const staticRoutes: Record<string, Blob> = {};
|
|
651
|
+
for (const blob of embeddedFiles) {
|
|
652
|
+
// Remove hash from filename: "icon-a1b2c3d4.png" -> "icon.png"
|
|
653
|
+
const name = blob.name.replace(/-[a-f0-9]+\./, ".");
|
|
654
|
+
staticRoutes[`/${name}`] = blob;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
serve({
|
|
658
|
+
static: staticRoutes,
|
|
659
|
+
fetch(req) {
|
|
660
|
+
return new Response("Not found", { status: 404 });
|
|
661
|
+
},
|
|
662
|
+
});
|
|
490
663
|
```
|
|
491
664
|
|
|
492
|
-
|
|
665
|
+
<Note>
|
|
666
|
+
`Bun.embeddedFiles` excludes bundled source code (`.ts`, `.js`, etc.) to help protect your application's source.
|
|
667
|
+
</Note>
|
|
493
668
|
|
|
494
669
|
#### Content hash
|
|
495
670
|
|
|
@@ -60,7 +60,7 @@ test("random", async () => {
|
|
|
60
60
|
|
|
61
61
|
expect(random).toHaveBeenCalled();
|
|
62
62
|
expect(random).toHaveBeenCalledTimes(3);
|
|
63
|
-
expect(random.mock.
|
|
63
|
+
expect(random.mock.calls).toEqual([[1], [2], [3]]);
|
|
64
64
|
expect(random.mock.results[0]).toEqual({ type: "return", value: a });
|
|
65
65
|
});
|
|
66
66
|
```
|
|
@@ -193,15 +193,17 @@ This is the maximum amount of time a connection is allowed to be idle before the
|
|
|
193
193
|
Thus far, the examples on this page have used the explicit `Bun.serve` API. Bun also supports an alternate syntax.
|
|
194
194
|
|
|
195
195
|
```ts server.ts
|
|
196
|
-
import {
|
|
196
|
+
import type { Serve } from "bun";
|
|
197
197
|
|
|
198
198
|
export default {
|
|
199
199
|
fetch(req) {
|
|
200
200
|
return new Response("Bun!");
|
|
201
201
|
},
|
|
202
|
-
} satisfies Serve
|
|
202
|
+
} satisfies Serve.Options<undefined>;
|
|
203
203
|
```
|
|
204
204
|
|
|
205
|
+
The type parameter `<undefined>` represents WebSocket data — if you add a `websocket` handler with custom data attached via `server.upgrade(req, { data: ... })`, replace `undefined` with your data type.
|
|
206
|
+
|
|
205
207
|
Instead of passing the server options into `Bun.serve`, `export default` it. This file can be executed as-is; when Bun sees a file with a `default` export containing a `fetch` handler, it passes it into `Bun.serve` under the hood.
|
|
206
208
|
|
|
207
209
|
---
|
package/docs/test/mocks.mdx
CHANGED
|
@@ -428,26 +428,26 @@ test("foo, bar, baz", () => {
|
|
|
428
428
|
const barSpy = spyOn(barModule, "bar");
|
|
429
429
|
const bazSpy = spyOn(bazModule, "baz");
|
|
430
430
|
|
|
431
|
-
// Original
|
|
432
|
-
expect(
|
|
433
|
-
expect(
|
|
434
|
-
expect(
|
|
431
|
+
// Original implementations still work
|
|
432
|
+
expect(fooModule.foo()).toBe("foo");
|
|
433
|
+
expect(barModule.bar()).toBe("bar");
|
|
434
|
+
expect(bazModule.baz()).toBe("baz");
|
|
435
435
|
|
|
436
436
|
// Mock implementations
|
|
437
437
|
fooSpy.mockImplementation(() => 42);
|
|
438
438
|
barSpy.mockImplementation(() => 43);
|
|
439
439
|
bazSpy.mockImplementation(() => 44);
|
|
440
440
|
|
|
441
|
-
expect(
|
|
442
|
-
expect(
|
|
443
|
-
expect(
|
|
441
|
+
expect(fooModule.foo()).toBe(42);
|
|
442
|
+
expect(barModule.bar()).toBe(43);
|
|
443
|
+
expect(bazModule.baz()).toBe(44);
|
|
444
444
|
|
|
445
445
|
// Restore all
|
|
446
446
|
mock.restore();
|
|
447
447
|
|
|
448
|
-
expect(
|
|
449
|
-
expect(
|
|
450
|
-
expect(
|
|
448
|
+
expect(fooModule.foo()).toBe("foo");
|
|
449
|
+
expect(barModule.bar()).toBe("bar");
|
|
450
|
+
expect(bazModule.baz()).toBe("baz");
|
|
451
451
|
});
|
|
452
452
|
```
|
|
453
453
|
|
|
@@ -455,10 +455,10 @@ Using `mock.restore()` can reduce the amount of code in your tests by adding it
|
|
|
455
455
|
|
|
456
456
|
## Vitest Compatibility
|
|
457
457
|
|
|
458
|
-
For added compatibility with tests written for Vitest, Bun provides the `vi`
|
|
458
|
+
For added compatibility with tests written for Vitest, Bun provides the `vi` object as an alias for parts of the Jest mocking API:
|
|
459
459
|
|
|
460
460
|
```ts title="test.ts" icon="/icons/typescript.svg"
|
|
461
|
-
import { test, expect } from "bun:test";
|
|
461
|
+
import { test, expect, vi } from "bun:test";
|
|
462
462
|
|
|
463
463
|
// Using the 'vi' alias similar to Vitest
|
|
464
464
|
test("vitest compatibility", () => {
|
package/package.json
CHANGED
package/s3.d.ts
CHANGED
|
@@ -281,6 +281,24 @@ declare module "bun" {
|
|
|
281
281
|
*/
|
|
282
282
|
type?: string;
|
|
283
283
|
|
|
284
|
+
/**
|
|
285
|
+
* The Content-Disposition header value.
|
|
286
|
+
* Controls how the file is presented when downloaded.
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* // Setting attachment disposition with filename
|
|
290
|
+
* const file = s3.file("report.pdf", {
|
|
291
|
+
* contentDisposition: "attachment; filename=\"quarterly-report.pdf\""
|
|
292
|
+
* });
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* // Setting inline disposition
|
|
296
|
+
* await s3.write("image.png", imageData, {
|
|
297
|
+
* contentDisposition: "inline"
|
|
298
|
+
* });
|
|
299
|
+
*/
|
|
300
|
+
contentDisposition?: string | undefined;
|
|
301
|
+
|
|
284
302
|
/**
|
|
285
303
|
* By default, Amazon S3 uses the STANDARD Storage Class to store newly created objects.
|
|
286
304
|
*
|