Node fs.openAsBlob function | API Reference | Bun
BuildDocsReferenceGuidesBlogDiscord/
node:fs/
openAsBlobFopenAsBlob
Search the reference...
/
BuildDocsReferenceGuidesBlogDiscord/
node:fs/
openAsBlobFopenAsBlob
function
fs.openAsBlobfunction
openAsBlob(path:
PathLike,options?:
OpenAsBlobOptions): Promise
Blob>;
Returns a Blob whose data is backed by the given file.
The file must not be modified after the Blob is created. Any modifications will cause reading the Blob data to fail with a DOMException error. Synchronous stat operations on the file when the Blob is created, and before each read in order to detect whether the file data has been modified on disk.
import { openAsBlob } from 'node:fs';
const blob = await openAsBlob('the.file.txt');
const ab = await blob.arrayBuffer();
blob.stream();
Referenced typestype
PathLike = string |
Buffer |
URLValid types for path values in "fs".
interface
OpenAsBlobOptionstype?: string
An optional mime type for the blob.
class
BlobA file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format. The File interface is based on Blob, inheriting blob functionality and expanding it to support files on the user's system.
MDN Reference
readonly
size: number
MDN Reference
readonly
type: string
MDN Reference
arrayBuffer(): Promise
ArrayBuffer>;
Returns a promise that resolves to the contents of the blob as an ArrayBuffer
bytes(): Promise
Uint8ArrayArrayBufferLike>>;
MDN Reference
bytes(): Promise
Uint8ArrayArrayBuffer>>;
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())
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.
image(options?:
ConstructorOptions):
Image;
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 on Bun.file(), Bun.s3(), fd-backed and in-memory blobs alike:
await Bun.file("photo.jpg").image().resize(400).webp().write("thumb.webp");
json(): Promiseany>;
Read the data from the blob as a JSON object.
This first decodes the data from UTF-8, then parses it as JSON.
slice(start?: number,end?: number,contentType?: string):
Blob;
MDN Reference
stream():
ReadableStreamUint8ArrayArrayBufferLike>>;
MDN Reference
stream():
ReadableStreamUint8ArrayArrayBuffer>>;
Returns a readable stream of the blob's contents
text(): Promisestring>;
Returns a promise that resolves to the contents of the blob as a string
Resources
ReferenceDocsGuidesDiscordMerch StoreGitHubBlog Toolkit
RuntimePackage managerTest runnerBundlerPackage runnerProject
Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicenseBaked with ❤️ in San Francisco
We're hiring →