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

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.

Bun Documentation

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.

Bun Documentation

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.

Bun Documentation

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.

Bun Documentation

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>>)

Bun Documentation

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">>)

Bun Documentation

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>>)

Bun Documentation

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">>)

Bun Documentation

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.

Bun Documentation

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

Bun Documentation

pub fn nanoseconds() -> Int

Returns the current number of nanoseconds elapsed since the start of the main Bun process. nanoseconds returns a monotonic time.

Bun Documentation

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.

Bun Documentation

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 using gleam/javascript/promise.await.

import brioche
import gleam/javascript/promise

let prom = promise.resolve("example")
let assert Ok(peeked) = brioche.peek(prom)
prom == peeked
// => True

Bun Documentation

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

Bun Documentation

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"

Bun Documentation

pub fn revision() -> String

The git commit of Bun that was compiled to create the current bun CLI.

import brioche
brioche.revision()
// => "fd9a5ea668e9ffce5fd5f25a7cfd2d45f0eaa85b"

Bun Documentation

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.

Bun Documentation

pub fn version() -> String

A string containing the version of the bun CLI that is currently running.

import brioche
brioche.version()
// => "1.2.4"

Bun Documentation

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)

Bun Documentation

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"

Bun Documentation

Search Document