feat: configurable external middleware pipeline#1687
Open
mcarlson wants to merge 2 commits into
Open
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Adds a middleware pipeline that lets users compose request/response
transformations without forking the gateway.
Middleware are standard Hono middleware loaded from a configurable
directory and registered in conf.json order. Each middleware file
exports a factory function that receives a config object and returns
a Hono middleware.
Config schema (conf.json):
- middleware_dir: path to middleware directory (default: ./middleware)
- middleware: ordered array of { name, config?, enabled? } entries
Features:
- Array order = execution order (first is outermost)
- Per-middleware config object passed to factory
- enabled: false to disable without removing from array
- Async factory support for middleware needing initialization
- Error reporting names the failing middleware at startup and runtime
- Supports .js (CJS) and .mjs (ESM) middleware files
- CLI override via MIDDLEWARES_DIR env var
Gateway refuses to start if any middleware file is missing or its
factory throws.
Builds on the external loading concept from Portkey-AI#1563 with ordering
guarantees, per-middleware config, and error reporting.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a configurable middleware pipeline that lets users compose request/response transformations without forking the gateway. Builds on the external loading concept from #1563 with ordering guarantees, per-middleware config, and error reporting.
conf.jsonConfig schema
{ "middleware_dir": "./middleware", "middleware": [ { "name": "request-logger", "config": { "verbose": true } }, { "name": "cache-optimizer", "enabled": false } ] }Middleware authoring
Files changed
src/types/middlewareConfig.tssrc/middlewares/external.tssrc/index.tsconf.jsonmiddleware_dirandmiddlewarefields (empty by default)examples/middleware/request-logger.jssrc/types/__tests__/middlewareConfig.test.tssrc/middlewares/__tests__/external.test.tsRelationship to existing systems
External middleware wraps the hooks pipeline — it runs before guardrails see the request and after they produce the response.
Limitations (v1)
Test plan
🤖 Generated with Claude Code