bun-types 1.3.2-canary.20251106T140813 → 1.3.2
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,109 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Workspaces"
|
|
3
|
+
description: "Develop complex monorepos with multiple independent packages"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
Bun supports [`workspaces`](https://docs.npmjs.com/cli/v9/using-npm/workspaces?v=true#description) in `package.json`. Workspaces make it easy to develop complex software as a _monorepo_ consisting of several independent packages.
|
|
7
|
+
|
|
8
|
+
It's common for a monorepo to have the following structure:
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
tree
|
|
12
|
+
<root>
|
|
13
|
+
├── README.md
|
|
14
|
+
├── bun.lock
|
|
15
|
+
├── package.json
|
|
16
|
+
├── tsconfig.json
|
|
17
|
+
└── packages
|
|
18
|
+
├── pkg-a
|
|
19
|
+
│ ├── index.ts
|
|
20
|
+
│ ├── package.json
|
|
21
|
+
│ └── tsconfig.json
|
|
22
|
+
├── pkg-b
|
|
23
|
+
│ ├── index.ts
|
|
24
|
+
│ ├── package.json
|
|
25
|
+
│ └── tsconfig.json
|
|
26
|
+
└── pkg-c
|
|
27
|
+
├── index.ts
|
|
28
|
+
├── package.json
|
|
29
|
+
└── tsconfig.json
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
In the root `package.json`, the `"workspaces"` key is used to indicate which subdirectories should be considered packages/workspaces within the monorepo. It's conventional to place all the workspace in a directory called `packages`.
|
|
33
|
+
|
|
34
|
+
```json
|
|
35
|
+
{
|
|
36
|
+
"name": "my-project",
|
|
37
|
+
"version": "1.0.0",
|
|
38
|
+
"workspaces": ["packages/*"],
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"example-package-in-monorepo": "workspace:*"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
<Note>
|
|
46
|
+
**Glob support** — Bun supports full glob syntax in `"workspaces"` (see [here](/runtime/glob#supported-glob-patterns)
|
|
47
|
+
for a comprehensive list of supported syntax), _except_ for exclusions (e.g. `!**/excluded/**`), which are not
|
|
48
|
+
implemented yet.
|
|
49
|
+
</Note>
|
|
50
|
+
|
|
51
|
+
Each workspace has it's own `package.json`. When referencing other packages in the monorepo, semver or workspace protocols (e.g. `workspace:*`) can be used as the version field in your `package.json`.
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{
|
|
55
|
+
"name": "pkg-a",
|
|
56
|
+
"version": "1.0.0",
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"pkg-b": "workspace:*"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`bun install` will install dependencies for all workspaces in the monorepo, de-duplicating packages if possible. If you only want to install dependencies for specific workspaces, you can use the `--filter` flag.
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
# Install dependencies for all workspaces starting with `pkg-` except for `pkg-c`
|
|
67
|
+
bun install --filter "pkg-*" --filter "!pkg-c"
|
|
68
|
+
|
|
69
|
+
# Paths can also be used. This is equivalent to the command above.
|
|
70
|
+
bun install --filter "./packages/pkg-*" --filter "!pkg-c" # or --filter "!./packages/pkg-c"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
When publishing, `workspace:` versions are replaced by the package's `package.json` version,
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
"workspace:*" -> "1.0.1"
|
|
77
|
+
"workspace:^" -> "^1.0.1"
|
|
78
|
+
"workspace:~" -> "~1.0.1"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Setting a specific version takes precedence over the package's `package.json` version,
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
"workspace:1.0.2" -> "1.0.2" // Even if current version is 1.0.1
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Workspaces have a couple major benefits.
|
|
88
|
+
|
|
89
|
+
- **Code can be split into logical parts.** If one package relies on another, you can simply add it as a dependency in `package.json`. If package `b` depends on `a`, `bun install` will install your local `packages/a` directory into `node_modules` instead of downloading it from the npm registry.
|
|
90
|
+
- **Dependencies can be de-duplicated.** If `a` and `b` share a common dependency, it will be _hoisted_ to the root `node_modules` directory. This reduces redundant disk usage and minimizes "dependency hell" issues associated with having multiple versions of a package installed simultaneously.
|
|
91
|
+
- **Run scripts in multiple packages.** You can use the [`--filter` flag](/pm/filter) to easily run `package.json` scripts in multiple packages in your workspace, , or `--workspaces` to run scripts across all workspaces.
|
|
92
|
+
|
|
93
|
+
## Share versions with Catalogs
|
|
94
|
+
|
|
95
|
+
When many packages need the same dependency versions, catalogs let you define
|
|
96
|
+
those versions once in the root `package.json` and reference them from your
|
|
97
|
+
workspaces using the `catalog:` protocol. Updating the catalog automatically
|
|
98
|
+
updates every package that references it. See
|
|
99
|
+
[Catalogs](/pm/catalogs) for details.
|
|
100
|
+
|
|
101
|
+
<Note>
|
|
102
|
+
⚡️ **Speed** — Installs are fast, even for big monorepos. Bun installs the [Remix](https://github.com/remix-run/remix) monorepo in about `500ms` on Linux.
|
|
103
|
+
|
|
104
|
+
- 28x faster than `npm install`
|
|
105
|
+
- 12x faster than `yarn install` (v1)
|
|
106
|
+
- 8x faster than `pnpm install`
|
|
107
|
+
|
|
108
|
+
<Image src="https://user-images.githubusercontent.com/709451/212829600-77df9544-7c9f-4d8d-a984-b2cd0fd2aa52.png" />
|
|
109
|
+
</Note>
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Benchmarking
|
|
3
|
+
description: How to benchmark Bun
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
Bun is designed for speed. Hot paths are extensively profiled and benchmarked. The source code for all of Bun's public benchmarks can be found in the [`/bench`](https://github.com/oven-sh/bun/tree/main/bench) directory of the Bun repo.
|
|
7
|
+
|
|
8
|
+
## Measuring time
|
|
9
|
+
|
|
10
|
+
To precisely measure time, Bun offers two runtime APIs functions:
|
|
11
|
+
|
|
12
|
+
1. The Web-standard [`performance.now()`](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now) function
|
|
13
|
+
2. `Bun.nanoseconds()` which is similar to `performance.now()` except it returns the current time since the application started in nanoseconds. You can use `performance.timeOrigin` to convert this to a Unix timestamp.
|
|
14
|
+
|
|
15
|
+
## Benchmarking tools
|
|
16
|
+
|
|
17
|
+
When writing your own benchmarks, it's important to choose the right tool.
|
|
18
|
+
|
|
19
|
+
- For microbenchmarks, a great general-purpose tool is [`mitata`](https://github.com/evanwashere/mitata).
|
|
20
|
+
- For load testing, you _must use_ an HTTP benchmarking tool that is at least as fast as `Bun.serve()`, or your results will be skewed. Some popular Node.js-based benchmarking tools like [`autocannon`](https://github.com/mcollina/autocannon) are not fast enough. We recommend one of the following:
|
|
21
|
+
- [`bombardier`](https://github.com/codesenberg/bombardier)
|
|
22
|
+
- [`oha`](https://github.com/hatoo/oha)
|
|
23
|
+
- [`http_load_test`](https://github.com/uNetworking/uSockets/blob/master/examples/http_load_test.c)
|
|
24
|
+
- For benchmarking scripts or CLI commands, we recommend [`hyperfine`](https://github.com/sharkdp/hyperfine).
|
|
25
|
+
|
|
26
|
+
## Measuring memory usage
|
|
27
|
+
|
|
28
|
+
Bun has two heaps. One heap is for the JavaScript runtime and the other heap is for everything else.
|
|
29
|
+
|
|
30
|
+
### JavaScript heap stats
|
|
31
|
+
|
|
32
|
+
The `bun:jsc` module exposes a few functions for measuring memory usage:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { heapStats } from "bun:jsc";
|
|
36
|
+
console.log(heapStats());
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
<Accordion title="View example statistics">
|
|
40
|
+
|
|
41
|
+
```ts expandable icon="/icons/typescript.svg"
|
|
42
|
+
{
|
|
43
|
+
heapSize: 1657575,
|
|
44
|
+
heapCapacity: 2872775,
|
|
45
|
+
extraMemorySize: 598199,
|
|
46
|
+
objectCount: 13790,
|
|
47
|
+
protectedObjectCount: 62,
|
|
48
|
+
globalObjectCount: 1,
|
|
49
|
+
protectedGlobalObjectCount: 1,
|
|
50
|
+
// A count of every object type in the heap
|
|
51
|
+
objectTypeCounts: {
|
|
52
|
+
CallbackObject: 25,
|
|
53
|
+
FunctionExecutable: 2078,
|
|
54
|
+
AsyncGeneratorFunction: 2,
|
|
55
|
+
'RegExp String Iterator': 1,
|
|
56
|
+
FunctionCodeBlock: 188,
|
|
57
|
+
ModuleProgramExecutable: 13,
|
|
58
|
+
String: 1,
|
|
59
|
+
UnlinkedModuleProgramCodeBlock: 13,
|
|
60
|
+
JSON: 1,
|
|
61
|
+
AsyncGenerator: 1,
|
|
62
|
+
Symbol: 1,
|
|
63
|
+
GetterSetter: 68,
|
|
64
|
+
ImportMeta: 10,
|
|
65
|
+
DOMAttributeGetterSetter: 1,
|
|
66
|
+
UnlinkedFunctionCodeBlock: 174,
|
|
67
|
+
RegExp: 52,
|
|
68
|
+
ModuleLoader: 1,
|
|
69
|
+
Intl: 1,
|
|
70
|
+
WeakMap: 4,
|
|
71
|
+
Generator: 2,
|
|
72
|
+
PropertyTable: 95,
|
|
73
|
+
'Array Iterator': 1,
|
|
74
|
+
JSLexicalEnvironment: 75,
|
|
75
|
+
UnlinkedFunctionExecutable: 2067,
|
|
76
|
+
WeakSet: 1,
|
|
77
|
+
console: 1,
|
|
78
|
+
Map: 23,
|
|
79
|
+
SparseArrayValueMap: 14,
|
|
80
|
+
StructureChain: 19,
|
|
81
|
+
Set: 18,
|
|
82
|
+
'String Iterator': 1,
|
|
83
|
+
FunctionRareData: 3,
|
|
84
|
+
JSGlobalLexicalEnvironment: 1,
|
|
85
|
+
Object: 481,
|
|
86
|
+
BigInt: 2,
|
|
87
|
+
StructureRareData: 55,
|
|
88
|
+
Array: 179,
|
|
89
|
+
AbortController: 2,
|
|
90
|
+
ModuleNamespaceObject: 11,
|
|
91
|
+
ShadowRealm: 1,
|
|
92
|
+
'Immutable Butterfly': 103,
|
|
93
|
+
Primordials: 1,
|
|
94
|
+
'Set Iterator': 1,
|
|
95
|
+
JSGlobalProxy: 1,
|
|
96
|
+
AsyncFromSyncIterator: 1,
|
|
97
|
+
ModuleRecord: 13,
|
|
98
|
+
FinalizationRegistry: 1,
|
|
99
|
+
AsyncIterator: 1,
|
|
100
|
+
InternalPromise: 22,
|
|
101
|
+
Iterator: 1,
|
|
102
|
+
CustomGetterSetter: 65,
|
|
103
|
+
Promise: 19,
|
|
104
|
+
WeakRef: 1,
|
|
105
|
+
InternalPromisePrototype: 1,
|
|
106
|
+
Function: 2381,
|
|
107
|
+
AsyncFunction: 2,
|
|
108
|
+
GlobalObject: 1,
|
|
109
|
+
ArrayBuffer: 2,
|
|
110
|
+
Boolean: 1,
|
|
111
|
+
Math: 1,
|
|
112
|
+
CallbackConstructor: 1,
|
|
113
|
+
Error: 2,
|
|
114
|
+
JSModuleEnvironment: 13,
|
|
115
|
+
WebAssembly: 1,
|
|
116
|
+
HashMapBucket: 300,
|
|
117
|
+
Callee: 3,
|
|
118
|
+
symbol: 37,
|
|
119
|
+
string: 2484,
|
|
120
|
+
Performance: 1,
|
|
121
|
+
ModuleProgramCodeBlock: 12,
|
|
122
|
+
JSSourceCode: 13,
|
|
123
|
+
JSPropertyNameEnumerator: 3,
|
|
124
|
+
NativeExecutable: 290,
|
|
125
|
+
Number: 1,
|
|
126
|
+
Structure: 1550,
|
|
127
|
+
SymbolTable: 108,
|
|
128
|
+
GeneratorFunction: 2,
|
|
129
|
+
'Map Iterator': 1
|
|
130
|
+
},
|
|
131
|
+
protectedObjectTypeCounts: {
|
|
132
|
+
CallbackConstructor: 1,
|
|
133
|
+
BigInt: 1,
|
|
134
|
+
RegExp: 2,
|
|
135
|
+
GlobalObject: 1,
|
|
136
|
+
UnlinkedModuleProgramCodeBlock: 13,
|
|
137
|
+
HashMapBucket: 2,
|
|
138
|
+
Structure: 41,
|
|
139
|
+
JSPropertyNameEnumerator: 1
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
</Accordion>
|
|
145
|
+
|
|
146
|
+
JavaScript is a garbage-collected language, not reference counted. It's normal and correct for objects to not be freed immediately in all cases, though it's not normal for objects to never be freed.
|
|
147
|
+
|
|
148
|
+
To force garbage collection to run manually:
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
Bun.gc(true); // synchronous
|
|
152
|
+
Bun.gc(false); // asynchronous
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Heap snapshots let you inspect what objects are not being freed. You can use the `bun:jsc` module to take a heap snapshot and then view it with Safari or WebKit GTK developer tools. To generate a heap snapshot:
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
import { generateHeapSnapshot } from "bun";
|
|
159
|
+
|
|
160
|
+
const snapshot = generateHeapSnapshot();
|
|
161
|
+
await Bun.write("heap.json", JSON.stringify(snapshot, null, 2));
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
To view the snapshot, open the `heap.json` file in Safari's Developer Tools (or WebKit GTK)
|
|
165
|
+
|
|
166
|
+
1. Open the Developer Tools
|
|
167
|
+
2. Click "Timeline"
|
|
168
|
+
3. Click "JavaScript Allocations" in the menu on the left. It might not be visible until you click the pencil icon to show all the timelines
|
|
169
|
+
4. Click "Import" and select your heap snapshot JSON
|
|
170
|
+
|
|
171
|
+
<Frame>
|
|
172
|
+
<img
|
|
173
|
+
src="https://user-images.githubusercontent.com/709451/204428943-ba999e8f-8984-4f23-97cb-b4e3e280363e.png"
|
|
174
|
+
alt="Importing a heap snapshot"
|
|
175
|
+
/>
|
|
176
|
+
</Frame>
|
|
177
|
+
|
|
178
|
+
Once imported, you should see something like this:
|
|
179
|
+
|
|
180
|
+
<Frame>
|
|
181
|
+
<img
|
|
182
|
+
alt="Viewing heap snapshot in Safari"
|
|
183
|
+
src="https://user-images.githubusercontent.com/709451/204429337-b0d8935f-3509-4071-b991-217794d1fb27.png"
|
|
184
|
+
caption="Viewing heap snapshot in Safari Dev Tools"
|
|
185
|
+
/>
|
|
186
|
+
</Frame>
|
|
187
|
+
|
|
188
|
+
> The [web debugger](/runtime/debugger#inspect) also offers the timeline feature which allows you to track and examine the memory usage of the running debug session.
|
|
189
|
+
|
|
190
|
+
### Native heap stats
|
|
191
|
+
|
|
192
|
+
Bun uses mimalloc for the other heap. To report a summary of non-JavaScript memory usage, set the `MIMALLOC_SHOW_STATS=1` environment variable. and stats will print on exit.
|
|
193
|
+
|
|
194
|
+
```sh terminal icon="terminal"
|
|
195
|
+
MIMALLOC_SHOW_STATS=1 bun script.js
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
```txt
|
|
199
|
+
heap stats: peak total freed current unit count
|
|
200
|
+
reserved: 64.0 MiB 64.0 MiB 0 64.0 MiB not all freed!
|
|
201
|
+
committed: 64.0 MiB 64.0 MiB 0 64.0 MiB not all freed!
|
|
202
|
+
reset: 0 0 0 0 ok
|
|
203
|
+
touched: 128.5 KiB 128.5 KiB 5.4 MiB -5.3 MiB ok
|
|
204
|
+
segments: 1 1 0 1 not all freed!
|
|
205
|
+
-abandoned: 0 0 0 0 ok
|
|
206
|
+
-cached: 0 0 0 0 ok
|
|
207
|
+
pages: 0 0 53 -53 ok
|
|
208
|
+
-abandoned: 0 0 0 0 ok
|
|
209
|
+
-extended: 0
|
|
210
|
+
-noretire: 0
|
|
211
|
+
mmaps: 0
|
|
212
|
+
commits: 0
|
|
213
|
+
threads: 0 0 0 0 ok
|
|
214
|
+
searches: 0.0 avg
|
|
215
|
+
numa nodes: 1
|
|
216
|
+
elapsed: 0.068 s
|
|
217
|
+
process: user: 0.061 s, system: 0.014 s, faults: 0, rss: 57.4 MiB, commit: 64.0 MiB
|
|
218
|
+
```
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Bindgen
|
|
3
|
+
description: Bindgen for Bun
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
<Note>This document is for maintainers and contributors to Bun, and describes internal implementation details.</Note>
|
|
7
|
+
|
|
8
|
+
The new bindings generator, introduced to the codebase in Dec 2024, scans for
|
|
9
|
+
`*.bind.ts` to find function and class definition, and generates glue code to
|
|
10
|
+
interop between JavaScript and native code.
|
|
11
|
+
|
|
12
|
+
There are currently other code generators and systems that achieve similar
|
|
13
|
+
purposes. The following will all eventually be completely phased out in favor of
|
|
14
|
+
this one:
|
|
15
|
+
|
|
16
|
+
- "Classes generator", converting `*.classes.ts` for custom classes.
|
|
17
|
+
- "JS2Native", allowing ad-hoc calls from `src/js` to native code.
|
|
18
|
+
|
|
19
|
+
## Creating JS Functions in Zig
|
|
20
|
+
|
|
21
|
+
Given a file implementing a simple function, such as `add`
|
|
22
|
+
|
|
23
|
+
```zig src/bun.js/math.zig
|
|
24
|
+
pub fn add(global: *jsc.JSGlobalObject, a: i32, b: i32) !i32 {
|
|
25
|
+
return std.math.add(i32, a, b) catch {
|
|
26
|
+
// Binding functions can return `error.OutOfMemory` and `error.JSError`.
|
|
27
|
+
// Others like `error.Overflow` from `std.math.add` must be converted.
|
|
28
|
+
// Remember to be descriptive.
|
|
29
|
+
return global.throwPretty("Integer overflow while adding", .{});
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const gen = bun.gen.math; // "math" being this file's basename
|
|
34
|
+
|
|
35
|
+
const std = @import("std");
|
|
36
|
+
const bun = @import("bun");
|
|
37
|
+
const jsc = bun.jsc;
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Then describe the API schema using a `.bind.ts` function. The binding file goes next to the Zig file.
|
|
41
|
+
|
|
42
|
+
```ts src/bun.js/math.bind.ts icon="/icons/typescript.svg"
|
|
43
|
+
import { t, fn } from "bindgen";
|
|
44
|
+
|
|
45
|
+
export const add = fn({
|
|
46
|
+
args: {
|
|
47
|
+
global: t.globalObject,
|
|
48
|
+
a: t.i32,
|
|
49
|
+
b: t.i32.default(1),
|
|
50
|
+
},
|
|
51
|
+
ret: t.i32,
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
This function declaration is equivalent to:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
/**
|
|
59
|
+
* Throws if zero arguments are provided.
|
|
60
|
+
* Wraps out of range numbers using modulo.
|
|
61
|
+
*/
|
|
62
|
+
declare function add(a: number, b: number = 1): number;
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The code generator will provide `bun.gen.math.jsAdd`, which is the native
|
|
66
|
+
function implementation. To pass to JavaScript, use
|
|
67
|
+
`bun.gen.math.createAddCallback(global)`. JS files in `src/js/` may use
|
|
68
|
+
`$bindgenFn("math.bind.ts", "add")` to get a handle to the implementation.
|
|
69
|
+
|
|
70
|
+
## Strings
|
|
71
|
+
|
|
72
|
+
The type for receiving strings is one of [`t.DOMString`](https://webidl.spec.whatwg.org/#idl-DOMString), [`t.ByteString`](https://webidl.spec.whatwg.org/#idl-ByteString), and [`t.USVString`](https://webidl.spec.whatwg.org/#idl-USVString). These map directly to their WebIDL counterparts, and have slightly different conversion logic. Bindgen will pass BunString to native code in all cases.
|
|
73
|
+
|
|
74
|
+
When in doubt, use DOMString.
|
|
75
|
+
|
|
76
|
+
`t.UTF8String` can be used in place of `t.DOMString`, but will call `bun.String.toUTF8`. The native callback gets `[]const u8` (WTF-8 data) passed to native code, freeing it after the function returns.
|
|
77
|
+
|
|
78
|
+
TLDRs from WebIDL spec:
|
|
79
|
+
|
|
80
|
+
- ByteString can only contain valid latin1 characters. It is not safe to assume bun.String is already in 8-bit format, but it is extremely likely.
|
|
81
|
+
- USVString will not contain invalid surrogate pairs, aka text that can be represented correctly in UTF-8.
|
|
82
|
+
- DOMString is the loosest but also most recommended strategy.
|
|
83
|
+
|
|
84
|
+
## Function Variants
|
|
85
|
+
|
|
86
|
+
A `variants` can specify multiple variants (also known as overloads).
|
|
87
|
+
|
|
88
|
+
```ts src/bun.js/math.bind.ts icon="/icons/typescript.svg"
|
|
89
|
+
import { t, fn } from "bindgen";
|
|
90
|
+
|
|
91
|
+
export const action = fn({
|
|
92
|
+
variants: [
|
|
93
|
+
{
|
|
94
|
+
args: {
|
|
95
|
+
a: t.i32,
|
|
96
|
+
},
|
|
97
|
+
ret: t.i32,
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
args: {
|
|
101
|
+
a: t.DOMString,
|
|
102
|
+
},
|
|
103
|
+
ret: t.DOMString,
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
In Zig, each variant gets a number, based on the order the schema defines.
|
|
110
|
+
|
|
111
|
+
```zig
|
|
112
|
+
fn action1(a: i32) i32 {
|
|
113
|
+
return a;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
fn action2(a: bun.String) bun.String {
|
|
117
|
+
return a;
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## `t.dictionary`
|
|
122
|
+
|
|
123
|
+
A `dictionary` is a definition for a JavaScript object, typically as a function inputs. For function outputs, it is usually a smarter idea to declare a class type to add functions and destructuring.
|
|
124
|
+
|
|
125
|
+
## Enumerations
|
|
126
|
+
|
|
127
|
+
To use [WebIDL's enumeration](https://webidl.spec.whatwg.org/#idl-enums) type, use either:
|
|
128
|
+
|
|
129
|
+
- `t.stringEnum`: Create and codegen a new enum type.
|
|
130
|
+
- `t.zigEnum`: Derive a bindgen type off of an existing enum in the codebase.
|
|
131
|
+
|
|
132
|
+
An example of `stringEnum` as used in `fmt.zig` / `bun:internal-for-testing`
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
export const Formatter = t.stringEnum("highlight-javascript", "escape-powershell");
|
|
136
|
+
|
|
137
|
+
export const fmtString = fn({
|
|
138
|
+
args: {
|
|
139
|
+
global: t.globalObject,
|
|
140
|
+
code: t.UTF8String,
|
|
141
|
+
formatter: Formatter,
|
|
142
|
+
},
|
|
143
|
+
ret: t.DOMString,
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
WebIDL strongly encourages using kebab case for enumeration values, to be consistent with existing Web APIs.
|
|
148
|
+
|
|
149
|
+
### Deriving enums from Zig code
|
|
150
|
+
|
|
151
|
+
TODO: zigEnum
|
|
152
|
+
|
|
153
|
+
## `t.oneOf`
|
|
154
|
+
|
|
155
|
+
A `oneOf` is a union between two or more types. It is represented by `union(enum)` in Zig.
|
|
156
|
+
|
|
157
|
+
TODO:
|
|
158
|
+
|
|
159
|
+
## Attributes
|
|
160
|
+
|
|
161
|
+
There are set of attributes that can be chained onto `t.*` types. On all types there are:
|
|
162
|
+
|
|
163
|
+
- `.required`, in dictionary parameters only
|
|
164
|
+
- `.optional`, in function arguments only
|
|
165
|
+
- `.default(T)`
|
|
166
|
+
|
|
167
|
+
When a value is optional, it is lowered to a Zig optional.
|
|
168
|
+
|
|
169
|
+
Depending on the type, there are more attributes available. See the type definitions in auto-complete for more details. Note that one of the above three can only be applied, and they must be applied at the end.
|
|
170
|
+
|
|
171
|
+
### Integer Attributes
|
|
172
|
+
|
|
173
|
+
Integer types allow customizing the overflow behavior with `clamp` or `enforceRange`
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
import { t, fn } from "bindgen";
|
|
177
|
+
|
|
178
|
+
export const add = fn({
|
|
179
|
+
args: {
|
|
180
|
+
global: t.globalObject,
|
|
181
|
+
// enforce in i32 range
|
|
182
|
+
a: t.i32.enforceRange(),
|
|
183
|
+
// clamp to u16 range
|
|
184
|
+
b: t.u16,
|
|
185
|
+
// enforce in arbitrary range, with a default if not provided
|
|
186
|
+
c: t.i32.enforceRange(0, 1000).default(5),
|
|
187
|
+
// clamp to arbitrary range, or null
|
|
188
|
+
d: t.u16.clamp(0, 10).optional,
|
|
189
|
+
},
|
|
190
|
+
ret: t.i32,
|
|
191
|
+
});
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
Various Node.js validator functions such as `validateInteger`, `validateNumber`, and more are available. Use these when implementing Node.js APIs, so the error messages match 1:1 what Node would do.
|
|
195
|
+
|
|
196
|
+
Unlike `enforceRange`, which is taken from WebIDL, `validate*` functions are much more strict on the input they accept. For example, Node's numerical validator check `typeof value === 'number'`, while WebIDL uses `ToNumber` for lossy conversion.
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
import { t, fn } from "bindgen";
|
|
200
|
+
|
|
201
|
+
export const add = fn({
|
|
202
|
+
args: {
|
|
203
|
+
global: t.globalObject,
|
|
204
|
+
// throw if not given a number
|
|
205
|
+
a: t.f64.validateNumber(),
|
|
206
|
+
// valid in i32 range
|
|
207
|
+
a: t.i32.validateInt32(),
|
|
208
|
+
// f64 within safe integer range
|
|
209
|
+
b: t.f64.validateInteger(),
|
|
210
|
+
// f64 in given range
|
|
211
|
+
c: t.f64.validateNumber(-10000, 10000),
|
|
212
|
+
},
|
|
213
|
+
ret: t.i32,
|
|
214
|
+
});
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## Callbacks
|
|
218
|
+
|
|
219
|
+
TODO
|
|
220
|
+
|
|
221
|
+
## Classes
|
|
222
|
+
|
|
223
|
+
TODO
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Building Windows
|
|
3
|
+
description: Building Bun on Windows
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
This document describes the build process for Windows. If you run into problems, please join the [#contributing channel on our Discord](http://bun.com/discord) for help.
|
|
7
|
+
|
|
8
|
+
It is strongly recommended to use [PowerShell 7 (`pwsh.exe`)](https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.4) instead of the default `powershell.exe`.
|
|
9
|
+
|
|
10
|
+
## Prerequisites
|
|
11
|
+
|
|
12
|
+
### Enable Scripts
|
|
13
|
+
|
|
14
|
+
By default, running unverified scripts are blocked.
|
|
15
|
+
|
|
16
|
+
```ps1
|
|
17
|
+
> Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### System Dependencies
|
|
21
|
+
|
|
22
|
+
Bun v1.1 or later. We use Bun to run it's own code generators.
|
|
23
|
+
|
|
24
|
+
```ps1
|
|
25
|
+
> irm bun.com/install.ps1 | iex
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
[Visual Studio](https://visualstudio.microsoft.com) with the "Desktop Development with C++" workload. While installing, make sure to install Git as well, if Git for Windows is not already installed.
|
|
29
|
+
|
|
30
|
+
Visual Studio can be installed graphically using the wizard or through WinGet:
|
|
31
|
+
|
|
32
|
+
```ps1
|
|
33
|
+
> winget install "Visual Studio Community 2022" --override "--add Microsoft.VisualStudio.Workload.NativeDesktop Microsoft.VisualStudio.Component.Git " -s msstore
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
After Visual Studio, you need the following:
|
|
37
|
+
|
|
38
|
+
- LLVM 19.1.7
|
|
39
|
+
- Go
|
|
40
|
+
- Rust
|
|
41
|
+
- NASM
|
|
42
|
+
- Perl
|
|
43
|
+
- Ruby
|
|
44
|
+
- Node.js
|
|
45
|
+
|
|
46
|
+
<Note>The Zig compiler is automatically downloaded, installed, and updated by the building process.</Note>
|
|
47
|
+
|
|
48
|
+
[Scoop](https://scoop.sh) can be used to install these remaining tools easily.
|
|
49
|
+
|
|
50
|
+
```ps1 Scoop
|
|
51
|
+
> irm https://get.scoop.sh | iex
|
|
52
|
+
> scoop install nodejs-lts go rust nasm ruby perl sccache
|
|
53
|
+
# scoop seems to be buggy if you install llvm and the rest at the same time
|
|
54
|
+
> scoop install llvm@19.1.7
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
<Note>
|
|
58
|
+
Please do not use WinGet/other package manager for these, as you will likely install Strawberry Perl instead of a more
|
|
59
|
+
minimal installation of Perl. Strawberry Perl includes many other utilities that get installed into `$Env:PATH` that
|
|
60
|
+
will conflict with MSVC and break the build.
|
|
61
|
+
</Note>
|
|
62
|
+
|
|
63
|
+
If you intend on building WebKit locally (optional), you should install these packages:
|
|
64
|
+
|
|
65
|
+
```ps1 Scoop
|
|
66
|
+
> scoop install make cygwin python
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
From here on out, it is **expected you use a PowerShell Terminal with `.\scripts\vs-shell.ps1` sourced**. This script is available in the Bun repository and can be loaded by executing it:
|
|
70
|
+
|
|
71
|
+
```ps1
|
|
72
|
+
> .\scripts\vs-shell.ps1
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
To verify, you can check for an MSVC-only command line such as `mt.exe`
|
|
76
|
+
|
|
77
|
+
```ps1
|
|
78
|
+
> Get-Command mt
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
<Note>
|
|
82
|
+
It is not recommended to install `ninja` / `cmake` into your global path, because you may run into a situation where
|
|
83
|
+
you try to build bun without .\scripts\vs-shell.ps1 sourced.
|
|
84
|
+
</Note>
|
|
85
|
+
|
|
86
|
+
## Building
|
|
87
|
+
|
|
88
|
+
```ps1
|
|
89
|
+
> bun run build
|
|
90
|
+
|
|
91
|
+
# after the initial `bun run build` you can use the following to build
|
|
92
|
+
> ninja -Cbuild/debug
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
If this was successful, you should have a `bun-debug.exe` in the `build/debug` folder.
|
|
96
|
+
|
|
97
|
+
```ps1
|
|
98
|
+
> .\build\debug\bun-debug.exe --revision
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
You should add this to `$Env:PATH`. The simplest way to do so is to open the start menu, type "Path", and then navigate the environment variables menu to add `C:\.....\bun\build\debug` to the user environment variable `PATH`. You should then restart your editor (if it does not update still, log out and log back in).
|
|
102
|
+
|
|
103
|
+
## Extra paths
|
|
104
|
+
|
|
105
|
+
- WebKit is extracted to `build/debug/cache/webkit/`
|
|
106
|
+
- Zig is extracted to `build/debug/cache/zig/bin/zig.exe`
|
|
107
|
+
|
|
108
|
+
## Tests
|
|
109
|
+
|
|
110
|
+
You can run the test suite either using `bun test <path>` or by using the wrapper script `bun node:test <path>`. The `bun node:test` command runs every test file in a separate instance of bun.exe, to prevent a crash in the test runner from stopping the entire suite.
|
|
111
|
+
|
|
112
|
+
```ps1
|
|
113
|
+
# Setup
|
|
114
|
+
> bun i --cwd packages\bun-internal-test
|
|
115
|
+
|
|
116
|
+
# Run the entire test suite with reporter
|
|
117
|
+
# the package.json script "test" uses "build/debug/bun-debug.exe" by default
|
|
118
|
+
> bun run test
|
|
119
|
+
|
|
120
|
+
# Run an individual test file:
|
|
121
|
+
> bun-debug test node\fs
|
|
122
|
+
> bun-debug test "C:\bun\test\js\bun\resolve\import-meta.test.js"
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Troubleshooting
|
|
126
|
+
|
|
127
|
+
### .rc file fails to build
|
|
128
|
+
|
|
129
|
+
`llvm-rc.exe` is odd. don't use it. use `rc.exe`, to do this make sure you are in a visual studio dev terminal, check `rc /?` to ensure it is `Microsoft Resource Compiler`
|
|
130
|
+
|
|
131
|
+
### failed to write output 'bun-debug.exe': permission denied
|
|
132
|
+
|
|
133
|
+
you cannot overwrite `bun-debug.exe` if it is already open. you likely have a running instance, maybe in the vscode debugger?
|