file

Bun

Symbol

file

function file(path: string | URL, options?: BlobPropertyBag): BunFile

Blob powered by the fastest system calls available for operating on files.

This Blob is lazy. That means it won't do any work until you read from it.

  • size will not be valid until the contents of the file are read at least once.
  • type is auto-set based on the file extension when possible
@param path

The path to the file (lazily loaded) if the path starts with s3:// it will behave like S3File

const file = Bun.file("./hello.json");
console.log(file.type); // "application/json"
console.log(await file.json()); // { hello: "world" }
function file(path: ArrayBufferLike | Uint8Array<ArrayBufferLike>, options?: BlobPropertyBag): BunFile

Blob that leverages the fastest system calls available to operate on files.

This Blob is lazy. It won't do any work until you read from it. Errors propagate as promise rejections.

Blob.size will not be valid until the contents of the file are read at least once. Blob.type will have a default set based on the file extension

@param path

The path to the file as a byte buffer (the buffer is copied) if the path starts with s3:// it will behave like S3File

const file = Bun.file(new TextEncoder.encode("./hello.json"));
console.log(file.type); // "application/json"
function file(fileDescriptor: number, options?: BlobPropertyBag): BunFile

Blob powered by the fastest system calls available for operating on files.

This Blob is lazy. That means it won't do any work until you read from it.

  • size will not be valid until the contents of the file are read at least once.
@param fileDescriptor

The file descriptor of the file

const file = Bun.file(fd);

Referenced types

class URL

The URL interface represents an object providing static methods used for creating object URLs.

MDN Reference

interface BlobPropertyBag

  • endings?: EndingType
  • type?: string

    Set a default "type". Not yet implemented.

interface BunFile

Blob powered by the fastest system calls available for operating on files.

