{"id":17921864,"url":"https://github.com/zth/rescript-bun","last_synced_at":"2025-04-03T11:14:27.242Z","repository":{"id":207333028,"uuid":"718991732","full_name":"zth/rescript-bun","owner":"zth","description":"Use Bun with ReScript.","archived":false,"fork":false,"pushed_at":"2024-06-05T08:55:00.000Z","size":125,"stargazers_count":49,"open_issues_count":1,"forks_count":4,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-06-05T10:14:05.667Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"ReScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zth.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-15T08:07:35.000Z","updated_at":"2024-06-05T08:55:03.000Z","dependencies_parsed_at":"2023-11-15T09:27:09.696Z","dependency_job_id":"f9c24882-2518-4f5b-ae02-059dcc2fe2dc","html_url":"https://github.com/zth/rescript-bun","commit_stats":null,"previous_names":["zth/rescript-bun"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zth%2Frescript-bun","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zth%2Frescript-bun/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zth%2Frescript-bun/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zth%2Frescript-bun/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zth","download_url":"https://codeload.github.com/zth/rescript-bun/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246989750,"owners_count":20865331,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-28T20:36:30.369Z","updated_at":"2025-04-03T11:14:27.224Z","avatar_url":"https://github.com/zth.png","language":"ReScript","readme":"# rescript-bun\n\n_Check out the `2.x` branch for ReScript v12 support._\n\nUse [Bun](https://bun.sh) with ReScript.\n\n\u003e **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!\n\n## Getting started\n\n**Template repo to get up and running quickly: https://github.com/zth/rescript-bun-starter**\n\nYou need to be on ReScript v11 `\u003e=11.1.0` and Core `\u003e=1.3.0`. This is designed for uncurried mode so you should use that as well (`\"uncurried\": true` in your bsconfig/`rescript.json`).\n\nInstall `rescript-bun` and `@rescript/core`:\n\n```bash\nnpm i rescript-bun @rescript/core\n```\n\nInclude them in your `rescript.json`:\n\n```json\n{\n  \"bs-dependencies\": [\"@rescript/core\", \"rescript-bun\"]\n}\n```\n\n`rescript-bun` is [namespaced](https://rescript-lang.org/docs/manual/latest/build-configuration#name-namespace), so you'll find all modules listed under the main module `RescriptBun`.\n\n**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`:\n\n```json\n{\n  \"bsc-flags\": [\n    \"-open RescriptCore\",\n    \"-open RescriptBun\",\n    \"-open RescriptBun.Globals\"\n  ]\n}\n```\n\n\u003e 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.\n\nThis will make all of Bun available to you without needing to dip into the `RescriptBun` module explicitly.\n\n## Credits\n\nThis lib copies [`rescript-nodejs`](https://github.com/TheSpyder/rescript-nodejs) for Bun's Node compatible bindings. Shout out to the maintainers of that project!\n\n## Examples\n\nHere'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.\n\n### Testing\n\nTo write tests using Bun's built in test runner, just `open Test` and you'll have everything available to you to write your tests:\n\n```rescript\nopen Test\n\ndescribe(\"Playing around with tests\", () =\u003e {\n  test(\"addition works\", () =\u003e {\n    expect(1 + 1)-\u003eExpect.toBe(2)\n  })\n})\n```\n\nThis will make all of Bun's testing utilities available to you in the global scope.\n\n### Setting up a simple server\n\nHere's setting up a simple web server.\n\n```rescript\nlet server = Bun.serve({\n  fetch: async (request, _server) =\u003e {\n    let userName =\n      request\n      -\u003eRequest.headers\n      -\u003eHeaders.get(\"x-user-name\")\n      -\u003eOption.getWithDefault(\"Unknown user\")\n\n    Response.make(`Hello ${userName}!`, ~options={status: 200})\n  },\n})\n\nlet port =\n  server\n  -\u003eBun.Server.port\n  -\u003eInt.toString\n\nlet hostName = server-\u003eBun.Server.hostname\n\nConsole.log(`Server listening on http://${hostName}:${port}!`)\n```\n\n### Hashing and verifying a password\n\n```rescript\nlet password = \"super-secure-pa$$word\"\n\nlet bcryptHash = await Bun.Password.hash(\n  password,\n  ~algorithm=BCryptAlgorithm({\n    cost: 4, // number between 4-31\n  }),\n)\n\nlet isMatch = await Bun.Password.verify(password, ~hash)\n```\n\n### Using the file system router\n\n```rescript\nlet router = Bun.FileSystemRouter.make({\n  style: NextJs,\n  dir: \"./pages\",\n  origin: \"https://mydomain.com\",\n  assetPrefix: \"_next/static/\",\n})\n\nlet matches = router-\u003eBun.FileSystemRouter.match(\"/\")\n```\n\n### Using HTMLRewriter to rewrite HTML\n\n```rescript\n// Rewrite all \u003cdiv\u003e to \u003csection\u003e\n\nlet rewriter = HTMLRewriter.make()-\u003eHTMLRewriter.on(\n  \"*\",\n  {\n    element: element =\u003e {\n      if element.tagName === \"div\" {\n        element.tagName = \"section\"\n      }\n    },\n  },\n)\n\nlet response = await fetch(\"https://bun.sh\")\nlet transformedResponse = rewriter-\u003eHTMLRewriter.transform(response)\n\nlet html = await transformedResponse-\u003eResponse.text\n\nConsole.log(html)\n```\n\n## Current project state\n\nCurrently, 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.\n\n### Missing bindings\n\n#### Crucial\n\n- [x] Globals\n- [x] Bun\n- [x] Tests\n- [x] Fs\n- [x] Stream (some stream utils already exist in Globals + Bun)\n- [x] Path\n\n#### Prio 2\n\n- [x] AsyncHooks\n- [x] Crypto\n- [x] Buffer\n- [x] Child_process (needs Stream?)\n- [x] HTML Rewriter\n- [x] Os\n- [ ] Sqlite\n- [x] Perf hooks\n- [x] StringDecoder\n- [x] Readline (needs Stream?)\n- [x] WorkerThreads\n\n#### Prio 3\n\n- [ ] FFI\n- [x] Util\n- [ ] SupportsColors\n- [x] Timers\n- [x] Tls\n- [x] Tty\n\n#### Unclear if needed\n\n- [x] Assert\n- [ ] Diagnostics channel\n- [x] Dns\n- [ ] Domain\n- [x] Events\n- [ ] JSC\n- [x] Module\n- [x] Net\n- [x] VM\n- [ ] WS (already multiple websocket things present)\n- [x] Zlib (bun has its own gzip?)\n- [x] Http (Nodes built in HTTP, should use Bun's own, right?)\n\n#### Definitively not needed\n\n- URL (available in globals)\n- Constants (deprecated)\n- Querystring (deprecated)\n- Punycode (deprecated)\n- Console (Core has it)\n\n## Other things to figure out\n\n- How to reuse/contribute to [`rescript-webapi`](https://github.com/TheSpyder/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.\n\n## Contributing\n\nContributions 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.\n\nIf you do want to contribute, _please open an issue saying you're starting work on module X_. So we don't accidentally double work.\n\n## Bindings style\n\n_This will be fleshed out in a short while_.\n","funding_links":[],"categories":["ReScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzth%2Frescript-bun","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzth%2Frescript-bun","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzth%2Frescript-bun/lists"}