fix(kafka): lazily import Avro and Protobuf codecs so JSON-only consumers bundle - #5578
Open
svozza wants to merge 1 commit into
Open
fix(kafka): lazily import Avro and Protobuf codecs so JSON-only consumers bundle#5578svozza wants to merge 1 commit into
svozza wants to merge 1 commit into
Conversation
svozza
force-pushed
the
fix/5557-kafka-optional-codec-deps
branch
from
August 23, 2026 18:04
60f5a55 to
85a3d6f
Compare
…mers bundle The consumer used string-literal dynamic imports for the Avro and Protobuf deserializers. Bundlers such as esbuild statically resolve literal dynamic imports and follow their transitive `avro-js`/`protobufjs` imports, so a primitive/JSON-only handler failed to bundle unless both packages were installed, and always shipped both codecs. Load each codec through a non-literal specifier inside an async factory (`createDeserializer`) so bundlers leave it as a runtime import that resolves from `node_modules` only when the format is used. `deserialize` stays synchronous (it runs inside record getters) and no top-level `await` is introduced, keeping the CommonJS build valid. Also declare `avro-js` and `protobufjs` as optional peer dependencies, matching the existing parser peer-dependency pattern. Closes #5557
svozza
force-pushed
the
fix/5557-kafka-optional-codec-deps
branch
from
August 24, 2026 08:15
85a3d6f to
43c57c9
Compare
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
The Kafka consumer used string-literal dynamic imports for the Avro and Protobuf deserializers. Bundlers such as esbuild statically resolve literal dynamic imports and follow their transitive
avro-js/protobufjsimports, so a primitive/JSON-only handler failed to bundle unless both packages were installed — and always shipped both codecs. This change loads each codec lazily through a non-literal specifier so bundlers leave it as a runtime import that only resolves when the format is actually used.Changes
avro-jsandprotobufjsvia a non-literal specifier inside an asynccreateDeserializerfactory insrc/deserializer/avro.tsandsrc/deserializer/protobuf.ts, so bundlers don't eagerly resolve them.deserializesynchronous (it runs inside record getters) and avoid top-levelawait, so the CommonJS build remains valid.getDeserializerinsrc/consumer.tsto await the new factory.AvroDeserializerandProtobufDeserializertype aliases tosrc/types/types.ts.avro-js(^1.12.1) andprotobufjs(^8.7.2) as optional peer dependencies, matching the existing parser peer-dependency pattern; they are not auto-installed, so JSON-only consumers are unaffected.deserializefrom the factory.docs/features/kafka.md.Testing
Verified against the
npm packtarball (published form, with the optional peer deps), not just the source tree:avro-jsnorprotobufjsis installed — the exact failure from Bug: Kafka consumer fails esbuild bundling unless unused Avro and Protobuf dependencies are installed #5557.nodejs22.xLambda (via a throwaway CloudFormation stack) and invoked it with the sample MSK event:200, no error, correctly deserialized{"name":"Powertools","age":5}.avro-jsinstalled (notprotobufjs) and deserializes correctly at runtime via the lazy import; confirmed on Node 24.npm install <tarball>pulls in neither codec, confirming the optional peer dependencies don't change the install footprint for existing consumers.Issue number: closes #5557
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.