2022-07-04 02:12:08 -07:00
|
|
|
|
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
|
2020-12-22 23:05:02 -08:00
|
|
|
|
import {
|
|
|
|
|
IExecuteFunctions,
|
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
IDataObject,
|
|
|
|
|
INodeExecutionData,
|
|
|
|
|
INodeType,
|
|
|
|
|
INodeTypeDescription,
|
2021-06-12 08:15:23 -07:00
|
|
|
|
JsonObject,
|
2021-04-16 09:33:36 -07:00
|
|
|
|
NodeApiError,
|
|
|
|
|
NodeOperationError,
|
2020-12-22 23:05:02 -08:00
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
|
|
import {
|
2021-01-12 23:57:06 -08:00
|
|
|
|
rabbitmqConnectExchange,
|
|
|
|
|
rabbitmqConnectQueue,
|
2020-12-22 23:05:02 -08:00
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
|
|
|
|
|
export class RabbitMQ implements INodeType {
|
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
|
displayName: 'RabbitMQ',
|
|
|
|
|
name: 'rabbitmq',
|
2022-06-20 07:54:01 -07:00
|
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-12-22 23:05:02 -08:00
|
|
|
|
icon: 'file:rabbitmq.png',
|
|
|
|
|
group: ['transform'],
|
|
|
|
|
version: 1,
|
|
|
|
|
description: 'Sends messages to a RabbitMQ topic',
|
|
|
|
|
defaults: {
|
|
|
|
|
name: 'RabbitMQ',
|
|
|
|
|
},
|
|
|
|
|
inputs: ['main'],
|
|
|
|
|
outputs: ['main'],
|
|
|
|
|
credentials: [
|
|
|
|
|
{
|
|
|
|
|
name: 'rabbitmq',
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
properties: [
|
2021-01-12 23:57:06 -08:00
|
|
|
|
{
|
|
|
|
|
displayName: 'Mode',
|
|
|
|
|
name: 'mode',
|
|
|
|
|
type: 'options',
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
name: 'Queue',
|
|
|
|
|
value: 'queue',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'Publish data to queue',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Exchange',
|
|
|
|
|
value: 'exchange',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'Publish data to exchange',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
default: 'queue',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'To where data should be moved',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
|
// Queue
|
|
|
|
|
// ----------------------------------
|
2020-12-22 23:05:02 -08:00
|
|
|
|
{
|
|
|
|
|
displayName: 'Queue / Topic',
|
|
|
|
|
name: 'queue',
|
|
|
|
|
type: 'string',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
|
|
|
|
mode: [
|
|
|
|
|
'queue',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-12-22 23:05:02 -08:00
|
|
|
|
default: '',
|
|
|
|
|
placeholder: 'queue-name',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'Name of the queue to publish to',
|
2020-12-22 23:05:02 -08:00
|
|
|
|
},
|
2021-01-12 23:57:06 -08:00
|
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
|
// Exchange
|
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Exchange',
|
|
|
|
|
name: 'exchange',
|
|
|
|
|
type: 'string',
|
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
|
|
|
|
mode: [
|
|
|
|
|
'exchange',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
default: '',
|
|
|
|
|
placeholder: 'exchange-name',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'Name of the exchange to publish to',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Type',
|
|
|
|
|
name: 'exchangeType',
|
|
|
|
|
type: 'options',
|
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
|
|
|
|
mode: [
|
|
|
|
|
'exchange',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
name: 'Direct',
|
|
|
|
|
value: 'direct',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'Direct exchange type',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Topic',
|
|
|
|
|
value: 'topic',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'Topic exchange type',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Headers',
|
|
|
|
|
value: 'headers',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'Headers exchange type',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Fanout',
|
|
|
|
|
value: 'fanout',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'Fanout exchange type',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
default: 'fanout',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'Type of exchange',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
},
|
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
|
displayName: 'Routing Key',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
name: 'routingKey',
|
|
|
|
|
type: 'string',
|
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
|
|
|
|
mode: [
|
|
|
|
|
'exchange',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
default: '',
|
|
|
|
|
placeholder: 'routing-key',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'The routing key for the message',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
|
// Default
|
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
2020-12-22 23:05:02 -08:00
|
|
|
|
{
|
|
|
|
|
displayName: 'Send Input Data',
|
|
|
|
|
name: 'sendInputData',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
default: true,
|
2022-06-20 07:54:01 -07:00
|
|
|
|
description: 'Whether to send the the data the node receives as JSON',
|
2020-12-22 23:05:02 -08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Message',
|
|
|
|
|
name: 'message',
|
|
|
|
|
type: 'string',
|
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
|
|
|
|
sendInputData: [
|
|
|
|
|
false,
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
default: '',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'The message to be sent',
|
2020-12-22 23:05:02 -08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Options',
|
|
|
|
|
name: 'options',
|
|
|
|
|
type: 'collection',
|
|
|
|
|
default: {},
|
|
|
|
|
placeholder: 'Add Option',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
options: [
|
2021-04-04 01:33:53 -07:00
|
|
|
|
{
|
|
|
|
|
displayName: 'Alternate Exchange',
|
|
|
|
|
name: 'alternateExchange',
|
|
|
|
|
type: 'string',
|
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
|
|
|
|
'/mode': [
|
|
|
|
|
'exchange',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
default: '',
|
|
|
|
|
description: 'An exchange to send messages to if this exchange can’t route them to any queues',
|
|
|
|
|
},
|
2021-01-12 23:57:06 -08:00
|
|
|
|
{
|
|
|
|
|
displayName: 'Arguments',
|
|
|
|
|
name: 'arguments',
|
|
|
|
|
placeholder: 'Add Argument',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'Arguments to add',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
type: 'fixedCollection',
|
|
|
|
|
typeOptions: {
|
|
|
|
|
multipleValues: true,
|
|
|
|
|
},
|
|
|
|
|
default: {},
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
name: 'argument',
|
|
|
|
|
displayName: 'Argument',
|
|
|
|
|
values: [
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Key',
|
|
|
|
|
name: 'key',
|
|
|
|
|
type: 'string',
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Value',
|
|
|
|
|
name: 'value',
|
|
|
|
|
type: 'string',
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
2022-05-30 03:16:44 -07:00
|
|
|
|
displayName: 'Auto Delete Queue',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
name: 'autoDelete',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
default: false,
|
2022-06-20 07:54:01 -07:00
|
|
|
|
description: 'Whether the queue will be deleted when the number of consumers drops to zero',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Durable',
|
|
|
|
|
name: 'durable',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
default: true,
|
2022-06-20 07:54:01 -07:00
|
|
|
|
description: 'Whether the queue will survive broker restarts',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Exclusive',
|
|
|
|
|
name: 'exclusive',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
|
|
|
|
'/mode': [
|
|
|
|
|
'queue',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
default: false,
|
2022-06-20 07:54:01 -07:00
|
|
|
|
description: 'Whether to scope the queue to the connection',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
},
|
|
|
|
|
{
|
2021-04-04 01:33:53 -07:00
|
|
|
|
displayName: 'Headers',
|
|
|
|
|
name: 'headers',
|
|
|
|
|
placeholder: 'Add Header',
|
2022-05-06 14:01:25 -07:00
|
|
|
|
description: 'Headers to add',
|
2021-04-04 01:33:53 -07:00
|
|
|
|
type: 'fixedCollection',
|
|
|
|
|
typeOptions: {
|
|
|
|
|
multipleValues: true,
|
|
|
|
|
},
|
|
|
|
|
default: {},
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
name: 'header',
|
|
|
|
|
displayName: 'Header',
|
|
|
|
|
values: [
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Key',
|
|
|
|
|
name: 'key',
|
|
|
|
|
type: 'string',
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Value',
|
|
|
|
|
name: 'value',
|
|
|
|
|
type: 'string',
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
2021-01-12 23:57:06 -08:00
|
|
|
|
],
|
|
|
|
|
},
|
2021-04-04 01:33:53 -07:00
|
|
|
|
],
|
2021-01-12 23:57:06 -08:00
|
|
|
|
},
|
|
|
|
|
],
|
2020-12-22 23:05:02 -08:00
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
2021-04-04 01:33:53 -07:00
|
|
|
|
let channel, options: IDataObject;
|
2020-12-22 23:05:02 -08:00
|
|
|
|
try {
|
|
|
|
|
const items = this.getInputData();
|
2021-01-12 23:57:06 -08:00
|
|
|
|
const mode = this.getNodeParameter('mode', 0) as string;
|
|
|
|
|
|
|
|
|
|
const returnItems: INodeExecutionData[] = [];
|
|
|
|
|
|
|
|
|
|
if (mode === 'queue') {
|
|
|
|
|
const queue = this.getNodeParameter('queue', 0) as string;
|
2020-12-22 23:05:02 -08:00
|
|
|
|
|
2021-04-04 01:33:53 -07:00
|
|
|
|
options = this.getNodeParameter('options', 0, {}) as IDataObject;
|
2020-12-22 23:05:02 -08:00
|
|
|
|
|
2021-01-12 23:57:06 -08:00
|
|
|
|
channel = await rabbitmqConnectQueue.call(this, queue, options);
|
2020-12-22 23:05:02 -08:00
|
|
|
|
|
2021-01-12 23:57:06 -08:00
|
|
|
|
const sendInputData = this.getNodeParameter('sendInputData', 0) as boolean;
|
2020-12-22 23:05:02 -08:00
|
|
|
|
|
2021-01-12 23:57:06 -08:00
|
|
|
|
let message: string;
|
2020-12-22 23:05:02 -08:00
|
|
|
|
|
2021-01-12 23:57:06 -08:00
|
|
|
|
const queuePromises = [];
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
|
if (sendInputData === true) {
|
|
|
|
|
message = JSON.stringify(items[i].json);
|
|
|
|
|
} else {
|
|
|
|
|
message = this.getNodeParameter('message', i) as string;
|
|
|
|
|
}
|
2020-12-22 23:05:02 -08:00
|
|
|
|
|
2021-04-04 01:33:53 -07:00
|
|
|
|
let headers: IDataObject = {};
|
|
|
|
|
if (options.headers && ((options.headers as IDataObject).header! as IDataObject[]).length) {
|
2021-04-04 01:49:25 -07:00
|
|
|
|
const itemOptions = this.getNodeParameter('options', i, {}) as IDataObject;
|
2021-04-04 01:33:53 -07:00
|
|
|
|
const additionalHeaders: IDataObject = {};
|
|
|
|
|
((itemOptions.headers as IDataObject).header as IDataObject[]).forEach((header: IDataObject) => {
|
|
|
|
|
additionalHeaders[header.key as string] = header.value;
|
|
|
|
|
});
|
|
|
|
|
headers = additionalHeaders;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queuePromises.push(channel.sendToQueue(queue, Buffer.from(message), { headers }));
|
2020-12-22 23:05:02 -08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 23:57:06 -08:00
|
|
|
|
// @ts-ignore
|
|
|
|
|
const promisesResponses = await Promise.allSettled(queuePromises);
|
|
|
|
|
|
2022-06-20 15:17:18 -07:00
|
|
|
|
// @ts-ignore
|
2021-06-12 08:15:23 -07:00
|
|
|
|
promisesResponses.forEach((response: JsonObject) => {
|
2021-01-12 23:57:06 -08:00
|
|
|
|
if (response!.status !== 'fulfilled') {
|
|
|
|
|
|
|
|
|
|
if (this.continueOnFail() !== true) {
|
2021-04-16 09:33:36 -07:00
|
|
|
|
throw new NodeApiError(this.getNode(), response);
|
2021-01-12 23:57:06 -08:00
|
|
|
|
} else {
|
|
|
|
|
// Return the actual reason as error
|
|
|
|
|
returnItems.push(
|
|
|
|
|
{
|
|
|
|
|
json: {
|
|
|
|
|
error: response.reason,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
returnItems.push({
|
|
|
|
|
json: {
|
|
|
|
|
success: response.value,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await channel.close();
|
2021-04-13 09:27:25 -07:00
|
|
|
|
await channel.connection.close();
|
2020-12-22 23:05:02 -08:00
|
|
|
|
}
|
2021-01-12 23:57:06 -08:00
|
|
|
|
else if (mode === 'exchange') {
|
|
|
|
|
const exchange = this.getNodeParameter('exchange', 0) as string;
|
|
|
|
|
const type = this.getNodeParameter('exchangeType', 0) as string;
|
|
|
|
|
const routingKey = this.getNodeParameter('routingKey', 0) as string;
|
2020-12-22 23:05:02 -08:00
|
|
|
|
|
2021-04-04 01:33:53 -07:00
|
|
|
|
options = this.getNodeParameter('options', 0, {}) as IDataObject;
|
2021-04-04 01:33:22 -07:00
|
|
|
|
|
2021-01-12 23:57:06 -08:00
|
|
|
|
channel = await rabbitmqConnectExchange.call(this, exchange, type, options);
|
|
|
|
|
|
|
|
|
|
const sendInputData = this.getNodeParameter('sendInputData', 0) as boolean;
|
2020-12-22 23:05:02 -08:00
|
|
|
|
|
2021-01-12 23:57:06 -08:00
|
|
|
|
let message: string;
|
2020-12-22 23:05:02 -08:00
|
|
|
|
|
2021-01-12 23:57:06 -08:00
|
|
|
|
const exchangePromises = [];
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
|
if (sendInputData === true) {
|
|
|
|
|
message = JSON.stringify(items[i].json);
|
2020-12-22 23:05:02 -08:00
|
|
|
|
} else {
|
2021-01-12 23:57:06 -08:00
|
|
|
|
message = this.getNodeParameter('message', i) as string;
|
2020-12-22 23:05:02 -08:00
|
|
|
|
}
|
2021-01-12 23:57:06 -08:00
|
|
|
|
|
2021-04-04 01:33:53 -07:00
|
|
|
|
let headers: IDataObject = {};
|
|
|
|
|
if (options.headers && ((options.headers as IDataObject).header! as IDataObject[]).length) {
|
2021-04-04 01:49:25 -07:00
|
|
|
|
const itemOptions = this.getNodeParameter('options', i, {}) as IDataObject;
|
2021-04-04 01:33:53 -07:00
|
|
|
|
const additionalHeaders: IDataObject = {};
|
|
|
|
|
((itemOptions.headers as IDataObject).header as IDataObject[]).forEach((header: IDataObject) => {
|
|
|
|
|
additionalHeaders[header.key as string] = header.value;
|
|
|
|
|
});
|
|
|
|
|
headers = additionalHeaders;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exchangePromises.push(channel.publish(exchange, routingKey, Buffer.from(message), { headers }));
|
2020-12-22 23:05:02 -08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 23:57:06 -08:00
|
|
|
|
// @ts-ignore
|
|
|
|
|
const promisesResponses = await Promise.allSettled(exchangePromises);
|
|
|
|
|
|
2022-06-20 15:17:18 -07:00
|
|
|
|
// @ts-ignore
|
2021-06-12 08:15:23 -07:00
|
|
|
|
promisesResponses.forEach((response: JsonObject) => {
|
2021-01-12 23:57:06 -08:00
|
|
|
|
if (response!.status !== 'fulfilled') {
|
|
|
|
|
|
|
|
|
|
if (this.continueOnFail() !== true) {
|
2021-04-16 09:33:36 -07:00
|
|
|
|
throw new NodeApiError(this.getNode(), response);
|
2021-01-12 23:57:06 -08:00
|
|
|
|
} else {
|
|
|
|
|
// Return the actual reason as error
|
|
|
|
|
returnItems.push(
|
|
|
|
|
{
|
|
|
|
|
json: {
|
|
|
|
|
error: response.reason,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
returnItems.push({
|
|
|
|
|
json: {
|
|
|
|
|
success: response.value,
|
|
|
|
|
},
|
|
|
|
|
});
|
2020-12-22 23:05:02 -08:00
|
|
|
|
});
|
|
|
|
|
|
2021-01-12 23:57:06 -08:00
|
|
|
|
await channel.close();
|
2021-04-13 09:27:25 -07:00
|
|
|
|
await channel.connection.close();
|
2021-01-12 23:57:06 -08:00
|
|
|
|
} else {
|
2021-04-16 09:33:36 -07:00
|
|
|
|
throw new NodeOperationError(this.getNode(), `The operation "${mode}" is not known!`);
|
2021-01-12 23:57:06 -08:00
|
|
|
|
}
|
2020-12-22 23:05:02 -08:00
|
|
|
|
|
|
|
|
|
return this.prepareOutputData(returnItems);
|
2021-01-12 23:57:06 -08:00
|
|
|
|
}
|
|
|
|
|
catch (error) {
|
2020-12-22 23:05:02 -08:00
|
|
|
|
if (channel) {
|
|
|
|
|
await channel.close();
|
2021-04-13 09:27:25 -07:00
|
|
|
|
await channel.connection.close();
|
2020-12-22 23:05:02 -08:00
|
|
|
|
}
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|