Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
app.post('/trigger/:template/:data', async function (req, httpResponse, next) {
console.log('Template: ' + req.params.template);
console.log('Clause: ' + req.params.data);
try {
const template = await Template.fromDirectory(`${process.env.CICERO_DIR}/${req.params.template}`);
const data = fs.readFileSync(`${process.env.CICERO_DIR}/${req.params.template}/${req.params.data}`);
const clause = new Clause(template);
if(req.params.data.endsWith('.json')) {
clause.setData(JSON.parse(data.toString()));
}
else {
clause.parse(data.toString());
}
const engine = new Engine();
let result;
if(Object.keys(req.body).length === 2 &&
Object.prototype.hasOwnProperty.call(req.body,'request') &&
Object.prototype.hasOwnProperty.call(req.body,'state')) {
result = await engine.trigger(clause, req.body.request, req.body.state);
} else {
// Add empty state in input, remove it on output
const state = { '$class' : 'org.accordproject.cicero.contract.AccordContractState', 'stateId' : 'ehlo' };
result = await engine.trigger(clause, req.body, state);
delete result.state;
}
httpResponse.send(result);
}
catch(err) {
return next(err);
}
static invoke(templatePath, samplePath, clauseName, paramsPath, statePath, currentTime, options) {
let clause;
const sampleText = fs.readFileSync(samplePath, 'utf8');
const paramsJson = JSON.parse(fs.readFileSync(paramsPath, 'utf8'));
const engine = new Engine();
return Commands.loadTemplate(templatePath, options)
.then(async (template) => {
// Initialize clause
clause = new Clause(template);
clause.parse(sampleText, currentTime);
let stateJson;
if(!fs.existsSync(statePath)) {
Logger.warn('A state file was not provided, initializing state. Try the --state flag or create a state.json in the root folder of your template.');
const initResult = await engine.init(clause, currentTime);
stateJson = initResult.state;
} else {
stateJson = JSON.parse(fs.readFileSync(statePath, 'utf8'));
}
return engine.invoke(clause, clauseName, paramsJson, stateJson, currentTime);
static trigger(templatePath, samplePath, requestsPath, statePath, currentTime, options) {
let clause;
const sampleText = fs.readFileSync(samplePath, 'utf8');
let requestsJson = [];
for (let i = 0; i < requestsPath.length; i++) {
requestsJson.push(JSON.parse(fs.readFileSync(requestsPath[i], 'utf8')));
}
const engine = new Engine();
return Commands.loadTemplate(templatePath, options)
.then(async (template) => {
// Initialize clause
clause = new Clause(template);
clause.parse(sampleText, currentTime);
let stateJson;
if(!fs.existsSync(statePath)) {
Logger.warn('A state file was not provided, initializing state. Try the --state flag or create a state.json in the root folder of your template.');
const initResult = await engine.init(clause, currentTime);
stateJson = initResult.state;
} else {
stateJson = JSON.parse(fs.readFileSync(statePath, 'utf8'));
}
// First execution to get the initial response
static initialize(templatePath, samplePath, currentTime, options) {
let clause;
const sampleText = fs.readFileSync(samplePath, 'utf8');
const engine = new Engine();
return Commands.loadTemplate(templatePath, options)
.then((template) => {
// Initialize clause
clause = new Clause(template);
clause.parse(sampleText, currentTime);
return engine.init(clause, currentTime);
})
.catch((err) => {
Logger.error(err.message);
});
}
Before(function () {
setDefaultTimeout(40 * 1000);
this.engine = new Engine();
this.currentTime = '1970-01-01T00:00:00Z';
this.state = null;
this.clause = null;
this.request = null;
this.answer = null;
});