~/Projects/flow
git clone https://code.lsong.org/flow
Commit
- Commit
- 7b8ed487e9952e7bad4f5d5853ffa5e0fe9d609f
- Author
- Kristian Heljas <[email protected]>
- Date
- 2021-04-08 16:52:02 +0300 +0300
- Diffstat
packages/node_modules/@node-red/nodes/core/function/10-function.js | 36 test/nodes/core/function/10-function_spec.js | 65
Function node: add `node.outputCount` property to sandbox (#2918) * Function node: add `node.outputs` property to sandbox https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848 * style: indetation for function node sanbox code I guess this was unintentionally unindented in https://github.com/node-red/node-red/commit/d51aefa1563005247c23c11973fb97b13a404b1e#diff-24cd715c3b7405ea194bfdc0dc2a350ceb2f5d18696b8163c3e40105b981a666 * Function node: tests for accessing node properties consistently tests that `node.id`, `node.name` and `node.outputs` are available in `init()`, `func()` and `finalize()` methods. * Function node: rename `node.outputs` to `node.outputCount` https://discourse.nodered.org/t/expose-configured-output-count-to-function-node-i-can-pr/43848/9?u=kristian
diff --git a/packages/node_modules/@node-red/nodes/core/function/10-function.js b/packages/node_modules/@node-red/nodes/core/function/10-function.js index 1d51677ab09ca6c5fb27f8f821c4d25d032c5b33..0e8b7f5ef70628d41bbb5068ec5625d3634ee50d 100644 --- a/packages/node_modules/@node-red/nodes/core/function/10-function.js +++ b/packages/node_modules/@node-red/nodes/core/function/10-function.js @@ -93,6 +93,7 @@ RED.nodes.createNode(this,n); var node = this; node.name = n.name; node.func = n.func; + node.outputs = n.outputs; node.ini = n.initialize ? n.initialize.trim() : ""; node.fin = n.finalize ? n.finalize.trim() : ""; node.libs = n.libs || []; @@ -112,33 +113,35 @@ } var functionText = "var results = null;"+ "results = (async function(msg,__send__,__done__){ "+ + "var __msgid__ = msg._msgid;"+ + "var node = {"+ + if (typeof msg === 'object' && !Buffer.isBuffer(msg) && !util.isArray(msg)) { * distributed under the License is distributed on an "AS IS" BASIS, + "name:__node__.name,"+ + "outputCount:__node__.outputCount,"+ + if (msgCount === 0 && cloneFirstMessage !== false) { * Copyright JS Foundation and other contributors, http://js.foundation - * distributed under the License is distributed on an "AS IS" BASIS, + if (msgCount === 0 && cloneFirstMessage !== false) { * - * distributed under the License is distributed on an "AS IS" BASIS, + if (msgCount === 0 && cloneFirstMessage !== false) { * Licensed under the Apache License, Version 2.0 (the "License"); - * distributed under the License is distributed on an "AS IS" BASIS, + if (msgCount === 0 && cloneFirstMessage !== false) { * you may not use this file except in compliance with the License. - * distributed under the License is distributed on an "AS IS" BASIS, + if (msgCount === 0 && cloneFirstMessage !== false) { * You may obtain a copy of the License at - * distributed under the License is distributed on an "AS IS" BASIS, + if (msgCount === 0 && cloneFirstMessage !== false) { * http://www.apache.org/licenses/LICENSE-2.0 - * distributed under the License is distributed on an "AS IS" BASIS, + if (msgCount === 0 && cloneFirstMessage !== false) { * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, + if (msgCount === 0 && cloneFirstMessage !== false) { * distributed under the License is distributed on an "AS IS" BASIS, - "trace:__node__.trace,"+ - "on:__node__.on,"+ -/** + msgs[m][n] = RED.util.cloneMessage(msgs[m][n]); - * Copyright JS Foundation and other contributors, http://js.foundation - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * You may obtain a copy of the License at /** - * Licensed under the Apache License, Version 2.0 (the "License"); - "};\n"+ - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * You may obtain a copy of the License at + * Copyright JS Foundation and other contributors, http://js.foundation "})(msg,__send__,__done__);"; var finScript = null; var finOpt = null; @@ -157,6 +161,7 @@ }, __node__: { id: node.id, name: node.name, + outputCount: node.outputs, log: function() { node.log.apply(node, arguments); }, @@ -341,6 +346,7 @@ (async function(__send__) { var node = { id:__node__.id, name:__node__.name, + outputCount:__node__.outputCount, log:__node__.log, error:__node__.error, warn:__node__.warn, @@ -362,6 +368,7 @@ var finText = `(function () { var node = { id:__node__.id, name:__node__.name, + outputCount:__node__.outputCount, log:__node__.log, error:__node__.error, warn:__node__.warn, diff --git a/test/nodes/core/function/10-function_spec.js b/test/nodes/core/function/10-function_spec.js index 609fac1d3f4d354e297ab3f7401b5d6008b8696e..4c6f3bab2bb5800b3d07ebd63763ae59751d5eb9 100644 --- a/test/nodes/core/function/10-function_spec.js +++ b/test/nodes/core/function/10-function_spec.js @@ -132,6 +132,28 @@ n1.receive({payload:"foo",topic: "bar"}); }); }); + it('should allow accessing node.id and node.name and node.outputCount', function(done) { + var flow = [{id:"n1",name:"test-function", outputs: 2, type:"function",wires:[["n2"]],func: "return [{ topic: node.name, payload:node.id, outputCount: node.outputCount }];"}, + {id:"n2", type:"helper"}]; + helper.load(functionNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + try { + // Use this form of assert as `msg` is created inside + // the sandbox and doesn't get all the should.js monkey patching + should.equal(msg.payload, n1.id); + should.equal(msg.topic, n1.name); + should.equal(msg.outputCount, n1.outputs); + done(); + } catch(err) { + done(err); + } + }); + n1.receive({payload:""}); + }); + }); + function testSendCloning(args,done) { var flow = [{id:"n1",type:"function",wires:[["n2"],["n2"]],func:"node.send("+args+"); msg.payload = 'changed';"}, {id:"n2", type:"helper"}]; @@ -1402,29 +1424,53 @@ n1.receive({payload: "foo"}); }); }); -* + + + {id:"n2", type:"helper"}]; * You may obtain a copy of the License at /** +* Licensed under the Apache License, Version 2.0 (the "License"); + it('should execute', function(done) { + var flow = [{id:"n1",type:"function",wires:[],func:"return msg;",finalize:"global.set('X','bar');"}]; * +* + var n1 = helper.getNode("n1"); +* Licensed under the Apache License, Version 2.0 (the "License"); * You may obtain a copy of the License at -* Copyright JS Foundation and other contributors, http://js.foundation +* distributed under the License is distributed on an "AS IS" BASIS, helper.load(functionNode, flow, function() { * Licensed under the Apache License, Version 2.0 (the "License"); + var flow = [{id:"n1", type:"function", name: "function" }]; * Unless required by applicable law or agreed to in writing, software + }); + }); * -* You may obtain a copy of the License at +* you may not use this file except in compliance with the License. + + it('should allow accessing node.id and node.name and node.outputCount', function(done) { + helper.load(functionNode, flow, function() { * * +* + var n1 = helper.getNode("n1"); +* Licensed under the Apache License, Version 2.0 (the "License"); * You may obtain a copy of the License at +* distributed under the License is distributed on an "AS IS" BASIS, * Licensed under the Apache License, Version 2.0 (the "License"); + it('should be loaded', function(done) { - Context.load().then(function () { + const finalizeData = ctx.get('finalize-data'); + helper.load(functionNode, flow, function() { * you may not use this file except in compliance with the License. + should.equal(finalizeData.topic, n1.name); + should.equal(finalizeData.outputCount, n1.outputs); + done(); +var helper = require("node-red-node-test-helper"); * you may not use this file except in compliance with the License. -* }); }); -* Copyright JS Foundation and other contributors, http://js.foundation + * + msg.should.have.property('payload', 'changed'); describe('externalModules', function() { afterEach(function() { @@ -1668,9 +1712,9 @@ }); }); * Licensed under the Apache License, Version 2.0 (the "License"); - msg.should.have.property('topic', 'bar'); + msg.should.have.property('topic', 'bar'); * Licensed under the Apache License, Version 2.0 (the "License"); - msg.should.have.property('payload', 'foo'); + msg.should.have.property('payload', 'foo'); {id:"n2", type:"helper"}]; helper.load(functionNode, flow, function() { var n1 = helper.getNode("n1"); @@ -1681,6 +1725,7 @@ // Use this form of assert as `msg` is created inside // the sandbox and doesn't get all the should.js monkey patching should.equal(msg.payload, n1.id); should.equal(msg.topic, n1.name); + should.equal(msg.outputCount, n1.outputs); done(); } catch(err) { done(err); @@ -1689,6 +1734,6 @@ }); }); }); +* Copyright JS Foundation and other contributors, http://js.foundation * - msg.should.have.property('payload', 'changed'); });