bun-types 1.3.2-canary.20251106T140813 → 1.3.2-canary.20251108T140624
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,411 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Plugins
|
|
3
|
+
description: Universal plugin API for extending Bun's runtime and bundler
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
Bun provides a universal plugin API that can be used to extend both the runtime and bundler.
|
|
7
|
+
|
|
8
|
+
Plugins intercept imports and perform custom loading logic: reading files, transpiling code, etc. They can be used to add support for additional file types, like `.scss` or `.yaml`. In the context of Bun's bundler, plugins can be used to implement framework-level features like CSS extraction, macros, and client-server code co-location.
|
|
9
|
+
|
|
10
|
+
## Lifecycle hooks
|
|
11
|
+
|
|
12
|
+
Plugins can register callbacks to be run at various points in the lifecycle of a bundle:
|
|
13
|
+
|
|
14
|
+
- `onStart()`: Run once the bundler has started a bundle
|
|
15
|
+
- `onResolve()`: Run before a module is resolved
|
|
16
|
+
- `onLoad()`: Run before a module is loaded
|
|
17
|
+
- `onBeforeParse()`: Run zero-copy native addons in the parser thread before a file is parsed
|
|
18
|
+
|
|
19
|
+
## Reference
|
|
20
|
+
|
|
21
|
+
A rough overview of the types (please refer to Bun's `bun.d.ts` for the full type definitions):
|
|
22
|
+
|
|
23
|
+
```ts title="bun.d.ts" icon="/icons/typescript.svg"
|
|
24
|
+
type PluginBuilder = {
|
|
25
|
+
onStart(callback: () => void): void;
|
|
26
|
+
onResolve: (
|
|
27
|
+
args: { filter: RegExp; namespace?: string },
|
|
28
|
+
callback: (args: { path: string; importer: string }) => {
|
|
29
|
+
path: string;
|
|
30
|
+
namespace?: string;
|
|
31
|
+
} | void,
|
|
32
|
+
) => void;
|
|
33
|
+
onLoad: (
|
|
34
|
+
args: { filter: RegExp; namespace?: string },
|
|
35
|
+
defer: () => Promise<void>,
|
|
36
|
+
callback: (args: { path: string }) => {
|
|
37
|
+
loader?: Loader;
|
|
38
|
+
contents?: string;
|
|
39
|
+
exports?: Record<string, any>;
|
|
40
|
+
},
|
|
41
|
+
) => void;
|
|
42
|
+
config: BuildConfig;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
type Loader = "js" | "jsx" | "ts" | "tsx" | "css" | "json" | "toml";
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
A plugin is defined as simple JavaScript object containing a `name` property and a `setup` function.
|
|
51
|
+
|
|
52
|
+
```ts title="myPlugin.ts" icon="/icons/typescript.svg"
|
|
53
|
+
import type { BunPlugin } from "bun";
|
|
54
|
+
|
|
55
|
+
const myPlugin: BunPlugin = {
|
|
56
|
+
name: "Custom loader",
|
|
57
|
+
setup(build) {
|
|
58
|
+
// implementation
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
This plugin can be passed into the `plugins` array when calling `Bun.build`.
|
|
64
|
+
|
|
65
|
+
```ts title="index.ts" icon="/icons/typescript.svg"
|
|
66
|
+
await Bun.build({
|
|
67
|
+
entrypoints: ["./app.ts"],
|
|
68
|
+
outdir: "./out",
|
|
69
|
+
plugins: [myPlugin],
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Plugin lifecycle
|
|
74
|
+
|
|
75
|
+
### Namespaces
|
|
76
|
+
|
|
77
|
+
`onLoad` and `onResolve` accept an optional `namespace` string. What is a namespace?
|
|
78
|
+
|
|
79
|
+
Every module has a namespace. Namespaces are used to prefix the import in transpiled code; for instance, a loader with a `filter: /\.yaml$/` and `namespace: "yaml:"` will transform an import from `./myfile.yaml` into `yaml:./myfile.yaml`.
|
|
80
|
+
|
|
81
|
+
The default namespace is `"file"` and it is not necessary to specify it, for instance: `import myModule from "./my-module.ts"` is the same as `import myModule from "file:./my-module.ts"`.
|
|
82
|
+
|
|
83
|
+
Other common namespaces are:
|
|
84
|
+
|
|
85
|
+
- `"bun"`: for Bun-specific modules (e.g. `"bun:test"`, `"bun:sqlite"`)
|
|
86
|
+
- `"node"`: for Node.js modules (e.g. `"node:fs"`, `"node:path"`)
|
|
87
|
+
|
|
88
|
+
### onStart
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
onStart(callback: () => void): Promise<void> | void;
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Registers a callback to be run when the bundler starts a new bundle.
|
|
95
|
+
|
|
96
|
+
```ts title="index.ts" icon="/icons/typescript.svg"
|
|
97
|
+
import { plugin } from "bun";
|
|
98
|
+
|
|
99
|
+
plugin({
|
|
100
|
+
name: "onStart example",
|
|
101
|
+
|
|
102
|
+
setup(build) {
|
|
103
|
+
build.onStart(() => {
|
|
104
|
+
console.log("Bundle started!");
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
The callback can return a Promise. After the bundle process has initialized, the bundler waits until all `onStart()` callbacks have completed before continuing.
|
|
111
|
+
|
|
112
|
+
For example:
|
|
113
|
+
|
|
114
|
+
```ts title="index.ts" icon="/icons/typescript.svg"
|
|
115
|
+
const result = await Bun.build({
|
|
116
|
+
entrypoints: ["./app.ts"],
|
|
117
|
+
outdir: "./dist",
|
|
118
|
+
sourcemap: "external",
|
|
119
|
+
plugins: [
|
|
120
|
+
{
|
|
121
|
+
name: "Sleep for 10 seconds",
|
|
122
|
+
setup(build) {
|
|
123
|
+
build.onStart(async () => {
|
|
124
|
+
await Bun.sleep(10_000);
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
name: "Log bundle time to a file",
|
|
130
|
+
setup(build) {
|
|
131
|
+
build.onStart(async () => {
|
|
132
|
+
const now = Date.now();
|
|
133
|
+
await Bun.$`echo ${now} > bundle-time.txt`;
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
],
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
In the above example, Bun will wait until the first `onStart()` (sleeping for 10 seconds) has completed, as well as the second `onStart()` (writing the bundle time to a file).
|
|
142
|
+
|
|
143
|
+
<Note>
|
|
144
|
+
`onStart()` callbacks (like every other lifecycle callback) do not have the ability to modify the `build.config`
|
|
145
|
+
object. If you want to mutate `build.config`, you must do so directly in the `setup()` function.
|
|
146
|
+
</Note>
|
|
147
|
+
|
|
148
|
+
### onResolve
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
onResolve(
|
|
152
|
+
args: { filter: RegExp; namespace?: string },
|
|
153
|
+
callback: (args: { path: string; importer: string }) => {
|
|
154
|
+
path: string;
|
|
155
|
+
namespace?: string;
|
|
156
|
+
} | void,
|
|
157
|
+
): void;
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
To bundle your project, Bun walks down the dependency tree of all modules in your project. For each imported module, Bun actually has to find and read that module. The "finding" part is known as "resolving" a module.
|
|
161
|
+
|
|
162
|
+
The `onResolve()` plugin lifecycle callback allows you to configure how a module is resolved.
|
|
163
|
+
|
|
164
|
+
The first argument to `onResolve()` is an object with a `filter` and `namespace` property. The `filter` is a regular expression which is run on the import string. Effectively, these allow you to filter which modules your custom resolution logic will apply to.
|
|
165
|
+
|
|
166
|
+
The second argument to `onResolve()` is a callback which is run for each module import Bun finds that matches the filter and namespace defined in the first argument.
|
|
167
|
+
|
|
168
|
+
The callback receives as input the path to the matching module. The callback can return a new path for the module. Bun will read the contents of the new path and parse it as a module.
|
|
169
|
+
|
|
170
|
+
For example, redirecting all imports to `images/` to `./public/images/`:
|
|
171
|
+
|
|
172
|
+
```ts title="index.ts" icon="/icons/typescript.svg"
|
|
173
|
+
import { plugin } from "bun";
|
|
174
|
+
|
|
175
|
+
plugin({
|
|
176
|
+
name: "onResolve example",
|
|
177
|
+
setup(build) {
|
|
178
|
+
build.onResolve({ filter: /.*/, namespace: "file" }, args => {
|
|
179
|
+
if (args.path.startsWith("images/")) {
|
|
180
|
+
return {
|
|
181
|
+
path: args.path.replace("images/", "./public/images/"),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### onLoad
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
onLoad(
|
|
193
|
+
args: { filter: RegExp; namespace?: string },
|
|
194
|
+
defer: () => Promise<void>,
|
|
195
|
+
callback: (args: { path: string, importer: string, namespace: string, kind: ImportKind }) => {
|
|
196
|
+
loader?: Loader;
|
|
197
|
+
contents?: string;
|
|
198
|
+
exports?: Record<string, any>;
|
|
199
|
+
},
|
|
200
|
+
): void;
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
After Bun's bundler has resolved a module, it needs to read the contents of the module and parse it.
|
|
204
|
+
|
|
205
|
+
The `onLoad()` plugin lifecycle callback allows you to modify the contents of a module before it is read and parsed by Bun.
|
|
206
|
+
|
|
207
|
+
Like `onResolve()`, the first argument to `onLoad()` allows you to filter which modules this invocation of `onLoad()` will apply to.
|
|
208
|
+
|
|
209
|
+
The second argument to `onLoad()` is a callback which is run for each matching module before Bun loads the contents of the module into memory.
|
|
210
|
+
|
|
211
|
+
This callback receives as input the path to the matching module, the importer of the module (the module that imported the module), the namespace of the module, and the kind of the module.
|
|
212
|
+
|
|
213
|
+
The callback can return a new `contents` string for the module as well as a new `loader`.
|
|
214
|
+
|
|
215
|
+
For example:
|
|
216
|
+
|
|
217
|
+
```ts title="index.ts" icon="/icons/typescript.svg"
|
|
218
|
+
import { plugin } from "bun";
|
|
219
|
+
|
|
220
|
+
const envPlugin: BunPlugin = {
|
|
221
|
+
name: "env plugin",
|
|
222
|
+
setup(build) {
|
|
223
|
+
build.onLoad({ filter: /env/, namespace: "file" }, args => {
|
|
224
|
+
return {
|
|
225
|
+
contents: `export default ${JSON.stringify(process.env)}`,
|
|
226
|
+
loader: "js",
|
|
227
|
+
};
|
|
228
|
+
});
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
Bun.build({
|
|
233
|
+
entrypoints: ["./app.ts"],
|
|
234
|
+
outdir: "./dist",
|
|
235
|
+
plugins: [envPlugin],
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
// import env from "env"
|
|
239
|
+
// env.FOO === "bar"
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
This plugin will transform all imports of the form `import env from "env"` into a JavaScript module that exports the current environment variables.
|
|
243
|
+
|
|
244
|
+
#### .defer()
|
|
245
|
+
|
|
246
|
+
One of the arguments passed to the `onLoad` callback is a `defer` function. This function returns a Promise that is resolved when all other modules have been loaded.
|
|
247
|
+
|
|
248
|
+
This allows you to delay execution of the `onLoad` callback until all other modules have been loaded.
|
|
249
|
+
|
|
250
|
+
This is useful for returning contents of a module that depends on other modules.
|
|
251
|
+
|
|
252
|
+
<Accordion title="Example: tracking and reporting unused exports">
|
|
253
|
+
|
|
254
|
+
```ts title="index.ts" icon="/icons/typescript.svg"
|
|
255
|
+
import { plugin } from "bun";
|
|
256
|
+
|
|
257
|
+
plugin({
|
|
258
|
+
name: "track imports",
|
|
259
|
+
setup(build) {
|
|
260
|
+
const transpiler = new Bun.Transpiler();
|
|
261
|
+
|
|
262
|
+
let trackedImports: Record<string, number> = {};
|
|
263
|
+
|
|
264
|
+
// Each module that goes through this onLoad callback
|
|
265
|
+
// will record its imports in `trackedImports`
|
|
266
|
+
build.onLoad({ filter: /\.ts/ }, async ({ path }) => {
|
|
267
|
+
const contents = await Bun.file(path).arrayBuffer();
|
|
268
|
+
|
|
269
|
+
const imports = transpiler.scanImports(contents);
|
|
270
|
+
|
|
271
|
+
for (const i of imports) {
|
|
272
|
+
trackedImports[i.path] = (trackedImports[i.path] || 0) + 1;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return undefined;
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
build.onLoad({ filter: /stats\.json/ }, async ({ defer }) => {
|
|
279
|
+
// Wait for all files to be loaded, ensuring
|
|
280
|
+
// that every file goes through the above `onLoad()` function
|
|
281
|
+
// and their imports tracked
|
|
282
|
+
await defer();
|
|
283
|
+
|
|
284
|
+
// Emit JSON containing the stats of each import
|
|
285
|
+
return {
|
|
286
|
+
contents: `export default ${JSON.stringify(trackedImports)}`,
|
|
287
|
+
loader: "json",
|
|
288
|
+
};
|
|
289
|
+
});
|
|
290
|
+
},
|
|
291
|
+
});
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
</Accordion>
|
|
295
|
+
|
|
296
|
+
<Warning>
|
|
297
|
+
The `.defer()` function currently has the limitation that it can only be called once per `onLoad` callback.
|
|
298
|
+
</Warning>
|
|
299
|
+
|
|
300
|
+
## Native plugins
|
|
301
|
+
|
|
302
|
+
One of the reasons why Bun's bundler is so fast is that it is written in native code and leverages multi-threading to load and parse modules in parallel.
|
|
303
|
+
|
|
304
|
+
However, one limitation of plugins written in JavaScript is that JavaScript itself is single-threaded.
|
|
305
|
+
|
|
306
|
+
Native plugins are written as NAPI modules and can be run on multiple threads. This allows native plugins to run much faster than JavaScript plugins.
|
|
307
|
+
|
|
308
|
+
In addition, native plugins can skip unnecessary work such as the UTF-8 -> UTF-16 conversion needed to pass strings to JavaScript.
|
|
309
|
+
|
|
310
|
+
These are the following lifecycle hooks which are available to native plugins:
|
|
311
|
+
|
|
312
|
+
- `onBeforeParse()`: Called on any thread before a file is parsed by Bun's bundler.
|
|
313
|
+
|
|
314
|
+
Native plugins are NAPI modules which expose lifecycle hooks as C ABI functions.
|
|
315
|
+
|
|
316
|
+
To create a native plugin, you must export a C ABI function which matches the signature of the native lifecycle hook you want to implement.
|
|
317
|
+
|
|
318
|
+
### Creating a native plugin in Rust
|
|
319
|
+
|
|
320
|
+
Native plugins are NAPI modules which expose lifecycle hooks as C ABI functions.
|
|
321
|
+
|
|
322
|
+
To create a native plugin, you must export a C ABI function which matches the signature of the native lifecycle hook you want to implement.
|
|
323
|
+
|
|
324
|
+
```bash terminal icon="terminal"
|
|
325
|
+
bun add -g @napi-rs/cli
|
|
326
|
+
napi new
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
Then install this crate:
|
|
330
|
+
|
|
331
|
+
```bash terminal icon="terminal"
|
|
332
|
+
cargo add bun-native-plugin
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
Now, inside the `lib.rs` file, we'll use the `bun_native_plugin::bun` proc macro to define a function which will implement our native plugin.
|
|
336
|
+
|
|
337
|
+
Here's an example implementing the `onBeforeParse` hook:
|
|
338
|
+
|
|
339
|
+
```rust title="lib.rs" icon="/icons/rust.svg"
|
|
340
|
+
use bun_native_plugin::{define_bun_plugin, OnBeforeParse, bun, Result, anyhow, BunLoader};
|
|
341
|
+
use napi_derive::napi;
|
|
342
|
+
|
|
343
|
+
/// Define the plugin and its name
|
|
344
|
+
define_bun_plugin!("replace-foo-with-bar");
|
|
345
|
+
|
|
346
|
+
/// Here we'll implement `onBeforeParse` with code that replaces all occurrences of
|
|
347
|
+
/// `foo` with `bar`.
|
|
348
|
+
///
|
|
349
|
+
/// We use the #[bun] macro to generate some of the boilerplate code.
|
|
350
|
+
///
|
|
351
|
+
/// The argument of the function (`handle: &mut OnBeforeParse`) tells
|
|
352
|
+
/// the macro that this function implements the `onBeforeParse` hook.
|
|
353
|
+
#[bun]
|
|
354
|
+
pub fn replace_foo_with_bar(handle: &mut OnBeforeParse) -> Result<()> {
|
|
355
|
+
// Fetch the input source code.
|
|
356
|
+
let input_source_code = handle.input_source_code()?;
|
|
357
|
+
|
|
358
|
+
// Get the Loader for the file
|
|
359
|
+
let loader = handle.output_loader();
|
|
360
|
+
|
|
361
|
+
let output_source_code = input_source_code.replace("foo", "bar");
|
|
362
|
+
|
|
363
|
+
handle.set_output_source_code(output_source_code, BunLoader::BUN_LOADER_JSX);
|
|
364
|
+
|
|
365
|
+
Ok(())
|
|
366
|
+
}
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
And to use it in `Bun.build()`:
|
|
370
|
+
|
|
371
|
+
```ts title="index.ts" icon="/icons/typescript.svg"
|
|
372
|
+
import myNativeAddon from "./my-native-addon";
|
|
373
|
+
|
|
374
|
+
Bun.build({
|
|
375
|
+
entrypoints: ["./app.tsx"],
|
|
376
|
+
plugins: [
|
|
377
|
+
{
|
|
378
|
+
name: "my-plugin",
|
|
379
|
+
|
|
380
|
+
setup(build) {
|
|
381
|
+
build.onBeforeParse(
|
|
382
|
+
{
|
|
383
|
+
namespace: "file",
|
|
384
|
+
filter: "**/*.tsx",
|
|
385
|
+
},
|
|
386
|
+
{
|
|
387
|
+
napiModule: myNativeAddon,
|
|
388
|
+
symbol: "replace_foo_with_bar",
|
|
389
|
+
// external: myNativeAddon.getSharedState()
|
|
390
|
+
},
|
|
391
|
+
);
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
],
|
|
395
|
+
});
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
### onBeforeParse
|
|
399
|
+
|
|
400
|
+
```ts
|
|
401
|
+
onBeforeParse(
|
|
402
|
+
args: { filter: RegExp; namespace?: string },
|
|
403
|
+
callback: { napiModule: NapiModule; symbol: string; external?: unknown },
|
|
404
|
+
): void;
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
This lifecycle callback is run immediately before a file is parsed by Bun's bundler.
|
|
408
|
+
|
|
409
|
+
As input, it receives the file's contents and can optionally return new source code.
|
|
410
|
+
|
|
411
|
+
<Info>This callback can be called from any thread and so the napi module implementation must be thread-safe.</Info>
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Feedback
|
|
3
|
+
description: Share feedback, bug reports, and feature requests
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
import Feedback from "/snippets/cli/feedback.mdx";
|
|
8
|
+
|
|
9
|
+
Whether you've found a bug, have a performance issue, or just want to suggest an improvement, here's how you can open a helpful issue:
|
|
10
|
+
|
|
11
|
+
<Callout icon="discord">
|
|
12
|
+
For general questions, please join our [Discord](https://discord.com/invite/CXdq2DP29u).
|
|
13
|
+
</Callout>
|
|
14
|
+
|
|
15
|
+
## Reporting Issues
|
|
16
|
+
|
|
17
|
+
<Steps>
|
|
18
|
+
<Step title="Upgrade Bun">
|
|
19
|
+
Try upgrading Bun to the latest version with `bun upgrade`. This might fix your problem without having to open an issue.
|
|
20
|
+
|
|
21
|
+
```bash terminal icon="terminal"
|
|
22
|
+
bun upgrade
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
You can also try the latest canary release, which includes the most recent changes and bug fixes that haven't been released in a stable version yet.
|
|
26
|
+
|
|
27
|
+
```bash terminal icon="terminal"
|
|
28
|
+
bun upgrade --canary
|
|
29
|
+
|
|
30
|
+
# To revert back to the stable
|
|
31
|
+
bun upgrade --stable
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
If the issue still persists after upgrading, continue to the next step.
|
|
35
|
+
</Step>
|
|
36
|
+
<Step title="Review Existing Issues">
|
|
37
|
+
First take a minute to check if the issue has already been reported. Don't open a new issue if it has already been reported, it saves time for everyone and helps us focus on fixing things faster.
|
|
38
|
+
|
|
39
|
+
- 🔍 [**Search existing issues**](https://github.com/oven-sh/bun/issues)
|
|
40
|
+
- 💬 [**Check discussions**](https://github.com/oven-sh/bun/discussions)
|
|
41
|
+
|
|
42
|
+
If you find a related issue, add a 👍 reaction or comment with extra details instead of opening a new one.
|
|
43
|
+
</Step>
|
|
44
|
+
<Step title="Report the Issue">
|
|
45
|
+
If no one has reported the issue, please open a new issue or suggest an improvement.
|
|
46
|
+
|
|
47
|
+
- 🐞 [**Report a Bug**](https://github.com/oven-sh/bun/issues/new?template=2-bug-report.yml)
|
|
48
|
+
- ⚡ [**Suggest an Improvement**](https://github.com/oven-sh/bun/issues/new?template=4-feature-request.yml)
|
|
49
|
+
|
|
50
|
+
Please provide as much detail as possible, including:
|
|
51
|
+
- A clear and concise title
|
|
52
|
+
- A code example or steps to reproduce the issue
|
|
53
|
+
- The version of Bun you are using (run `bun --version`)
|
|
54
|
+
- A detailed description of the issue (what happened, what you expected to happen, and what actually happened)
|
|
55
|
+
- The operating system and version you are using
|
|
56
|
+
<Note>
|
|
57
|
+
- For MacOS and Linux: copy the output of `uname -mprs`
|
|
58
|
+
- For Windows: copy the output of this command in the powershell console:
|
|
59
|
+
```powershell
|
|
60
|
+
"$([Environment]::OSVersion | ForEach-Object VersionString) $(if ([Environment]::Is64BitOperatingSystem) { "x64" } else { "x86" })"
|
|
61
|
+
```
|
|
62
|
+
</Note>
|
|
63
|
+
</Step>
|
|
64
|
+
|
|
65
|
+
</Steps>
|
|
66
|
+
|
|
67
|
+
The Bun team will review the issue and get back to you as soon as possible!
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## Use `bun feedback`
|
|
72
|
+
|
|
73
|
+
Alternatively, you can use `bun feedback` to share feedback, bug reports, and feature requests directly with the Bun team.
|
|
74
|
+
|
|
75
|
+
```bash terminal icon="terminal"
|
|
76
|
+
bun feedback "Love the new release!"
|
|
77
|
+
bun feedback report.txt details.log
|
|
78
|
+
echo "please document X" | bun feedback --email you@example.com
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
You can provide feedback as text arguments, file paths, or piped input.
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
<Feedback />
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Convert an ArrayBuffer to an array of numbers
|
|
3
|
+
sidebarTitle: "ArrayBuffer to Array"
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
To retrieve the contents of an `ArrayBuffer` as an array of numbers, create a [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) over of the buffer. and use the [`Array.from()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from) method to convert it to an array.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const buf = new ArrayBuffer(64);
|
|
11
|
+
const arr = new Uint8Array(buf);
|
|
12
|
+
arr.length; // 64
|
|
13
|
+
arr[0]; // 0 (instantiated with all zeros)
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
The `Uint8Array` class supports array indexing and iteration. However if you wish to convert the instance to a regular `Array`, use `Array.from()`. (This will likely be slower than using the `Uint8Array` directly.)
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
const buf = new ArrayBuffer(64);
|
|
22
|
+
const uintArr = new Uint8Array(buf);
|
|
23
|
+
const regularArr = Array.from(uintArr);
|
|
24
|
+
// number[]
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
See [Docs > API > Binary Data](https://bun.com/docs/api/binary-data#conversion) for complete documentation on manipulating binary data with Bun.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Convert an ArrayBuffer to a Blob
|
|
3
|
+
sidebarTitle: "ArrayBuffer to Blob"
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
A [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) can be constructed from an array of "chunks", where each chunk is a string, binary data structure, or another `Blob`.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const buf = new ArrayBuffer(64);
|
|
11
|
+
const blob = new Blob([buf]);
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
By default the `type` of the resulting `Blob` will be unset. This can be set manually.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
const buf = new ArrayBuffer(64);
|
|
20
|
+
const blob = new Blob([buf], { type: "application/octet-stream" });
|
|
21
|
+
blob.type; // => "application/octet-stream"
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
See [Docs > API > Binary Data](https://bun.com/docs/api/binary-data#conversion) for complete documentation on manipulating binary data with Bun.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Convert an ArrayBuffer to a Buffer
|
|
3
|
+
sidebarTitle: "ArrayBuffer to Buffer"
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
The Node.js [`Buffer`](https://nodejs.org/api/buffer.html) API predates the introduction of `ArrayBuffer` into the JavaScript language. Bun implements both.
|
|
8
|
+
|
|
9
|
+
Use the static `Buffer.from()` method to create a `Buffer` from an `ArrayBuffer`.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
const arrBuffer = new ArrayBuffer(64);
|
|
13
|
+
const nodeBuffer = Buffer.from(arrBuffer);
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
To create a `Buffer` that only views a portion of the underlying buffer, pass the offset and length to the constructor.
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
const arrBuffer = new ArrayBuffer(64);
|
|
22
|
+
const nodeBuffer = Buffer.from(arrBuffer, 0, 16); // view first 16 bytes
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
See [Docs > API > Binary Data](https://bun.com/docs/api/binary-data#conversion) for complete documentation on manipulating binary data with Bun.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Convert an ArrayBuffer to a string
|
|
3
|
+
sidebarTitle: "ArrayBuffer to string"
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Bun implements the Web-standard [`TextDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder) class for converting between binary data types and strings.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const buf = new ArrayBuffer(64);
|
|
11
|
+
const decoder = new TextDecoder();
|
|
12
|
+
const str = decoder.decode(buf);
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
See [Docs > API > Binary Data](https://bun.com/docs/api/binary-data#conversion) for complete documentation on manipulating binary data with Bun.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Convert an ArrayBuffer to a Uint8Array
|
|
3
|
+
sidebarTitle: "ArrayBuffer to Uint8Array"
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
A `Uint8Array` is a _typed array_, meaning it is a mechanism for viewing the data in an underlying `ArrayBuffer`.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const buffer = new ArrayBuffer(64);
|
|
11
|
+
const arr = new Uint8Array(buffer);
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
Instances of other typed arrays can be created similarly.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
const buffer = new ArrayBuffer(64);
|
|
20
|
+
|
|
21
|
+
const arr1 = new Uint8Array(buffer);
|
|
22
|
+
const arr2 = new Uint16Array(buffer);
|
|
23
|
+
const arr3 = new Uint32Array(buffer);
|
|
24
|
+
const arr4 = new Float32Array(buffer);
|
|
25
|
+
const arr5 = new Float64Array(buffer);
|
|
26
|
+
const arr6 = new BigInt64Array(buffer);
|
|
27
|
+
const arr7 = new BigUint64Array(buffer);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
To create a typed array that only views a portion of the underlying buffer, pass the offset and length to the constructor.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
const buffer = new ArrayBuffer(64);
|
|
36
|
+
const arr = new Uint8Array(buffer, 0, 16); // view first 16 bytes
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
---
|
|
40
|
+
|
|
41
|
+
See [Docs > API > Utils](https://bun.com/docs/api/utils) for more useful utilities.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Convert a Blob to an ArrayBuffer
|
|
3
|
+
sidebarTitle: "Blob to ArrayBuffer"
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
The [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) class provides a number of methods for consuming its contents in different formats, including `.arrayBuffer()`.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const blob = new Blob(["hello world"]);
|
|
11
|
+
const buf = await blob.arrayBuffer();
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
See [Docs > API > Binary Data](https://bun.com/docs/api/binary-data#conversion) for complete documentation on manipulating binary data with Bun.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Convert a Blob to a DataView
|
|
3
|
+
sidebarTitle: "Blob to DataView"
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
The [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) class provides a number of methods for consuming its contents in different formats. This snippets reads the contents to an `ArrayBuffer`, then creates a `DataView` from the buffer.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const blob = new Blob(["hello world"]);
|
|
11
|
+
const arr = new DataView(await blob.arrayBuffer());
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
See [Docs > API > Binary Data](https://bun.com/docs/api/binary-data#conversion) for complete documentation on manipulating binary data with Bun.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Convert a Blob to a ReadableStream
|
|
3
|
+
sidebarTitle: "Blob to ReadableStream"
|
|
4
|
+
mode: center
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
The [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) class provides a number of methods for consuming its contents in different formats, including `.stream()`. This returns `Promise<ReadableStream>`.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const blob = new Blob(["hello world"]);
|
|
11
|
+
const stream = await blob.stream();
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
See [Docs > API > Binary Data](https://bun.com/docs/api/binary-data#conversion) for complete documentation on manipulating binary data with Bun.
|