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.
Files changed (316) hide show
  1. package/bun.d.ts +102 -6
  2. package/docs/bundler/bytecode.mdx +465 -0
  3. package/docs/bundler/css.mdx +1024 -0
  4. package/docs/bundler/esbuild.mdx +253 -0
  5. package/docs/bundler/executables.mdx +535 -0
  6. package/docs/bundler/fullstack.mdx +1064 -0
  7. package/docs/bundler/hot-reloading.mdx +229 -0
  8. package/docs/bundler/html-static.mdx +386 -0
  9. package/docs/bundler/index.mdx +1499 -0
  10. package/docs/bundler/loaders.mdx +356 -0
  11. package/docs/bundler/macros.mdx +328 -0
  12. package/docs/bundler/minifier.mdx +1306 -0
  13. package/docs/bundler/plugins.mdx +411 -0
  14. package/docs/feedback.mdx +85 -0
  15. package/docs/guides/binary/arraybuffer-to-array.mdx +29 -0
  16. package/docs/guides/binary/arraybuffer-to-blob.mdx +26 -0
  17. package/docs/guides/binary/arraybuffer-to-buffer.mdx +27 -0
  18. package/docs/guides/binary/arraybuffer-to-string.mdx +17 -0
  19. package/docs/guides/binary/arraybuffer-to-typedarray.mdx +41 -0
  20. package/docs/guides/binary/blob-to-arraybuffer.mdx +16 -0
  21. package/docs/guides/binary/blob-to-dataview.mdx +16 -0
  22. package/docs/guides/binary/blob-to-stream.mdx +16 -0
  23. package/docs/guides/binary/blob-to-string.mdx +17 -0
  24. package/docs/guides/binary/blob-to-typedarray.mdx +16 -0
  25. package/docs/guides/binary/buffer-to-arraybuffer.mdx +16 -0
  26. package/docs/guides/binary/buffer-to-blob.mdx +16 -0
  27. package/docs/guides/binary/buffer-to-readablestream.mdx +43 -0
  28. package/docs/guides/binary/buffer-to-string.mdx +27 -0
  29. package/docs/guides/binary/buffer-to-typedarray.mdx +16 -0
  30. package/docs/guides/binary/dataview-to-string.mdx +17 -0
  31. package/docs/guides/binary/typedarray-to-arraybuffer.mdx +27 -0
  32. package/docs/guides/binary/typedarray-to-blob.mdx +18 -0
  33. package/docs/guides/binary/typedarray-to-buffer.mdx +16 -0
  34. package/docs/guides/binary/typedarray-to-dataview.mdx +16 -0
  35. package/docs/guides/binary/typedarray-to-readablestream.mdx +43 -0
  36. package/docs/guides/binary/typedarray-to-string.mdx +18 -0
  37. package/docs/guides/deployment/aws-lambda.mdx +204 -0
  38. package/docs/guides/deployment/digital-ocean.mdx +161 -0
  39. package/docs/guides/deployment/google-cloud-run.mdx +197 -0
  40. package/docs/guides/deployment/railway.mdx +145 -0
  41. package/docs/guides/deployment/render.mdx +82 -0
  42. package/docs/guides/deployment/vercel.mdx +99 -0
  43. package/docs/guides/ecosystem/astro.mdx +82 -0
  44. package/docs/guides/ecosystem/discordjs.mdx +80 -0
  45. package/docs/guides/ecosystem/docker.mdx +151 -0
  46. package/docs/guides/ecosystem/drizzle.mdx +195 -0
  47. package/docs/guides/ecosystem/edgedb.mdx +257 -0
  48. package/docs/guides/ecosystem/elysia.mdx +31 -0
  49. package/docs/guides/ecosystem/express.mdx +43 -0
  50. package/docs/guides/ecosystem/hono.mdx +47 -0
  51. package/docs/guides/ecosystem/mongoose.mdx +92 -0
  52. package/docs/guides/ecosystem/neon-drizzle.mdx +234 -0
  53. package/docs/guides/ecosystem/neon-serverless-postgres.mdx +60 -0
  54. package/docs/guides/ecosystem/nextjs.mdx +57 -0
  55. package/docs/guides/ecosystem/nuxt.mdx +90 -0
  56. package/docs/guides/ecosystem/pm2.mdx +55 -0
  57. package/docs/guides/ecosystem/prisma-postgres.mdx +169 -0
  58. package/docs/guides/ecosystem/prisma.mdx +164 -0
  59. package/docs/guides/ecosystem/qwik.mdx +114 -0
  60. package/docs/guides/ecosystem/react.mdx +52 -0
  61. package/docs/guides/ecosystem/remix.mdx +97 -0
  62. package/docs/guides/ecosystem/sentry.mdx +54 -0
  63. package/docs/guides/ecosystem/solidstart.mdx +66 -0
  64. package/docs/guides/ecosystem/ssr-react.mdx +49 -0
  65. package/docs/guides/ecosystem/stric.mdx +54 -0
  66. package/docs/guides/ecosystem/sveltekit.mdx +138 -0
  67. package/docs/guides/ecosystem/systemd.mdx +114 -0
  68. package/docs/guides/ecosystem/upstash.mdx +87 -0
  69. package/docs/guides/ecosystem/vite.mdx +77 -0
  70. package/docs/guides/html-rewriter/extract-links.mdx +72 -0
  71. package/docs/guides/html-rewriter/extract-social-meta.mdx +97 -0
  72. package/docs/guides/http/cluster.mdx +69 -0
  73. package/docs/guides/http/fetch-unix.mdx +35 -0
  74. package/docs/guides/http/fetch.mdx +26 -0
  75. package/docs/guides/http/file-uploads.mdx +97 -0
  76. package/docs/guides/http/hot.mdx +28 -0
  77. package/docs/guides/http/proxy.mdx +26 -0
  78. package/docs/guides/http/server.mdx +48 -0
  79. package/docs/guides/http/simple.mdx +20 -0
  80. package/docs/guides/http/stream-file.mdx +50 -0
  81. package/docs/guides/http/stream-iterator.mdx +49 -0
  82. package/docs/guides/http/stream-node-streams-in-bun.mdx +22 -0
  83. package/docs/guides/http/tls.mdx +32 -0
  84. package/docs/guides/index.mdx +10 -0
  85. package/docs/guides/install/add-dev.mdx +28 -0
  86. package/docs/guides/install/add-git.mdx +38 -0
  87. package/docs/guides/install/add-optional.mdx +27 -0
  88. package/docs/guides/install/add-peer.mdx +45 -0
  89. package/docs/guides/install/add-tarball.mdx +35 -0
  90. package/docs/guides/install/add.mdx +44 -0
  91. package/docs/guides/install/azure-artifacts.mdx +76 -0
  92. package/docs/guides/install/cicd.mdx +43 -0
  93. package/docs/guides/install/custom-registry.mdx +32 -0
  94. package/docs/guides/install/from-npm-install-to-bun-install.mdx +230 -0
  95. package/docs/guides/install/git-diff-bun-lockfile.mdx +47 -0
  96. package/docs/guides/install/jfrog-artifactory.mdx +28 -0
  97. package/docs/guides/install/npm-alias.mdx +25 -0
  98. package/docs/guides/install/registry-scope.mdx +40 -0
  99. package/docs/guides/install/trusted.mdx +50 -0
  100. package/docs/guides/install/workspaces.mdx +70 -0
  101. package/docs/guides/install/yarnlock.mdx +50 -0
  102. package/docs/guides/process/argv.mdx +66 -0
  103. package/docs/guides/process/ctrl-c.mdx +18 -0
  104. package/docs/guides/process/ipc.mdx +69 -0
  105. package/docs/guides/process/nanoseconds.mdx +15 -0
  106. package/docs/guides/process/os-signals.mdx +41 -0
  107. package/docs/guides/process/spawn-stderr.mdx +34 -0
  108. package/docs/guides/process/spawn-stdout.mdx +28 -0
  109. package/docs/guides/process/spawn.mdx +43 -0
  110. package/docs/guides/process/stdin.mdx +62 -0
  111. package/docs/guides/read-file/arraybuffer.mdx +30 -0
  112. package/docs/guides/read-file/buffer.mdx +21 -0
  113. package/docs/guides/read-file/exists.mdx +18 -0
  114. package/docs/guides/read-file/json.mdx +19 -0
  115. package/docs/guides/read-file/mime.mdx +22 -0
  116. package/docs/guides/read-file/stream.mdx +28 -0
  117. package/docs/guides/read-file/string.mdx +24 -0
  118. package/docs/guides/read-file/uint8array.mdx +23 -0
  119. package/docs/guides/read-file/watch.mdx +66 -0
  120. package/docs/guides/runtime/build-time-constants.mdx +295 -0
  121. package/docs/guides/runtime/cicd.mdx +45 -0
  122. package/docs/guides/runtime/codesign-macos-executable.mdx +61 -0
  123. package/docs/guides/runtime/define-constant.mdx +149 -0
  124. package/docs/guides/runtime/delete-directory.mdx +39 -0
  125. package/docs/guides/runtime/delete-file.mdx +21 -0
  126. package/docs/guides/runtime/heap-snapshot.mdx +28 -0
  127. package/docs/guides/runtime/import-html.mdx +17 -0
  128. package/docs/guides/runtime/import-json.mdx +46 -0
  129. package/docs/guides/runtime/import-toml.mdx +32 -0
  130. package/docs/guides/runtime/import-yaml.mdx +104 -0
  131. package/docs/guides/runtime/read-env.mdx +37 -0
  132. package/docs/guides/runtime/set-env.mdx +51 -0
  133. package/docs/guides/runtime/shell.mdx +42 -0
  134. package/docs/guides/runtime/timezone.mdx +38 -0
  135. package/docs/guides/runtime/tsconfig-paths.mdx +31 -0
  136. package/docs/guides/runtime/typescript.mdx +51 -0
  137. package/docs/guides/runtime/vscode-debugger.mdx +48 -0
  138. package/docs/guides/runtime/web-debugger.mdx +103 -0
  139. package/docs/guides/streams/node-readable-to-arraybuffer.mdx +13 -0
  140. package/docs/guides/streams/node-readable-to-blob.mdx +13 -0
  141. package/docs/guides/streams/node-readable-to-json.mdx +14 -0
  142. package/docs/guides/streams/node-readable-to-string.mdx +14 -0
  143. package/docs/guides/streams/node-readable-to-uint8array.mdx +13 -0
  144. package/docs/guides/streams/to-array.mdx +16 -0
  145. package/docs/guides/streams/to-arraybuffer.mdx +16 -0
  146. package/docs/guides/streams/to-blob.mdx +16 -0
  147. package/docs/guides/streams/to-buffer.mdx +17 -0
  148. package/docs/guides/streams/to-json.mdx +16 -0
  149. package/docs/guides/streams/to-string.mdx +16 -0
  150. package/docs/guides/streams/to-typedarray.mdx +24 -0
  151. package/docs/guides/test/bail.mdx +24 -0
  152. package/docs/guides/test/coverage-threshold.mdx +67 -0
  153. package/docs/guides/test/coverage.mdx +49 -0
  154. package/docs/guides/test/happy-dom.mdx +73 -0
  155. package/docs/guides/test/migrate-from-jest.mdx +125 -0
  156. package/docs/guides/test/mock-clock.mdx +50 -0
  157. package/docs/guides/test/mock-functions.mdx +70 -0
  158. package/docs/guides/test/rerun-each.mdx +16 -0
  159. package/docs/guides/test/run-tests.mdx +116 -0
  160. package/docs/guides/test/skip-tests.mdx +43 -0
  161. package/docs/guides/test/snapshot.mdx +102 -0
  162. package/docs/guides/test/spy-on.mdx +49 -0
  163. package/docs/guides/test/svelte-test.mdx +113 -0
  164. package/docs/guides/test/testing-library.mdx +93 -0
  165. package/docs/guides/test/timeout.mdx +17 -0
  166. package/docs/guides/test/todo-tests.mdx +74 -0
  167. package/docs/guides/test/update-snapshots.mdx +49 -0
  168. package/docs/guides/test/watch-mode.mdx +24 -0
  169. package/docs/guides/util/base64.mdx +17 -0
  170. package/docs/guides/util/deep-equals.mdx +41 -0
  171. package/docs/guides/util/deflate.mdx +20 -0
  172. package/docs/guides/util/detect-bun.mdx +25 -0
  173. package/docs/guides/util/entrypoint.mdx +19 -0
  174. package/docs/guides/util/escape-html.mdx +24 -0
  175. package/docs/guides/util/file-url-to-path.mdx +16 -0
  176. package/docs/guides/util/gzip.mdx +20 -0
  177. package/docs/guides/util/hash-a-password.mdx +56 -0
  178. package/docs/guides/util/import-meta-dir.mdx +15 -0
  179. package/docs/guides/util/import-meta-file.mdx +15 -0
  180. package/docs/guides/util/import-meta-path.mdx +15 -0
  181. package/docs/guides/util/javascript-uuid.mdx +25 -0
  182. package/docs/guides/util/main.mdx +43 -0
  183. package/docs/guides/util/path-to-file-url.mdx +16 -0
  184. package/docs/guides/util/sleep.mdx +24 -0
  185. package/docs/guides/util/version.mdx +23 -0
  186. package/docs/guides/util/which-path-to-executable-bin.mdx +17 -0
  187. package/docs/guides/websocket/compression.mdx +33 -0
  188. package/docs/guides/websocket/context.mdx +74 -0
  189. package/docs/guides/websocket/pubsub.mdx +40 -0
  190. package/docs/guides/websocket/simple.mdx +35 -0
  191. package/docs/guides/write-file/append.mdx +54 -0
  192. package/docs/guides/write-file/basic.mdx +46 -0
  193. package/docs/guides/write-file/blob.mdx +30 -0
  194. package/docs/guides/write-file/cat.mdx +19 -0
  195. package/docs/guides/write-file/file-cp.mdx +18 -0
  196. package/docs/guides/write-file/filesink.mdx +54 -0
  197. package/docs/guides/write-file/response.mdx +19 -0
  198. package/docs/guides/write-file/stdout.mdx +23 -0
  199. package/docs/guides/write-file/stream.mdx +19 -0
  200. package/docs/guides/write-file/unlink.mdx +18 -0
  201. package/docs/index.mdx +133 -0
  202. package/docs/installation.mdx +365 -0
  203. package/docs/pm/bunx.mdx +83 -0
  204. package/docs/pm/catalogs.mdx +292 -0
  205. package/docs/pm/cli/add.mdx +179 -0
  206. package/docs/pm/cli/audit.mdx +60 -0
  207. package/docs/pm/cli/install.mdx +471 -0
  208. package/docs/pm/cli/link.mdx +48 -0
  209. package/docs/pm/cli/outdated.mdx +197 -0
  210. package/docs/pm/cli/patch.mdx +69 -0
  211. package/docs/pm/cli/pm.mdx +319 -0
  212. package/docs/pm/cli/publish.mdx +123 -0
  213. package/docs/pm/cli/remove.mdx +16 -0
  214. package/docs/pm/cli/update.mdx +140 -0
  215. package/docs/pm/cli/why.mdx +84 -0
  216. package/docs/pm/filter.mdx +102 -0
  217. package/docs/pm/global-cache.mdx +72 -0
  218. package/docs/pm/isolated-installs.mdx +205 -0
  219. package/docs/pm/lifecycle.mdx +57 -0
  220. package/docs/pm/lockfile.mdx +64 -0
  221. package/docs/pm/npmrc.mdx +111 -0
  222. package/docs/pm/overrides.mdx +83 -0
  223. package/docs/pm/scopes-registries.mdx +35 -0
  224. package/docs/pm/security-scanner-api.mdx +95 -0
  225. package/docs/pm/workspaces.mdx +109 -0
  226. package/docs/project/benchmarking.mdx +218 -0
  227. package/docs/project/bindgen.mdx +223 -0
  228. package/docs/project/building-windows.mdx +133 -0
  229. package/docs/project/contributing.mdx +343 -0
  230. package/docs/project/feedback.mdx +20 -0
  231. package/docs/project/license.mdx +78 -0
  232. package/docs/project/roadmap.mdx +8 -0
  233. package/docs/quickstart.mdx +240 -0
  234. package/docs/runtime/auto-install.mdx +97 -0
  235. package/docs/runtime/binary-data.mdx +846 -0
  236. package/docs/runtime/bun-apis.mdx +59 -0
  237. package/docs/runtime/bunfig.mdx +642 -0
  238. package/docs/runtime/c-compiler.mdx +204 -0
  239. package/docs/runtime/child-process.mdx +532 -0
  240. package/docs/runtime/color.mdx +267 -0
  241. package/docs/runtime/console.mdx +67 -0
  242. package/docs/runtime/cookies.mdx +454 -0
  243. package/docs/runtime/debugger.mdx +335 -0
  244. package/docs/runtime/environment-variables.mdx +214 -0
  245. package/docs/runtime/ffi.mdx +565 -0
  246. package/docs/runtime/file-io.mdx +306 -0
  247. package/docs/runtime/file-system-router.mdx +118 -0
  248. package/docs/runtime/file-types.mdx +354 -0
  249. package/docs/runtime/glob.mdx +181 -0
  250. package/docs/runtime/globals.mdx +72 -0
  251. package/docs/runtime/hashing.mdx +315 -0
  252. package/docs/runtime/html-rewriter.mdx +340 -0
  253. package/docs/runtime/http/cookies.mdx +79 -0
  254. package/docs/runtime/http/error-handling.mdx +40 -0
  255. package/docs/runtime/http/metrics.mdx +36 -0
  256. package/docs/runtime/http/routing.mdx +289 -0
  257. package/docs/runtime/http/server.mdx +647 -0
  258. package/docs/runtime/http/tls.mdx +101 -0
  259. package/docs/runtime/http/websockets.mdx +404 -0
  260. package/docs/runtime/index.mdx +223 -0
  261. package/docs/runtime/jsx.mdx +115 -0
  262. package/docs/runtime/module-resolution.mdx +342 -0
  263. package/docs/runtime/networking/dns.mdx +111 -0
  264. package/docs/runtime/networking/fetch.mdx +468 -0
  265. package/docs/runtime/networking/tcp.mdx +239 -0
  266. package/docs/runtime/networking/udp.mdx +129 -0
  267. package/docs/runtime/node-api.mdx +19 -0
  268. package/docs/runtime/nodejs-compat.mdx +468 -0
  269. package/docs/runtime/plugins.mdx +405 -0
  270. package/docs/runtime/redis.mdx +582 -0
  271. package/docs/runtime/s3.mdx +863 -0
  272. package/docs/runtime/secrets.mdx +336 -0
  273. package/docs/runtime/semver.mdx +57 -0
  274. package/docs/runtime/shell.mdx +637 -0
  275. package/docs/runtime/sql.mdx +1404 -0
  276. package/docs/runtime/sqlite.mdx +699 -0
  277. package/docs/runtime/streams.mdx +232 -0
  278. package/docs/runtime/templating/create.mdx +269 -0
  279. package/docs/runtime/templating/init.mdx +58 -0
  280. package/docs/runtime/transpiler.mdx +288 -0
  281. package/docs/runtime/typescript.mdx +58 -0
  282. package/docs/runtime/utils.mdx +922 -0
  283. package/docs/runtime/watch-mode.mdx +161 -0
  284. package/docs/runtime/web-apis.mdx +29 -0
  285. package/docs/runtime/workers.mdx +328 -0
  286. package/docs/runtime/yaml.mdx +469 -0
  287. package/docs/snippets/cli/add.mdx +166 -0
  288. package/docs/snippets/cli/build.mdx +196 -0
  289. package/docs/snippets/cli/feedback.mdx +17 -0
  290. package/docs/snippets/cli/init.mdx +84 -0
  291. package/docs/snippets/cli/install.mdx +173 -0
  292. package/docs/snippets/cli/link.mdx +163 -0
  293. package/docs/snippets/cli/outdated.mdx +140 -0
  294. package/docs/snippets/cli/patch.mdx +171 -0
  295. package/docs/snippets/cli/publish.mdx +198 -0
  296. package/docs/snippets/cli/remove.mdx +146 -0
  297. package/docs/snippets/cli/run.mdx +293 -0
  298. package/docs/snippets/cli/test.mdx +100 -0
  299. package/docs/snippets/cli/update.mdx +144 -0
  300. package/docs/snippets/product-card.mdx +32 -0
  301. package/docs/snippets/product-tiles.mdx +94 -0
  302. package/docs/test/code-coverage.mdx +409 -0
  303. package/docs/test/configuration.mdx +467 -0
  304. package/docs/test/dates-times.mdx +129 -0
  305. package/docs/test/discovery.mdx +90 -0
  306. package/docs/test/dom.mdx +226 -0
  307. package/docs/test/index.mdx +380 -0
  308. package/docs/test/lifecycle.mdx +348 -0
  309. package/docs/test/mocks.mdx +637 -0
  310. package/docs/test/reporters.mdx +117 -0
  311. package/docs/test/runtime-behavior.mdx +342 -0
  312. package/docs/test/snapshots.mdx +434 -0
  313. package/docs/test/writing-tests.mdx +635 -0
  314. package/docs/typescript.mdx +54 -0
  315. package/package.json +8 -6
  316. package/test.d.ts +2 -2
