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,232 @@
1
+ ---
2
+ title: Streams
3
+ description: Use Bun's streams API to work with binary data without loading it all into memory at once
4
+ ---
5
+
6
+ Streams are an important abstraction for working with binary data without loading it all into memory at once. They are commonly used for reading and writing files, sending and receiving network requests, and processing large amounts of data.
7
+
8
+ Bun implements the Web APIs [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) and [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream).
9
+
10
+ <Note>
11
+ Bun also implements the `node:stream` module, including
12
+ [`Readable`](https://nodejs.org/api/stream.html#stream_readable_streams),
13
+ [`Writable`](https://nodejs.org/api/stream.html#stream_writable_streams), and
14
+ [`Duplex`](https://nodejs.org/api/stream.html#stream_duplex_and_transform_streams). For complete documentation, refer
15
+ to the [Node.js docs](https://nodejs.org/api/stream.html).
16
+ </Note>
17
+
18
+ To create a simple `ReadableStream`:
19
+
20
+ ```ts
21
+ const stream = new ReadableStream({
22
+ start(controller) {
23
+ controller.enqueue("hello");
24
+ controller.enqueue("world");
25
+ controller.close();
26
+ },
27
+ });
28
+ ```
29
+
30
+ The contents of a `ReadableStream` can be read chunk-by-chunk with `for await` syntax.
31
+
32
+ ```ts
33
+ for await (const chunk of stream) {
34
+ console.log(chunk);
35
+ }
36
+
37
+ // hello
38
+ // world
39
+ ```
40
+
41
+ ---
42
+
43
+ ## Direct `ReadableStream`
44
+
45
+ Bun implements an optimized version of `ReadableStream` that avoid unnecessary data copying & queue management logic.
46
+
47
+ With a traditional `ReadableStream`, chunks of data are _enqueued_. Each chunk is copied into a queue, where it sits until the stream is ready to send more data.
48
+
49
+ ```ts
50
+ const stream = new ReadableStream({
51
+ start(controller) {
52
+ controller.enqueue("hello");
53
+ controller.enqueue("world");
54
+ controller.close();
55
+ },
56
+ });
57
+ ```
58
+
59
+ With a direct `ReadableStream`, chunks of data are written directly to the stream. No queueing happens, and there's no need to clone the chunk data into memory. The `controller` API is updated to reflect this; instead of `.enqueue()` you call `.write`.
60
+
61
+ ```ts
62
+ const stream = new ReadableStream({
63
+ type: "direct", // [!code ++]
64
+ pull(controller) {
65
+ controller.write("hello");
66
+ controller.write("world");
67
+ },
68
+ });
69
+ ```
70
+
71
+ When using a direct `ReadableStream`, all chunk queueing is handled by the destination. The consumer of the stream receives exactly what is passed to `controller.write()`, without any encoding or modification.
72
+
73
+ ---
74
+
75
+ ## Async generator streams
76
+
77
+ Bun also supports async generator functions as a source for `Response` and `Request`. This is an easy way to create a `ReadableStream` that fetches data from an asynchronous source.
78
+
79
+ ```ts
80
+ const response = new Response(
81
+ (async function* () {
82
+ yield "hello";
83
+ yield "world";
84
+ })(),
85
+ );
86
+
87
+ await response.text(); // "helloworld"
88
+ ```
89
+
90
+ You can also use `[Symbol.asyncIterator]` directly.
91
+
92
+ ```ts
93
+ const response = new Response({
94
+ [Symbol.asyncIterator]: async function* () {
95
+ yield "hello";
96
+ yield "world";
97
+ },
98
+ });
99
+
100
+ await response.text(); // "helloworld"
101
+ ```
102
+
103
+ If you need more granular control over the stream, `yield` will return the direct ReadableStream controller.
104
+
105
+ ```ts
106
+ const response = new Response({
107
+ [Symbol.asyncIterator]: async function* () {
108
+ const controller = yield "hello";
109
+ await controller.end();
110
+ },
111
+ });
112
+
113
+ await response.text(); // "hello"
114
+ ```
115
+
116
+ ---
117
+
118
+ ## `Bun.ArrayBufferSink`
119
+
120
+ The `Bun.ArrayBufferSink` class is a fast incremental writer for constructing an `ArrayBuffer` of unknown size.
121
+
122
+ ```ts
123
+ const sink = new Bun.ArrayBufferSink();
124
+
125
+ sink.write("h");
126
+ sink.write("e");
127
+ sink.write("l");
128
+ sink.write("l");
129
+ sink.write("o");
130
+
131
+ sink.end();
132
+ // ArrayBuffer(5) [ 104, 101, 108, 108, 111 ]
133
+ ```
134
+
135
+ To instead retrieve the data as a `Uint8Array`, pass the `asUint8Array` option to the `start` method.
136
+
137
+ ```ts
138
+ const sink = new Bun.ArrayBufferSink();
139
+ sink.start({
140
+ asUint8Array: true, // [!code ++]
141
+ });
142
+
143
+ sink.write("h");
144
+ sink.write("e");
145
+ sink.write("l");
146
+ sink.write("l");
147
+ sink.write("o");
148
+
149
+ sink.end();
150
+ // Uint8Array(5) [ 104, 101, 108, 108, 111 ]
151
+ ```
152
+
153
+ The `.write()` method supports strings, typed arrays, `ArrayBuffer`, and `SharedArrayBuffer`.
154
+
155
+ ```ts
156
+ sink.write("h");
157
+ sink.write(new Uint8Array([101, 108]));
158
+ sink.write(Buffer.from("lo").buffer);
159
+
160
+ sink.end();
161
+ ```
162
+
163
+ Once `.end()` is called, no more data can be written to the `ArrayBufferSink`. However, in the context of buffering a stream, it's useful to continuously write data and periodically `.flush()` the contents (say, into a `WriteableStream`). To support this, pass `stream: true` to the constructor.
164
+
165
+ ```ts
166
+ const sink = new Bun.ArrayBufferSink();
167
+ sink.start({
168
+ stream: true, // [!code ++]
169
+ });
170
+
171
+ sink.write("h");
172
+ sink.write("e");
173
+ sink.write("l");
174
+ sink.flush();
175
+ // ArrayBuffer(5) [ 104, 101, 108 ]
176
+
177
+ sink.write("l");
178
+ sink.write("o");
179
+ sink.flush();
180
+ // ArrayBuffer(5) [ 108, 111 ]
181
+ ```
182
+
183
+ The `.flush()` method returns the buffered data as an `ArrayBuffer` (or `Uint8Array` if `asUint8Array: true`) and clears internal buffer.
184
+
185
+ To manually set the size of the internal buffer in bytes, pass a value for `highWaterMark`:
186
+
187
+ ```ts
188
+ const sink = new Bun.ArrayBufferSink();
189
+ sink.start({
190
+ highWaterMark: 1024 * 1024, // 1 MB // [!code ++]
191
+ });
192
+ ```
193
+
194
+ ---
195
+
196
+ ## Reference
197
+
198
+ ```ts See Typescript Definitions expandable
199
+ /**
200
+ * Fast incremental writer that becomes an `ArrayBuffer` on end().
201
+ */
202
+ export class ArrayBufferSink {
203
+ constructor();
204
+
205
+ start(options?: {
206
+ asUint8Array?: boolean;
207
+ /**
208
+ * Preallocate an internal buffer of this size
209
+ * This can significantly improve performance when the chunk size is small
210
+ */
211
+ highWaterMark?: number;
212
+ /**
213
+ * On {@link ArrayBufferSink.flush}, return the written data as a `Uint8Array`.
214
+ * Writes will restart from the beginning of the buffer.
215
+ */
216
+ stream?: boolean;
217
+ }): void;
218
+
219
+ write(chunk: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer): number;
220
+ /**
221
+ * Flush the internal buffer
222
+ *
223
+ * If {@link ArrayBufferSink.start} was passed a `stream` option, this will return a `ArrayBuffer`
224
+ * If {@link ArrayBufferSink.start} was passed a `stream` option and `asUint8Array`, this will return a `Uint8Array`
225
+ * Otherwise, this will return the number of bytes written since the last flush
226
+ *
227
+ * This API might change later to separate Uint8ArraySink and ArrayBufferSink
228
+ */
229
+ flush(): number | Uint8Array<ArrayBuffer> | ArrayBuffer;
230
+ end(): ArrayBuffer | Uint8Array<ArrayBuffer>;
231
+ }
232
+ ```
@@ -0,0 +1,269 @@
1
+ ---
2
+ title: bun create
3
+ description: Create a new Bun project from a React component, a `create-<template>` npm package, a GitHub repo, or a local template
4
+ ---
5
+
6
+ <Note>
7
+ You don't need `bun create` to use Bun. You don't need any configuration at all. This command exists to make getting
8
+ started a bit quicker and easier.
9
+ </Note>
10
+
11
+ ---
12
+
13
+ Template a new Bun project with `bun create`. This is a flexible command that can be used to create a new project from a React component, a `create-<template>` npm package, a GitHub repo, or a local template.
14
+
15
+ If you're looking to create a brand new empty project, use [`bun init`](https://bun.com/docs/cli/init).
16
+
17
+ ## From a React component
18
+
19
+ `bun create ./MyComponent.tsx` turns an existing React component into a complete dev environment with hot reload and production builds in one command.
20
+
21
+ ```bash
22
+ $ bun create ./MyComponent.jsx # .tsx also supported
23
+ ```
24
+
25
+ <Frame>
26
+ <video
27
+ style={{ aspectRatio: "2062 / 1344", width: "100%", height: "100%", objectFit: "contain" }}
28
+ loop
29
+ autoPlay
30
+ muted
31
+ playsInline
32
+ >
33
+ <source
34
+ src="/images/bun-create-shadcn.mp4"
35
+ style={{ width: "100%", height: "100%", objectFit: "contain" }}
36
+ type="video/mp4"
37
+ />
38
+ </video>
39
+ </Frame>
40
+
41
+ <Note>
42
+ 🚀 **Create React App Successor** — `bun create <component>` provides everything developers loved about Create React App, but with modern tooling, faster builds, and backend support.
43
+ </Note>
44
+
45
+ #### How this works
46
+
47
+ When you run `bun create <component>`, Bun:
48
+
49
+ 1. Uses [Bun's JavaScript bundler](https://bun.com/docs/bundler) to analyze your module graph.
50
+ 2. Collects all the dependencies needed to run the component.
51
+ 3. Scans the exports of the entry point for a React component.
52
+ 4. Generates a `package.json` file with the dependencies and scripts needed to run the component.
53
+ 5. Installs any missing dependencies using [`bun install --only-missing`](https://bun.com/docs/cli/install).
54
+ 6. Generates the following files:
55
+ - `${component}.html`
56
+ - `${component}.client.tsx` (entry point for the frontend)
57
+ - `${component}.css` (css file)
58
+ 7. Starts a frontend dev server automatically.
59
+
60
+ ### Using TailwindCSS with Bun
61
+
62
+ [TailwindCSS](https://tailwindcss.com/) is an extremely popular utility-first CSS framework used to style web applications.
63
+
64
+ When you run `bun create <component>`, Bun scans your JSX/TSX file for TailwindCSS class names (and any files it imports). If it detects TailwindCSS class names, it will add the following dependencies to your `package.json`:
65
+
66
+ ```json package.json icon="file-code"
67
+ {
68
+ "dependencies": {
69
+ "tailwindcss": "^4",
70
+ "bun-plugin-tailwind": "latest"
71
+ }
72
+ }
73
+ ```
74
+
75
+ We also configure `bunfig.toml` to use Bun's TailwindCSS plugin with `Bun.serve()`
76
+
77
+ ```toml bunfig.toml icon="settings"
78
+ [serve.static]
79
+ plugins = ["bun-plugin-tailwind"]
80
+ ```
81
+
82
+ And a `${component}.css` file with `@import "tailwindcss";` at the top:
83
+
84
+ ```css MyComponent.css icon="file-code"
85
+ @import "tailwindcss";
86
+ ```
87
+
88
+ ### Using `shadcn/ui` with Bun
89
+
90
+ [`shadcn/ui`](https://ui.shadcn.com/) is an extremely popular component library tool for building web applications.
91
+
92
+ `bun create <component>` scans for any shadcn/ui components imported from `@/components/ui`.
93
+
94
+ If it finds any, it runs:
95
+
96
+ ```bash terminal icon="terminal"
97
+ # Assuming bun detected imports to @/components/ui/accordion and @/components/ui/button
98
+ bunx shadcn@canary add accordion button # and any other components
99
+ ```
100
+
101
+ Since `shadcn/ui` itself uses TailwindCSS, `bun create` also adds the necessary TailwindCSS dependencies to your `package.json` and configures `bunfig.toml` to use Bun's TailwindCSS plugin with `Bun.serve()` as described above.
102
+
103
+ Additionally, we setup the following:
104
+
105
+ - `tsconfig.json` to alias `"@/*"` to `"src/*"` or `.` (depending on if there is a `src/` directory)
106
+ - `components.json` so that shadcn/ui knows its a shadcn/ui project
107
+ - `styles/globals.css` file that configures Tailwind v4 in the way that shadcn/ui expects
108
+ - `${component}.build.ts` file that builds the component for production with `bun-plugin-tailwind` configured
109
+
110
+ `bun create ./MyComponent.jsx` is one of the easiest ways to run code generated from LLMs like [Claude](https://claude.ai) or ChatGPT locally.
111
+
112
+ ## From `npm`
113
+
114
+ ```sh terminal icon="terminal"
115
+ bun create <template> [<destination>]
116
+ ```
117
+
118
+ Assuming you don't have a [local template](#from-a-local-template) with the same name, this command will download and execute the `create-<template>` package from npm. The following two commands will behave identically:
119
+
120
+ ```sh terminal icon="terminal"
121
+ bun create remix
122
+ bunx create-remix
123
+ ```
124
+
125
+ Refer to the documentation of the associated `create-<template>` package for complete documentation and usage instructions.
126
+
127
+ ## From GitHub
128
+
129
+ This will download the contents of the GitHub repo to disk.
130
+
131
+ ```bash terminal icon="terminal"
132
+ bun create <user>/<repo>
133
+ bun create github.com/<user>/<repo>
134
+ ```
135
+
136
+ Optionally specify a name for the destination folder. If no destination is specified, the repo name will be used.
137
+
138
+ ```bash terminal icon="terminal"
139
+ bun create <user>/<repo> mydir
140
+ bun create github.com/<user>/<repo> mydir
141
+ ```
142
+
143
+ Bun will perform the following steps:
144
+
145
+ - Download the template
146
+ - Copy all template files into the destination folder
147
+ - Install dependencies with `bun install`.
148
+ - Initialize a fresh Git repo. Opt out with the `--no-git` flag.
149
+ - Run the template's configured `start` script, if defined.
150
+
151
+ <Note>By default Bun will _not overwrite_ any existing files. Use the `--force` flag to overwrite existing files.</Note>
152
+
153
+ ## From a local template
154
+
155
+ <Warning>
156
+ Unlike remote templates, running `bun create` with a local template will delete the entire destination folder if it
157
+ already exists! Be careful.
158
+ </Warning>
159
+
160
+ Bun's templater can be extended to support custom templates defined on your local file system. These templates should live in one of the following directories:
161
+
162
+ - `$HOME/.bun-create/<name>`: global templates
163
+ - `<project root>/.bun-create/<name>`: project-specific templates
164
+
165
+ <Note>You can customize the global template path by setting the `BUN_CREATE_DIR` environment variable.</Note>
166
+
167
+ To create a local template, navigate to `$HOME/.bun-create` and create a new directory with the desired name of your template.
168
+
169
+ ```bash
170
+ cd $HOME/.bun-create
171
+ mkdir foo
172
+ cd foo
173
+ ```
174
+
175
+ Then, create a `package.json` file in that directory with the following contents:
176
+
177
+ ```json package.json icon="file-json"
178
+ {
179
+ "name": "foo"
180
+ }
181
+ ```
182
+
183
+ You can run `bun create foo` elsewhere on your file system to verify that Bun is correctly finding your local template.
184
+
185
+ #### Setup logic
186
+
187
+ You can specify pre- and post-install setup scripts in the `"bun-create"` section of your local template's `package.json`.
188
+
189
+ ```json package.json icon="file-json"
190
+ {
191
+ "name": "@bun-examples/simplereact",
192
+ "version": "0.0.1",
193
+ "main": "index.js",
194
+ "dependencies": {
195
+ "react": "^17.0.2",
196
+ "react-dom": "^17.0.2"
197
+ },
198
+ "bun-create": {
199
+ "preinstall": "echo 'Installing...'", // a single command
200
+ "postinstall": ["echo 'Done!'"], // an array of commands
201
+ "start": "bun run echo 'Hello world!'"
202
+ }
203
+ }
204
+ ```
205
+
206
+ The following fields are supported. Each of these can correspond to a string or array of strings. An array of commands will be executed in order.
207
+
208
+ | Field | Description |
209
+ | ------------- | ----------------------------------- |
210
+ | `postinstall` | runs after installing dependencies |
211
+ | `preinstall` | runs before installing dependencies |
212
+
213
+ After cloning a template, `bun create` will automatically remove the `"bun-create"` section from `package.json` before writing it to the destination folder.
214
+
215
+ ## Reference
216
+
217
+ ### CLI flags
218
+
219
+ | Flag | Description |
220
+ | -------------- | -------------------------------------- |
221
+ | `--force` | Overwrite existing files |
222
+ | `--no-install` | Skip installing `node_modules` & tasks |
223
+ | `--no-git` | Don't initialize a git repository |
224
+ | `--open` | Start & open in-browser after finish |
225
+
226
+ ### Environment variables
227
+
228
+ | Name | Description |
229
+ | ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
230
+ | `GITHUB_API_DOMAIN` | If you're using a GitHub enterprise or a proxy, you can customize the GitHub domain Bun pings for downloads |
231
+ | `GITHUB_TOKEN` (or `GITHUB_ACCESS_TOKEN`) | This lets `bun create` work with private repositories or if you get rate-limited. `GITHUB_TOKEN` is chosen over `GITHUB_ACCESS_TOKEN` if both exist. |
232
+
233
+ <Accordion title={<span>How <code>bun create</code> works</span>}>
234
+
235
+ When you run `bun create ${template} ${destination}`, here’s what happens:
236
+
237
+ IF remote template
238
+
239
+ 1. GET `registry.npmjs.org/@bun-examples/${template}/latest` and parse it
240
+ 2. GET `registry.npmjs.org/@bun-examples/${template}/-/${template}-${latestVersion}.tgz`
241
+ 3. Decompress & extract `${template}-${latestVersion}.tgz` into `${destination}`
242
+ - If there are files that would overwrite, warn and exit unless `--force` is passed
243
+
244
+ IF GitHub repo
245
+
246
+ 1. Download the tarball from GitHub’s API
247
+ 2. Decompress & extract into `${destination}`
248
+ - If there are files that would overwrite, warn and exit unless `--force` is passed
249
+
250
+ ELSE IF local template
251
+
252
+ 1. Open local template folder
253
+ 2. Delete destination directory recursively
254
+ 3. Copy files recursively using the fastest system calls available (on macOS `fcopyfile` and Linux, `copy_file_range`). Do not copy or traverse into `node_modules` folder if exists (this alone makes it faster than `cp`)
255
+
256
+ 4. Parse the `package.json` (again!), update `name` to be `${basename(destination)}`, remove the `bun-create` section from the `package.json` and save the updated `package.json` to disk.
257
+ - IF Next.js is detected, add `bun-framework-next` to the list of dependencies
258
+ - IF Create React App is detected, add the entry point in `/src/index.{js,jsx,ts,tsx}` to `public/index.html`
259
+ - IF Relay is detected, add `bun-macro-relay` so that Relay works
260
+ 5. Auto-detect the npm client, preferring `pnpm`, `yarn` (v1), and lastly `npm`
261
+ 6. Run any tasks defined in `"bun-create": { "preinstall" }` with the npm client
262
+ 7. Run `${npmClient} install` unless `--no-install` is passed OR no dependencies are in package.json
263
+ 8. Run any tasks defined in `"bun-create": { "postinstall" }` with the npm client
264
+ 9. Run `git init; git add -A .; git commit -am "Initial Commit";`
265
+ - Rename `gitignore` to `.gitignore`. NPM automatically removes `.gitignore` files from appearing in packages.
266
+ - If there are dependencies, this runs in a separate thread concurrently while node_modules are being installed
267
+ - Using libgit2 if available was tested and performed 3x slower in microbenchmarks
268
+
269
+ </Accordion>
@@ -0,0 +1,58 @@
1
+ ---
2
+ title: "bun init"
3
+ description: "Scaffold an empty Bun project with the interactive `bun init` command"
4
+ ---
5
+
6
+ import Init from "/snippets/cli/init.mdx";
7
+
8
+ Get started with Bun by scaffolding a new project with `bun init`.
9
+
10
+ ```bash terminal icon="terminal"
11
+ bun init my-app
12
+ ```
13
+
14
+ ```txt
15
+ ? Select a project template - Press return to submit.
16
+ ❯ Blank
17
+ React
18
+ Library
19
+
20
+ ✓ Select a project template: Blank
21
+
22
+ + .gitignore
23
+ + CLAUDE.md
24
+ + .cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc -> CLAUDE.md
25
+ + index.ts
26
+ + tsconfig.json (for editor autocomplete)
27
+ + README.md
28
+ ```
29
+
30
+ Press `enter` to accept the default answer for each prompt, or pass the `-y` flag to auto-accept the defaults.
31
+
32
+ ---
33
+
34
+ `bun init` is a quick way to start a blank project with Bun. It guesses with sane defaults and is non-destructive when run multiple times.
35
+
36
+ <Frame>
37
+ ![Demo](https://user-images.githubusercontent.com/709451/183006613-271960a3-ff22-4f7c-83f5-5e18f684c836.gif)
38
+ </Frame>
39
+
40
+ It creates:
41
+
42
+ - a `package.json` file with a name that defaults to the current directory name
43
+ - a `tsconfig.json` file or a `jsconfig.json` file, depending if the entry point is a TypeScript file or not
44
+ - an entry point which defaults to `index.ts` unless any of `index.{tsx, jsx, js, mts, mjs}` exist or the `package.json` specifies a `module` or `main` field
45
+ - a `README.md` file
46
+
47
+ AI Agent rules (disable with `$BUN_AGENT_RULE_DISABLED=1`):
48
+
49
+ - a `CLAUDE.md` file when Claude CLI is detected (disable with `CLAUDE_CODE_AGENT_RULE_DISABLED` env var)
50
+ - a `.cursor/rules/*.mdc` file to guide [Cursor AI](https://cursor.sh) to use Bun instead of Node.js and npm when Cursor is detected
51
+
52
+ If you pass `-y` or `--yes`, it will assume you want to continue without asking questions.
53
+
54
+ At the end, it runs `bun install` to install `@types/bun`.
55
+
56
+ ---
57
+
58
+ <Init />