support for execution of async functions + integration with backend through server.exec()

This commit is contained in:
azivner 2018-01-23 21:59:30 -05:00
parent 27cb6b1c4d
commit f439969962
4 changed files with 32 additions and 3 deletions

View file

@ -196,11 +196,11 @@ const noteEditor = (function() {
return currentNote ? currentNote.detail.type : null;
}
function executeScript() {
async function executeScript() {
if (getCurrentNoteType() === 'code') {
const script = codeEditor.getValue();
eval(script);
eval("(async function() {" + script + "})()");
}
}

View file

@ -29,6 +29,14 @@ const server = (function() {
return await call('DELETE', url);
}
async function exec(script) {
if (typeof script === "function") {
script = script.toString();
}
return await post('script/exec', { script: script });
}
let i = 1;
const reqResolves = {};
@ -92,6 +100,6 @@ const server = (function() {
post,
put,
remove,
getHeaders
exec
}
})();

19
routes/api/script.js Normal file
View file

@ -0,0 +1,19 @@
"use strict";
const express = require('express');
const router = express.Router();
const auth = require('../../services/auth');
const wrap = require('express-promise-wrap').wrap;
const log = require('../../services/log');
router.post('/exec', auth.checkApiAuth, wrap(async (req, res, next) => {
log.info('Executing script: ' + req.body.script);
const ret = await eval("(" + req.body.script + ")()");
log.info('Execution result: ' + ret);
res.send(ret);
}));
module.exports = router;

View file

@ -27,6 +27,7 @@ const anonymizationRoute = require('./api/anonymization');
const cleanupRoute = require('./api/cleanup');
const imageRoute = require('./api/image');
const attributesRoute = require('./api/attributes');
const scriptRoute = require('./api/script');
function register(app) {
app.use('/', indexRoute);
@ -57,6 +58,7 @@ function register(app) {
app.use('/api/anonymization', anonymizationRoute);
app.use('/api/cleanup', cleanupRoute);
app.use('/api/images', imageRoute);
app.use('/api/script', scriptRoute);
}
module.exports = {