2020-12-22 23:05:02 -08:00
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
IExecuteFunctions,
|
|
|
|
ITriggerFunctions,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
const amqplib = require('amqplib');
|
|
|
|
|
2021-01-12 23:57:06 -08:00
|
|
|
export async function rabbitmqConnect(this: IExecuteFunctions | ITriggerFunctions, options: IDataObject): Promise<any> { // tslint:disable-line:no-any
|
2022-04-14 23:00:47 -07:00
|
|
|
const credentials = await this.getCredentials('rabbitmq');
|
2020-12-22 23:05:02 -08:00
|
|
|
|
|
|
|
const credentialKeys = [
|
|
|
|
'hostname',
|
|
|
|
'port',
|
|
|
|
'username',
|
|
|
|
'password',
|
|
|
|
'vhost',
|
|
|
|
];
|
2021-01-12 23:57:06 -08:00
|
|
|
|
2020-12-22 23:05:02 -08:00
|
|
|
const credentialData: IDataObject = {};
|
|
|
|
credentialKeys.forEach(key => {
|
|
|
|
credentialData[key] = credentials[key] === '' ? undefined : credentials[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
const optsData: IDataObject = {};
|
|
|
|
if (credentials.ssl === true) {
|
|
|
|
credentialData.protocol = 'amqps';
|
|
|
|
|
|
|
|
optsData.ca = credentials.ca === '' ? undefined : [Buffer.from(credentials.ca as string)];
|
2021-04-15 07:20:56 -07:00
|
|
|
if (credentials.passwordless === true) {
|
|
|
|
optsData.cert = credentials.cert === '' ? undefined : Buffer.from(credentials.cert as string);
|
|
|
|
optsData.key = credentials.key === '' ? undefined : Buffer.from(credentials.key as string);
|
|
|
|
optsData.passphrase = credentials.passphrase === '' ? undefined : credentials.passphrase;
|
|
|
|
optsData.credentials = amqplib.credentials.external();
|
|
|
|
}
|
2020-12-22 23:05:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const connection = await amqplib.connect(credentialData, optsData);
|
|
|
|
|
|
|
|
connection.on('error', (error: Error) => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
const channel = await connection.createChannel().catch(console.warn);
|
|
|
|
|
|
|
|
if (options.arguments && ((options.arguments as IDataObject).argument! as IDataObject[]).length) {
|
|
|
|
const additionalArguments: IDataObject = {};
|
|
|
|
((options.arguments as IDataObject).argument as IDataObject[]).forEach((argument: IDataObject) => {
|
|
|
|
additionalArguments[argument.key as string] = argument.value;
|
|
|
|
});
|
|
|
|
options.arguments = additionalArguments;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resolve(channel);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
2021-01-12 23:57:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function rabbitmqConnectQueue(this: IExecuteFunctions | ITriggerFunctions, queue: string, options: IDataObject): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const channel = await rabbitmqConnect.call(this, options);
|
|
|
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
await channel.assertQueue(queue, options);
|
|
|
|
resolve(channel);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-12-22 23:05:02 -08:00
|
|
|
|
2021-01-12 23:57:06 -08:00
|
|
|
export async function rabbitmqConnectExchange(this: IExecuteFunctions | ITriggerFunctions, exchange: string, type: string, options: IDataObject): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const channel = await rabbitmqConnect.call(this, options);
|
|
|
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
await channel.assertExchange(exchange, type, options);
|
|
|
|
resolve(channel);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
2020-12-22 23:05:02 -08:00
|
|
|
}
|