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,468 @@
1
+ ---
2
+ title: Fetch
3
+ description: Send HTTP requests with Bun's fetch API
4
+ ---
5
+
6
+ Bun implements the WHATWG `fetch` standard, with some extensions to meet the needs of server-side JavaScript.
7
+
8
+ Bun also implements `node:http`, but `fetch` is generally recommended instead.
9
+
10
+ ## Sending an HTTP request
11
+
12
+ To send an HTTP request, use `fetch`
13
+
14
+ ```ts
15
+ const response = await fetch("http://example.com");
16
+
17
+ console.log(response.status); // => 200
18
+
19
+ const text = await response.text(); // or response.json(), response.formData(), etc.
20
+ ```
21
+
22
+ `fetch` also works with HTTPS URLs.
23
+
24
+ ```ts
25
+ const response = await fetch("https://example.com");
26
+ ```
27
+
28
+ You can also pass `fetch` a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) object.
29
+
30
+ ```ts
31
+ const request = new Request("http://example.com", {
32
+ method: "POST",
33
+ body: "Hello, world!",
34
+ });
35
+
36
+ const response = await fetch(request);
37
+ ```
38
+
39
+ ### Sending a POST request
40
+
41
+ To send a POST request, pass an object with the `method` property set to `"POST"`.
42
+
43
+ ```ts
44
+ const response = await fetch("http://example.com", {
45
+ method: "POST",
46
+ body: "Hello, world!",
47
+ });
48
+ ```
49
+
50
+ `body` can be a string, a `FormData` object, an `ArrayBuffer`, a `Blob`, and more. See the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body) for more information.
51
+
52
+ ### Proxying requests
53
+
54
+ To proxy a request, pass an object with the `proxy` property set to a URL.
55
+
56
+ ```ts
57
+ const response = await fetch("http://example.com", {
58
+ proxy: "http://proxy.com",
59
+ });
60
+ ```
61
+
62
+ ### Custom headers
63
+
64
+ To set custom headers, pass an object with the `headers` property set to an object.
65
+
66
+ ```ts
67
+ const response = await fetch("http://example.com", {
68
+ headers: {
69
+ "X-Custom-Header": "value",
70
+ },
71
+ });
72
+ ```
73
+
74
+ You can also set headers using the [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) object.
75
+
76
+ ```ts
77
+ const headers = new Headers();
78
+ headers.append("X-Custom-Header", "value");
79
+
80
+ const response = await fetch("http://example.com", {
81
+ headers,
82
+ });
83
+ ```
84
+
85
+ ### Response bodies
86
+
87
+ To read the response body, use one of the following methods:
88
+
89
+ - `response.text(): Promise<string>`: Returns a promise that resolves with the response body as a string.
90
+ - `response.json(): Promise<any>`: Returns a promise that resolves with the response body as a JSON object.
91
+ - `response.formData(): Promise<FormData>`: Returns a promise that resolves with the response body as a `FormData` object.
92
+ - `response.bytes(): Promise<Uint8Array>`: Returns a promise that resolves with the response body as a `Uint8Array`.
93
+ - `response.arrayBuffer(): Promise<ArrayBuffer>`: Returns a promise that resolves with the response body as an `ArrayBuffer`.
94
+ - `response.blob(): Promise<Blob>`: Returns a promise that resolves with the response body as a `Blob`.
95
+
96
+ #### Streaming response bodies
97
+
98
+ You can use async iterators to stream the response body.
99
+
100
+ ```ts
101
+ const response = await fetch("http://example.com");
102
+
103
+ for await (const chunk of response.body) {
104
+ console.log(chunk);
105
+ }
106
+ ```
107
+
108
+ You can also more directly access the `ReadableStream` object.
109
+
110
+ ```ts
111
+ const response = await fetch("http://example.com");
112
+
113
+ const stream = response.body;
114
+
115
+ const reader = stream.getReader();
116
+ const { value, done } = await reader.read();
117
+ ```
118
+
119
+ ### Streaming request bodies
120
+
121
+ You can also stream data in request bodies using a `ReadableStream`:
122
+
123
+ ```ts
124
+ const stream = new ReadableStream({
125
+ start(controller) {
126
+ controller.enqueue("Hello");
127
+ controller.enqueue(" ");
128
+ controller.enqueue("World");
129
+ controller.close();
130
+ },
131
+ });
132
+
133
+ const response = await fetch("http://example.com", {
134
+ method: "POST",
135
+ body: stream,
136
+ });
137
+ ```
138
+
139
+ When using streams with HTTP(S):
140
+
141
+ - The data is streamed directly to the network without buffering the entire body in memory
142
+ - If the connection is lost, the stream will be canceled
143
+ - The `Content-Length` header is not automatically set unless the stream has a known size
144
+
145
+ When using streams with S3:
146
+
147
+ - For PUT/POST requests, Bun automatically uses multipart upload
148
+ - The stream is consumed in chunks and uploaded in parallel
149
+ - Progress can be monitored through the S3 options
150
+
151
+ ### Fetching a URL with a timeout
152
+
153
+ To fetch a URL with a timeout, use `AbortSignal.timeout`:
154
+
155
+ ```ts
156
+ const response = await fetch("http://example.com", {
157
+ signal: AbortSignal.timeout(1000),
158
+ });
159
+ ```
160
+
161
+ #### Canceling a request
162
+
163
+ To cancel a request, use an `AbortController`:
164
+
165
+ ```ts
166
+ const controller = new AbortController();
167
+
168
+ const response = await fetch("http://example.com", {
169
+ signal: controller.signal,
170
+ });
171
+
172
+ controller.abort();
173
+ ```
174
+
175
+ ### Unix domain sockets
176
+
177
+ To fetch a URL using a Unix domain socket, use the `unix: string` option:
178
+
179
+ ```ts
180
+ const response = await fetch("https://hostname/a/path", {
181
+ unix: "/var/run/path/to/unix.sock",
182
+ method: "POST",
183
+ body: JSON.stringify({ message: "Hello from Bun!" }),
184
+ headers: {
185
+ "Content-Type": "application/json",
186
+ },
187
+ });
188
+ ```
189
+
190
+ ### TLS
191
+
192
+ To use a client certificate, use the `tls` option:
193
+
194
+ ```ts
195
+ await fetch("https://example.com", {
196
+ tls: {
197
+ key: Bun.file("/path/to/key.pem"),
198
+ cert: Bun.file("/path/to/cert.pem"),
199
+ // ca: [Bun.file("/path/to/ca.pem")],
200
+ },
201
+ });
202
+ ```
203
+
204
+ #### Custom TLS Validation
205
+
206
+ To customize the TLS validation, use the `checkServerIdentity` option in `tls`
207
+
208
+ ```ts
209
+ await fetch("https://example.com", {
210
+ tls: {
211
+ checkServerIdentity: (hostname, peerCertificate) => {
212
+ // Return an Error if the certificate is invalid
213
+ },
214
+ },
215
+ });
216
+ ```
217
+
218
+ This is similar to how it works in Node's `net` module.
219
+
220
+ #### Disable TLS validation
221
+
222
+ To disable TLS validation, set `rejectUnauthorized` to `false`:
223
+
224
+ ```ts
225
+ await fetch("https://example.com", {
226
+ tls: {
227
+ rejectUnauthorized: false,
228
+ },
229
+ });
230
+ ```
231
+
232
+ This is especially useful to avoid SSL errors when using self-signed certificates, but this disables TLS validation and should be used with caution.
233
+
234
+ ### Request options
235
+
236
+ In addition to the standard fetch options, Bun provides several extensions:
237
+
238
+ ```ts
239
+ const response = await fetch("http://example.com", {
240
+ // Control automatic response decompression (default: true)
241
+ // Supports gzip, deflate, brotli (br), and zstd
242
+ decompress: true,
243
+
244
+ // Disable connection reuse for this request
245
+ keepalive: false,
246
+
247
+ // Debug logging level
248
+ verbose: true, // or "curl" for more detailed output
249
+ });
250
+ ```
251
+
252
+ ### Protocol support
253
+
254
+ Beyond HTTP(S), Bun's fetch supports several additional protocols:
255
+
256
+ #### S3 URLs - `s3://`
257
+
258
+ Bun supports fetching from S3 buckets directly.
259
+
260
+ ```ts
261
+ // Using environment variables for credentials
262
+ const response = await fetch("s3://my-bucket/path/to/object");
263
+
264
+ // Or passing credentials explicitly
265
+ const response = await fetch("s3://my-bucket/path/to/object", {
266
+ s3: {
267
+ accessKeyId: "YOUR_ACCESS_KEY",
268
+ secretAccessKey: "YOUR_SECRET_KEY",
269
+ region: "us-east-1",
270
+ },
271
+ });
272
+ ```
273
+
274
+ Note: Only PUT and POST methods support request bodies when using S3. For uploads, Bun automatically uses multipart upload for streaming bodies.
275
+
276
+ You can read more about Bun's S3 support in the [S3](/runtime/s3) documentation.
277
+
278
+ #### File URLs - `file://`
279
+
280
+ You can fetch local files using the `file:` protocol:
281
+
282
+ ```ts
283
+ const response = await fetch("file:///path/to/file.txt");
284
+ const text = await response.text();
285
+ ```
286
+
287
+ On Windows, paths are automatically normalized:
288
+
289
+ ```ts
290
+ // Both work on Windows
291
+ const response = await fetch("file:///C:/path/to/file.txt");
292
+ const response2 = await fetch("file:///c:/path\\to/file.txt");
293
+ ```
294
+
295
+ #### Data URLs - `data:`
296
+
297
+ Bun supports the `data:` URL scheme:
298
+
299
+ ```ts
300
+ const response = await fetch("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==");
301
+ const text = await response.text(); // "Hello, World!"
302
+ ```
303
+
304
+ #### Blob URLs - `blob:`
305
+
306
+ You can fetch blobs using URLs created by `URL.createObjectURL()`:
307
+
308
+ ```ts
309
+ const blob = new Blob(["Hello, World!"], { type: "text/plain" });
310
+ const url = URL.createObjectURL(blob);
311
+ const response = await fetch(url);
312
+ ```
313
+
314
+ ### Error handling
315
+
316
+ Bun's fetch implementation includes several specific error cases:
317
+
318
+ - Using a request body with GET/HEAD methods will throw an error (which is expected for the fetch API)
319
+ - Attempting to use both `proxy` and `unix` options together will throw an error
320
+ - TLS certificate validation failures when `rejectUnauthorized` is true (or undefined)
321
+ - S3 operations may throw specific errors related to authentication or permissions
322
+
323
+ ### Content-Type handling
324
+
325
+ Bun automatically sets the `Content-Type` header for request bodies when not explicitly provided:
326
+
327
+ - For `Blob` objects, uses the blob's `type`
328
+ - For `FormData`, sets appropriate multipart boundary
329
+
330
+ ## Debugging
331
+
332
+ To help with debugging, you can pass `verbose: true` to `fetch`:
333
+
334
+ ```ts
335
+ const response = await fetch("http://example.com", {
336
+ verbose: true,
337
+ });
338
+ ```
339
+
340
+ This will print the request and response headers to your terminal:
341
+
342
+ ```sh
343
+ [fetch] > HTTP/1.1 GET http://example.com/
344
+ [fetch] > Connection: keep-alive
345
+ [fetch] > User-Agent: Bun/1.3.1
346
+ [fetch] > Accept: */*
347
+ [fetch] > Host: example.com
348
+ [fetch] > Accept-Encoding: gzip, deflate, br, zstd
349
+
350
+ [fetch] < 200 OK
351
+ [fetch] < Content-Encoding: gzip
352
+ [fetch] < Age: 201555
353
+ [fetch] < Cache-Control: max-age=604800
354
+ [fetch] < Content-Type: text/html; charset=UTF-8
355
+ [fetch] < Date: Sun, 21 Jul 2024 02:41:14 GMT
356
+ [fetch] < Etag: "3147526947+gzip"
357
+ [fetch] < Expires: Sun, 28 Jul 2024 02:41:14 GMT
358
+ [fetch] < Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
359
+ [fetch] < Server: ECAcc (sac/254F)
360
+ [fetch] < Vary: Accept-Encoding
361
+ [fetch] < X-Cache: HIT
362
+ [fetch] < Content-Length: 648
363
+ ```
364
+
365
+ Note: `verbose: boolean` is not part of the Web standard `fetch` API and is specific to Bun.
366
+
367
+ ## Performance
368
+
369
+ Before an HTTP request can be sent, the DNS lookup must be performed. This can take a significant amount of time, especially if the DNS server is slow or the network connection is poor.
370
+
371
+ After the DNS lookup, the TCP socket must be connected and the TLS handshake might need to be performed. This can also take a significant amount of time.
372
+
373
+ After the request completes, consuming the response body can also take a significant amount of time and memory.
374
+
375
+ At every step of the way, Bun provides APIs to help you optimize the performance of your application.
376
+
377
+ ### DNS prefetching
378
+
379
+ To prefetch a DNS entry, you can use the `dns.prefetch` API. This API is useful when you know you'll need to connect to a host soon and want to avoid the initial DNS lookup.
380
+
381
+ ```ts
382
+ import { dns } from "bun";
383
+
384
+ dns.prefetch("bun.com");
385
+ ```
386
+
387
+ #### DNS caching
388
+
389
+ By default, Bun caches and deduplicates DNS queries in-memory for up to 30 seconds. You can see the cache stats by calling `dns.getCacheStats()`:
390
+
391
+ To learn more about DNS caching in Bun, see the [DNS caching](/runtime/networking/dns) documentation.
392
+
393
+ ### Preconnect to a host
394
+
395
+ To preconnect to a host, you can use the `fetch.preconnect` API. This API is useful when you know you'll need to connect to a host soon and want to start the initial DNS lookup, TCP socket connection, and TLS handshake early.
396
+
397
+ ```ts
398
+ import { fetch } from "bun";
399
+
400
+ fetch.preconnect("https://bun.com");
401
+ ```
402
+
403
+ Note: calling `fetch` immediately after `fetch.preconnect` will not make your request faster. Preconnecting only helps if you know you'll need to connect to a host soon, but you're not ready to make the request yet.
404
+
405
+ #### Preconnect at startup
406
+
407
+ To preconnect to a host at startup, you can pass `--fetch-preconnect`:
408
+
409
+ ```sh
410
+ bun --fetch-preconnect https://bun.com ./my-script.ts
411
+ ```
412
+
413
+ This is sort of like `<link rel="preconnect">` in HTML.
414
+
415
+ This feature is not implemented on Windows yet. If you're interested in using this feature on Windows, please file an issue and we can implement support for it on Windows.
416
+
417
+ ### Connection pooling & HTTP keep-alive
418
+
419
+ Bun automatically reuses connections to the same host. This is known as connection pooling. This can significantly reduce the time it takes to establish a connection. You don't need to do anything to enable this; it's automatic.
420
+
421
+ #### Simultaneous connection limit
422
+
423
+ By default, Bun limits the maximum number of simultaneous `fetch` requests to 256. We do this for several reasons:
424
+
425
+ - It improves overall system stability. Operating systems have an upper limit on the number of simultaneous open TCP sockets, usually in the low thousands. Nearing this limit causes your entire computer to behave strangely. Applications hang and crash.
426
+ - It encourages HTTP Keep-Alive connection reuse. For short-lived HTTP requests, the slowest step is often the initial connection setup. Reusing connections can save a lot of time.
427
+
428
+ When the limit is exceeded, the requests are queued and sent as soon as the next request ends.
429
+
430
+ You can increase the maximum number of simultaneous connections via the `BUN_CONFIG_MAX_HTTP_REQUESTS` environment variable:
431
+
432
+ ```sh
433
+ bun_CONFIG_MAX_HTTP_REQUESTS=512 bun ./my-script.ts
434
+ ```
435
+
436
+ The max value for this limit is currently set to 65,336. The maximum port number is 65,535, so it's quite difficult for any one computer to exceed this limit.
437
+
438
+ ### Response buffering
439
+
440
+ Bun goes to great lengths to optimize the performance of reading the response body. The fastest way to read the response body is to use one of these methods:
441
+
442
+ - `response.text(): Promise<string>`
443
+ - `response.json(): Promise<any>`
444
+ - `response.formData(): Promise<FormData>`
445
+ - `response.bytes(): Promise<Uint8Array>`
446
+ - `response.arrayBuffer(): Promise<ArrayBuffer>`
447
+ - `response.blob(): Promise<Blob>`
448
+
449
+ You can also use `Bun.write` to write the response body to a file on disk:
450
+
451
+ ```ts
452
+ import { write } from "bun";
453
+
454
+ await write("output.txt", response);
455
+ ```
456
+
457
+ ### Implementation details
458
+
459
+ - Connection pooling is enabled by default but can be disabled per-request with `keepalive: false`. The `"Connection: close"` header can also be used to disable keep-alive.
460
+ - Large file uploads are optimized using the operating system's `sendfile` syscall under specific conditions:
461
+ - The file must be larger than 32KB
462
+ - The request must not be using a proxy
463
+ - On macOS, only regular files (not pipes, sockets, or devices) can use `sendfile`
464
+ - When these conditions aren't met, or when using S3/streaming uploads, Bun falls back to reading the file into memory
465
+ - This optimization is particularly effective for HTTP (not HTTPS) requests where the file can be sent directly from the kernel to the network stack
466
+ - S3 operations automatically handle signing requests and merging authentication headers
467
+
468
+ Note: Many of these features are Bun-specific extensions to the standard fetch API.
@@ -0,0 +1,239 @@
1
+ ---
2
+ title: TCP
3
+ description: Use Bun's native TCP API to implement performance sensitive systems like database clients, game servers, or anything that needs to communicate over TCP (instead of HTTP)
4
+ ---
5
+
6
+ This is a low-level API intended for library authors and for advanced use cases.
7
+
8
+ ## Start a server (`Bun.listen()`)
9
+
10
+ To start a TCP server with `Bun.listen`:
11
+
12
+ ```ts server.ts icon="/icons/typescript.svg"
13
+ Bun.listen({
14
+ hostname: "localhost",
15
+ port: 8080,
16
+ socket: {
17
+ data(socket, data) {}, // message received from client
18
+ open(socket) {}, // socket opened
19
+ close(socket, error) {}, // socket closed
20
+ drain(socket) {}, // socket ready for more data
21
+ error(socket, error) {}, // error handler
22
+ },
23
+ });
24
+ ```
25
+
26
+ <Accordion title="An API designed for speed">
27
+
28
+ In Bun, a set of handlers are declared once per server instead of assigning callbacks to each socket, as with Node.js `EventEmitters` or the web-standard `WebSocket` API.
29
+
30
+ ```ts server.ts icon="/icons/typescript.svg"
31
+ Bun.listen({
32
+ hostname: "localhost",
33
+ port: 8080,
34
+ socket: {
35
+ open(socket) {},
36
+ data(socket, data) {},
37
+ drain(socket) {},
38
+ close(socket, error) {},
39
+ error(socket, error) {},
40
+ },
41
+ });
42
+ ```
43
+
44
+ For performance-sensitive servers, assigning listeners to each socket can cause significant garbage collector pressure and increase memory usage. By contrast, Bun only allocates one handler function for each event and shares it among all sockets. This is a small optimization, but it adds up.
45
+
46
+ </Accordion>
47
+
48
+ Contextual data can be attached to a socket in the `open` handler.
49
+
50
+ ```ts server.ts icon="/icons/typescript.svg"
51
+ type SocketData = { sessionId: string };
52
+
53
+ Bun.listen<SocketData>({
54
+ hostname: "localhost",
55
+ port: 8080,
56
+ socket: {
57
+ data(socket, data) {
58
+ socket.write(`${socket.data.sessionId}: ack`); // [!code ++]
59
+ },
60
+ open(socket) {
61
+ socket.data = { sessionId: "abcd" }; // [!code ++]
62
+ },
63
+ },
64
+ });
65
+ ```
66
+
67
+ To enable TLS, pass a `tls` object containing `key` and `cert` fields.
68
+
69
+ ```ts server.ts icon="/icons/typescript.svg"
70
+ Bun.listen({
71
+ hostname: "localhost",
72
+ port: 8080,
73
+ socket: {
74
+ data(socket, data) {},
75
+ },
76
+ tls: {
77
+ // can be string, BunFile, TypedArray, Buffer, or array thereof
78
+ key: Bun.file("./key.pem"), // [!code ++]
79
+ cert: Bun.file("./cert.pem"), // [!code ++]
80
+ },
81
+ });
82
+ ```
83
+
84
+ The `key` and `cert` fields expect the _contents_ of your TLS key and certificate. This can be a string, `BunFile`, `TypedArray`, or `Buffer`.
85
+
86
+ ```ts server.ts icon="/icons/typescript.svg"
87
+ Bun.listen({
88
+ // ...
89
+ tls: {
90
+ key: Bun.file("./key.pem"), // BunFile
91
+ key: fs.readFileSync("./key.pem"), // Buffer
92
+ key: fs.readFileSync("./key.pem", "utf8"), // string
93
+ key: [Bun.file("./key1.pem"), Bun.file("./key2.pem")], // array of above
94
+ },
95
+ });
96
+ ```
97
+
98
+ The result of `Bun.listen` is a server that conforms to the `TCPSocket` interface.
99
+
100
+ ```ts server.ts icon="/icons/typescript.svg"
101
+ const server = Bun.listen({
102
+ /* config*/
103
+ });
104
+
105
+ // stop listening
106
+ // parameter determines whether active connections are closed
107
+ server.stop(true);
108
+
109
+ // let Bun process exit even if server is still listening
110
+ server.unref();
111
+ ```
112
+
113
+ ---
114
+
115
+ ## Create a connection (`Bun.connect()`)
116
+
117
+ Use `Bun.connect` to connect to a TCP server. Specify the server to connect to with `hostname` and `port`. TCP clients can define the same set of handlers as `Bun.listen`, plus a couple client-specific handlers.
118
+
119
+ ```ts server.ts icon="/icons/typescript.svg"
120
+ // The client
121
+ const socket = await Bun.connect({
122
+ hostname: "localhost",
123
+ port: 8080,
124
+
125
+ socket: {
126
+ data(socket, data) {},
127
+ open(socket) {},
128
+ close(socket, error) {},
129
+ drain(socket) {},
130
+ error(socket, error) {},
131
+
132
+ // client-specific handlers
133
+ connectError(socket, error) {}, // connection failed
134
+ end(socket) {}, // connection closed by server
135
+ timeout(socket) {}, // connection timed out
136
+ },
137
+ });
138
+ ```
139
+
140
+ To require TLS, specify `tls: true`.
141
+
142
+ ```ts
143
+ // The client
144
+ const socket = await Bun.connect({
145
+ // ... config
146
+ tls: true, // [!code ++]
147
+ });
148
+ ```
149
+
150
+ ---
151
+
152
+ ## Hot reloading
153
+
154
+ Both TCP servers and sockets can be hot reloaded with new handlers.
155
+
156
+ <CodeGroup>
157
+
158
+ ```ts server.ts icon="/icons/typescript.svg"
159
+ const server = Bun.listen({
160
+ /* config */
161
+ });
162
+
163
+ // reloads handlers for all active server-side sockets
164
+ server.reload({
165
+ socket: {
166
+ data() {
167
+ // new 'data' handler
168
+ },
169
+ },
170
+ });
171
+ ```
172
+
173
+ ```ts client.ts icon="/icons/typescript.svg"
174
+ const socket = await Bun.connect({
175
+ /* config */
176
+ });
177
+
178
+ socket.reload({
179
+ data() {
180
+ // new 'data' handler
181
+ },
182
+ });
183
+ ```
184
+
185
+ </CodeGroup>
186
+
187
+ ---
188
+
189
+ ## Buffering
190
+
191
+ Currently, TCP sockets in Bun do not buffer data. For performance-sensitive code, it's important to consider buffering carefully. For example, this:
192
+
193
+ ```ts
194
+ socket.write("h");
195
+ socket.write("e");
196
+ socket.write("l");
197
+ socket.write("l");
198
+ socket.write("o");
199
+ ```
200
+
201
+ ...performs significantly worse than this:
202
+
203
+ ```ts
204
+ socket.write("hello");
205
+ ```
206
+
207
+ To simplify this for now, consider using Bun's `ArrayBufferSink` with the `{stream: true}` option:
208
+
209
+ ```ts server.ts icon="/icons/typescript.svg"
210
+ import { ArrayBufferSink } from "bun";
211
+
212
+ const sink = new ArrayBufferSink();
213
+ sink.start({
214
+ stream: true, // [!code ++]
215
+ highWaterMark: 1024,
216
+ });
217
+
218
+ sink.write("h");
219
+ sink.write("e");
220
+ sink.write("l");
221
+ sink.write("l");
222
+ sink.write("o");
223
+
224
+ queueMicrotask(() => {
225
+ const data = sink.flush();
226
+ const wrote = socket.write(data);
227
+ if (wrote < data.byteLength) {
228
+ // put it back in the sink if the socket is full
229
+ sink.write(data.subarray(wrote));
230
+ }
231
+ });
232
+ ```
233
+
234
+ <Note>
235
+ **Corking**
236
+
237
+ Support for corking is planned, but in the meantime backpressure must be managed manually with the `drain` handler.
238
+
239
+ </Note>