@auriclabs/events 0.4.0 → 0.4.1
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/.turbo/turbo-build.log +54 -32
- package/CHANGELOG.md +7 -0
- package/dist/index.cjs +85 -2
- package/dist/index.d.cts +145 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +145 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +84 -1
- package/dist/index.mjs.map +1 -1
- package/dist/stream-handler.entry.cjs +86 -1
- package/dist/stream-handler.entry.d.cts +185 -1
- package/dist/stream-handler.entry.d.cts.map +1 -1
- package/dist/stream-handler.entry.d.mts +185 -1
- package/dist/stream-handler.entry.d.mts.map +1 -1
- package/dist/stream-handler.entry.mjs +85 -1
- package/dist/stream-handler.entry.mjs.map +1 -1
- package/package.json +2 -2
- package/dist/index2.d.cts +0 -148
- package/dist/index2.d.cts.map +0 -1
- package/dist/index2.d.mts +0 -148
- package/dist/index2.d.mts.map +0 -1
- package/dist/stream-handler.cjs +0 -91
- package/dist/stream-handler.mjs +0 -88
- package/dist/stream-handler.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { t as createStreamHandler } from "./stream-handler.mjs";
|
|
2
1
|
import { normalizePaginationResponse } from "@auriclabs/pagination";
|
|
3
2
|
import { ConditionalCheckFailedException, DynamoDBClient } from "@aws-sdk/client-dynamodb";
|
|
4
3
|
import { DynamoDBDocumentClient, GetCommand, QueryCommand, TransactWriteCommand } from "@aws-sdk/lib-dynamodb";
|
|
5
4
|
import { retry } from "@auriclabs/api-core";
|
|
6
5
|
import { logger } from "@auriclabs/logger";
|
|
7
6
|
import { ulid } from "ulid";
|
|
7
|
+
import { EventBridgeClient, PutEventsCommand } from "@aws-sdk/client-eventbridge";
|
|
8
|
+
import { SQSClient, SendMessageBatchCommand } from "@aws-sdk/client-sqs";
|
|
9
|
+
import { unmarshall } from "@aws-sdk/util-dynamodb";
|
|
10
|
+
import { kebabCase } from "lodash-es";
|
|
8
11
|
//#region src/event-service.ts
|
|
9
12
|
const ddb = DynamoDBDocumentClient.from(new DynamoDBClient(), { marshallOptions: { removeUndefinedValues: true } });
|
|
10
13
|
const pad = (n, w = 9) => String(n).padStart(w, "0");
|
|
@@ -221,6 +224,86 @@ const createEventListener = (eventHandlers, { debug = false } = {}) => async (sq
|
|
|
221
224
|
return response;
|
|
222
225
|
};
|
|
223
226
|
//#endregion
|
|
227
|
+
//#region src/stream-handler.ts
|
|
228
|
+
const BATCH_SIZE = 10;
|
|
229
|
+
/**
|
|
230
|
+
* Creates a Lambda handler for DynamoDB stream events.
|
|
231
|
+
* Processes INSERT events from the event store table and forwards them to SQS queues and EventBridge.
|
|
232
|
+
*/
|
|
233
|
+
function createStreamHandler(config) {
|
|
234
|
+
const sqsClient = new SQSClient();
|
|
235
|
+
const eventBridge = new EventBridgeClient({});
|
|
236
|
+
function chunkArray(array, chunkSize) {
|
|
237
|
+
const chunks = [];
|
|
238
|
+
for (let i = 0; i < array.length; i += chunkSize) chunks.push(array.slice(i, i + chunkSize));
|
|
239
|
+
return chunks;
|
|
240
|
+
}
|
|
241
|
+
async function sendToQueuesBatch(eventRecords) {
|
|
242
|
+
await Promise.all(config.queueUrls.map((queue) => sendToQueueBatch(eventRecords, queue)));
|
|
243
|
+
}
|
|
244
|
+
async function sendToQueueBatch(eventRecords, queue) {
|
|
245
|
+
const batches = chunkArray(eventRecords, BATCH_SIZE);
|
|
246
|
+
for (const batch of batches) try {
|
|
247
|
+
const entries = batch.map((eventRecord, index) => ({
|
|
248
|
+
Id: `${eventRecord.eventId}-${index}`,
|
|
249
|
+
MessageBody: JSON.stringify(eventRecord),
|
|
250
|
+
MessageGroupId: eventRecord.aggregateId,
|
|
251
|
+
MessageDeduplicationId: eventRecord.eventId
|
|
252
|
+
}));
|
|
253
|
+
await sqsClient.send(new SendMessageBatchCommand({
|
|
254
|
+
QueueUrl: queue,
|
|
255
|
+
Entries: entries
|
|
256
|
+
}));
|
|
257
|
+
} catch (error) {
|
|
258
|
+
logger.error({
|
|
259
|
+
error,
|
|
260
|
+
batch,
|
|
261
|
+
queue
|
|
262
|
+
}, "Error sending batch to queue");
|
|
263
|
+
throw error;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
async function sendToBusBatch(eventRecords) {
|
|
267
|
+
const batches = chunkArray(eventRecords, BATCH_SIZE);
|
|
268
|
+
for (const batch of batches) try {
|
|
269
|
+
const entries = batch.map((eventRecord) => {
|
|
270
|
+
return {
|
|
271
|
+
Source: eventRecord.source ?? kebabCase(eventRecord.aggregateType.split(".")[0]),
|
|
272
|
+
DetailType: eventRecord.eventType,
|
|
273
|
+
Detail: JSON.stringify(eventRecord),
|
|
274
|
+
EventBusName: config.busName
|
|
275
|
+
};
|
|
276
|
+
});
|
|
277
|
+
await eventBridge.send(new PutEventsCommand({ Entries: entries }));
|
|
278
|
+
} catch (error) {
|
|
279
|
+
logger.error({
|
|
280
|
+
error,
|
|
281
|
+
batch
|
|
282
|
+
}, "Error sending batch to bus");
|
|
283
|
+
throw error;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return async (event) => {
|
|
287
|
+
const eventRecords = event.Records.filter((record) => record.eventName === "INSERT").map((record) => {
|
|
288
|
+
try {
|
|
289
|
+
const data = record.dynamodb?.NewImage;
|
|
290
|
+
return unmarshall(data);
|
|
291
|
+
} catch (error) {
|
|
292
|
+
logger.error({
|
|
293
|
+
error,
|
|
294
|
+
record
|
|
295
|
+
}, "Error unmarshalling event record");
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
}).filter((eventRecord) => eventRecord?.itemType === "event");
|
|
299
|
+
if (eventRecords.length > 0) {
|
|
300
|
+
const tasks = [sendToQueuesBatch(eventRecords)];
|
|
301
|
+
if (config.busName) tasks.push(sendToBusBatch(eventRecords));
|
|
302
|
+
await Promise.all(tasks);
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
//#endregion
|
|
224
307
|
export { appendEventContext, createDispatch, createEventListener, createEventService, createStreamHandler, dispatchEvent, dispatchEvents, getEventContext, getEventService, initEvents, resetEventContext, setEventContext };
|
|
225
308
|
|
|
226
309
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/event-service.ts","../src/init.ts","../src/context.ts","../src/dispatch-event.ts","../src/dispatch-events.ts","../src/create-dispatch.ts","../src/create-event-listener.ts"],"sourcesContent":["import { normalizePaginationResponse, PaginationResponse } from '@auriclabs/pagination';\nimport { DynamoDBClient, ConditionalCheckFailedException } from '@aws-sdk/client-dynamodb';\nimport {\n DynamoDBDocumentClient,\n TransactWriteCommand,\n GetCommand,\n QueryCommand,\n} from '@aws-sdk/lib-dynamodb';\n\nimport type {\n EventRecord,\n AggregateHead,\n AggregatePK,\n EventId,\n AggregateId,\n AggregateType,\n EventSK,\n Source,\n} from './types';\n\nconst ddb = DynamoDBDocumentClient.from(new DynamoDBClient(), {\n marshallOptions: {\n removeUndefinedValues: true,\n },\n});\n\nconst pad = (n: number, w = 9): EventId => String(n).padStart(w, '0') as EventId;\nconst pkFor = (aggregateType: string, aggregateId: string): AggregatePK =>\n `AGG#${aggregateType}#${aggregateId}`;\n\nexport interface AppendArgs<P = unknown> {\n tenantId: string;\n aggregateType: string;\n aggregateId: string;\n source: string;\n /** Version you observed before appending (0 for brand new) */\n expectedVersion: number;\n /** Required for idempotent retries (e.g., the command id) */\n idempotencyKey: string;\n\n // Event properties (flattened)\n eventId: string; // ULID/UUID – must be stable across retries\n eventType: string;\n occurredAt?: string; // default: now ISO\n payload?: Readonly<P>;\n schemaVersion?: number; // optional but recommended\n\n // Optional metadata\n correlationId?: string;\n causationId?: string;\n actorId?: string;\n}\n\nexport interface AppendEventResult {\n pk: string;\n sk: string;\n version: number;\n}\n\nexport interface EventService {\n appendEvent<P = unknown>(args: AppendArgs<P>): Promise<AppendEventResult>;\n getHead(aggregateType: string, aggregateId: string): Promise<AggregateHead | undefined>;\n getEvent(\n aggregateType: string,\n aggregateId: string,\n version: number,\n ): Promise<EventRecord | undefined>;\n listEvents(params: {\n aggregateType: string;\n aggregateId: string;\n fromVersionExclusive?: number;\n toVersionInclusive?: number;\n limit?: number;\n }): Promise<PaginationResponse<EventRecord>>;\n}\n\nexport function createEventService(tableName: string): EventService {\n const TABLE = tableName;\n\n return {\n async appendEvent<P = unknown>(args: AppendArgs<P>): Promise<AppendEventResult> {\n const {\n tenantId,\n aggregateType,\n aggregateId,\n expectedVersion,\n idempotencyKey,\n eventId,\n eventType,\n occurredAt,\n source,\n payload,\n schemaVersion,\n correlationId,\n causationId,\n actorId,\n } = args;\n\n const pk = pkFor(aggregateType, aggregateId);\n const nextVersion = expectedVersion + 1;\n const sk = `EVT#${pad(nextVersion)}` as EventSK;\n const nowIso = new Date().toISOString();\n const eventOccurredAt = occurredAt ?? nowIso;\n\n try {\n await ddb.send(\n new TransactWriteCommand({\n TransactItems: [\n {\n Update: {\n TableName: TABLE,\n Key: { pk, sk: 'HEAD' },\n UpdateExpression:\n 'SET currentVersion = :next, lastEventId = :eid, lastIdemKey = :idem, updatedAt = :now, aggregateId = if_not_exists(aggregateId, :aid), aggregateType = if_not_exists(aggregateType, :atype)',\n ConditionExpression:\n '(attribute_not_exists(currentVersion) AND :expected = :zero) ' +\n 'OR currentVersion = :expected ' +\n 'OR lastIdemKey = :idem',\n ExpressionAttributeValues: {\n ':zero': 0,\n ':expected': expectedVersion,\n ':next': nextVersion,\n ':eid': eventId,\n ':idem': idempotencyKey,\n ':now': nowIso,\n ':aid': aggregateId,\n ':atype': aggregateType,\n },\n },\n },\n {\n Put: {\n TableName: TABLE,\n Item: {\n pk,\n sk,\n itemType: 'event',\n source: source as Source,\n aggregateId: aggregateId as AggregateId,\n aggregateType: aggregateType as AggregateType,\n version: nextVersion,\n\n tenantId,\n\n eventId: eventId as EventId,\n eventType: eventType,\n schemaVersion: schemaVersion ?? 1,\n occurredAt: eventOccurredAt,\n\n correlationId,\n causationId,\n actorId,\n\n payload: payload as Readonly<unknown>,\n } satisfies EventRecord,\n ConditionExpression: 'attribute_not_exists(pk) OR eventId = :eid',\n ExpressionAttributeValues: { ':eid': eventId },\n },\n },\n ],\n }),\n );\n } catch (err) {\n if (err instanceof ConditionalCheckFailedException) {\n throw new Error(\n `OCC failed for aggregate ${aggregateType}/${aggregateId}: expectedVersion=${expectedVersion}`,\n );\n }\n throw err;\n }\n\n return { pk, sk, version: nextVersion };\n },\n\n async getHead(aggregateType: string, aggregateId: string): Promise<AggregateHead | undefined> {\n const pk = pkFor(aggregateType, aggregateId);\n const res = await ddb.send(new GetCommand({ TableName: TABLE, Key: { pk, sk: 'HEAD' } }));\n return res.Item as AggregateHead | undefined;\n },\n\n async getEvent(\n aggregateType: string,\n aggregateId: string,\n version: number,\n ): Promise<EventRecord | undefined> {\n const pk = pkFor(aggregateType, aggregateId);\n const sk = `EVT#${pad(version)}`;\n const res = await ddb.send(new GetCommand({ TableName: TABLE, Key: { pk, sk } }));\n return res.Item as EventRecord | undefined;\n },\n\n async listEvents(params: {\n aggregateType: string;\n aggregateId: string;\n fromVersionExclusive?: number;\n toVersionInclusive?: number;\n limit?: number;\n }): Promise<PaginationResponse<EventRecord>> {\n const pk = pkFor(params.aggregateType, params.aggregateId);\n const fromSk =\n params.fromVersionExclusive != null\n ? `EVT#${pad(params.fromVersionExclusive + 1)}`\n : 'EVT#000000000';\n const toSk =\n params.toVersionInclusive != null\n ? `EVT#${pad(params.toVersionInclusive)}`\n : 'EVT#999999999';\n\n const res = await ddb.send(\n new QueryCommand({\n TableName: TABLE,\n KeyConditionExpression: 'pk = :pk AND sk BETWEEN :from AND :to',\n ExpressionAttributeValues: {\n ':pk': pk,\n ':from': fromSk,\n ':to': toSk,\n },\n ScanIndexForward: true,\n Limit: params.limit,\n }),\n );\n\n return normalizePaginationResponse({\n data: (res.Items ?? []) as EventRecord[],\n cursor: res.LastEvaluatedKey && (res.LastEvaluatedKey as { pk: string; sk: string }).sk,\n });\n },\n };\n}\n","import { createEventService, EventService } from './event-service';\n\nlet _eventService: EventService | undefined;\n\nexport function initEvents(config: { tableName: string }): void {\n _eventService = createEventService(config.tableName);\n}\n\nexport function getEventService(): EventService {\n if (!_eventService) {\n throw new Error('Call initEvents() before using events');\n }\n return _eventService;\n}\n","import { AppendArgs } from './event-service';\n\nexport type EventContext = Partial<AppendArgs>;\n\nlet context: EventContext = {};\n\nexport const setEventContext = (newContext: EventContext) => {\n context = { ...newContext };\n};\n\nexport const getEventContext = () => context;\n\nexport const resetEventContext = () => {\n context = {};\n};\n\nexport const appendEventContext = (event: EventContext) => {\n context = { ...context, ...event };\n};\n","import { retry } from '@auriclabs/api-core';\nimport { logger } from '@auriclabs/logger';\nimport { ulid } from 'ulid';\n\nimport { getEventContext } from './context';\nimport { AppendArgs, AppendEventResult } from './event-service';\nimport { getEventService } from './init';\n\nexport type DispatchEventArgs = Omit<\n AppendArgs,\n 'eventId' | 'expectedVersion' | 'schemaVersion' | 'occurredAt' | 'idempotencyKey'\n> &\n Partial<Pick<AppendArgs, 'idempotencyKey' | 'eventId'>>;\n\nexport const dispatchEvent = async (event: DispatchEventArgs): Promise<AppendEventResult> => {\n const eventService = getEventService();\n const eventId = event.eventId ?? `evt-${ulid()}`;\n const occurredAt = new Date().toISOString();\n const idempotencyKey = event.idempotencyKey ?? eventId;\n\n return retry(async () => {\n const head = await eventService.getHead(event.aggregateType, event.aggregateId);\n logger.debug({ event }, 'Dispatching event');\n return eventService.appendEvent({\n ...getEventContext(),\n ...event,\n eventId,\n expectedVersion: head?.currentVersion ?? 0,\n schemaVersion: 1,\n occurredAt,\n idempotencyKey,\n });\n });\n};\n","import { dispatchEvent, DispatchEventArgs } from './dispatch-event';\n\nexport interface DispatchEventsArgs {\n inOrder?: boolean;\n}\n\nexport const dispatchEvents = async (\n events: DispatchEventArgs[],\n { inOrder = false }: DispatchEventsArgs = {},\n) => {\n if (inOrder) {\n for (const event of events) {\n await dispatchEvent(event);\n }\n } else {\n await Promise.all(events.map((event) => dispatchEvent(event)));\n }\n};\n","import { ulid } from 'ulid';\n\nimport { EventContext, getEventContext } from './context';\nimport { dispatchEvent, DispatchEventArgs } from './dispatch-event';\nimport { AppendEventResult } from './event-service';\n\nexport type MakePartial<T, O> = Omit<T, keyof O> & Partial<O>;\n\nexport type DispatchRecord<\n Options extends Partial<DispatchEventArgs> = Partial<DispatchEventArgs>,\n> = Record<\n string,\n (\n ...args: any[]\n ) =>\n | ValueOrFactoryRecord<MakePartial<DispatchEventArgs, Options>>\n | DispatchEventArgsFactory<Options>\n>;\n\nexport type ValueOrFactory<T> = T | ((context: EventContext) => T);\nexport type ValueOrFactoryRecord<T> = {\n [K in keyof T]: ValueOrFactory<T[K]>;\n};\n\nexport type DispatchEventArgsFactory<Options extends Partial<DispatchEventArgs>> = (\n context: EventContext,\n) => MakePartial<DispatchEventArgs, Options>;\n\nexport type DispatchRecordResponse<\n R extends DispatchRecord<O>,\n O extends Partial<DispatchEventArgs>,\n> = {\n [K in keyof R]: (...args: Parameters<R[K]>) => Promise<AppendEventResult>;\n};\n\nexport function createDispatch<DR extends DispatchRecord<O>, O extends Partial<DispatchEventArgs>>(\n record: DR,\n optionsOrFactory?: ValueOrFactoryRecord<O> | ((context: EventContext) => O),\n): DispatchRecordResponse<DR, O> {\n return Object.fromEntries(\n Object.entries(record).map(([key, value]) => [\n key,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (...args: any[]) => {\n const eventId = `evt-${ulid()}`;\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n const result = value(...args);\n const context: EventContext = { eventId, ...getEventContext() };\n const executeValueFn = (value: any) =>\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call\n typeof value === 'function' ? value(context) : value;\n const parseResponse = (result: any) =>\n Object.fromEntries(\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n Object.entries(result).map(([key, value]) => [key, executeValueFn(value)]),\n );\n Object.assign(\n context,\n typeof result === 'function' ? result(context) : parseResponse(result),\n );\n Object.assign(\n context,\n typeof optionsOrFactory === 'function'\n ? optionsOrFactory(context)\n : parseResponse(optionsOrFactory),\n );\n return dispatchEvent(context as DispatchEventArgs);\n },\n ]),\n ) as DispatchRecordResponse<DR, O>;\n}\n","import { logger } from '@auriclabs/logger';\nimport { SQSBatchResponse, SQSEvent } from 'aws-lambda';\n\nimport { setEventContext } from './context';\nimport { EventHandlers, EventRecord } from './types';\n\nexport interface CreateEventListenerOptions {\n debug?: boolean;\n}\n\nexport const createEventListener =\n (eventHandlers: EventHandlers, { debug = false }: CreateEventListenerOptions = {}) =>\n async (sqsEvent: SQSEvent) => {\n const response: SQSBatchResponse = {\n batchItemFailures: [],\n };\n let hasFailed = false;\n for (const record of sqsEvent.Records) {\n // skip the job if it has failed\n if (hasFailed) {\n response.batchItemFailures.push({\n itemIdentifier: record.messageId,\n });\n continue;\n }\n\n let event: EventRecord | undefined;\n try {\n event = JSON.parse(record.body) as EventRecord;\n if (debug) {\n logger.debug({ event }, 'Processing event');\n }\n let handler = eventHandlers[event.eventType];\n while (typeof handler === 'string') {\n handler = eventHandlers[handler];\n }\n if (typeof handler === 'function') {\n setEventContext({\n causationId: event.eventId,\n correlationId: event.correlationId,\n actorId: event.actorId,\n });\n await handler(event);\n }\n } catch (error) {\n hasFailed = true;\n logger.error({ error, event, body: record.body }, 'Error processing event');\n response.batchItemFailures.push({\n itemIdentifier: record.messageId,\n });\n }\n }\n return response;\n };\n"],"mappings":";;;;;;;;AAoBA,MAAM,MAAM,uBAAuB,KAAK,IAAI,gBAAgB,EAAE,EAC5D,iBAAiB,EACf,uBAAuB,MACxB,EACF,CAAC;AAEF,MAAM,OAAO,GAAW,IAAI,MAAe,OAAO,EAAE,CAAC,SAAS,GAAG,IAAI;AACrE,MAAM,SAAS,eAAuB,gB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,OAAO,aAAuB;CAC5B,MAAM,WAA6B,EACjC,mBAAmB,EAAE,EACtB;CACD,IAAI,YAAY;AAChB,MAAK,MAAM,UAAU,SAAS,SAAS;AAErC,MAAI,WAAW;AACb,YAAS,kBAAkB,KAAK,EAC9B,gBAAgB,OAAO,WACxB,CAAC;AACF;;EAGF,IAAI;AACJ,MAAI;AACF,WAAQ,KAAK,MAAM,OAAO,KAAK;AAC/B,OAAI,MACF,QAAO,MAAM,EAAE,OAAO,EAAE,mBAAmB;GAE7C,IAAI,UAAU,cAAc,MAAM;AAClC,UAAO,OAAO,YAAY,SACxB,WAAU,cAAc;AAE1B,OAAI,OAAO,YAAY,YAAY;AACjC,oBAAgB;KACd,aAAa,MAAM;KACnB,eAAe,MAAM;KACrB,SAAS,MAAM;KAChB,CAAC;AACF,UAAM,QAAQ,MAAM;;WAEf,OAAO;AACd,eAAY;AACZ,UAAO,MAAM;IAAE;IAAO;IAAO,MAAM,OAAO;IAAM,EAAE,yBAAyB;AAC3E,YAAS,kBAAkB,KAAK,EAC9B,gBAAgB,OAAO,WACxB,CAAC;;;AAGN,QAAO"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/event-service.ts","../src/init.ts","../src/context.ts","../src/dispatch-event.ts","../src/dispatch-events.ts","../src/create-dispatch.ts","../src/create-event-listener.ts","../src/stream-handler.ts"],"sourcesContent":["import { normalizePaginationResponse, PaginationResponse } from '@auriclabs/pagination';\nimport { DynamoDBClient, ConditionalCheckFailedException } from '@aws-sdk/client-dynamodb';\nimport {\n DynamoDBDocumentClient,\n TransactWriteCommand,\n GetCommand,\n QueryCommand,\n} from '@aws-sdk/lib-dynamodb';\n\nimport type {\n EventRecord,\n AggregateHead,\n AggregatePK,\n EventId,\n AggregateId,\n AggregateType,\n EventSK,\n Source,\n} from './types';\n\nconst ddb = DynamoDBDocumentClient.from(new DynamoDBClient(), {\n marshallOptions: {\n removeUndefinedValues: true,\n },\n});\n\nconst pad = (n: number, w = 9): EventId => String(n).padStart(w, '0') as EventId;\nconst pkFor = (aggregateType: string, aggregateId: string): AggregatePK =>\n `AGG#${aggregateType}#${aggregateId}`;\n\nexport interface AppendArgs<P = unknown> {\n tenantId: string;\n aggregateType: string;\n aggregateId: string;\n source: string;\n /** Version you observed before appending (0 for brand new) */\n expectedVersion: number;\n /** Required for idempotent retries (e.g., the command id) */\n idempotencyKey: string;\n\n // Event properties (flattened)\n eventId: string; // ULID/UUID – must be stable across retries\n eventType: string;\n occurredAt?: string; // default: now ISO\n payload?: Readonly<P>;\n schemaVersion?: number; // optional but recommended\n\n // Optional metadata\n correlationId?: string;\n causationId?: string;\n actorId?: string;\n}\n\nexport interface AppendEventResult {\n pk: string;\n sk: string;\n version: number;\n}\n\nexport interface EventService {\n appendEvent<P = unknown>(args: AppendArgs<P>): Promise<AppendEventResult>;\n getHead(aggregateType: string, aggregateId: string): Promise<AggregateHead | undefined>;\n getEvent(\n aggregateType: string,\n aggregateId: string,\n version: number,\n ): Promise<EventRecord | undefined>;\n listEvents(params: {\n aggregateType: string;\n aggregateId: string;\n fromVersionExclusive?: number;\n toVersionInclusive?: number;\n limit?: number;\n }): Promise<PaginationResponse<EventRecord>>;\n}\n\nexport function createEventService(tableName: string): EventService {\n const TABLE = tableName;\n\n return {\n async appendEvent<P = unknown>(args: AppendArgs<P>): Promise<AppendEventResult> {\n const {\n tenantId,\n aggregateType,\n aggregateId,\n expectedVersion,\n idempotencyKey,\n eventId,\n eventType,\n occurredAt,\n source,\n payload,\n schemaVersion,\n correlationId,\n causationId,\n actorId,\n } = args;\n\n const pk = pkFor(aggregateType, aggregateId);\n const nextVersion = expectedVersion + 1;\n const sk = `EVT#${pad(nextVersion)}` as EventSK;\n const nowIso = new Date().toISOString();\n const eventOccurredAt = occurredAt ?? nowIso;\n\n try {\n await ddb.send(\n new TransactWriteCommand({\n TransactItems: [\n {\n Update: {\n TableName: TABLE,\n Key: { pk, sk: 'HEAD' },\n UpdateExpression:\n 'SET currentVersion = :next, lastEventId = :eid, lastIdemKey = :idem, updatedAt = :now, aggregateId = if_not_exists(aggregateId, :aid), aggregateType = if_not_exists(aggregateType, :atype)',\n ConditionExpression:\n '(attribute_not_exists(currentVersion) AND :expected = :zero) ' +\n 'OR currentVersion = :expected ' +\n 'OR lastIdemKey = :idem',\n ExpressionAttributeValues: {\n ':zero': 0,\n ':expected': expectedVersion,\n ':next': nextVersion,\n ':eid': eventId,\n ':idem': idempotencyKey,\n ':now': nowIso,\n ':aid': aggregateId,\n ':atype': aggregateType,\n },\n },\n },\n {\n Put: {\n TableName: TABLE,\n Item: {\n pk,\n sk,\n itemType: 'event',\n source: source as Source,\n aggregateId: aggregateId as AggregateId,\n aggregateType: aggregateType as AggregateType,\n version: nextVersion,\n\n tenantId,\n\n eventId: eventId as EventId,\n eventType: eventType,\n schemaVersion: schemaVersion ?? 1,\n occurredAt: eventOccurredAt,\n\n correlationId,\n causationId,\n actorId,\n\n payload: payload as Readonly<unknown>,\n } satisfies EventRecord,\n ConditionExpression: 'attribute_not_exists(pk) OR eventId = :eid',\n ExpressionAttributeValues: { ':eid': eventId },\n },\n },\n ],\n }),\n );\n } catch (err) {\n if (err instanceof ConditionalCheckFailedException) {\n throw new Error(\n `OCC failed for aggregate ${aggregateType}/${aggregateId}: expectedVersion=${expectedVersion}`,\n );\n }\n throw err;\n }\n\n return { pk, sk, version: nextVersion };\n },\n\n async getHead(aggregateType: string, aggregateId: string): Promise<AggregateHead | undefined> {\n const pk = pkFor(aggregateType, aggregateId);\n const res = await ddb.send(new GetCommand({ TableName: TABLE, Key: { pk, sk: 'HEAD' } }));\n return res.Item as AggregateHead | undefined;\n },\n\n async getEvent(\n aggregateType: string,\n aggregateId: string,\n version: number,\n ): Promise<EventRecord | undefined> {\n const pk = pkFor(aggregateType, aggregateId);\n const sk = `EVT#${pad(version)}`;\n const res = await ddb.send(new GetCommand({ TableName: TABLE, Key: { pk, sk } }));\n return res.Item as EventRecord | undefined;\n },\n\n async listEvents(params: {\n aggregateType: string;\n aggregateId: string;\n fromVersionExclusive?: number;\n toVersionInclusive?: number;\n limit?: number;\n }): Promise<PaginationResponse<EventRecord>> {\n const pk = pkFor(params.aggregateType, params.aggregateId);\n const fromSk =\n params.fromVersionExclusive != null\n ? `EVT#${pad(params.fromVersionExclusive + 1)}`\n : 'EVT#000000000';\n const toSk =\n params.toVersionInclusive != null\n ? `EVT#${pad(params.toVersionInclusive)}`\n : 'EVT#999999999';\n\n const res = await ddb.send(\n new QueryCommand({\n TableName: TABLE,\n KeyConditionExpression: 'pk = :pk AND sk BETWEEN :from AND :to',\n ExpressionAttributeValues: {\n ':pk': pk,\n ':from': fromSk,\n ':to': toSk,\n },\n ScanIndexForward: true,\n Limit: params.limit,\n }),\n );\n\n return normalizePaginationResponse({\n data: (res.Items ?? []) as EventRecord[],\n cursor: res.LastEvaluatedKey && (res.LastEvaluatedKey as { pk: string; sk: string }).sk,\n });\n },\n };\n}\n","import { createEventService, EventService } from './event-service';\n\nlet _eventService: EventService | undefined;\n\nexport function initEvents(config: { tableName: string }): void {\n _eventService = createEventService(config.tableName);\n}\n\nexport function getEventService(): EventService {\n if (!_eventService) {\n throw new Error('Call initEvents() before using events');\n }\n return _eventService;\n}\n","import { AppendArgs } from './event-service';\n\nexport type EventContext = Partial<AppendArgs>;\n\nlet context: EventContext = {};\n\nexport const setEventContext = (newContext: EventContext) => {\n context = { ...newContext };\n};\n\nexport const getEventContext = () => context;\n\nexport const resetEventContext = () => {\n context = {};\n};\n\nexport const appendEventContext = (event: EventContext) => {\n context = { ...context, ...event };\n};\n","import { retry } from '@auriclabs/api-core';\nimport { logger } from '@auriclabs/logger';\nimport { ulid } from 'ulid';\n\nimport { getEventContext } from './context';\nimport { AppendArgs, AppendEventResult } from './event-service';\nimport { getEventService } from './init';\n\nexport type DispatchEventArgs = Omit<\n AppendArgs,\n 'eventId' | 'expectedVersion' | 'schemaVersion' | 'occurredAt' | 'idempotencyKey'\n> &\n Partial<Pick<AppendArgs, 'idempotencyKey' | 'eventId'>>;\n\nexport const dispatchEvent = async (event: DispatchEventArgs): Promise<AppendEventResult> => {\n const eventService = getEventService();\n const eventId = event.eventId ?? `evt-${ulid()}`;\n const occurredAt = new Date().toISOString();\n const idempotencyKey = event.idempotencyKey ?? eventId;\n\n return retry(async () => {\n const head = await eventService.getHead(event.aggregateType, event.aggregateId);\n logger.debug({ event }, 'Dispatching event');\n return eventService.appendEvent({\n ...getEventContext(),\n ...event,\n eventId,\n expectedVersion: head?.currentVersion ?? 0,\n schemaVersion: 1,\n occurredAt,\n idempotencyKey,\n });\n });\n};\n","import { dispatchEvent, DispatchEventArgs } from './dispatch-event';\n\nexport interface DispatchEventsArgs {\n inOrder?: boolean;\n}\n\nexport const dispatchEvents = async (\n events: DispatchEventArgs[],\n { inOrder = false }: DispatchEventsArgs = {},\n) => {\n if (inOrder) {\n for (const event of events) {\n await dispatchEvent(event);\n }\n } else {\n await Promise.all(events.map((event) => dispatchEvent(event)));\n }\n};\n","import { ulid } from 'ulid';\n\nimport { EventContext, getEventContext } from './context';\nimport { dispatchEvent, DispatchEventArgs } from './dispatch-event';\nimport { AppendEventResult } from './event-service';\n\nexport type MakePartial<T, O> = Omit<T, keyof O> & Partial<O>;\n\nexport type DispatchRecord<\n Options extends Partial<DispatchEventArgs> = Partial<DispatchEventArgs>,\n> = Record<\n string,\n (\n ...args: any[]\n ) =>\n | ValueOrFactoryRecord<MakePartial<DispatchEventArgs, Options>>\n | DispatchEventArgsFactory<Options>\n>;\n\nexport type ValueOrFactory<T> = T | ((context: EventContext) => T);\nexport type ValueOrFactoryRecord<T> = {\n [K in keyof T]: ValueOrFactory<T[K]>;\n};\n\nexport type DispatchEventArgsFactory<Options extends Partial<DispatchEventArgs>> = (\n context: EventContext,\n) => MakePartial<DispatchEventArgs, Options>;\n\nexport type DispatchRecordResponse<\n R extends DispatchRecord<O>,\n O extends Partial<DispatchEventArgs>,\n> = {\n [K in keyof R]: (...args: Parameters<R[K]>) => Promise<AppendEventResult>;\n};\n\nexport function createDispatch<DR extends DispatchRecord<O>, O extends Partial<DispatchEventArgs>>(\n record: DR,\n optionsOrFactory?: ValueOrFactoryRecord<O> | ((context: EventContext) => O),\n): DispatchRecordResponse<DR, O> {\n return Object.fromEntries(\n Object.entries(record).map(([key, value]) => [\n key,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n (...args: any[]) => {\n const eventId = `evt-${ulid()}`;\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n const result = value(...args);\n const context: EventContext = { eventId, ...getEventContext() };\n const executeValueFn = (value: any) =>\n // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call\n typeof value === 'function' ? value(context) : value;\n const parseResponse = (result: any) =>\n Object.fromEntries(\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument\n Object.entries(result).map(([key, value]) => [key, executeValueFn(value)]),\n );\n Object.assign(\n context,\n typeof result === 'function' ? result(context) : parseResponse(result),\n );\n Object.assign(\n context,\n typeof optionsOrFactory === 'function'\n ? optionsOrFactory(context)\n : parseResponse(optionsOrFactory),\n );\n return dispatchEvent(context as DispatchEventArgs);\n },\n ]),\n ) as DispatchRecordResponse<DR, O>;\n}\n","import { logger } from '@auriclabs/logger';\nimport { SQSBatchResponse, SQSEvent } from 'aws-lambda';\n\nimport { setEventContext } from './context';\nimport { EventHandlers, EventRecord } from './types';\n\nexport interface CreateEventListenerOptions {\n debug?: boolean;\n}\n\nexport const createEventListener =\n (eventHandlers: EventHandlers, { debug = false }: CreateEventListenerOptions = {}) =>\n async (sqsEvent: SQSEvent) => {\n const response: SQSBatchResponse = {\n batchItemFailures: [],\n };\n let hasFailed = false;\n for (const record of sqsEvent.Records) {\n // skip the job if it has failed\n if (hasFailed) {\n response.batchItemFailures.push({\n itemIdentifier: record.messageId,\n });\n continue;\n }\n\n let event: EventRecord | undefined;\n try {\n event = JSON.parse(record.body) as EventRecord;\n if (debug) {\n logger.debug({ event }, 'Processing event');\n }\n let handler = eventHandlers[event.eventType];\n while (typeof handler === 'string') {\n handler = eventHandlers[handler];\n }\n if (typeof handler === 'function') {\n setEventContext({\n causationId: event.eventId,\n correlationId: event.correlationId,\n actorId: event.actorId,\n });\n await handler(event);\n }\n } catch (error) {\n hasFailed = true;\n logger.error({ error, event, body: record.body }, 'Error processing event');\n response.batchItemFailures.push({\n itemIdentifier: record.messageId,\n });\n }\n }\n return response;\n };\n","import { logger } from '@auriclabs/logger';\nimport { AttributeValue } from '@aws-sdk/client-dynamodb';\nimport { EventBridgeClient, PutEventsCommand } from '@aws-sdk/client-eventbridge';\nimport { SendMessageBatchCommand, SQSClient } from '@aws-sdk/client-sqs';\nimport { unmarshall } from '@aws-sdk/util-dynamodb';\nimport { DynamoDBStreamEvent } from 'aws-lambda';\nimport { kebabCase } from 'lodash-es';\n\nimport { AggregateHead, EventRecord } from './types';\n\nconst BATCH_SIZE = 10;\n\nexport interface CreateStreamHandlerConfig {\n busName?: string;\n queueUrls: string[];\n}\n\n/**\n * Creates a Lambda handler for DynamoDB stream events.\n * Processes INSERT events from the event store table and forwards them to SQS queues and EventBridge.\n */\nexport function createStreamHandler(config: CreateStreamHandlerConfig) {\n const sqsClient = new SQSClient();\n const eventBridge = new EventBridgeClient({});\n\n function chunkArray<T>(array: T[], chunkSize: number): T[][] {\n const chunks: T[][] = [];\n for (let i = 0; i < array.length; i += chunkSize) {\n chunks.push(array.slice(i, i + chunkSize));\n }\n return chunks;\n }\n\n async function sendToQueuesBatch(eventRecords: EventRecord[]) {\n await Promise.all(config.queueUrls.map((queue) => sendToQueueBatch(eventRecords, queue)));\n }\n\n async function sendToQueueBatch(eventRecords: EventRecord[], queue: string) {\n const batches = chunkArray(eventRecords, BATCH_SIZE);\n\n for (const batch of batches) {\n try {\n const entries = batch.map((eventRecord, index) => ({\n Id: `${eventRecord.eventId}-${index}`,\n MessageBody: JSON.stringify(eventRecord),\n MessageGroupId: eventRecord.aggregateId,\n MessageDeduplicationId: eventRecord.eventId,\n }));\n\n await sqsClient.send(\n new SendMessageBatchCommand({\n QueueUrl: queue,\n Entries: entries,\n }),\n );\n } catch (error) {\n logger.error({ error, batch, queue }, 'Error sending batch to queue');\n throw error;\n }\n }\n }\n\n async function sendToBusBatch(eventRecords: EventRecord[]) {\n const batches = chunkArray(eventRecords, BATCH_SIZE);\n\n for (const batch of batches) {\n try {\n const entries = batch.map((eventRecord) => {\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const source = eventRecord.source ?? kebabCase(eventRecord.aggregateType.split('.')[0]);\n return {\n Source: source,\n DetailType: eventRecord.eventType,\n Detail: JSON.stringify(eventRecord),\n EventBusName: config.busName,\n };\n });\n\n await eventBridge.send(\n new PutEventsCommand({\n Entries: entries,\n }),\n );\n } catch (error) {\n logger.error({ error, batch }, 'Error sending batch to bus');\n throw error;\n }\n }\n }\n\n return async (event: DynamoDBStreamEvent): Promise<void> => {\n const eventRecords = event.Records.filter((record) => record.eventName === 'INSERT')\n .map((record) => {\n try {\n const data = record.dynamodb?.NewImage;\n return unmarshall(data as Record<string, AttributeValue>) as EventRecord | AggregateHead;\n } catch (error) {\n logger.error({ error, record }, 'Error unmarshalling event record');\n return undefined;\n }\n })\n .filter((eventRecord): eventRecord is EventRecord => eventRecord?.itemType === 'event');\n\n if (eventRecords.length > 0) {\n const tasks: Promise<void>[] = [sendToQueuesBatch(eventRecords)];\n if (config.busName) {\n tasks.push(sendToBusBatch(eventRecords));\n }\n await Promise.all(tasks);\n }\n };\n}\n"],"mappings":";;;;;;;;;;;AAoBA,MAAM,MAAM,uBAAuB,KAAK,IAAI,gBAAgB,EAAE,EAC5D,iBAAiB,EACf,uBAAuB,MACxB,EACF,CAAC;AAEF,MAAM,OAAO,GAAW,IAAI,MAAe,OAAO,EAAE,CAAC,SAAS,GAAG,IAAI;AACrE,MAAM,SAAS,eAAuB,gBACpC,OAAO,cAAc,GAAG;AAgD1B,SAAgB,mBAAmB,WAAiC;CAClE,MAAM,QAAQ;AAEd,QAAO;EACL,MAA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
|
|
3
|
+
let _aws_sdk_client_eventbridge = require("@aws-sdk/client-eventbridge");
|
|
4
|
+
let _aws_sdk_client_sqs = require("@aws-sdk/client-sqs");
|
|
5
|
+
let _aws_sdk_util_dynamodb = require("@aws-sdk/util-dynamodb");
|
|
6
|
+
let lodash_es = require("lodash-es");
|
|
7
|
+
//#region src/stream-handler.ts
|
|
8
|
+
const BATCH_SIZE = 10;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a Lambda handler for DynamoDB stream events.
|
|
11
|
+
* Processes INSERT events from the event store table and forwards them to SQS queues and EventBridge.
|
|
12
|
+
*/
|
|
13
|
+
function createStreamHandler(config) {
|
|
14
|
+
const sqsClient = new _aws_sdk_client_sqs.SQSClient();
|
|
15
|
+
const eventBridge = new _aws_sdk_client_eventbridge.EventBridgeClient({});
|
|
16
|
+
function chunkArray(array, chunkSize) {
|
|
17
|
+
const chunks = [];
|
|
18
|
+
for (let i = 0; i < array.length; i += chunkSize) chunks.push(array.slice(i, i + chunkSize));
|
|
19
|
+
return chunks;
|
|
20
|
+
}
|
|
21
|
+
async function sendToQueuesBatch(eventRecords) {
|
|
22
|
+
await Promise.all(config.queueUrls.map((queue) => sendToQueueBatch(eventRecords, queue)));
|
|
23
|
+
}
|
|
24
|
+
async function sendToQueueBatch(eventRecords, queue) {
|
|
25
|
+
const batches = chunkArray(eventRecords, BATCH_SIZE);
|
|
26
|
+
for (const batch of batches) try {
|
|
27
|
+
const entries = batch.map((eventRecord, index) => ({
|
|
28
|
+
Id: `${eventRecord.eventId}-${index}`,
|
|
29
|
+
MessageBody: JSON.stringify(eventRecord),
|
|
30
|
+
MessageGroupId: eventRecord.aggregateId,
|
|
31
|
+
MessageDeduplicationId: eventRecord.eventId
|
|
32
|
+
}));
|
|
33
|
+
await sqsClient.send(new _aws_sdk_client_sqs.SendMessageBatchCommand({
|
|
34
|
+
QueueUrl: queue,
|
|
35
|
+
Entries: entries
|
|
36
|
+
}));
|
|
37
|
+
} catch (error) {
|
|
38
|
+
_auriclabs_logger.logger.error({
|
|
39
|
+
error,
|
|
40
|
+
batch,
|
|
41
|
+
queue
|
|
42
|
+
}, "Error sending batch to queue");
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async function sendToBusBatch(eventRecords) {
|
|
47
|
+
const batches = chunkArray(eventRecords, BATCH_SIZE);
|
|
48
|
+
for (const batch of batches) try {
|
|
49
|
+
const entries = batch.map((eventRecord) => {
|
|
50
|
+
return {
|
|
51
|
+
Source: eventRecord.source ?? (0, lodash_es.kebabCase)(eventRecord.aggregateType.split(".")[0]),
|
|
52
|
+
DetailType: eventRecord.eventType,
|
|
53
|
+
Detail: JSON.stringify(eventRecord),
|
|
54
|
+
EventBusName: config.busName
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
await eventBridge.send(new _aws_sdk_client_eventbridge.PutEventsCommand({ Entries: entries }));
|
|
58
|
+
} catch (error) {
|
|
59
|
+
_auriclabs_logger.logger.error({
|
|
60
|
+
error,
|
|
61
|
+
batch
|
|
62
|
+
}, "Error sending batch to bus");
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return async (event) => {
|
|
67
|
+
const eventRecords = event.Records.filter((record) => record.eventName === "INSERT").map((record) => {
|
|
68
|
+
try {
|
|
69
|
+
const data = record.dynamodb?.NewImage;
|
|
70
|
+
return (0, _aws_sdk_util_dynamodb.unmarshall)(data);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
_auriclabs_logger.logger.error({
|
|
73
|
+
error,
|
|
74
|
+
record
|
|
75
|
+
}, "Error unmarshalling event record");
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
}).filter((eventRecord) => eventRecord?.itemType === "event");
|
|
79
|
+
if (eventRecords.length > 0) {
|
|
80
|
+
const tasks = [sendToQueuesBatch(eventRecords)];
|
|
81
|
+
if (config.busName) tasks.push(sendToBusBatch(eventRecords));
|
|
82
|
+
await Promise.all(tasks);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
//#endregion
|
|
2
87
|
//#region src/stream-handler.entry.ts
|
|
3
|
-
const handler =
|
|
88
|
+
const handler = createStreamHandler({
|
|
4
89
|
busName: process.env.EVENT_BUS_NAME,
|
|
5
90
|
queueUrls: JSON.parse(process.env.QUEUE_URL_LIST ?? "[]")
|
|
6
91
|
});
|
|
@@ -1,5 +1,189 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Writable } from "node:stream";
|
|
2
2
|
|
|
3
|
+
//#region ../../node_modules/.pnpm/@types+aws-lambda@8.10.152/node_modules/@types/aws-lambda/handler.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* {@link Handler} context parameter.
|
|
6
|
+
* See {@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html AWS documentation}.
|
|
7
|
+
*/
|
|
8
|
+
interface Context {
|
|
9
|
+
callbackWaitsForEmptyEventLoop: boolean;
|
|
10
|
+
functionName: string;
|
|
11
|
+
functionVersion: string;
|
|
12
|
+
invokedFunctionArn: string;
|
|
13
|
+
memoryLimitInMB: string;
|
|
14
|
+
awsRequestId: string;
|
|
15
|
+
logGroupName: string;
|
|
16
|
+
logStreamName: string;
|
|
17
|
+
identity?: CognitoIdentity | undefined;
|
|
18
|
+
clientContext?: ClientContext | undefined;
|
|
19
|
+
tenantId?: string | undefined;
|
|
20
|
+
getRemainingTimeInMillis(): number; // Functions for compatibility with earlier Node.js Runtime v0.10.42
|
|
21
|
+
// No longer documented, so they are deprecated, but they still work
|
|
22
|
+
// as of the 12.x runtime, so they are not removed from the types.
|
|
23
|
+
/** @deprecated Use handler callback or promise result */
|
|
24
|
+
done(error?: Error, result?: any): void;
|
|
25
|
+
/** @deprecated Use handler callback with first argument or reject a promise result */
|
|
26
|
+
fail(error: Error | string): void;
|
|
27
|
+
/** @deprecated Use handler callback with second argument or resolve a promise result */
|
|
28
|
+
succeed(messageOrObject: any): void; // Unclear what behavior this is supposed to have, I couldn't find any still extant reference,
|
|
29
|
+
// and it behaves like the above, ignoring the object parameter.
|
|
30
|
+
/** @deprecated Use handler callback or promise result */
|
|
31
|
+
succeed(message: string, object: any): void;
|
|
32
|
+
}
|
|
33
|
+
interface CognitoIdentity {
|
|
34
|
+
cognitoIdentityId: string;
|
|
35
|
+
cognitoIdentityPoolId: string;
|
|
36
|
+
}
|
|
37
|
+
interface ClientContext {
|
|
38
|
+
client: ClientContextClient;
|
|
39
|
+
Custom?: any;
|
|
40
|
+
env: ClientContextEnv;
|
|
41
|
+
}
|
|
42
|
+
interface ClientContextClient {
|
|
43
|
+
installationId: string;
|
|
44
|
+
appTitle: string;
|
|
45
|
+
appVersionName: string;
|
|
46
|
+
appVersionCode: string;
|
|
47
|
+
appPackageName: string;
|
|
48
|
+
}
|
|
49
|
+
interface ClientContextEnv {
|
|
50
|
+
platformVersion: string;
|
|
51
|
+
platform: string;
|
|
52
|
+
make: string;
|
|
53
|
+
model: string;
|
|
54
|
+
locale: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Interface for using response streaming from AWS Lambda.
|
|
58
|
+
* To indicate to the runtime that Lambda should stream your function’s responses, you must wrap your function handler with the `awslambda.streamifyResponse()` decorator.
|
|
59
|
+
*
|
|
60
|
+
* The `streamifyResponse` decorator accepts the following additional parameter, `responseStream`, besides the default node handler parameters, `event`, and `context`.
|
|
61
|
+
* The new `responseStream` object provides a stream object that your function can write data to. Data written to this stream is sent immediately to the client. You can optionally set the Content-Type header of the response to pass additional metadata to your client about the contents of the stream.
|
|
62
|
+
*
|
|
63
|
+
* {@link https://aws.amazon.com/blogs/compute/introducing-aws-lambda-response-streaming/ AWS blog post}
|
|
64
|
+
* {@link https://docs.aws.amazon.com/lambda/latest/dg/config-rs-write-functions.html AWS documentation}
|
|
65
|
+
*
|
|
66
|
+
* @example <caption>Writing to the response stream</caption>
|
|
67
|
+
* import 'aws-lambda';
|
|
68
|
+
*
|
|
69
|
+
* export const handler = awslambda.streamifyResponse(
|
|
70
|
+
* async (event, responseStream, context) => {
|
|
71
|
+
* responseStream.setContentType("text/plain");
|
|
72
|
+
* responseStream.write("Hello, world!");
|
|
73
|
+
* responseStream.end();
|
|
74
|
+
* }
|
|
75
|
+
* );
|
|
76
|
+
*
|
|
77
|
+
* @example <caption>Using pipeline</caption>
|
|
78
|
+
* import 'aws-lambda';
|
|
79
|
+
* import { Readable } from 'stream';
|
|
80
|
+
* import { pipeline } from 'stream/promises';
|
|
81
|
+
* import zlib from 'zlib';
|
|
82
|
+
*
|
|
83
|
+
* export const handler = awslambda.streamifyResponse(
|
|
84
|
+
* async (event, responseStream, context) => {
|
|
85
|
+
* // As an example, convert event to a readable stream.
|
|
86
|
+
* const requestStream = Readable.from(Buffer.from(JSON.stringify(event)));
|
|
87
|
+
*
|
|
88
|
+
* await pipeline(requestStream, zlib.createGzip(), responseStream);
|
|
89
|
+
* }
|
|
90
|
+
* );
|
|
91
|
+
*/
|
|
92
|
+
type StreamifyHandler<TEvent = any, TResult = any> = (event: TEvent, responseStream: awslambda.HttpResponseStream, context: Context) => TResult | Promise<TResult>;
|
|
93
|
+
declare global {
|
|
94
|
+
namespace awslambda {
|
|
95
|
+
class HttpResponseStream extends Writable {
|
|
96
|
+
static from(writable: Writable, metadata: Record<string, unknown>): HttpResponseStream;
|
|
97
|
+
setContentType: (contentType: string) => void;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Decorator for using response streaming from AWS Lambda.
|
|
101
|
+
* To indicate to the runtime that Lambda should stream your function’s responses, you must wrap your function handler with the `awslambda.streamifyResponse()` decorator.
|
|
102
|
+
*
|
|
103
|
+
* The `streamifyResponse` decorator accepts the following additional parameter, `responseStream`, besides the default node handler parameters, `event`, and `context`.
|
|
104
|
+
* The new `responseStream` object provides a stream object that your function can write data to. Data written to this stream is sent immediately to the client. You can optionally set the Content-Type header of the response to pass additional metadata to your client about the contents of the stream.
|
|
105
|
+
*
|
|
106
|
+
* {@link https://aws.amazon.com/blogs/compute/introducing-aws-lambda-response-streaming/ AWS blog post}
|
|
107
|
+
* {@link https://docs.aws.amazon.com/lambda/latest/dg/config-rs-write-functions.html AWS documentation}
|
|
108
|
+
*
|
|
109
|
+
* @example <caption>Writing to the response stream</caption>
|
|
110
|
+
* import 'aws-lambda';
|
|
111
|
+
*
|
|
112
|
+
* export const handler = awslambda.streamifyResponse(
|
|
113
|
+
* async (event, responseStream, context) => {
|
|
114
|
+
* responseStream.setContentType("text/plain");
|
|
115
|
+
* responseStream.write("Hello, world!");
|
|
116
|
+
* responseStream.end();
|
|
117
|
+
* }
|
|
118
|
+
* );
|
|
119
|
+
*
|
|
120
|
+
* @example <caption>Using pipeline</caption>
|
|
121
|
+
* import 'aws-lambda';
|
|
122
|
+
* import { Readable } from 'stream';
|
|
123
|
+
* import { pipeline } from 'stream/promises';
|
|
124
|
+
* import zlib from 'zlib';
|
|
125
|
+
*
|
|
126
|
+
* export const handler = awslambda.streamifyResponse(
|
|
127
|
+
* async (event, responseStream, context) => {
|
|
128
|
+
* // As an example, convert event to a readable stream.
|
|
129
|
+
* const requestStream = Readable.from(Buffer.from(JSON.stringify(event)));
|
|
130
|
+
*
|
|
131
|
+
* await pipeline(requestStream, zlib.createGzip(), responseStream);
|
|
132
|
+
* }
|
|
133
|
+
* );
|
|
134
|
+
*/
|
|
135
|
+
function streamifyResponse<TEvent = any, TResult = void>(handler: StreamifyHandler<TEvent, TResult>): StreamifyHandler<TEvent, TResult>;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
//#endregion
|
|
139
|
+
//#region ../../node_modules/.pnpm/@types+aws-lambda@8.10.152/node_modules/@types/aws-lambda/trigger/dynamodb-stream.d.ts
|
|
140
|
+
// http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_AttributeValue.html
|
|
141
|
+
interface AttributeValue {
|
|
142
|
+
B?: string | undefined;
|
|
143
|
+
BS?: string[] | undefined;
|
|
144
|
+
BOOL?: boolean | undefined;
|
|
145
|
+
L?: AttributeValue[] | undefined;
|
|
146
|
+
M?: {
|
|
147
|
+
[id: string]: AttributeValue;
|
|
148
|
+
} | undefined;
|
|
149
|
+
N?: string | undefined;
|
|
150
|
+
NS?: string[] | undefined;
|
|
151
|
+
NULL?: boolean | undefined;
|
|
152
|
+
S?: string | undefined;
|
|
153
|
+
SS?: string[] | undefined;
|
|
154
|
+
}
|
|
155
|
+
// http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_StreamRecord.html
|
|
156
|
+
interface StreamRecord {
|
|
157
|
+
ApproximateCreationDateTime?: number | undefined;
|
|
158
|
+
Keys?: {
|
|
159
|
+
[key: string]: AttributeValue;
|
|
160
|
+
} | undefined;
|
|
161
|
+
NewImage?: {
|
|
162
|
+
[key: string]: AttributeValue;
|
|
163
|
+
} | undefined;
|
|
164
|
+
OldImage?: {
|
|
165
|
+
[key: string]: AttributeValue;
|
|
166
|
+
} | undefined;
|
|
167
|
+
SequenceNumber?: string | undefined;
|
|
168
|
+
SizeBytes?: number | undefined;
|
|
169
|
+
StreamViewType?: "KEYS_ONLY" | "NEW_IMAGE" | "OLD_IMAGE" | "NEW_AND_OLD_IMAGES" | undefined;
|
|
170
|
+
}
|
|
171
|
+
// http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_Record.html
|
|
172
|
+
interface DynamoDBRecord {
|
|
173
|
+
awsRegion?: string | undefined;
|
|
174
|
+
dynamodb?: StreamRecord | undefined;
|
|
175
|
+
eventID?: string | undefined;
|
|
176
|
+
eventName?: "INSERT" | "MODIFY" | "REMOVE" | undefined;
|
|
177
|
+
eventSource?: string | undefined;
|
|
178
|
+
eventSourceARN?: string | undefined;
|
|
179
|
+
eventVersion?: string | undefined;
|
|
180
|
+
userIdentity?: any;
|
|
181
|
+
}
|
|
182
|
+
// http://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-ddb-update
|
|
183
|
+
interface DynamoDBStreamEvent {
|
|
184
|
+
Records: DynamoDBRecord[];
|
|
185
|
+
}
|
|
186
|
+
//#endregion
|
|
3
187
|
//#region src/stream-handler.entry.d.ts
|
|
4
188
|
declare const handler: (event: DynamoDBStreamEvent) => Promise<void>;
|
|
5
189
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-handler.entry.d.cts","names":[],"sources":["../src/stream-handler.entry.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"stream-handler.entry.d.cts","names":["Writable","Handler","TEvent","TResult","Context","Callback","Promise","event","context","callback","CognitoIdentity","ClientContext","Error","callbackWaitsForEmptyEventLoop","functionName","functionVersion","invokedFunctionArn","memoryLimitInMB","awsRequestId","logGroupName","logStreamName","identity","clientContext","tenantId","getRemainingTimeInMillis","done","error","result","fail","succeed","messageOrObject","message","object","cognitoIdentityId","cognitoIdentityPoolId","ClientContextClient","ClientContextEnv","client","Custom","env","installationId","appTitle","appVersionName","appVersionCode","appPackageName","platformVersion","platform","make","model","locale","StreamifyHandler","awslambda","HttpResponseStream","responseStream","_0","Record","global","from","writable","metadata","setContentType","contentType","streamifyResponse","handler","sideEffect","Handler","DynamoDBStreamHandler","DynamoDBStreamEvent","DynamoDBBatchResponse","AttributeValue","B","BS","BOOL","L","M","id","N","NS","NULL","S","SS","StreamRecord","ApproximateCreationDateTime","Keys","key","NewImage","OldImage","SequenceNumber","SizeBytes","StreamViewType","DynamoDBRecord","awsRegion","dynamodb","eventID","eventName","eventSource","eventSourceARN","eventVersion","userIdentity","Records","DynamoDBBatchItemFailure","batchItemFailures","itemIdentifier"],"sources":["../../../node_modules/.pnpm/@types+aws-lambda@8.10.152/node_modules/@types/aws-lambda/handler.d.ts","../../../node_modules/.pnpm/@types+aws-lambda@8.10.152/node_modules/@types/aws-lambda/trigger/dynamodb-stream.d.ts","../src/stream-handler.entry.ts"],"x_google_ignoreList":[0,1],"mappings":";;;;;;;UA+FiBI,OAAAA;EACbS,8BAAAA;EACAC,YAAAA;EACAC,eAAAA;EACAC,kBAAA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