ilusm.dev

mdw

Middleware composition - create a named middleware pipeline, push handler functions onto the chain, and run the entire chain sequentially against a request and response pair. Integrates with srv, net, and obs.

Load with: use mdw

What this module does

mdw is a minimal middleware composition layer. A middleware instance is an object with a name, a version, and a chain list of handler functions. Handlers are appended with mdwus and executed in order by mdwru, each receiving the same (req, res) pair.

Quick example

use mdw

mw = mdwne("api", "2.0.0")

# Register middleware handlers
mw = mdwus(mw, \(req, res) prn("logging: " + req.url))
mw = mdwus(mw, \(req, res)
    if req.headers.Authorization == nil:
        res.status = 401
)

# Run the chain for an incoming request
mdwru(mw, {url: "/api/data", headers: {}}, {status: 200})

Functions

Pipeline

mdwne(name, version)

Creates a new middleware pipeline with a name, optional version (default "1.0.0"), and an empty chain list.

mdwus(mw, fn)

Appends handler function fn(req, res) to the pipeline's chain. Returns the updated pipeline.

mdwru(mw, req, res)

Runs every handler in the chain sequentially, passing req and res to each. Handlers may mutate res in place.

Notes

  • Handlers run serially - there is no next() / async propagation.
  • Requires trl, txt, jsn, tim, and obs.