npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rescript-bun

v0.4.1

Published

Use Bun with ReScript.

Downloads

21

Readme

rescript-bun

Use Bun with ReScript.

Currently alpha state software. You're encouraged to start using it, but please report all issues. There will be both bindings missing and some bindings will probably be wrong/suboptimal. We're going to work through the API surface as we go. Report all issues you find!

Getting started

Template repo to get up and running quickly: https://github.com/zth/rescript-bun-starter

You need to be on at least a recent RC of ReScript v11. This is designed for uncurried mode so you should use that as well ("uncurried": true in your bsconfig/rescript.json).

Install rescript-bun and @rescript/core:

npm i rescript-bun @rescript/core

Include them in your rescript.json:

{
  "bs-dependencies": ["@rescript/core", "rescript-bun"]
}

rescript-bun is namespaced, so you'll find all modules listed under the main module RescriptBun.

You're strongly encouraged to open RescriptBun globally, to get the best possible developer experience. You do that by adding this to your rescript.json:

{
  "bsc-flags": [
    "-open RescriptCore",
    "-open RescriptBun",
    "-open RescriptBun.Globals"
  ]
}

Notice -open RescriptBun.Globals. This will expose all Bun globals. This might be a matter of taste, but I recommend opening it to get the best experience.

This will make all of Bun available to you without needing to dip into the RescriptBun module explicitly.

Credits

This lib copies rescript-nodejs for Bun's Node compatible bindings. Shout out to the maintainers of that project!

Examples

Here's a few examples of how it looks. More examples (often inspired by https://bun.sh/guides) can be found in the playground/examples directory in this repo.

Testing

To write tests using Bun's built in test runner, just open Test and you'll have everything available to you to write your tests:

open Test

describe("Playing around with tests", () => {
  test("addition works", () => {
    expect(1 + 1)->Expect.toBe(2)
  })
})

This will make all of Bun's testing utilities available to you in the global scope.

Setting up a simple server

Here's setting up a simple web server.

let server = Bun.serve({
  fetch: async (request, _server) => {
    let userName =
      request
      ->Request.headers
      ->Headers.get("x-user-name")
      ->Option.getWithDefault("Unknown user")

    Response.make(`Hello ${userName}!`, ~options={status: 200})
  },
})

let port =
  server
  ->Bun.Server.port
  ->Int.toString

let hostName = server->Bun.Server.hostname

Console.log(`Server listening on http://${hostName}:${port}!`)

Hashing and verifying a password

let password = "super-secure-pa$$word"

let bcryptHash = await Bun.Password.hash(
  password,
  ~algorithm=BCryptAlgorithm({
    cost: 4, // number between 4-31
  }),
)

let isMatch = await Bun.Password.verify(password, ~hash)

Using the file system router

let router = Bun.FileSystemRouter.make({
  style: NextJs,
  dir: "./pages",
  origin: "https://mydomain.com",
  assetPrefix: "_next/static/",
})

let matches = router->Bun.FileSystemRouter.match("/")

Using HTMLRewriter to rewrite HTML

// Rewrite all <div> to <section>

let rewriter = HTMLRewriter.make()->HTMLRewriter.on(
  "*",
  {
    element: element => {
      if element.tagName === "div" {
        element.tagName = "section"
      }
    },
  },
)

let response = await fetch("https://bun.sh")
let transformedResponse = rewriter->HTMLRewriter.transform(response)

let html = await transformedResponse->Response.text

Console.log(html)

Current project state

Currently, bindings exist for the most common things. There's still a good amount of bindings missing. Some bindings will be covered as we go along, while others won't be added.

Missing bindings

Crucial

  • [x] Globals
  • [x] Bun
  • [x] Tests
  • [x] Fs
  • [x] Stream (some stream utils already exist in Globals + Bun)
  • [x] Path

Prio 2

  • [x] AsyncHooks
  • [x] Crypto
  • [x] Buffer
  • [x] Child_process (needs Stream?)
  • [x] HTML Rewriter
  • [x] Os
  • [ ] Sqlite
  • [x] Perf hooks
  • [x] StringDecoder
  • [x] Readline (needs Stream?)
  • [x] WorkerThreads

Prio 3

  • [ ] FFI
  • [x] Util
  • [ ] SupportsColors
  • [x] Timers
  • [x] Tls
  • [x] Tty

Unclear if needed

  • [x] Assert
  • [ ] Diagnostics channel
  • [x] Dns
  • [ ] Domain
  • [x] Events
  • [ ] JSC
  • [x] Module
  • [x] Net
  • [x] VM
  • [ ] WS (already multiple websocket things present)
  • [x] Zlib (bun has its own gzip?)
  • [x] Http (Nodes built in HTTP, should use Bun's own, right?)

Definitively not needed

  • URL (available in globals)
  • Constants (deprecated)
  • Querystring (deprecated)
  • Punycode (deprecated)
  • Console (Core has it)

Other things to figure out

  • How to reuse/contribute to rescript-webapi instead of rolling our own bindings. I've intentionally not reused any other existing library because I wanted to start from scratch and follow ReScript v11 idioms as much as possible. But once all of this settles, we need to figure out and share the common denominator with rescript-webapi and other similar projects to this.

Contributing

Contributions are very welcome. We're aiming to cover close to 100% of the Bun API surface, which is quite huge task. But, it's definitively possible and the initial large effort pays dividends over time.

If you do want to contribute, please open an issue saying you're starting work on module X. So we don't accidentally double work.

Bindings style

This will be fleshed out in a short while.