UNPKG

@angstone/monostone

Version:

monolitic event-sourced framework

114 lines 4.65 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const error_1 = require("../error"); const _1 = require("./"); const sanitizeHtml = require("sanitize-html"); const EVENT_REDUCE_TIMEOUT = +(process.env.EVENT_REDUCE_TIMEOUT || 3000); function execute(command, request, eventReduced$) { return __awaiter(this, void 0, void 0, function* () { request = sanitizeRequest(request); let ruleBroken; if (command.rule) { if (command.rule.preValidation) { ruleBroken = yield command.rule.preValidation(request); if (ruleBroken) { return Promise.reject(ruleBroken); } } if (command.rule.validation) { const ruleBrokenOrNewRequest = yield command.rule.validation(request); if (ruleBrokenOrNewRequest) { if (typeof ruleBrokenOrNewRequest === "string") { return Promise.reject(ruleBrokenOrNewRequest); } else { request = ruleBrokenOrNewRequest.req; } } } } const eventNumber = yield _1.EventTools.send({ command, request }); if (command.rule && command.rule.respond) { yield new Promise((resolve, reject) => { const resolveListener = (eventRead) => { if (eventRead.eventNumber === eventNumber) { resolve(); eventReduced$.removeListener(command.commandType, resolveListener); } }; eventReduced$.addListener(command.commandType, resolveListener); setTimeout(() => { eventReduced$.removeListener(command.commandType, resolveListener); reject("Server was unable to reduce in time"); }, EVENT_REDUCE_TIMEOUT); }); return yield command.rule.respond(eventNumber, request); } else { return { eventNumber }; } }); } exports.execute = execute; function createCommand(commandRecipe) { let ruleSheet; try { ruleSheet = require(commandRecipe.featurePath + "/commands/" + commandRecipe.commandName + "/" + commandRecipe.commandName + ".rule").rule; } catch (e) { error_1.error.fatal(e, "failed to load ruleSheet for command " + commandRecipe.commandName); } if (!ruleSheet) { error_1.error.fatal("failed to load ruleSheet for command " + commandRecipe.commandName); } const rule = _1.RuleTools.loadRule(ruleSheet); const reducer = _1.ReducerTools.loadReducer({ commandName: commandRecipe.commandName, featurePath: commandRecipe.featurePath, }); return { commandType: commandRecipe.featureName + " " + commandRecipe.commandName, featureName: commandRecipe.featureName, commandName: commandRecipe.commandName, rule, reducer }; } exports.createCommand = createCommand; function createCommands(commandsRecipe) { if (commandsRecipe.commandNames.length == 0) { try { commandsRecipe.commandNames = _1.FolderTools.getDirectories(commandsRecipe.featurePath + '/commands').map(_1.FolderTools.lastNameOfFilePath); } catch (e) { } } const commands = []; commandsRecipe.commandNames.forEach((commandName, index) => { commands[index] = createCommand({ commandName, featureName: commandsRecipe.featureName, featurePath: commandsRecipe.featurePath }); }); return commands; } exports.createCommands = createCommands; function sanitizeRequest(req) { Object.keys(req).forEach((key) => { if (key != 'token') req[key] = sanitizeHtml(req[key]); }); return req; } exports.sanitizeRequest = sanitizeRequest; //# sourceMappingURL=command.tools.js.map