This Blob is lazy. That means it won't do any work until you read from it.

  • size will not be valid until the contents of the file are read at least once.
  • type is 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"}'
  • lastModified: number

    A UNIX timestamp indicating when the file was last modified.

  • readonly name?: string

    The name or path of the file, as specified in the constructor.

  • readonly size: number
  • readonly type: string
  • Returns a promise that resolves to the contents of the blob as an ArrayBuffer

  • bytes(): Promise<Uint8Array<ArrayBufferLike>>

    Returns a promise that resolves to the contents of the blob as a Uint8Array (array of bytes) its the same as new Uint8Array(await blob.arrayBuffer())

  • delete(): Promise<void>

    Deletes the file (same as unlink)

  • exists(): Promise<boolean>

    Does the file exist?

    This returns true for regular files and FIFOs. It returns false for directories. Note that 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 an error handler 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.

  • formData(): Promise<FormData>

    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-data body or a application/x-www-form-urlencoded body.

    The type property of the blob is used to determine the format of the body.

    This is a non-standard addition to the Blob API, to make it conform more closely to the BodyMixin API.

  • json(): Promise<any>

    Read the data from the blob as a JSON object.

    This first decodes the data from UTF-8, then parses it as JSON.

  • slice(begin?: number, end?: number, contentType?: string): BunFile

    Offset any operation on the file starting at begin and ending at end. end is relative to 0

    Similar to TypedArray.subarray. Does not copy the file, open the file, or modify the file.

    If begin > 0, () will be slower on macOS

    @param begin

    start offset in bytes

    @param end

    absolute offset in bytes (relative to 0)

    @param contentType

    MIME type for the new BunFile

    slice(begin?: number, contentType?: string): BunFile

    Offset any operation on the file starting at begin

    Similar to TypedArray.subarray. Does not copy the file, open the file, or modify the file.

    If begin > 0, Bun.write() will be slower on macOS

    @param begin

    start offset in bytes

    @param contentType

    MIME type for the new BunFile

    slice(contentType?: string): BunFile

    Slice the file from the beginning to the end, optionally with a new MIME type.

    @param contentType

    MIME type for the new BunFile

  • stat(): Promise<Stats>

    Provides useful information about the file.

  • stream(): ReadableStream<Uint8Array<ArrayBufferLike>>

    Returns a readable stream of the blob's contents

  • text(): Promise<string>

    Returns a promise that resolves to the contents of the blob as a string

  • write(data: string | ArrayBuffer | SharedArrayBuffer | Request | Response | BunFile | ArrayBufferView<ArrayBufferLike>, options?: { highWaterMark: number }): Promise<number>

    Write data to the file. This is equivalent to using Bun.write with a BunFile.

    @param data

    The data to write.

    @param options

    The options to use for the write.

  • writer(options?: { highWaterMark: number }): FileSink

    Incremental writer for files and pipes.

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.

  • readonly [Symbol.toStringTag]: 'Uint8Array'
  • readonly buffer: TArrayBuffer

    The ArrayBuffer instance referenced by the array.

  • readonly byteLength: number

    The length in bytes of the array.

  • readonly byteOffset: number

    The offset in bytes of the array.

  • readonly BYTES_PER_ELEMENT: number

    The size in bytes of each element in the array.

  • readonly length: number

    The length of the array.

  • [Symbol.iterator](): ArrayIterator<number>
  • at(index: number): undefined | number

    Returns the item located at the specified index.

    @param index

    The zero-based index of the desired code unit. A negative index will count back from the last item.

  • copyWithin(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 target

    If target is negative, it is treated as length+target where length is the length of the array.

    @param start

    If start is negative, it is treated as length+start. If end is negative, it is treated as length+end.

    @param end

    If not specified, length of the this object is used as its default value.

  • entries(): ArrayIterator<[number, number]>

    Returns an array of key, value pairs for every entry in the array

  • every(predicate: (value: number, index: number, array: this) => unknown, thisArg?: any): boolean

    Determines whether all the members of an array satisfy the specified test.

    @param predicate

    A 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 thisArg

    An 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 start to end index to a static value and returns the modified array

    @param value

    value to fill array section with

    @param start

    index 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 end

    index to stop filling the array at. If end is negative, it is treated as length+end.

  • filter(predicate: (value: number, index: number, array: this) => any, thisArg?: any): Uint8Array<ArrayBuffer>

    Returns the elements of an array that meet the condition specified in a callback function.

    @param predicate

    A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.

    @param thisArg

    An 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 predicate

    find 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 thisArg

    If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

  • findIndex(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 predicate

    find 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 thisArg

    If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

  • findLast<S extends number>(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 predicate

    findLast 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 thisArg

    If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

    findLast(predicate: (value: number, index: number, array: this) => unknown, thisArg?: any): undefined | number
  • findLastIndex(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 predicate

    findLastIndex 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 thisArg

    If provided, it will be used as the this value for each invocation of predicate. If it is not provided, undefined is used instead.

  • forEach(callbackfn: (value: number, index: number, array: this) => void, thisArg?: any): void

    Performs the specified action for each element in an array.

    @param callbackfn

    A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.

    @param thisArg

    An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

  • includes(searchElement: number, fromIndex?: number): boolean

    Determines whether an array includes a certain element, returning true or false as appropriate.

    @param searchElement

    The element to search for.

    @param fromIndex

    The position in this array at which to begin searching for searchElement.

  • indexOf(searchElement: number, fromIndex?: number): number

    Returns the index of the first occurrence of a value in an array.

    @param searchElement

    The value to locate in the array.

    @param fromIndex

    The 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 separator

    A 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.

  • keys(): ArrayIterator<number>

    Returns an list of keys in the array

  • lastIndexOf(searchElement: number, fromIndex?: number): number

    Returns the index of the last occurrence of a value in an array.

    @param searchElement

    The value to locate in the array.

    @param fromIndex

    The 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): Uint8Array<ArrayBuffer>

    Calls a defined callback function on each element of an array, and returns an array that contains the results.

    @param callbackfn

    A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.

    @param thisArg

    An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

  • reduce(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 callbackfn

    A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

    reduce(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number, initialValue: number): number
    reduce<U>(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 callbackfn

    A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

    @param initialValue

    If 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.

  • reduceRight(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 callbackfn

    A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.

    reduceRight(callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: this) => number, initialValue: number): number
    reduceRight<U>(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 callbackfn

    A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.

    @param initialValue

    If 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.

  • reverse(): this

    Reverses the elements in an Array.

  • set(array: ArrayLike<number>, offset?: number): void

    Sets a value or an array of values.

    @param array

    A typed or untyped array of values to set.

    @param offset

    The index in the current array at which the values are to be written.

  • setFromBase64(base64: string, offset?: number): { read: number; written: number }

    Set the contents of the Uint8Array from a base64 encoded string

    @param base64

    The base64 encoded string to decode into the array

    @param offset

    Optional starting index to begin setting the decoded bytes (default: 0)

  • setFromHex(hex: string): { read: number; written: number }

    Set the contents of the Uint8Array from a hex encoded string

    @param hex

    The hex encoded string to decode into the array. The string must have an even number of characters, be valid hexadecimal characters and contain no whitespace.

  • slice(start?: number, end?: number): Uint8Array<ArrayBuffer>

    Returns a section of an array.

    @param start

    The beginning of the specified portion of the array.

    @param end

    The 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 predicate

    A 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 thisArg

    An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

  • sort(compareFn?: (a: number, b: number) => number): this

    Sorts an array.

    @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)
    
  • subarray(begin?: number, end?: number): Uint8Array<TArrayBuffer>

    Gets a new Uint8Array view of the ArrayBuffer store for this array, referencing the elements at begin, inclusive, up to end, exclusive.

    @param begin

    The index of the beginning of the array.

    @param end

    The index of the end of the array.

  • toBase64(options?: { alphabet: 'base64' | 'base64url'; omitPadding: boolean }): string

    Convert the Uint8Array to a base64 encoded string

    @returns

    The base64 encoded string representation of the Uint8Array

  • toHex(): string

    Convert the Uint8Array to a hex encoded string

    @returns

    The hex encoded string representation of the Uint8Array

  • toLocaleString(): string

    Converts a number to a string by using the current locale.

    toLocaleString(locales: string | string[], options?: NumberFormatOptions): string
  • Copies the array and returns the copy with the elements in reverse order.

  • toSorted(compareFn?: (a: number, b: number) => number): Uint8Array<ArrayBuffer>

    Copies and sorts the array.

    @param compareFn

    Function 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]
    
  • toString(): string

    Returns a string representation of an array.

  • valueOf(): this

    Returns the primitive value of the specified object.

  • values(): ArrayIterator<number>

    Returns an list of values in the array

  • with(index: number, value: number): Uint8Array<ArrayBuffer>

    Copies the array and inserts the given number at the provided index.

    @param index

    The index of the value to overwrite. If the index is negative, then it replaces from the end of the array.

    @param value

    The value to insert into the copied array.

    @returns

    A copy of the original array with the inserted value.