Skip to content

Feature Request: Expose Bunrest's routing engine or allow pre-router hooks #49

@AirplanegoBrr

Description

@AirplanegoBrr

I’d like to request an enhancement, exposing bunrest’s underlying routing engine so that it can be passed into Bun.serve- OR A way to run code before its passed to the internal router

This would make it possible to integrate socket.io with bunrest more cleanly using the new @socket.io/bun-engine

Below is a bit of a hacky work around- (running the private openServer function) maybe making it so you can run code BEFORE the request is passed to the router would be viable? I'm not sure how this would be properly implemented tho as my skills are little to none.

You can pass the socket.io WS engine into bunrest via the app.ws, however below because I was running bun.serve myself I changed it to just run the "raw" way

You do need 3 modules for this, socket.io @socket.io/bun-engine and socket.io-client

import { Server } from "socket.io";
import { Server as Engine } from "@socket.io/bun-engine";
import server from "bunrest";
import path from "path"

const io = new Server();
const engine = new Engine({ path: "/socket.io/" });
io.bind(engine);

const app = server();

app.get("/", (req, res) => {
    res.setHeader("content-type", "text/html");
    res.send(`
    <script src="/socket.io/socket.io.min.js"></script>
    <script>
      const socket = io();
      socket.emit("hi");
      socket.on("hello", msg => console.log("server says:", msg));
    </script>
  `);
});

// socket.io events
io.on("connection", (socket) => {
    console.log("client connected", socket.id);
    socket.on("hi", () => {
        socket.emit("hello", "world!");
    });
});

const bunRestRouter = (app as any).openServer(0, "", {})

// start Bun.serve yourself
Bun.serve({
    port: 3100,
    async fetch(req, server) {
        // let socket.io engine handle its endpoints
        if (req.url.includes("/socket.io/")) {
            // You can remove the below and use the socket.io cdn to load the client script if you want
            if (req.url.endsWith("socket.io.min.js")) return new Response(Bun.file(path.join(import.meta.dir, "node_modules/socket.io-client/dist/socket.io.min.js"))); // This is really hacky- please just use the socket.io cdn!
            return engine.handler().fetch(req, server);
        }

        // otherwise pass to bunrest
        return await bunRestRouter.fetch(req, server);
    },
    websocket: {
        ...engine.handler().websocket,
    },
});

console.log("Listening on http://localhost:3100");

It's a shame to see that this project has seen any movement for a year now- but I guess things like the Built-in bun router and elysiajs have kinda stepped on this unfortunately. I do love the express way of making HTTP servers but it seems like its a dying field, CTX and return responses have just become the norm for whatever reason

Sorry for this messy issue! Kept going back and forth on ideas

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions