@aws-lambda-powertools/kafka 0.0.0 → 2.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +14 -0
- package/README.md +299 -2
- package/lib/cjs/constants.d.ts +10 -0
- package/lib/cjs/constants.d.ts.map +1 -0
- package/lib/cjs/constants.js +12 -0
- package/lib/cjs/consumer.d.ts +42 -0
- package/lib/cjs/consumer.d.ts.map +1 -0
- package/lib/cjs/consumer.js +250 -0
- package/lib/cjs/deserializer/avro.d.ts +9 -0
- package/lib/cjs/deserializer/avro.d.ts.map +1 -0
- package/lib/cjs/deserializer/avro.js +25 -0
- package/lib/cjs/deserializer/json.d.ts +9 -0
- package/lib/cjs/deserializer/json.d.ts.map +1 -0
- package/lib/cjs/deserializer/json.js +25 -0
- package/lib/cjs/deserializer/primitive.d.ts +10 -0
- package/lib/cjs/deserializer/primitive.d.ts.map +1 -0
- package/lib/cjs/deserializer/primitive.js +16 -0
- package/lib/cjs/deserializer/protobuf.d.ts +12 -0
- package/lib/cjs/deserializer/protobuf.d.ts.map +1 -0
- package/lib/cjs/deserializer/protobuf.js +80 -0
- package/lib/cjs/errors.d.ts +27 -0
- package/lib/cjs/errors.d.ts.map +1 -0
- package/lib/cjs/errors.js +44 -0
- package/lib/cjs/index.d.ts +3 -0
- package/lib/cjs/index.d.ts.map +1 -0
- package/lib/cjs/index.js +7 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/types/index.d.ts +2 -0
- package/lib/cjs/types/index.d.ts.map +1 -0
- package/lib/cjs/types/index.js +2 -0
- package/lib/cjs/types/types.d.ts +244 -0
- package/lib/cjs/types/types.d.ts.map +1 -0
- package/lib/cjs/types/types.js +2 -0
- package/lib/esm/constants.d.ts +10 -0
- package/lib/esm/constants.d.ts.map +1 -0
- package/lib/esm/constants.js +9 -0
- package/lib/esm/consumer.d.ts +42 -0
- package/lib/esm/consumer.d.ts.map +1 -0
- package/lib/esm/consumer.js +214 -0
- package/lib/esm/deserializer/avro.d.ts +9 -0
- package/lib/esm/deserializer/avro.d.ts.map +1 -0
- package/lib/esm/deserializer/avro.js +19 -0
- package/lib/esm/deserializer/json.d.ts +9 -0
- package/lib/esm/deserializer/json.d.ts.map +1 -0
- package/lib/esm/deserializer/json.js +22 -0
- package/lib/esm/deserializer/primitive.d.ts +10 -0
- package/lib/esm/deserializer/primitive.d.ts.map +1 -0
- package/lib/esm/deserializer/primitive.js +13 -0
- package/lib/esm/deserializer/protobuf.d.ts +12 -0
- package/lib/esm/deserializer/protobuf.d.ts.map +1 -0
- package/lib/esm/deserializer/protobuf.js +77 -0
- package/lib/esm/errors.d.ts +27 -0
- package/lib/esm/errors.d.ts.map +1 -0
- package/lib/esm/errors.js +38 -0
- package/lib/esm/index.d.ts +3 -0
- package/lib/esm/index.d.ts.map +1 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/types/index.d.ts +2 -0
- package/lib/esm/types/index.d.ts.map +1 -0
- package/lib/esm/types/index.js +1 -0
- package/lib/esm/types/types.d.ts +244 -0
- package/lib/esm/types/types.d.ts.map +1 -0
- package/lib/esm/types/types.js +1 -0
- package/package.json +77 -19
- package/lib/index.d.ts +0 -0
- package/lib/index.js +0 -3
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deserialize a base64 encoded string into either a JSON object or plain string
|
|
3
|
+
*
|
|
4
|
+
* @param data - The base64 encoded string to deserialize
|
|
5
|
+
* @returns The deserialized data as either a JSON object or string
|
|
6
|
+
*/
|
|
7
|
+
declare const deserialize: (data: string) => any;
|
|
8
|
+
export { deserialize };
|
|
9
|
+
//# sourceMappingURL=json.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../../src/deserializer/json.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,QAAA,MAAM,WAAW,GAAI,MAAM,MAAM,QAYhC,CAAC;AAEF,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deserialize = void 0;
|
|
4
|
+
const primitive_js_1 = require("./primitive.js");
|
|
5
|
+
/**
|
|
6
|
+
* Deserialize a base64 encoded string into either a JSON object or plain string
|
|
7
|
+
*
|
|
8
|
+
* @param data - The base64 encoded string to deserialize
|
|
9
|
+
* @returns The deserialized data as either a JSON object or string
|
|
10
|
+
*/
|
|
11
|
+
const deserialize = (data) => {
|
|
12
|
+
const plainText = (0, primitive_js_1.deserialize)(data);
|
|
13
|
+
try {
|
|
14
|
+
// Attempt to parse the decoded data as JSON
|
|
15
|
+
// we assume it's a JSON but it can also be a string, we don't know
|
|
16
|
+
return JSON.parse(plainText);
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
// If JSON parsing fails, log the error and return the decoded string
|
|
20
|
+
// in case we could not parse it we return the base64 decoded value
|
|
21
|
+
console.error(`Failed to parse JSON from base64 value: ${data}`, error);
|
|
22
|
+
return plainText;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
exports.deserialize = deserialize;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deserialize a base64-encoded primitive value (string).
|
|
3
|
+
*
|
|
4
|
+
* When customers don't provide a schema configuration, we assume the value is a base64-encoded string.
|
|
5
|
+
*
|
|
6
|
+
* @param data - The base64-encoded string to deserialize.
|
|
7
|
+
*/
|
|
8
|
+
declare const deserialize: (data: string) => string;
|
|
9
|
+
export { deserialize };
|
|
10
|
+
//# sourceMappingURL=primitive.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"primitive.d.ts","sourceRoot":"","sources":["../../../src/deserializer/primitive.ts"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,QAAA,MAAM,WAAW,GAAI,MAAM,MAAM,WAEhC,CAAC;AAEF,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deserialize = void 0;
|
|
4
|
+
const base64_1 = require("@aws-lambda-powertools/commons/utils/base64");
|
|
5
|
+
const decoder = new TextDecoder('utf-8');
|
|
6
|
+
/**
|
|
7
|
+
* Deserialize a base64-encoded primitive value (string).
|
|
8
|
+
*
|
|
9
|
+
* When customers don't provide a schema configuration, we assume the value is a base64-encoded string.
|
|
10
|
+
*
|
|
11
|
+
* @param data - The base64-encoded string to deserialize.
|
|
12
|
+
*/
|
|
13
|
+
const deserialize = (data) => {
|
|
14
|
+
return decoder.decode((0, base64_1.fromBase64)(data, 'base64'));
|
|
15
|
+
};
|
|
16
|
+
exports.deserialize = deserialize;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ProtobufMessage, SchemaMetadata } from '../types/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Deserialize a Protobuf message from a base64-encoded string.
|
|
4
|
+
*
|
|
5
|
+
* @template T - The type of the deserialized message object.
|
|
6
|
+
*
|
|
7
|
+
* @param data - The base64-encoded string representing the Protobuf binary data.
|
|
8
|
+
* @param messageType - The Protobuf message type definition - see {@link Message | `Message`} from {@link https://www.npmjs.com/package/protobufjs | `protobufjs`}.
|
|
9
|
+
*/
|
|
10
|
+
declare const deserialize: <T>(data: string, messageType: ProtobufMessage<T>, schemaMetadata: SchemaMetadata) => T;
|
|
11
|
+
export { deserialize };
|
|
12
|
+
//# sourceMappingURL=protobuf.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protobuf.d.ts","sourceRoot":"","sources":["../../../src/deserializer/protobuf.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAOzE;;;;;;;GAOG;AACH,QAAA,MAAM,WAAW,GAAI,CAAC,EACpB,MAAM,MAAM,EACZ,aAAa,eAAe,CAAC,CAAC,CAAC,EAC/B,gBAAgB,cAAc,KAC7B,CAiDF,CAAC;AAuBF,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deserialize = void 0;
|
|
4
|
+
const protobufjs_1 = require("protobufjs");
|
|
5
|
+
const errors_js_1 = require("../errors.js");
|
|
6
|
+
/**
|
|
7
|
+
* Default order of varint types used in Protobuf to attempt deserializing Confluent Schema Registry messages.
|
|
8
|
+
*/
|
|
9
|
+
const varintOrder = ['int32', 'sint32'];
|
|
10
|
+
/**
|
|
11
|
+
* Deserialize a Protobuf message from a base64-encoded string.
|
|
12
|
+
*
|
|
13
|
+
* @template T - The type of the deserialized message object.
|
|
14
|
+
*
|
|
15
|
+
* @param data - The base64-encoded string representing the Protobuf binary data.
|
|
16
|
+
* @param messageType - The Protobuf message type definition - see {@link Message | `Message`} from {@link https://www.npmjs.com/package/protobufjs | `protobufjs`}.
|
|
17
|
+
*/
|
|
18
|
+
const deserialize = (data, messageType, schemaMetadata) => {
|
|
19
|
+
const buffer = Buffer.from(data, 'base64');
|
|
20
|
+
try {
|
|
21
|
+
if (schemaMetadata.schemaId === undefined) {
|
|
22
|
+
return messageType.decode(buffer, buffer.length);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* If `schemaId` is longer than 10 chars, it's an UUID, otherwise it's a numeric ID.
|
|
26
|
+
*
|
|
27
|
+
* When this is the case, we know the schema is coming from Glue Schema Registry,
|
|
28
|
+
* and the first byte of the buffer is a magic byte that we need to remove before
|
|
29
|
+
* decoding the message.
|
|
30
|
+
*/
|
|
31
|
+
if (schemaMetadata.schemaId.length > 10) {
|
|
32
|
+
// remove the first byte from the buffer
|
|
33
|
+
const reader = new protobufjs_1.BufferReader(buffer);
|
|
34
|
+
reader.uint32();
|
|
35
|
+
return messageType.decode(reader);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
throw new errors_js_1.KafkaConsumerDeserializationError(`Failed to deserialize Protobuf message: ${error}, message: ${data}, messageType: ${messageType}`);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* If schemaId is numeric, inferred from its length, we know it's coming from Confluent Schema Registry,
|
|
43
|
+
* so we need to remove the MessageIndex bytes.
|
|
44
|
+
* We don't know the type of the index, so we try both `int32` and `sint32`. If both fail, we throw an error.
|
|
45
|
+
*/
|
|
46
|
+
try {
|
|
47
|
+
const newBuffer = clipConfluentSchemaRegistryBuffer(buffer, varintOrder[0]);
|
|
48
|
+
return messageType.decode(newBuffer);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
try {
|
|
52
|
+
const newBuffer = clipConfluentSchemaRegistryBuffer(buffer, varintOrder[1]);
|
|
53
|
+
const decoded = messageType.decode(newBuffer);
|
|
54
|
+
// swap varint order if the first attempt failed so we can use the correct one for subsequent messages
|
|
55
|
+
varintOrder.reverse();
|
|
56
|
+
return decoded;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
throw new errors_js_1.KafkaConsumerDeserializationError(`Failed to deserialize Protobuf message: ${error}, message: ${data}, messageType: ${messageType}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
exports.deserialize = deserialize;
|
|
64
|
+
/**
|
|
65
|
+
* Clip the Confluent Schema Registry buffer to remove the index bytes.
|
|
66
|
+
*
|
|
67
|
+
* @param buffer - The buffer to clip.
|
|
68
|
+
* @param intType - The type of the integer to read from the buffer, either 'int32' or 'sint32'.
|
|
69
|
+
*/
|
|
70
|
+
const clipConfluentSchemaRegistryBuffer = (buffer, intType) => {
|
|
71
|
+
const reader = new protobufjs_1.BufferReader(buffer);
|
|
72
|
+
/**
|
|
73
|
+
* Read the first varint byte to get the index count or 0.
|
|
74
|
+
* Doing so, also advances the reader position to the next byte after the index count.
|
|
75
|
+
*/
|
|
76
|
+
const indexCount = intType === 'int32' ? reader.int32() : reader.sint32();
|
|
77
|
+
// Skip the index bytes
|
|
78
|
+
reader.skip(indexCount);
|
|
79
|
+
return reader;
|
|
80
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base error class for Kafka consumer-related errors.
|
|
3
|
+
* All Kafka consumer errors should extend this class.
|
|
4
|
+
*/
|
|
5
|
+
declare class KafkaConsumerError extends Error {
|
|
6
|
+
constructor(message: string, options?: ErrorOptions);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Error thrown when deserialization of a Kafka message fails.
|
|
10
|
+
*/
|
|
11
|
+
declare class KafkaConsumerDeserializationError extends KafkaConsumerError {
|
|
12
|
+
constructor(message: string, options?: ErrorOptions);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Error thrown when a required Avro schema is missing during Kafka message consumption.
|
|
16
|
+
*/
|
|
17
|
+
declare class KafkaConsumerMissingSchemaError extends KafkaConsumerError {
|
|
18
|
+
constructor(message: string, options?: ErrorOptions);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Error thrown when parsing a Kafka message fails.
|
|
22
|
+
*/
|
|
23
|
+
declare class KafkaConsumerParserError extends KafkaConsumerError {
|
|
24
|
+
constructor(message: string, options?: ErrorOptions);
|
|
25
|
+
}
|
|
26
|
+
export { KafkaConsumerError, KafkaConsumerMissingSchemaError, KafkaConsumerDeserializationError, KafkaConsumerParserError, };
|
|
27
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,cAAM,kBAAmB,SAAQ,KAAK;gBACxB,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIpD;AAED;;GAEG;AACH,cAAM,iCAAkC,SAAQ,kBAAkB;gBACpD,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIpD;AAED;;GAEG;AACH,cAAM,+BAAgC,SAAQ,kBAAkB;gBAClD,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIpD;AAED;;GAEG;AACH,cAAM,wBAAyB,SAAQ,kBAAkB;gBAC3C,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIpD;AAED,OAAO,EACL,kBAAkB,EAClB,+BAA+B,EAC/B,iCAAiC,EACjC,wBAAwB,GACzB,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KafkaConsumerParserError = exports.KafkaConsumerDeserializationError = exports.KafkaConsumerMissingSchemaError = exports.KafkaConsumerError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Base error class for Kafka consumer-related errors.
|
|
6
|
+
* All Kafka consumer errors should extend this class.
|
|
7
|
+
*/
|
|
8
|
+
class KafkaConsumerError extends Error {
|
|
9
|
+
constructor(message, options) {
|
|
10
|
+
super(message, options);
|
|
11
|
+
this.name = 'KafkaConsumerError';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.KafkaConsumerError = KafkaConsumerError;
|
|
15
|
+
/**
|
|
16
|
+
* Error thrown when deserialization of a Kafka message fails.
|
|
17
|
+
*/
|
|
18
|
+
class KafkaConsumerDeserializationError extends KafkaConsumerError {
|
|
19
|
+
constructor(message, options) {
|
|
20
|
+
super(message, options);
|
|
21
|
+
this.name = 'KafkaConsumerDeserializationError';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.KafkaConsumerDeserializationError = KafkaConsumerDeserializationError;
|
|
25
|
+
/**
|
|
26
|
+
* Error thrown when a required Avro schema is missing during Kafka message consumption.
|
|
27
|
+
*/
|
|
28
|
+
class KafkaConsumerMissingSchemaError extends KafkaConsumerError {
|
|
29
|
+
constructor(message, options) {
|
|
30
|
+
super(message, options);
|
|
31
|
+
this.name = 'KafkaConsumerMissingSchemaError';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.KafkaConsumerMissingSchemaError = KafkaConsumerMissingSchemaError;
|
|
35
|
+
/**
|
|
36
|
+
* Error thrown when parsing a Kafka message fails.
|
|
37
|
+
*/
|
|
38
|
+
class KafkaConsumerParserError extends KafkaConsumerError {
|
|
39
|
+
constructor(message, options) {
|
|
40
|
+
super(message, options);
|
|
41
|
+
this.name = 'KafkaConsumerParserError';
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.KafkaConsumerParserError = KafkaConsumerParserError;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC"}
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.kafkaConsumer = exports.SchemaType = void 0;
|
|
4
|
+
var constants_js_1 = require("./constants.js");
|
|
5
|
+
Object.defineProperty(exports, "SchemaType", { enumerable: true, get: function () { return constants_js_1.SchemaType; } });
|
|
6
|
+
var consumer_js_1 = require("./consumer.js");
|
|
7
|
+
Object.defineProperty(exports, "kafkaConsumer", { enumerable: true, get: function () { return consumer_js_1.kafkaConsumer; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "commonjs" }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,cAAc,EACd,eAAe,EACf,QAAQ,EACR,eAAe,EACf,MAAM,EACN,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,iBAAiB,EACjB,cAAc,GACf,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import type { Reader } from 'protobufjs';
|
|
3
|
+
import type { SchemaType as SchemaTypeMap } from '../constants.js';
|
|
4
|
+
/**
|
|
5
|
+
* Union type for supported schema types (JSON, Avro, Protobuf).
|
|
6
|
+
*/
|
|
7
|
+
type SchemaType = (typeof SchemaTypeMap)[keyof typeof SchemaTypeMap];
|
|
8
|
+
/**
|
|
9
|
+
* Metadata about the schema used for the field.
|
|
10
|
+
*/
|
|
11
|
+
type SchemaMetadata = {
|
|
12
|
+
/**
|
|
13
|
+
* The schema type of the value (JSON, AVRO, PROTOBUF)
|
|
14
|
+
*/
|
|
15
|
+
dataFormat: string;
|
|
16
|
+
/**
|
|
17
|
+
* The schema identifier
|
|
18
|
+
*/
|
|
19
|
+
schemaId?: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Represents a single Kafka consumer record with generic key and value types.
|
|
23
|
+
*/
|
|
24
|
+
type ConsumerRecord<K, V> = {
|
|
25
|
+
/**
|
|
26
|
+
* The deserialized key of the record
|
|
27
|
+
*/
|
|
28
|
+
key: K;
|
|
29
|
+
/**
|
|
30
|
+
* The deserialized value of the record
|
|
31
|
+
*/
|
|
32
|
+
value: V;
|
|
33
|
+
/**
|
|
34
|
+
* The original (raw, encoded) key as received from Kafka, or `undefined` if not present
|
|
35
|
+
*/
|
|
36
|
+
originalKey?: string;
|
|
37
|
+
/**
|
|
38
|
+
* The original (raw, encoded) value as received from Kafka
|
|
39
|
+
*/
|
|
40
|
+
originalValue: string;
|
|
41
|
+
/**
|
|
42
|
+
* Optional array of headers as key-value string pairs, or `null/`undefined` if not present
|
|
43
|
+
*/
|
|
44
|
+
headers?: {
|
|
45
|
+
[k: string]: string;
|
|
46
|
+
}[] | null;
|
|
47
|
+
/**
|
|
48
|
+
* Optional array of original record headers
|
|
49
|
+
*/
|
|
50
|
+
originalHeaders?: RecordHeader[] | null;
|
|
51
|
+
/**
|
|
52
|
+
* The topic from which the record was consumed
|
|
53
|
+
*/
|
|
54
|
+
topic: string;
|
|
55
|
+
/**
|
|
56
|
+
* The partition from which the record was consumed
|
|
57
|
+
*/
|
|
58
|
+
partition: number;
|
|
59
|
+
/**
|
|
60
|
+
* The offset of the record within the partition
|
|
61
|
+
*/
|
|
62
|
+
offset: number;
|
|
63
|
+
/**
|
|
64
|
+
* The timestamp of the record
|
|
65
|
+
*/
|
|
66
|
+
timestamp: number;
|
|
67
|
+
/**
|
|
68
|
+
* The type of timestamp (CREATE_TIME or LOG_APPEND_TIME)
|
|
69
|
+
*/
|
|
70
|
+
timestampType: 'CREATE_TIME' | 'LOG_APPEND_TIME';
|
|
71
|
+
/**
|
|
72
|
+
* Metadata about the value schema
|
|
73
|
+
*/
|
|
74
|
+
valueSchemaMetadata: SchemaMetadata;
|
|
75
|
+
/**
|
|
76
|
+
* Metadata about the key schema
|
|
77
|
+
*/
|
|
78
|
+
keySchemaMetadata: SchemaMetadata;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Represents a collection of Kafka consumer records, along with MSK event metadata.
|
|
82
|
+
*/
|
|
83
|
+
type ConsumerRecords<K = unknown, V = unknown> = {
|
|
84
|
+
/**
|
|
85
|
+
* Array of consumer records
|
|
86
|
+
*/
|
|
87
|
+
records: Array<ConsumerRecord<K, V>>;
|
|
88
|
+
} & Omit<MSKEvent, 'records'>;
|
|
89
|
+
/**
|
|
90
|
+
* Union type for supported schema configurations (JSON, Avro, Protobuf).
|
|
91
|
+
*/
|
|
92
|
+
type SchemaConfigValue = JsonConfig | AvroConfig | ProtobufConfig<unknown>;
|
|
93
|
+
/**
|
|
94
|
+
* Configuration for JSON schema validation.
|
|
95
|
+
*/
|
|
96
|
+
type JsonConfig = {
|
|
97
|
+
/**
|
|
98
|
+
* Indicates the schema type is JSON
|
|
99
|
+
*/
|
|
100
|
+
type: typeof SchemaTypeMap.JSON;
|
|
101
|
+
/**
|
|
102
|
+
* Optional {@link https://github.com/standard-schema/standard-schema | Standard Schema} for runtime validation
|
|
103
|
+
*/
|
|
104
|
+
parserSchema?: StandardSchemaV1;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Configuration for Avro schema validation.
|
|
108
|
+
*/
|
|
109
|
+
type AvroConfig = {
|
|
110
|
+
/**
|
|
111
|
+
* Indicates the schema type is Avro
|
|
112
|
+
*/
|
|
113
|
+
type: typeof SchemaTypeMap.AVRO;
|
|
114
|
+
/**
|
|
115
|
+
* Avro schema definition as a string
|
|
116
|
+
*/
|
|
117
|
+
schema: string;
|
|
118
|
+
/**
|
|
119
|
+
* Optional {@link https://github.com/standard-schema/standard-schema | Standard Schema} for runtime validation
|
|
120
|
+
*/
|
|
121
|
+
parserSchema?: StandardSchemaV1;
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Configuration for Protobuf schema validation.
|
|
125
|
+
*/
|
|
126
|
+
type ProtobufConfig<T> = {
|
|
127
|
+
/**
|
|
128
|
+
* Indicates the schema type is Protobuf
|
|
129
|
+
*/
|
|
130
|
+
type: typeof SchemaTypeMap.PROTOBUF;
|
|
131
|
+
/**
|
|
132
|
+
* Protobuf message type for decoding
|
|
133
|
+
*/
|
|
134
|
+
schema: ProtobufMessage<T>;
|
|
135
|
+
/**
|
|
136
|
+
* Optional {@link https://github.com/standard-schema/standard-schema | Standard Schema} for runtime validation
|
|
137
|
+
*/
|
|
138
|
+
parserSchema?: StandardSchemaV1;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Configuration for key and value schema validation.
|
|
142
|
+
*/
|
|
143
|
+
type SchemaConfig = {
|
|
144
|
+
/**
|
|
145
|
+
* Schema type for the value.
|
|
146
|
+
* If not provided, the value will not be validated.
|
|
147
|
+
*/
|
|
148
|
+
value: SchemaConfigValue;
|
|
149
|
+
/**
|
|
150
|
+
* Schema type for the key.
|
|
151
|
+
* If not provided, the key will not be validated.
|
|
152
|
+
*/
|
|
153
|
+
key?: SchemaConfigValue;
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Represents a Kafka record header as a mapping of header key to byte array.
|
|
157
|
+
*/
|
|
158
|
+
type RecordHeader = {
|
|
159
|
+
/**
|
|
160
|
+
* Header key mapped to its value as an array of bytes
|
|
161
|
+
*/
|
|
162
|
+
[headerKey: string]: number[];
|
|
163
|
+
};
|
|
164
|
+
/**
|
|
165
|
+
* Represents a single Kafka record as received from MSK.
|
|
166
|
+
*/
|
|
167
|
+
type Record = {
|
|
168
|
+
/**
|
|
169
|
+
* Kafka topic name
|
|
170
|
+
*/
|
|
171
|
+
topic: string;
|
|
172
|
+
/**
|
|
173
|
+
* Partition number within the topic
|
|
174
|
+
*/
|
|
175
|
+
partition: number;
|
|
176
|
+
/**
|
|
177
|
+
* Offset of the record within the partition
|
|
178
|
+
*/
|
|
179
|
+
offset: number;
|
|
180
|
+
/**
|
|
181
|
+
* Timestamp of the record
|
|
182
|
+
*/
|
|
183
|
+
timestamp: number;
|
|
184
|
+
/**
|
|
185
|
+
* Type of timestamp (creation or log append time)
|
|
186
|
+
*/
|
|
187
|
+
timestampType: 'CREATE_TIME' | 'LOG_APPEND_TIME';
|
|
188
|
+
/**
|
|
189
|
+
* Base64-encoded key of the record
|
|
190
|
+
*/
|
|
191
|
+
key?: string | null;
|
|
192
|
+
/**
|
|
193
|
+
* Base64-encoded value of the record
|
|
194
|
+
*/
|
|
195
|
+
value: string;
|
|
196
|
+
/**
|
|
197
|
+
* Array of record headers
|
|
198
|
+
*/
|
|
199
|
+
headers: RecordHeader[] | null;
|
|
200
|
+
/**
|
|
201
|
+
* Metadata about the value schema
|
|
202
|
+
*/
|
|
203
|
+
valueSchemaMetadata: SchemaMetadata;
|
|
204
|
+
/**
|
|
205
|
+
* Metadata about the key schema
|
|
206
|
+
*/
|
|
207
|
+
keySchemaMetadata: SchemaMetadata;
|
|
208
|
+
};
|
|
209
|
+
/**
|
|
210
|
+
* AWS Lambda event structure for MSK (Managed Streaming for Kafka).
|
|
211
|
+
* @see {@link https://docs.aws.amazon.com/lambda/latest/dg/with-msk.html | AWS Lambda with MSK}
|
|
212
|
+
*/
|
|
213
|
+
type MSKEvent = {
|
|
214
|
+
/**
|
|
215
|
+
* Event source identifier (always 'aws:kafka')
|
|
216
|
+
*/
|
|
217
|
+
eventSource: 'aws:kafka';
|
|
218
|
+
/**
|
|
219
|
+
* ARN of the Kafka event source
|
|
220
|
+
*/
|
|
221
|
+
eventSourceArn: string;
|
|
222
|
+
/**
|
|
223
|
+
* Comma-separated list of Kafka bootstrap servers
|
|
224
|
+
*/
|
|
225
|
+
bootstrapServers: string;
|
|
226
|
+
/**
|
|
227
|
+
* Mapping of topic names to arrays of records
|
|
228
|
+
*/
|
|
229
|
+
records: {
|
|
230
|
+
[topic: string]: Record[];
|
|
231
|
+
};
|
|
232
|
+
};
|
|
233
|
+
type ProtobufMessage<T = unknown> = {
|
|
234
|
+
decode(reader: Reader | Uint8Array, length?: number): T;
|
|
235
|
+
};
|
|
236
|
+
type Deserializer = (input: string, schema?: unknown, schemaMetadata?: SchemaMetadata) => unknown;
|
|
237
|
+
type DeserializeOptions = {
|
|
238
|
+
value: string;
|
|
239
|
+
deserializer: Deserializer;
|
|
240
|
+
config?: SchemaConfigValue;
|
|
241
|
+
schemaMetadata: SchemaMetadata;
|
|
242
|
+
};
|
|
243
|
+
export type { ConsumerRecord, ConsumerRecords, Deserializer, MSKEvent, ProtobufMessage, Record, RecordHeader, SchemaType, SchemaConfig, SchemaConfigValue, SchemaMetadata, DeserializeOptions, };
|
|
244
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.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AAe,EACf,MAAM,EACN,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,iBAAiB,EACjB,cAAc,EACd,kBAAkB,GACnB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,QAAA,MAAM,UAAU;;;;CAIN,CAAC;AAEX,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { AsyncHandler } from '@aws-lambda-powertools/commons/types';
|
|
2
|
+
import type { Context, Handler } from 'aws-lambda';
|
|
3
|
+
import type { ConsumerRecords, MSKEvent, SchemaConfig } from './types/types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Wrap a handler function to automatically deserialize and validate Kafka records from an MSK event.
|
|
6
|
+
*
|
|
7
|
+
* The returned function will:
|
|
8
|
+
* - Deserialize the key and value of each record using the provided schema config.
|
|
9
|
+
* - Validate the deserialized key and value using Zod schemas if provided.
|
|
10
|
+
* - Replace the `records` property in the event with an array of deserialized and validated records.
|
|
11
|
+
* - Call the original handler with the modified event and original context/arguments.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { kafkaConsumer } from '@aws-lambda-powertools/kafka';
|
|
16
|
+
* import { z } from 'zod';
|
|
17
|
+
*
|
|
18
|
+
* const keySchema = z.string();
|
|
19
|
+
* const valueSchema = z.object({
|
|
20
|
+
* id: z.number(),
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* export const handler = kafkaConsumer<z.infer<keySchema>, z.infer<valueSchema>>(async (event, context) => {
|
|
24
|
+
* // event.records is now an array of deserialized and validated records
|
|
25
|
+
* for (const record of event.records) {
|
|
26
|
+
* console.log(record.key, record.value);
|
|
27
|
+
* }
|
|
28
|
+
* }, {
|
|
29
|
+
* key: { type: 'json', parserSchema: keySchema },
|
|
30
|
+
* value: { type: 'json', parserSchema: valueSchema },
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @typeParam K - Optional type of the deserialized key - defaults to `unknown`.
|
|
35
|
+
* @typeParam V - Optional type of the deserialized value - defaults to `unknown`.
|
|
36
|
+
*
|
|
37
|
+
* @param handler - The original handler function to wrap. It should accept the deserialized event as its first argument.
|
|
38
|
+
* @param config - The schema configuration for deserializing and validating record keys and values.
|
|
39
|
+
*/
|
|
40
|
+
declare const kafkaConsumer: <K, V>(handler: AsyncHandler<Handler<ConsumerRecords<K, V>>>, config?: SchemaConfig) => ((event: MSKEvent, context: Context) => Promise<unknown>);
|
|
41
|
+
export { kafkaConsumer };
|
|
42
|
+
//# sourceMappingURL=consumer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consumer.d.ts","sourceRoot":"","sources":["../../src/consumer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sCAAsC,CAAC;AAGzE,OAAO,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AASnD,OAAO,KAAK,EAEV,eAAe,EAIf,QAAQ,EACR,YAAY,EAEb,MAAM,kBAAkB,CAAC;AAsM1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,QAAA,MAAM,aAAa,GAAI,CAAC,EAAE,CAAC,EACzB,SAAS,YAAY,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EACrD,SAAS,YAAY,KACpB,CAAC,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CAwB1D,CAAC;AAEF,OAAO,EAAE,aAAa,EAAE,CAAC"}
|