ffi opens shared libraries with dlopen and turns declared C symbols
into JS functions. No glue code, no build step, no Node-API — the native
library can be anything with a C ABI: system libz, sqlite3, your own
libfoo.dylib.
Native code runs with the full privileges of the ff process and bypasses
every permission scope, so loading it is gated:
Permissions
--allow-ffi is a bare switch — there are no per-library or per-pointer
scopes. It grants every library and every memory helper, and --allow-all
includes it.
Every ffi entry point except ffi.allowed requires the flag:
ffi.allowed reports whether the flag is set:
fs, so loading a
.dylib / .so does not need --allow-read. --allow-ffi is the only
gate, and it does not constrain the path.
All pointer and memory helpers are gated.ffi.ptr,ffi.view,ffi.cstring,ffi.read,ffi.write,ffi.copyBytes,ffi.writeBytes,ffi.struct,ffi.union, andffi.isNullall require--allow-ffi.ffi.allowedis the only ungated member. There is no pointer-only mode.
Prebuilt binaries ship without FFI. Release binaries and the GHCR image are built withFull model: Permissions.-Dffi=false, soffi.dlopenthrowsffi.dlopen: FFI not compiled in (build with -Dffi=true)after--allow-ffiis granted. That is a build-capability error, not a permission denial. FFI works out of the box in source builds (the default).
Quick look
dlopen
path— anything the OS loader accepts: absolute,./-relative, or a system name ("libz.1.dylib","libz.so.1").symbols—{ name: { parameters: [...], result: "<type>" | struct | union, optional?: true, nonblocking?: true } }.- Missing library →
TypeErrorwith the loader’s error text. - Missing symbol →
TypeError, unlessoptional: true, in which caselib.symbols.nameisnull. - Max 32 parameters per symbol.
A closed library throws
TypeError: ffi: library is closed. If the
library object is garbage-collected, the handle is closed for you.
Types
Unknown type names throw at
dlopen. void as a parameter throws.
Pointers
FfiPtr is an opaque handle — the address lives inside it.
--allow-ffi; see
Permissions.
Memory
Read and write native memory at any pointer, with byte offsets:ffi.view(p) returns the same pointer object with DataView-style
getters (Deno-compatible names):
pointer; cstring strings
are read with ffi.cstring/getCString, buffers with
ffi.copyBytes/copyInto. No bounds checking — C’s rules.
Structs
A C struct is a byte image in C layout. Describe it once, get the layout math for free:offsets). Deno-style
type-only lists work too — fields become f0, f1, …:
u8 padding fields yourself. Field types may be any
scalar including pointer/cstring/buffer, or nested struct/union
specs. void fields are rejected.
Nested structs work inline or with named specs:
- Parameters: pass a
Uint8Arrayof at leastBlob.sizebytes, fields written atBlob.offsets. - Results: a fresh
Uint8ArrayofBlob.sizebytes — read it withDataView, orffi.view(ffi.ptr(result)).
void f(const z_stream *)) don’t use struct types at all: lay the fields into a scratch
buffer and pass it as buffer/pointer. Blob.offsets and
ffi.read/ffi.write do the layout work:
Unions
A C union overlays all members at offset 0. Size = the largest member’s size, aligned to the largest member’s alignment. Passing works exactly like structs — byte images in, byte images out:{ union: [...] } works
in symbol specs and as nested struct fields:
Callbacks
Pass a JS function where C expects a function pointer. Create one withffi.callback(spec, fn):
ffi.callback requires --allow-ffi, like ffi.dlopen.
The spec shape matches symbol specs (parameters, result). Use
add.pointer as a function-type argument:
- same-thread: rethrows at the caller (
lib.symbols.apply(...)throws) - foreign-thread with
nonblocking: rejects the enclosing Promise - foreign-thread without
nonblocking: reported as an uncaught error
close() frees the native closure. It refuses (TypeError) while a
call is in flight — call it when C is done with the pointer. The
finalizer is a backstop; freeing under a live native pointer crashes.
cstring results are not supported from callbacks (v1).
Nonblocking calls
Mark a symbolnonblocking: true and it returns a Promise instead of
running on the JS thread. The native call runs on a worker thread; the
event loop stays free:
bufferarguments are copied in at call time and copied back after settle. Mutation is only visible after the Promise resolves.cstringarguments are copied;pointeris passed by value (caller keeps the memory alive).- The result is wrapped at settle time (same as a sync call).
- Up to 32 concurrent calls (job pool).
Ownership
The rules are C’s rules — the FFI adds no safety net:- A
bufferyou pass must stay alive for the duration of the call. Keep theTypedArrayreferenced; don’t rely on detached/collected memory. (Fornonblockingcalls, buffers are copied — safe to mutate after settle.) - A
pointer/bufferresult carries no ownership. If the library allocated it, call the library’s own free function —ffinever frees native memory. cstringresults and struct/union results are copies — always safe to keep.- Calling past a buffer’s bounds, or through a stale pointer, is undefined behavior. Keep the FFI surface you expose small.
Contracts & limits
- Deadlock: never invoke a JS callback from a thread spawned by a
synchronous
fficall. The JS thread is blocked insideffi_calland cannot service the bridge. Callbacks from threads owned by C (or from nonblocking worker threads) are safe. close()while busy:FfiCallback.close()throws while a bridged call is in flight or a nonblocking call holds the callback. Close after C is done.cstringcallback results are not supported (v1).- No Windows (POSIX
dlopenonly), no variadic functions. - Max 32 parameters, 64 fields/members, 512 bytes per struct/union, nesting depth 8.
--allow-ffihas no path or library scopes; it grants every library.- Denial is a
PermissionDeniedError withcode: "PERMISSION_DENIED". FFI messages omit thefor '...'suffix because there is no scope.
See also
- Permissions — flags, scopes, denial contract
- child_process — another full-authority escape hatch
- API overview — every injected global
- Limitations — every hard cap