brioche
brioche
exposes common, shared types across the entirety of Bun, and
provides an opinionated set of bindings on default Bun namespace.
Every functions not implemented have already their equivalent in the Gleam
world, and they should be privileged when using them is needed.
Unimplemented bindings
Bun.env
, useenvoy
instead.Bun.sleep
, usegleam/javascript/promise.wait
instead.Bun.deepEquals
, use default equality operator in Gleam. Equality in Gleam is already resolved as deep equality.Bun.fileURLToPath
will not be implemented, as solutions like this already exists in Gleam. Otherwise, they should be implemented directly ongleam/uri
.Bun.pathToFileURL
will not be implemented, as solutions like this already exists in Gleam. Otherwise, they should be implemented directly ongleam/uri
.Bun.resolveSync
will not be implemented.Bun.readableStreamTo*
is looking for help to find the correct API to implement.serialize
&deserialize
inbun:jsc
will not be implemented. Using native Gleam ways to serialize & deserialize data should be used instead.estimateShallowMemoryUsageOf
inbun:jsc
will probably not be implemented.
Types
File
created by Bun. A Bun File
represents a lazily-loaded file;
initializing it does not actually read the file from disk. File
should be
used in conjuction with brioche/file
module. Bun File
is the recommended
way to manipulate files on Bun as operations on files are heavily optimised.
However, Bun does not implements every filesystem operations, but instead,
provides an almost complete implementation of
node:fs
module. It should be used for
every complex operations needed on filesystem.
FileSink
should be used instead of File
when incremental
writing are needed.
pub type File
Native incremental file writing on File
. Incremental writing on files
with regular API can be extremely inefficient due to how file writing is
handled with regular File
. FileSink
provides a highly-optimized,
efficient API to write in a File
in a buffered way: instead of writing its
content immediately to the file when a write operation is required,
FileSink
works by buffering the content, and writing the content after
buffer reach a reasonable size. As such, every write operations on FileSink
are synchronous, and can easily be used in synchronous code.
FileSink
can be obtained with file.writer
from a file.
pub type FileSink
Status of a Promise. A Promise can be either fulfilled (i.e. resolved), pending or rejected.
pub type PromiseStatus {
Fulfilled
Pending
Rejected
}
Constructors
-
Fulfilled
-
Pending
-
Rejected
Server created by Bun. Bun implements efficient, high-performance HTTP
servers with a clean & simple API, able to serve static routes, dynamic
routes and WebSockets, all-in-one. Bun servers are the recommended way to
create HTTP & WebSocket servers, but Bun is also fully compatible with
node:http
& node:https
for compatibility
purposes.
Parametric type represents the data passed to a WebSocket connection when
created. Servers can be created using server.serve
in brioche/server
module.
pub type Server(context)
WebSocket created by Bun. Bun implements natively WebSockets when using Bun
Server
. Every connection can easily be upgraded
to a WebSocket connection by using server.upgrade
. Once a connection has
been upgraded, Bun automatically handles the WebSocket lifecycle.
pub type WebSocket(context)
Functions
pub fn deflate(content: BitArray) -> Result(BitArray, Nil)
Compresses a BitArray
using zlib’s DEFLATE algorithm.
import brioche
brioche.deflate(<<"content">>)
// => Ok(<<75, 206, 207, 43, 73, 205, 43, 1, 0>>)
pub fn gunzip(content: BitArray) -> Result(BitArray, Nil)
Decompresses a BitArray
using zlib’s GUNZIP algorithm.
import brioche
brioche.gunzip(<<31, 139, 8, 0, 0, 0, 0, 0, 0, 19, 75, 206, 207, 43, 73, 205, 43, 1, 0, 169, 48, 197, 254, 7, 0, 0, 0>>)
// => Ok(<<"content">>)
pub fn gzip(content: BitArray) -> Result(BitArray, Nil)
Compresses a BitArray
using zlib’s GZIP algorithm.
import brioche
brioche.gzip(<<"content">>)
// => Ok(<<31, 139, 8, 0, 0, 0, 0, 0, 0, 19, 75, 206, 207, 43, 73, 205, 43, 1, 0, 169, 48, 197, 254, 7, 0, 0, 0>>)
pub fn inflate(content: BitArray) -> Result(BitArray, a)
Decompresses a BitArray
using zlib’s DEFLATE algorithm.
import brioche
brioche.inflate(<<75, 206, 207, 43, 73, 205, 43, 1, 0>>)
// => Ok(<<"content">>)
pub fn inspect(content: a) -> String
Inspect a data structure, and return it as string. You should always use
string.inspect
in first intention, unless you’re sure to target Bun only.
pub fn main_script() -> String
An absolute path to the entrypoint of the current program (the file that
was executed with bun run
or gleam run
when using runtime = "bun" in
gleam.toml`).
While probably not particularly useful on Gleam, it can be used to determine if you’re running a script, your main file, etc.
import brioche
brioche.main_script()
// => /path/to/script.mjs
pub fn nanoseconds() -> Int
Returns the current number of nanoseconds elapsed since the start of the
main Bun process. nanoseconds
returns a monotonic time.
pub fn open_in_editor(file: String) -> Nil
Opens a file in the default OS editor. Bun auto-detects the editor via the
$VISUAL
or $EDITOR
environment variables.
pub fn peek(promise: Promise(a)) -> Result(a, Nil)
Reads a Promise result without gleam/javascript/promise.await
, but only
if the promise has already fulfilled. In any other case, peek
will
return an error.
peek
is important when attempting to reduce number of extraneous microticks in performance-sensitive code. It’s an advanced API and you probably shouldn’t use it unless you know what you’re doing. Always prefer usinggleam/javascript/promise.await
.
import brioche
import gleam/javascript/promise
let prom = promise.resolve("example")
let assert Ok(peeked) = brioche.peek(prom)
prom == peeked
// => True
pub fn peek_status(promise: Promise(a)) -> PromiseStatus
Reads a Promise status without resolving it.
peek_status
is an advanced API and you probably shouldn’t use it unless you know whate you’re doing. Always prefer using classical control-flow algorithm.
import bun
import gleam/javascript/promise
let prom = promise.resolve("example")
let status = bun.peek_status(prom)
// => Fulfilled
pub fn random_uuid_v7() -> String
Generates a random UUID v7, which is monotonic and suitable for sorting and databases. A UUID v7 is a 128-bit value that encodes the current timestamp, a random value, a counter & 8 bytes of cryptographically secure random value. The timestamp is encoded using the lowest 48 bits, and the random value and counter are encoded using the remaining bits.
import brioche
brioche.random_uuid_v7()
// => "0192ce11-26d5-7dc3-9305-1426de888c5a"
pub fn revision() -> String
The git commit of Bun that was compiled
to create the current bun
CLI.
import brioche
brioche.revision()
// => "fd9a5ea668e9ffce5fd5f25a7cfd2d45f0eaa85b"
pub fn string_width(string: String) -> Int
Compute the width of a string, in a highly efficient way.
Contrarily to string.length
, that utility can be used when there’s some
performance issue. string.length
should be used in a first intention
unless you’re sure to target only Bun.
pub fn version() -> String
A string containing the version of the bun
CLI that is currently running.
import brioche
brioche.version()
// => "1.2.4"
pub fn wait(milliseconds ms: Int) -> Nil
A blocking, synchronous version of gleam/javascript/promise.wait
.
promise.wait
should almost always be used instead of wait
. However,
that function can be used in scripts, in WebWorkers, or every other
environment where blocking the main thread is not an issue.
Sleep duration should be indicated in milliseconds.
import brioche
// Sleep for 1 second.
brioche.wait(1000)
pub fn which(bin: String) -> Result(String, Nil)
Returns the path to an executable, similar to typing which
in your terminal.
import brioche
brioche.which("ls")
// => "/usr/bin/ls"