bun-types 1.3.2-canary.20251105T140650 → 1.3.2-canary.20251106T140813

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