bun-types 1.1.37-canary.20241124T140524 → 1.1.37-canary.20241125T140601

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 (272) hide show
  1. package/docs/api/binary-data.md +1028 -0
  2. package/docs/api/cc.md +197 -0
  3. package/docs/api/color.md +262 -0
  4. package/docs/api/console.md +38 -0
  5. package/docs/api/dns.md +113 -0
  6. package/docs/api/fetch.md +308 -0
  7. package/docs/api/ffi.md +536 -0
  8. package/docs/api/file-io.md +358 -0
  9. package/docs/api/file-system-router.md +112 -0
  10. package/docs/api/file.md +19 -0
  11. package/docs/api/glob.md +157 -0
  12. package/docs/api/globals.md +387 -0
  13. package/docs/api/hashing.md +314 -0
  14. package/docs/api/html-rewriter.md +31 -0
  15. package/docs/api/http.md +661 -0
  16. package/docs/api/import-meta.md +69 -0
  17. package/docs/api/node-api.md +16 -0
  18. package/docs/api/semver.md +52 -0
  19. package/docs/api/spawn.md +441 -0
  20. package/docs/api/sqlite.md +692 -0
  21. package/docs/api/streams.md +214 -0
  22. package/docs/api/tcp.md +221 -0
  23. package/docs/api/test.md +1 -0
  24. package/docs/api/transpiler.md +274 -0
  25. package/docs/api/udp.md +125 -0
  26. package/docs/api/utils.md +773 -0
  27. package/docs/api/websockets.md +547 -0
  28. package/docs/api/workers.md +230 -0
  29. package/docs/benchmarks.md +120 -0
  30. package/docs/bun-flavored-toml.md +42 -0
  31. package/docs/bundler/executables.md +291 -0
  32. package/docs/bundler/index.md +1543 -0
  33. package/docs/bundler/intro.md +75 -0
  34. package/docs/bundler/loaders.md +288 -0
  35. package/docs/bundler/macros.md +327 -0
  36. package/docs/bundler/plugins.md +30 -0
  37. package/docs/bundler/vs-esbuild.md +1127 -0
  38. package/docs/cli/add.md +163 -0
  39. package/docs/cli/bun-completions.md +3 -0
  40. package/docs/cli/bun-create.md +254 -0
  41. package/docs/cli/bun-dev.md +21 -0
  42. package/docs/cli/bun-install.md +255 -0
  43. package/docs/cli/bun-upgrade.md +39 -0
  44. package/docs/cli/bunx.md +80 -0
  45. package/docs/cli/filter.md +57 -0
  46. package/docs/cli/init.md +40 -0
  47. package/docs/cli/install.md +205 -0
  48. package/docs/cli/link.md +38 -0
  49. package/docs/cli/outdated.md +61 -0
  50. package/docs/cli/patch-commit.md +9 -0
  51. package/docs/cli/pm.md +150 -0
  52. package/docs/cli/publish.md +107 -0
  53. package/docs/cli/remove.md +5 -0
  54. package/docs/cli/run.md +196 -0
  55. package/docs/cli/test.md +247 -0
  56. package/docs/cli/unlink.md +7 -0
  57. package/docs/cli/update.md +34 -0
  58. package/docs/contributing/upgrading-webkit.md +57 -0
  59. package/docs/ecosystem/elysia.md +24 -0
  60. package/docs/ecosystem/express.md +37 -0
  61. package/docs/ecosystem/hono.md +18 -0
  62. package/docs/ecosystem/react.md +65 -0
  63. package/docs/ecosystem/stric.md +37 -0
  64. package/docs/guides/binary/arraybuffer-to-array.md +27 -0
  65. package/docs/guides/binary/arraybuffer-to-blob.md +24 -0
  66. package/docs/guides/binary/arraybuffer-to-buffer.md +25 -0
  67. package/docs/guides/binary/arraybuffer-to-string.md +15 -0
  68. package/docs/guides/binary/arraybuffer-to-typedarray.md +39 -0
  69. package/docs/guides/binary/blob-to-arraybuffer.md +14 -0
  70. package/docs/guides/binary/blob-to-dataview.md +14 -0
  71. package/docs/guides/binary/blob-to-stream.md +14 -0
  72. package/docs/guides/binary/blob-to-string.md +15 -0
  73. package/docs/guides/binary/blob-to-typedarray.md +14 -0
  74. package/docs/guides/binary/buffer-to-arraybuffer.md +14 -0
  75. package/docs/guides/binary/buffer-to-blob.md +14 -0
  76. package/docs/guides/binary/buffer-to-readablestream.md +41 -0
  77. package/docs/guides/binary/buffer-to-string.md +25 -0
  78. package/docs/guides/binary/buffer-to-typedarray.md +14 -0
  79. package/docs/guides/binary/dataview-to-string.md +15 -0
  80. package/docs/guides/binary/typedarray-to-arraybuffer.md +25 -0
  81. package/docs/guides/binary/typedarray-to-blob.md +16 -0
  82. package/docs/guides/binary/typedarray-to-buffer.md +14 -0
  83. package/docs/guides/binary/typedarray-to-dataview.md +14 -0
  84. package/docs/guides/binary/typedarray-to-readablestream.md +41 -0
  85. package/docs/guides/binary/typedarray-to-string.md +16 -0
  86. package/docs/guides/ecosystem/astro.md +72 -0
  87. package/docs/guides/ecosystem/discordjs.md +77 -0
  88. package/docs/guides/ecosystem/docker.md +140 -0
  89. package/docs/guides/ecosystem/drizzle.md +185 -0
  90. package/docs/guides/ecosystem/edgedb.md +228 -0
  91. package/docs/guides/ecosystem/elysia.md +31 -0
  92. package/docs/guides/ecosystem/express.md +40 -0
  93. package/docs/guides/ecosystem/hono.md +39 -0
  94. package/docs/guides/ecosystem/mongoose.md +87 -0
  95. package/docs/guides/ecosystem/neon-drizzle.md +220 -0
  96. package/docs/guides/ecosystem/neon-serverless-postgres.md +55 -0
  97. package/docs/guides/ecosystem/nextjs.md +38 -0
  98. package/docs/guides/ecosystem/nuxt.md +56 -0
  99. package/docs/guides/ecosystem/pm2.md +57 -0
  100. package/docs/guides/ecosystem/prisma.md +140 -0
  101. package/docs/guides/ecosystem/qwik.md +107 -0
  102. package/docs/guides/ecosystem/react.md +49 -0
  103. package/docs/guides/ecosystem/remix.md +78 -0
  104. package/docs/guides/ecosystem/render.md +79 -0
  105. package/docs/guides/ecosystem/sentry.md +52 -0
  106. package/docs/guides/ecosystem/solidstart.md +58 -0
  107. package/docs/guides/ecosystem/ssr-react.md +51 -0
  108. package/docs/guides/ecosystem/stric.md +55 -0
  109. package/docs/guides/ecosystem/sveltekit.md +125 -0
  110. package/docs/guides/ecosystem/systemd.md +113 -0
  111. package/docs/guides/ecosystem/vite.md +70 -0
  112. package/docs/guides/http/cluster.md +66 -0
  113. package/docs/guides/http/fetch-unix.md +33 -0
  114. package/docs/guides/http/fetch.md +24 -0
  115. package/docs/guides/http/file-uploads.md +94 -0
  116. package/docs/guides/http/hot.md +22 -0
  117. package/docs/guides/http/proxy.md +24 -0
  118. package/docs/guides/http/server.md +46 -0
  119. package/docs/guides/http/simple.md +18 -0
  120. package/docs/guides/http/stream-file.md +48 -0
  121. package/docs/guides/http/stream-iterator.md +47 -0
  122. package/docs/guides/http/stream-node-streams-in-bun.md +20 -0
  123. package/docs/guides/http/tls.md +30 -0
  124. package/docs/guides/install/add-dev.md +26 -0
  125. package/docs/guides/install/add-git.md +36 -0
  126. package/docs/guides/install/add-optional.md +25 -0
  127. package/docs/guides/install/add-peer.md +17 -0
  128. package/docs/guides/install/add-tarball.md +33 -0
  129. package/docs/guides/install/add.md +42 -0
  130. package/docs/guides/install/azure-artifacts.md +73 -0
  131. package/docs/guides/install/cicd.md +41 -0
  132. package/docs/guides/install/custom-registry.md +30 -0
  133. package/docs/guides/install/from-npm-install-to-bun-install.md +214 -0
  134. package/docs/guides/install/git-diff-bun-lockfile.md +38 -0
  135. package/docs/guides/install/jfrog-artifactory.md +28 -0
  136. package/docs/guides/install/npm-alias.md +23 -0
  137. package/docs/guides/install/registry-scope.md +36 -0
  138. package/docs/guides/install/trusted.md +48 -0
  139. package/docs/guides/install/workspaces.md +70 -0
  140. package/docs/guides/install/yarnlock.md +42 -0
  141. package/docs/guides/process/argv.md +57 -0
  142. package/docs/guides/process/ctrl-c.md +16 -0
  143. package/docs/guides/process/ipc.md +66 -0
  144. package/docs/guides/process/nanoseconds.md +13 -0
  145. package/docs/guides/process/os-signals.md +39 -0
  146. package/docs/guides/process/spawn-stderr.md +31 -0
  147. package/docs/guides/process/spawn-stdout.md +26 -0
  148. package/docs/guides/process/spawn.md +41 -0
  149. package/docs/guides/process/stdin.md +54 -0
  150. package/docs/guides/read-file/arraybuffer.md +28 -0
  151. package/docs/guides/read-file/buffer.md +19 -0
  152. package/docs/guides/read-file/exists.md +16 -0
  153. package/docs/guides/read-file/json.md +17 -0
  154. package/docs/guides/read-file/mime.md +20 -0
  155. package/docs/guides/read-file/stream.md +26 -0
  156. package/docs/guides/read-file/string.md +22 -0
  157. package/docs/guides/read-file/uint8array.md +21 -0
  158. package/docs/guides/read-file/watch.md +68 -0
  159. package/docs/guides/runtime/cicd.md +43 -0
  160. package/docs/guides/runtime/define-constant.md +145 -0
  161. package/docs/guides/runtime/import-html.md +15 -0
  162. package/docs/guides/runtime/import-json.md +44 -0
  163. package/docs/guides/runtime/import-toml.md +30 -0
  164. package/docs/guides/runtime/read-env.md +32 -0
  165. package/docs/guides/runtime/set-env.md +37 -0
  166. package/docs/guides/runtime/shell.md +40 -0
  167. package/docs/guides/runtime/timezone.md +35 -0
  168. package/docs/guides/runtime/tsconfig-paths.md +29 -0
  169. package/docs/guides/runtime/typescript.md +47 -0
  170. package/docs/guides/runtime/vscode-debugger.md +47 -0
  171. package/docs/guides/runtime/web-debugger.md +82 -0
  172. package/docs/guides/streams/node-readable-to-arraybuffer.md +11 -0
  173. package/docs/guides/streams/node-readable-to-blob.md +11 -0
  174. package/docs/guides/streams/node-readable-to-json.md +12 -0
  175. package/docs/guides/streams/node-readable-to-string.md +12 -0
  176. package/docs/guides/streams/node-readable-to-uint8array.md +11 -0
  177. package/docs/guides/streams/to-array.md +14 -0
  178. package/docs/guides/streams/to-arraybuffer.md +14 -0
  179. package/docs/guides/streams/to-blob.md +14 -0
  180. package/docs/guides/streams/to-buffer.md +15 -0
  181. package/docs/guides/streams/to-json.md +14 -0
  182. package/docs/guides/streams/to-string.md +14 -0
  183. package/docs/guides/streams/to-typedarray.md +22 -0
  184. package/docs/guides/test/bail.md +22 -0
  185. package/docs/guides/test/coverage-threshold.md +60 -0
  186. package/docs/guides/test/coverage.md +44 -0
  187. package/docs/guides/test/happy-dom.md +68 -0
  188. package/docs/guides/test/migrate-from-jest.md +110 -0
  189. package/docs/guides/test/mock-clock.md +48 -0
  190. package/docs/guides/test/mock-functions.md +68 -0
  191. package/docs/guides/test/rerun-each.md +14 -0
  192. package/docs/guides/test/run-tests.md +111 -0
  193. package/docs/guides/test/skip-tests.md +39 -0
  194. package/docs/guides/test/snapshot.md +99 -0
  195. package/docs/guides/test/spy-on.md +46 -0
  196. package/docs/guides/test/testing-library.md +87 -0
  197. package/docs/guides/test/timeout.md +15 -0
  198. package/docs/guides/test/todo-tests.md +67 -0
  199. package/docs/guides/test/update-snapshots.md +50 -0
  200. package/docs/guides/test/watch-mode.md +19 -0
  201. package/docs/guides/util/base64.md +15 -0
  202. package/docs/guides/util/deep-equals.md +39 -0
  203. package/docs/guides/util/deflate.md +18 -0
  204. package/docs/guides/util/detect-bun.md +23 -0
  205. package/docs/guides/util/entrypoint.md +17 -0
  206. package/docs/guides/util/escape-html.md +22 -0
  207. package/docs/guides/util/file-url-to-path.md +14 -0
  208. package/docs/guides/util/gzip.md +18 -0
  209. package/docs/guides/util/hash-a-password.md +54 -0
  210. package/docs/guides/util/import-meta-dir.md +13 -0
  211. package/docs/guides/util/import-meta-file.md +13 -0
  212. package/docs/guides/util/import-meta-path.md +13 -0
  213. package/docs/guides/util/main.md +32 -0
  214. package/docs/guides/util/path-to-file-url.md +14 -0
  215. package/docs/guides/util/sleep.md +22 -0
  216. package/docs/guides/util/version.md +21 -0
  217. package/docs/guides/util/which-path-to-executable-bin.md +15 -0
  218. package/docs/guides/websocket/compression.md +31 -0
  219. package/docs/guides/websocket/context.md +72 -0
  220. package/docs/guides/websocket/pubsub.md +38 -0
  221. package/docs/guides/websocket/simple.md +33 -0
  222. package/docs/guides/write-file/append.md +52 -0
  223. package/docs/guides/write-file/basic.md +44 -0
  224. package/docs/guides/write-file/blob.md +28 -0
  225. package/docs/guides/write-file/cat.md +17 -0
  226. package/docs/guides/write-file/file-cp.md +16 -0
  227. package/docs/guides/write-file/filesink.md +52 -0
  228. package/docs/guides/write-file/response.md +17 -0
  229. package/docs/guides/write-file/stdout.md +21 -0
  230. package/docs/guides/write-file/stream.md +17 -0
  231. package/docs/guides/write-file/unlink.md +23 -0
  232. package/docs/index.md +77 -0
  233. package/docs/install/cache.md +57 -0
  234. package/docs/install/index.md +202 -0
  235. package/docs/install/lifecycle.md +46 -0
  236. package/docs/install/lockfile.md +90 -0
  237. package/docs/install/npmrc.md +75 -0
  238. package/docs/install/overrides.md +73 -0
  239. package/docs/install/patch.md +57 -0
  240. package/docs/install/registries.md +30 -0
  241. package/docs/install/workspaces.md +70 -0
  242. package/docs/installation.md +289 -0
  243. package/docs/project/benchmarking.md +203 -0
  244. package/docs/project/building-windows.md +162 -0
  245. package/docs/project/internals/build-process-for-ci.md +75 -0
  246. package/docs/project/roadmap.md +87 -0
  247. package/docs/quickstart.md +144 -0
  248. package/docs/runtime/autoimport.md +94 -0
  249. package/docs/runtime/bun-apis.md +129 -0
  250. package/docs/runtime/bunfig.md +532 -0
  251. package/docs/runtime/debugger.md +325 -0
  252. package/docs/runtime/env.md +214 -0
  253. package/docs/runtime/hot.md +139 -0
  254. package/docs/runtime/index.md +309 -0
  255. package/docs/runtime/jsx.md +326 -0
  256. package/docs/runtime/loaders.md +127 -0
  257. package/docs/runtime/modules.md +298 -0
  258. package/docs/runtime/nodejs-apis.md +456 -0
  259. package/docs/runtime/plugins.md +605 -0
  260. package/docs/runtime/shell.md +537 -0
  261. package/docs/runtime/typescript.md +60 -0
  262. package/docs/runtime/web-apis.md +128 -0
  263. package/docs/test/coverage.md +91 -0
  264. package/docs/test/dom.md +75 -0
  265. package/docs/test/hot.md +15 -0
  266. package/docs/test/lifecycle.md +81 -0
  267. package/docs/test/mocks.md +236 -0
  268. package/docs/test/snapshots.md +15 -0
  269. package/docs/test/time.md +106 -0
  270. package/docs/test/writing.md +547 -0
  271. package/docs/typescript.md +51 -0
  272. package/package.json +5 -4
