@kirkelliott/zap 0.1.22 → 0.1.23
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/handler.js +10 -6
- package/package.json +1 -1
package/dist/handler.js
CHANGED
|
@@ -10,13 +10,15 @@ const loader = async (name) => {
|
|
|
10
10
|
const { Body } = await s3.send(new client_s3_1.GetObjectCommand({ Bucket: BUCKET, Key: `${name}.zap` }));
|
|
11
11
|
return Body.transformToString();
|
|
12
12
|
};
|
|
13
|
-
const handler = async (event) => {
|
|
13
|
+
const handler = async (event, context) => {
|
|
14
|
+
const start = Date.now();
|
|
14
15
|
// Cron invocation from EventBridge
|
|
15
16
|
if (event?.zap?.cron) {
|
|
16
17
|
try {
|
|
17
18
|
const source = await loader(event.zap.cron);
|
|
18
19
|
const fn = (0, eval_1.evalModule)(source, loader);
|
|
19
20
|
await fn();
|
|
21
|
+
console.log(JSON.stringify({ type: 'cron', handler: event.zap.cron, ms: Date.now() - start, requestId: context.awsRequestId }));
|
|
20
22
|
}
|
|
21
23
|
catch (err) {
|
|
22
24
|
console.error(`cron error [${event.zap.cron}]:`, err.message);
|
|
@@ -32,16 +34,18 @@ const handler = async (event) => {
|
|
|
32
34
|
headers: e.headers,
|
|
33
35
|
body: e.body ?? null,
|
|
34
36
|
};
|
|
37
|
+
const key = req.path === '/' ? 'index' : req.path.replace(/^\//, '');
|
|
35
38
|
try {
|
|
36
|
-
const key = req.path === '/' ? 'index' : req.path.replace(/^\//, '');
|
|
37
39
|
const source = await loader(key);
|
|
38
40
|
const res = await (0, eval_1.run)(source, req, loader);
|
|
39
|
-
|
|
41
|
+
const status = res.status ?? 200;
|
|
42
|
+
console.log(JSON.stringify({ handler: key, method: req.method, status, ms: Date.now() - start, requestId: context.awsRequestId }));
|
|
43
|
+
return { statusCode: status, headers: res.headers, body: (0, types_1.serialize)(res.body) };
|
|
40
44
|
}
|
|
41
45
|
catch (err) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return { statusCode:
|
|
46
|
+
const status = err.name === 'NoSuchKey' ? 404 : 500;
|
|
47
|
+
console.log(JSON.stringify({ handler: key, method: req.method, status, ms: Date.now() - start, requestId: context.awsRequestId }));
|
|
48
|
+
return { statusCode: status, body: status === 404 ? `No handler for ${req.path}` : err.message };
|
|
45
49
|
}
|
|
46
50
|
};
|
|
47
51
|
exports.handler = handler;
|