@aws-lambda-powertools/parser
Version:
The parser package for the Powertools for AWS Lambda (TypeScript) library.
86 lines (85 loc) • 3.15 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CloudWatchLogEventSchema = exports.CloudWatchLogsDecodeSchema = exports.CloudWatchLogsSchema = void 0;
const node_zlib_1 = require("node:zlib");
const zod_1 = require("zod");
const CloudWatchLogEventSchema = zod_1.z.object({
id: zod_1.z.string(),
timestamp: zod_1.z.number(),
message: zod_1.z.string(),
});
exports.CloudWatchLogEventSchema = CloudWatchLogEventSchema;
const CloudWatchLogsDecodeSchema = zod_1.z.object({
messageType: zod_1.z.string(),
owner: zod_1.z.string(),
logGroup: zod_1.z.string(),
logStream: zod_1.z.string(),
subscriptionFilters: zod_1.z.array(zod_1.z.string()),
logEvents: zod_1.z.array(CloudWatchLogEventSchema).nonempty(),
});
exports.CloudWatchLogsDecodeSchema = CloudWatchLogsDecodeSchema;
/**
* Zod schema for CloudWatch Logs.
*
* @example
* ```json
* {
* "awslogs": {
* "data": "H4sIAAAAAAAAAHWPwQqCQBCGX0Xm7EFtK+smZBEUgXoLCdMhFtKV3akI8d0bLYmibvPPN3wz00CJxmQnTO41whwWQRIctmEcB6sQbFC3CjW3XW8kxpOpP+OC22d1Wml1qZkQGtoMsScxaczKN3plG8zlaHIta5KqWsozoTYw3/djzwhpLwivWFGHGpAFe7DL68JlBUk+l7KSN7tCOEJ4M3/qOI49vMHj+zCKdlFqLaU2ZHV2a4Ct/an0/ivdX8oYc1UVX860fQDQiMdxRQEAAA=="
* }
* }
* ```
* The `data` field compressed JSON string, once transformed the payload will look like:
*
* @example
* ```json
* {
* "owner": "123456789012",
* "logGroup": "CloudTrail",
* "logStream": "123456789012_CloudTrail_us-east-1",
* "subscriptionFilters": [
* "Destination"
* ],
* "messageType": "DATA_MESSAGE",
* "logEvents": [
* {
* "id": "31953106606966983378809025079804211143289615424298221568",
* "timestamp": 1432826855000,
* "message": "{\"eventVersion\":\"1.03\",\"userIdentity\":{\"type\":\"Root\"}"
* },
* {
* "id": "31953106606966983378809025079804211143289615424298221569",
* "timestamp": 1432826855000,
* "message": "{\"eventVersion\":\"1.03\",\"userIdentity\":{\"type\":\"Root\"}"
* },
* {
* "id": "31953106606966983378809025079804211143289615424298221570",
* "timestamp": 1432826855000,
* "message": "{\"eventVersion\":\"1.03\",\"userIdentity\":{\"type\":\"Root\"}"
* }
* ]
* }
* ```
*
* @see {@link CloudWatchLogsEvent | `CloudWatchLogsEvent`}
* @see {@link https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/SubscriptionFilters.html#LambdaFunctionExample}
*/
const CloudWatchLogsSchema = zod_1.z.object({
awslogs: zod_1.z.object({
data: zod_1.z.base64().transform((data, ctx) => {
try {
const uncompressed = (0, node_zlib_1.gunzipSync)(Buffer.from(data, 'base64')).toString('utf8');
return CloudWatchLogsDecodeSchema.parse(JSON.parse(uncompressed));
}
catch {
ctx.addIssue({
code: 'custom',
message: 'Failed to decompress CloudWatch log data',
fatal: true,
});
return zod_1.z.NEVER;
}
}),
}),
});
exports.CloudWatchLogsSchema = CloudWatchLogsSchema;