@@ -0,0 +1,358 @@
1
+ {% callout %}
2
+
3
+ <!-- **Note** — The `Bun.file` and `Bun.write` APIs documented on this page are heavily optimized and represent the recommended way to perform file-system tasks using Bun. Existing Node.js projects may use Bun's [nearly complete](https://bun.sh/docs/runtime/nodejs-apis#node-fs) implementation of the [`node:fs`](https://nodejs.org/api/fs.html) module. -->
4
+
5
+ **Note** — The `Bun.file` and `Bun.write` APIs documented on this page are heavily optimized and represent the recommended way to perform file-system tasks using Bun. For operations that are not yet available with `Bun.file`, such as `mkdir` or `readdir`, you can use Bun's [nearly complete](https://bun.sh/docs/runtime/nodejs-apis#node-fs) implementation of the [`node:fs`](https://nodejs.org/api/fs.html) module.
6
+
7
+ {% /callout %}
8
+
9
+ Bun provides a set of optimized APIs for reading and writing files.
10
+
11
+ ## Reading files (`Bun.file()`)
12
+
13
+ `Bun.file(path): BunFile`
14
+
15
+ Create a `BunFile` instance with the `Bun.file(path)` function. A `BunFile` represents a lazily-loaded file; initializing it does not actually read the file from disk.
16
+
17
+ ```ts
18
+ const foo = Bun.file("foo.txt"); // relative to cwd
19
+ foo.size; // number of bytes
20
+ foo.type; // MIME type
21
+ ```
22
+
23
+ The reference conforms to the [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) interface, so the contents can be read in various formats.
24
+
25
+ ```ts
26
+ const foo = Bun.file("foo.txt");
27
+
28
+ await foo.text(); // contents as a string
29
+ await foo.stream(); // contents as ReadableStream
30
+ await foo.arrayBuffer(); // contents as ArrayBuffer
31
+ await foo.bytes(); // contents as Uint8Array
32
+ ```
33
+
34
+ File references can also be created using numerical [file descriptors](https://en.wikipedia.org/wiki/File_descriptor) or `file://` URLs.
35
+
36
+ ```ts
37
+ Bun.file(1234);
38
+ Bun.file(new URL(import.meta.url)); // reference to the current file
39
+ ```
40
+
41
+ A `BunFile` can point to a location on disk where a file does not exist.
42
+
43
+ ```ts
44
+ const notreal = Bun.file("notreal.txt");
45
+ notreal.size; // 0
46
+ notreal.type; // "text/plain;charset=utf-8"
47
+ const exists = await notreal.exists(); // false
48
+ ```
49
+
50
+ The default MIME type is `text/plain;charset=utf-8`, but it can be overridden by passing a second argument to `Bun.file`.
51
+
52
+ ```ts
53
+ const notreal = Bun.file("notreal.json", { type: "application/json" });
54
+ notreal.type; // => "application/json;charset=utf-8"
55
+ ```
56
+
57
+ For convenience, Bun exposes `stdin`, `stdout` and `stderr` as instances of `BunFile`.
58
+
59
+ ```ts
60
+ Bun.stdin; // readonly
61
+ Bun.stdout;
62
+ Bun.stderr;
63
+ ```
64
+
65
+ ## Writing files (`Bun.write()`)
66
+
67
+ `Bun.write(destination, data): Promise<number>`
68
+
69
+ The `Bun.write` function is a multi-tool for writing payloads of all kinds to disk.
70
+
71
+ The first argument is the `destination` which can have any of the following types:
72
+
73
+ - `string`: A path to a location on the file system. Use the `"path"` module to manipulate paths.
74
+ - `URL`: A `file://` descriptor.
75
+ - `BunFile`: A file reference.
76
+
77
+ The second argument is the data to be written. It can be any of the following:
78
+
79
+ - `string`
80
+ - `Blob` (including `BunFile`)
81
+ - `ArrayBuffer` or `SharedArrayBuffer`
82
+ - `TypedArray` (`Uint8Array`, et. al.)
83
+ - `Response`
84
+
85
+ All possible permutations are handled using the fastest available system calls on the current platform.
86
+
87
+ {% details summary="See syscalls" %}
88
+
89
+ {% table %}
90
+
91
+ - Output
92
+ - Input
93
+ - System call
94
+ - Platform
95
+
96
+ ---
97
+
98
+ - file
99
+ - file
100
+ - copy_file_range
101
+ - Linux
102
+
103
+ ---
104
+
105
+ - file
106
+ - pipe
107
+ - sendfile
108
+ - Linux
109
+
110
+ ---
111
+
112
+ - pipe
113
+ - pipe
114
+ - splice
115
+ - Linux
116
+
117
+ ---
118
+
119
+ - terminal
120
+ - file
121
+ - sendfile
122
+ - Linux
123
+
124
+ ---
125
+
126
+ - terminal
127
+ - terminal
128
+ - sendfile
129
+ - Linux
130
+
131
+ ---
132
+
133
+ - socket
134
+ - file or pipe
135
+ - sendfile (if http, not https)
136
+ - Linux
137
+
138
+ ---
139
+
140
+ - file (doesn't exist)
141
+ - file (path)
142
+ - clonefile
143
+ - macOS
144
+
145
+ ---
146
+
147
+ - file (exists)
148
+ - file
149
+ - fcopyfile
150
+ - macOS
151
+
152
+ ---
153
+
154
+ - file
155
+ - Blob or string
156
+ - write
157
+ - macOS
158
+
159
+ ---
160
+
161
+ - file
162
+ - Blob or string
163
+ - write
164
+ - Linux
165
+
166
+ {% /table %}
167
+
168
+ {% /details %}
169
+
170
+ To write a string to disk:
171
+
172
+ ```ts
173
+ const data = `It was the best of times, it was the worst of times.`;
174
+ await Bun.write("output.txt", data);
175
+ ```
176
+
177
+ To copy a file to another location on disk:
178
+
179
+ ```js
180
+ const input = Bun.file("input.txt");
181
+ const output = Bun.file("output.txt"); // doesn't exist yet!
182
+ await Bun.write(output, input);
183
+ ```
184
+
185
+ To write a byte array to disk:
186
+
187
+ ```ts
188
+ const encoder = new TextEncoder();
189
+ const data = encoder.encode("datadatadata"); // Uint8Array
190
+ await Bun.write("output.txt", data);
191
+ ```
192
+
193
+ To write a file to `stdout`:
194
+
195
+ ```ts
196
+ const input = Bun.file("input.txt");
197
+ await Bun.write(Bun.stdout, input);
198
+ ```
199
+
200
+ To write the body of an HTTP response to disk:
201
+
202
+ ```ts
203
+ const response = await fetch("https://bun.sh");
204
+ await Bun.write("index.html", response);
205
+ ```
206
+
207
+ ## Incremental writing with `FileSink`
208
+
209
+ Bun provides a native incremental file writing API called `FileSink`. To retrieve a `FileSink` instance from a `BunFile`:
210
+
211
+ ```ts
212
+ const file = Bun.file("output.txt");
213
+ const writer = file.writer();
214
+ ```
215
+
216
+ To incrementally write to the file, call `.write()`.
217
+
218
+ ```ts
219
+ const file = Bun.file("output.txt");
220
+ const writer = file.writer();
221
+
222
+ writer.write("it was the best of times\n");
223
+ writer.write("it was the worst of times\n");
224
+ ```
225
+
226
+ These chunks will be buffered internally. To flush the buffer to disk, use `.flush()`. This returns the number of flushed bytes.
227
+
228
+ ```ts
229
+ writer.flush(); // write buffer to disk
230
+ ```
231
+
232
+ The buffer will also auto-flush when the `FileSink`'s _high water mark_ is reached; that is, when its internal buffer is full. This value can be configured.
233
+
234
+ ```ts
235
+ const file = Bun.file("output.txt");
236
+ const writer = file.writer({ highWaterMark: 1024 * 1024 }); // 1MB
237
+ ```
238
+
239
+ To flush the buffer and close the file:
240
+
241
+ ```ts
242
+ writer.end();
243
+ ```
244
+
245
+ Note that, by default, the `bun` process will stay alive until this `FileSink` is explicitly closed with `.end()`. To opt out of this behavior, you can "unref" the instance.
246
+
247
+ ```ts
248
+ writer.unref();
249
+
250
+ // to "re-ref" it later
251
+ writer.ref();
252
+ ```
253
+
254
+ ## Directories
255
+
256
+ Bun's implementation of `node:fs` is fast, and we haven't implemented a Bun-specific API for reading directories just yet. For now, you should use `node:fs` for working with directories in Bun.
257
+
258
+ ### Reading directories (readdir)
259
+
260
+ To read a directory in Bun, use `readdir` from `node:fs`.
261
+
262
+ ```ts
263
+ import { readdir } from "node:fs/promises";
264
+
265
+ // read all the files in the current directory
266
+ const files = await readdir(import.meta.dir);
267
+ ```
268
+
269
+ #### Reading directories recursively
270
+
271
+ To recursively read a directory in Bun, use `readdir` with `recursive: true`.
272
+
273
+ ```ts
274
+ import { readdir } from "node:fs/promises";
275
+
276
+ // read all the files in the current directory, recursively
277
+ const files = await readdir("../", { recursive: true });
278
+ ```
279
+
280
+ ### Creating directories (mkdir)
281
+
282
+ To recursively create a directory, use `mkdir` in `node:fs`:
283
+
284
+ ```ts
285
+ import { mkdir } from "node:fs/promises";
286
+
287
+ await mkdir("path/to/dir", { recursive: true });
288
+ ```
289
+
290
+ ## Benchmarks
291
+
292
+ The following is a 3-line implementation of the Linux `cat` command.
293
+
294
+ ```ts#cat.ts
295
+ // Usage
296
+ // $ bun ./cat.ts ./path-to-file
297
+
298
+ import { resolve } from "path";
299
+
300
+ const path = resolve(process.argv.at(-1));
301
+ await Bun.write(Bun.stdout, Bun.file(path));
302
+ ```
303
+
304
+ To run the file:
305
+
306
+ ```bash
307
+ $ bun ./cat.ts ./path-to-file
308
+ ```
309
+
310
+ It runs 2x faster than GNU `cat` for large files on Linux.
311
+
312
+ {% image src="/images/cat.jpg" /%}
313
+
314
+ ## Reference
315
+
316
+ ```ts
317
+ interface Bun {
318
+ stdin: BunFile;
319
+ stdout: BunFile;
320
+ stderr: BunFile;
321
+
322
+ file(path: string | number | URL, options?: { type?: string }): BunFile;
323
+
324
+ write(
325
+ destination: string | number | BunFile | URL,
326
+ input:
327
+ | string
328
+ | Blob
329
+ | ArrayBuffer
330
+ | SharedArrayBuffer
331
+ | TypedArray
332
+ | Response,
333
+ ): Promise<number>;
334
+ }
335
+
336
+ interface BunFile {
337
+ readonly size: number;
338
+ readonly type: string;
339
+
340
+ text(): Promise<string>;
341
+ stream(): ReadableStream;
342
+ arrayBuffer(): Promise<ArrayBuffer>;
343
+ json(): Promise<any>;
344
+ writer(params: { highWaterMark?: number }): FileSink;
345
+ exists(): Promise<boolean>;
346
+ }
347
+
348
+ export interface FileSink {
349
+ write(
350
+ chunk: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer,
351
+ ): number;
352
+ flush(): number | Promise<number>;
353
+ end(error?: Error): number | Promise<number>;
354
+ start(options?: { highWaterMark?: number }): void;
355
+ ref(): void;
356
+ unref(): void;
357
+ }
358
+ ```
@@ -0,0 +1,112 @@
1
+ Bun provides a fast API for resolving routes against file-system paths. This API is primarily intended for library authors. At the moment only Next.js-style file-system routing is supported, but other styles may be added in the future.
2
+
3
+ ## Next.js-style
4
+
5
+ The `FileSystemRouter` class can resolve routes against a `pages` directory. (The Next.js 13 `app` directory is not yet supported.) Consider the following `pages` directory:
6
+
7
+ ```txt
8
+ pages
9
+ ├── index.tsx
10
+ ├── settings.tsx
11
+ ├── blog
12
+ │   ├── [slug].tsx
13
+ │   └── index.tsx
14
+ └── [[...catchall]].tsx
15
+ ```
16
+
17
+ The `FileSystemRouter` can be used to resolve routes against this directory:
18
+
19
+ ```ts
20
+ const router = new Bun.FileSystemRouter({
21
+ style: "nextjs",
22
+ dir: "./pages",
23
+ origin: "https://mydomain.com",
24
+ assetPrefix: "_next/static/"
25
+ });
26
+ router.match("/");
27
+
28
+ // =>
29
+ {
30
+ filePath: "/path/to/pages/index.tsx",
31
+ kind: "exact",
32
+ name: "/",
33
+ pathname: "/",
34
+ src: "https://mydomain.com/_next/static/pages/index.tsx"
35
+ }
36
+ ```
37
+
38
+ Query parameters will be parsed and returned in the `query` property.
39
+
40
+ ```ts
41
+ router.match("/settings?foo=bar");
42
+
43
+ // =>
44
+ {
45
+ filePath: "/Users/colinmcd94/Documents/bun/fun/pages/settings.tsx",
46
+ kind: "dynamic",
47
+ name: "/settings",
48
+ pathname: "/settings?foo=bar",
49
+ src: "https://mydomain.com/_next/static/pages/settings.tsx",
50
+ query: {
51
+ foo: "bar"
52
+ }
53
+ }
54
+ ```
55
+
56
+ The router will automatically parse URL parameters and return them in the `params` property:
57
+
58
+ ```ts
59
+ router.match("/blog/my-cool-post");
60
+
61
+ // =>
62
+ {
63
+ filePath: "/Users/colinmcd94/Documents/bun/fun/pages/blog/[slug].tsx",
64
+ kind: "dynamic",
65
+ name: "/blog/[slug]",
66
+ pathname: "/blog/my-cool-post",
67
+ src: "https://mydomain.com/_next/static/pages/blog/[slug].tsx",
68
+ params: {
69
+ slug: "my-cool-post"
70
+ }
71
+ }
72
+ ```
73
+
74
+ The `.match()` method also accepts `Request` and `Response` objects. The `url` property will be used to resolve the route.
75
+
76
+ ```ts
77
+ router.match(new Request("https://example.com/blog/my-cool-post"));
78
+ ```
79
+
80
+ The router will read the directory contents on initialization. To re-scan the files, use the `.reload()` method.
81
+
82
+ ```ts
83
+ router.reload();
84
+ ```
85
+
86
+ ## Reference
87
+
88
+ ```ts
89
+ interface Bun {
90
+ class FileSystemRouter {
91
+ constructor(params: {
92
+ dir: string;
93
+ style: "nextjs";
94
+ origin?: string;
95
+ assetPrefix?: string;
96
+ fileExtensions?: string[];
97
+ });
98
+
99
+ reload(): void;
100
+
101
+ match(path: string | Request | Response): {
102
+ filePath: string;
103
+ kind: "exact" | "catch-all" | "optional-catch-all" | "dynamic";
104
+ name: string;
105
+ pathname: string;
106
+ src: string;
107
+ params?: Record<string, string>;
108
+ query?: Record<string, string>;
109
+ } | null
110
+ }
111
+ }
112
+ ```
@@ -0,0 +1,19 @@
1
+ Bun.js has fast paths for common use cases that make Web APIs live up to the performance demands of servers and CLIs.
2
+
3
+ `Bun.file(path)` returns a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) that represents a lazily-loaded file.
4
+
5
+ When you pass a file blob to `Bun.write`, Bun automatically uses a faster system call:
6
+
7
+ ```js
8
+ const blob = Bun.file("input.txt");
9
+ await Bun.write("output.txt", blob);
10
+ ```
11
+
12
+ On Linux, this uses the [`copy_file_range`](https://man7.org/linux/man-pages/man2/copy_file_range.2.html) syscall and on macOS, this becomes `clonefile` (or [`fcopyfile`](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/copyfile.3.html)).
13
+
14
+ `Bun.write` also supports [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) objects. It automatically converts to a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
15
+
16
+ ```js
17
+ // Eventually, this will stream the response to disk but today it buffers
18
+ await Bun.write("index.html", await fetch("https://example.com"));
19
+ ```
@@ -0,0 +1,157 @@
1
+ Bun includes a fast native implementation of file globbing.
2
+
3
+ ## Quickstart
4
+
5
+ **Scan a directory for files matching `*.ts`**:
6
+
7
+ ```ts
8
+ import { Glob } from "bun";
9
+
10
+ const glob = new Glob("**/*.ts");
11
+
12
+ // Scans the current working directory and each of its sub-directories recursively
13
+ for await (const file of glob.scan(".")) {
14
+ console.log(file); // => "index.ts"
15
+ }
16
+ ```
17
+
18
+ **Match a string against a glob pattern**:
19
+
20
+ ```ts
21
+ import { Glob } from "bun";
22
+
23
+ const glob = new Glob("*.ts");
24
+
25
+ glob.match("index.ts"); // => true
26
+ glob.match("index.js"); // => false
27
+ ```
28
+
29
+ `Glob` is a class which implements the following interface:
30
+
31
+ ```ts
32
+ class Glob {
33
+ scan(root: string | ScanOptions): AsyncIterable<string>;
34
+ scanSync(root: string | ScanOptions): Iterable<string>;
35
+
36
+ match(path: string): boolean;
37
+ }
38
+
39
+ interface ScanOptions {
40
+ /**
41
+ * The root directory to start matching from. Defaults to `process.cwd()`
42
+ */
43
+ cwd?: string;
44
+
45
+ /**
46
+ * Allow patterns to match entries that begin with a period (`.`).
47
+ *
48
+ * @default false
49
+ */
50
+ dot?: boolean;
51
+
52
+ /**
53
+ * Return the absolute path for entries.
54
+ *
55
+ * @default false
56
+ */
57
+ absolute?: boolean;
58
+
59
+ /**
60
+ * Indicates whether to traverse descendants of symbolic link directories.
61
+ *
62
+ * @default false
63
+ */
64
+ followSymlinks?: boolean;
65
+
66
+ /**
67
+ * Throw an error when symbolic link is broken
68
+ *
69
+ * @default false
70
+ */
71
+ throwErrorOnBrokenSymlink?: boolean;
72
+
73
+ /**
74
+ * Return only files.
75
+ *
76
+ * @default true
77
+ */
78
+ onlyFiles?: boolean;
79
+ }
80
+ ```
81
+
82
+ ## Supported Glob Patterns
83
+
84
+ Bun supports the following glob patterns:
85
+
86
+ ### `?` - Match any single character
87
+
88
+ ```ts
89
+ const glob = new Glob("???.ts");
90
+ glob.match("foo.ts"); // => true
91
+ glob.match("foobar.ts"); // => false
92
+ ```
93
+
94
+ ### `*` - Matches zero or more characters, except for path separators (`/` or `\`)
95
+
96
+ ```ts
97
+ const glob = new Glob("*.ts");
98
+ glob.match("index.ts"); // => true
99
+ glob.match("src/index.ts"); // => false
100
+ ```
101
+
102
+ ### `**` - Match any number of characters including `/`
103
+
104
+ ```ts
105
+ const glob = new Glob("**/*.ts");
106
+ glob.match("index.ts"); // => true
107
+ glob.match("src/index.ts"); // => true
108
+ glob.match("src/index.js"); // => false
109
+ ```
110
+
111
+ ### `[ab]` - Matches one of the characters contained in the brackets, as well as character ranges
112
+
113
+ ```ts
114
+ const glob = new Glob("ba[rz].ts");
115
+ glob.match("bar.ts"); // => true
116
+ glob.match("baz.ts"); // => true
117
+ glob.match("bat.ts"); // => false
118
+ ```
119
+
120
+ You can use character ranges (e.g `[0-9]`, `[a-z]`) as well as the negation operators `^` or `!` to match anything _except_ the characters contained within the braces (e.g `[^ab]`, `[!a-z]`)
121
+
122
+ ```ts
123
+ const glob = new Glob("ba[a-z][0-9][^4-9].ts");
124
+ glob.match("bar01.ts"); // => true
125
+ glob.match("baz83.ts"); // => true
126
+ glob.match("bat22.ts"); // => true
127
+ glob.match("bat24.ts"); // => false
128
+ glob.match("ba0a8.ts"); // => false
129
+ ```
130
+
131
+ ### `{a,b,c}` - Match any of the given patterns
132
+
133
+ ```ts
134
+ const glob = new Glob("{a,b,c}.ts");
135
+ glob.match("a.ts"); // => true
136
+ glob.match("b.ts"); // => true
137
+ glob.match("c.ts"); // => true
138
+ glob.match("d.ts"); // => false
139
+ ```
140
+
141
+ These match patterns can be deeply nested (up to 10 levels), and contain any of the wildcards from above.
142
+
143
+ ### `!` - Negates the result at the start of a pattern
144
+
145
+ ```ts
146
+ const glob = new Glob("!index.ts");
147
+ glob.match("index.ts"); // => false
148
+ glob.match("foo.ts"); // => true
149
+ ```
150
+
151
+ ### `\` - Escapes any of the special characters above
152
+
153
+ ```ts
154
+ const glob = new Glob("\\!index.ts");
155
+ glob.match("!index.ts"); // => true
156
+ glob.match("index.ts"); // => false
157
+ ```