write

Bun

Symbol

BunFile.write

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.

Referenced types

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.

  • readonly [Symbol.toStringTag]: string
  • readonly byteLength: number

    Read-only. The length of the ArrayBuffer (in bytes).

  • resize(newByteLength?: number): void

    Resizes the ArrayBuffer to the specified size (in bytes).

    MDN

    resize(byteLength: number): ArrayBuffer

    Resize an ArrayBuffer in-place.

  • slice(begin: number, end?: number): ArrayBuffer

    Returns a section of an ArrayBuffer.

  • transfer(newByteLength?: number): ArrayBuffer

    Creates a new ArrayBuffer with the same byte content as this buffer, then detaches this buffer.

    MDN

  • transferToFixedLength(newByteLength?: number): ArrayBuffer

    Creates a new non-resizable ArrayBuffer with the same byte content as this buffer, then detaches this buffer.

    MDN

interface SharedArrayBuffer

class Request

This Fetch API interface represents a resource request.

MDN Reference

  • readonly body: null | ReadableStream<Uint8Array<ArrayBufferLike>>
  • readonly bodyUsed: boolean
  • readonly cache: RequestCache

    Returns the cache mode associated with request, which is a string indicating how the request will interact with the browser's cache when fetching.

    MDN Reference

  • readonly credentials: RequestCredentials

    Returns the credentials mode associated with request, which is a string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL.

    MDN Reference

  • readonly destination: RequestDestination

    Returns the kind of resource requested by request, e.g., "document" or "script".

    MDN Reference

  • readonly headers: Headers

    Returns a Headers object consisting of the headers associated with request. Note that headers added in the network layer by the user agent will not be accounted for in this object, e.g., the "Host" header.

    MDN Reference

  • readonly integrity: string

    Returns request's subresource integrity metadata, which is a cryptographic hash of the resource being fetched. Its value consists of multiple hashes separated by whitespace. [SRI]

    MDN Reference

  • readonly keepalive: boolean

    Returns a boolean indicating whether or not request can outlive the global in which it was created.

    MDN Reference

  • readonly method: string

    Returns request's HTTP method, which is "GET" by default.

    MDN Reference

  • readonly mode: RequestMode

    Returns the mode associated with request, which is a string indicating whether the request will use CORS, or will be restricted to same-origin URLs.

    MDN Reference

  • readonly redirect: RequestRedirect

    Returns the redirect mode associated with request, which is a string indicating how redirects for the request will be handled during fetching. A request will follow redirects by default.

    MDN Reference

  • readonly referrer: string

    Returns the referrer of request. Its value can be a same-origin URL if explicitly set in init, the empty string to indicate no referrer, and "about:client" when defaulting to the global's default. This is used during fetching to determine the value of the Referer header of the request being made.

    MDN Reference

  • readonly referrerPolicy: ReferrerPolicy

    Returns the referrer policy associated with request. This is used during fetching to compute the value of the request's referrer.

    MDN Reference

  • readonly signal: AbortSignal

    Returns the signal associated with request, which is an AbortSignal object indicating whether or not request has been aborted, and its abort event handler.

    MDN Reference

  • readonly url: string
  • bytes(): Promise<Uint8Array<ArrayBufferLike>>
  • json(): Promise<any>
  • text(): Promise<string>

class Response

This Fetch API interface represents the response to a request.

MDN Reference

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.

type ArrayBufferView<TArrayBuffer extends ArrayBufferLike = ArrayBufferLike> = NodeJS.TypedArray<TArrayBuffer> | DataView<TArrayBuffer>