v0.3.2 — typed responses and body limits

Runs anywhere. A web framework.

Take a Request, return a Response. That is all it takes for the same code to run on Bun, Node.js, Deno, Cloudflare Workers and browsers.

  • 01Bun
  • 02Node.js
  • 03Deno
  • 04Workers
  • 05Browser
5Runtimes
6.9 KBGzipped size
0Dependencies
1,881Tests

JOURNEY — GET /users/42, there and back.

01RouterFind the route
02requestIdTag it with an ID
03corsCheck the origin
04authCheck who it is
05handlerAnswer
01 — 05

A request arrives

GET /users/42. A web-standard Request goes straight into Senri.

  1. 01 — 05A request arrives

    GET /users/42. A web-standard Request goes straight into Senri.

  2. 02 — 05Finding the route

    Through a branching tree of routes, straight to /users/:id. The id comes typed.

  3. 03 — 05Through the middleware

    requestId, cors, auth. Layer by layer, like peeling an onion.

  4. 04 — 05The handler answers

    Return what you want to return. An object becomes JSON.

  5. 05 — 05The response goes back

    The Response travels back the way it came. The dashboard shows where every millisecond went.

→ GET /users/42Always visible at /_senri200 OK · 3.4ms ←
REQUEST
GET /users/42
host: example.com
accept: application/json
authorization: Bearer ••••
ONION — /_senri
requestId0.1ms
cors0.2ms
auth1.1ms
handler1.6ms
RESPONSE
Waiting…

TRY IT — Try it right here.

You can edit the code. When you send an edited version, the real Senri loads and runs in your browser (about 130 KB).

Response: GET /users/42
Press Send or pick a path below

With the original code, you see what the real Senri returned at build time.

RUNTIMES — The same code on every runtime.

app.ts stays the same.

Write serve(app) and it starts on Bun.

The same serve(app) runs on Node.js.

The same serve(app) runs as is with deno run.

Write export default app and it runs on Cloudflare Workers.

Inside a Service Worker, the same app answers.

Stays the same
my-app — Visual Studio Code
src/app.tspackage.json
1
2
3
4
5
import { createApp } from "@senrijs/senri";
 
export const app = createApp();
 
app.get("/users/:id", ({ id }) => ({ id }));
Only this changes
src/index.ts
import { serve } from "@senrijs/senri/serve";
import { app } from "./app.ts";

serve(app);
src/index.ts
import { serve } from "@senrijs/senri/serve";
import { app } from "./app.ts";

serve(app);
src/main.ts
import { serve } from "@senrijs/senri/serve";
import { app } from "./app.ts";

serve(app);
src/index.ts
import { app } from "./app.ts";

export default app;
src/sw.ts
import { app } from "./app.ts";

self.addEventListener("fetch", (e) =>
  e.respondWith(app.fetch(e.request)));
zsh
$ bun src/index.ts
senr/ ready http://localhost:3927
 
$ curl localhost:3927/users/42
{"id":"42"}
zsh
$ node src/index.ts
senr/ ready http://localhost:3927
 
$ curl localhost:3927/users/42
{"id":"42"}
zsh
$ deno run --allow-net --allow-env=PORT,NODE_ENV src/main.ts
senr/ ready http://localhost:3927
 
$ curl localhost:3927/users/42
{"id":"42"}
zsh
$ wrangler dev
Ready on http://localhost:8787
 
$ curl localhost:8787/users/42
{"id":"42"}
Console — localhost
> navigator.serviceWorker.register("/sw.js")
 
> await fetch("/users/42").then((r) => r.json())
{id: "42"}

Start it. See it.

Your routes, then every request as it arrives. Mistype a path and it asks: did you mean?

From the screen to the runtime.

01 — 04The runtime at the base

Bun, Node.js, Deno, Workers and browsers all speak the web-standard Request and Response.

02 — 04Senri on top

Senri is a layer that takes a Request and returns a Response. It absorbs the differences between runtimes.

03 — 04Middleware in a row

Through requestId, cors and auth in order, and finally the handler answers.

04 — 04Ippo, someday

On top of that, Ippo, a meta-framework for React and Vue, is planned.

Many branches, one straight path.

01 — 05A request comes in

GET /users/42 has arrived. Now to find which route it is.

02 — 05From the root

Routes are kept as a tree. They are not compared one by one.

03 — 05Into /users

The first segment, users, narrows it to one branch.

04 — 05Reaching /users/:id

The rest, 42, fits :id. It is a GET, so this is the route.

05 — 05A typed id

The handler gets { id: "42" }, and id has a TypeScript type.

FEATURES — Only what you need.

PATTERNS

1 of 4
01

Return it, get JSON

Return an object and it becomes a JSON response.

app.get("/todos", () => [
  { id: 1, title: "牛乳を買う" },
]);

TIMELINE — Step by step, so far.

  1. ippo
    planned: ippoIppo
    A meta-framework for React and Vue, running on Senri.
    Planned
  2. 0.3.1
    HEAD -> maintag: v0.3.1Types fixed for JSR
    WebSocket types now suit JSR, and the create-senri templates use 0.3.
  3. 0.3.0
    tag: v0.3.0Body limits and cleaner types
    Bodies are limited to 1 MiB by default. The client types responses by status.
  4. 0.2.23
    tag: v0.2.23OpenTelemetry and Prometheus
    Traces and metrics are now available.
  5. 0.2.22
    tag: v0.2.22Typed client and OpenAPI
    JWT, CSRF, WebSocket and more: what production needs.
  6. site
    docssenri.skyia.jp is live
    Docs in Japanese and English.
  7. 0.2.18
    tag: v0.2.18Multipart
    Read big files bit by bit.
  8. 0.2.0
    tag: v0.2.0Into the browser
    It now runs in a Service Worker too.
  9. 0.1.0
    tag: v0.1.0First release
    Published on npm and JSR.
All releases →

Get started.

A journey of a thousand miles begins with a single step.