Open a library using "bun:ffi"
Symbol
dlopen
The name of the library or file path. This will be passed to dlopen()
Map of symbols to load where the key is the symbol name and the value is the FFIFunction
import {dlopen} from 'bun:ffi';
const lib = dlopen("duckdb.dylib", {
get_version: {
returns: "cstring",
args: [],
},
});
lib.symbols.get_version();
// "1.0.0"
This is powered by just-in-time compiling C wrappers that convert JavaScript types to C types and back. Internally, bun uses tinycc, so a big thanks goes to Fabrice Bellard and TinyCC maintainers for making this possible.
Referenced types
class URL
The URL interface represents an object providing static methods used for creating object URLs.
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"}'
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) its the same as
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. 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 anerror
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.
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 aapplication/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 theBodyMixin
API.Read the data from the blob as a JSON object.
This first decodes the data from UTF-8, then parses it as JSON.
Offset any operation on the file starting at
begin
and ending atend
.end
is relative to 0Similar to
TypedArray.subarray
. Does not copy the file, open the file, or modify the file.If
begin
> 0, () will be slower on macOS@param beginstart offset in bytes
@param endabsolute offset in bytes (relative to 0)
@param contentTypeMIME type for the new 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 beginstart offset in bytes
@param contentTypeMIME type for the new BunFile
Provides useful information about the file.
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.
- 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 dataThe data to write.
@param optionsThe options to use for the write.