diff --git a/packages/nodes-base/nodes/Amqp/Amqp.node.ts b/packages/nodes-base/nodes/Amqp/Amqp.node.ts index 3fd8133711..14637d44aa 100644 --- a/packages/nodes-base/nodes/Amqp/Amqp.node.ts +++ b/packages/nodes-base/nodes/Amqp/Amqp.node.ts @@ -1,4 +1,9 @@ -import { ContainerOptions, Delivery } from 'rhea'; +import { + create_container, + ContainerOptions, + Dictionary, + EventContext, +} from 'rhea'; import { IExecuteFunctions } from 'n8n-core'; import { @@ -50,6 +55,13 @@ export class Amqp implements INodeType { placeholder: 'Add Option', default: {}, options: [ + { + displayName: 'Container ID', + name: 'containerId', + type: 'string', + default: '', + description: 'Will be used to pass to the RHEA Backend as container_id', + }, { displayName: 'Data as Object', name: 'dataAsObject', @@ -57,6 +69,20 @@ export class Amqp implements INodeType { default: false, description: 'Send the data as an object.', }, + { + displayName: 'Reconnect', + name: 'reconnect', + type: 'boolean', + default: true, + description: 'Automatically reconnect if disconnected', + }, + { + displayName: 'Reconnect Limit', + name: 'reconnectLimit', + type: 'number', + default: 50, + description: 'Maximum number of reconnect attempts', + }, { displayName: 'Send property', name: 'sendOnlyProperty', @@ -69,7 +95,7 @@ export class Amqp implements INodeType { ], }; - async execute(this: IExecuteFunctions): Promise < INodeExecutionData[][] > { + async execute(this: IExecuteFunctions): Promise { const credentials = this.getCredentials('amqp'); if (!credentials) { throw new Error('Credentials are mandatory!'); @@ -78,40 +104,44 @@ export class Amqp implements INodeType { const sink = this.getNodeParameter('sink', 0, '') as string; const applicationProperties = this.getNodeParameter('headerParametersJson', 0, {}) as string | object; const options = this.getNodeParameter('options', 0, {}) as IDataObject; + const containerId = options.containerId as string; + const containerReconnect = options.reconnect as boolean || true; + const containerReconnectLimit = options.reconnectLimit as number || 50; - let headerProperties = applicationProperties; + let headerProperties: Dictionary; // tslint:disable-line:no-any if (typeof applicationProperties === 'string' && applicationProperties !== '') { headerProperties = JSON.parse(applicationProperties); + } else { + headerProperties = applicationProperties as object; } if (sink === '') { throw new Error('Queue or Topic required!'); } - const container = require('rhea'); + const container = create_container(); + /* + Values are documentet here: https://github.com/amqp/rhea#container + */ const connectOptions: ContainerOptions = { host: credentials.hostname, hostname: credentials.hostname, port: credentials.port, - reconnect: true, // this id the default anyway - reconnect_limit: 50, // try for max 50 times, based on a back-off algorithm + reconnect: containerReconnect, + reconnect_limit: containerReconnectLimit, + username: credentials.username ? credentials.username : undefined, + password: credentials.password ? credentials.password : undefined, + transport: credentials.transportType ? credentials.transportType : undefined, + container_id: containerId ? containerId : undefined, + id: containerId ? containerId : undefined, }; - if (credentials.username || credentials.password) { - container.options.username = credentials.username; - container.options.password = credentials.password; - connectOptions.username = credentials.username; - connectOptions.password = credentials.password; - } - if (credentials.transportType !== '') { - connectOptions.transport = credentials.transportType; - } - const conn = container.connect(connectOptions); + const sender = conn.open_sender(sink); const responseData: IDataObject[] = await new Promise((resolve) => { - container.once('sendable', (context: any) => { // tslint:disable-line:no-any + container.once('sendable', (context: EventContext) => { const returnData = []; const items = this.getInputData(); @@ -129,12 +159,12 @@ export class Amqp implements INodeType { body = JSON.stringify(body); } - const result = context.sender.send({ + const result = context.sender?.send({ application_properties: headerProperties, body, }); - returnData.push({ id: result.id }); + returnData.push({ id: result?.id }); } resolve(returnData); diff --git a/packages/nodes-base/nodes/Amqp/AmqpTrigger.node.ts b/packages/nodes-base/nodes/Amqp/AmqpTrigger.node.ts index 40af50c4f2..cd9510a9a4 100644 --- a/packages/nodes-base/nodes/Amqp/AmqpTrigger.node.ts +++ b/packages/nodes-base/nodes/Amqp/AmqpTrigger.node.ts @@ -1,4 +1,10 @@ -import { ContainerOptions } from 'rhea'; +import { + create_container, + ContainerOptions, + EventContext, + Message, + ReceiverOptions, +} from 'rhea'; import { ITriggerFunctions } from 'n8n-core'; import { @@ -61,6 +67,13 @@ export class AmqpTrigger implements INodeType { placeholder: 'Add Option', default: {}, options: [ + { + displayName: 'Container ID', + name: 'containerId', + type: 'string', + default: '', + description: 'Will be used to pass to the RHEA Backend as container_id', + }, { displayName: 'Convert Body To String', name: 'jsonConvertByteArrayToString', @@ -75,6 +88,13 @@ export class AmqpTrigger implements INodeType { default: false, description: 'Parse the body to an object.', }, + { + displayName: 'Messages per Cicle', + name: 'pullMessagesNumber', + type: 'number', + default: 100, + description: 'Number of messages to pull from the bus for every cicle', + }, { displayName: 'Only Body', name: 'onlyBody', @@ -83,11 +103,18 @@ export class AmqpTrigger implements INodeType { description: 'Returns only the body property.', }, { - displayName: 'Messages per Cicle', - name: 'pullMessagesNumber', + displayName: 'Reconnect', + name: 'reconnect', + type: 'boolean', + default: true, + description: 'Automatically reconnect if disconnected', + }, + { + displayName: 'Reconnect Limit', + name: 'reconnectLimit', type: 'number', - default: 100, - description: 'Number of messages to pull from the bus for every cicle', + default: 50, + description: 'Maximum number of reconnect attempts', }, { displayName: 'Sleep Time', @@ -113,7 +140,10 @@ export class AmqpTrigger implements INodeType { const clientname = this.getNodeParameter('clientname', '') as string; const subscription = this.getNodeParameter('subscription', '') as string; const options = this.getNodeParameter('options', {}) as IDataObject; - const pullMessagesNumber = options.pullMessagesNumber || 100; + const pullMessagesNumber = options.pullMessagesNumber as number || 100; + const containerId = options.containerId as string; + const containerReconnect = options.reconnect as boolean || true; + const containerReconnectLimit = options.reconnectLimit as number || 50; if (sink === '') { throw new Error('Queue or Topic required!'); @@ -125,34 +155,22 @@ export class AmqpTrigger implements INodeType { durable = true; } - const container = require('rhea'); - const connectOptions: ContainerOptions = { - host: credentials.hostname, - hostname: credentials.hostname, - port: credentials.port, - reconnect: true, // this id the default anyway - reconnect_limit: 50, // try for max 50 times, based on a back-off algorithm - container_id: (durable ? clientname : null), - }; - if (credentials.username || credentials.password) { - // Old rhea implementation. not shure if it is neccessary - container.options.username = credentials.username; - container.options.password = credentials.password; - connectOptions.username = credentials.username; - connectOptions.password = credentials.password; - } - if (credentials.transportType) { - connectOptions.transport = credentials.transportType; - } + const container = create_container(); - let lastMsgId: number | undefined = undefined; + let lastMsgId: string | number | Buffer | undefined = undefined; const self = this; - container.on('receiver_open', (context: any) => { // tslint:disable-line:no-any - context.receiver.add_credit(pullMessagesNumber); + container.on('receiver_open', (context: EventContext) => { + context.receiver?.add_credit(pullMessagesNumber); }); - container.on('message', (context: any) => { // tslint:disable-line:no-any + container.on('message', (context: EventContext) => { + + // No message in the context + if (!context.message) { + return; + } + // ignore duplicate message check, don't think it's necessary, but it was in the rhea-lib example code if (context.message.message_id && context.message.message_id === lastMsgId) { return; @@ -167,6 +185,12 @@ export class AmqpTrigger implements INodeType { data.body = String.fromCharCode.apply(null, JSON.parse(cont).data); } + if (options.jsonConvertByteArrayToString === true && data.body.content !== undefined) { + // The buffer is not ready... Stringify and parse back to load it. + const cont = JSON.stringify(data.body.content); + data.body = String.fromCharCode.apply(null, JSON.parse(cont).data); + } + if (options.jsonConvertByteArrayToString === true && data.body.content !== undefined) { // The buffer is not ready... Stringify and parse back to load it. const content = JSON.stringify(data.body.content); @@ -181,35 +205,41 @@ export class AmqpTrigger implements INodeType { } - self.emit([self.helpers.returnJsonArray([data])]); + self.emit([self.helpers.returnJsonArray([data as any])]); // tslint:disable-line:no-any - if (context.receiver.credit === 0) { + if (!context.receiver?.has_credit()) { setTimeout(() => { - context.receiver.add_credit(pullMessagesNumber); + context.receiver?.add_credit(pullMessagesNumber); }, options.sleepTime as number || 10); } }); + /* + Values are documentet here: https://github.com/amqp/rhea#container + */ + const connectOptions: ContainerOptions = { + host: credentials.hostname, + hostname: credentials.hostname, + port: credentials.port, + reconnect: containerReconnect, + reconnect_limit: containerReconnectLimit, + username: credentials.username ? credentials.username : undefined, + password: credentials.password ? credentials.password : undefined, + transport: credentials.transportType ? credentials.transportType : undefined, + container_id: containerId ? containerId : undefined, + id: containerId ? containerId : undefined, + }; const connection = container.connect(connectOptions); - let clientOptions = undefined; - if (durable) { - clientOptions = { - name: subscription, - source: { - address: sink, - durable: 2, - expiry_policy: 'never', - }, - credit_window: 0, // prefetch 1 - }; - } else { - clientOptions = { - source: { - address: sink, - }, - credit_window: 0, // prefetch 1 - }; - } + + const clientOptions: ReceiverOptions = { + name: subscription ? subscription : undefined, + source: { + address: sink, + durable: (durable ? 2 : undefined), + expiry_policy: (durable ? 'never' : undefined), + }, + credit_window: 0, // prefetch 1 + }; connection.open_receiver(clientOptions); @@ -231,14 +261,15 @@ export class AmqpTrigger implements INodeType { const timeoutHandler = setTimeout(() => { reject(new Error('Aborted, no message received within 30secs. This 30sec timeout is only set for "manually triggered execution". Active Workflows will listen indefinitely.')); }, 30000); - container.on('message', (context: any) => { // tslint:disable-line:no-any + container.on('message', (context: EventContext) => { // Check if the only property present in the message is body // in which case we only emit the content of the body property // otherwise we emit all properties and their content - if (Object.keys(context.message)[0] === 'body' && Object.keys(context.message).length === 1) { - self.emit([self.helpers.returnJsonArray([context.message.body])]); + const message = context.message as Message; + if (Object.keys(message)[0] === 'body' && Object.keys(message).length === 1) { + self.emit([self.helpers.returnJsonArray([message.body])]); } else { - self.emit([self.helpers.returnJsonArray([context.message])]); + self.emit([self.helpers.returnJsonArray([message as any])]); // tslint:disable-line:no-any } clearTimeout(timeoutHandler); resolve(true);