| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  | const sql = require('./sql'); | 
					
						
							|  |  |  | const ScriptContext = require('./script_context'); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-02 20:56:58 -05:00
										 |  |  | async function executeNote(note) { | 
					
						
							|  |  |  |     if (note.isProtected || !note.isJavaScript()) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-03 09:11:41 -05:00
										 |  |  |     const manualTransactionHandling = (await note.getAttributeMap()).manual_transaction_handling !== undefined; | 
					
						
							| 
									
										
										
										
											2018-03-02 20:56:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |     const bundle = await getScriptBundle(note); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |     const ctx = new ScriptContext(null, note, bundle.allNotes); | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (manualTransactionHandling) { | 
					
						
							|  |  |  |         return await execute(ctx, script, ''); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         return await sql.doInTransaction(async () => execute(ctx, script, '')); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-02 20:56:58 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-30 22:44:46 -05:00
										 |  |  | async function executeScript(dataKey, script, params) { | 
					
						
							|  |  |  |     const ctx = new ScriptContext(dataKey); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  |     const paramsStr = getParams(params); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-24 22:44:45 -05:00
										 |  |  |     return await sql.doInTransaction(async () => execute(ctx, script, paramsStr)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-24 22:44:45 -05:00
										 |  |  | async function execute(ctx, script, paramsStr) { | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  |     return await (function() { return eval(`const api = this;\r\n(${script})(${paramsStr})`); }.call(ctx)); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-24 14:42:52 -05:00
										 |  |  | const timeouts = {}; | 
					
						
							|  |  |  | const intervals = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function clearExistingJob(name) { | 
					
						
							|  |  |  |     if (timeouts[name]) { | 
					
						
							|  |  |  |         clearTimeout(timeouts[name]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         delete timeouts[name]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (intervals[name]) { | 
					
						
							|  |  |  |         clearInterval(intervals[name]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         delete intervals[name]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-24 22:44:45 -05:00
										 |  |  | async function executeJob(script, params, manualTransactionHandling) { | 
					
						
							|  |  |  |     const ctx = new ScriptContext(); | 
					
						
							|  |  |  |     const paramsStr = getParams(params); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (manualTransactionHandling) { | 
					
						
							|  |  |  |         return await execute(ctx, script, paramsStr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         return await sql.doInTransaction(async () => execute(ctx, script, paramsStr)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-24 14:42:52 -05:00
										 |  |  | async function setJob(opts) { | 
					
						
							| 
									
										
										
										
											2018-02-24 22:44:45 -05:00
										 |  |  |     const { name, runEveryMs, initialRunAfterMs } = opts; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     clearExistingJob(name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const jobFunc = () => executeJob(opts.job, opts.params, opts.manualTransactionHandling); | 
					
						
							| 
									
										
										
										
											2018-02-24 14:42:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-24 22:44:45 -05:00
										 |  |  |     if (runEveryMs && runEveryMs > 0) { | 
					
						
							|  |  |  |         intervals[name] = setInterval(jobFunc, runEveryMs); | 
					
						
							| 
									
										
										
										
											2018-02-24 14:42:52 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-24 22:44:45 -05:00
										 |  |  |     if (initialRunAfterMs && initialRunAfterMs > 0) { | 
					
						
							|  |  |  |         timeouts[name] = setTimeout(jobFunc, initialRunAfterMs); | 
					
						
							| 
									
										
										
										
											2018-02-24 14:42:52 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  | async function getRenderScript(note) { | 
					
						
							|  |  |  |     const subTreeScripts = await getModules(note, [note.noteId]); | 
					
						
							| 
									
										
										
										
											2018-03-03 09:11:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // last \r\n is necessary if script contains line comment on its last line
 | 
					
						
							|  |  |  |     return "async function() {" + subTreeScripts + note.content + "\r\n}"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  | async function getNoteScript(note) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  | async function getScriptBundle(note, includedNoteIds = []) { | 
					
						
							|  |  |  |     if (!note.isJavaScript()) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |     const bundle = { | 
					
						
							|  |  |  |         note: note, | 
					
						
							|  |  |  |         script: '', | 
					
						
							|  |  |  |         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-04 12:06:35 -05:00
										 |  |  |     for (const child of await note.getChildren()) { | 
					
						
							|  |  |  |         const childBundle = await getScriptBundle(child, 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; | 
					
						
							|  |  |  |             bundle.allNotes = bundle.allNotes.concat(childBundle.allNotes); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-03 09:11:41 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 12:06:35 -05:00
										 |  |  |     bundle.script += `
 | 
					
						
							|  |  |  | api.__modules['${note.noteId}'] = {}; | 
					
						
							|  |  |  | await (async function(module, api, startNote, currentNote` + (modules.length > 0 ? ', ' : '') +
 | 
					
						
							|  |  |  |         modules.map(child => child.title).join(', ') + `) {
 | 
					
						
							|  |  |  | ${note.content} | 
					
						
							|  |  |  | })(api.__modules['${note.noteId}'], api, api.__startNote, api.__notes['${note.noteId}']` + (modules.length > 0 ? ', ' : '') +
 | 
					
						
							|  |  |  |         modules.map(mod => `api.__modules['${mod.noteId}'].exports`).join(', ') + `);
 | 
					
						
							|  |  |  | `;
 | 
					
						
							| 
									
										
										
										
											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-01-26 23:40:48 -05:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2018-03-02 20:56:58 -05:00
										 |  |  |     executeNote, | 
					
						
							| 
									
										
										
										
											2018-02-24 14:42:52 -05:00
										 |  |  |     executeScript, | 
					
						
							| 
									
										
										
										
											2018-03-03 09:11:41 -05:00
										 |  |  |     setJob, | 
					
						
							| 
									
										
										
										
											2018-03-04 10:32:53 -05:00
										 |  |  |     getNoteScript, | 
					
						
							|  |  |  |     getRenderScript | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | }; |