| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  | const sql = require('./sql'); | 
					
						
							|  |  |  | const ScriptContext = require('./script_context'); | 
					
						
							| 
									
										
										
										
											2018-03-31 09:07:58 -04:00
										 |  |  | const repository = require('./repository'); | 
					
						
							| 
									
										
										
										
											2018-05-22 23:51:13 -04:00
										 |  |  | const cls = require('./cls'); | 
					
						
							|  |  |  | const sourceIdService = require('./source_id'); | 
					
						
							| 
									
										
										
										
											2018-08-17 11:31:42 +02:00
										 |  |  | const log = require('./log'); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 12:28:20 +01:00
										 |  |  | async function executeNote(note, apiParams) { | 
					
						
							| 
									
										
										
										
											2019-01-16 22:52:32 +01:00
										 |  |  |     if (!note.isJavaScript() || note.getScriptEnv() !== 'backend' || !note.isContentAvailable) { | 
					
						
							| 
									
										
										
										
											2019-03-20 22:28:54 +01:00
										 |  |  |         log.info(`Cannot execute note ${note.noteId}`); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-02 20:56:58 -05:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |     const bundle = await getScriptBundle(note); | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-20 22:28:54 +01:00
										 |  |  |     return await executeBundle(bundle, apiParams); | 
					
						
							| 
									
										
										
										
											2018-03-06 23:04:35 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-03 11:15:32 +01:00
										 |  |  | async function executeNoteNoException(note, apiParams) { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |         await executeNote(note, apiParams); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							|  |  |  |         // just swallow, exception is logged already in executeNote
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 12:28:20 +01:00
										 |  |  | async function executeBundle(bundle, apiParams = {}) { | 
					
						
							|  |  |  |     if (!apiParams.startNote) { | 
					
						
							| 
									
										
										
										
											2018-03-06 23:04:35 -05:00
										 |  |  |         // this is the default case, the only exception is when we want to preserve frontend startNote
 | 
					
						
							| 
									
										
										
										
											2019-01-27 12:28:20 +01:00
										 |  |  |         apiParams.startNote = bundle.note; | 
					
						
							| 
									
										
										
										
											2018-03-06 23:04:35 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  |     // last \r\n is necessary if script contains line comment on its last line
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |     const script = "async function() {\r\n" + bundle.script + "\r\n}"; | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 12:28:20 +01:00
										 |  |  |     const ctx = new ScriptContext(bundle.allNotes, apiParams); | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-17 11:31:42 +02:00
										 |  |  |     try { | 
					
						
							|  |  |  |         if (await bundle.note.hasLabel('manualTransactionHandling')) { | 
					
						
							| 
									
										
										
										
											2019-01-27 12:28:20 +01:00
										 |  |  |             return await execute(ctx, script); | 
					
						
							| 
									
										
										
										
											2018-08-17 11:31:42 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2019-01-27 12:28:20 +01:00
										 |  |  |             return await sql.transactional(async () => await execute(ctx, script)); | 
					
						
							| 
									
										
										
										
											2018-08-17 11:31:42 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-17 11:31:42 +02:00
										 |  |  |     catch (e) { | 
					
						
							|  |  |  |         log.error(`Execution of script "${bundle.note.title}" (${bundle.note.noteId}) failed with error: ${e.message}`); | 
					
						
							| 
									
										
										
										
											2019-02-03 11:15:32 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         throw e; | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-02 20:56:58 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-06 23:04:35 -05:00
										 |  |  | /** | 
					
						
							|  |  |  |  * This method preserves frontend startNode - that's why we start execution from currentNote and override | 
					
						
							|  |  |  |  * bundle's startNote. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-08-10 13:30:20 +02:00
										 |  |  | async function executeScript(script, params, startNoteId, currentNoteId, originEntityName, originEntityId) { | 
					
						
							| 
									
										
										
										
											2018-03-04 23:28:26 -05:00
										 |  |  |     const startNote = await repository.getNote(startNoteId); | 
					
						
							| 
									
										
										
										
											2019-03-26 22:24:04 +01:00
										 |  |  |     const currentNote = await repository.getNote(currentNoteId); | 
					
						
							| 
									
										
										
										
											2018-08-10 13:30:20 +02:00
										 |  |  |     const originEntity = await repository.getEntityFromName(originEntityName, originEntityId); | 
					
						
							| 
									
										
										
										
											2018-03-04 23:28:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-26 22:24:04 +01:00
										 |  |  |     currentNote.content = `return await (${script}\r\n)(${getParams(params)})`; | 
					
						
							| 
									
										
										
										
											2018-03-07 00:17:18 -05:00
										 |  |  |     currentNote.type = 'code'; | 
					
						
							| 
									
										
										
										
											2018-03-06 23:04:35 -05:00
										 |  |  |     currentNote.mime = 'application/javascript;env=backend'; | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-06 23:04:35 -05:00
										 |  |  |     const bundle = await getScriptBundle(currentNote); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 21:17:34 +01:00
										 |  |  |     return await executeBundle(bundle, { startNote, originEntity }); | 
					
						
							| 
									
										
										
										
											2018-02-24 22:44:45 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 22:18:27 +01:00
										 |  |  | async function execute(ctx, script) { | 
					
						
							| 
									
										
										
										
											2019-01-27 12:28:20 +01:00
										 |  |  |     return await (function() { return eval(`const apiContext = this;\r\n(${script}\r\n)()`); }.call(ctx)); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getParams(params) { | 
					
						
							| 
									
										
										
										
											2018-02-24 14:42:52 -05:00
										 |  |  |     if (!params) { | 
					
						
							|  |  |  |         return params; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 09:53:36 -05:00
										 |  |  |     return params.map(p => { | 
					
						
							|  |  |  |         if (typeof p === "string" && p.startsWith("!@#Function: ")) { | 
					
						
							|  |  |  |             return p.substr(13); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             return JSON.stringify(p); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }).join(","); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 12:16:05 +01:00
										 |  |  | async function getScriptBundleForFrontend(note) { | 
					
						
							|  |  |  |     const bundle = await getScriptBundle(note); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // for frontend we return just noteIds because frontend needs to use its own entity instances
 | 
					
						
							|  |  |  |     bundle.noteId = bundle.note.noteId; | 
					
						
							|  |  |  |     delete bundle.note; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bundle.allNoteIds = bundle.allNotes.map(note => note.noteId); | 
					
						
							|  |  |  |     delete bundle.allNotes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return bundle; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-07 00:17:18 -05:00
										 |  |  | async function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds = []) { | 
					
						
							| 
									
										
										
										
											2019-01-08 21:21:49 +01:00
										 |  |  |     if (!note.isContentAvailable) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-26 19:27:47 -04:00
										 |  |  |     if (!note.isJavaScript() && !note.isHtml()) { | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 21:56:55 -04:00
										 |  |  |     if (!root && await note.hasLabel('disableInclusion')) { | 
					
						
							| 
									
										
										
										
											2018-03-04 22:09:51 -05:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-07 00:17:18 -05:00
										 |  |  |     if (root) { | 
					
						
							| 
									
										
										
										
											2018-03-05 23:09:36 -05:00
										 |  |  |         scriptEnv = note.getScriptEnv(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-06 23:04:35 -05:00
										 |  |  |     if (note.type !== 'file' && scriptEnv !== note.getScriptEnv()) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |     const bundle = { | 
					
						
							|  |  |  |         note: note, | 
					
						
							|  |  |  |         script: '', | 
					
						
							| 
									
										
										
										
											2018-03-04 21:05:14 -05:00
										 |  |  |         html: '', | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |         allNotes: [note] | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2018-03-03 09:11:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |     if (includedNoteIds.includes(note.noteId)) { | 
					
						
							|  |  |  |         return bundle; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-03 09:11:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |     includedNoteIds.push(note.noteId); | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |     const modules = []; | 
					
						
							| 
									
										
										
										
											2018-03-03 09:11:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-31 23:08:22 -04:00
										 |  |  |     for (const child of await note.getChildNotes()) { | 
					
						
							| 
									
										
										
										
											2018-03-07 00:17:18 -05:00
										 |  |  |         const childBundle = await getScriptBundle(child, false, scriptEnv, includedNoteIds); | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |         if (childBundle) { | 
					
						
							|  |  |  |             modules.push(childBundle.note); | 
					
						
							|  |  |  |             bundle.script += childBundle.script; | 
					
						
							| 
									
										
										
										
											2018-03-04 21:05:14 -05:00
										 |  |  |             bundle.html += childBundle.html; | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |             bundle.allNotes = bundle.allNotes.concat(childBundle.allNotes); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-03 09:11:41 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-10 11:53:51 -05:00
										 |  |  |     const moduleNoteIds = modules.map(mod => mod.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 21:05:14 -05:00
										 |  |  |     if (note.isJavaScript()) { | 
					
						
							|  |  |  |         bundle.script += `
 | 
					
						
							| 
									
										
										
										
											2018-03-06 23:04:35 -05:00
										 |  |  | apiContext.modules['${note.noteId}'] = {}; | 
					
						
							| 
									
										
										
										
											2018-09-03 17:31:52 +02:00
										 |  |  | ${root ? 'return ' : ''}await ((async function(exports, module, require, api` + (modules.length > 0 ? ', ' : '') +
 | 
					
						
							| 
									
										
										
										
											2018-03-06 23:04:35 -05:00
										 |  |  |             modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) {
 | 
					
						
							| 
									
										
										
										
											2018-08-22 14:40:49 +02:00
										 |  |  | try { | 
					
						
							| 
									
										
										
										
											2019-02-06 21:29:23 +01:00
										 |  |  | ${await note.getContent()}; | 
					
						
							| 
									
										
										
										
											2018-08-22 14:40:49 +02:00
										 |  |  | } catch (e) { throw new Error("Load of script note \\"${note.title}\\" (${note.noteId}) failed with: " + e.message); } | 
					
						
							| 
									
										
										
										
											2018-08-19 22:28:32 +02:00
										 |  |  | if (!module.exports) module.exports = {}; | 
					
						
							|  |  |  | for (const exportKey in exports) module.exports[exportKey] = exports[exportKey]; | 
					
						
							| 
									
										
										
										
											2018-09-03 17:31:52 +02:00
										 |  |  | }).call({}, {}, apiContext.modules['${note.noteId}'], apiContext.require(${JSON.stringify(moduleNoteIds)}), apiContext.apis['${note.noteId}']` + (modules.length > 0 ? ', ' : '') +
 | 
					
						
							|  |  |  |             modules.map(mod => `apiContext.modules['${mod.noteId}'].exports`).join(', ') + `));
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  | `;
 | 
					
						
							| 
									
										
										
										
											2018-03-04 21:05:14 -05:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (note.isHtml()) { | 
					
						
							| 
									
										
										
										
											2019-02-06 21:29:23 +01:00
										 |  |  |         bundle.html += await note.getContent(); | 
					
						
							| 
									
										
										
										
											2018-03-04 21:05:14 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |     return bundle; | 
					
						
							| 
									
										
										
										
											2018-03-03 09:11:41 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-06 23:04:35 -05:00
										 |  |  | function sanitizeVariableName(str) { | 
					
						
							|  |  |  |     return str.replace(/[^a-z0-9_]/gim, ""); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2018-03-02 20:56:58 -05:00
										 |  |  |     executeNote, | 
					
						
							| 
									
										
										
										
											2019-02-03 11:15:32 +01:00
										 |  |  |     executeNoteNoException, | 
					
						
							| 
									
										
										
										
											2018-02-24 14:42:52 -05:00
										 |  |  |     executeScript, | 
					
						
							| 
									
										
										
										
											2019-01-13 12:16:05 +01:00
										 |  |  |     getScriptBundleForFrontend | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | }; |