Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bun): Support new Bun.serve APIs #16035

Merged
merged 3 commits into from
Apr 11, 2025
Merged

Conversation

AbhiPrasad
Copy link
Member

@AbhiPrasad AbhiPrasad commented Apr 11, 2025

Supercedes #15978

resolves #15941
resolves #15827
resolves #15816

Bun recently updated their Bun.serve API with new functionality, which unfortunately broke our existing instrumentation. This is detailed with https://bun.sh/docs/api/http#bun-serve. Specifically, they added a new routing API that looks like so:

Bun.serve({
  // `routes` requires Bun v1.2.3+
  routes: {
    // Dynamic routes
    "/users/:id": req => {
      return new Response(`Hello User ${req.params.id}!`);
    },

    // Per-HTTP method handlers
    "/api/posts": {
      GET: () => new Response("List posts"),
      POST: async req => {
        const body = await req.json();
        return Response.json({ created: true, ...body });
      },
    },

    // Wildcard route for all routes that start with "/api/" and aren't otherwise matched
    "/api/*": Response.json({ message: "Not found" }, { status: 404 }),

    // Redirect from /blog/hello to /blog/hello/world
    "/blog/hello": Response.redirect("/blog/hello/world"),

    // Serve a file by buffering it in memory
    "/favicon.ico": new Response(await Bun.file("./favicon.ico").bytes(), {
      headers: {
        "Content-Type": "image/x-icon",
      },
    }),
  },

  // (optional) fallback for unmatched routes:
  // Required if Bun's version < 1.2.3
  fetch(req) {
    return new Response("Not Found", { status: 404 });
  },
});

Because there are now dynamic routes and wildcard routes, we can actually generate route transaction source and send parameterized routes to Sentry. The fetch API is still supported.

The only API we don't support is static routes/responses. This is because these are optimized by Bun itself, and if we turn it into a function (which we need to do to time it), we'll lose out on the optimization. For now they aren't instrumented.

@AbhiPrasad AbhiPrasad requested a review from a team April 11, 2025 12:00
@AbhiPrasad AbhiPrasad self-assigned this Apr 11, 2025
@AbhiPrasad AbhiPrasad requested review from mydea and Lms24 and removed request for a team April 11, 2025 12:00
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

});
}

// TODO: Handle static routes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: IIUC correctly, we likely won't ever instrument static routes to avoid breaking the customization? So is this TODO even necessary?

Suggested change
// TODO: Handle static routes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shot a message to the Bun team about this, I guess we can remove the TODO in the code, and instead create a new GH issue we can use to track it.

If the bun team comes back with it not being possible, we can just close the issue and change the docs accordingly.

@AbhiPrasad AbhiPrasad enabled auto-merge (squash) April 11, 2025 12:51
@AbhiPrasad AbhiPrasad merged commit be737e4 into develop Apr 11, 2025
152 checks passed
@AbhiPrasad AbhiPrasad deleted the abhi-bun-serve-new-apis branch April 11, 2025 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants