UNPKG

@getanthill/datastore

Version:

Event-Sourced Datastore

97 lines 3.67 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.aggregate = void 0; const assert_1 = require("assert"); const get_1 = __importDefault(require("lodash/get")); const set_1 = __importDefault(require("lodash/set")); const sdk_1 = require("../../sdk"); /** * @alpha * * Aggregation route * * @param services Services */ function aggregate(services) { const fheStep = async (step, data) => { const publicKey = (0, get_1.default)(data, step.public_key ?? 'public_key') ?? step.public_key; const another = await services.fhe.clone(); await another.connect(); const UploadedPublicKey = another.seal.PublicKey(); UploadedPublicKey.load(another.context, publicKey); another.keys.public = UploadedPublicKey; const asyncFn = another.compile(step.script); const args = step.args.map((arg) => (0, get_1.default)(data, arg)); const res = await another.compute(asyncFn, ...args); const destination = step.destination ?? step.type; (0, set_1.default)(data, destination, res); return data; }; return async (req, res, next) => { let aggregator; try { services.telemetry.logger.debug('[aggregate] Datastores initialization...'); const datastores = new Map(services.config.datastores.map((c) => { (0, assert_1.ok)(!!c.name, 'Missing Datastore configuration name'); return [ c.name, new sdk_1.Datastore({ ...c.config, telemetry: services.telemetry, timeout: parseInt(req.header('timeout') || '30000', 10), token: req.header('Authorization') || '', }), ]; })); aggregator = new sdk_1.Aggregator(datastores); if (services.config.features.fhe.isEnabled === true) { aggregator.addStepType('fhe', { handler: fheStep, }); } let pipeline = []; let initialData = {}; if (Array.isArray(req.body)) { pipeline = req.body; } else { pipeline = req.body.pipeline; initialData = req.body.data; } services.telemetry.logger.debug('[aggregate] Starting the aggregation', { steps_count: pipeline.length, }); const aggregation = await aggregator.aggregate(pipeline, initialData); if (req.header('logs') === 'true') { // @ts-ignore res.body = { aggregation, logs: aggregator.logs }; } else { // @ts-ignore res.body = aggregation; } // @ts-ignore res.json(res.body); } catch (err) { if (err === sdk_1.Aggregator.ERROR_INVALID_PIPELINE_DEFINITION) { err.status = 400; /* @ts-ignore */ err.details = aggregator?.logs; return next(err); } if (sdk_1.Aggregator.ERRORS.includes(err)) { err.status = 422; /* @ts-ignore */ err.details = aggregator?.logs; return next(err); } next(err); } }; } exports.aggregate = aggregate; //# sourceMappingURL=controllers.js.map