function
file
Blob powered by the fastest system calls available for operating on files.
This Blob is lazy: it does no work until you read from it.
sizeis not valid until the contents of the file are read at least once.typeis auto-set based on the file extension when possible
The path to the file (lazily loaded). If the path starts with s3://, the file behaves like S3File
const file = Bun.file("./hello.json");
console.log(file.type); // "application/json"
console.log(await file.json()); // { hello: "world" }Blob that uses the fastest system calls available to operate on files.
This Blob is lazy: it does no work until you read from it. Errors propagate as promise rejections.
Blob.size is not valid until the contents of the file are read at least once. Blob.type is set based on the file extension when possible
The path to the file as a byte buffer (the buffer is copied). If the path starts with s3://, the file behaves like S3File
const file = Bun.file(new TextEncoder().encode("./hello.json"));
console.log(file.type); // "application/json"Blob powered by the fastest system calls available for operating on files.
This Blob is lazy: it does no work until you read from it.
sizeis not valid until the contents of the file are read at least once.
An open file descriptor
const file = Bun.file(fd);Referenced types
class URL
The URL interface represents an object providing static methods used for creating object URLs.
interface BlobPropertyBag
interface BunFile
Blob powered by the fastest system calls available for operating on files.
This Blob is lazy: it does no work until you read from it.
sizeis not valid until the contents of the file are read at least once.typeis auto-set based on the file extension when possible
const file = Bun.file("./hello.json");
console.log(file.type); // "application/json"
console.log(await file.text()); // '{"hello":"world"}'Returns a promise that resolves to the contents of the blob as an ArrayBuffer
Returns a promise that resolves to the contents of the blob as a Uint8Array (array of bytes). Equivalent to
new Uint8Array(await blob.arrayBuffer())Deletes the file (same as unlink)
Does the file exist?
This returns true for regular files and FIFOs. It returns false for directories. A race condition can occur where the file is deleted or renamed after this is called but before you open it.
This does a system call to check if the file exists, which can be slow.
If using this in an HTTP server, it's faster to instead use
return new Response(Bun.file(path))and then anerrorhandler to handle exceptions.Instead of checking for a file's existence and then performing the operation, it is faster to just perform the operation and handle the error.
For empty Blob, this always returns true.
Read the data from the blob as a FormData object.
This first decodes the data from UTF-8, then parses it as a
multipart/form-databody or anapplication/x-www-form-urlencodedbody.The blob's
typeproperty determines the format of the body.This is a non-standard addition to the
BlobAPI, to make it conform more closely to theBodyMixinAPI.Wrap this blob in a Bun.Image pipeline. Equivalent to
new Bun.Image(this, options)— the constructor is synchronous (the underlying read happens lazily when an Image terminal is awaited), so this works onBun.file(),Bun.s3(), fd-backed and in-memory blobs alike:await Bun.file("photo.jpg").image().resize(400).webp().write("thumb.webp");Read the data from the blob as a JSON object.
This first decodes the data from UTF-8, then parses it as JSON.
- begin?: number,end?: number,contentType?: string
Offset any operation on the file starting at
beginand ending atend.endis relative to 0Similar to
TypedArray.subarray. Does not copy the file, open the file, or modify the file.If
begin> 0, () is slower on macOS@param beginstart offset in bytes
@param endabsolute offset in bytes (relative to 0)
@param contentTypeMIME type for the new BunFile
begin?: number,contentType?: stringOffset any operation on the file starting at
beginSimilar to
TypedArray.subarray. Does not copy the file, open the file, or modify the file.If
begin> 0, Bun.write() is slower on macOS@param beginstart offset in bytes
@param contentTypeMIME type for the new BunFile
Returns a readable stream of the blob's contents
Returns a promise that resolves to the contents of the blob as a string
Deletes the file.
- data: string | ArrayBuffer | SharedArrayBuffer | BunFile | Request | Response | ArrayBufferView<ArrayBufferLike>,options?: { highWaterMark: number }): Promise<number>;
Write data to the file. This is equivalent to using Bun.write with a BunFile.
@param dataThe data to write.
@param optionsThe options to use for the write.
class Uint8Array<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>
A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the requested number of bytes could not be allocated an exception is raised.
- @param index
The zero-based index of the desired code unit. A negative index will count back from the last item.
- target: number,start: number,end?: number): this;
Returns the this object after copying a section of the array identified by start and end to the same array starting at position target
@param targetIf target is negative, it is treated as length+target where length is the length of the array.
@param startIf start is negative, it is treated as length+start. If end is negative, it is treated as length+end.
@param endIf not specified, length of the this object is used as its default value.
Returns an array of key, value pairs for every entry in the array
- predicate: (value: number, index: number, array: this) => unknown,thisArg?: any): boolean;
Determines whether all the members of an array satisfy the specified test.
@param predicateA function that accepts up to three arguments. The every method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value false, or until the end of the array.
@param thisArgAn object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
- fill(value: number,start?: number,end?: number): this;
Changes all array elements from
starttoendindex to a staticvalueand returns the modified array@param valuevalue to fill array section with
@param startindex to start filling the array at. If start is negative, it is treated as length+start where length is the length of the array.
@param endindex to stop filling the array at. If end is negative, it is treated as length+end.
- predicate: (value: number, index: number, array: this) => any,thisArg?: any
Returns the elements of an array that meet the condition specified in a callback function.
@param predicateA function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
@param thisArgAn object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
- find(predicate: (value: number, index: number, obj: this) => boolean,thisArg?: any): undefined | number;
Returns the value of the first element in the array where predicate is true, and undefined otherwise.
@param predicatefind calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, find immediately returns that element value. Otherwise, find returns undefined.
@param thisArgIf provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
- predicate: (value: number, index: number, obj: this) => boolean,thisArg?: any): number;
Returns the index of the first element in the array where predicate is true, and -1 otherwise.
@param predicatefind calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns true. If such an element is found, findIndex immediately returns that element index. Otherwise, findIndex returns -1.
@param thisArgIf provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
- predicate: (value: number, index: number, array: this) => value is S,thisArg?: any): undefined | S;
Returns the value of the last element in the array where predicate is true, and undefined otherwise.
@param predicatefindLast calls predicate once for each element of the array, in descending order, until it finds one where predicate returns true. If such an element is found, findLast immediately returns that element value. Otherwise, findLast returns undefined.
@param thisArgIf provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
predicate: (value: number, index: number, array: this) => unknown,thisArg?: any): undefined | number; - predicate: (value: number, index: number, array: this) => unknown,thisArg?: any): number;
Returns the index of the last element in the array where predicate is true, and -1 otherwise.
@param predicatefindLastIndex calls predicate once for each element of the array, in descending order, until it finds one where predicate returns true. If such an element is found, findLastIndex immediately returns that element index. Otherwise, findLastIndex returns -1.
@param thisArgIf provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.
- callbackfn: (value: number, index: number, array: this) => void,thisArg?: any): void;
Performs the specified action for each element in an array.
@param callbackfnA function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
@param thisArgAn object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
- searchElement: number,fromIndex?: number): boolean;
Determines whether an array includes a certain element, returning true or false as appropriate.
@param searchElementThe element to search for.
@param fromIndexThe position in this array at which to begin searching for searchElement.
- searchElement: number,fromIndex?: number): number;
Returns the index of the first occurrence of a value in an array.
@param searchElementThe value to locate in the array.
@param fromIndexThe array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
- join(separator?: string): string;
Adds all the elements of an array separated by the specified separator string.
@param separatorA string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.
Returns an list of keys in the array
- searchElement: number,fromIndex?: number): number;
Returns the index of the last occurrence of a value in an array.
@param searchElementThe value to locate in the array.
@param fromIndexThe array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
- map(callbackfn: (value: number, index: number, array: this) => number,thisArg?: any
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param callbackfnA function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param thisArgAn object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
- callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number): number;
Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
@param callbackfnA function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number,initialValue: number): number;callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: this) => U,initialValue: U): U;Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
@param callbackfnA function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@param initialValueIf initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
- callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number): number;
Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
@param callbackfnA function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number,initialValue: number): number;callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: this) => U,initialValue: U): U;Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
@param callbackfnA function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
@param initialValueIf initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
Reverses the elements in an Array.
- @param array
A typed or untyped array of values to set.
@param offsetThe index in the current array at which the values are to be written.
- base64: string,offset?: number): { read: number; written: number };
Set the contents of the Uint8Array from a base64 encoded string
@param base64The base64 encoded string to decode into the array
@param offsetOptional starting index to begin setting the decoded bytes (default: 0)
- hex: string): { read: number; written: number };
Set the contents of the Uint8Array from a hex encoded string
@param hexThe hex encoded string to decode into the array. The string must have an even number of characters, contain only hexadecimal characters, and contain no whitespace.
- @param start
The beginning of the specified portion of the array.
@param endThe end of the specified portion of the array. This is exclusive of the element at the index 'end'.
- some(predicate: (value: number, index: number, array: this) => unknown,thisArg?: any): boolean;
Determines whether the specified callback function returns true for any element of an array.
@param predicateA function that accepts up to three arguments. The some method calls the predicate function for each element in the array until the predicate returns a value which is coercible to the Boolean value true, or until the end of the array.
@param thisArgAn object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
- @param compareFn
Function used to determine the order of the elements. It is expected to return a negative value if first argument is less than second argument, zero if they're equal and a positive value otherwise. If omitted, the elements are sorted in ascending order.
[11,2,22,1].sort((a, b) => a - b) - begin?: number,end?: number
Gets a new Uint8Array view of the ArrayBuffer store for this array, referencing the elements at begin, inclusive, up to end, exclusive.
@param beginThe index of the beginning of the array.
@param endThe index of the end of the array.
- options?: { alphabet: 'base64' | 'base64url'; omitPadding: boolean }): string;
Convert the Uint8Array to a base64 encoded string
Convert the Uint8Array to a hex encoded string
Converts a number to a string by using the current locale.
Copies the array and returns the copy with the elements in reverse order.
- compareFn?: (a: number, b: number) => number
Copies and sorts the array.
@param compareFnFunction used to determine the order of the elements. It is expected to return a negative value if the first argument is less than the second argument, zero if they're equal, and a positive value otherwise. If omitted, the elements are sorted in ascending order.
const myNums = Uint8Array.from([11, 2, 22, 1]); myNums.toSorted((a, b) => a - b) // Uint8Array(4) [1, 2, 11, 22] Returns a string representation of an array.
Returns the primitive value of the specified object.
Returns an list of values in the array
- with(index: number,value: number
Copies the array and inserts the given number at the provided index.
@param indexThe index of the value to overwrite. If the index is negative, then it replaces from the end of the array.
@param valueThe value to insert into the copied array.
@returnsA copy of the original array with the inserted value.
class ArrayBuffer
Represents a raw buffer of binary data, which is used to store data for the different typed arrays. ArrayBuffers cannot be read from or written to directly, but can be passed to a typed array or DataView Object to interpret the raw buffer as needed.