Files
plugins/packages/jwt
chrnx 4caf8809df
CI / dco (pull_request) Successful in 4s
CI / build (pull_request) Failing after 8s
CI / runtimes (bun) (pull_request) Failing after 7s
CI / runtimes (deno) (pull_request) Failing after 9s
CI / runtimes (edge) (pull_request) Failing after 7s
chore(plugins): publish to JSR alone, and drop the npm build
The second registry was costing a tsup config per package, a dual `exports`
map and a `dist/` that could go stale between a merge and a release — and it
was not buying a reader anything. `npx jsr add @green-tea/<name>` installs
under npm, yarn, pnpm and bun through `npm.jsr.io`: one line in `.npmrc`, and
the import specifier stays `@green-tea/<name>`. The `@jsr/` name only ever
appears in the lockfile.

Nothing is published yet — all three packages are absent from both registries
— so this costs no consumer anything, and it is the reversible direction:
adding npm later breaks nobody, while removing it after a publish would.

JSR is also the only one of the two that can record *where a plugin runs*, per
package, which is not cosmetic for a framework whose claim is one app on four
runtimes. `jwt` needs `node:fs/promises` and cannot serve workerd; `metrics`
imports nothing and runs everywhere. npm has nowhere to put that.

`private: true` stays and becomes the guard: an accidental `npm publish` now
fails rather than shipping entry points that describe a build nobody runs.
`test/workspace.test.ts` pins that invariant instead of the dual-registry one.

The CI job keeps the name `build` deliberately — renaming it would change a
required status check on the protected branches, which is a separate change.
`deno publish --dry-run` in the `runtimes` job now stands in for the build, and
it is the publish path these packages actually take.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-09-23 10:13:39 -05:00
..

@green-tea/jwt

Loads JWT signing keys at boot and publishes them into the graph as one registry. A handler asks for it with @needs('jwt') and signs or verifies by key name.

Runtimes

Runtime Supported Why not
Node ✅
Deno ✅
Bun ✅
workerd (edge) ❌ Reads the signing key from a file path, and workerd has no filesystem.

Usage

import { createApp } from '@green-tea/core';
import { jwt } from '@green-tea/jwt';

const app = createApp({
  modules: [AuthModule],
  plugins: [
    jwt({
      keys: {
        access: { path: '/etc/keys/access.json', settings: { issuer: 'api', maxAge: '15m' } },
        refresh: { path: '/etc/keys/refresh.json', settings: { maxAge: '7d' } },
      },
    }),
  ],
});

Signing belongs in the handler — it is the response to that one request:

@Post('/signin')
async signIn(@needs('jwt') jwt: JwtRegistry) {
  return { token: await jwt.get('access').sign({ sub: user.id }) };
}

Verifying does not. It is a precondition shared by every protected route, and its result is what those routes are actually asking for, so it belongs in a @Step:

@Step({ provides: 'user', needs: ['jwt', 'req'] })
class Authenticate {
  async run(ctx) {
    const header = ctx.req.headers.authorization ?? '';
    if (!header.startsWith('Bearer ')) throw new Unauthorized('missing bearer token');

    // verify() throws on a bad signature, a wrong issuer or an expired token, which cuts the
    // request before the handler — the handler only ever sees a token that was good.
    return { user: await ctx.jwt.get('access').verify(header.slice(7)) };
  }
}

@Get('/me')
me(@needs('user') user: Claims) {
  return user;
}

The handler asks for user, never for jwt, and green-tea runs Authenticate only for the routes that do — the route table says which ones, rather than a matcher on a middleware chain.

Keys are read once, while providers boot. A missing or malformed key therefore fails app.listen() — or await app.boot() on Deno and Bun — instead of the first sign-in of the day. Call app.boot() before Deno.serve or Bun.serve; without it, the failure surfaces on every request instead.

Two instances need two graph names: jwt({ provides: 'serviceJwt', keys }) publishes under serviceJwt, and the plugin takes that name too.