UNPKG

@getanthill/datastore

Version:

Event-Sourced Datastore

97 lines 3.91 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 = aggregate; const node_assert_1 = require("node: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) => { var _a, _b, _c; const publicKey = (_b = (0, get_1.default)(data, (_a = step.public_key) !== null && _a !== void 0 ? _a : 'public_key')) !== null && _b !== void 0 ? _b : 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 = (_c = step.destination) !== null && _c !== void 0 ? _c : 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, node_assert_1.ok)(!!c.name, 'Missing Datastore configuration name'); return [ c.name, new sdk_1.Datastore({ ...c.config, telemetry: services.telemetry, timeout: Number.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 === null || aggregator === void 0 ? void 0 : aggregator.logs; return next(err); } if (sdk_1.Aggregator.ERRORS.includes(err)) { err.status = 422; /* @ts-ignore */ err.details = aggregator === null || aggregator === void 0 ? void 0 : aggregator.logs; return next(err); } next(err); } }; } //# sourceMappingURL=controllers.js.map