new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Bun · Runtime · all subjects

binary data

80 notes in this subject, read out of this brain and free to use. This is page 1 of 2.

Convert ReadableStream to Blob with Response

To convert a ReadableStream to a Blob: await new Response(stream).blob().

Int32Array description and range

Int32Array interprets every four bytes as a signed 32-bit integer. Range is -2147483648 to 2147483647.

Float16Array description and range

Float16Array interprets every two bytes as a 16-bit floating point number. Range is -6.55e4 to 6.55e4.

Float32Array description and range

Float32Array interprets every four bytes as a 32-bit floating point number. Range is -3.4e38 to 3.4e38.

Float64Array description and range

Float64Array interprets every eight bytes as a 64-bit floating point number. Range is -1.7e308 to 1.7e308.

Convert TypedArray to number array

To convert a TypedArray to an array of numbers: Array.from(arr).

Convert TypedArray to Blob

To create a Blob from a TypedArray: new Blob([arr], { type: "text/plain" }).

Convert DataView to ArrayBuffer

To get the underlying ArrayBuffer from a DataView: view.buffer.

Uint32Array construction RangeError on non-multiple buffer

Instantiating a Uint32Array from an ArrayBuffer throws RangeError if the buffer length is not a multiple of 4 bytes. Example: new Uint32Array(new ArrayBuffer(10)); // RangeError: ArrayBuffer length minus the byteOffset is not a multiple of the element size.

TypedArray methods not available

TypedArray does not support array mutation methods that would require resizing the underlying buffer, such as push and pop, because the buffer size is fixed.

TypedArray out of bounds assignment is no-op

Assigning a value to a typed array index that is out of bounds is silently ignored (no-op). For example: arr[3] = 255; where the array length is 3 does not throw an error.

DataView to TypedArray conversion alignment requirement

Converting a DataView to a TypedArray only works if the byteLength of the DataView is a multiple of the BYTES_PER_ELEMENT of the target TypedArray subclass. For example, converting a 3-byte DataView to Uint32Array (4 bytes per element) fails.

TypedArray family overview

TypedArray is a family of classes providing an Array-like interface for binary data. Includes Uint8Array, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array, Float16Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array, and Uint8ClampedArray. TypedArray itself is internal to JavaScript and cannot be instantiated directly.

Uint8Array base64 and hex conversion

Uint8Array in Bun supports toBase64() method to convert to base64 string, and fromBase64(string) static method to convert from base64. Also supports toHex() method to convert to hex string, and fromHex(string) static method to convert from hex.

Blob constructor and creation

Blob is created using new Blob(parts, options) where parts is an array that can contain strings, ArrayBuffer, TypedArray, DataView, or other Blob instances. Constructor concatenates the parts in order. The options parameter can include a type property for MIME type. Blob has properties: type (MIME type) and size (in bytes).

Blob methods for reading contents

Blob provides async methods for reading its contents: await blob.text() returns string content, await blob.bytes() returns Uint8Array copy of contents, await blob.arrayBuffer() returns ArrayBuffer copy of contents, and blob.stream() returns a ReadableStream.

BunFile as Blob subclass

BunFile is a Bun-specific subclass of Blob that represents a lazily-loaded file on disk. Created with Bun.file(path). Like File, it adds a name and lastModified property, but unlike File it does not require the file to be loaded into memory.

File class constructor

File is a subclass of Blob that adds a name and lastModified property. Constructor: new File(parts, filename, options). Parts is an array of string/binary data. Options can include type property for MIME type.

DataView getter and setter methods

DataView provides paired getter/setter methods for reading/writing binary data: getBigInt64/setBigInt64, getBigUint64/setBigUint64, getFloat32/setFloat32, getFloat64/setFloat64, getInt16/setInt16, getInt32/setInt32, getInt8/setInt8, getUint16/setUint16, getUint32/setUint32, getUint8/setUint8.

Uint16Array description and range

Uint16Array interprets every two bytes as an unsigned 16-bit integer. Range is 0 to 65535.

Uint32Array description and range

Uint32Array interprets every four bytes as an unsigned 32-bit integer. Range is 0 to 4294967295.

Int8Array description and range

Int8Array interprets every one byte as a signed 8-bit integer. Range is -128 to 127.

BigUint64Array description and range

BigUint64Array interprets every eight bytes as an unsigned BigInt. Range is 0 to 18446744073709551615, though BigInt is capable of representing larger numbers.

Uint8ClampedArray description

Uint8ClampedArray is the same as Uint8Array but automatically clamps to the range 0-255 when assigning a value to an element.

ArrayBuffer creation

ArrayBuffer is created with new ArrayBuffer(byteLength). It represents a fixed-length sequence of bytes in memory. It does not support array methods or direct read/write operations; you must use a view like TypedArray or DataView to access the data.

ArrayBuffer byteLength and slice

ArrayBuffer provides a byteLength property indicating the size in bytes. It also provides a slice(start, end) method that returns a new ArrayBuffer containing a copy of the bytes from start to end.

TypedArray construction from various sources

TypedArray can be constructed from: (1) an existing ArrayBuffer with optional byteOffset and length parameters, (2) a number indicating the length to create a new buffer, (3) an array of numbers, or (4) another typed array.

