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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (308) hide show
  1. package/docs/README.md +28 -0
  2. package/package.json +1 -1
  3. package/docs/api/binary-data.md +0 -1038
  4. package/docs/api/cc.md +0 -197
  5. package/docs/api/color.md +0 -262
  6. package/docs/api/console.md +0 -57
  7. package/docs/api/cookie.md +0 -449
  8. package/docs/api/dns.md +0 -110
  9. package/docs/api/fetch.md +0 -463
  10. package/docs/api/ffi.md +0 -557
  11. package/docs/api/file-io.md +0 -366
  12. package/docs/api/file-system-router.md +0 -112
  13. package/docs/api/file.md +0 -19
  14. package/docs/api/glob.md +0 -178
  15. package/docs/api/globals.md +0 -387
  16. package/docs/api/hashing.md +0 -319
  17. package/docs/api/html-rewriter.md +0 -334
  18. package/docs/api/http.md +0 -1408
  19. package/docs/api/import-meta.md +0 -69
  20. package/docs/api/node-api.md +0 -14
  21. package/docs/api/redis.md +0 -597
  22. package/docs/api/s3.md +0 -850
  23. package/docs/api/secrets.md +0 -319
  24. package/docs/api/semver.md +0 -52
  25. package/docs/api/spawn.md +0 -582
  26. package/docs/api/sql.md +0 -1374
  27. package/docs/api/sqlite.md +0 -694
  28. package/docs/api/streams.md +0 -230
  29. package/docs/api/tcp.md +0 -221
  30. package/docs/api/transpiler.md +0 -276
  31. package/docs/api/udp.md +0 -123
  32. package/docs/api/utils.md +0 -891
  33. package/docs/api/websockets.md +0 -573
  34. package/docs/api/workers.md +0 -322
  35. package/docs/api/yaml.md +0 -668
  36. package/docs/benchmarks.md +0 -120
  37. package/docs/bun-flavored-toml.md +0 -42
  38. package/docs/bundler/css.md +0 -1028
  39. package/docs/bundler/css_modules.md +0 -145
  40. package/docs/bundler/executables.md +0 -626
  41. package/docs/bundler/fullstack.md +0 -418
  42. package/docs/bundler/hmr.md +0 -234
  43. package/docs/bundler/html.md +0 -349
  44. package/docs/bundler/index.md +0 -1735
  45. package/docs/bundler/intro.md +0 -75
  46. package/docs/bundler/loaders.md +0 -410
  47. package/docs/bundler/macros.md +0 -329
  48. package/docs/bundler/plugins.md +0 -449
  49. package/docs/bundler/vs-esbuild.md +0 -1127
  50. package/docs/cli/add.md +0 -173
  51. package/docs/cli/bun-completions.md +0 -3
  52. package/docs/cli/bun-create.md +0 -338
  53. package/docs/cli/bun-install.md +0 -349
  54. package/docs/cli/bun-upgrade.md +0 -39
  55. package/docs/cli/bunx.md +0 -89
  56. package/docs/cli/filter.md +0 -90
  57. package/docs/cli/info.md +0 -65
  58. package/docs/cli/init.md +0 -85
  59. package/docs/cli/install.md +0 -344
  60. package/docs/cli/link.md +0 -40
  61. package/docs/cli/outdated.md +0 -90
  62. package/docs/cli/patch-commit.md +0 -11
  63. package/docs/cli/pm.md +0 -285
  64. package/docs/cli/publish.md +0 -120
  65. package/docs/cli/remove.md +0 -7
  66. package/docs/cli/run.md +0 -261
  67. package/docs/cli/test.md +0 -397
  68. package/docs/cli/unlink.md +0 -9
  69. package/docs/cli/update.md +0 -129
  70. package/docs/cli/why.md +0 -67
  71. package/docs/contributing/upgrading-webkit.md +0 -57
  72. package/docs/ecosystem/elysia.md +0 -24
  73. package/docs/ecosystem/express.md +0 -37
  74. package/docs/ecosystem/hono.md +0 -18
  75. package/docs/ecosystem/react.md +0 -65
  76. package/docs/ecosystem/stric.md +0 -38
  77. package/docs/guides/binary/arraybuffer-to-array.md +0 -27
  78. package/docs/guides/binary/arraybuffer-to-blob.md +0 -24
  79. package/docs/guides/binary/arraybuffer-to-buffer.md +0 -25
  80. package/docs/guides/binary/arraybuffer-to-string.md +0 -15
  81. package/docs/guides/binary/arraybuffer-to-typedarray.md +0 -39
  82. package/docs/guides/binary/blob-to-arraybuffer.md +0 -14
  83. package/docs/guides/binary/blob-to-dataview.md +0 -14
  84. package/docs/guides/binary/blob-to-stream.md +0 -14
  85. package/docs/guides/binary/blob-to-string.md +0 -15
  86. package/docs/guides/binary/blob-to-typedarray.md +0 -14
  87. package/docs/guides/binary/buffer-to-arraybuffer.md +0 -14
  88. package/docs/guides/binary/buffer-to-blob.md +0 -14
  89. package/docs/guides/binary/buffer-to-readablestream.md +0 -41
  90. package/docs/guides/binary/buffer-to-string.md +0 -25
  91. package/docs/guides/binary/buffer-to-typedarray.md +0 -14
  92. package/docs/guides/binary/dataview-to-string.md +0 -15
  93. package/docs/guides/binary/typedarray-to-arraybuffer.md +0 -25
  94. package/docs/guides/binary/typedarray-to-blob.md +0 -16
  95. package/docs/guides/binary/typedarray-to-buffer.md +0 -14
  96. package/docs/guides/binary/typedarray-to-dataview.md +0 -14
  97. package/docs/guides/binary/typedarray-to-readablestream.md +0 -41
  98. package/docs/guides/binary/typedarray-to-string.md +0 -16
  99. package/docs/guides/deployment/railway.md +0 -157
  100. package/docs/guides/ecosystem/astro.md +0 -72
  101. package/docs/guides/ecosystem/discordjs.md +0 -77
  102. package/docs/guides/ecosystem/docker.md +0 -140
  103. package/docs/guides/ecosystem/drizzle.md +0 -185
  104. package/docs/guides/ecosystem/edgedb.md +0 -228
  105. package/docs/guides/ecosystem/elysia.md +0 -31
  106. package/docs/guides/ecosystem/express.md +0 -40
  107. package/docs/guides/ecosystem/hono.md +0 -39
  108. package/docs/guides/ecosystem/mongoose.md +0 -87
  109. package/docs/guides/ecosystem/neon-drizzle.md +0 -220
  110. package/docs/guides/ecosystem/neon-serverless-postgres.md +0 -55
  111. package/docs/guides/ecosystem/nextjs.md +0 -49
  112. package/docs/guides/ecosystem/nuxt.md +0 -56
  113. package/docs/guides/ecosystem/pm2.md +0 -57
  114. package/docs/guides/ecosystem/prisma.md +0 -141
  115. package/docs/guides/ecosystem/qwik.md +0 -107
  116. package/docs/guides/ecosystem/react.md +0 -50
  117. package/docs/guides/ecosystem/remix.md +0 -78
  118. package/docs/guides/ecosystem/render.md +0 -79
  119. package/docs/guides/ecosystem/sentry.md +0 -52
  120. package/docs/guides/ecosystem/solidstart.md +0 -58
  121. package/docs/guides/ecosystem/ssr-react.md +0 -51
  122. package/docs/guides/ecosystem/stric.md +0 -55
  123. package/docs/guides/ecosystem/sveltekit.md +0 -125
  124. package/docs/guides/ecosystem/systemd.md +0 -113
  125. package/docs/guides/ecosystem/vite.md +0 -71
  126. package/docs/guides/html-rewriter/extract-links.md +0 -68
  127. package/docs/guides/html-rewriter/extract-social-meta.md +0 -93
  128. package/docs/guides/http/cluster.md +0 -66
  129. package/docs/guides/http/fetch-unix.md +0 -33
  130. package/docs/guides/http/fetch.md +0 -24
  131. package/docs/guides/http/file-uploads.md +0 -94
  132. package/docs/guides/http/hot.md +0 -26
  133. package/docs/guides/http/proxy.md +0 -24
  134. package/docs/guides/http/server.md +0 -46
  135. package/docs/guides/http/simple.md +0 -18
  136. package/docs/guides/http/stream-file.md +0 -48
  137. package/docs/guides/http/stream-iterator.md +0 -47
  138. package/docs/guides/http/stream-node-streams-in-bun.md +0 -20
  139. package/docs/guides/http/tls.md +0 -30
  140. package/docs/guides/install/add-dev.md +0 -26
  141. package/docs/guides/install/add-git.md +0 -36
  142. package/docs/guides/install/add-optional.md +0 -25
  143. package/docs/guides/install/add-peer.md +0 -43
  144. package/docs/guides/install/add-tarball.md +0 -33
  145. package/docs/guides/install/add.md +0 -42
  146. package/docs/guides/install/azure-artifacts.md +0 -73
  147. package/docs/guides/install/cicd.md +0 -41
  148. package/docs/guides/install/custom-registry.md +0 -30
  149. package/docs/guides/install/from-npm-install-to-bun-install.md +0 -214
  150. package/docs/guides/install/git-diff-bun-lockfile.md +0 -44
  151. package/docs/guides/install/jfrog-artifactory.md +0 -28
  152. package/docs/guides/install/npm-alias.md +0 -23
  153. package/docs/guides/install/registry-scope.md +0 -38
  154. package/docs/guides/install/trusted.md +0 -46
  155. package/docs/guides/install/workspaces.md +0 -70
  156. package/docs/guides/install/yarnlock.md +0 -44
  157. package/docs/guides/process/argv.md +0 -58
  158. package/docs/guides/process/ctrl-c.md +0 -16
  159. package/docs/guides/process/ipc.md +0 -66
  160. package/docs/guides/process/nanoseconds.md +0 -13
  161. package/docs/guides/process/os-signals.md +0 -39
  162. package/docs/guides/process/spawn-stderr.md +0 -31
  163. package/docs/guides/process/spawn-stdout.md +0 -26
  164. package/docs/guides/process/spawn.md +0 -41
  165. package/docs/guides/process/stdin.md +0 -54
  166. package/docs/guides/read-file/arraybuffer.md +0 -28
  167. package/docs/guides/read-file/buffer.md +0 -19
  168. package/docs/guides/read-file/exists.md +0 -16
  169. package/docs/guides/read-file/json.md +0 -17
  170. package/docs/guides/read-file/mime.md +0 -20
  171. package/docs/guides/read-file/stream.md +0 -26
  172. package/docs/guides/read-file/string.md +0 -22
  173. package/docs/guides/read-file/uint8array.md +0 -21
  174. package/docs/guides/read-file/watch.md +0 -68
  175. package/docs/guides/runtime/build-time-constants.md +0 -293
  176. package/docs/guides/runtime/cicd.md +0 -43
  177. package/docs/guides/runtime/codesign-macos-executable.md +0 -56
  178. package/docs/guides/runtime/define-constant.md +0 -145
  179. package/docs/guides/runtime/delete-directory.md +0 -37
  180. package/docs/guides/runtime/delete-file.md +0 -19
  181. package/docs/guides/runtime/heap-snapshot.md +0 -26
  182. package/docs/guides/runtime/import-html.md +0 -15
  183. package/docs/guides/runtime/import-json.md +0 -44
  184. package/docs/guides/runtime/import-toml.md +0 -30
  185. package/docs/guides/runtime/import-yaml.md +0 -102
  186. package/docs/guides/runtime/read-env.md +0 -32
  187. package/docs/guides/runtime/set-env.md +0 -47
  188. package/docs/guides/runtime/shell.md +0 -40
  189. package/docs/guides/runtime/timezone.md +0 -35
  190. package/docs/guides/runtime/tsconfig-paths.md +0 -29
  191. package/docs/guides/runtime/typescript.md +0 -49
  192. package/docs/guides/runtime/vscode-debugger.md +0 -47
  193. package/docs/guides/runtime/web-debugger.md +0 -82
  194. package/docs/guides/streams/node-readable-to-arraybuffer.md +0 -11
  195. package/docs/guides/streams/node-readable-to-blob.md +0 -11
  196. package/docs/guides/streams/node-readable-to-json.md +0 -12
  197. package/docs/guides/streams/node-readable-to-string.md +0 -12
  198. package/docs/guides/streams/node-readable-to-uint8array.md +0 -11
  199. package/docs/guides/streams/to-array.md +0 -14
  200. package/docs/guides/streams/to-arraybuffer.md +0 -14
  201. package/docs/guides/streams/to-blob.md +0 -14
  202. package/docs/guides/streams/to-buffer.md +0 -15
  203. package/docs/guides/streams/to-json.md +0 -14
  204. package/docs/guides/streams/to-string.md +0 -14
  205. package/docs/guides/streams/to-typedarray.md +0 -22
  206. package/docs/guides/test/bail.md +0 -22
  207. package/docs/guides/test/coverage-threshold.md +0 -60
  208. package/docs/guides/test/coverage.md +0 -44
  209. package/docs/guides/test/happy-dom.md +0 -68
  210. package/docs/guides/test/migrate-from-jest.md +0 -143
  211. package/docs/guides/test/mock-clock.md +0 -48
  212. package/docs/guides/test/mock-functions.md +0 -68
  213. package/docs/guides/test/rerun-each.md +0 -14
  214. package/docs/guides/test/run-tests.md +0 -111
  215. package/docs/guides/test/skip-tests.md +0 -39
  216. package/docs/guides/test/snapshot.md +0 -95
  217. package/docs/guides/test/spy-on.md +0 -46
  218. package/docs/guides/test/svelte-test.md +0 -120
  219. package/docs/guides/test/testing-library.md +0 -92
  220. package/docs/guides/test/timeout.md +0 -15
  221. package/docs/guides/test/todo-tests.md +0 -67
  222. package/docs/guides/test/update-snapshots.md +0 -46
  223. package/docs/guides/test/watch-mode.md +0 -19
  224. package/docs/guides/util/base64.md +0 -15
  225. package/docs/guides/util/deep-equals.md +0 -39
  226. package/docs/guides/util/deflate.md +0 -18
  227. package/docs/guides/util/detect-bun.md +0 -23
  228. package/docs/guides/util/entrypoint.md +0 -17
  229. package/docs/guides/util/escape-html.md +0 -22
  230. package/docs/guides/util/file-url-to-path.md +0 -14
  231. package/docs/guides/util/gzip.md +0 -18
  232. package/docs/guides/util/hash-a-password.md +0 -54
  233. package/docs/guides/util/import-meta-dir.md +0 -13
  234. package/docs/guides/util/import-meta-file.md +0 -13
  235. package/docs/guides/util/import-meta-path.md +0 -13
  236. package/docs/guides/util/javascript-uuid.md +0 -23
  237. package/docs/guides/util/main.md +0 -32
  238. package/docs/guides/util/path-to-file-url.md +0 -14
  239. package/docs/guides/util/sleep.md +0 -22
  240. package/docs/guides/util/version.md +0 -21
  241. package/docs/guides/util/which-path-to-executable-bin.md +0 -15
  242. package/docs/guides/websocket/compression.md +0 -31
  243. package/docs/guides/websocket/context.md +0 -77
  244. package/docs/guides/websocket/pubsub.md +0 -41
  245. package/docs/guides/websocket/simple.md +0 -33
  246. package/docs/guides/write-file/append.md +0 -52
  247. package/docs/guides/write-file/basic.md +0 -44
  248. package/docs/guides/write-file/blob.md +0 -28
  249. package/docs/guides/write-file/cat.md +0 -17
  250. package/docs/guides/write-file/file-cp.md +0 -16
  251. package/docs/guides/write-file/filesink.md +0 -52
  252. package/docs/guides/write-file/response.md +0 -17
  253. package/docs/guides/write-file/stdout.md +0 -21
  254. package/docs/guides/write-file/stream.md +0 -17
  255. package/docs/guides/write-file/unlink.md +0 -16
  256. package/docs/index.md +0 -77
  257. package/docs/install/audit.md +0 -57
  258. package/docs/install/cache.md +0 -59
  259. package/docs/install/catalogs.md +0 -298
  260. package/docs/install/index.md +0 -248
  261. package/docs/install/isolated.md +0 -197
  262. package/docs/install/lifecycle.md +0 -46
  263. package/docs/install/lockfile.md +0 -58
  264. package/docs/install/npmrc.md +0 -105
  265. package/docs/install/overrides.md +0 -73
  266. package/docs/install/patch.md +0 -59
  267. package/docs/install/registries.md +0 -30
  268. package/docs/install/security-scanner-api.md +0 -81
  269. package/docs/install/workspaces.md +0 -114
  270. package/docs/installation.md +0 -327
  271. package/docs/project/asan.md +0 -124
  272. package/docs/project/benchmarking.md +0 -203
  273. package/docs/project/bindgen.md +0 -225
  274. package/docs/project/building-windows.md +0 -162
  275. package/docs/project/contributing.md +0 -347
  276. package/docs/project/internals/build-process-for-ci.md +0 -75
  277. package/docs/project/licensing.md +0 -73
  278. package/docs/project/roadmap.md +0 -87
  279. package/docs/quickstart.md +0 -157
  280. package/docs/runtime/autoimport.md +0 -94
  281. package/docs/runtime/bun-apis.md +0 -207
  282. package/docs/runtime/bunfig.md +0 -772
  283. package/docs/runtime/debugger.md +0 -325
  284. package/docs/runtime/env.md +0 -253
  285. package/docs/runtime/hot.md +0 -145
  286. package/docs/runtime/index.md +0 -312
  287. package/docs/runtime/jsx.md +0 -385
  288. package/docs/runtime/loaders.md +0 -130
  289. package/docs/runtime/modules.md +0 -320
  290. package/docs/runtime/nodejs-apis.md +0 -463
  291. package/docs/runtime/plugins.md +0 -561
  292. package/docs/runtime/shell.md +0 -605
  293. package/docs/runtime/typescript.md +0 -139
  294. package/docs/runtime/web-apis.md +0 -128
  295. package/docs/test/configuration.md +0 -154
  296. package/docs/test/coverage.md +0 -142
  297. package/docs/test/discovery.md +0 -85
  298. package/docs/test/dom.md +0 -75
  299. package/docs/test/examples/concurrent-test-glob.md +0 -132
  300. package/docs/test/hot.md +0 -15
  301. package/docs/test/lifecycle.md +0 -81
  302. package/docs/test/mocks.md +0 -313
  303. package/docs/test/reporters.md +0 -117
  304. package/docs/test/runtime-behavior.md +0 -95
  305. package/docs/test/snapshots.md +0 -68
  306. package/docs/test/time.md +0 -126
  307. package/docs/test/writing.md +0 -825
  308. package/docs/typescript.md +0 -53
