2018-01-28 06:18:19 +08:00
|
|
|
const sql = require('./sql');
|
|
|
|
const ScriptContext = require('./script_context');
|
2018-03-31 21:07:58 +08:00
|
|
|
const repository = require('./repository');
|
2018-01-27 12:40:48 +08:00
|
|
|
|
2018-03-31 20:53:52 +08:00
|
|
|
async function executeNote(note) {
|
2018-03-07 12:04:35 +08:00
|
|
|
if (!note.isJavaScript()) {
|
2018-03-03 09:56:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-05 01:06:35 +08:00
|
|
|
const bundle = await getScriptBundle(note);
|
2018-03-04 23:32:53 +08:00
|
|
|
|
2018-03-31 20:53:52 +08:00
|
|
|
await executeBundle(bundle);
|
2018-03-07 12:04:35 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 20:53:52 +08:00
|
|
|
async function executeBundle(bundle, startNote) {
|
2018-03-07 12:04:35 +08:00
|
|
|
if (!startNote) {
|
|
|
|
// this is the default case, the only exception is when we want to preserve frontend startNote
|
|
|
|
startNote = bundle.note;
|
|
|
|
}
|
|
|
|
|
2018-03-04 23:32:53 +08:00
|
|
|
// last \r\n is necessary if script contains line comment on its last line
|
2018-03-05 01:06:35 +08:00
|
|
|
const script = "async function() {\r\n" + bundle.script + "\r\n}";
|
2018-03-04 23:32:53 +08:00
|
|
|
|
2018-03-31 20:53:52 +08:00
|
|
|
const ctx = new ScriptContext(startNote, bundle.allNotes);
|
2018-03-04 23:32:53 +08:00
|
|
|
|
2018-04-03 09:56:55 +08:00
|
|
|
if (await bundle.note.hasLabel('manualTransactionHandling')) {
|
2018-03-04 23:32:53 +08:00
|
|
|
return await execute(ctx, script, '');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return await sql.doInTransaction(async () => execute(ctx, script, ''));
|
|
|
|
}
|
2018-03-03 09:56:58 +08:00
|
|
|
}
|
|
|
|
|
2018-03-07 12:04:35 +08:00
|
|
|
/**
|
|
|
|
* This method preserves frontend startNode - that's why we start execution from currentNote and override
|
|
|
|
* bundle's startNote.
|
|
|
|
*/
|
2018-03-31 20:53:52 +08:00
|
|
|
async function executeScript(script, params, startNoteId, currentNoteId) {
|
2018-03-05 12:28:26 +08:00
|
|
|
const startNote = await repository.getNote(startNoteId);
|
2018-03-07 12:04:35 +08:00
|
|
|
const currentNote = await repository.getNote(currentNoteId);
|
2018-03-05 12:28:26 +08:00
|
|
|
|
2018-03-07 13:17:18 +08:00
|
|
|
currentNote.content = `return await (${script}\r\n)(${getParams(params)})`;
|
|
|
|
currentNote.type = 'code';
|
2018-03-07 12:04:35 +08:00
|
|
|
currentNote.mime = 'application/javascript;env=backend';
|
2018-01-27 12:40:48 +08:00
|
|
|
|
2018-03-07 12:04:35 +08:00
|
|
|
const bundle = await getScriptBundle(currentNote);
|
|
|
|
|
2018-03-31 20:53:52 +08:00
|
|
|
return await executeBundle(bundle, startNote);
|
2018-02-25 11:44:45 +08:00
|
|
|
}
|
2018-01-27 12:40:48 +08:00
|
|
|
|
2018-02-25 11:44:45 +08:00
|
|
|
async function execute(ctx, script, paramsStr) {
|
2018-03-07 12:04:35 +08:00
|
|
|
return await (function() { return eval(`const apiContext = this;\r\n(${script}\r\n)(${paramsStr})`); }.call(ctx));
|
2018-01-27 12:40:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function getParams(params) {
|
2018-02-25 03:42:52 +08:00
|
|
|
if (!params) {
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2018-02-18 22:53:36 +08:00
|
|
|
return params.map(p => {
|
|
|
|
if (typeof p === "string" && p.startsWith("!@#Function: ")) {
|
|
|
|
return p.substr(13);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return JSON.stringify(p);
|
|
|
|
}
|
|
|
|
}).join(",");
|
2018-01-27 12:40:48 +08:00
|
|
|
}
|
|
|
|
|
2018-03-07 13:17:18 +08:00
|
|
|
async function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds = []) {
|
2018-03-05 10:05:14 +08:00
|
|
|
if (!note.isJavaScript() && !note.isHtml() && note.type !== 'render') {
|
2018-03-05 01:06:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-03-04 23:32:53 +08:00
|
|
|
|
2018-04-03 09:56:55 +08:00
|
|
|
if (!root && await note.hasLabel('disableInclusion')) {
|
2018-03-05 11:09:51 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-07 13:17:18 +08:00
|
|
|
if (root) {
|
2018-03-06 12:09:36 +08:00
|
|
|
scriptEnv = note.getScriptEnv();
|
|
|
|
}
|
|
|
|
|
2018-03-07 12:04:35 +08:00
|
|
|
if (note.type !== 'file' && scriptEnv !== note.getScriptEnv()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-05 01:06:35 +08:00
|
|
|
const bundle = {
|
|
|
|
note: note,
|
|
|
|
script: '',
|
2018-03-05 10:05:14 +08:00
|
|
|
html: '',
|
2018-03-05 01:06:35 +08:00
|
|
|
allNotes: [note]
|
|
|
|
};
|
2018-03-03 22:11:41 +08:00
|
|
|
|
2018-03-05 01:06:35 +08:00
|
|
|
if (includedNoteIds.includes(note.noteId)) {
|
|
|
|
return bundle;
|
|
|
|
}
|
2018-03-03 22:11:41 +08:00
|
|
|
|
2018-03-05 01:06:35 +08:00
|
|
|
includedNoteIds.push(note.noteId);
|
2018-03-04 23:32:53 +08:00
|
|
|
|
2018-03-05 01:06:35 +08:00
|
|
|
const modules = [];
|
2018-03-03 22:11:41 +08:00
|
|
|
|
2018-04-01 11:08:22 +08:00
|
|
|
for (const child of await note.getChildNotes()) {
|
2018-03-07 13:17:18 +08:00
|
|
|
const childBundle = await getScriptBundle(child, false, scriptEnv, includedNoteIds);
|
2018-03-04 23:32:53 +08:00
|
|
|
|
2018-03-05 01:06:35 +08:00
|
|
|
if (childBundle) {
|
|
|
|
modules.push(childBundle.note);
|
|
|
|
bundle.script += childBundle.script;
|
2018-03-05 10:05:14 +08:00
|
|
|
bundle.html += childBundle.html;
|
2018-03-05 01:06:35 +08:00
|
|
|
bundle.allNotes = bundle.allNotes.concat(childBundle.allNotes);
|
|
|
|
}
|
2018-03-03 22:11:41 +08:00
|
|
|
}
|
|
|
|
|
2018-03-11 00:53:51 +08:00
|
|
|
const moduleNoteIds = modules.map(mod => mod.noteId);
|
|
|
|
|
2018-03-05 10:05:14 +08:00
|
|
|
if (note.isJavaScript()) {
|
|
|
|
bundle.script += `
|
2018-03-07 12:04:35 +08:00
|
|
|
apiContext.modules['${note.noteId}'] = {};
|
2018-03-11 00:53:51 +08:00
|
|
|
${root ? 'return ' : ''}await (async function(exports, module, require, api` + (modules.length > 0 ? ', ' : '') +
|
2018-03-07 12:04:35 +08:00
|
|
|
modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) {
|
2018-03-05 01:06:35 +08:00
|
|
|
${note.content}
|
2018-03-11 00:53:51 +08:00
|
|
|
})({}, apiContext.modules['${note.noteId}'], apiContext.require(${JSON.stringify(moduleNoteIds)}), apiContext.apis['${note.noteId}']` + (modules.length > 0 ? ', ' : '') +
|
2018-03-07 12:04:35 +08:00
|
|
|
modules.map(mod => `apiContext.modules['${mod.noteId}'].exports`).join(', ') + `);
|
2018-03-05 01:06:35 +08:00
|
|
|
`;
|
2018-03-05 10:05:14 +08:00
|
|
|
}
|
|
|
|
else if (note.isHtml()) {
|
|
|
|
bundle.html += note.content;
|
|
|
|
}
|
2018-03-04 23:32:53 +08:00
|
|
|
|
2018-03-05 01:06:35 +08:00
|
|
|
return bundle;
|
2018-03-03 22:11:41 +08:00
|
|
|
}
|
|
|
|
|
2018-03-07 12:04:35 +08:00
|
|
|
function sanitizeVariableName(str) {
|
|
|
|
return str.replace(/[^a-z0-9_]/gim, "");
|
|
|
|
}
|
|
|
|
|
2018-01-27 12:40:48 +08:00
|
|
|
module.exports = {
|
2018-03-03 09:56:58 +08:00
|
|
|
executeNote,
|
2018-02-25 03:42:52 +08:00
|
|
|
executeScript,
|
2018-03-07 13:17:18 +08:00
|
|
|
getScriptBundle
|
2018-01-27 12:40:48 +08:00
|
|
|
};
|