@celerity-sdk/serverless-aws 0.3.1 → 0.4.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/dist/index.cjs CHANGED
@@ -31,20 +31,29 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  // src/index.ts
32
32
  var index_exports = {};
33
33
  __export(index_exports, {
34
+ ApiGatewayWebSocketSender: () => ApiGatewayWebSocketSender,
34
35
  AwsLambdaAdapter: () => AwsLambdaAdapter,
36
+ detectEventType: () => detectEventType,
35
37
  handler: () => handler,
36
38
  mapApiGatewayV2Event: () => mapApiGatewayV2Event,
37
- mapHttpResponseToResult: () => mapHttpResponseToResult
39
+ mapApiGatewayWebSocketEvent: () => mapApiGatewayWebSocketEvent,
40
+ mapConsumerResultToSqsBatchResponse: () => mapConsumerResultToSqsBatchResponse,
41
+ mapEventBridgeEvent: () => mapEventBridgeEvent,
42
+ mapHttpResponseToResult: () => mapHttpResponseToResult,
43
+ mapSqsEvent: () => mapSqsEvent
38
44
  });
39
45
  module.exports = __toCommonJS(index_exports);
40
46
 
41
47
  // src/adapter.ts
42
48
  var import_node_path = require("path");
43
- var import_debug2 = __toESM(require("debug"), 1);
49
+ var import_debug3 = __toESM(require("debug"), 1);
44
50
  var import_core = require("@celerity-sdk/core");
51
+ var import_types = require("@celerity-sdk/types");
45
52
 
46
53
  // src/event-mapper.ts
47
54
  var import_debug = __toESM(require("debug"), 1);
55
+ var import_util_dynamodb = require("@aws-sdk/util-dynamodb");
56
+ var import_common = require("@celerity-sdk/common");
48
57
  var debug = (0, import_debug.default)("celerity:serverless-aws");