TypedArray array methods support

TypedArray supports common array methods including filter, map, reduce, forEach, every, find, includes, and indexOf. Methods like push and pop are not available because they would require resizing the underlying ArrayBuffer.

Buffer Node.js API implementation

Bun implements Node.js Buffer API, a subclass of Uint8Array with convenience methods. Created with Buffer.from(). Provides Array-like and DataView-like methods for working with binary data. Node.js API, not available in browsers.

TextEncoder and TextDecoder for string conversion

TextEncoder.encode(string) converts a string to Uint8Array in UTF-8 encoding. TextDecoder.decode(buffer) converts Uint8Array or other binary data back to a string. TextDecoder defaults to UTF-8 but can support other encodings.

ReadableStream creation and consumption

ReadableStream is created with new ReadableStream(underlyingSource). The underlyingSource object has a start(controller) method where controller.enqueue(chunk) queues data and controller.close() ends the stream. Streams are consumed with for await...of iteration.

Bun readableStream conversion utilities

Bun provides optimized conversion functions from ReadableStream: Bun.readableStreamToArrayBuffer(stream) returns Promise<ArrayBuffer>, Bun.readableStreamToBytes(stream) returns Promise<Uint8Array>, Bun.readableStreamToText(stream) returns Promise<string>, and Bun.readableStreamToArray(stream) returns array of chunks.

ReadableStream.tee() method

ReadableStream provides a tee() method that returns an array of two independent ReadableStream copies. This allows the original stream to be consumed by multiple consumers: const [a, b] = stream.tee();

Convert ArrayBuffer to TypedArray

To create a TypedArray from an ArrayBuffer: new Uint8Array(buf). To create a view over a slice of an ArrayBuffer with a specific byte offset and length: new Uint32Array(buf, byteOffset, length).

Convert ArrayBuffer to DataView

To create a DataView from an ArrayBuffer: new DataView(buf).

Convert ArrayBuffer to Buffer

To create a Buffer from an entire ArrayBuffer: Buffer.from(buf). To create a Buffer from a slice of an ArrayBuffer: Buffer.from(buf, byteOffset, length).

Convert ArrayBuffer to string UTF-8

To convert an ArrayBuffer to a UTF-8 string: new TextDecoder().decode(buf).

Convert ArrayBuffer to number array

To convert an ArrayBuffer to an array of numbers: Array.from(new Uint8Array(buf)).

Convert TypedArray to ArrayBuffer

To get the underlying ArrayBuffer from a TypedArray: arr.buffer. Note that a TypedArray can be a view of a slice of the buffer, so sizes may differ.

Convert TypedArray to DataView

To create a DataView over the same byte range as a TypedArray: new DataView(arr.buffer, arr.byteOffset, arr.byteLength).

Convert TypedArray to Buffer

To create a Buffer from a TypedArray: Buffer.from(arr).

Convert TypedArray to string UTF-8

To convert a TypedArray to a UTF-8 string: new TextDecoder().decode(arr).

Convert DataView to TypedArray

To create a TypedArray from a DataView: new Uint8Array(view.buffer, view.byteOffset, view.byteLength). Conversion only works if byteLength of the DataView is a multiple of the BYTES_PER_ELEMENT of the TypedArray subclass.

Convert DataView to string UTF-8

To convert a DataView to a UTF-8 string: new TextDecoder().decode(view).

Convert DataView to number array

To convert a DataView to an array of numbers: Array.from(new Uint8Array(view.buffer, view.byteOffset, view.byteLength)).

Convert DataView to Blob

To create a Blob from a DataView: new Blob([view], { type: "text/plain" }).

Convert Buffer to ArrayBuffer

To get the underlying ArrayBuffer from a Buffer: buf.buffer.

Convert Buffer to TypedArray

To create a TypedArray from a Buffer: new Uint8Array(buf).

Convert Buffer to DataView

To create a DataView from a Buffer: new DataView(buf.buffer, buf.byteOffset, buf.byteLength).

Convert Buffer to string UTF-8

To convert a Buffer to a UTF-8 string: buf.toString().

Convert Buffer to string base64

To convert a Buffer to a base64 string: buf.toString("base64").

Convert Buffer to string hex

To convert a Buffer to a hex string: buf.toString("hex").

Convert Buffer to number array

To convert a Buffer to an array of numbers: Array.from(buf).

Convert Buffer to Blob

To create a Blob from a Buffer: new Blob([buf], { type: "text/plain" }).

Convert Blob to TypedArray

To convert a Blob to a TypedArray (Uint8Array): await blob.bytes().

Convert Blob to DataView

To convert a Blob to a DataView: new DataView(await blob.arrayBuffer()).

Convert Blob to Buffer

To convert a Blob to a Buffer: Buffer.from(await blob.arrayBuffer()).

Convert Blob to string UTF-8

To convert a Blob to a UTF-8 string: await blob.text().

Convert Blob to number array

To convert a Blob to an array of numbers: Array.from(await blob.bytes()).

Convert ReadableStream to ArrayBuffer with Response

To convert a ReadableStream to an ArrayBuffer using Response: await new Response(stream).arrayBuffer().

Give your agent this brain