@aws-lambda-powertools/kafka
Version:
Utility to easily handle message deserialization and parsing of Kafka events in AWS Lambda functions
26 lines (25 loc) • 976 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserialize = void 0;
const primitive_js_1 = require("./primitive.js");
/**
* Deserialize a base64 encoded string into either a JSON object or plain string
*
* @param data - The base64 encoded string to deserialize
* @returns The deserialized data as either a JSON object or string
*/
const deserialize = (data) => {
const plainText = (0, primitive_js_1.deserialize)(data);
try {
// Attempt to parse the decoded data as JSON
// we assume it's a JSON but it can also be a string, we don't know
return JSON.parse(plainText);
}
catch (error) {
// If JSON parsing fails, log the error and return the decoded string
// in case we could not parse it we return the base64 decoded value
console.error(`Failed to parse JSON from base64 value: ${data}`, error);
return plainText;
}
};
exports.deserialize = deserialize;