Created
March 15, 2023 20:30
-
-
Save sonntam/35615fd44f341b0e50e58cf0a3ff3642 to your computer and use it in GitHub Desktop.
Node-Red subflow creating a subflow node that functions as an async version of the built-in exec node with stdin support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": "6ea632aca2282da9", | |
"type": "subflow", | |
"name": "exec stdin", | |
"info": "## Inputs \r\n\r\nSimilar to the exec node this starts a process.\r\nThe input message format is as follows:\r\n\r\n```js\r\nmsg = {\r\n topic: \"<command to be executed>\",\r\n timeout: <timeout in milliseconds before process is killed>,\r\n payload: <passed to stdin:optional>\r\n}\r\n```\r\n\r\n## Outputs\r\n\r\nThe first output contains error message from the underlying\r\nnode.js functionality (if any).\r\n\r\nThe second output contains stdout output.\r\n\r\nThe third output contains stderr output.", | |
"category": "function", | |
"in": [ | |
{ | |
"x": 60, | |
"y": 80, | |
"wires": [ | |
{ | |
"id": "0c0c99c7af618215" | |
} | |
] | |
} | |
], | |
"out": [ | |
{ | |
"x": 760, | |
"y": 80, | |
"wires": [ | |
{ | |
"id": "0c0c99c7af618215", | |
"port": 0 | |
} | |
] | |
}, | |
{ | |
"x": 770, | |
"y": 140, | |
"wires": [ | |
{ | |
"id": "0c0c99c7af618215", | |
"port": 1 | |
} | |
] | |
}, | |
{ | |
"x": 770, | |
"y": 200, | |
"wires": [ | |
{ | |
"id": "0c0c99c7af618215", | |
"port": 2 | |
} | |
] | |
} | |
], | |
"env": [], | |
"meta": {}, | |
"color": "#dc8e6b", | |
"inputLabels": [ | |
"cmd stdin" | |
], | |
"outputLabels": [ | |
"err", | |
"stdout", | |
"stderr" | |
], | |
"icon": "node-red/cog.svg" | |
}, | |
{ | |
"id": "0c0c99c7af618215", | |
"type": "function", | |
"z": "6ea632aca2282da9", | |
"name": "exec", | |
"func": "var exec = childProcess.exec;\n\nvar child = exec(msg.topic, \n {\n timeout: msg.timeout\n },\n function (err,stdout,stderr) {\n node.send({ payload: err });\n node.send([null, Object.assign({}, msg, { \n payload: stdout \n })]);\n node.send([null, null, Object.assign({}, msg, {\n payload: stderr\n })]);\n });\n\nvar stdinStream = new stream.Readable();\nif( msg.hasOwnProperty(\"payload\") && msg.payload != null)\n{\n stdinStream.push(msg.payload.toString()); // Add data to the internal queue for users of the stream to consume\n stdinStream.push(null); // Signals the end of the stream (EOF)\n stdinStream.pipe(child.stdin);\n}", | |
"outputs": 3, | |
"noerr": 0, | |
"initialize": "", | |
"finalize": "", | |
"libs": [ | |
{ | |
"var": "childProcess", | |
"module": "child_process" | |
}, | |
{ | |
"var": "stream", | |
"module": "stream" | |
} | |
], | |
"x": 530, | |
"y": 80, | |
"wires": [ | |
[], | |
[], | |
[] | |
] | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment