Pikku middleware

From pikkujs/pikku · View interactive version

Pikku middleware is an AI agent skill from the GitHub repository pikkujs/pikku. Pikku middleware: global, HTTP/prefix, tag-scoped, per-wiring; order and priority; pikkuMiddleware; setInitialSession; bearer gates. Choosing auth is pikku-auth; scopes are pikku-permissions.

How to use this skill

Skill file

Pikku Middleware

What to reach for and why. For how to call it, run this once: pikku doc middleware pikkuMiddleware pikkuMiddlewareFactory addGlobalMiddleware addHTTPMiddleware addTagMiddleware PikkuWire telemetry. Depth: references/middleware-patterns.md (the client-side RPCInvoke caller, setting a session from an API key, request logging and audit).

Which level?

Broadest to narrowest. Pick the narrowest that covers the job.

Rules

Order: collect, then sort by priority

Step 1 collects broadest to narrowest:

global → httpGroup/* → httpGroup/prefix → wiringTags → wiringMiddleware → funcTags → funcMiddleware → function body

Step 2 sorts that whole flat list: highest → high → medium (default) → low → lowest.

Priority is the primary key across every scope, not within one. The list is flattened first and sorted once, so a priority: 'lowest' global runs _after_ an inline per-route middleware of default priority: the narrower scope does not win. Scope order survives only as the tiebreaker between equal priorities, because the sort is stable. Set priority with the config-object form of pikkuMiddleware, when a middleware must run before or after others wherever it was registered (session extraction before auth checks).

Timing a call is not middleware: the function runner records one span per invocation when a runtime injects singletonServices.tracer.

⛔ MACHINE AUTH: THE TOKEN BECOMES A SESSION. ⛔

A caller that has an identity — a sandbox, a deployed stage, a pool host, a device — is authenticated ONCE, in middleware, which calls setInitialSession. The function is then an ordinary pikkuFunc gated with scopes. It reads session. It NEVER re-derives who the caller is.

Either a function is sessionless (genuinely public) or it has a session. Anything in between — a token verified inside func, a token verified in a permissions check that returns true, an identity passed in the input schema, the same resolver memoised per request so N functions can each call it — is the anti-pattern this section exists to kill.

The middleware leaves an existing session alone, reads the bearer, resolves it, calls setInitialSession with the identity and its scopes, and always calls next(). The function declares scopes: ['machine:sandbox'] (enforced by the runner, seen by the inspector) and only narrows session, with no verification.

An unresolved token leaves the session unset and the function throws MissingSessionError: 401, for free. Declare the scope tree once with defineScope (see pikku-permissions).

It MUST be `addHTTPMiddleware`, never `addTagMiddleware`

A session set in tag middleware is invisible to the function when the call arrives over POST /rpc/:rpcName. Tag middleware runs inside runPikkuFunc, and the RPC dispatch calls it without a sessionService, so invocationWire.session is never re-read after your setInitialSession: the function sees the session the OUTER wire had, which is none. addHTTPMiddleware('*') runs on the /rpc route itself, before its handler, and that session is the one the dispatched function inherits. Tag middleware is still right for a gate that only says yes/no.

A cron is a machine identity too — set it in the task's own middleware

A scheduled task has no caller and no header, but it is still a machine principal, and without a session it cannot invoke a gated RPC or be attributed in anything it writes. Give it one the same way, in the middleware of its wireScheduler: a pikkuMiddleware that calls `setInitialSession({ userId: cron:${scheduledTask?.name}, scopes: ['machine:cron'] }) and returns next()`.

The one sessionless exception: bootstrap

An endpoint that runs BEFORE the caller has an identity — registering a new host with a shared bootstrap key, a login, a device-code request — has no session to set. That one stays pikkuSessionlessFunc and declares its gate in permissions (see pikku-permissions).

Service-to-service bearer gate

Use this when the callee needs to know only THAT the caller is trusted, not WHICH caller it is. If it needs to know which, use the session pattern above.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.