UNPKG

@aws-lambda-powertools/kafka

Version:

Utility to easily handle message deserialization and parsing of Kafka events in AWS Lambda functions

26 lines (25 loc) 996 B
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.deserialize = void 0; const avro_js_1 = __importDefault(require("avro-js")); const errors_js_1 = require("../errors.js"); /** * Deserialize an Avro message from a base64-encoded string using the provided Avro schema. * * @param data - The base64-encoded string representing the Avro binary data. * @param schema - The Avro schema as a JSON string. */ const deserialize = (data, schema) => { try { const type = avro_js_1.default.parse(schema); const buffer = Buffer.from(data, 'base64'); return type.fromBuffer(buffer); } catch (error) { throw new errors_js_1.KafkaConsumerDeserializationError(`Failed to deserialize Avro message: ${error}, message: ${data}, schema: ${schema}`); } }; exports.deserialize = deserialize;