CodeVetter · Repo Unpack · Behavior traces
koajs/koa
The inspected flows cover startup, a JSON response, signed-cookie output, error handling, and streamed responses. Each follows the implementation from entry boundary through context delegation to the transport. These are source walkthroughs, not executed traces.
- Startup: new Application initializes prototypes/settings → use(fn) appends handlers → listen() calls callback() → callback() composes middleware and installs a default error listener if needed → http.createServer(...).listen(...).
- Request entry: callback's listener → createContext(req,res) → optional ctxStorage.run(ctx) → handleRequest(), which sets 404 and registers onFinished before invoking middleware.
- Middleware cascade: the first handler calls next() → the second runs → the second's continuation runs → the first's continuation runs; the compose test asserts [1,2,3,4] and a final 404 when neither sets a body.
- JSON response: middleware assigns ctx.body → response.body stores the object, defaults status to 200 unless explicit, removes stale length, and chooses JSON type → respond() stringifies and res.end() sends bytes.
- Signed-cookie output: app.keys is configured → middleware accesses ctx.cookies → the getter constructs Cookies(req,res,{keys,secure}) → cookies.set(...,{signed:true}) emits value and .sig cookies; setting 204 ends without a body.
- Error response: ctx.throw() creates and throws an HTTP error → handleRequest's catch invokes ctx.onerror → the application error event fires → unsent headers are cleared/replaced → validated status and exposed-or-generic text are sent.
- Stream response: body assignment recognizes a Node stream and registers destruction on response completion → middleware resolves → respond() selects the stream → Stream.pipeline writes to res and forwards errors to ctx.onerror.
- Return navigation: ctx.back() delegates to response.back() → Referrer/Referer is read → a URL is resolved against ctx.href → matching hosts redirect to the referrer, otherwise alt or '/' → redirect() sets encoded Location and negotiated text/HTML.