2013-09-05 16:02:48 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2013-09-05 16:02:48 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
module.exports = function(RED) {
|
2014-07-08 13:27:09 +02:00
|
|
|
"use strict";
|
2021-01-11 11:32:16 +01:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
var util = require("util");
|
|
|
|
var vm = require("vm");
|
2021-06-02 13:32:44 +02:00
|
|
|
var acorn = require("acorn");
|
|
|
|
var acornWalk = require("acorn-walk");
|
2014-05-05 09:58:53 +02:00
|
|
|
|
2019-09-12 23:08:52 +02:00
|
|
|
function sendResults(node,send,_msgid,msgs,cloneFirstMessage) {
|
2015-03-17 14:40:12 +01:00
|
|
|
if (msgs == null) {
|
|
|
|
return;
|
|
|
|
} else if (!util.isArray(msgs)) {
|
|
|
|
msgs = [msgs];
|
|
|
|
}
|
|
|
|
var msgCount = 0;
|
2017-06-24 13:15:03 +02:00
|
|
|
for (var m=0; m<msgs.length; m++) {
|
2015-03-17 14:40:12 +01:00
|
|
|
if (msgs[m]) {
|
2017-05-15 14:54:05 +02:00
|
|
|
if (!util.isArray(msgs[m])) {
|
|
|
|
msgs[m] = [msgs[m]];
|
|
|
|
}
|
|
|
|
for (var n=0; n < msgs[m].length; n++) {
|
|
|
|
var msg = msgs[m][n];
|
|
|
|
if (msg !== null && msg !== undefined) {
|
|
|
|
if (typeof msg === 'object' && !Buffer.isBuffer(msg) && !util.isArray(msg)) {
|
2019-09-12 23:08:52 +02:00
|
|
|
if (msgCount === 0 && cloneFirstMessage !== false) {
|
|
|
|
msgs[m][n] = RED.util.cloneMessage(msgs[m][n]);
|
|
|
|
msg = msgs[m][n];
|
|
|
|
}
|
2017-05-15 14:54:05 +02:00
|
|
|
msg._msgid = _msgid;
|
2016-06-06 12:40:02 +02:00
|
|
|
msgCount++;
|
2017-05-15 14:54:05 +02:00
|
|
|
} else {
|
|
|
|
var type = typeof msg;
|
|
|
|
if (type === 'object') {
|
|
|
|
type = Buffer.isBuffer(msg)?'Buffer':(util.isArray(msg)?'Array':'Date');
|
|
|
|
}
|
2018-07-31 22:54:15 +02:00
|
|
|
node.error(RED._("function.error.non-message-returned",{ type: type }));
|
2016-06-06 12:40:02 +02:00
|
|
|
}
|
2015-03-17 14:40:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (msgCount>0) {
|
2019-08-06 15:27:56 +02:00
|
|
|
send(msgs);
|
2015-03-17 14:40:12 +01:00
|
|
|
}
|
|
|
|
}
|
2015-05-08 16:31:48 +02:00
|
|
|
|
2020-04-06 09:34:41 +02:00
|
|
|
function createVMOpt(node, kind) {
|
|
|
|
var opt = {
|
|
|
|
filename: 'Function node'+kind+':'+node.id+(node.name?' ['+node.name+']':''), // filename for stack traces
|
|
|
|
displayErrors: true
|
|
|
|
// Using the following options causes node 4/6 to not include the line number
|
|
|
|
// in the stack output. So don't use them.
|
|
|
|
// lineOffset: -11, // line number offset to be used for stack traces
|
|
|
|
// columnOffset: 0, // column number offset to be used for stack traces
|
|
|
|
};
|
|
|
|
return opt;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateErrorInfo(err) {
|
|
|
|
if (err.stack) {
|
|
|
|
var stack = err.stack.toString();
|
|
|
|
var m = /^([^:]+):([^:]+):(\d+).*/.exec(stack);
|
|
|
|
if (m) {
|
|
|
|
var line = parseInt(m[3]) -1;
|
|
|
|
var kind = "body:";
|
|
|
|
if (/setup/.exec(m[1])) {
|
|
|
|
kind = "setup:";
|
|
|
|
}
|
|
|
|
if (/cleanup/.exec(m[1])) {
|
|
|
|
kind = "cleanup:";
|
|
|
|
}
|
|
|
|
err.message += " ("+kind+"line "+line+")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function FunctionNode(n) {
|
2021-02-12 19:14:13 +01:00
|
|
|
RED.nodes.createNode(this,n);
|
2015-02-28 12:45:23 +01:00
|
|
|
var node = this;
|
2020-04-06 09:34:41 +02:00
|
|
|
node.name = n.name;
|
|
|
|
node.func = n.func;
|
2021-04-08 15:52:02 +02:00
|
|
|
node.outputs = n.outputs;
|
2023-06-21 15:27:32 +02:00
|
|
|
node.timeout = n.timeout*1000;
|
2023-05-22 10:16:37 +02:00
|
|
|
if(node.timeout>0){
|
|
|
|
node.timeoutOptions = {
|
|
|
|
timeout:node.timeout,
|
|
|
|
breakOnSigint:true
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 11:36:49 +02:00
|
|
|
node.ini = n.initialize ? n.initialize.trim() : "";
|
|
|
|
node.fin = n.finalize ? n.finalize.trim() : "";
|
2021-02-12 23:40:30 +01:00
|
|
|
node.libs = n.libs || [];
|
2021-02-12 19:14:13 +01:00
|
|
|
|
2021-07-15 11:07:52 +02:00
|
|
|
if (RED.settings.functionExternalModules === false && node.libs.length > 0) {
|
2021-04-08 13:06:35 +02:00
|
|
|
throw new Error(RED._("function.error.externalModuleNotAllowed"));
|
2021-02-12 19:14:13 +01:00
|
|
|
}
|
2019-08-06 15:27:56 +02:00
|
|
|
|
2020-05-22 21:49:18 +02:00
|
|
|
|
2019-08-06 15:27:56 +02:00
|
|
|
|
2015-03-17 14:40:12 +01:00
|
|
|
var functionText = "var results = null;"+
|
2021-01-11 11:32:16 +01:00
|
|
|
"results = (async function(msg,__send__,__done__){ "+
|
2021-04-08 15:52:02 +02:00
|
|
|
"var __msgid__ = msg._msgid;"+
|
|
|
|
"var node = {"+
|
|
|
|
"id:__node__.id,"+
|
|
|
|
"name:__node__.name,"+
|
2022-01-25 22:32:28 +01:00
|
|
|
"path:__node__.path,"+
|
2021-04-08 15:52:02 +02:00
|
|
|
"outputCount:__node__.outputCount,"+
|
|
|
|
"log:__node__.log,"+
|
|
|
|
"error:__node__.error,"+
|
|
|
|
"warn:__node__.warn,"+
|
|
|
|
"debug:__node__.debug,"+
|
|
|
|
"trace:__node__.trace,"+
|
|
|
|
"on:__node__.on,"+
|
|
|
|
"status:__node__.status,"+
|
|
|
|
"send:function(msgs,cloneMsg){ __node__.send(__send__,__msgid__,msgs,cloneMsg);},"+
|
|
|
|
"done:__done__"+
|
|
|
|
"};\n"+
|
|
|
|
node.func+"\n"+
|
2021-01-11 11:32:16 +01:00
|
|
|
"})(msg,__send__,__done__);";
|
2021-06-02 13:32:44 +02:00
|
|
|
|
|
|
|
var handleNodeDoneCall = true;
|
|
|
|
|
|
|
|
// Check to see if the Function appears to call `node.done()`. If so,
|
|
|
|
// we will assume it is well written and does actually call node.done().
|
|
|
|
// Otherwise, we will call node.done() after the function returns regardless.
|
|
|
|
if (/node\.done\s*\(\s*\)/.test(functionText)) {
|
|
|
|
// We have spotted the code contains `node.done`. It could be in a comment
|
|
|
|
// so need to do the extra work to parse the AST and examine it properly.
|
|
|
|
acornWalk.simple(acorn.parse(functionText,{ecmaVersion: "latest"} ), {
|
|
|
|
CallExpression(astNode) {
|
|
|
|
if (astNode.callee && astNode.callee.object) {
|
|
|
|
if (astNode.callee.object.name === "node" && astNode.callee.property.name === "done") {
|
|
|
|
handleNodeDoneCall = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-06 09:34:41 +02:00
|
|
|
var finScript = null;
|
|
|
|
var finOpt = null;
|
|
|
|
node.topic = n.topic;
|
|
|
|
node.outstandingTimers = [];
|
|
|
|
node.outstandingIntervals = [];
|
2020-11-16 22:04:38 +01:00
|
|
|
node.clearStatus = false;
|
|
|
|
|
2014-09-05 11:40:57 +02:00
|
|
|
var sandbox = {
|
|
|
|
console:console,
|
|
|
|
util:util,
|
|
|
|
Buffer:Buffer,
|
2018-05-25 11:50:58 +02:00
|
|
|
Date: Date,
|
2016-06-13 23:16:36 +02:00
|
|
|
RED: {
|
|
|
|
util: RED.util
|
|
|
|
},
|
2015-03-17 14:40:12 +01:00
|
|
|
__node__: {
|
2018-05-21 12:34:56 +02:00
|
|
|
id: node.id,
|
|
|
|
name: node.name,
|
2022-01-25 22:32:28 +01:00
|
|
|
path: node._path,
|
2021-04-08 15:52:02 +02:00
|
|
|
outputCount: node.outputs,
|
2015-03-17 14:40:12 +01:00
|
|
|
log: function() {
|
2015-02-28 12:45:23 +01:00
|
|
|
node.log.apply(node, arguments);
|
|
|
|
},
|
2015-10-15 14:33:01 +02:00
|
|
|
error: function() {
|
2015-02-28 12:45:23 +01:00
|
|
|
node.error.apply(node, arguments);
|
|
|
|
},
|
|
|
|
warn: function() {
|
|
|
|
node.warn.apply(node, arguments);
|
2015-03-17 14:40:12 +01:00
|
|
|
},
|
2018-03-20 21:40:36 +01:00
|
|
|
debug: function() {
|
|
|
|
node.debug.apply(node, arguments);
|
|
|
|
},
|
|
|
|
trace: function() {
|
|
|
|
node.trace.apply(node, arguments);
|
|
|
|
},
|
2019-09-12 23:08:52 +02:00
|
|
|
send: function(send, id, msgs, cloneMsg) {
|
|
|
|
sendResults(node, send, id, msgs, cloneMsg);
|
2015-03-17 14:40:12 +01:00
|
|
|
},
|
|
|
|
on: function() {
|
2016-04-21 23:24:59 +02:00
|
|
|
if (arguments[0] === "input") {
|
|
|
|
throw new Error(RED._("function.error.inputListener"));
|
|
|
|
}
|
2015-10-15 14:33:01 +02:00
|
|
|
node.on.apply(node, arguments);
|
|
|
|
},
|
|
|
|
status: function() {
|
2020-11-16 22:04:38 +01:00
|
|
|
node.clearStatus = true;
|
2015-10-15 14:33:01 +02:00
|
|
|
node.status.apply(node, arguments);
|
2015-02-28 12:45:23 +01:00
|
|
|
}
|
|
|
|
},
|
2014-09-05 11:40:57 +02:00
|
|
|
context: {
|
2015-12-29 23:17:30 +01:00
|
|
|
set: function() {
|
|
|
|
node.context().set.apply(node,arguments);
|
|
|
|
},
|
|
|
|
get: function() {
|
|
|
|
return node.context().get.apply(node,arguments);
|
|
|
|
},
|
2017-07-04 15:52:14 +02:00
|
|
|
keys: function() {
|
|
|
|
return node.context().keys.apply(node,arguments);
|
|
|
|
},
|
2015-12-29 23:17:30 +01:00
|
|
|
get global() {
|
|
|
|
return node.context().global;
|
|
|
|
},
|
|
|
|
get flow() {
|
|
|
|
return node.context().flow;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
flow: {
|
|
|
|
set: function() {
|
|
|
|
node.context().flow.set.apply(node,arguments);
|
|
|
|
},
|
|
|
|
get: function() {
|
|
|
|
return node.context().flow.get.apply(node,arguments);
|
2017-07-04 15:52:14 +02:00
|
|
|
},
|
|
|
|
keys: function() {
|
|
|
|
return node.context().flow.keys.apply(node,arguments);
|
2015-12-29 23:17:30 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
global: {
|
|
|
|
set: function() {
|
|
|
|
node.context().global.set.apply(node,arguments);
|
|
|
|
},
|
|
|
|
get: function() {
|
|
|
|
return node.context().global.get.apply(node,arguments);
|
2017-07-04 15:52:14 +02:00
|
|
|
},
|
|
|
|
keys: function() {
|
|
|
|
return node.context().global.keys.apply(node,arguments);
|
2015-12-29 23:17:30 +01:00
|
|
|
}
|
2015-03-17 14:40:12 +01:00
|
|
|
},
|
2018-08-30 23:42:30 +02:00
|
|
|
env: {
|
2023-06-22 11:17:48 +02:00
|
|
|
get: function(envVar) {
|
|
|
|
return RED.util.getSetting(node, envVar);
|
2018-08-30 23:42:30 +02:00
|
|
|
}
|
|
|
|
},
|
2015-11-15 23:22:17 +01:00
|
|
|
setTimeout: function () {
|
|
|
|
var func = arguments[0];
|
|
|
|
var timerId;
|
|
|
|
arguments[0] = function() {
|
2015-11-03 11:47:29 +01:00
|
|
|
sandbox.clearTimeout(timerId);
|
2015-11-15 23:22:17 +01:00
|
|
|
try {
|
2020-04-06 09:34:41 +02:00
|
|
|
func.apply(node,arguments);
|
2015-11-15 23:22:17 +01:00
|
|
|
} catch(err) {
|
|
|
|
node.error(err,{});
|
|
|
|
}
|
|
|
|
};
|
2020-04-06 09:34:41 +02:00
|
|
|
timerId = setTimeout.apply(node,arguments);
|
2015-11-03 11:47:29 +01:00
|
|
|
node.outstandingTimers.push(timerId);
|
2015-11-14 22:21:14 +01:00
|
|
|
return timerId;
|
2015-11-03 11:47:29 +01:00
|
|
|
},
|
|
|
|
clearTimeout: function(id) {
|
|
|
|
clearTimeout(id);
|
|
|
|
var index = node.outstandingTimers.indexOf(id);
|
|
|
|
if (index > -1) {
|
|
|
|
node.outstandingTimers.splice(index,1);
|
|
|
|
}
|
|
|
|
},
|
2015-11-15 23:22:17 +01:00
|
|
|
setInterval: function() {
|
|
|
|
var func = arguments[0];
|
|
|
|
var timerId;
|
|
|
|
arguments[0] = function() {
|
|
|
|
try {
|
2020-04-06 09:34:41 +02:00
|
|
|
func.apply(node,arguments);
|
2015-11-15 23:22:17 +01:00
|
|
|
} catch(err) {
|
|
|
|
node.error(err,{});
|
|
|
|
}
|
|
|
|
};
|
2020-04-06 09:34:41 +02:00
|
|
|
timerId = setInterval.apply(node,arguments);
|
2015-11-03 11:47:29 +01:00
|
|
|
node.outstandingIntervals.push(timerId);
|
2015-11-14 22:21:14 +01:00
|
|
|
return timerId;
|
2015-11-03 11:47:29 +01:00
|
|
|
},
|
|
|
|
clearInterval: function(id) {
|
|
|
|
clearInterval(id);
|
|
|
|
var index = node.outstandingIntervals.indexOf(id);
|
|
|
|
if (index > -1) {
|
|
|
|
node.outstandingIntervals.splice(index,1);
|
|
|
|
}
|
2020-04-10 16:06:43 +02:00
|
|
|
}
|
2014-09-05 11:40:57 +02:00
|
|
|
};
|
2017-09-12 16:13:13 +02:00
|
|
|
if (util.hasOwnProperty('promisify')) {
|
|
|
|
sandbox.setTimeout[util.promisify.custom] = function(after, value) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
2018-07-31 22:54:15 +02:00
|
|
|
sandbox.setTimeout(function(){ resolve(value); }, after);
|
2017-09-12 16:13:13 +02:00
|
|
|
});
|
2018-07-31 22:54:15 +02:00
|
|
|
};
|
2020-04-10 16:06:43 +02:00
|
|
|
sandbox.promisify = util.promisify;
|
2017-09-12 16:13:13 +02:00
|
|
|
}
|
2021-07-14 20:18:39 +02:00
|
|
|
const moduleLoadPromises = [];
|
2021-01-11 11:32:16 +01:00
|
|
|
|
2021-02-12 19:14:13 +01:00
|
|
|
if (node.hasOwnProperty("libs")) {
|
2021-02-16 14:58:59 +01:00
|
|
|
let moduleErrors = false;
|
2021-02-12 19:14:13 +01:00
|
|
|
var modules = node.libs;
|
|
|
|
modules.forEach(module => {
|
|
|
|
var vname = module.hasOwnProperty("var") ? module.var : null;
|
|
|
|
if (vname && (vname !== "")) {
|
2021-02-16 14:58:59 +01:00
|
|
|
if (sandbox.hasOwnProperty(vname) || vname === 'node') {
|
|
|
|
node.error(RED._("function.error.moduleNameError",{name:vname}))
|
|
|
|
moduleErrors = true;
|
|
|
|
return;
|
|
|
|
}
|
2021-02-12 19:14:13 +01:00
|
|
|
sandbox[vname] = null;
|
2021-07-14 20:18:39 +02:00
|
|
|
var spec = module.module;
|
|
|
|
if (spec && (spec !== "")) {
|
2021-07-14 22:10:59 +02:00
|
|
|
moduleLoadPromises.push(RED.import(module.module).then(lib => {
|
2021-07-20 15:42:43 +02:00
|
|
|
sandbox[vname] = lib.default;
|
2021-07-14 20:18:39 +02:00
|
|
|
}).catch(err => {
|
|
|
|
node.error(RED._("function.error.moduleLoadError",{module:module.spec, error:err.toString()}))
|
|
|
|
throw err;
|
2021-07-14 22:10:59 +02:00
|
|
|
}));
|
2021-02-12 19:14:13 +01:00
|
|
|
}
|
2021-01-11 11:32:16 +01:00
|
|
|
}
|
2021-02-12 19:14:13 +01:00
|
|
|
});
|
2021-07-14 22:10:59 +02:00
|
|
|
if (moduleErrors) {
|
|
|
|
throw new Error(RED._("function.error.externalModuleLoadError"));
|
|
|
|
}
|
2021-02-12 19:14:13 +01:00
|
|
|
}
|
2021-07-14 22:10:59 +02:00
|
|
|
const RESOLVING = 0;
|
|
|
|
const RESOLVED = 1;
|
|
|
|
const ERROR = 2;
|
|
|
|
var state = RESOLVING;
|
|
|
|
var messages = [];
|
|
|
|
var processMessage = (() => {});
|
2021-02-16 14:58:59 +01:00
|
|
|
|
2021-07-14 22:10:59 +02:00
|
|
|
node.on("input", function(msg,send,done) {
|
|
|
|
if(state === RESOLVING) {
|
|
|
|
messages.push({msg:msg, send:send, done:done});
|
|
|
|
}
|
|
|
|
else if(state === RESOLVED) {
|
|
|
|
processMessage(msg, send, done);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Promise.all(moduleLoadPromises).then(() => {
|
2021-07-14 20:18:39 +02:00
|
|
|
var context = vm.createContext(sandbox);
|
|
|
|
try {
|
|
|
|
var iniScript = null;
|
|
|
|
var iniOpt = null;
|
|
|
|
if (node.ini && (node.ini !== "")) {
|
|
|
|
var iniText = `
|
|
|
|
(async function(__send__) {
|
|
|
|
var node = {
|
|
|
|
id:__node__.id,
|
|
|
|
name:__node__.name,
|
2022-01-25 22:32:28 +01:00
|
|
|
path:__node__.path,
|
2021-07-14 20:18:39 +02:00
|
|
|
outputCount:__node__.outputCount,
|
|
|
|
log:__node__.log,
|
|
|
|
error:__node__.error,
|
|
|
|
warn:__node__.warn,
|
|
|
|
debug:__node__.debug,
|
|
|
|
trace:__node__.trace,
|
|
|
|
status:__node__.status,
|
|
|
|
send: function(msgs, cloneMsg) {
|
|
|
|
__node__.send(__send__, RED.util.generateId(), msgs, cloneMsg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
`+ node.ini +`
|
|
|
|
})(__initSend__);`;
|
|
|
|
iniOpt = createVMOpt(node, " setup");
|
|
|
|
iniScript = new vm.Script(iniText, iniOpt);
|
2023-05-22 10:16:37 +02:00
|
|
|
if(node.timeout>0){
|
|
|
|
iniOpt.timeout = node.timeout;
|
|
|
|
iniOpt.breakOnSigint = true;
|
|
|
|
}
|
2021-07-14 20:18:39 +02:00
|
|
|
}
|
|
|
|
node.script = vm.createScript(functionText, createVMOpt(node, ""));
|
|
|
|
if (node.fin && (node.fin !== "")) {
|
|
|
|
var finText = `(function () {
|
|
|
|
var node = {
|
|
|
|
id:__node__.id,
|
|
|
|
name:__node__.name,
|
2022-01-25 22:32:28 +01:00
|
|
|
path:__node__.path,
|
2021-07-14 20:18:39 +02:00
|
|
|
outputCount:__node__.outputCount,
|
|
|
|
log:__node__.log,
|
|
|
|
error:__node__.error,
|
|
|
|
warn:__node__.warn,
|
|
|
|
debug:__node__.debug,
|
|
|
|
trace:__node__.trace,
|
|
|
|
status:__node__.status,
|
|
|
|
send: function(msgs, cloneMsg) {
|
|
|
|
__node__.error("Cannot send from close function");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
`+node.fin +`
|
|
|
|
})();`;
|
|
|
|
finOpt = createVMOpt(node, " cleanup");
|
|
|
|
finScript = new vm.Script(finText, finOpt);
|
2023-05-22 10:16:37 +02:00
|
|
|
if(node.timeout>0){
|
|
|
|
finOpt.timeout = node.timeout;
|
|
|
|
finOpt.breakOnSigint = true;
|
|
|
|
}
|
2021-07-14 20:18:39 +02:00
|
|
|
}
|
|
|
|
var promise = Promise.resolve();
|
|
|
|
if (iniScript) {
|
|
|
|
context.__initSend__ = function(msgs) { node.send(msgs); };
|
|
|
|
promise = iniScript.runInContext(context, iniOpt);
|
|
|
|
}
|
2021-02-16 14:58:59 +01:00
|
|
|
|
2021-07-14 20:18:39 +02:00
|
|
|
processMessage = function (msg, send, done) {
|
|
|
|
var start = process.hrtime();
|
|
|
|
context.msg = msg;
|
|
|
|
context.__send__ = send;
|
2023-05-22 10:16:37 +02:00
|
|
|
context.__done__ = done;
|
|
|
|
var opts = {};
|
|
|
|
if (node.timeout>0){
|
|
|
|
opts = node.timeoutOptions;
|
|
|
|
}
|
|
|
|
node.script.runInContext(context,opts);
|
2021-07-14 20:18:39 +02:00
|
|
|
context.results.then(function(results) {
|
|
|
|
sendResults(node,send,msg._msgid,results,false);
|
|
|
|
if (handleNodeDoneCall) {
|
|
|
|
done();
|
2021-02-17 15:41:50 +01:00
|
|
|
}
|
2015-05-08 16:31:48 +02:00
|
|
|
|
2021-07-14 20:18:39 +02:00
|
|
|
var duration = process.hrtime(start);
|
|
|
|
var converted = Math.floor((duration[0] * 1e9 + duration[1])/10000)/100;
|
|
|
|
node.metric("duration", msg, converted);
|
|
|
|
if (process.env.NODE_RED_FUNCTION_TIME) {
|
|
|
|
node.status({fill:"yellow",shape:"dot",text:""+converted});
|
|
|
|
}
|
|
|
|
}).catch(err => {
|
|
|
|
if ((typeof err === "object") && err.hasOwnProperty("stack")) {
|
|
|
|
//remove unwanted part
|
|
|
|
var index = err.stack.search(/\n\s*at ContextifyScript.Script.runInContext/);
|
|
|
|
err.stack = err.stack.slice(0, index).split('\n').slice(0,-1).join('\n');
|
|
|
|
var stack = err.stack.split(/\r?\n/);
|
2018-04-17 15:46:09 +02:00
|
|
|
|
2021-07-14 20:18:39 +02:00
|
|
|
//store the error in msg to be used in flows
|
|
|
|
msg.error = err;
|
2018-05-08 12:40:16 +02:00
|
|
|
|
2021-07-14 20:18:39 +02:00
|
|
|
var line = 0;
|
|
|
|
var errorMessage;
|
|
|
|
if (stack.length > 0) {
|
|
|
|
while (line < stack.length && stack[line].indexOf("ReferenceError") !== 0) {
|
|
|
|
line++;
|
|
|
|
}
|
2015-07-05 23:40:24 +02:00
|
|
|
|
2021-07-14 20:18:39 +02:00
|
|
|
if (line < stack.length) {
|
|
|
|
errorMessage = stack[line];
|
|
|
|
var m = /:(\d+):(\d+)$/.exec(stack[line+1]);
|
|
|
|
if (m) {
|
|
|
|
var lineno = Number(m[1])-1;
|
|
|
|
var cha = m[2];
|
|
|
|
errorMessage += " (line "+lineno+", col "+cha+")";
|
|
|
|
}
|
2019-08-19 11:42:14 +02:00
|
|
|
}
|
2015-07-05 23:40:24 +02:00
|
|
|
}
|
2021-07-14 20:18:39 +02:00
|
|
|
if (!errorMessage) {
|
|
|
|
errorMessage = err.toString();
|
|
|
|
}
|
|
|
|
done(errorMessage);
|
2019-08-19 11:42:14 +02:00
|
|
|
}
|
2021-07-14 20:18:39 +02:00
|
|
|
else if (typeof err === "string") {
|
|
|
|
done(err);
|
2021-01-11 11:32:16 +01:00
|
|
|
}
|
2021-07-14 20:18:39 +02:00
|
|
|
else {
|
|
|
|
done(JSON.stringify(err));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
node.on("close", function() {
|
|
|
|
if (finScript) {
|
|
|
|
try {
|
|
|
|
finScript.runInContext(context, finOpt);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
node.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (node.outstandingTimers.length > 0) {
|
|
|
|
clearTimeout(node.outstandingTimers.pop());
|
2019-08-19 11:42:14 +02:00
|
|
|
}
|
2021-07-14 20:18:39 +02:00
|
|
|
while (node.outstandingIntervals.length > 0) {
|
|
|
|
clearInterval(node.outstandingIntervals.pop());
|
2020-04-10 16:06:43 +02:00
|
|
|
}
|
2021-07-14 20:18:39 +02:00
|
|
|
if (node.clearStatus) {
|
|
|
|
node.status({});
|
2015-02-07 20:52:14 +01:00
|
|
|
}
|
2020-05-22 21:49:18 +02:00
|
|
|
});
|
2020-04-10 16:06:43 +02:00
|
|
|
|
2021-07-14 20:18:39 +02:00
|
|
|
promise.then(function (v) {
|
|
|
|
var msgs = messages;
|
|
|
|
messages = [];
|
|
|
|
while (msgs.length > 0) {
|
|
|
|
msgs.forEach(function (s) {
|
|
|
|
processMessage(s.msg, s.send, s.done);
|
|
|
|
});
|
|
|
|
msgs = messages;
|
|
|
|
messages = [];
|
2021-02-12 19:14:13 +01:00
|
|
|
}
|
2021-07-14 20:18:39 +02:00
|
|
|
state = RESOLVED;
|
|
|
|
}).catch((error) => {
|
2020-04-10 16:06:43 +02:00
|
|
|
messages = [];
|
2021-07-14 20:18:39 +02:00
|
|
|
state = ERROR;
|
|
|
|
node.error(error);
|
|
|
|
});
|
2020-04-10 16:06:43 +02:00
|
|
|
|
2021-07-14 20:18:39 +02:00
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
// eg SyntaxError - which v8 doesn't include line number information
|
|
|
|
// so we can't do better than this
|
|
|
|
updateErrorInfo(err);
|
|
|
|
node.error(err);
|
|
|
|
}
|
|
|
|
}).catch(err => {
|
2021-07-15 16:37:12 +02:00
|
|
|
node.error(RED._("function.error.externalModuleLoadError"));
|
2021-07-14 20:18:39 +02:00
|
|
|
});
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2021-01-11 11:32:16 +01:00
|
|
|
RED.nodes.registerType("function",FunctionNode, {
|
2021-02-12 19:14:13 +01:00
|
|
|
dynamicModuleList: "libs",
|
|
|
|
settings: {
|
2021-07-15 11:07:52 +02:00
|
|
|
functionExternalModules: { value: true, exportable: true }
|
2021-02-12 19:14:13 +01:00
|
|
|
}
|
2021-01-11 11:32:16 +01:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.library.register("functions");
|
2018-07-31 22:54:15 +02:00
|
|
|
};
|