Skip to content

fix(kafka): lazily import Avro and Protobuf codecs so JSON-only consumers bundle - #5578

Open
svozza wants to merge 1 commit into
mainfrom
fix/5557-kafka-optional-codec-deps
Open

fix(kafka): lazily import Avro and Protobuf codecs so JSON-only consumers bundle#5578
svozza wants to merge 1 commit into
mainfrom
fix/5557-kafka-optional-codec-deps

Conversation

@svozza

@svozza svozza commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

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/protobufjs imports, 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

  • Load avro-js and protobufjs via a non-literal specifier inside an async createDeserializer factory in src/deserializer/avro.ts and src/deserializer/protobuf.ts, so bundlers don't eagerly resolve them.
  • Keep deserialize synchronous (it runs inside record getters) and avoid top-level await, so the CommonJS build remains valid.
  • Update getDeserializer in src/consumer.ts to await the new factory.
  • Add AvroDeserializer and ProtobufDeserializer type aliases to src/types/types.ts.
  • Declare avro-js (^1.12.1) and protobufjs (^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.
  • Update the Avro/Protobuf deserializer unit tests to obtain deserialize from the factory.
  • Document the optional peer dependencies and supported versions in docs/features/kafka.md.

Testing

Verified against the npm pack tarball (published form, with the optional peer deps), not just the source tree:

  • A JSON-only handler now bundles with esbuild when neither avro-js nor protobufjs is installed — the exact failure from Bug: Kafka consumer fails esbuild bundling unless unused Avro and Protobuf dependencies are installed #5557.
  • Deployed that bundle to a real nodejs22.x Lambda (via a throwaway CloudFormation stack) and invoked it with the sample MSK event: 200, no error, correctly deserialized {"name":"Powertools","age":5}.
  • An Avro handler bundles with only avro-js installed (not protobufjs) 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.

@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/L PRs between 100-499 LOC label Aug 23, 2026
@svozza
svozza force-pushed the fix/5557-kafka-optional-codec-deps branch from 60f5a55 to 85a3d6f Compare August 23, 2026 18:04
@svozza
svozza requested a review from dreamorosi August 23, 2026 20:33
…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
svozza force-pushed the fix/5557-kafka-optional-codec-deps branch from 85a3d6f to 43c57c9 Compare August 24, 2026 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L PRs between 100-499 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Kafka consumer fails esbuild bundling unless unused Avro and Protobuf dependencies are installed

1 participant