@@ -1,1038 +0,0 @@
1
- This page is intended as an introduction to working with binary data in JavaScript. Bun implements a number of data types and utilities for working with binary data, most of which are Web-standard. Any Bun-specific APIs will be noted as such.
2
-
3
- Below is a quick "cheat sheet" that doubles as a table of contents. Click an item in the left column to jump to that section.
4
-
5
- {% table %}
6
-
7
- ---
8
-
9
- - [`TypedArray`](#typedarray)
10
- - A family of classes that provide an `Array`-like interface for interacting with binary data. Includes `Uint8Array`, `Uint16Array`, `Int8Array`, and more.
11
-
12
- ---
13
-
14
- - [`Buffer`](#buffer)
15
- - A subclass of `Uint8Array` that implements a wide range of convenience methods. Unlike the other elements in this table, this is a Node.js API (which Bun implements). It can't be used in the browser.
16
-
17
- ---
18
-
19
- - [`DataView`](#dataview)
20
- - A class that provides a `get/set` API for writing some number of bytes to an `ArrayBuffer` at a particular byte offset. Often used reading or writing binary protocols.
21
-
22
- ---
23
-
24
- - [`Blob`](#blob)
25
- - A readonly blob of binary data usually representing a file. Has a MIME `type`, a `size`, and methods for converting to `ArrayBuffer`, `ReadableStream`, and string.
26
-
27
- ---
28
-
29
- - [`File`](#file)
30
- - A subclass of `Blob` that represents a file. Has a `name` and `lastModified` timestamp. There is experimental support in Node.js v20.
31
-
32
- ---
33
-
34
- - [`BunFile`](#bunfile)
35
- - _Bun only_. A subclass of `Blob` that represents a lazily-loaded file on disk. Created with `Bun.file(path)`.
36
-
37
- {% /table %}
38
-
39
- ## `ArrayBuffer` and views
40
-
41
- Until 2009, there was no language-native way to store and manipulate binary data in JavaScript. ECMAScript v5 introduced a range of new mechanisms for this. The most fundamental building block is `ArrayBuffer`, a simple data structure that represents a sequence of bytes in memory.
42
-
43
- ```ts
44
- // this buffer can store 8 bytes
45
- const buf = new ArrayBuffer(8);
46
- ```
47
-
48
- Despite the name, it isn't an array and supports none of the array methods and operators one might expect. In fact, there is no way to directly read or write values from an `ArrayBuffer`. There's very little you can do with one except check its size and create "slices" from it.
49
-
50
- ```ts
51
- const buf = new ArrayBuffer(8);
52
- buf.byteLength; // => 8
53
-
54
- const slice = buf.slice(0, 4); // returns new ArrayBuffer
55
- slice.byteLength; // => 4
56
- ```
57
-
58
- To do anything interesting we need a construct known as a "view". A view is a class that _wraps_ an `ArrayBuffer` instance and lets you read and manipulate the underlying data. There are two types of views: _typed arrays_ and `DataView`.
59
-
60
- ### `DataView`
61
-
62
- The `DataView` class is a lower-level interface for reading and manipulating the data in an `ArrayBuffer`.
63
-
64
- Below we create a new `DataView` and set the first byte to 3.
65
-
66
- ```ts
67
- const buf = new ArrayBuffer(4);
68
- // [0b00000000, 0b00000000, 0b00000000, 0b00000000]
69
-
70
- const dv = new DataView(buf);
71
- dv.setUint8(0, 3); // write value 3 at byte offset 0
72
- dv.getUint8(0); // => 3
73
- // [0b00000011, 0b00000000, 0b00000000, 0b00000000]
74
- ```
75
-
76
- Now let's write a `Uint16` at byte offset `1`. This requires two bytes. We're using the value `513`, which is `2 * 256 + 1`; in bytes, that's `00000010 00000001`.
77
-
78
- ```ts
79
- dv.setUint16(1, 513);
80
- // [0b00000011, 0b00000010, 0b00000001, 0b00000000]
81
-
82
- console.log(dv.getUint16(1)); // => 513
83
- ```
84
-
85
- We've now assigned a value to the first three bytes in our underlying `ArrayBuffer`. Even though the second and third bytes were created using `setUint16()`, we can still read each of its component bytes using `getUint8()`.
86
-
87
- ```ts
88
- console.log(dv.getUint8(1)); // => 2
89
- console.log(dv.getUint8(2)); // => 1
90
- ```
91
-
92
- Attempting to write a value that requires more space than is available in the underlying `ArrayBuffer` will cause an error. Below we attempt to write a `Float64` (which requires 8 bytes) at byte offset `0`, but there are only four total bytes in the buffer.
93
-
94
- ```ts
95
- dv.setFloat64(0, 3.1415);
96
- // ^ RangeError: Out of bounds access
97
- ```
98
-
99
- The following methods are available on `DataView`:
100
-
101
- {% table %}
102
-
103
- - Getters
104
- - Setters
105
-
106
- ---
107
-
108
- - [`getBigInt64()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getBigInt64)
109
- - [`setBigInt64()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setBigInt64)
110
-
111
- ---
112
-
113
- - [`getBigUint64()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getBigUint64)
114
- - [`setBigUint64()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setBigUint64)
115
-
116
- ---
117
-
118
- - [`getFloat32()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getFloat32)
119
- - [`setFloat32()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setFloat32)
120
-
121
- ---
122
-
123
- - [`getFloat64()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getFloat64)
124
- - [`setFloat64()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setFloat64)
125
-
126
- ---
127
-
128
- - [`getInt16()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getInt16)
129
- - [`setInt16()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setInt16)
130
-
131
- ---
132
-
133
- - [`getInt32()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getInt32)
134
- - [`setInt32()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setInt32)
135
-
136
- ---
137
-
138
- - [`getInt8()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getInt8)
139
- - [`setInt8()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setInt8)
140
-
141
- ---
142
-
143
- - [`getUint16()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getUint16)
144
- - [`setUint16()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setUint16)
145
-
146
- ---
147
-
148
- - [`getUint32()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getUint32)
149
- - [`setUint32()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setUint32)
150
-
151
- ---
152
-
153
- - [`getUint8()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getUint8)
154
- - [`setUint8()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setUint8)
155
-
156
- {% /table %}
157
-
158
- ### `TypedArray`
159
-
160
- Typed arrays are a family of classes that provide an `Array`-like interface for interacting with data in an `ArrayBuffer`. Whereas a `DataView` lets you write numbers of varying size at a particular offset, a `TypedArray` interprets the underlying bytes as an array of numbers, each of a fixed size.
161
-
162
- {% callout %}
163
- **Note** — It's common to refer to this family of classes collectively by their shared superclass `TypedArray`. This class as _internal_ to JavaScript; you can't directly create instances of it, and `TypedArray` is not defined in the global scope. Think of it as an `interface` or an abstract class.
164
- {% /callout %}
165
-
166
- ```ts
167
- const buffer = new ArrayBuffer(3);
168
- const arr = new Uint8Array(buffer);
169
-
170
- // contents are initialized to zero
171
- console.log(arr); // Uint8Array(3) [0, 0, 0]
172
-
173
- // assign values like an array
174
- arr[0] = 0;
175
- arr[1] = 10;
176
- arr[2] = 255;
177
- arr[3] = 255; // no-op, out of bounds
178
- ```
179
-
180
- While an `ArrayBuffer` is a generic sequence of bytes, these typed array classes interpret the bytes as an array of numbers of a given byte size.
181
- The top row contains the raw bytes, and the later rows contain how these bytes will be interpreted when _viewed_ using different typed array classes.
182
-
183
- The following classes are typed arrays, along with a description of how they interpret the bytes in an `ArrayBuffer`:
184
-
185
- {% table %}
186
-
187
- - Class
188
- - Description
189
-
190
- ---
191
-
192
- - [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)
193
- - Every one (1) byte is interpreted as an unsigned 8-bit integer. Range 0 to 255.
194
-
195
- ---
196
-
197
- - [`Uint16Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array)
198
- - Every two (2) bytes are interpreted as an unsigned 16-bit integer. Range 0 to 65535.
199
-
200
- ---
201
-
202
- - [`Uint32Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array)
203
- - Every four (4) bytes are interpreted as an unsigned 32-bit integer. Range 0 to 4294967295.
204
-
205
- ---
206
-
207
- - [`Int8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array)
208
- - Every one (1) byte is interpreted as a signed 8-bit integer. Range -128 to 127.
209
-
210
- ---
211
-
212
- - [`Int16Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int16Array)
213
- - Every two (2) bytes are interpreted as a signed 16-bit integer. Range -32768 to 32767.
214
-
215
- ---
216
-
217
- - [`Int32Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array)
218
- - Every four (4) bytes are interpreted as a signed 32-bit integer. Range -2147483648 to 2147483647.
219
-
220
- ---
221
-
222
- - [`Float16Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float16Array)
223
- - Every two (2) bytes are interpreted as a 16-bit floating point number. Range -6.104e5 to 6.55e4.
224
-
225
- ---
226
-
227
- - [`Float32Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array)
228
- - Every four (4) bytes are interpreted as a 32-bit floating point number. Range -3.4e38 to 3.4e38.
229
-
230
- ---
231
-
232
- - [`Float64Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array)
233
- - Every eight (8) bytes are interpreted as a 64-bit floating point number. Range -1.7e308 to 1.7e308.
234
-
235
- ---
236
-
237
- - [`BigInt64Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array)
238
- - Every eight (8) bytes are interpreted as a signed `BigInt`. Range -9223372036854775808 to 9223372036854775807 (though `BigInt` is capable of representing larger numbers).
239
-
240
- ---
241
-
242
- - [`BigUint64Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigUint64Array)
243
- - Every eight (8) bytes are interpreted as an unsigned `BigInt`. Range 0 to 18446744073709551615 (though `BigInt` is capable of representing larger numbers).
244
-
245
- ---
246
-
247
- - [`Uint8ClampedArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray)
248
- - Same as `Uint8Array`, but automatically "clamps" to the range 0-255 when assigning a value to an element.
249
-
250
- {% /table %}
251
-
252
- The table below demonstrates how the bytes in an `ArrayBuffer` are interpreted when viewed using different typed array classes.
253
-
254
- {% table %}
255
-
256
- ---
257
-
258
- - `ArrayBuffer`
259
- - `00000000`
260
- - `00000001`
261
- - `00000010`
262
- - `00000011`
263
- - `00000100`
264
- - `00000101`
265
- - `00000110`
266
- - `00000111`
267
-
268
- ---
269
-
270
- - `Uint8Array`
271
- - 0
272
- - 1
273
- - 2
274
- - 3
275
- - 4
276
- - 5
277
- - 6
278
- - 7
279
-
280
- ---
281
-
282
- - `Uint16Array`
283
- - 256 (`1 * 256 + 0`) {% colspan=2 %}
284
- - 770 (`3 * 256 + 2`) {% colspan=2 %}
285
- - 1284 (`5 * 256 + 4`) {% colspan=2 %}
286
- - 1798 (`7 * 256 + 6`) {% colspan=2 %}
287
-
288
- ---
289
-
290
- - `Uint32Array`
291
- - 50462976 {% colspan=4 %}
292
- - 117835012 {% colspan=4 %}
293
-
294
- ---
295
-
296
- - `BigUint64Array`
297
- - 506097522914230528n {% colspan=8 %}
298
-
299
- {% /table %}
300
-
301
- To create a typed array from a pre-defined `ArrayBuffer`:
302
-
303
- ```ts
304
- // create typed array from ArrayBuffer
305
- const buf = new ArrayBuffer(10);
306
- const arr = new Uint8Array(buf);
307
-
308
- arr[0] = 30;
309
- arr[1] = 60;
310
-
311
- // all elements are initialized to zero
312
- console.log(arr); // => Uint8Array(10) [ 30, 60, 0, 0, 0, 0, 0, 0, 0, 0 ];
313
- ```
314
-
315
- If we tried to instantiate a `Uint32Array` from this same `ArrayBuffer`, we'd get an error.
316
-
317
- ```ts
318
- const buf = new ArrayBuffer(10);
319
- const arr = new Uint32Array(buf);
320
- // ^ RangeError: ArrayBuffer length minus the byteOffset
321
- // is not a multiple of the element size
322
- ```
323
-
324
- A `Uint32` value requires four bytes (16 bits). Because the `ArrayBuffer` is 10 bytes long, there's no way to cleanly divide its contents into 4-byte chunks.
325
-
326
- To fix this, we can create a typed array over a particular "slice" of an `ArrayBuffer`. The `Uint16Array` below only "views" the _first_ 8 bytes of the underlying `ArrayBuffer`. To achieve these, we specify a `byteOffset` of `0` and a `length` of `2`, which indicates the number of `Uint32` numbers we want our array to hold.
327
-
328
- ```ts
329
- // create typed array from ArrayBuffer slice
330
- const buf = new ArrayBuffer(10);
331
- const arr = new Uint32Array(buf, 0, 2);
332
-
333
- /*
334
- buf _ _ _ _ _ _ _ _ _ _ 10 bytes
335
- arr [_______,_______] 2 4-byte elements
336
- */
337
-
338
- arr.byteOffset; // 0
339
- arr.length; // 2
340
- ```
341
-
342
- You don't need to explicitly create an `ArrayBuffer` instance; you can instead directly specify a length in the typed array constructor:
343
-
344
- ```ts
345
- const arr2 = new Uint8Array(5);
346
-
347
- // all elements are initialized to zero
348
- // => Uint8Array(5) [0, 0, 0, 0, 0]
349
- ```
350
-
351
- Typed arrays can also be instantiated directly from an array of numbers, or another typed array:
352
-
353
- ```ts
354
- // from an array of numbers
355
- const arr1 = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
356
- arr1[0]; // => 0;
357
- arr1[7]; // => 7;
358
-
359
- // from another typed array
360
- const arr2 = new Uint8Array(arr);
361
- ```
362
-
363
- Broadly speaking, typed arrays provide the same methods as regular arrays, with a few exceptions. For example, `push` and `pop` are not available on typed arrays, because they would require resizing the underlying `ArrayBuffer`.
364
-
365
- ```ts
366
- const arr = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
367
-
368
- // supports common array methods
369
- arr.filter(n => n > 128); // Uint8Array(1) [255]
370
- arr.map(n => n * 2); // Uint8Array(8) [0, 2, 4, 6, 8, 10, 12, 14]
371
- arr.reduce((acc, n) => acc + n, 0); // 28
372
- arr.forEach(n => console.log(n)); // 0 1 2 3 4 5 6 7
373
- arr.every(n => n < 10); // true
374
- arr.find(n => n > 5); // 6
375
- arr.includes(5); // true
376
- arr.indexOf(5); // 5
377
- ```
378
-
379
- Refer to the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) for more information on the properties and methods of typed arrays.
380
-
381
- ### `Uint8Array`
382
-
383
- It's worth specifically highlighting `Uint8Array`, as it represents a classic "byte array"—a sequence of 8-bit unsigned integers between 0 and 255. This is the most common typed array you'll encounter in JavaScript.
384
-
385
- In Bun, and someday in other JavaScript engines, it has methods available for converting between byte arrays and serialized representations of those arrays as base64 or hex strings.
386
-
387
- ```ts
388
- new Uint8Array([1, 2, 3, 4, 5]).toBase64(); // "AQIDBA=="
389
- Uint8Array.fromBase64("AQIDBA=="); // Uint8Array(4) [1, 2, 3, 4, 5]
390
-
391
- new Uint8Array([255, 254, 253, 252, 251]).toHex(); // "fffefdfcfb=="
392
- Uint8Array.fromHex("fffefdfcfb"); // Uint8Array(5) [255, 254, 253, 252, 251]
393
- ```
394
-
395
- It is the return value of [`TextEncoder#encode`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder), and the input type of [`TextDecoder#decode`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder), two utility classes designed to translate strings and various binary encodings, most notably `"utf-8"`.
396
-
397
- ```ts
398
- const encoder = new TextEncoder();
399
- const bytes = encoder.encode("hello world");
400
- // => Uint8Array(11) [ 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 ]
401
-
402
- const decoder = new TextDecoder();
403
- const text = decoder.decode(bytes);
404
- // => hello world
405
- ```
406
-
407
- ### `Buffer`
408
-
409
- Bun implements `Buffer`, a Node.js API for working with binary data that pre-dates the introduction of typed arrays in the JavaScript spec. It has since been re-implemented as a subclass of `Uint8Array`. It provides a wide range of methods, including several Array-like and `DataView`-like methods.
410
-
411
- ```ts
412
- const buf = Buffer.from("hello world");
413
- // => Buffer(11) [ 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 ]
414
-
415
- buf.length; // => 11
416
- buf[0]; // => 104, ascii for 'h'
417
- buf.writeUInt8(72, 0); // => ascii for 'H'
418
-
419
- console.log(buf.toString());
420
- // => Hello world
421
- ```
422
-
423
- For complete documentation, refer to the [Node.js documentation](https://nodejs.org/api/buffer.html).
424
-
425
- ## `Blob`
426
-
427
- `Blob` is a Web API commonly used for representing files. `Blob` was initially implemented in browsers (unlike `ArrayBuffer` which is part of JavaScript itself), but it is now supported in Node and Bun.
428
-
429
- It isn't common to directly create `Blob` instances. More often, you'll receive instances of `Blob` from an external source (like an `<input type="file">` element in the browser) or library. That said, it is possible to create a `Blob` from one or more string or binary "blob parts".
430
-
431
- ```ts
432
- const blob = new Blob(["<html>Hello</html>"], {
433
- type: "text/html",
434
- });
435
-
436
- blob.type; // => text/html
437
- blob.size; // => 19
438
- ```
439
-
440
- These parts can be `string`, `ArrayBuffer`, `TypedArray`, `DataView`, or other `Blob` instances. The blob parts are concatenated together in the order they are provided.
441
-
442
- ```ts
443
- const blob = new Blob([
444
- "<html>",
445
- new Blob(["<body>"]),
446
- new Uint8Array([104, 101, 108, 108, 111]), // "hello" in binary
447
- "</body></html>",
448
- ]);
449
- ```
450
-
451
- The contents of a `Blob` can be asynchronously read in various formats.
452
-
453
- ```ts
454
- await blob.text(); // => <html><body>hello</body></html>
455
- await blob.bytes(); // => Uint8Array (copies contents)
456
- await blob.arrayBuffer(); // => ArrayBuffer (copies contents)
457
- await blob.stream(); // => ReadableStream
458
- ```
459
-
460
- ### `BunFile`
461
-
462
- `BunFile` is a subclass of `Blob` used to represent a lazily-loaded file on disk. Like `File`, it adds a `name` and `lastModified` property. Unlike `File`, it does not require the file to be loaded into memory.
463
-
464
- ```ts
465
- const file = Bun.file("index.txt");
466
- // => BunFile
467
- ```
468
-
469
- ### `File`
470
-
471
- {% callout %}
472
- Browser only. Experimental support in Node.js 20.
473
- {% /callout %}
474
-
475
- [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) is a subclass of `Blob` that adds a `name` and `lastModified` property. It's commonly used in the browser to represent files uploaded via a `<input type="file">` element. Node.js and Bun implement `File`.
476
-
477
- ```ts
478
- // on browser!
479
- // <input type="file" id="file" />
480
-
481
- const files = document.getElementById("file").files;
482
- // => File[]
483
- ```
484
-
485
- ```ts
486
- const file = new File(["<html>Hello</html>"], "index.html", {
487
- type: "text/html",
488
- });
489
- ```
490
-
491
- Refer to the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/API/Blob) for complete docs information.
492
-
493
- ## Streams
494
-
495
- 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.
496
-
497
- 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/ReadableStream).
498
-
499
- {% callout %}
500
- Bun also implements the `node:stream` module, including [`Readable`](https://nodejs.org/api/stream.html#stream_readable_streams), [`Writable`](https://nodejs.org/api/stream.html#stream_writable_streams), and [`Duplex`](https://nodejs.org/api/stream.html#stream_duplex_and_transform_streams). For complete documentation, refer to the Node.js docs.
501
- {% /callout %}
502
-
503
- To create a simple readable stream:
504
-
505
- ```ts
506
- const stream = new ReadableStream({
507
- start(controller) {
508
- controller.enqueue("hello");
509
- controller.enqueue("world");
510
- controller.close();
511
- },
512
- });
513
- ```
514
-
515
- The contents of this stream can be read chunk-by-chunk with `for await` syntax.
516
-
517
- ```ts
518
- for await (const chunk of stream) {
519
- console.log(chunk);
520
- // => "hello"
521
- // => "world"
522
- }
523
- ```
524
-
525
- For a more complete discussion of streams in Bun, see [API > Streams](https://bun.com/docs/api/streams).
526
-
527
- ## Conversion
528
-
529
- Converting from one binary format to another is a common task. This section is intended as a reference.
530
-
531
- ### From `ArrayBuffer`
532
-
533
- Since `ArrayBuffer` stores the data that underlies other binary structures like `TypedArray`, the snippets below are not _converting_ from `ArrayBuffer` to another format. Instead, they are _creating_ a new instance using the data stored underlying data.
534
-
535
- #### To `TypedArray`
536
-
537
- ```ts
538
- new Uint8Array(buf);
539
- ```
540
-
541
- #### To `DataView`
542
-
543
- ```ts
544
- new DataView(buf);
545
- ```
546
-
547
- #### To `Buffer`
548
-
549
- ```ts
550
- // create Buffer over entire ArrayBuffer
551
- Buffer.from(buf);
552
-
553
- // create Buffer over a slice of the ArrayBuffer
554
- Buffer.from(buf, 0, 10);
555
- ```
556
-
557
- #### To `string`
558
-
559
- As UTF-8:
560
-
561
- ```ts
562
- new TextDecoder().decode(buf);
563
- ```
564
-
565
- #### To `number[]`
566
-
567
- ```ts
568
- Array.from(new Uint8Array(buf));
569
- ```
570
-
571
- #### To `Blob`
572
-
573
- ```ts
574
- new Blob([buf], { type: "text/plain" });
575
- ```
576
-
577
- <!-- #### To `File`
578
-
579
- ```ts
580
- new File([buf], "filename.txt", { type: "text/plain", lastModified: Date.now() });
581
- ``` -->
582
-
583
- #### To `ReadableStream`
584
-
585
- The following snippet creates a `ReadableStream` and enqueues the entire `ArrayBuffer` as a single chunk.
586
-
587
- ```ts
588
- new ReadableStream({
589
- start(controller) {
590
- controller.enqueue(buf);
591
- controller.close();
592
- },
593
- });
594
- ```
595
-
596
- {% details summary="With chunking" %}
597
- To stream the `ArrayBuffer` in chunks, use a `Uint8Array` view and enqueue each chunk.
598
-
599
- ```ts
600
- const view = new Uint8Array(buf);
601
- const chunkSize = 1024;
602
-
603
- new ReadableStream({
604
- start(controller) {
605
- for (let i = 0; i < view.length; i += chunkSize) {
606
- controller.enqueue(view.slice(i, i + chunkSize));
607
- }
608
- controller.close();
609
- },
610
- });
611
- ```
612
-
613
- {% /details %}
614
-
615
- ### From `TypedArray`
616
-
617
- #### To `ArrayBuffer`
618
-
619
- This retrieves the underlying `ArrayBuffer`. Note that a `TypedArray` can be a view of a _slice_ of the underlying buffer, so the sizes may differ.
620
-
621
- ```ts
622
- arr.buffer;
623
- ```
624
-
625
- #### To `DataView`
626
-
627
- To creates a `DataView` over the same byte range as the TypedArray.
628
-
629
- ```ts
630
- new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
631
- ```
632
-
633
- #### To `Buffer`
634
-
635
- ```ts
636
- Buffer.from(arr);
637
- ```
638
-
639
- #### To `string`
640
-
641
- As UTF-8:
642
-
643
- ```ts
644
- new TextDecoder().decode(arr);
645
- ```
646
-
647
- #### To `number[]`
648
-
649
- ```ts
650
- Array.from(arr);
651
- ```
652
-
653
- #### To `Blob`
654
-
655
- ```ts
656
- // only if arr is a view of its entire backing TypedArray
657
- new Blob([arr.buffer], { type: "text/plain" });
658
- ```
659
-
660
- <!-- #### To `File`
661
-
662
- ```ts
663
- new File([arr.buffer], "filename.txt", { type: "text/plain", lastModified: Date.now() });
664
- ``` -->
665
-
666
- #### To `ReadableStream`
667
-
668
- ```ts
669
- new ReadableStream({
670
- start(controller) {
671
- controller.enqueue(arr);
672
- controller.close();
673
- },
674
- });
675
- ```
676
-
677
- {% details summary="With chunking" %}
678
- To stream the `ArrayBuffer` in chunks, split the `TypedArray` into chunks and enqueue each one individually.
679
-
680
- ```ts
681
- new ReadableStream({
682
- start(controller) {
683
- for (let i = 0; i < arr.length; i += chunkSize) {
684
- controller.enqueue(arr.slice(i, i + chunkSize));
685
- }
686
- controller.close();
687
- },
688
- });
689
- ```
690
-
691
- {% /details %}
692
-
693
- ### From `DataView`
694
-
695
- #### To `ArrayBuffer`
696
-
697
- ```ts
698
- view.buffer;
699
- ```
700
-
701
- #### To `TypedArray`
702
-
703
- Only works if the `byteLength` of the `DataView` is a multiple of the `BYTES_PER_ELEMENT` of the `TypedArray` subclass.
704
-
705
- ```ts
706
- new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
707
- new Uint16Array(view.buffer, view.byteOffset, view.byteLength / 2);
708
- new Uint32Array(view.buffer, view.byteOffset, view.byteLength / 4);
709
- // etc...
710
- ```
711
-
712
- #### To `Buffer`
713
-
714
- ```ts
715
- Buffer.from(view.buffer, view.byteOffset, view.byteLength);
716
- ```
717
-
718
- #### To `string`
719
-
720
- As UTF-8:
721
-
722
- ```ts
723
- new TextDecoder().decode(view);
724
- ```
725
-
726
- #### To `number[]`
727
-
728
- ```ts
729
- Array.from(view);
730
- ```
731
-
732
- #### To `Blob`
733
-
734
- ```ts
735
- new Blob([view.buffer], { type: "text/plain" });
736
- ```
737
-
738
- <!-- #### To `File`
739
-
740
- ```ts
741
- new File([view.buffer], "filename.txt", { type: "text/plain", lastModified: Date.now() });
742
- ``` -->
743
-
744
- #### To `ReadableStream`
745
-
746
- ```ts
747
- new ReadableStream({
748
- start(controller) {
749
- controller.enqueue(view.buffer);
750
- controller.close();
751
- },
752
- });
753
- ```
754
-
755
- {% details summary="With chunking" %}
756
- To stream the `ArrayBuffer` in chunks, split the `DataView` into chunks and enqueue each one individually.
757
-
758
- ```ts
759
- new ReadableStream({
760
- start(controller) {
761
- for (let i = 0; i < view.byteLength; i += chunkSize) {
762
- controller.enqueue(view.buffer.slice(i, i + chunkSize));
763
- }
764
- controller.close();
765
- },
766
- });
767
- ```
768
-
769
- {% /details %}
770
-
771
- ### From `Buffer`
772
-
773
- #### To `ArrayBuffer`
774
-
775
- ```ts
776
- buf.buffer;
777
- ```
778
-
779
- #### To `TypedArray`
780
-
781
- ```ts
782
- new Uint8Array(buf);
783
- ```
784
-
785
- #### To `DataView`
786
-
787
- ```ts
788
- new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
789
- ```
790
-
791
- #### To `string`
792
-
793
- As UTF-8:
794
-
795
- ```ts
796
- buf.toString();
797
- ```
798
-
799
- As base64:
800
-
801
- ```ts
802
- buf.toString("base64");
803
- ```
804
-
805
- As hex:
806
-
807
- ```ts
808
- buf.toString("hex");
809
- ```
810
-
811
- #### To `number[]`
812
-
813
- ```ts
814
- Array.from(buf);
815
- ```
816
-
817
- #### To `Blob`
818
-
819
- ```ts
820
- new Blob([buf], { type: "text/plain" });
821
- ```
822
-
823
- <!-- #### To `File`
824
-
825
- ```ts
826
- new File([buf], "filename.txt", { type: "text/plain", lastModified: Date.now() });
827
- ``` -->
828
-
829
- #### To `ReadableStream`
830
-
831
- ```ts
832
- new ReadableStream({
833
- start(controller) {
834
- controller.enqueue(buf);
835
- controller.close();
836
- },
837
- });
838
- ```
839
-
840
- {% details summary="With chunking" %}
841
- To stream the `ArrayBuffer` in chunks, split the `Buffer` into chunks and enqueue each one individually.
842
-
843
- ```ts
844
- new ReadableStream({
845
- start(controller) {
846
- for (let i = 0; i < buf.length; i += chunkSize) {
847
- controller.enqueue(buf.slice(i, i + chunkSize));
848
- }
849
- controller.close();
850
- },
851
- });
852
- ```
853
-
854
- {% /details %}
855
-
856
- ### From `Blob`
857
-
858
- #### To `ArrayBuffer`
859
-
860
- The `Blob` class provides a convenience method for this purpose.
861
-
862
- ```ts
863
- await blob.arrayBuffer();
864
- ```
865
-
866
- #### To `TypedArray`
867
-
868
- ```ts
869
- await blob.bytes();
870
- ```
871
-
872
- #### To `DataView`
873
-
874
- ```ts
875
- new DataView(await blob.arrayBuffer());
876
- ```
877
-
878
- #### To `Buffer`
879
-
880
- ```ts
881
- Buffer.from(await blob.arrayBuffer());
882
- ```
883
-
884
- #### To `string`
885
-
886
- As UTF-8:
887
-
888
- ```ts
889
- await blob.text();
890
- ```
891
-
892
- #### To `number[]`
893
-
894
- ```ts
895
- Array.from(await blob.bytes());
896
- ```
897
-
898
- #### To `ReadableStream`
899
-
900
- ```ts
901
- blob.stream();
902
- ```
903
-
904
- <!-- ### From `File` -->
905
-
906
- ### From `ReadableStream`
907
-
908
- It's common to use [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) as a convenient intermediate representation to make it easier to convert `ReadableStream` to other formats.
909
-
910
- ```ts
911
- stream; // ReadableStream
912
-
913
- const buffer = new Response(stream).arrayBuffer();
914
- ```
915
-
916
- However this approach is verbose and adds overhead that slows down overall performance unnecessarily. Bun implements a set of optimized convenience functions for converting `ReadableStream` various binary formats.
917
-
918
- #### To `ArrayBuffer`
919
-
920
- ```ts
921
- // with Response
922
- new Response(stream).arrayBuffer();
923
-
924
- // with Bun function
925
- Bun.readableStreamToArrayBuffer(stream);
926
- ```
927
-
928
- #### To `Uint8Array`
929
-
930
- ```ts
931
- // with Response
932
- new Response(stream).bytes();
933
-
934
- // with Bun function
935
- Bun.readableStreamToBytes(stream);
936
- ```
937
-
938
- #### To `TypedArray`
939
-
940
- ```ts
941
- // with Response
942
- const buf = await new Response(stream).arrayBuffer();
943
- new Int8Array(buf);
944
-
945
- // with Bun function
946
- new Int8Array(Bun.readableStreamToArrayBuffer(stream));
947
- ```
948
-
949
- #### To `DataView`
950
-
951
- ```ts
952
- // with Response
953
- const buf = await new Response(stream).arrayBuffer();
954
- new DataView(buf);
955
-
956
- // with Bun function
957
- new DataView(Bun.readableStreamToArrayBuffer(stream));
958
- ```
959
-
960
- #### To `Buffer`
961
-
962
- ```ts
963
- // with Response
964
- const buf = await new Response(stream).arrayBuffer();
965
- Buffer.from(buf);
966
-
967
- // with Bun function
968
- Buffer.from(Bun.readableStreamToArrayBuffer(stream));
969
- ```
970
-
971
- #### To `string`
972
-
973
- As UTF-8:
974
-
975
- ```ts
976
- // with Response
977
- await new Response(stream).text();
978
-
979
- // with Bun function
980
- await Bun.readableStreamToText(stream);
981
- ```
982
-
983
- #### To `number[]`
984
-
985
- ```ts
986
- // with Response
987
- const arr = await new Response(stream).bytes();
988
- Array.from(arr);
989
-
990
- // with Bun function
991
- Array.from(new Uint8Array(Bun.readableStreamToArrayBuffer(stream)));
992
- ```
993
-
994
- Bun provides a utility for resolving a `ReadableStream` to an array of its chunks. Each chunk may be a string, typed array, or `ArrayBuffer`.
995
-
996
- ```ts
997
- // with Bun function
998
- Bun.readableStreamToArray(stream);
999
- ```
1000
-
1001
- #### To `Blob`
1002
-
1003
- ```ts
1004
- new Response(stream).blob();
1005
- ```
1006
-
1007
- <!-- #### To `File`
1008
-
1009
- ```ts
1010
- new Response(stream)
1011
- .blob()
1012
- .then(blob => new File([blob], "filename.txt", { type: "text/plain", lastModified: Date.now() }));
1013
- ``` -->
1014
-
1015
- #### To `ReadableStream`
1016
-
1017
- To split a `ReadableStream` into two streams that can be consumed independently:
1018
-
1019
- ```ts
1020
- const [a, b] = stream.tee();
1021
- ```
1022
-
1023
- <!-- - Use Buffer
1024
- - TextEncoder
1025
- - `Bun.ArrayBufferSink`
1026
- - ReadableStream
1027
- - AsyncIterator
1028
- - TypedArray vs ArrayBuffer vs DataView
1029
- - Bun.indexOfLine
1030
- - “direct” readablestream
1031
- - readable stream has assumptions about
1032
- - its very generic
1033
- - all data is copies and queued
1034
- - direct : no queueing
1035
- - just a write function
1036
- - you can write strings
1037
- - more synchronous
1038
- - corking works better -->