Conversation
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
adenhertog
force-pushed
the
issue-263-middleware
branch
from
October 3, 2026 01:44
3ffdeb4 to
f28cf38
Compare
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…h headers Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (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.
Closes #263
Summary
Adds
withMiddleware()withincoming,handlerandoutgoingstages, and native transportheadersset by outgoing middleware. Removes the eight lifecycle emitters,withMessageReadMiddleware,MiddlewareDispatcherandTypedEmitter.Background
The only extension points were read middleware over
TransportMessageand eight synchronous emitters onBusInstance. Nothing wrapped an outgoing send or a single handler, and transports built native headers only fromMessageAttributes, so raw headers (#202) couldn't be set.Problem
Checked against master:
next()left the message in flight. The delete lived inside the final read middleware. Incoming middleware now runs arounddispatchMessageToHandlers, and core deletes or returns the message after the chain settles (when incoming middleware does not call next()inbus-instance-middleware.integration.ts).beforeDispatchwas emitted afterhandlers.map(dispatchMessageToHandler)had already started the handlers. The emitter is removed, and the handler stage wraps each invocation.MiddlewareDispatcherandTypedEmitterwere exported from the root, anddispatchMessageToHandlerwas public. The first two are deleted (index.spec.tsasserts it), anddispatchMessageToHandleris now private.Approach
bus-core/src/middleware/):Next,Middleware<TContext>(it must return aPromise),IncomingContext extends HandlerContext,HandlerInvocationContext(+handlerName),OutgoingContext(asend/publishunion with mutableattributesandheaders),BusMiddleware, andMiddlewareNextCalledTwice(names the stage).TransportSendOptions/TransportHeadersandTransportHeaderReservedare intransport/. All are exported from the root. The internalMiddlewarePipelineis built per bus inbuild().Receivermessages, including messages with no handler. Not callingnext()deletes the message. A throw returns it for retry, and catching without rethrowing marks it handled. The context is frozen, and its sends go straight to the transport.HandlerDispatchRejected. For workflows it wraps the registered handler: state load, invoke and save. The registry names that handler after the workflow (via a smallregisterWorkflowHandlerhelper, so the handler bodies weren't reindented and the diff against Errors thrown in workflows aren't visible #152's changes stays small), sohandlerNameis the workflow name: the class name, or the state's$namefordefineWorkflow. Function handlers usefn.name, or'anonymous'.send()/publish()afterprepareTransportOptions, which also stampsmessageId/sentAt(from stamp a message id and sent time on every message, and add transport endpoint names #325), so middleware sees them (tested). Its last step snapshots the context (attributes two levels deep, headers), so changes made afternext()never reach the transport, then either buffers into the outbox or callstransport.send/publish(message, attributes, { headers }). Flushing doesn't rerun it. Not callingnext()drops the message with a debug log. If a middleware throws afternext(), the message it just buffered is taken back out of the outbox, so "a throw buffers nothing" holds.Transport.assertSendOptions()is called by core in the outgoing terminal step, before buffering or sending, so a reserved header rejects thesend()/publish()itself instead of failing the outbox flush after siblings went out. RabbitMQ and SQS implement it and still check insend/publish. The in-memory queue reserves no names, so it doesn't implement it. RabbitMQ spreadsheadersinto the AMQP headers (attributes,stickyAttributes,sentAt,failedAttempts,x-deathandx-first-death-*/x-last-death-*are reserved). The integration tests show headers survive a retry and the dead letter queue on both transports. SQS writes each header as an SNS message attribute under its own name, typed as intoAttributeValue, with empty strings dropped.correlationId,messageId,sentAtand theattributes./stickyAttributes.prefixes are reserved.sentAtandmessageIdare reserved because stamp a message id and sent time on every message, and add transport endpoint names #325 writes them, so a middleware header can't overwrite them. Rebased onto stamp a message id and sent time on every message, and add transport endpoint names #325. InMemoryQueue keeps headers onraw.headers.RecordingInMemoryQueuefixture replacesafterSend/afterPublishobservers. New tests:middleware-pipeline.spec.ts(order, short-circuit, next-twice per stage, and a middleware tested as a plain function) andbus-instance-middleware.integration.ts(every acceptance criterion, plus a two-bus isolation test). The "async lifecycle listener rejects" test was deleted, since listeners no longer exist./guide/middlewareis rewritten for all three stages, with a hooks → middleware table./guide/lifecycle-hooksis deleted, with a redirect (it was a GitBook path, so it's in thegitbookgroup), and the links to it are fixed. The custom transport page, its snippet and the add-transport skill coverTransportSendOptions.MIGRATING.mdentry, aminorchangeset with a Breaking: note (bus-core, bus-rabbitmq, bus-sqs), and updates to the root and adapterCLAUDE.md.For reviewers:
x-delayon anx-delayed-messageexchange. Headers now reach the broker, butx-delayalone doesn't delay anything. The docs say so in a warning, and the headers snippet uses a genericx-tenantheader rather than show a delay that wouldn't work. Delayed delivery belongs to Delayed / deferred message delivery #258.Tests:
pnpm build,format:check,lint,test:unit(448 passed), all integration tests against the shared infra (379 passed),check:packages,check:message-types,docs:typecheck,docs:build,docs:check-redirectsanddocs:check-readmes.🤖 Generated with Claude Code