Skip to content

Commit

Permalink
Update node templates for Node-RED v1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuhitoyokoi committed Dec 23, 2019
1 parent 2b90a03 commit 2d26d43
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 41 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"grunt-shell": "3.0.1",
"grunt-simple-mocha": "0.4.1",
"istanbul": "0.4.5",
"node-red": "0.20.7",
"node-red": "1.0.3",
"node-red-node-test-helper": "0.2.3",
"q": "1.5.1",
"should": "13.2.3",
Expand Down
8 changes: 5 additions & 3 deletions templates/function/README.md.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{&projectName}}
=====================
================

Node-RED node for {{&nodeName}}

Expand All @@ -8,7 +8,9 @@ Node-RED node for {{&nodeName}}
Install
-------

Run the following command in your Node-RED user directory - typically `~/.node-red`
To install the stable version use the `Menu - Manage palette - Install`
option and search for {{&projectName}}, or run the following
command in your Node-RED user directory, typically `~/.node-red`

npm install {{&projectName}}
npm install {{&projectName}}

90 changes: 59 additions & 31 deletions templates/function/node.js.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function(RED) {
var util = require("util");
var vm = require("vm");
function sendResults(node,_msgid,msgs) {
function sendResults(node,send,_msgid,msgs,cloneFirstMessage) {
if (msgs == null) {
return;
} else if (!util.isArray(msgs)) {
Expand All @@ -35,6 +35,10 @@ module.exports = function(RED) {
var msg = msgs[m][n];
if (msg !== null && msg !== undefined) {
if (typeof msg === 'object' && !Buffer.isBuffer(msg) && !util.isArray(msg)) {
if (msgCount === 0 && cloneFirstMessage !== false) {
msgs[m][n] = RED.util.cloneMessage(msgs[m][n]);
msg = msgs[m][n];
}
msg._msgid = _msgid;
msgCount++;
} else {
Expand All @@ -49,7 +53,7 @@ module.exports = function(RED) {
}
}
if (msgCount>0) {
node.send(msgs);
send(msgs);
}
}

Expand All @@ -58,8 +62,17 @@ module.exports = function(RED) {
var node = this;
this.name = n.name;
this.func = n.func;
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(this.func)) {
handleNodeDoneCall = false;
}

var functionText = "var results = null;"+
"results = (function(msg){ "+
"results = (function(msg,__send__,__done__){ "+
"var __msgid__ = msg._msgid;"+
"var node = {"+
"id:__node__.id,"+
Expand All @@ -71,10 +84,11 @@ module.exports = function(RED) {
"trace:__node__.trace,"+
"on:__node__.on,"+
"status:__node__.status,"+
"send:function(msgs){ __node__.send(__msgid__,msgs);}"+
"send:function(msgs,cloneMsg){ __node__.send(__send__,__msgid__,msgs,cloneMsg);},"+
"done:__done__"+
"};\n"+
"{{&func}}"+"\n"+
"})(msg);";
"})(msg,send,done);";
this.topic = n.topic;
this.outstandingTimers = [];
this.outstandingIntervals = [];
Expand Down Expand Up @@ -104,8 +118,8 @@ module.exports = function(RED) {
trace: function() {
node.trace.apply(node, arguments);
},
send: function(id, msgs) {
sendResults(node, id, msgs);
send: function(send, id, msgs, cloneMsg) {
sendResults(node, send, id, msgs, cloneMsg);
},
on: function() {
if (arguments[0] === "input") {
Expand Down Expand Up @@ -223,12 +237,18 @@ module.exports = function(RED) {
// lineOffset: -11, // line number offset to be used for stack traces
// columnOffset: 0, // column number offset to be used for stack traces
});
this.on("input", function(msg) {
this.on("input", function(msg,send,done) {
try {
var start = process.hrtime();
context.msg = msg;
context.send = send;
context.done = done;
this.script.runInContext(context);
sendResults(this,msg._msgid,context.results);
sendResults(this,send,msg._msgid,context.results,false);
if (handleNodeDoneCall) {
done();
}
var duration = process.hrtime(start);
var converted = Math.floor((duration[0] * 1e9 + duration[1])/10000)/100;
Expand All @@ -237,35 +257,43 @@ module.exports = function(RED) {
this.status({fill:"yellow",shape:"dot",text:""+converted});
}
} catch(err) {
//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/);
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/);
//store the error in msg to be used in flows
msg.error = err;
//store the error in msg to be used in flows
msg.error = err;
var line = 0;
var errorMessage;
if (stack.length > 0) {
while (line < stack.length && stack[line].indexOf("ReferenceError") !== 0) {
line++;
}
var line = 0;
var errorMessage;
if (stack.length > 0) {
while (line < stack.length && stack[line].indexOf("ReferenceError") !== 0) {
line++;
}
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+")";
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+")";
}
}
}
if (!errorMessage) {
errorMessage = err.toString();
}
done(errorMessage);
}
else if (typeof err === "string") {
done(err);
}
if (!errorMessage) {
errorMessage = err.toString();
else {
done(JSON.stringify(err));
}
this.error(errorMessage, msg);
}
});
this.on("close", function() {
Expand Down
2 changes: 1 addition & 1 deletion templates/function/package.json.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"devDependencies": {
"mocha": "6.2.0",
"node-red": "0.20.7",
"node-red": "1.0.3",
"node-red-node-test-helper": "0.2.3"
},
"license": "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion templates/swagger/README.md.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Install
-------

To install the stable version use the `Menu - Manage palette - Install`
option and search for node-red-node-serialport, or run the following
option and search for {{&projectName}}, or run the following
command in your Node-RED user directory, typically `~/.node-red`

npm install {{&projectName}}
Expand Down
8 changes: 5 additions & 3 deletions templates/webofthings/README.md.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{projectName}}
=====================
================

Node-RED node for {{nodeName}}

Expand All @@ -8,9 +8,11 @@ Node-RED node for {{nodeName}}
Install
-------

Run the following command in your Node-RED user directory - typically `~/.node-red`
To install the stable version use the `Menu - Manage palette - Install`
option and search for {{projectName}}, or run the following
command in your Node-RED user directory, typically `~/.node-red`

npm install {{projectName}}
npm install {{projectName}}

Interactions
------------
Expand Down
2 changes: 1 addition & 1 deletion templates/webofthings/package.json.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
"{{name}}": "{{value}}"{{^last}}, {{/last}}
{{/wotmeta}}
}
}
}

0 comments on commit 2d26d43

Please sign in to comment.