js_bun

    bun-specific js bindings

    js
    bun
    Download zip
    Author
    Version
    0.13.1
    License
    MIT
    Last updated
    3 days ago
    Downloads
    99

    #mizchi/js_bun

    MoonBit bindings for Bun-specific APIs — the Bun global and the runtime's own extras. Everything else a Bun program needs (fetch, URL, streams, the JS built-ins) lives in mizchi/js_web and mizchi/js_builtin.

    Single package: import it as mizchi/js_bun.

    #API Support Status

    Status reflects what bun_test/ actually exercises — 🧪 covered, 🚧 bound but untested. Note that the bun test step in CI is continue-on-error: true, so these are advisory rather than gating.

    CategoryAPIStatusNote
    Entry point
    Bun globalbun(), is_bun()🧪 Testedis_bun() guards non-Bun runtimes
    Process
    Process infoargv(), cwd(), main_(), version()🧪 Tested
    Environmentenv(), env_get()🚧 Partially
    Build inforevision()🚧 Partially
    Exitexit(code?)🚧 Partially
    File I/O
    Readfile_text(), file_arrayBuffer()🧪 TestedBun.file(path).text() / .arrayBuffer()
    Writewrite(path, data)🧪 TestedReturns bytes written
    Deleteunlink_sync(path)🧪 Tested
    Subprocess
    Spawnspawn(SpawnOptions)🧪 TestedSubprocess exposes pid, exitCode, exited(), kill(), stdio
    Spawn (sync)spawnSync()🚧 Partially
    Lookupwhich(cmd)🧪 Tested
    HTTP server
    Serveserve(port?, hostname?, handler)🚧 PartiallyServer exposes port, hostname, stop()
    Hashing / crypto
    Hashhash_(data, algorithm?)🧪 Tested
    Passwordspassword_hash(), password_verify()🧪 TestedArgon2 by default
    UUIDrandom_uuid_v7()🧪 Tested
    CryptoHashernew(), update(), digest_hex/base64/buffer()🚧 Partially
    Glob
    MatchingGlob(), match_(), scan(cwd?)🚧 Partially
    Test runner
    Definetest_(), test_async()🧪 Testedbun test; this module's own suite uses them
    Assertexpect() → toBe, toEqual, toContain, toBeTruthy, toBeFalsy, toBeNull, toBeDefined, toBeUndefined🧪 Tested
    Misc
    Timingnanoseconds(), sleep(ms)🧪 Tested
    StringsescapeHTML(), stringWidth()🧪 Tested
    Lazy promisespeek(v)🚧 Partially

    #Usage

    # moon.mod import { "mizchi/js_core@0.13.0", "mizchi/js_bun@0.13.0", }

    # moon.pkg import { "mizchi/js_core" @core, "mizchi/js_bun" @bun, }

    The module root's default alias would be @js_bun, so give it @bun as above.

    ///|
    pub async fn read_config() -> String {
    @bun.bun().file_text("config.json")
    }

    See the User Guide for picking modules and aliases.

    Bun

    #external
    pub type Bun

    Bun::argv

    fn Bun::argv(self : Bun) -> Array[String]

    Get command line arguments (excluding the binary and script path)

    Bun::as_any

    fn Bun::as_any(self : Bun) ->
    Any

    Bun::cwd

    fn Bun::cwd(self : Bun) -> String

    Get current working directory (Bun.cwd is a property, not a function)

    Bun::env

    fn Bun::env(self : Bun) ->
    Any

    Get all environment variables as an object

    Bun::env_get

    fn Bun::env_get(self : Bun, key : String) -> String?

    Get environment variable

    Bun::escapeHTML

    fn Bun::escapeHTML(self : Bun, html : String) -> String

    Escape HTML string (Bun.escapeHTML)

    Bun::exit

    fn Bun::exit(self : Bun, code? : Int) -> Unit

    Exit the process

    Bun::file_arrayBuffer

    async fn Bun::file_arrayBuffer(self : Bun, path : String) ->
    ArrayBuffer

    Read a file as ArrayBuffer

    Bun::file_text

    async fn Bun::file_text(self : Bun, path : String) -> String

    Read a file as text

    Bun::hash_

    fn Bun::hash_(self : Bun, data :
    Any
    , algorithm? : String) ->
    Any

    Hash data with specified algorithm (Bun.hash) Algorithms: "sha1", "sha256", "sha512", "md5", etc.

    Bun::main_

    fn Bun::main_(self : Bun) -> String

    Get the main module path NOTE: If you define ::main, moon coverage analyze will fail https://github.com/moonbitlang/moonbit-docs/issues/1071

    Bun::nanoseconds

    fn Bun::nanoseconds(self : Bun) -> Int

    Get a high-resolution timestamp in nanoseconds

    Bun::password_hash

    async fn Bun::password_hash(self : Bun, password : String) -> String

    Hash a password using bcrypt

    Bun::password_verify

    async fn Bun::password_verify(self : Bun, password : String, hash : String) -> Bool

    Verify a password against a hash

    Bun::peek

    fn Bun::peek(self : Bun, promise :
    Any
    ) ->
    Any

    Peek at a Promise state (Bun.peek)

    Bun::revision

    fn Bun::revision(self : Bun) -> String

    Get Bun revision

    Bun::serve

    fn Bun::serve(self : Bun, port? : Int, hostname? : String, fetch :
    Any
    ) -> Server

    Create an HTTP server with a fetch handler

    Bun::sleep

    async fn Bun::sleep(self : Bun, ms : Int) -> Unit

    Sleep for a duration in milliseconds

    Bun::spawn

    fn Bun::spawn(self : Bun, opts : SpawnOptions) -> Subprocess

    Spawn a subprocess

    Bun::spawnSync

    fn Bun::spawnSync(self : Bun, opts : SpawnOptions) ->
    Any

    Spawn a subprocess synchronously

    Bun::stringWidth

    fn Bun::stringWidth(self : Bun, text : String) -> Int

    Get string display width (Bun.stringWidth)

    Bun::test_

    fn[E : Show + Error] Bun::test_(self : Bun, name : String, f : (TestContext) -> Unit raise E) -> Unit

    Bun.test(name, fn) - wrapper for convenience

    Bun::test_async

    fn Bun::test_async(self : Bun, name : String, f : async (TestContext) -> Unit) -> Unit

    Bun.test(name, async fn) - wrapper for convenience

    Bun::version

    fn Bun::version(self : Bun) -> String

    Get Bun version

    Bun::which

    fn Bun::which(self : Bun, bin : String) -> String?

    Find executable in PATH (Bun.which)

    Bun::write

    async fn Bun::write(self : Bun, path : String, data :
    Any
    ) -> Int

    Write data to a file

    CryptoHasher

    #external
    pub type CryptoHasher

    CryptoHasher::CryptoHasher

    fn CryptoHasher::CryptoHasher(algorithm : String) -> CryptoHasher

    Create a new CryptoHasher with specified algorithm

    CryptoHasher::as_any

    CryptoHasher::digest_base64

    fn CryptoHasher::digest_base64(self : CryptoHasher) -> String

    Get digest as base64 string

    CryptoHasher::digest_buffer

    Get digest as ArrayBuffer

    CryptoHasher::digest_hex

    fn CryptoHasher::digest_hex(self : CryptoHasher) -> String

    Get digest as hex string

    CryptoHasher::new

    #deprecated("use `CryptoHasher(..)` instead")
    fn CryptoHasher::new(algorithm : String) -> CryptoHasher

    Deprecated alias for the CryptoHasher(..) constructor.

    CryptoHasher::update

    fn CryptoHasher::update(self : CryptoHasher, data :
    Any
    ) -> Unit

    Update hasher with data

    Expectation

    #external
    pub type Expectation

    expect() function for assertions

    Expectation::as_any

    Expectation::toBe

    fn Expectation::toBe(self : Expectation, expected :
    Any
    ) -> Unit

    Assert that the value equals the expected value

    Expectation::toBeDefined

    fn Expectation::toBeDefined(self : Expectation) -> Unit

    Assert that the value is defined (not undefined)

    Expectation::toBeFalsy

    fn Expectation::toBeFalsy(self : Expectation) -> Unit

    Assert that the value is falsy

    Expectation::toBeNull

    fn Expectation::toBeNull(self : Expectation) -> Unit

    Assert that the value is null

    Expectation::toBeTruthy

    fn Expectation::toBeTruthy(self : Expectation) -> Unit

    Assert that the value is truthy

    Expectation::toBeUndefined

    fn Expectation::toBeUndefined(self : Expectation) -> Unit

    Assert that the value is undefined

    Expectation::toContain

    fn Expectation::toContain(self : Expectation, substring : String) -> Unit

    Assert that a string contains a substring

    Expectation::toEqual

    fn Expectation::toEqual(self : Expectation, expected :
    Any
    ) -> Unit

    Assert that the value equals the expected value (deep equality)

    Glob

    #external
    pub type Glob

    Glob::Glob

    fn Glob::Glob(pattern : String) -> Glob

    Create a Glob matcher with pattern

    Glob::as_any

    fn Glob::as_any(self : Glob) ->
    Any

    Glob::match_

    fn Glob::match_(self : Glob, path : String) -> Bool

    Match a string against the glob pattern

    Glob::new

    #deprecated("use `Glob(..)` instead")
    fn Glob::new(pattern : String) -> Glob

    Deprecated alias for the Glob(..) constructor.

    Glob::scan

    fn Glob::scan(self : Glob, cwd? : String) ->
    Any

    Scan files matching the glob pattern

    Server

    #external
    pub type Server

    Server::as_any

    fn Server::as_any(self : Server) ->
    Any

    Server::hostname

    fn Server::hostname(self : Server) -> String

    Get the server hostname

    Server::port

    fn Server::port(self : Server) -> Int

    Get the server port

    Server::stop

    fn Server::stop(self : Server) -> Unit

    Stop the server

    SpawnOptions

    pub struct SpawnOptions {
    cmd : Array[String]
    cwd : String?
    env :
    Any
    ?
    stdin : String?
    stdout : String?
    stderr : String?
    }

    Spawn options for Bun.spawn()

    SpawnOptions::SpawnOptions

    fn SpawnOptions::SpawnOptions(cmd : Array[String]) -> SpawnOptions

    Create spawn options from command array

    SpawnOptions::new

    #deprecated("use `SpawnOptions(..)` instead")
    fn SpawnOptions::new(cmd : Array[String]) -> SpawnOptions

    Deprecated alias for the SpawnOptions(..) constructor.

    Subprocess

    #external
    pub type Subprocess

    Subprocess::as_any

    Subprocess::exitCode

    fn Subprocess::exitCode(self : Subprocess) -> Int?

    Get exit code (if process has exited)

    Subprocess::exited

    async fn Subprocess::exited(self : Subprocess) -> Int

    Wait for the process to exit

    Subprocess::kill

    fn Subprocess::kill(self : Subprocess, signal? : Int) -> Unit

    Kill the subprocess

    Subprocess::pid

    fn Subprocess::pid(self : Subprocess) -> Int

    Get process ID

    Subprocess::stderr

    Get stderr reader

    Subprocess::stdin

    Get stdin writer

    Subprocess::stdout

    Get stdout reader

    TestContext

    #external
    pub type TestContext

    TestContext::as_any

    TestContext::name

    fn TestContext::name(self : TestContext) -> String

    Get the test name

    bun

    fn bun() -> Bun

    Bun global object

    expect

    fn expect(value :
    Any
    ) -> Expectation

    Create an expectation

    is_bun

    fn is_bun() -> Bool

    Returns true if running in Bun

    random_uuid_v7

    fn random_uuid_v7() -> String

    Generate UUIDv7 (Bun.randomUUIDv7)
    fn unlink_sync(path : String) -> Unit

    Delete a file (uses Node.js fs.unlinkSync for compatibility)