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