Skip to content

Commit 57dc4b5

Browse files
Merge pull request #69 from kazuhitoyokoi/master-updatefunctionnodetemplate3
2 parents 6b13ccc + 980f728 commit 57dc4b5

File tree

8 files changed

+232
-1
lines changed

8 files changed

+232
-1
lines changed

Gruntfile.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ module.exports = function (grunt) {
7676
generateNode_handleSetTimeout: {
7777
command: 'node bin/node-red-nodegen.js samples/handle-setTimeout.js -o ./nodegen'
7878
},
79+
generateNode_useTheSameDateObjectFromOutsideTheSandbox: {
80+
command: 'node bin/node-red-nodegen.js samples/use-the-same-Date-object-from-outside-the-sandbox.js -o ./nodegen'
81+
},
82+
generateNode_logADebugMessage: {
83+
command: 'node bin/node-red-nodegen.js samples/log-a-Debug-Message.js -o ./nodegen'
84+
},
85+
generateNode_logATraceMessage: {
86+
command: 'node bin/node-red-nodegen.js samples/log-a-Trace-Message.js -o ./nodegen'
87+
},
7988
generateNode_logAWarningMessage: {
8089
command: 'node bin/node-red-nodegen.js samples/log-a-Warning-Message.js -o ./nodegen'
8190
},

samples/log-a-Debug-Message.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// name: log a Debug Message
2+
// outputs: 1
3+
node.debug('test');

samples/log-a-Trace-Message.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// name: log a Trace Message
2+
// outputs: 1
3+
node.trace('test');
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// name: use the same Date object from outside the sandbox
2+
// outputs: 1
3+
msg.payload=global.get('typeTest')(new Date());
4+
return msg;

templates/function/node.js.mustache

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ module.exports = function(RED) {
6565
"log:__node__.log,"+
6666
"error:__node__.error,"+
6767
"warn:__node__.warn,"+
68+
"debug:__node__.debug,"+
69+
"trace:__node__.trace,"+
6870
"on:__node__.on,"+
6971
"status:__node__.status,"+
7072
"send:function(msgs){ __node__.send(__msgid__,msgs);}"+
@@ -78,6 +80,7 @@ module.exports = function(RED) {
7880
console:console,
7981
util:util,
8082
Buffer:Buffer,
83+
Date: Date,
8184
RED: {
8285
util: RED.util
8386
},
@@ -91,6 +94,12 @@ module.exports = function(RED) {
9194
warn: function() {
9295
node.warn.apply(node, arguments);
9396
},
97+
debug: function() {
98+
node.debug.apply(node, arguments);
99+
},
100+
trace: function() {
101+
node.trace.apply(node, arguments);
102+
},
94103
send: function(id, msgs) {
95104
sendResults(node, id, msgs);
96105
},
@@ -196,7 +205,14 @@ module.exports = function(RED) {
196205
}
197206
var context = vm.createContext(sandbox);
198207
try {
199-
this.script = vm.createScript(functionText);
208+
this.script = vm.createScript(functionText, {
209+
filename: 'Function node:'+this.id+(this.name?' ['+this.name+']':''), // filename for stack traces
210+
displayErrors: true
211+
// Using the following options causes node 4/6 to not include the line number
212+
// in the stack output. So don't use them.
213+
// lineOffset: -11, // line number offset to be used for stack traces
214+
// columnOffset: 0, // column number offset to be used for stack traces
215+
});
200216
this.on("input", function(msg) {
201217
try {
202218
var start = process.hrtime();
@@ -211,6 +227,13 @@ module.exports = function(RED) {
211227
this.status({fill:"yellow",shape:"dot",text:""+converted});
212228
}
213229
} catch(err) {
230+
//remove unwanted part
231+
var index = err.stack.search(/\n\s*at ContextifyScript.Script.runInContext/);
232+
err.stack = err.stack.slice(0, index).split('\n').slice(0,-1).join('\n');
233+
var stack = err.stack.split(/\r?\n/);
234+
235+
//store the error in msg to be used in flows
236+
msg.error = err;
214237
215238
var line = 0;
216239
var errorMessage;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright JS Foundation and other contributors, http://js.foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
17+
var should = require("should");
18+
var helper = require("node-red-node-test-helper");
19+
var functionNode = require("../../../nodegen/node-red-contrib-log-a-debug-message");
20+
21+
describe('node-red-contrib-log-a-debug-message', function () {
22+
23+
before(function (done) {
24+
helper.startServer(done);
25+
});
26+
27+
after(function(done) {
28+
helper.stopServer(done);
29+
});
30+
31+
afterEach(function () {
32+
helper.unload();
33+
});
34+
35+
it('should be loaded', function (done) {
36+
var flow = [{id: "n1", type: "log-a-debug-message", name: "log-a-debug-message" }];
37+
helper.load(functionNode, flow, function () {
38+
var n1 = helper.getNode('n1');
39+
n1.should.have.property('name', 'log-a-debug-message');
40+
done();
41+
});
42+
});
43+
it('should log a Debug Message', function (done) {
44+
var flow = [{id: "n1", type: "log-a-debug-message", wires: [["n2"]]},
45+
{id: "n2", type: "helper"}];
46+
helper.load(functionNode, flow, function () {
47+
var n1 = helper.getNode("n1");
48+
n1.receive({payload: "foo", topic: "bar"});
49+
try {
50+
helper.log().called.should.be.true();
51+
var logEvents = helper.log().args.filter(function (evt) {
52+
return evt[0].type == "log-a-debug-message";
53+
});
54+
logEvents.should.have.length(1);
55+
var msg = logEvents[0][0];
56+
msg.should.have.property('level', helper.log().DEBUG);
57+
msg.should.have.property('id', 'n1');
58+
msg.should.have.property('type', 'log-a-debug-message');
59+
msg.should.have.property('msg', 'test');
60+
done();
61+
} catch (err) {
62+
done(err);
63+
}
64+
});
65+
});
66+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright JS Foundation and other contributors, http://js.foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
17+
var should = require("should");
18+
var helper = require("node-red-node-test-helper");
19+
var functionNode = require("../../../nodegen/node-red-contrib-log-a-trace-message");
20+
21+
describe('node-red-contrib-log-a-trace-message', function () {
22+
23+
before(function (done) {
24+
helper.startServer(done);
25+
});
26+
27+
after(function(done) {
28+
helper.stopServer(done);
29+
});
30+
31+
afterEach(function () {
32+
helper.unload();
33+
});
34+
35+
it('should be loaded', function (done) {
36+
var flow = [{id: "n1", type: "log-a-trace-message", name: "log-a-trace-message" }];
37+
helper.load(functionNode, flow, function () {
38+
var n1 = helper.getNode('n1');
39+
n1.should.have.property('name', 'log-a-trace-message');
40+
done();
41+
});
42+
});
43+
it('should log a Trace Message', function (done) {
44+
var flow = [{id: "n1", type: "log-a-trace-message", wires: [["n2"]]},
45+
{id: "n2", type: "helper"}];
46+
helper.load(functionNode, flow, function () {
47+
var n1 = helper.getNode("n1");
48+
n1.receive({payload: "foo", topic: "bar"});
49+
try {
50+
helper.log().called.should.be.true();
51+
var logEvents = helper.log().args.filter(function (evt) {
52+
return evt[0].type == "log-a-trace-message";
53+
});
54+
logEvents.should.have.length(1);
55+
var msg = logEvents[0][0];
56+
msg.should.have.property('level', helper.log().TRACE);
57+
msg.should.have.property('id', 'n1');
58+
msg.should.have.property('type', 'log-a-trace-message');
59+
msg.should.have.property('msg', 'test');
60+
done();
61+
} catch (err) {
62+
done(err);
63+
}
64+
});
65+
});
66+
});
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright JS Foundation and other contributors, http://js.foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
17+
var should = require("should");
18+
var helper = require("node-red-node-test-helper");
19+
var functionNode = require("../../../nodegen/node-red-contrib-use-the-same-date-object-from-outside-the-sandbox");
20+
21+
describe('use-the-same-date-object-from-outside-the-sandbox', function () {
22+
23+
before(function (done) {
24+
helper.startServer(done);
25+
});
26+
27+
after(function (done) {
28+
helper.stopServer(done);
29+
});
30+
31+
afterEach(function () {
32+
helper.unload();
33+
});
34+
35+
it('should be loaded', function (done) {
36+
var flow = [{id: "n1", type: "use-the-same-date-object-from-outside-the-sandbox", name: "use-the-same-date-object-from-outside-the-sandbox" }];
37+
helper.load(functionNode, flow, function () {
38+
var n1 = helper.getNode('n1');
39+
n1.should.have.property('name', 'use-the-same-date-object-from-outside-the-sandbox');
40+
done();
41+
});
42+
});
43+
it('should use the same Date object from outside the sandbox', function (done) {
44+
var flow = [{id: "n1", type: "use-the-same-date-object-from-outside-the-sandbox", wires: [["n2"]]},
45+
{id: "n2", type: "helper"}];
46+
helper.load(functionNode, flow, function () {
47+
var n1 = helper.getNode("n1");
48+
var n2 = helper.getNode("n2");
49+
n1.context().global.set("typeTest", function (d) { return d instanceof Date });
50+
n2.on("input", function (msg) {
51+
msg.should.have.property('payload', true);
52+
done();
53+
});
54+
n1.receive({payload: "foo", topic: "bar"});
55+
});
56+
});
57+
});

0 commit comments

Comments
 (0)