@5minds/node-red-contrib-processcube-amqp 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Dockerfile ADDED
@@ -0,0 +1,16 @@
1
+ FROM node:20 as builder
2
+
3
+
4
+ COPY ./ /src/node-red-contrib-processcube-amqp
5
+
6
+ WORKDIR /src/node-red-contrib-processcube-amqp
7
+
8
+ RUN npm install
9
+
10
+
11
+ FROM nodered/node-red:latest
12
+
13
+
14
+ COPY --from=builder /src/node-red-contrib-processcube-amqp /src/node-red-contrib-processcube-amqp
15
+
16
+ RUN npm install /src/node-red-contrib-processcube-amqp/
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 5Minds IT-Solutions GmbH & Co. KG
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # node-red-contrib-processcube-amqp
@@ -0,0 +1,18 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('amqp-config',{
3
+ category: 'config',
4
+ defaults: {
5
+ connectionString: {value:"amqp://localhost:5672",required:true}
6
+ },
7
+ label: function() {
8
+ return this.connectionString;
9
+ }
10
+ });
11
+ </script>
12
+
13
+ <script type="text/html" data-template-name="amqp-config">
14
+ <div class="form-row">
15
+ <label for="node-config-input-connectionString"><i class="fa fa-bookmark"></i> ConnectionString</label>
16
+ <input type="text" id="node-config-input-connectionString">
17
+ </div>
18
+ </script>
package/amqp-config.js ADDED
@@ -0,0 +1,7 @@
1
+ module.exports = function(RED) {
2
+ function AMQPServerNode(n) {
3
+ RED.nodes.createNode(this,n);
4
+ this.connectionString = n.connectionString;
5
+ }
6
+ RED.nodes.registerType("amqp-config",AMQPServerNode);
7
+ }
@@ -0,0 +1,63 @@
1
+ const amqplib = require('amqplib');
2
+ const { v4: uuidv4 } = require('uuid');
3
+
4
+ module.exports = function(connectionString) {
5
+ this.connectionString = connectionString;
6
+ this.onCloseHandler = {};
7
+
8
+ this.connect = async function() {
9
+ console.log('connect!!!');
10
+
11
+ this.connection = await createConnection(this.connectionString);
12
+ this.connection.on("close", async () => {
13
+ this.connection = undefined;
14
+
15
+ const onCloseHandler = this.onCloseHandler;
16
+ this.onCloseHandler = {};
17
+
18
+ const promises = [];
19
+
20
+
21
+ for (const [key, handler] of Object.entries(onCloseHandler)) {
22
+ promises.push(handler());
23
+ }
24
+
25
+ await this.connect();
26
+ await Promise.all(promises);
27
+ });
28
+ }
29
+
30
+ this.createChannel = async function() {
31
+ while (!this.connection) {
32
+ await sleep(100);
33
+ }
34
+
35
+ return await this.connection.createChannel();
36
+ }
37
+
38
+ this.onClose = function (handler) {
39
+ const handlerId = uuidv4();
40
+ this.onCloseHandler[handlerId] = handler;
41
+
42
+ return handlerId;
43
+ }
44
+
45
+ this.removeOnCloseHandler = function(handlerId) {
46
+ delete this.onCloseHandler[handlerId];
47
+ }
48
+
49
+ async function createConnection(connectionString) {
50
+
51
+ while (true) {
52
+ try {
53
+ return await amqplib.connect(connectionString);
54
+ } catch (e) {
55
+ await sleep(500);
56
+ }
57
+ }
58
+ }
59
+ }
60
+
61
+ async function sleep(ms) {
62
+ return new Promise(resolve => setTimeout(resolve, ms));
63
+ }
@@ -0,0 +1,67 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('amqp-input',{
3
+ category: 'ProcessCube',
4
+ color: '#00aed7',
5
+ defaults: {
6
+ name: {value: "", type: "string"},
7
+ amqpServer: {value: "", type: "amqp-config"},
8
+ exchange: {value: "", type: "string", required:true},
9
+ exchangeType: { value: 'fanout'},
10
+ routingKey: { value: "" },
11
+ queue: {value: "", type: "string", required:true}
12
+ },
13
+ inputs: 0,
14
+ outputs: 1,
15
+ icon: "font-awesome/fa-envelope-open",
16
+ label: function() {
17
+ return this.name||"amqp-input";
18
+ },
19
+ oneditprepare: function () {
20
+
21
+ $('#node-input-exchangeType').change(function (e) {
22
+ const exchangeType = this.value
23
+
24
+ if (['fanout'].includes(exchangeType)) {
25
+ $('.routing-key-form-input').hide()
26
+ } else {
27
+ $('.routing-key-form-input').show()
28
+ }
29
+ })
30
+ },
31
+ });
32
+ </script>
33
+
34
+ <script type="text/html" data-template-name="amqp-input">
35
+ <div class="form-row">
36
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
37
+ <input type="text" id="node-input-name" placeholder="Name">
38
+ </div>
39
+ <div class="form-row">
40
+ <label for="node-input-amqpServer"><i class="fa fa-tag"></i> AMQP Server</label>
41
+ <input type="text" id="node-input-amqpServer" placeholder="">
42
+ </div>
43
+ <div class="form-row wide-label-amqp-in">
44
+ <label for="node-input-exchangeType"><i class="fa fa-filter"></i>&nbsp;&nbsp;Type</label>
45
+ <select id="node-input-exchangeType">
46
+ <option value="topic">Topic</option>
47
+ <option value="direct">Direct</option>
48
+ <option value="fanout">Fanout</option>
49
+ </select>
50
+ </div>
51
+ <div class="form-row">
52
+ <label for="node-input-exchange"><i class="fa fa-tag"></i> Exchange</label>
53
+ <input type="text" id="node-input-exchange" placeholder="Exchange">
54
+ </div>
55
+ <div class="form-row wide-label-amqp-in routing-key-form-input">
56
+ <label for="node-input-routingKey"><i class="fa fa-filter"></i>&nbsp;&nbsp;Routing Key</label>
57
+ <input type="text" id="node-input-routingKey">
58
+ </div>
59
+ <div class="form-row">
60
+ <label for="node-input-queue"><i class="fa fa-tag"></i> Queue</label>
61
+ <input type="text" id="node-input-queue" placeholder="Queue">
62
+ </div>
63
+ </script>
64
+
65
+ <script type="text/html" data-help-name="amqp-input">
66
+ <p>A node to consume AMQP Messages.</p>
67
+ </script>
package/amqp-input.js ADDED
@@ -0,0 +1,61 @@
1
+ const AMQPConnection = require('./amqp-connection');
2
+
3
+ function showStatus(node, msgCounter) {
4
+ if (msgCounter >= 1) {
5
+ node.status({fill: "blue", shape: "dot", text: `handling tasks ${msgCounter}`});
6
+ } else {
7
+ node.status({fill: "blue", shape: "ring", text: `subcribed ${msgCounter}`});
8
+ }
9
+ }
10
+
11
+ module.exports = function(RED) {
12
+
13
+ function AMQPInput(config) {
14
+ RED.nodes.createNode(this,config);
15
+ var node = this;
16
+ var flowContext = node.context().flow;
17
+
18
+ const amqpServer = RED.nodes.getNode(config.amqpServer);
19
+
20
+ async function initNode() {
21
+ var connection = flowContext.get('amqpConnection');
22
+
23
+ if (!connection) {
24
+ connection = new AMQPConnection(amqpServer.connectionString);
25
+ flowContext.set('amqpConnection', connection);
26
+ await connection.connect();
27
+ }
28
+
29
+ const channel = await connection.createChannel();
30
+ await channel.assertExchange(config.exchange, config.exchangeType);
31
+ const queue = await channel.assertQueue(config.queue);
32
+ await channel.bindQueue(queue.queue, config.exchange, config.routingKey);
33
+
34
+ await channel.consume(queue.queue, async (message) => {
35
+ const msg = {
36
+ payload: JSON.parse(message.content.toString())
37
+ };
38
+
39
+ node.send(msg);
40
+ channel.ack(message);
41
+ });
42
+
43
+ const onCloseHandlerId = connection.onClose(async () => {
44
+ console.log('onClose connection');
45
+ await initNode();
46
+ });
47
+
48
+ node.on("close", async () => {
49
+ console.log('closeChannel');
50
+ try {
51
+ await channel.close();
52
+ connection.removeOnCloseHandler(onCloseHandlerId);
53
+ } catch {
54
+ console.warn('Channel closed');
55
+ }
56
+ });
57
+ }
58
+ initNode();
59
+ }
60
+ RED.nodes.registerType("amqp-input", AMQPInput);
61
+ }
@@ -0,0 +1,62 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('amqp-output',{
3
+ category: 'ProcessCube',
4
+ color: '#00aed7',
5
+ defaults: {
6
+ name: {value: "", type: "string"},
7
+ amqpServer: {value: "", type: "amqp-config"},
8
+ exchange: {value: "", type: "string", required:true},
9
+ exchangeType: { value: 'fanout'},
10
+ routingKey: { value: "" },
11
+ },
12
+ inputs: 1,
13
+ outputs: 0,
14
+ icon: "font-awesome/fa-envelope-open",
15
+ label: function() {
16
+ return this.name||"amqp-output";
17
+ },
18
+ oneditprepare: function () {
19
+
20
+ $('#node-input-exchangeType').change(function (e) {
21
+ const exchangeType = this.value
22
+
23
+ if (['fanout'].includes(exchangeType)) {
24
+ $('.routing-key-form-input').hide()
25
+ } else {
26
+ $('.routing-key-form-input').show()
27
+ }
28
+ })
29
+ },
30
+ });
31
+ </script>
32
+
33
+ <script type="text/html" data-template-name="amqp-output">
34
+ <div class="form-row">
35
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
36
+ <input type="text" id="node-input-name" placeholder="Name">
37
+ </div>
38
+ <div class="form-row">
39
+ <label for="node-input-amqpServer"><i class="fa fa-tag"></i> AMQP Server</label>
40
+ <input type="text" id="node-input-amqpServer" placeholder="">
41
+ </div>
42
+ <div class="form-row wide-label-amqp-in">
43
+ <label for="node-input-exchangeType"><i class="fa fa-filter"></i>&nbsp;&nbsp;Type</label>
44
+ <select id="node-input-exchangeType">
45
+ <option value="topic">Topic</option>
46
+ <option value="direct">Direct</option>
47
+ <option value="fanout">Fanout</option>
48
+ </select>
49
+ </div>
50
+ <div class="form-row">
51
+ <label for="node-input-exchange"><i class="fa fa-tag"></i> Exchange</label>
52
+ <input type="text" id="node-input-exchange" placeholder="Exchange">
53
+ </div>
54
+ <div class="form-row wide-label-amqp-in routing-key-form-input">
55
+ <label for="node-input-routingKey"><i class="fa fa-filter"></i>&nbsp;&nbsp;Routing Key</label>
56
+ <input type="text" id="node-input-routingKey">
57
+ </div>
58
+ </script>
59
+
60
+ <script type="text/html" data-help-name="amqp-output">
61
+ <p>A node to publish AMQP Messages.</p>
62
+ </script>
package/amqp-output.js ADDED
@@ -0,0 +1,29 @@
1
+ const amqplib = require('amqplib');
2
+ const AMQPConnection = require('./amqp-connection');
3
+
4
+ module.exports = function(RED) {
5
+ function AMQPOutput(config) {
6
+ RED.nodes.createNode(this,config);
7
+ var node = this;
8
+ var flowContext = node.context().flow;
9
+
10
+ const amqpServer = RED.nodes.getNode(config.amqpServer);
11
+
12
+ node.on('input', async (msg) => {
13
+ var connection = flowContext.get('amqpConnection');
14
+
15
+ if (!connection) {
16
+ connection = new AMQPConnection(amqpServer.connectionString);
17
+ flowContext.set('amqpConnection', connection);
18
+ }
19
+
20
+ const channel = await connection.createChannel();
21
+ await channel.assertExchange(config.exchange, config.exchangeType);
22
+
23
+ await channel.publish(config.exchange, config.routingKey, Buffer.from(JSON.stringify(msg.payload)));
24
+ await channel.close();
25
+ });
26
+
27
+ }
28
+ RED.nodes.registerType("amqp-output", AMQPOutput);
29
+ }
@@ -0,0 +1,18 @@
1
+ services:
2
+ node-red:
3
+ image: 5minds/node-red:latest
4
+ build:
5
+ context: .
6
+ environment:
7
+ - TZ=Europe/Berlin
8
+ ports:
9
+ - "1880:1880"
10
+ volumes:
11
+ - ./.node-red/data:/data
12
+ - ./:/data/node_modules/node-red-contrib-processcube-amqp
13
+
14
+ rabbitmq:
15
+ image: "rabbitmq:3-management"
16
+ ports:
17
+ - "5672:5672"
18
+ - "15672:15672"
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@5minds/node-red-contrib-processcube-amqp",
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "description": "Node-RED nodes for AMQP",
6
+ "authors": [
7
+ {
8
+ "name": "Martin Moellenbeck",
9
+ "email": "Martin.Moellenbeck@5Minds.de"
10
+ },
11
+ {
12
+ "name": "Robin Lenz",
13
+ "email": "Robin.Lenz@5Minds.de"
14
+ }
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/5minds/node-red-contrib-processcube-amqp"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/5minds/node-red-contrib-processcube-amqp",
22
+ "email": "processcube@5minds.de"
23
+ },
24
+ "engines": {
25
+ "node": ">=14",
26
+ "npm": ">=9.8"
27
+ },
28
+ "node-red": {
29
+ "version": ">=3.1.9",
30
+ "nodes": {
31
+ "amqpInput": "amqp-input.js",
32
+ "amqpOutput": "amqp-output.js",
33
+ "amqpConfig": "amqp-config.js"
34
+ }
35
+ },
36
+ "dependencies": {
37
+ "amqplib": "0.10.4",
38
+ "uuid": "^9.0.1"
39
+ },
40
+ "keywords": [
41
+ "node-red",
42
+ "processcube",
43
+ "amqp",
44
+ "messagebus",
45
+ "rabbitmq"
46
+ ]
47
+ }