2019-11-02 04:16:20 -07:00
|
|
|
import { ContainerOptions, Delivery } from 'rhea';
|
|
|
|
|
2020-11-10 23:45:31 -08:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2019-11-01 05:50:33 -07:00
|
|
|
import {
|
2020-08-06 05:42:26 -07:00
|
|
|
IDataObject,
|
2019-11-01 05:50:33 -07:00
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2020-05-12 07:26:51 -07:00
|
|
|
export class Amqp implements INodeType {
|
2019-11-01 05:50:33 -07:00
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'AMQP Sender',
|
2020-05-12 07:26:51 -07:00
|
|
|
name: 'amqp',
|
2019-11-01 05:50:33 -07:00
|
|
|
icon: 'file:amqp.png',
|
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Sends a raw-message via AMQP 1.0, executed once per item',
|
|
|
|
defaults: {
|
|
|
|
name: 'AMQP Sender',
|
|
|
|
color: '#00FF00',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [{
|
|
|
|
name: 'amqp',
|
|
|
|
required: true,
|
|
|
|
}],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Queue / Topic',
|
|
|
|
name: 'sink',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: 'topic://sourcename.something',
|
|
|
|
description: 'name of the queue of topic to publish to',
|
|
|
|
},
|
|
|
|
// Header Parameters
|
|
|
|
{
|
|
|
|
displayName: 'Headers',
|
|
|
|
name: 'headerParametersJson',
|
|
|
|
type: 'json',
|
|
|
|
default: '',
|
|
|
|
description: 'Header parameters as JSON (flat object). Sent as application_properties in amqp-message meta info.',
|
2020-08-06 05:42:26 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Option',
|
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Data as Object',
|
|
|
|
name: 'dataAsObject',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
|
|
|
description: 'Send the data as an object.',
|
|
|
|
},
|
2020-10-28 15:29:45 -07:00
|
|
|
{
|
|
|
|
displayName: 'Send property',
|
|
|
|
name: 'sendOnlyProperty',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2020-10-28 15:30:30 -07:00
|
|
|
description: 'The only property to send. If empty the whole item will be sent.',
|
2020-10-28 15:29:45 -07:00
|
|
|
},
|
2020-08-06 05:42:26 -07:00
|
|
|
],
|
|
|
|
},
|
2020-11-10 23:45:31 -08:00
|
|
|
],
|
2019-11-01 05:50:33 -07:00
|
|
|
};
|
|
|
|
|
2020-11-10 23:45:31 -08:00
|
|
|
async execute(this: IExecuteFunctions): Promise < INodeExecutionData[][] > {
|
2019-11-01 05:50:33 -07:00
|
|
|
const credentials = this.getCredentials('amqp');
|
|
|
|
if (!credentials) {
|
|
|
|
throw new Error('Credentials are mandatory!');
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:45:31 -08:00
|
|
|
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;
|
2019-11-01 05:50:33 -07:00
|
|
|
|
|
|
|
let headerProperties = applicationProperties;
|
2020-08-05 02:09:36 -07:00
|
|
|
if (typeof applicationProperties === 'string' && applicationProperties !== '') {
|
2019-11-02 04:16:20 -07:00
|
|
|
headerProperties = JSON.parse(applicationProperties);
|
2019-11-01 05:50:33 -07:00
|
|
|
}
|
|
|
|
|
2019-11-02 04:16:20 -07:00
|
|
|
if (sink === '') {
|
2019-11-01 05:50:33 -07:00
|
|
|
throw new Error('Queue or Topic required!');
|
|
|
|
}
|
|
|
|
|
2019-11-02 04:16:20 -07:00
|
|
|
const container = require('rhea');
|
2019-11-01 05:50:33 -07:00
|
|
|
|
2019-11-02 04:16:20 -07:00
|
|
|
const connectOptions: ContainerOptions = {
|
2019-11-01 05:50:33 -07:00
|
|
|
host: credentials.hostname,
|
2020-10-28 15:29:45 -07:00
|
|
|
hostname: credentials.hostname,
|
2019-11-01 05:50:33 -07:00
|
|
|
port: credentials.port,
|
|
|
|
reconnect: true, // this id the default anyway
|
2020-08-05 02:09:36 -07:00
|
|
|
reconnect_limit: 50, // try for max 50 times, based on a back-off algorithm
|
2019-11-02 04:16:20 -07:00
|
|
|
};
|
2019-11-01 05:50:33 -07:00
|
|
|
if (credentials.username || credentials.password) {
|
|
|
|
container.options.username = credentials.username;
|
|
|
|
container.options.password = credentials.password;
|
2020-10-28 15:29:45 -07:00
|
|
|
connectOptions.username = credentials.username;
|
|
|
|
connectOptions.password = credentials.password;
|
|
|
|
}
|
2020-10-28 15:30:30 -07:00
|
|
|
if (credentials.transportType !== '') {
|
2020-10-28 15:29:45 -07:00
|
|
|
connectOptions.transport = credentials.transportType;
|
2019-11-01 05:50:33 -07:00
|
|
|
}
|
|
|
|
|
2020-11-10 23:45:31 -08:00
|
|
|
const conn = container.connect(connectOptions);
|
|
|
|
const sender = conn.open_sender(sink);
|
|
|
|
|
|
|
|
const responseData: IDataObject[] = await new Promise((resolve) => {
|
2020-11-10 23:44:59 -08:00
|
|
|
container.once('sendable', (context: any) => { // tslint:disable-line:no-any
|
2020-11-10 23:45:31 -08:00
|
|
|
const returnData = [];
|
2019-11-01 05:50:33 -07:00
|
|
|
|
2020-11-10 23:45:31 -08:00
|
|
|
const items = this.getInputData();
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
const item = items[i];
|
2020-10-28 15:29:45 -07:00
|
|
|
|
2020-11-10 23:45:31 -08:00
|
|
|
let body: IDataObject | string = item.json;
|
|
|
|
const sendOnlyProperty = options.sendOnlyProperty as string;
|
2020-08-06 05:42:26 -07:00
|
|
|
|
2020-11-10 23:45:31 -08:00
|
|
|
if (sendOnlyProperty) {
|
|
|
|
body = body[sendOnlyProperty] as string;
|
|
|
|
}
|
2020-08-06 05:42:26 -07:00
|
|
|
|
2020-11-10 23:45:31 -08:00
|
|
|
if (options.dataAsObject !== true) {
|
|
|
|
body = JSON.stringify(body);
|
|
|
|
}
|
2020-08-06 05:42:26 -07:00
|
|
|
|
2020-11-10 23:45:31 -08:00
|
|
|
const result = context.sender.send({
|
|
|
|
application_properties: headerProperties,
|
|
|
|
body,
|
|
|
|
});
|
2019-11-01 05:50:33 -07:00
|
|
|
|
2020-11-10 23:45:31 -08:00
|
|
|
returnData.push({ id: result.id });
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(returnData);
|
2019-11-01 05:50:33 -07:00
|
|
|
});
|
|
|
|
});
|
2019-11-02 04:16:20 -07:00
|
|
|
|
2020-11-10 23:44:59 -08:00
|
|
|
sender.close();
|
|
|
|
conn.close();
|
|
|
|
|
2020-11-10 23:45:31 -08:00
|
|
|
return [this.helpers.returnJsonArray(responseData)];
|
2019-11-01 05:50:33 -07:00
|
|
|
}
|
|
|
|
}
|