@@ -0,0 +1,922 @@
1
+ ---
2
+ title: Utils
3
+ description: Use Bun's utility functions to work with the runtime
4
+ ---
5
+
6
+ ## `Bun.version`
7
+
8
+ A `string` containing the version of the `bun` CLI that is currently running.
9
+
10
+ ```ts terminal icon="terminal"
11
+ Bun.version;
12
+ // => "0.6.4"
13
+ ```
14
+
15
+ ## `Bun.revision`
16
+
17
+ The git commit of [Bun](https://github.com/oven-sh/bun) that was compiled to create the current `bun` CLI.
18
+
19
+ ```ts terminal icon="terminal"
20
+ Bun.revision;
21
+ // => "f02561530fda1ee9396f51c8bc99b38716e38296"
22
+ ```
23
+
24
+ ## `Bun.env`
25
+
26
+ An alias for `process.env`.
27
+
28
+ ## `Bun.main`
29
+
30
+ An absolute path to the entrypoint of the current program (the file that was executed with `bun run`).
31
+
32
+ ```ts script.ts
33
+ Bun.main;
34
+ // /path/to/script.ts
35
+ ```
36
+
37
+ This is particular useful for determining whether a script is being directly executed, as opposed to being imported by another script.
38
+
39
+ ```ts
40
+ if (import.meta.path === Bun.main) {
41
+ // this script is being directly executed
42
+ } else {
43
+ // this file is being imported from another script
44
+ }
45
+ ```
46
+
47
+ This is analogous to the [`require.main = module` trick](https://stackoverflow.com/questions/6398196/detect-if-called-through-require-or-directly-by-command-line) in Node.js.
48
+
49
+ ## `Bun.sleep()`
50
+
51
+ `Bun.sleep(ms: number)`
52
+
53
+ Returns a `Promise` that resolves after the given number of milliseconds.
54
+
55
+ ```ts
56
+ console.log("hello");
57
+ await Bun.sleep(1000);
58
+ console.log("hello one second later!");
59
+ ```
60
+
61
+ Alternatively, pass a `Date` object to receive a `Promise` that resolves at that point in time.
62
+
63
+ ```ts
64
+ const oneSecondInFuture = new Date(Date.now() + 1000);
65
+
66
+ console.log("hello");
67
+ await Bun.sleep(oneSecondInFuture);
68
+ console.log("hello one second later!");
69
+ ```
70
+
71
+ ## `Bun.sleepSync()`
72
+
73
+ `Bun.sleepSync(ms: number)`
74
+
75
+ A blocking synchronous version of `Bun.sleep`.
76
+
77
+ ```ts
78
+ console.log("hello");
79
+ Bun.sleepSync(1000); // blocks thread for one second
80
+ console.log("hello one second later!");
81
+ ```
82
+
83
+ ## `Bun.which()`
84
+
85
+ `Bun.which(bin: string)`
86
+
87
+ Returns the path to an executable, similar to typing `which` in your terminal.
88
+
89
+ ```ts
90
+ const ls = Bun.which("ls");
91
+ console.log(ls); // "/usr/bin/ls"
92
+ ```
93
+
94
+ By default Bun looks at the current `PATH` environment variable to determine the path. To configure `PATH`:
95
+
96
+ ```ts
97
+ const ls = Bun.which("ls", {
98
+ PATH: "/usr/local/bin:/usr/bin:/bin",
99
+ });
100
+ console.log(ls); // "/usr/bin/ls"
101
+ ```
102
+
103
+ Pass a `cwd` option to resolve for executable from within a specific directory.
104
+
105
+ ```ts
106
+ const ls = Bun.which("ls", {
107
+ cwd: "/tmp",
108
+ PATH: "",
109
+ });
110
+
111
+ console.log(ls); // null
112
+ ```
113
+
114
+ You can think of this as a builtin alternative to the [`which`](https://www.npmjs.com/package/which) npm package.
115
+
116
+ ## `Bun.randomUUIDv7()`
117
+
118
+ `Bun.randomUUIDv7()` returns a [UUID v7](https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-01.html#name-uuidv7-layout-and-bit-order), which is monotonic and suitable for sorting and databases.
119
+
120
+ ```ts
121
+ import { randomUUIDv7 } from "bun";
122
+
123
+ const id = randomUUIDv7();
124
+ // => "0192ce11-26d5-7dc3-9305-1426de888c5a"
125
+ ```
126
+
127
+ A UUID v7 is a 128-bit value that encodes the current timestamp, a random value, and a counter. The timestamp is encoded using the lowest 48 bits, and the random value and counter are encoded using the remaining bits.
128
+
129
+ The `timestamp` parameter defaults to the current time in milliseconds. When the timestamp changes, the counter is reset to a pseudo-random integer wrapped to 4096. This counter is atomic and threadsafe, meaning that using `Bun.randomUUIDv7()` in many Workers within the same process running at the same timestamp will not have colliding counter values.
130
+
131
+ The final 8 bytes of the UUID are a cryptographically secure random value. It uses the same random number generator used by `crypto.randomUUID()` (which comes from BoringSSL, which in turn comes from the platform-specific system random number generator usually provided by the underlying hardware).
132
+
133
+ ```ts
134
+ namespace Bun {
135
+ function randomUUIDv7(encoding?: "hex" | "base64" | "base64url" = "hex", timestamp?: number = Date.now()): string;
136
+ /**
137
+ * If you pass "buffer", you get a 16-byte buffer instead of a string.
138
+ */
139
+ function randomUUIDv7(encoding: "buffer", timestamp?: number = Date.now()): Buffer;
140
+
141
+ // If you only pass a timestamp, you get a hex string
142
+ function randomUUIDv7(timestamp?: number = Date.now()): string;
143
+ }
144
+ ```
145
+
146
+ You can optionally set encoding to `"buffer"` to get a 16-byte buffer instead of a string. This can sometimes avoid string conversion overhead.
147
+
148
+ ```ts buffer.ts
149
+ const buffer = Bun.randomUUIDv7("buffer");
150
+ ```
151
+
152
+ `base64` and `base64url` encodings are also supported when you want a slightly shorter string.
153
+
154
+ ```ts base64.ts
155
+ const base64 = Bun.randomUUIDv7("base64");
156
+ const base64url = Bun.randomUUIDv7("base64url");
157
+ ```
158
+
159
+ ## `Bun.peek()`
160
+
161
+ `Bun.peek(prom: Promise)`
162
+
163
+ Reads a promise's result without `await` or `.then`, but only if the promise has already fulfilled or rejected.
164
+
165
+ ```ts
166
+ import { peek } from "bun";
167
+
168
+ const promise = Promise.resolve("hi");
169
+
170
+ // no await!
171
+ const result = peek(promise);
172
+ console.log(result); // "hi"
173
+ ```
174
+
175
+ This is important when attempting to reduce number of extraneous microticks in performance-sensitive code. It's an advanced API and you probably shouldn't use it unless you know what you're doing.
176
+
177
+ ```ts
178
+ import { peek } from "bun";
179
+ import { expect, test } from "bun:test";
180
+
181
+ test("peek", () => {
182
+ const promise = Promise.resolve(true);
183
+
184
+ // no await necessary!
185
+ expect(peek(promise)).toBe(true);
186
+
187
+ // if we peek again, it returns the same value
188
+ const again = peek(promise);
189
+ expect(again).toBe(true);
190
+
191
+ // if we peek a non-promise, it returns the value
192
+ const value = peek(42);
193
+ expect(value).toBe(42);
194
+
195
+ // if we peek a pending promise, it returns the promise again
196
+ const pending = new Promise(() => {});
197
+ expect(peek(pending)).toBe(pending);
198
+
199
+ // If we peek a rejected promise, it:
200
+ // - returns the error
201
+ // - does not mark the promise as handled
202
+ const rejected = Promise.reject(new Error("Successfully tested promise rejection"));
203
+ expect(peek(rejected).message).toBe("Successfully tested promise rejection");
204
+ });
205
+ ```
206
+
207
+ The `peek.status` function lets you read the status of a promise without resolving it.
208
+
209
+ ```ts
210
+ import { peek } from "bun";
211
+ import { expect, test } from "bun:test";
212
+
213
+ test("peek.status", () => {
214
+ const promise = Promise.resolve(true);
215
+ expect(peek.status(promise)).toBe("fulfilled");
216
+
217
+ const pending = new Promise(() => {});
218
+ expect(peek.status(pending)).toBe("pending");
219
+
220
+ const rejected = Promise.reject(new Error("oh nooo"));
221
+ expect(peek.status(rejected)).toBe("rejected");
222
+ });
223
+ ```
224
+
225
+ ## `Bun.openInEditor()`
226
+
227
+ Opens a file in your default editor. Bun auto-detects your editor via the `$VISUAL` or `$EDITOR` environment variables.
228
+
229
+ ```ts
230
+ const currentFile = import.meta.url;
231
+ Bun.openInEditor(currentFile);
232
+ ```
233
+
234
+ You can override this via the `debug.editor` setting in your [`bunfig.toml`](/runtime/bunfig).
235
+
236
+ ```toml bunfig.toml
237
+ [debug] // [!code ++]
238
+ editor = "code" // [!code ++]
239
+ ```
240
+
241
+ Or specify an editor with the `editor` param. You can also specify a line and column number.
242
+
243
+ ```ts
244
+ Bun.openInEditor(import.meta.url, {
245
+ editor: "vscode", // or "subl"
246
+ line: 10,
247
+ column: 5,
248
+ });
249
+ ```
250
+
251
+ ## `Bun.deepEquals()`
252
+
253
+ Recursively checks if two objects are equivalent. This is used internally by `expect().toEqual()` in `bun:test`.
254
+
255
+ ```ts
256
+ const foo = { a: 1, b: 2, c: { d: 3 } };
257
+
258
+ // true
259
+ Bun.deepEquals(foo, { a: 1, b: 2, c: { d: 3 } });
260
+
261
+ // false
262
+ Bun.deepEquals(foo, { a: 1, b: 2, c: { d: 4 } });
263
+ ```
264
+
265
+ A third boolean parameter can be used to enable "strict" mode. This is used by `expect().toStrictEqual()` in the test runner.
266
+
267
+ ```ts
268
+ const a = { entries: [1, 2] };
269
+ const b = { entries: [1, 2], extra: undefined };
270
+
271
+ Bun.deepEquals(a, b); // => true
272
+ Bun.deepEquals(a, b, true); // => false
273
+ ```
274
+
275
+ In strict mode, the following are considered unequal:
276
+
277
+ ```ts
278
+ // undefined values
279
+ Bun.deepEquals({}, { a: undefined }, true); // false
280
+
281
+ // undefined in arrays
282
+ Bun.deepEquals(["asdf"], ["asdf", undefined], true); // false
283
+
284
+ // sparse arrays
285
+ Bun.deepEquals([, 1], [undefined, 1], true); // false
286
+
287
+ // object literals vs instances w/ same properties
288
+ class Foo {
289
+ a = 1;
290
+ }
291
+ Bun.deepEquals(new Foo(), { a: 1 }, true); // false
292
+ ```
293
+
294
+ ## `Bun.escapeHTML()`
295
+
296
+ `Bun.escapeHTML(value: string | object | number | boolean): string`
297
+
298
+ Escapes the following characters from an input string:
299
+
300
+ - `"` becomes `"`
301
+ - `&` becomes `&`
302
+ - `'` becomes `'`
303
+ - `<` becomes `&lt;`
304
+ - `>` becomes `&gt;`
305
+
306
+ This function is optimized for large input. On an M1X, it processes 480 MB/s -
307
+ 20 GB/s, depending on how much data is being escaped and whether there is non-ascii
308
+ text. Non-string types will be converted to a string before escaping.
309
+
310
+ ## `Bun.stringWidth()`
311
+
312
+ <Note>~6,756x faster `string-width` alternative</Note>
313
+
314
+ Get the column count of a string as it would be displayed in a terminal.
315
+ Supports ANSI escape codes, emoji, and wide characters.
316
+
317
+ Example usage:
318
+
319
+ ```ts
320
+ Bun.stringWidth("hello"); // => 5
321
+ Bun.stringWidth("\u001b[31mhello\u001b[0m"); // => 5
322
+ Bun.stringWidth("\u001b[31mhello\u001b[0m", { countAnsiEscapeCodes: true }); // => 12
323
+ ```
324
+
325
+ This is useful for:
326
+
327
+ - Aligning text in a terminal
328
+ - Quickly checking if a string contains ANSI escape codes
329
+ - Measuring the width of a string in a terminal
330
+
331
+ This API is designed to match the popular "string-width" package, so that
332
+ existing code can be easily ported to Bun and vice versa.
333
+
334
+ [In this benchmark](https://github.com/oven-sh/bun/blob/5147c0ba7379d85d4d1ed0714b84d6544af917eb/bench/snippets/string-width.mjs#L13), `Bun.stringWidth` is a ~6,756x faster than the `string-width` npm package for input larger than about 500 characters. Big thanks to [sindresorhus](https://github.com/sindresorhus) for their work on `string-width`!
335
+
336
+ ```ts
337
+ ❯ bun string-width.mjs
338
+ cpu: 13th Gen Intel(R) Core(TM) i9-13900
339
+ runtime: bun 1.0.29 (x64-linux)
340
+
341
+ benchmark time (avg) (min … max) p75 p99 p995
342
+ ------------------------------------------------------------------------------------- -----------------------------
343
+ Bun.stringWidth 500 chars ascii 37.09 ns/iter (36.77 ns … 41.11 ns) 37.07 ns 38.84 ns 38.99 ns
344
+
345
+ ❯ node string-width.mjs
346
+
347
+ benchmark time (avg) (min … max) p75 p99 p995
348
+ ------------------------------------------------------------------------------------- -----------------------------
349
+ npm/string-width 500 chars ascii 249,710 ns/iter (239,970 ns … 293,180 ns) 250,930 ns 276,700 ns 281,450 ns
350
+ ```
351
+
352
+ To make `Bun.stringWidth` fast, we've implemented it in Zig using optimized SIMD instructions, accounting for Latin1, UTF-16, and UTF-8 encodings. It passes `string-width`'s tests.
353
+
354
+ <Accordion title="View full benchmark">
355
+
356
+ As a reminder, 1 nanosecond (ns) is 1 billionth of a second. Here's a quick reference for converting between units:
357
+
358
+ | Unit | 1 Millisecond |
359
+ | ---- | ------------- |
360
+ | ns | 1,000,000 |
361
+ | µs | 1,000 |
362
+ | ms | 1 |
363
+
364
+ ```bash terminal icon="terminal"
365
+ ❯ bun string-width.mjs
366
+ cpu: 13th Gen Intel(R) Core(TM) i9-13900
367
+ runtime: bun 1.0.29 (x64-linux)
368
+
369
+ benchmark time (avg) (min … max) p75 p99 p995
370
+ ------------------------------------------------------------------------------------- -----------------------------
371
+ Bun.stringWidth 5 chars ascii 16.45 ns/iter (16.27 ns … 19.71 ns) 16.48 ns 16.93 ns 17.21 ns
372
+ Bun.stringWidth 50 chars ascii 19.42 ns/iter (18.61 ns … 27.85 ns) 19.35 ns 21.7 ns 22.31 ns
373
+ Bun.stringWidth 500 chars ascii 37.09 ns/iter (36.77 ns … 41.11 ns) 37.07 ns 38.84 ns 38.99 ns
374
+ Bun.stringWidth 5,000 chars ascii 216.9 ns/iter (215.8 ns … 228.54 ns) 216.23 ns 228.52 ns 228.53 ns
375
+ Bun.stringWidth 25,000 chars ascii 1.01 µs/iter (1.01 µs … 1.01 µs) 1.01 µs 1.01 µs 1.01 µs
376
+ Bun.stringWidth 7 chars ascii+emoji 54.2 ns/iter (53.36 ns … 58.19 ns) 54.23 ns 57.55 ns 57.94 ns
377
+ Bun.stringWidth 70 chars ascii+emoji 354.26 ns/iter (350.51 ns … 363.96 ns) 355.93 ns 363.11 ns 363.96 ns
378
+ Bun.stringWidth 700 chars ascii+emoji 3.3 µs/iter (3.27 µs … 3.4 µs) 3.3 µs 3.4 µs 3.4 µs
379
+ Bun.stringWidth 7,000 chars ascii+emoji 32.69 µs/iter (32.22 µs … 45.27 µs) 32.7 µs 34.57 µs 34.68 µs
380
+ Bun.stringWidth 35,000 chars ascii+emoji 163.35 µs/iter (161.17 µs … 170.79 µs) 163.82 µs 169.66 µs 169.93 µs
381
+ Bun.stringWidth 8 chars ansi+emoji 66.15 ns/iter (65.17 ns … 69.97 ns) 66.12 ns 69.8 ns 69.87 ns
382
+ Bun.stringWidth 80 chars ansi+emoji 492.95 ns/iter (488.05 ns … 499.5 ns) 494.8 ns 498.58 ns 499.5 ns
383
+ Bun.stringWidth 800 chars ansi+emoji 4.73 µs/iter (4.71 µs … 4.88 µs) 4.72 µs 4.88 µs 4.88 µs
384
+ Bun.stringWidth 8,000 chars ansi+emoji 47.02 µs/iter (46.37 µs … 67.44 µs) 46.96 µs 49.57 µs 49.63 µs
385
+ Bun.stringWidth 40,000 chars ansi+emoji 234.45 µs/iter (231.78 µs … 240.98 µs) 234.92 µs 236.34 µs 236.62 µs
386
+ Bun.stringWidth 19 chars ansi+emoji+ascii 135.46 ns/iter (133.67 ns … 143.26 ns) 135.32 ns 142.55 ns 142.77 ns
387
+ Bun.stringWidth 190 chars ansi+emoji+ascii 1.17 µs/iter (1.16 µs … 1.17 µs) 1.17 µs 1.17 µs 1.17 µs
388
+ Bun.stringWidth 1,900 chars ansi+emoji+ascii 11.45 µs/iter (11.26 µs … 20.41 µs) 11.45 µs 12.08 µs 12.11 µs
389
+ Bun.stringWidth 19,000 chars ansi+emoji+ascii 114.06 µs/iter (112.86 µs … 120.06 µs) 114.25 µs 115.86 µs 116.15 µs
390
+ Bun.stringWidth 95,000 chars ansi+emoji+ascii 572.69 µs/iter (565.52 µs … 607.22 µs) 572.45 µs 604.86 µs 605.21 µs
391
+ ```
392
+
393
+ ```bash terminal icon="terminal"
394
+ ❯ node string-width.mjs
395
+ cpu: 13th Gen Intel(R) Core(TM) i9-13900
396
+ runtime: node v21.4.0 (x64-linux)
397
+
398
+ benchmark time (avg) (min … max) p75 p99 p995
399
+ -------------------------------------------------------------------------------------- -----------------------------
400
+ npm/string-width 5 chars ascii 3.19 µs/iter (3.13 µs … 3.48 µs) 3.25 µs 3.48 µs 3.48 µs
401
+ npm/string-width 50 chars ascii 20.09 µs/iter (18.93 µs … 435.06 µs) 19.49 µs 21.89 µs 22.59 µs
402
+ npm/string-width 500 chars ascii 249.71 µs/iter (239.97 µs … 293.18 µs) 250.93 µs 276.7 µs 281.45 µs
403
+ npm/string-width 5,000 chars ascii 6.69 ms/iter (6.58 ms … 6.76 ms) 6.72 ms 6.76 ms 6.76 ms
404
+ npm/string-width 25,000 chars ascii 139.57 ms/iter (137.17 ms … 143.28 ms) 140.49 ms 143.28 ms 143.28 ms
405
+ npm/string-width 7 chars ascii+emoji 3.7 µs/iter (3.62 µs … 3.94 µs) 3.73 µs 3.94 µs 3.94 µs
406
+ npm/string-width 70 chars ascii+emoji 23.93 µs/iter (22.44 µs … 331.2 µs) 23.15 µs 25.98 µs 30.2 µs
407
+ npm/string-width 700 chars ascii+emoji 251.65 µs/iter (237.78 µs … 444.69 µs) 252.92 µs 325.89 µs 354.08 µs
408
+ npm/string-width 7,000 chars ascii+emoji 4.95 ms/iter (4.82 ms … 5.19 ms) 5 ms 5.04 ms 5.19 ms
409
+ npm/string-width 35,000 chars ascii+emoji 96.93 ms/iter (94.39 ms … 102.58 ms) 97.68 ms 102.58 ms 102.58 ms
410
+ npm/string-width 8 chars ansi+emoji 3.92 µs/iter (3.45 µs … 4.57 µs) 4.09 µs 4.57 µs 4.57 µs
411
+ npm/string-width 80 chars ansi+emoji 24.46 µs/iter (22.87 µs … 4.2 ms) 23.54 µs 25.89 µs 27.41 µs
412
+ npm/string-width 800 chars ansi+emoji 259.62 µs/iter (246.76 µs … 480.12 µs) 258.65 µs 349.84 µs 372.55 µs
413
+ npm/string-width 8,000 chars ansi+emoji 5.46 ms/iter (5.41 ms … 5.57 ms) 5.48 ms 5.55 ms 5.57 ms
414
+ npm/string-width 40,000 chars ansi+emoji 108.91 ms/iter (107.55 ms … 109.5 ms) 109.25 ms 109.5 ms 109.5 ms
415
+ npm/string-width 19 chars ansi+emoji+ascii 6.53 µs/iter (6.35 µs … 6.75 µs) 6.54 µs 6.75 µs 6.75 µs
416
+ npm/string-width 190 chars ansi+emoji+ascii 55.52 µs/iter (52.59 µs … 352.73 µs) 54.19 µs 80.77 µs 167.21 µs
417
+ npm/string-width 1,900 chars ansi+emoji+ascii 701.71 µs/iter (653.94 µs … 893.78 µs) 715.3 µs 855.37 µs 872.9 µs
418
+ npm/string-width 19,000 chars ansi+emoji+ascii 27.19 ms/iter (26.89 ms … 27.41 ms) 27.28 ms 27.41 ms 27.41 ms
419
+ npm/string-width 95,000 chars ansi+emoji+ascii 3.68 s/iter (3.66 s … 3.7 s) 3.69 s 3.7 s 3.7 s
420
+ ```
421
+
422
+ </Accordion>
423
+
424
+ TypeScript definition:
425
+
426
+ ```ts expandable
427
+ namespace Bun {
428
+ export function stringWidth(
429
+ /**
430
+ * The string to measure
431
+ */
432
+ input: string,
433
+ options?: {
434
+ /**
435
+ * If `true`, count ANSI escape codes as part of the string width. If `false`, ANSI escape codes are ignored when calculating the string width.
436
+ *
437
+ * @default false
438
+ */
439
+ countAnsiEscapeCodes?: boolean;
440
+ /**
441
+ * When it's ambiugous and `true`, count emoji as 1 characters wide. If `false`, emoji are counted as 2 character wide.
442
+ *
443
+ * @default true
444
+ */
445
+ ambiguousIsNarrow?: boolean;
446
+ },
447
+ ): number;
448
+ }
449
+ ```
450
+
451
+ ---
452
+
453
+ ## `Bun.fileURLToPath()`
454
+
455
+ Converts a `file://` URL to an absolute path.
456
+
457
+ ```ts
458
+ const path = Bun.fileURLToPath(new URL("file:///foo/bar.txt"));
459
+ console.log(path); // "/foo/bar.txt"
460
+ ```
461
+
462
+ ---
463
+
464
+ ## `Bun.pathToFileURL()`
465
+
466
+ Converts an absolute path to a `file://` URL.
467
+
468
+ ```ts
469
+ const url = Bun.pathToFileURL("/foo/bar.txt");
470
+ console.log(url); // "file:///foo/bar.txt"
471
+ ```
472
+
473
+ ---
474
+
475
+ ## `Bun.gzipSync()`
476
+
477
+ Compresses a `Uint8Array` using zlib's GZIP algorithm.
478
+
479
+ ```ts
480
+ const buf = Buffer.from("hello".repeat(100)); // Buffer extends Uint8Array
481
+ const compressed = Bun.gzipSync(buf);
482
+
483
+ buf; // => Uint8Array(500)
484
+ compressed; // => Uint8Array(30)
485
+ ```
486
+
487
+ Optionally, pass a parameters object as the second argument:
488
+
489
+ <Accordion title="zlib compression options">
490
+
491
+ ```ts expandable
492
+ export type ZlibCompressionOptions = {
493
+ /**
494
+ * The compression level to use. Must be between `-1` and `9`.
495
+ * - A value of `-1` uses the default compression level (Currently `6`)
496
+ * - A value of `0` gives no compression
497
+ * - A value of `1` gives least compression, fastest speed
498
+ * - A value of `9` gives best compression, slowest speed
499
+ */
500
+ level?: -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
501
+ /**
502
+ * How much memory should be allocated for the internal compression state.
503
+ *
504
+ * A value of `1` uses minimum memory but is slow and reduces compression ratio.
505
+ *
506
+ * A value of `9` uses maximum memory for optimal speed. The default is `8`.
507
+ */
508
+ memLevel?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
509
+ /**
510
+ * The base 2 logarithm of the window size (the size of the history buffer).
511
+ *
512
+ * Larger values of this parameter result in better compression at the expense of memory usage.
513
+ *
514
+ * The following value ranges are supported:
515
+ * - `9..15`: The output will have a zlib header and footer (Deflate)
516
+ * - `-9..-15`: The output will **not** have a zlib header or footer (Raw Deflate)
517
+ * - `25..31` (16+`9..15`): The output will have a gzip header and footer (gzip)
518
+ *
519
+ * The gzip header will have no file name, no extra data, no comment, no modification time (set to zero) and no header CRC.
520
+ */
521
+ windowBits?:
522
+ | -9
523
+ | -10
524
+ | -11
525
+ | -12
526
+ | -13
527
+ | -14
528
+ | -15
529
+ | 9
530
+ | 10
531
+ | 11
532
+ | 12
533
+ | 13
534
+ | 14
535
+ | 15
536
+ | 25
537
+ | 26
538
+ | 27
539
+ | 28
540
+ | 29
541
+ | 30
542
+ | 31;
543
+ /**
544
+ * Tunes the compression algorithm.
545
+ *
546
+ * - `Z_DEFAULT_STRATEGY`: For normal data **(Default)**
547
+ * - `Z_FILTERED`: For data produced by a filter or predictor
548
+ * - `Z_HUFFMAN_ONLY`: Force Huffman encoding only (no string match)
549
+ * - `Z_RLE`: Limit match distances to one (run-length encoding)
550
+ * - `Z_FIXED` prevents the use of dynamic Huffman codes
551
+ *
552
+ * `Z_RLE` is designed to be almost as fast as `Z_HUFFMAN_ONLY`, but give better compression for PNG image data.
553
+ *
554
+ * `Z_FILTERED` forces more Huffman coding and less string matching, it is
555
+ * somewhat intermediate between `Z_DEFAULT_STRATEGY` and `Z_HUFFMAN_ONLY`.
556
+ * Filtered data consists mostly of small values with a somewhat random distribution.
557
+ */
558
+ strategy?: number;
559
+ };
560
+ ```
561
+
562
+ </Accordion>
563
+
564
+ ---
565
+
566
+ ## `Bun.gunzipSync()`
567
+
568
+ Decompresses a `Uint8Array` using zlib's GUNZIP algorithm.
569
+
570
+ ```ts
571
+ const buf = Buffer.from("hello".repeat(100)); // Buffer extends Uint8Array
572
+ const compressed = Bun.gzipSync(buf);
573
+
574
+ const dec = new TextDecoder();
575
+ const uncompressed = Bun.gunzipSync(compressed);
576
+ dec.decode(uncompressed);
577
+ // => "hellohellohello..."
578
+ ```
579
+
580
+ ---
581
+
582
+ ## `Bun.deflateSync()`
583
+
584
+ Compresses a `Uint8Array` using zlib's DEFLATE algorithm.
585
+
586
+ ```ts
587
+ const buf = Buffer.from("hello".repeat(100));
588
+ const compressed = Bun.deflateSync(buf);
589
+
590
+ buf; // => Buffer(500)
591
+ compressed; // => Uint8Array(12)
592
+ ```
593
+
594
+ The second argument supports the same set of configuration options as [`Bun.gzipSync`](#bun-gzipsync).
595
+
596
+ ---
597
+
598
+ ## `Bun.inflateSync()`
599
+
600
+ Decompresses a `Uint8Array` using zlib's INFLATE algorithm.
601
+
602
+ ```ts
603
+ const buf = Buffer.from("hello".repeat(100));
604
+ const compressed = Bun.deflateSync(buf);
605
+
606
+ const dec = new TextDecoder();
607
+ const decompressed = Bun.inflateSync(compressed);
608
+ dec.decode(decompressed);
609
+ // => "hellohellohello..."
610
+ ```
611
+
612
+ ---
613
+
614
+ ## `Bun.zstdCompress()` / `Bun.zstdCompressSync()`
615
+
616
+ Compresses a `Uint8Array` using the Zstandard algorithm.
617
+
618
+ ```ts
619
+ const buf = Buffer.from("hello".repeat(100));
620
+
621
+ // Synchronous
622
+ const compressedSync = Bun.zstdCompressSync(buf);
623
+ // Asynchronous
624
+ const compressedAsync = await Bun.zstdCompress(buf);
625
+
626
+ // With compression level (1-22, default: 3)
627
+ const compressedLevel = Bun.zstdCompressSync(buf, { level: 6 });
628
+ ```
629
+
630
+ ## `Bun.zstdDecompress()` / `Bun.zstdDecompressSync()`
631
+
632
+ Decompresses a `Uint8Array` using the Zstandard algorithm.
633
+
634
+ ```ts
635
+ const buf = Buffer.from("hello".repeat(100));
636
+ const compressed = Bun.zstdCompressSync(buf);
637
+
638
+ // Synchronous
639
+ const decompressedSync = Bun.zstdDecompressSync(compressed);
640
+ // Asynchronous
641
+ const decompressedAsync = await Bun.zstdDecompress(compressed);
642
+
643
+ const dec = new TextDecoder();
644
+ dec.decode(decompressedSync);
645
+ // => "hellohellohello..."
646
+ ```
647
+
648
+ ---
649
+
650
+ ## `Bun.inspect()`
651
+
652
+ Serializes an object to a `string` exactly as it would be printed by `console.log`.
653
+
654
+ ```ts
655
+ const obj = { foo: "bar" };
656
+ const str = Bun.inspect(obj);
657
+ // => '{\nfoo: "bar" \n}'
658
+
659
+ const arr = new Uint8Array([1, 2, 3]);
660
+ const str = Bun.inspect(arr);
661
+ // => "Uint8Array(3) [ 1, 2, 3 ]"
662
+ ```
663
+
664
+ ### `Bun.inspect.custom`
665
+
666
+ This is the symbol that Bun uses to implement `Bun.inspect`. You can override this to customize how your objects are printed. It is identical to `util.inspect.custom` in Node.js.
667
+
668
+ ```ts
669
+ class Foo {
670
+ [Bun.inspect.custom]() {
671
+ return "foo";
672
+ }
673
+ }
674
+
675
+ const foo = new Foo();
676
+ console.log(foo); // => "foo"
677
+ ```
678
+
679
+ ### `Bun.inspect.table(tabularData, properties, options)`
680
+
681
+ Format tabular data into a string. Like [`console.table`](https://developer.mozilla.org/en-US/docs/Web/API/console/table_static), except it returns a string rather than printing to the console.
682
+
683
+ ```ts
684
+ console.log(
685
+ Bun.inspect.table([
686
+ { a: 1, b: 2, c: 3 },
687
+ { a: 4, b: 5, c: 6 },
688
+ { a: 7, b: 8, c: 9 },
689
+ ]),
690
+ );
691
+ //
692
+ // ┌───┬───┬───┬───┐
693
+ // │ │ a │ b │ c │
694
+ // ├───┼───┼───┼───┤
695
+ // │ 0 │ 1 │ 2 │ 3 │
696
+ // │ 1 │ 4 │ 5 │ 6 │
697
+ // │ 2 │ 7 │ 8 │ 9 │
698
+ // └───┴───┴───┴───┘
699
+ ```
700
+
701
+ Additionally, you can pass an array of property names to display only a subset of properties.
702
+
703
+ ```ts
704
+ console.log(
705
+ Bun.inspect.table(
706
+ [
707
+ { a: 1, b: 2, c: 3 },
708
+ { a: 4, b: 5, c: 6 },
709
+ ],
710
+ ["a", "c"],
711
+ ),
712
+ );
713
+ //
714
+ // ┌───┬───┬───┐
715
+ // │ │ a │ c │
716
+ // ├───┼───┼───┤
717
+ // │ 0 │ 1 │ 3 │
718
+ // │ 1 │ 4 │ 6 │
719
+ // └───┴───┴───┘
720
+ ```
721
+
722
+ You can also conditionally enable ANSI colors by passing `{ colors: true }`.
723
+
724
+ ```ts
725
+ console.log(
726
+ Bun.inspect.table(
727
+ [
728
+ { a: 1, b: 2, c: 3 },
729
+ { a: 4, b: 5, c: 6 },
730
+ ],
731
+ {
732
+ colors: true,
733
+ },
734
+ ),
735
+ );
736
+ ```
737
+
738
+ ---
739
+
740
+ ## `Bun.nanoseconds()`
741
+
742
+ Returns the number of nanoseconds since the current `bun` process started, as a `number`. Useful for high-precision timing and benchmarking.
743
+
744
+ ```ts
745
+ Bun.nanoseconds();
746
+ // => 7288958
747
+ ```
748
+
749
+ ---
750
+
751
+ ## `Bun.readableStreamTo*()`
752
+
753
+ Bun implements a set of convenience functions for asynchronously consuming the body of a `ReadableStream` and converting it to various binary formats.
754
+
755
+ ```ts
756
+ const stream = (await fetch("https://bun.com")).body;
757
+ stream; // => ReadableStream
758
+
759
+ await Bun.readableStreamToArrayBuffer(stream);
760
+ // => ArrayBuffer
761
+
762
+ await Bun.readableStreamToBytes(stream);
763
+ // => Uint8Array
764
+
765
+ await Bun.readableStreamToBlob(stream);
766
+ // => Blob
767
+
768
+ await Bun.readableStreamToJSON(stream);
769
+ // => object
770
+
771
+ await Bun.readableStreamToText(stream);
772
+ // => string
773
+
774
+ // returns all chunks as an array
775
+ await Bun.readableStreamToArray(stream);
776
+ // => unknown[]
777
+
778
+ // returns all chunks as a FormData object (encoded as x-www-form-urlencoded)
779
+ await Bun.readableStreamToFormData(stream);
780
+
781
+ // returns all chunks as a FormData object (encoded as multipart/form-data)
782
+ await Bun.readableStreamToFormData(stream, multipartFormBoundary);
783
+ ```
784
+
785
+ ---
786
+
787
+ ## `Bun.resolveSync()`
788
+
789
+ Resolves a file path or module specifier using Bun's internal module resolution algorithm. The first argument is the path to resolve, and the second argument is the "root". If no match is found, an `Error` is thrown.
790
+
791
+ ```ts
792
+ Bun.resolveSync("./foo.ts", "/path/to/project");
793
+ // => "/path/to/project/foo.ts"
794
+
795
+ Bun.resolveSync("zod", "/path/to/project");
796
+ // => "/path/to/project/node_modules/zod/index.ts"
797
+ ```
798
+
799
+ To resolve relative to the current working directory, pass `process.cwd()` or `"."` as the root.
800
+
801
+ ```ts
802
+ Bun.resolveSync("./foo.ts", process.cwd());
803
+ Bun.resolveSync("./foo.ts", "/path/to/project");
804
+ ```
805
+
806
+ To resolve relative to the directory containing the current file, pass `import.meta.dir`.
807
+
808
+ ```ts
809
+ Bun.resolveSync("./foo.ts", import.meta.dir);
810
+ ```
811
+
812
+ ---
813
+
814
+ ## `Bun.stripANSI()`
815
+
816
+ <Note>~6-57x faster `strip-ansi` alternative</Note>
817
+
818
+ `Bun.stripANSI(text: string): string`
819
+
820
+ Strip ANSI escape codes from a string. This is useful for removing colors and formatting from terminal output.
821
+
822
+ ```ts
823
+ const coloredText = "\u001b[31mHello\u001b[0m \u001b[32mWorld\u001b[0m";
824
+ const plainText = Bun.stripANSI(coloredText);
825
+ console.log(plainText); // => "Hello World"
826
+
827
+ // Works with various ANSI codes
828
+ const formatted = "\u001b[1m\u001b[4mBold and underlined\u001b[0m";
829
+ console.log(Bun.stripANSI(formatted)); // => "Bold and underlined"
830
+ ```
831
+
832
+ `Bun.stripANSI` is significantly faster than the popular [`strip-ansi`](https://www.npmjs.com/package/strip-ansi) npm package:
833
+
834
+ ```bash terminal icon="terminal"
835
+ bun bench/snippets/strip-ansi.mjs
836
+ ```
837
+
838
+ ```txt
839
+ cpu: Apple M3 Max
840
+ runtime: bun 1.2.21 (arm64-darwin)
841
+
842
+ benchmark avg (min … max) p75 / p99
843
+ ------------------------------------------------------- ----------
844
+ Bun.stripANSI 11 chars no-ansi 8.13 ns/iter 8.27 ns
845
+ (7.45 ns … 33.59 ns) 10.29 ns
846
+
847
+ Bun.stripANSI 13 chars ansi 51.68 ns/iter 52.51 ns
848
+ (46.16 ns … 113.71 ns) 57.71 ns
849
+
850
+ Bun.stripANSI 16,384 chars long-no-ansi 298.39 ns/iter 305.44 ns
851
+ (281.50 ns … 331.65 ns) 320.70 ns
852
+
853
+ Bun.stripANSI 212,992 chars long-ansi 227.65 µs/iter 234.50 µs
854
+ (216.46 µs … 401.92 µs) 262.25 µs
855
+ ```
856
+
857
+ ```bash terminal icon="terminal"
858
+ node bench/snippets/strip-ansi.mjs
859
+ ```
860
+
861
+ ```txt
862
+ cpu: Apple M3 Max
863
+ runtime: node 24.6.0 (arm64-darwin)
864
+
865
+ benchmark avg (min … max) p75 / p99
866
+ -------------------------------------------------------- ---------
867
+ npm/strip-ansi 11 chars no-ansi 466.79 ns/iter 468.67 ns
868
+ (454.08 ns … 570.67 ns) 543.67 ns
869
+
870
+ npm/strip-ansi 13 chars ansi 546.77 ns/iter 550.23 ns
871
+ (532.74 ns … 651.08 ns) 590.35 ns
872
+
873
+ npm/strip-ansi 16,384 chars long-no-ansi 4.85 µs/iter 4.89 µs
874
+ (4.71 µs … 5.00 µs) 4.98 µs
875
+
876
+ npm/strip-ansi 212,992 chars long-ansi 1.36 ms/iter 1.38 ms
877
+ (1.27 ms … 1.73 ms) 1.49 ms
878
+
879
+ ```
880
+
881
+ ---
882
+
883
+ ## `serialize` & `deserialize` in `bun:jsc`
884
+
885
+ To save a JavaScript value into an ArrayBuffer & back, use `serialize` and `deserialize` from the `"bun:jsc"` module.
886
+
887
+ ```js
888
+ import { serialize, deserialize } from "bun:jsc";
889
+
890
+ const buf = serialize({ foo: "bar" });
891
+ const obj = deserialize(buf);
892
+ console.log(obj); // => { foo: "bar" }
893
+ ```
894
+
895
+ Internally, [`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone) and [`postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) serialize and deserialize the same way. This exposes the underlying [HTML Structured Clone Algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) to JavaScript as an ArrayBuffer.
896
+
897
+ ---
898
+
899
+ ## `estimateShallowMemoryUsageOf` in `bun:jsc`
900
+
901
+ The `estimateShallowMemoryUsageOf` function returns a best-effort estimate of the memory usage of an object in bytes, excluding the memory usage of properties or other objects it references. For accurate per-object memory usage, use `Bun.generateHeapSnapshot`.
902
+
903
+ ```js
904
+ import { estimateShallowMemoryUsageOf } from "bun:jsc";
905
+
906
+ const obj = { foo: "bar" };
907
+ const usage = estimateShallowMemoryUsageOf(obj);
908
+ console.log(usage); // => 16
909
+
910
+ const buffer = Buffer.alloc(1024 * 1024);
911
+ estimateShallowMemoryUsageOf(buffer);
912
+ // => 1048624
913
+
914
+ const req = new Request("https://bun.com");
915
+ estimateShallowMemoryUsageOf(req);
916
+ // => 167
917
+
918
+ const array = Array(1024).fill({ a: 1 });
919
+ // Arrays are usually not stored contiguously in memory, so this will not return a useful value (which isn't a bug).
920
+ estimateShallowMemoryUsageOf(array);
921
+ // => 16
922
+ ```