49
58
  function parseBody(event) {
50
59
  if (!event.body) return {
@@ -142,36 +151,300 @@ function mapHttpResponseToResult(response) {
142
151
  return result;
143
152
  }
144
153
  __name(mapHttpResponseToResult, "mapHttpResponseToResult");
154
+ var VALID_HANDLER_TYPES = /* @__PURE__ */ new Set([
155
+ "http",
156
+ "websocket",
157
+ "consumer",
158
+ "schedule",
159
+ "custom"
160
+ ]);
161
+ function hasNested(obj, key, nested) {
162
+ const child = obj[key];
163
+ return child !== null && typeof child === "object" && nested in child;
164
+ }
165
+ __name(hasNested, "hasNested");
166
+ function detectEventType(event) {
167
+ const envType = process.env.CELERITY_HANDLER_TYPE;
168
+ if (envType && VALID_HANDLER_TYPES.has(envType)) {
169
+ debug("detectEventType: using env var CELERITY_HANDLER_TYPE=%s", envType);
170
+ return envType;
171
+ }
172
+ if (!event || typeof event !== "object") return "custom";
173
+ const e = event;
174
+ if (hasNested(e, "requestContext", "http")) return "http";
175
+ if (hasNested(e, "requestContext", "connectionId") && hasNested(e, "requestContext", "eventType")) return "websocket";
176
+ if (Array.isArray(e.Records) && e.Records.length > 0 && e.Records[0]?.eventSource === "aws:sqs") return "consumer";
177
+ if ("source" in e && "detail-type" in e) return "schedule";
178
+ return "custom";
179
+ }
180
+ __name(detectEventType, "detectEventType");
181
+ var WS_EVENT_TYPE_MAP = {
182
+ CONNECT: "connect",
183
+ MESSAGE: "message",
184
+ DISCONNECT: "disconnect"
185
+ };
186
+ function mapApiGatewayWebSocketEvent(event) {
187
+ const rc = event.requestContext;
188
+ const eventType = WS_EVENT_TYPE_MAP[rc.eventType] ?? "message";
189
+ let jsonBody;
190
+ let binaryBody;
191
+ if (event.body) {
192
+ if (event.isBase64Encoded) {
193
+ binaryBody = Buffer.from(event.body, "base64");
194
+ } else {
195
+ try {
196
+ jsonBody = JSON.parse(event.body);
197
+ } catch {
198
+ jsonBody = event.body;
199
+ }
200
+ }
201
+ }
202
+ const message = {
203
+ messageType: binaryBody ? "binary" : "json",
204
+ eventType,
205
+ connectionId: rc.connectionId,
206
+ messageId: rc.requestId,
207
+ jsonBody,
208
+ binaryBody,
209
+ requestContext: {
210
+ requestId: rc.requestId,
211
+ requestTime: rc.requestTimeEpoch,
212
+ path: `/${rc.stage}`,
213
+ protocolVersion: "websocket",
214
+ headers: {},
215
+ clientIp: "",
216
+ query: {},
217
+ cookies: {}
218
+ },
219
+ traceContext: null
220
+ };
221
+ const routeKey = rc.routeKey;
222
+ const endpoint = `https://${rc.domainName}/${rc.stage}`;
223
+ debug("mapWebSocketEvent: %s connectionId=%s routeKey=%s", eventType, rc.connectionId, routeKey);
224
+ return {
225
+ message,
226
+ routeKey,
227
+ endpoint
228
+ };
229
+ }
230
+ __name(mapApiGatewayWebSocketEvent, "mapApiGatewayWebSocketEvent");
231
+ function tryParseJson(body) {
232
+ try {
233
+ return JSON.parse(body);
234
+ } catch {
235
+ return void 0;
236
+ }
237
+ }
238
+ __name(tryParseJson, "tryParseJson");
239
+ function isS3Notification(parsed) {
240
+ if (!parsed || typeof parsed !== "object") return false;
241
+ const obj = parsed;
242
+ if (!Array.isArray(obj.Records) || obj.Records.length === 0) return false;
243
+ const first = obj.Records[0];
244
+ return first.eventSource === "aws:s3";
245
+ }
246
+ __name(isS3Notification, "isS3Notification");
247
+ function isDynamoDBStreamRecord(parsed) {
248
+ if (!parsed || typeof parsed !== "object") return false;
249
+ return parsed.eventSource === "aws:dynamodb";
250
+ }
251
+ __name(isDynamoDBStreamRecord, "isDynamoDBStreamRecord");
252
+ function extractTableName(eventSourceARN) {
253
+ if (!eventSourceARN) return void 0;
254
+ const match = eventSourceARN.match(/table\/([^/]+)/);
255
+ return match?.[1];
256
+ }
257
+ __name(extractTableName, "extractTableName");
258
+ function transformS3Body(record) {
259
+ return JSON.stringify({
260
+ key: record.s3.object.key,
261
+ ...record.s3.object.size !== void 0 && {
262
+ size: record.s3.object.size
263
+ },
264
+ ...record.s3.object.eTag !== void 0 && {
265
+ eTag: record.s3.object.eTag
266
+ }
267
+ });
268
+ }
269
+ __name(transformS3Body, "transformS3Body");
270
+ function unmarshallRecord(attrs) {
271
+ if (!attrs) return void 0;
272
+ return (0, import_util_dynamodb.unmarshall)(attrs);
273
+ }
274
+ __name(unmarshallRecord, "unmarshallRecord");
275
+ function transformDynamoDBBody(record) {
276
+ const db = record.dynamodb;
277
+ return JSON.stringify({
278
+ keys: unmarshallRecord(db?.Keys) ?? {},
279
+ ...db?.NewImage !== void 0 && {
280
+ newItem: unmarshallRecord(db.NewImage)
281
+ },
282
+ ...db?.OldImage !== void 0 && {
283
+ oldItem: unmarshallRecord(db.OldImage)
284
+ }
285
+ });
286
+ }
287
+ __name(transformDynamoDBBody, "transformDynamoDBBody");
288
+ function mapSqsRecord(record) {
289
+ const sqsVendor = {
290
+ receiptHandle: record.receiptHandle,
291
+ attributes: record.attributes,
292
+ md5OfBody: record.md5OfBody,
293
+ eventSource: record.eventSource,
294
+ awsRegion: record.awsRegion
295
+ };
296
+ const parsed = tryParseJson(record.body);
297
+ if (isS3Notification(parsed)) {
298
+ const s3Record = parsed.Records[0];
299
+ return {
300
+ messageId: record.messageId,
301
+ body: transformS3Body(s3Record),
302
+ source: record.eventSourceARN,
303
+ sourceType: "bucket",
304
+ sourceName: s3Record.s3.bucket.name,
305
+ eventType: (0, import_common.mapBucketEventType)(s3Record.eventName),
306
+ messageAttributes: record.messageAttributes,
307
+ vendor: {
308
+ ...sqsVendor,
309
+ originalBody: parsed
310
+ }
311
+ };
312
+ }
313
+ if (isDynamoDBStreamRecord(parsed)) {
314
+ return {
315
+ messageId: record.messageId,
316
+ body: transformDynamoDBBody(parsed),
317
+ source: record.eventSourceARN,
318
+ sourceType: "datastore",
319
+ sourceName: extractTableName(parsed.eventSourceARN),
320
+ eventType: (0, import_common.mapDatastoreEventType)(parsed.eventName),
321
+ messageAttributes: record.messageAttributes,
322
+ vendor: {
323
+ ...sqsVendor,
324
+ originalBody: parsed
325
+ }
326
+ };
327
+ }
328
+ return {
329
+ messageId: record.messageId,
330
+ body: record.body,
331
+ source: record.eventSourceARN,
332
+ messageAttributes: record.messageAttributes,
333
+ vendor: sqsVendor
334
+ };
335
+ }
336
+ __name(mapSqsRecord, "mapSqsRecord");
337
+ function mapSqsEvent(event, handlerTag) {
338
+ const messages = event.Records.map(mapSqsRecord);
339
+ const firstTraceHeader = event.Records[0]?.attributes?.AWSTraceHeader;
340
+ const traceContext = firstTraceHeader ? {
341
+ "x-amzn-trace-id": firstTraceHeader
342
+ } : null;
343
+ debug("mapSqsEvent: %d records, handlerTag=%s", messages.length, handlerTag);
344
+ return {
345
+ handlerTag,
346
+ messages,
347
+ vendor: {
348
+ eventSource: "aws:sqs"
349
+ },
350
+ traceContext
351
+ };
352
+ }
353
+ __name(mapSqsEvent, "mapSqsEvent");
354
+ function mapConsumerResultToSqsBatchResponse(failures) {
355
+ const batchItemFailures = (failures ?? []).map((f) => ({
356
+ itemIdentifier: f.messageId
357
+ }));
358
+ return {
359
+ batchItemFailures
360
+ };
361
+ }
362
+ __name(mapConsumerResultToSqsBatchResponse, "mapConsumerResultToSqsBatchResponse");
363
+ function mapEventBridgeEvent(event, handlerTag) {
364
+ debug("mapEventBridgeEvent: id=%s handlerTag=%s", event.id, handlerTag);
365
+ return {
366
+ handlerTag,
367
+ scheduleId: event.id,
368
+ messageId: event.id,
369
+ schedule: "",
370
+ input: event.detail,
371
+ vendor: {
372
+ source: event.source,
373
+ detailType: event["detail-type"],
374
+ account: event.account,
375
+ region: event.region
376
+ },
377
+ traceContext: null
378
+ };
379
+ }
380
+ __name(mapEventBridgeEvent, "mapEventBridgeEvent");
145
381
 
146
- // src/adapter.ts
382
+ // src/websocket-sender.ts
383
+ var import_debug2 = __toESM(require("debug"), 1);
147
384
  var debug2 = (0, import_debug2.default)("celerity:serverless-aws");
385
+ var API_GW_MGMT_PKG = "@aws-sdk/client-apigatewaymanagementapi";
386
+ var ApiGatewayWebSocketSender = class {
387
+ static {
388
+ __name(this, "ApiGatewayWebSocketSender");
389
+ }
390
+ endpoint;
391
+ client = null;
392
+ constructor(endpoint) {
393
+ this.endpoint = endpoint;
394
+ debug2("ApiGatewayWebSocketSender: created with endpoint=%s", endpoint);
395
+ }
396
+ async sendMessage(connectionId, data, _options) {
397
+ const client = await this.getClient();
398
+ const payload = typeof data === "string" ? data : JSON.stringify(data);
399
+ const { PostToConnectionCommand } = await import(API_GW_MGMT_PKG);
400
+ await client.send(new PostToConnectionCommand({
401
+ ConnectionId: connectionId,
402
+ Data: new TextEncoder().encode(payload)
403
+ }));
404
+ debug2("ApiGatewayWebSocketSender: sent message to connectionId=%s", connectionId);
405
+ }
406
+ async getClient() {
407
+ if (!this.client) {
408
+ const { ApiGatewayManagementApiClient } = await import(API_GW_MGMT_PKG);
409
+ this.client = new ApiGatewayManagementApiClient({
410
+ endpoint: this.endpoint
411
+ });
412
+ debug2("ApiGatewayWebSocketSender: client initialized for endpoint=%s", this.endpoint);
413
+ }
414
+ return this.client;
415
+ }
416
+ };
417
+
418
+ // src/adapter.ts
419
+ var debug3 = (0, import_debug3.default)("celerity:serverless-aws");
148
420
  var AwsLambdaAdapter = class {
149
421
  static {
150
422
  __name(this, "AwsLambdaAdapter");
151
423
  }
152
424
  config;
425
+ wsSenderRegistered = false;
153
426
  constructor() {
154
427
  this.config = captureAwsLambdaConfig();
155
428
  }
156
- createHandler(registry, options) {
157
- let cachedHandler = null;
429
+ createHttpHandler(registry, options) {
430
+ let cachedHandler2 = null;
158
431
  return async (event, _context) => {
159
432
  const apiEvent = event;
160
433
  const httpRequest = mapApiGatewayV2Event(apiEvent);
161
- if (!cachedHandler) {
162
- debug2("adapter: cache miss, looking up handler for %s %s", httpRequest.method, httpRequest.path);
163
- cachedHandler = (this.config.handlerId ? registry.getHandlerById(this.config.handlerId) : void 0) ?? null;
164
- if (!cachedHandler && this.config.handlerId) {
165
- cachedHandler = await (0, import_core.resolveHandlerByModuleRef)(this.config.handlerId, registry, this.config.moduleDir);
434
+ if (!cachedHandler2) {
435
+ debug3("adapter: cache miss, looking up handler for %s %s", httpRequest.method, httpRequest.path);
436
+ cachedHandler2 = (this.config.handlerId ? registry.getHandlerById("http", this.config.handlerId) : void 0) ?? null;
437
+ if (!cachedHandler2 && this.config.handlerId) {
438
+ cachedHandler2 = await (0, import_core.resolveHandlerByModuleRef)(this.config.handlerId, "http", registry, this.config.moduleDir);
166
439
  }
167
- if (!cachedHandler) {
168
- cachedHandler = registry.getHandler(httpRequest.path, httpRequest.method) ?? null;
440
+ if (!cachedHandler2) {
441
+ cachedHandler2 = registry.getHandler("http", `${httpRequest.method} ${httpRequest.path}`) ?? null;
169
442
  }
170
443
  } else {
171
- debug2("adapter: using cached handler for %s %s", httpRequest.method, httpRequest.path);
444
+ debug3("adapter: using cached handler for %s %s", httpRequest.method, httpRequest.path);
172
445
  }
173
- if (!cachedHandler) {
174
- debug2("adapter: no handler found \u2192 404");
446
+ if (!cachedHandler2) {
447
+ debug3("adapter: no handler found \u2192 404");
175
448
  return {
176
449
  statusCode: 404,
177
450
  headers: {
@@ -182,97 +455,202 @@ var AwsLambdaAdapter = class {
182
455
  })
183
456
  };
184
457
  }
185
- const httpResponse = await (0, import_core.executeHandlerPipeline)(cachedHandler, httpRequest, options);
458
+ const httpResponse = await (0, import_core.executeHttpPipeline)(cachedHandler2, httpRequest, options);
186
459
  return mapHttpResponseToResult(httpResponse);
187
460
  };
188
461
  }
462
+ createWebSocketHandler(registry, options) {
463
+ let cachedHandler2 = null;
464
+ return async (event, _context) => {
465
+ const wsEvent = event;
466
+ const { message, routeKey, endpoint } = mapApiGatewayWebSocketEvent(wsEvent);
467
+ if (!this.wsSenderRegistered) {
468
+ const sender = new ApiGatewayWebSocketSender(endpoint);
469
+ options.container.register(import_types.WebSocketSender, {
470
+ useValue: sender
471
+ });
472
+ this.wsSenderRegistered = true;
473
+ debug3("adapter: registered ApiGatewayWebSocketSender for endpoint=%s", endpoint);
474
+ }
475
+ if (!cachedHandler2) {
476
+ debug3("adapter: cache miss, looking up WebSocket handler for routeKey=%s", routeKey);
477
+ cachedHandler2 = (this.config.handlerId ? registry.getHandlerById("websocket", this.config.handlerId) : void 0) ?? null;
478
+ if (!cachedHandler2 && this.config.handlerId) {
479
+ cachedHandler2 = await (0, import_core.resolveHandlerByModuleRef)(this.config.handlerId, "websocket", registry, this.config.moduleDir);
480
+ }
481
+ if (!cachedHandler2) {
482
+ cachedHandler2 = registry.getHandler("websocket", routeKey) ?? null;
483
+ }
484
+ } else {
485
+ debug3("adapter: using cached WebSocket handler");
486
+ }
487
+ if (!cachedHandler2) {
488
+ debug3("adapter: no WebSocket handler found \u2192 404");
489
+ return {
490
+ statusCode: 404
491
+ };
492
+ }
493
+ await (0, import_core.executeWebSocketPipeline)(cachedHandler2, message, options);
494
+ return {
495
+ statusCode: 200
496
+ };
497
+ };
498
+ }
499
+ createConsumerHandler(registry, options) {
500
+ let cachedHandler2 = null;
501
+ return async (event, _context) => {
502
+ const sqsEvent = event;
503
+ const handlerTag = this.config.handlerTag ?? sqsEvent.Records[0]?.eventSourceARN ?? "unknown";
504
+ const consumerEvent = mapSqsEvent(sqsEvent, handlerTag);
505
+ if (!cachedHandler2) {
506
+ debug3("adapter: cache miss, looking up Consumer handler for tag=%s", handlerTag);
507
+ cachedHandler2 = (this.config.handlerId ? registry.getHandlerById("consumer", this.config.handlerId) : void 0) ?? null;
508
+ if (!cachedHandler2 && this.config.handlerId) {
509
+ cachedHandler2 = await (0, import_core.resolveHandlerByModuleRef)(this.config.handlerId, "consumer", registry, this.config.moduleDir);
510
+ }
511
+ if (!cachedHandler2) {
512
+ cachedHandler2 = registry.getHandler("consumer", handlerTag) ?? null;
513
+ }
514
+ } else {
515
+ debug3("adapter: using cached Consumer handler");
516
+ }
517
+ if (!cachedHandler2) {
518
+ debug3("adapter: no Consumer handler found \u2192 empty response");
519
+ return {
520
+ batchItemFailures: []
521
+ };
522
+ }
523
+ const result = await (0, import_core.executeConsumerPipeline)(cachedHandler2, consumerEvent, options);
524
+ return mapConsumerResultToSqsBatchResponse(result.failures);
525
+ };
526
+ }
527
+ createScheduleHandler(registry, options) {
528
+ let cachedHandler2 = null;
529
+ return async (event, _context) => {
530
+ const ebEvent = event;
531
+ const handlerTag = this.config.handlerTag ?? ebEvent.resources?.[0] ?? "unknown";
532
+ const scheduleEvent = mapEventBridgeEvent(ebEvent, handlerTag);
533
+ if (!cachedHandler2) {
534
+ debug3("adapter: cache miss, looking up Schedule handler for tag=%s", handlerTag);
535
+ cachedHandler2 = (this.config.handlerId ? registry.getHandlerById("schedule", this.config.handlerId) : void 0) ?? null;
536
+ if (!cachedHandler2 && this.config.handlerId) {
537
+ cachedHandler2 = await (0, import_core.resolveHandlerByModuleRef)(this.config.handlerId, "schedule", registry, this.config.moduleDir);
538
+ }
539
+ if (!cachedHandler2) {
540
+ cachedHandler2 = registry.getHandler("schedule", handlerTag) ?? null;
541
+ }
542
+ } else {
543
+ debug3("adapter: using cached Schedule handler");
544
+ }
545
+ if (!cachedHandler2) {
546
+ debug3("adapter: no Schedule handler found");
547
+ return {
548
+ success: false,
549
+ errorMessage: `No handler for schedule tag: ${handlerTag}`
550
+ };
551
+ }
552
+ return (0, import_core.executeSchedulePipeline)(cachedHandler2, scheduleEvent, options);
553
+ };
554
+ }
555
+ createCustomHandler(registry, options) {
556
+ let cachedHandler2 = null;
557
+ return async (event, _context) => {
558
+ let handlerName = this.config.handlerId;
559
+ let payload = event;
560
+ if (!handlerName && event && typeof event === "object") {
561
+ const e = event;
562
+ if (typeof e.handlerName === "string") {
563
+ handlerName = e.handlerName;
564
+ payload = e.payload ?? {};
565
+ }
566
+ }
567
+ if (!cachedHandler2) {
568
+ debug3("adapter: cache miss, looking up Custom handler for name=%s", handlerName);
569
+ cachedHandler2 = (handlerName ? registry.getHandlerById("custom", handlerName) : void 0) ?? null;
570
+ if (!cachedHandler2 && handlerName) {
571
+ cachedHandler2 = await (0, import_core.resolveHandlerByModuleRef)(handlerName, "custom", registry, this.config.moduleDir);
572
+ }
573
+ if (!cachedHandler2 && handlerName) {
574
+ cachedHandler2 = registry.getHandler("custom", handlerName) ?? null;
575
+ }
576
+ if (!cachedHandler2) {
577
+ const allCustom = registry.getHandlersByType("custom");
578
+ if (allCustom.length === 1) cachedHandler2 = allCustom[0];
579
+ }
580
+ } else {
581
+ debug3("adapter: using cached Custom handler");
582
+ }
583
+ if (!cachedHandler2) {
584
+ debug3("adapter: no Custom handler found");
585
+ return {
586
+ error: `No handler found for custom invoke: ${handlerName ?? "unknown"}`
587
+ };
588
+ }
589
+ return (0, import_core.executeCustomPipeline)(cachedHandler2, payload, options);
590
+ };
591
+ }
189
592
  };
190
593
  function captureAwsLambdaConfig() {
191
594
  const modulePath = process.env.CELERITY_MODULE_PATH;
192
595
  return {
193
596
  handlerId: process.env.CELERITY_HANDLER_ID,
597
+ handlerTag: process.env.CELERITY_HANDLER_TAG,
194
598
  moduleDir: modulePath ? (0, import_node_path.dirname)((0, import_node_path.resolve)(modulePath)) : process.cwd()
195
599
  };
196
600
  }
197
601
  __name(captureAwsLambdaConfig, "captureAwsLambdaConfig");
198
602
 
199
603
  // src/entry.ts
200
- var import_node_path2 = require("path");
201
- var import_debug3 = __toESM(require("debug"), 1);
604
+ var import_debug4 = __toESM(require("debug"), 1);
202
605
  var import_core2 = require("@celerity-sdk/core");
203
- var debug3 = (0, import_debug3.default)("celerity:serverless-aws");
204
- var cached = null;
606
+ var debug4 = (0, import_debug4.default)("celerity:serverless-aws");
607
+ var app = null;
608
+ var cachedHandler = null;
205
609
  var shutdownRegistered = false;
206
- function registerShutdownHandler(options) {
610
+ async function ensureBootstrapped() {
611
+ if (!app) {
612
+ debug4("entry: cold start, bootstrapping via CelerityFactory");
613
+ const rootModule = await (0, import_core2.discoverModule)();
614
+ app = await import_core2.CelerityFactory.create(rootModule, {
615
+ adapter: new AwsLambdaAdapter()
616
+ });
617
+ debug4("entry: bootstrap complete");
618
+ }
619
+ return app;
620
+ }
621
+ __name(ensureBootstrapped, "ensureBootstrapped");
622
+ function registerShutdownHandler(application) {
207
623
  if (shutdownRegistered) return;
208
624
  shutdownRegistered = true;
209
- debug3("entry: SIGTERM shutdown handler registered");
625
+ debug4("entry: SIGTERM shutdown handler registered");
210
626
  process.on("SIGTERM", async () => {
211
- await options.container.closeAll();
212
- await (0, import_core2.disposeLayers)([
213
- ...options.systemLayers ?? [],
214
- ...options.appLayers ?? []
215
- ]);
627
+ await application.close();
216
628
  process.exit(0);
217
629
  });
218
630
  }
219
631
  __name(registerShutdownHandler, "registerShutdownHandler");
220
- async function ensureBootstrapped() {
221
- if (!cached) {
222
- debug3("entry: cold start, bootstrapping");
223
- const systemLayers = await (0, import_core2.createDefaultSystemLayers)();
224
- debug3("entry: %d system layers created", systemLayers.length);
225
- const rootModule = await (0, import_core2.discoverModule)();
226
- const result = await (0, import_core2.bootstrap)(rootModule);
227
- const modulePath = process.env.CELERITY_MODULE_PATH;
228
- cached = {
229
- registry: result.registry,
230
- options: {
231
- container: result.container,
232
- systemLayers
233
- },
234
- moduleDir: modulePath ? (0, import_node_path2.dirname)((0, import_node_path2.resolve)(modulePath)) : process.cwd()
235
- };
236
- debug3("entry: bootstrap complete");
237
- }
238
- return cached;
239
- }
240
- __name(ensureBootstrapped, "ensureBootstrapped");
241
- async function handler(event, _context) {
242
- const { registry, options, moduleDir } = await ensureBootstrapped();
243
- registerShutdownHandler(options);
244
- const apiEvent = event;
245
- const httpRequest = mapApiGatewayV2Event(apiEvent);
246
- debug3("entry: %s %s", httpRequest.method, httpRequest.path);
247
- const handlerId = process.env.CELERITY_HANDLER_ID;
248
- let resolved = handlerId ? registry.getHandlerById(handlerId) : void 0;
249
- if (!resolved && handlerId) {
250
- resolved = await (0, import_core2.resolveHandlerByModuleRef)(handlerId, registry, moduleDir) ?? void 0;
251
- }
252
- if (!resolved) {
253
- resolved = registry.getHandler(httpRequest.path, httpRequest.method);
254
- }
255
- if (!resolved) {
256
- return {
257
- statusCode: 404,
258
- headers: {
259
- "content-type": "application/json"
260
- },
261
- body: JSON.stringify({
262
- statusCode: 404,
263
- message: `No handler for ${httpRequest.method} ${httpRequest.path}`
264
- })
265
- };
632
+ async function handler(event, context) {
633
+ const application = await ensureBootstrapped();
634
+ registerShutdownHandler(application);
635
+ if (!cachedHandler) {
636
+ const eventType = detectEventType(event);
637
+ debug4("entry: creating handler for type=%s", eventType);
638
+ cachedHandler = application.createHandler(eventType);
266
639
  }
267
- const httpResponse = await (0, import_core2.executeHandlerPipeline)(resolved, httpRequest, options);
268
- return mapHttpResponseToResult(httpResponse);
640
+ return cachedHandler(event, context);
269
641
  }
270
642
  __name(handler, "handler");
271
643
  // Annotate the CommonJS export names for ESM import in node:
272
644
  0 && (module.exports = {
645
+ ApiGatewayWebSocketSender,
273
646
  AwsLambdaAdapter,
647
+ detectEventType,
274
648
  handler,
275
649
  mapApiGatewayV2Event,
276
- mapHttpResponseToResult
650
+ mapApiGatewayWebSocketEvent,
651
+ mapConsumerResultToSqsBatchResponse,
652
+ mapEventBridgeEvent,
653
+ mapHttpResponseToResult,
654
+ mapSqsEvent
277
655
  });
278
656
  //# sourceMappingURL=index.cjs.map