@kirkelliott/zap 0.1.22 → 0.1.24
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 +18 -7
- package/package.json +1 -1
package/dist/handler.js
CHANGED
|
@@ -6,17 +6,26 @@ const eval_1 = require("./eval");
|
|
|
6
6
|
const types_1 = require("./types");
|
|
7
7
|
const s3 = new client_s3_1.S3Client({});
|
|
8
8
|
const BUCKET = process.env.ZAP_BUCKET;
|
|
9
|
+
const TTL = 5_000;
|
|
10
|
+
const cache = new Map();
|
|
9
11
|
const loader = async (name) => {
|
|
12
|
+
const hit = cache.get(name);
|
|
13
|
+
if (hit && Date.now() - hit.at < TTL)
|
|
14
|
+
return hit.source;
|
|
10
15
|
const { Body } = await s3.send(new client_s3_1.GetObjectCommand({ Bucket: BUCKET, Key: `${name}.zap` }));
|
|
11
|
-
|
|
16
|
+
const source = await Body.transformToString();
|
|
17
|
+
cache.set(name, { source, at: Date.now() });
|
|
18
|
+
return source;
|
|
12
19
|
};
|
|
13
|
-
const handler = async (event) => {
|
|
20
|
+
const handler = async (event, context) => {
|
|
21
|
+
const start = Date.now();
|
|
14
22
|
// Cron invocation from EventBridge
|
|
15
23
|
if (event?.zap?.cron) {
|
|
16
24
|
try {
|
|
17
25
|
const source = await loader(event.zap.cron);
|
|
18
26
|
const fn = (0, eval_1.evalModule)(source, loader);
|
|
19
27
|
await fn();
|
|
28
|
+
console.log(JSON.stringify({ type: 'cron', handler: event.zap.cron, ms: Date.now() - start, requestId: context.awsRequestId }));
|
|
20
29
|
}
|
|
21
30
|
catch (err) {
|
|
22
31
|
console.error(`cron error [${event.zap.cron}]:`, err.message);
|
|
@@ -32,16 +41,18 @@ const handler = async (event) => {
|
|
|
32
41
|
headers: e.headers,
|
|
33
42
|
body: e.body ?? null,
|
|
34
43
|
};
|
|
44
|
+
const key = req.path === '/' ? 'index' : req.path.replace(/^\//, '');
|
|
35
45
|
try {
|
|
36
|
-
const key = req.path === '/' ? 'index' : req.path.replace(/^\//, '');
|
|
37
46
|
const source = await loader(key);
|
|
38
47
|
const res = await (0, eval_1.run)(source, req, loader);
|
|
39
|
-
|
|
48
|
+
const status = res.status ?? 200;
|
|
49
|
+
console.log(JSON.stringify({ handler: key, method: req.method, status, ms: Date.now() - start, requestId: context.awsRequestId }));
|
|
50
|
+
return { statusCode: status, headers: res.headers, body: (0, types_1.serialize)(res.body) };
|
|
40
51
|
}
|
|
41
52
|
catch (err) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return { statusCode:
|
|
53
|
+
const status = err.name === 'NoSuchKey' ? 404 : 500;
|
|
54
|
+
console.log(JSON.stringify({ handler: key, method: req.method, status, ms: Date.now() - start, requestId: context.awsRequestId }));
|
|
55
|
+
return { statusCode: status, body: status === 404 ? `No handler for ${req.path}` : err.message };
|
|
45
56
|
}
|
|
46
57
|
};
|
|
47
58
|
exports.handler = handler;
|