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 packageJson = fs.readFileSync(file, 'utf8');
const pkgJson = JSON.parse(packageJson);
if (pkgJson.name !== selectedTemplate) {
selected = false;
}
}
if (selected) {
// read the parent directory as a template
const templatePath = path.dirname(file);
console.log(`Processing ${templatePath}`);
const dest = templatePath.replace('/node_modules/@accordproject/cicero-template-library/src/', '/static/');
await fs.ensureDir(archiveDir);
try {
const template = await Template.fromDirectory(templatePath);
const templateName = template.getMetadata().getName();
if (!process.env.SKIP_GENERATION && (templateName === 'helloworld' || templateName === 'empty' || templateName === 'empty-contract')) {
const language = template.getMetadata().getLanguage();
let archive;
// Only produce Ergo archives
if (language === 0) {
archive = await template.toArchive('ergo');
const destPath = path.dirname(dest);
await fs.ensureDir(destPath);
const archiveFileName = `${template.getIdentifier()}.cta`;
const archiveFilePath = `${archiveDir}/${archiveFileName}`;
await writeFile(archiveFilePath, archive);
console.log(`Copied: ${archiveFileName}`);
} else {
async function loadClause(templateDir) {
const template = await Template.fromDirectory(templateDir);
return new Clause(template);
}
static loadTemplate(templatePath, options) {
if (Commands.isTemplateArchive(templatePath)) {
const buffer = fs.readFileSync(templatePath);
return Template.fromArchive(buffer, options);
} else {
return Template.fromDirectory(templatePath, options);
}
}