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(http/unstable): route module #5644

Merged
merged 18 commits into from
Aug 7, 2024
Merged

feat(http/unstable): route module #5644

merged 18 commits into from
Aug 7, 2024

Conversation

iuioiua
Copy link
Contributor

@iuioiua iuioiua commented Aug 6, 2024

This PR adds routing functionality to @std/http. This implementation aims to:

  1. Provide simple and performant routing for majority of basic use cases, but not advanced use cases.
  2. Be as un-opinionated as possible.
  3. Be compatible with Deno.serve() and deno serve.

Benchmarks

Source

import { type Route, route } from "@std/http/route";

// From https://hono.dev/docs/concepts/benchmarks
const routes: Route[] = [
  {
    pattern: new URLPattern({ pathname: "/user" }),
    handler: () => new Response("User"),
  },
  {
    pattern: new URLPattern({ pathname: "/user/comments" }),
    handler: () => new Response("User Comments"),
  },
  {
    pattern: new URLPattern({ pathname: "/user/avatar" }),
    handler: () => new Response("User Avatar"),
  },
  {
    pattern: new URLPattern({ pathname: "/user/lookup/email/:address" }),
    handler: () => new Response("User Lookup Email Address"),
  },
  {
    pattern: new URLPattern({ pathname: "/event/:id" }),
    handler: () => new Response("Event"),
  },
  {
    pattern: new URLPattern({ pathname: "/event/:id/comments" }),
    handler: () => new Response("Event Comments"),
  },
  {
    pattern: new URLPattern({ pathname: "/event/:id/comments" }),
    method: "POST",
    handler: () => new Response("POST Event Comments"),
  },
  {
    pattern: new URLPattern({ pathname: "/status" }),
    method: "POST",
    handler: () => new Response("Status"),
  },
  {
    pattern: new URLPattern({
      pathname: "/very/deeply/nested/route/hello/there",
    }),
    handler: () => new Response("Very Deeply Nested Route"),
  },
  {
    pattern: new URLPattern({ pathname: "/user/lookup/username/:username" }),
    handler: (_req, _info, params) => {
      return new Response(`Hello ${params?.pathname.groups.username}`);
    },
  },
];

const handler = route(routes, () => new Response("Not Found"));

for (
  const x of [
    {
      name: "short static",
      method: "GET",
      path: "/user",
    },
    {
      name: "static with same radix",
      method: "GET",
      path: "/user/comments",
    },
    {
      name: "dynamic route",
      method: "GET",
      path: "/user/lookup/username/hey",
    },
    {
      name: "mixed static dynamic",
      method: "GET",
      path: "/event/abcd1234/comments",
    },
    {
      name: "post",
      method: "POST",
      path: "/event/abcd1234/comment",
    },
    {
      name: "long static",
      method: "GET",
      path: "/very/deeply/nested/route/hello/there",
    },
    {
      name: "wildcard",
      method: "GET",
      path: "/static/index.html",
    },
  ]
) {
  Deno.bench(x.name, async () => {
    await handler(
      new Request(`http://localhost:8000${x.path}`, { method: x.method }),
    );
  });
}

Results

cpu: Apple M2
runtime: deno 1.45.5+696d528 (aarch64-apple-darwin)

file:///Users/asher/GitHub/std/bench.ts
benchmark                   time (avg)        iter/s             (min … max)       p75       p99      p995
---------------------------------------------------------------------------- -----------------------------
short static                 2.38 µs/iter     419,777.4     (2.26 µs … 2.95 µs) 2.38 µs 2.95 µs 2.95 µs
static with same radix       2.86 µs/iter     349,942.4     (2.77 µs … 3.06 µs) 2.88 µs 3.06 µs 3.06 µs
dynamic route                5.94 µs/iter     168,443.5     (5.85 µs … 6.15 µs) 5.99 µs 6.15 µs 6.15 µs
mixed static dynamic         4.54 µs/iter     220,296.4     (4.46 µs … 4.69 µs) 4.57 µs 4.69 µs 4.69 µs
post                         5.45 µs/iter     183,421.6     (5.37 µs … 5.82 µs) 5.5 µs 5.82 µs 5.82 µs
long static                  5.69 µs/iter     175,850.3     (5.61 µs … 5.83 µs) 5.7 µs 5.83 µs 5.83 µs
wildcard                     5.34 µs/iter     187,382.4     (5.29 µs … 5.46 µs) 5.35 µs 5.46 µs 5.46 µs

@github-actions github-actions bot removed the cli label Aug 6, 2024
Copy link

codecov bot commented Aug 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.85%. Comparing base (d99e1f8) to head (e9836b2).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5644      +/-   ##
==========================================
- Coverage   96.44%   95.85%   -0.60%     
==========================================
  Files         468      470       +2     
  Lines       38220    38198      -22     
  Branches     5542     5511      -31     
==========================================
- Hits        36863    36614     -249     
- Misses       1315     1542     +227     
  Partials       42       42              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@iuioiua iuioiua marked this pull request as ready for review August 7, 2024 07:12
@iuioiua iuioiua requested a review from kt3k as a code owner August 7, 2024 07:12
http/route.ts Outdated Show resolved Hide resolved
@iuioiua
Copy link
Contributor Author

iuioiua commented Aug 7, 2024

We now use URLPattern for all routes (and no strings). This dramatically simplifies the API and logic while still remaining performant. Benchmarks have been updated.

@kt3k kt3k changed the title feat(http): route module feat(http/unstable): route module Aug 7, 2024
Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

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

LGTM

@iuioiua iuioiua merged commit e8aa4df into main Aug 7, 2024
14 checks passed
@iuioiua iuioiua deleted the router branch August 7, 2024 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants