← Back to Pull requests

fix(transports): send the envelope as the value of a kafka record #44

Closed
unknown closed this pull request without merging on Oct 22, 2026 06:54 UTC
View on GitHub
unknown unknown commented on Oct 18, 2026 17:48 UTC
Author

PR Checklist


Please check if your PR fulfills the following requirements:

  • The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md

  • Tests for the changes have been added (for bug fixes / features)

  • Docs have been added / updated (for bug fixes / features)
  • PR Type


    What kind of change does this PR introduce?

  • Bugfix

  • Feature

  • Code style update (formatting, local variables)

  • Refactoring (no functional changes, no api changes)

  • Build related changes

  • CI related changes

  • Other... Please describe:
  • What is the current behavior?

    Issue Number: N/A

    ClientProxyTransport emits the OutboxEnvelope as data by default, and the envelope always has a key. ClientKafka's default serializer (KafkaRequestSerializer) takes any data with a key or a value as the Kafka record itself, so the envelope becomes the record, with a null value, and kafkajs sends only its key and headers. The broker acknowledges it and the relay deletes the row, but the id, topic, createdAt and payload never left the process: the consumer's @Payload() is null (the raw Kafka message on NestJS 11). On a compacted topic, a keyed message is also a tombstone, which compaction then uses to delete the key's earlier records.

    What is the new behavior?

    For a ClientKafka, recognized by subscribeToResponseOf() (a ClientKafkaProxy method no other Nest client has) so the package still never loads @nestjs/microservices, the default data is now { key: message.key, value: envelope, headers: message.headers }, the Kafka mapping the forRootAsync e2e test passes as toPacket. The consumer's @Payload() is the envelope. Only the value changes: the record already carried the message's key and headers, so partitioning is the same. Other clients still get the envelope, and a custom toPacket is used as is, through a ClientKafka too.

    The mapping is exported as toKafkaRecord, so a ClientKafka behind an emit-only facade or a test double, which the detection can't see, opts in with toPacket: toKafkaRecord. The toPacket JSDoc ties the Kafka default to the default serializer's key-or-value rule, says a ClientKafka with its own serializer gets the record as data too, gives the toPacket that keeps the envelope for one that expects it, and warns that with the default serializer data must never be the envelope or a spread of it. A custom toPacket is called as the function it was given, not with the transport as this.

    The Kafka test builds a ClientKafka from its prototype (its NestJS 11 constructor loads kafkajs, which the repo doesn't install), runs the real KafkaRequestSerializer on what it emits and asserts the topic, key, value and headers of each record (NestJS 11 and 12 in CI). Further tests cover a custom toPacket through a ClientKafka, toPacket: toKafkaRecord through a plain client, the this of a custom toPacket, and the other Nest clients, which still get the envelope itself. Without the change, the Kafka test fails with value: null for both messages (run); with it, the suite passes (run).

    Does this PR introduce a breaking change?


  • Yes

  • No
  • A ClientKafka with its own serializer now receives { key, value: envelope, headers } as data instead of the envelope, so a serializer that expects the envelope (one that encodes data whole, or reads its fields) sees the record; toPacket: (message, envelope) => ({ pattern: message.topic, data: envelope }) keeps the previous data. Through the default serializer only the record's value changes, from null to the envelope.

    Other information

    Kafka is the only Nest transport whose serializer reads plain objects as records (RMQ, NATS and MQTT check instanceof their record classes), so the other defaults are unaffected.

    The outbox docs page describes the default as client.emit(topic, envelope); through a ClientKafka it is now emit(topic, { key, value: envelope, headers }). That line lives in nestjs/docs.nestjs.com, so it is not in this PR.

    Diff: 530 lines (+397 / -133) across 6 files Reviewer: kamilmysliwiec Review duration: 5105m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 530 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.375
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #44"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.9% (MEDIUM) · Higher = larger blast radius
    47.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.9% · Safe to merge
    18.9%
    Repository