### Set up a simple server with rescript-bun Source: https://github.com/zth/rescript-bun/blob/main/README.md Example of setting up a basic HTTP server using Bun.serve. It demonstrates how to access request headers and send a response. ```rescript let server = Bun.serve({ fetch: async (request, _server) => { let userName = request ->Request.headers ->Headers.get("x-user-name") ->Option.getOr("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} લાગ!`) ``` -------------------------------- ### Install rescript-bun Source: https://context7.com/zth/rescript-bun/llms.txt Install the rescript-bun library using npm. Configure rescript.json to open RescriptBun and RescriptBun.Globals. ```bash npm i rescript-bun@2 ``` ```json { "dependencies": ["rescript-bun"], "bsc-flags": ["-open RescriptBun", "-open RescriptBun.Globals"] } ``` -------------------------------- ### Use file system router with rescript-bun Source: https://github.com/zth/rescript-bun/blob/main/README.md Example of creating a file system router using Bun.FileSystemRouter.make. This setup is inspired by Next.js routing and specifies a directory for pages and an origin. ```rescript let router = Bun.FileSystemRouter.make({ style: NextJs, dir: "./pages", origin: "https://mydomain.com", assetPrefix: "_next/static/", }) let matches = router->Bun.FileSystemRouter.match("/") ``` -------------------------------- ### Install rescript-bun Source: https://github.com/zth/rescript-bun/blob/main/README.md Install the rescript-bun package using npm. Ensure you are using ReScript v12 or later. ```bash npm i rescript-bun@2 ``` -------------------------------- ### Bun.S3 Client for Object Storage Source: https://context7.com/zth/rescript-bun/llms.txt Demonstrates creating a scoped S3 client, writing, reading, checking existence, getting stats, generating presigned URLs, listing objects, and deleting files. Ensure correct credentials and bucket name are configured. ```rescript open Bun.S3 // Create a scoped client let client = S3Client.make({ accessKeyId: "AKIAIOSFODNN7EXAMPLE", secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", region: "us-east-1", bucket: "my-bucket", }) // Get a file handle and write to it let s3File = client->S3Client.file("uploads/photo.jpg") let _ = await s3File->S3File.writeString("file content here") // Read back let exists = await s3File->S3File.exists Console.log(`Exists: ${exists->Bool.toString}`) let stats = await s3File->S3File.stat Console.log(`Size: ${stats.size->Float.toString}, ETag: ${stats.etag}`) // Generate a presigned URL (expires in 5 minutes) let url = s3File->S3File.presign(~options={expiresIn: 300.}) Console.log(`Download URL: ${url}`) // List objects let listing = await client->S3Client.list(~input={prefix: "uploads/", maxKeys: 10.}) switch listing.contents { | Some(items) => items->Array.forEach(item => Console.log(item.key->Option.getOr("?"))) | None => Console.log("No items found") } // Delete await s3File->S3File.delete ``` -------------------------------- ### Write tests with rescript-bun Source: https://github.com/zth/rescript-bun/blob/main/README.md Use the 'Test' module to write tests with Bun's built-in test runner. This example demonstrates a simple addition test. ```rescript open Test describe("Playing around with tests", () => { test("addition works", () => { expect(1 + 1)->Expect.toBe(2) }) }) ``` -------------------------------- ### CSRF Token Generation and Verification with Bun.CSRF Source: https://context7.com/zth/rescript-bun/llms.txt Provides examples for generating CSRF tokens using explicit secrets or default in-memory secrets, with options for algorithm, encoding, and expiry. Verification with and without explicit options is also shown. ```rescript let secret = "my-very-secret-key" // Generate with explicit secret let token = Bun.CSRF.generateWithSecret(secret) Console.log(token) // base64url encoded token // Generate with options let customToken = Bun.CSRF.generateWithSecretOptions(secret, { algorithm: Sha256, encoding: Base64Url, expiresIn: 3600000., // 1 hour in ms }) // Verify let isValid = Bun.CSRF.verifyWithOptions(token, {secret, algorithm: Sha256}) Console.log(`CSRF valid: ${isValid->Bool.toString}`) // Default in-memory secret (suitable for single-process apps) let defaultToken = Bun.CSRF.generate() let defaultValid = Bun.CSRF.verify(defaultToken) Console.log(`Default CSRF valid: ${defaultValid->Bool.toString}) ``` -------------------------------- ### Transform HTML with HTMLRewriter in Rescript Source: https://context7.com/zth/rescript-bun/llms.txt Shows how to use HTMLRewriter to modify HTML content. Examples include renaming tags, collecting specific elements, and injecting new content. This is useful for dynamic content manipulation on the server. ```rescript // Rewrite all