mirror of
https://github.com/gorules/zen.git
synced 2026-10-04 16:02:18 +00:00
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
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,
|
|
};
|
|
};
|