@aws-lambda-powertools/parser
Version:
The parser package for the Powertools for AWS Lambda (TypeScript) library.
132 lines (131 loc) • 4.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KafkaRecordSchema = exports.KafkaMskEventSchema = exports.KafkaSelfManagedEventSchema = void 0;
const zod_1 = require("zod");
/**
* Zod schema for a Kafka record from an Kafka event.
*/
const KafkaRecordSchema = zod_1.z.object({
topic: zod_1.z.string(),
partition: zod_1.z.number(),
offset: zod_1.z.number(),
timestamp: zod_1.z.number(),
timestampType: zod_1.z.string(),
key: zod_1.z
.string()
.transform((key) => {
return Buffer.from(key, 'base64').toString();
})
.optional(),
value: zod_1.z.string().transform((value) => {
return Buffer.from(value, 'base64').toString();
}),
headers: zod_1.z.array(zod_1.z.record(zod_1.z.string(), zod_1.z.array(zod_1.z.number()).transform((value) => {
return String.fromCharCode(...value);
}))),
});
exports.KafkaRecordSchema = KafkaRecordSchema;
const KafkaBaseEventSchema = zod_1.z.object({
bootstrapServers: zod_1.z
.string()
.transform((bootstrapServers) => bootstrapServers.split(','))
.nullish(),
records: zod_1.z.record(zod_1.z.string(), zod_1.z.array(KafkaRecordSchema).nonempty()),
});
/** Zod schema for Kafka event from Self Managed Kafka
*
* @example
* ```json
* {
* "eventSource":"SelfManagedKafka",
* "bootstrapServers":"b-2.demo-cluster-1.a1bcde.c1.kafka.us-east-1.amazonaws.com:9092,b-1.demo-cluster-1.a1bcde.c1.kafka.us-east-1.amazonaws.com:9092",
* "records":{
* "mytopic-0":[
* {
* "topic":"mytopic",
* "partition":0,
* "offset":15,
* "timestamp":1545084650987,
* "timestampType":"CREATE_TIME",
* "key":"cmVjb3JkS2V5",
* "value":"eyJrZXkiOiJ2YWx1ZSJ9",
* "headers":[
* {
* "headerKey":[
* 104,
* 101,
* 97,
* 100,
* 101,
* 114,
* 86,
* 97,
* 108,
* 117,
* 101
* ]
* }
* ]
* }
* ]
* }
* }
* ```
*
* @see {@link KafkaSelfManagedEvent | `KafkaSelfManagedEvent`}
* @see {@link https://docs.aws.amazon.com/lambda/latest/dg/with-kafka.html}
*/
const KafkaSelfManagedEventSchema = KafkaBaseEventSchema.extend({
eventSource: zod_1.z.literal('SelfManagedKafka'),
});
exports.KafkaSelfManagedEventSchema = KafkaSelfManagedEventSchema;
/**
* Zod schema for Kafka event from MSK
*
* @example
* ```json
* {
* "eventSource":"aws:kafka",
* "eventSourceArn":"arn:aws:kafka:us-east-1:0123456789019:cluster/SalesCluster/abcd1234-abcd-cafe-abab-9876543210ab-4",
* "bootstrapServers":"b-2.demo-cluster-1.a1bcde.c1.kafka.us-east-1.amazonaws.com:9092,b-1.demo-cluster-1.a1bcde.c1.kafka.us-east-1.amazonaws.com:9092",
* "records":{
* "mytopic-0":[
* {
* "topic":"mytopic",
* "partition":0,
* "offset":15,
* "timestamp":1545084650987,
* "timestampType":"CREATE_TIME",
* "key":"cmVjb3JkS2V5",
* "value":"eyJrZXkiOiJ2YWx1ZSJ9",
* "headers":[
* {
* "headerKey":[
* 104,
* 101,
* 97,
* 100,
* 101,
* 114,
* 86,
* 97,
* 108,
* 117,
* 101
* ]
* }
* ]
* }
* ]
* }
* }
* ```
*
* @see {@link KafkaMskEvent | `KafkaMskEvent`}
* @see {@link https://docs.aws.amazon.com/lambda/latest/dg/with-msk.html}
*/
const KafkaMskEventSchema = KafkaBaseEventSchema.extend({
eventSource: zod_1.z.literal('aws:kafka'),
eventSourceArn: zod_1.z.string(),
});
exports.KafkaMskEventSchema = KafkaMskEventSchema;