type
Spawn.Readable
Referenced types
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.