Skip to content
Eugene Lazutkin edited this page Aug 8, 2026 · 2 revisions

Koa adapter

toKoaMiddleware(handler) adapts the bundler core to Koa. It is terminal — it never calls next — so mount it on the bundle route:

import Koa from 'koa';
import route from 'koa-route';
import compress from 'koa-compress';
import {createBundler} from 'double-meh-bundler';
import {toKoaMiddleware} from 'double-meh-bundler/koa.js';

const app = new Koa();
app.use(compress()); // the envelope's compression payoff is middleware business in Koa
app.use(route.put('/bundle', toKoaMiddleware(createBundler({isUrlAcceptable}))));

Unlike the node adapter, this one does not compress: Koa has a middleware idiom for that (koa-compress in front), and the adapter respects it.

Streamed bundles inherit that division of labour, with a catch: koa-compress uses zlib's default flush, which re-buffers a +jsonl stream into one blob — the parts still arrive, just not early. Serving streamed bundles through Koa means telling the compressor to flush:

import {constants} from 'node:zlib';

app.use(compress({gzip: {flush: constants.Z_SYNC_FLUSH}}));

That is the same trade the node adapter makes for you (see Wire format § The compression trade) — and since it is app-wide here, prefer scoping the compressor to the bundle route rather than paying the flush on every response.

Duck-typed, structurally typed

The adapter imports nothing from Koa at runtime, and its types deliberately avoid @types/koa too: the declared parameter is a structural KoaLikeContext naming exactly the members touched (req, href, method, status, body, set). The real Koa Context satisfies it by shape. Koa itself appears in this repo only as a devDependency exercising the conformance test against a real Koa app.

The 204 trap

Koa's body setter rewrites the status to 204 when assigned null — which would silently turn the bundler's bodyless 405 method guard into a 204. The adapter assigns an empty string for bodyless responses, preserving real statuses; the conformance test pins this behavior.

See also: Node & Express adapter, Bundler core.

🔍 Search the wiki

double-meh-bundler

Start

API

Protocol

Project

Clone this wiki locally