feat: serverless examples (#11)

This commit is contained in:
Ivan Miletic
2023-04-06 16:57:03 +02:00
committed by GitHub
parent d9a8e0ed7d
commit 2bebcd6a99
5 changed files with 74 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
# NodeJS + Lambda + S3 Serverless Rules Engine
Requirements:
* ZenEngineNodeJS Lambda Layer
+43
View File
@@ -0,0 +1,43 @@
import { ZenEngine } from '@gorules/zen-engine';
import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
const client = new S3Client({});
const { BUCKET_NAME } = process.env;
const streamToBuffer = (stream) => new Promise((resolve, reject) => {
const chunks = [];
stream.on('data', (chunk) => chunks.push(chunk));
stream.on('error', reject);
stream.on('end', () => resolve(Buffer.concat(chunks)));
});
const loader = async (key) => {
try {
const params = {
Bucket: BUCKET_NAME,
Key: key,
};
const command = new GetObjectCommand(params);
const response = await client.send(command);
const { Body } = response;
return streamToBuffer(Body);
} catch (e) {
console.error(e);
}
};
export const handler = async(event) => {
const { key, context } = event;
const engine = new ZenEngine({
loader
});
const decision = await engine.getDecision(key);
const result = await decision.evaluate(context);
return {
statusCode: 200,
body: result,
};
};