2020-12-22 23:05:02 -08:00
|
|
|
|
import {
|
|
|
|
|
IExecuteFunctions,
|
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
IDataObject,
|
|
|
|
|
INodeExecutionData,
|
|
|
|
|
INodeType,
|
|
|
|
|
INodeTypeDescription,
|
|
|
|
|
} 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',
|
|
|
|
|
icon: 'file:rabbitmq.png',
|
|
|
|
|
group: ['transform'],
|
|
|
|
|
version: 1,
|
|
|
|
|
description: 'Sends messages to a RabbitMQ topic',
|
|
|
|
|
defaults: {
|
|
|
|
|
name: 'RabbitMQ',
|
|
|
|
|
color: '#ff6600',
|
|
|
|
|
},
|
|
|
|
|
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',
|
|
|
|
|
description: 'Publish data to queue.',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Exchange',
|
|
|
|
|
value: 'exchange',
|
|
|
|
|
description: 'Publish data to exchange.',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
default: 'queue',
|
|
|
|
|
description: 'To where data should be moved.',
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
|
// 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',
|
|
|
|
|
description: 'Name of the queue to publish to.',
|
|
|
|
|
},
|
2021-01-12 23:57:06 -08:00
|
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
|
// Exchange
|
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Exchange',
|
|
|
|
|
name: 'exchange',
|
|
|
|
|
type: 'string',
|
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
|
|
|
|
mode: [
|
|
|
|
|
'exchange',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
default: '',
|
|
|
|
|
placeholder: 'exchange-name',
|
|
|
|
|
description: 'Name of the exchange to publish to.',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Type',
|
|
|
|
|
name: 'exchangeType',
|
|
|
|
|
type: 'options',
|
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
|
|
|
|
mode: [
|
|
|
|
|
'exchange',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
name: 'Direct',
|
|
|
|
|
value: 'direct',
|
|
|
|
|
description: 'Direct exchange type.',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Topic',
|
|
|
|
|
value: 'topic',
|
|
|
|
|
description: 'Topic exchange type.',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Headers',
|
|
|
|
|
value: 'headers',
|
|
|
|
|
description: 'Headers exchange type.',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'Fanout',
|
|
|
|
|
value: 'fanout',
|
|
|
|
|
description: 'Fanout exchange type.',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
default: 'fanout',
|
|
|
|
|
description: 'Type of exchange.',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Routing key',
|
|
|
|
|
name: 'routingKey',
|
|
|
|
|
type: 'string',
|
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
|
|
|
|
mode: [
|
|
|
|
|
'exchange',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
default: '',
|
|
|
|
|
placeholder: 'routing-key',
|
|
|
|
|
description: 'The routing key for the message.',
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
|
// Default
|
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
2020-12-22 23:05:02 -08:00
|
|
|
|
{
|
|
|
|
|
displayName: 'Send Input Data',
|
|
|
|
|
name: 'sendInputData',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
default: true,
|
2021-01-12 23:57:06 -08:00
|
|
|
|
description: '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: '',
|
|
|
|
|
description: 'The message to be sent.',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Options',
|
|
|
|
|
name: 'options',
|
|
|
|
|
type: 'collection',
|
|
|
|
|
default: {},
|
|
|
|
|
placeholder: 'Add Option',
|
2021-01-12 23:57:06 -08:00
|
|
|
|
options: [
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Arguments',
|
|
|
|
|
name: 'arguments',
|
|
|
|
|
placeholder: 'Add Argument',
|
|
|
|
|
description: 'Arguments to add.',
|
|
|
|
|
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: '',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
2021-04-04 01:33:22 -07:00
|
|
|
|
{
|
|
|
|
|
displayName: 'Headers',
|
|
|
|
|
name: 'headers',
|
|
|
|
|
placeholder: 'Add Header',
|
|
|
|
|
description: 'Headers to add.',
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
displayName: 'Auto Delete',
|
|
|
|
|
name: 'autoDelete',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
default: false,
|
|
|
|
|
description: 'The queue will be deleted when the number of consumers drops to zero .',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Durable',
|
|
|
|
|
name: 'durable',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
default: true,
|
|
|
|
|
description: 'The queue will survive broker restarts.',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
displayName: 'Exclusive',
|
|
|
|
|
name: 'exclusive',
|
|
|
|
|
type: 'boolean',
|
|
|
|
|
displayOptions: {
|
|
|
|
|
show: {
|
|
|
|
|
'/mode': [
|
|
|
|
|
'queue',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
default: false,
|
|
|
|
|
description: 'Scopes the queue to the connection.',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
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',
|
|
|
|
|
},
|
|
|
|
|
],
|
2020-12-22 23:05:02 -08:00
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
|
let channel;
|
|
|
|
|
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-01-12 23:57:06 -08:00
|
|
|
|
const 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-01-12 23:57:06 -08:00
|
|
|
|
queuePromises.push(channel.sendToQueue(queue, Buffer.from(message)));
|
2020-12-22 23:05:02 -08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 23:57:06 -08:00
|
|
|
|
// @ts-ignore
|
|
|
|
|
const promisesResponses = await Promise.allSettled(queuePromises);
|
|
|
|
|
|
|
|
|
|
promisesResponses.forEach((response: IDataObject) => {
|
|
|
|
|
if (response!.status !== 'fulfilled') {
|
|
|
|
|
|
|
|
|
|
if (this.continueOnFail() !== true) {
|
|
|
|
|
throw new Error(response!.reason as string);
|
|
|
|
|
} else {
|
|
|
|
|
// Return the actual reason as error
|
|
|
|
|
returnItems.push(
|
|
|
|
|
{
|
|
|
|
|
json: {
|
|
|
|
|
error: response.reason,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
returnItems.push({
|
|
|
|
|
json: {
|
|
|
|
|
success: response.value,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await channel.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-01-12 23:57:06 -08:00
|
|
|
|
const options = this.getNodeParameter('options', 0, {}) as IDataObject;
|
2020-12-22 23:05:02 -08:00
|
|
|
|
|
2021-04-04 01:33:22 -07:00
|
|
|
|
let headers : IDataObject = {};
|
|
|
|
|
if (options.headers && ((options.headers as IDataObject).header! as IDataObject[]).length) {
|
|
|
|
|
const additionalHeaders: IDataObject = {};
|
|
|
|
|
((options.headers as IDataObject).header as IDataObject[]).forEach((header: IDataObject) => {
|
|
|
|
|
additionalHeaders[header.key as string] = header.value;
|
|
|
|
|
});
|
|
|
|
|
headers = additionalHeaders;
|
|
|
|
|
}
|
|
|
|
|
|
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:22 -07:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
promisesResponses.forEach((response: IDataObject) => {
|
|
|
|
|
if (response!.status !== 'fulfilled') {
|
|
|
|
|
|
|
|
|
|
if (this.continueOnFail() !== true) {
|
|
|
|
|
throw new Error(response!.reason as string);
|
|
|
|
|
} 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();
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`The operation "${mode}" is not known!`);
|
|
|
|
|
}
|
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();
|
|
|
|
|
}
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|