first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled

This commit is contained in:
2026-03-17 16:22:57 +03:30
commit 3d5eaf9445
15349 changed files with 2847338 additions and 0 deletions
@@ -0,0 +1,25 @@
{
"node": "n8n-nodes-base.amqp",
"nodeVersion": "1.0",
"codexVersion": "1.0",
"categories": ["Development", "Communication"],
"resources": {
"credentialDocumentation": [
{
"url": "https://docs.n8n.io/integrations/builtin/credentials/amqp/"
}
],
"primaryDocumentation": [
{
"url": "https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.amqp/"
}
],
"generic": [
{
"label": "Learn to Automate Your Factory's Incident Reporting: A Step by Step Guide",
"icon": "🏭",
"url": "https://n8n.io/blog/learn-to-automate-your-factorys-incident-reporting-a-step-by-step-guide/"
}
]
}
}
@@ -0,0 +1,218 @@
import { mock } from 'jest-mock-extended';
import type {
ICredentialDataDecryptedObject,
IExecuteFunctions,
ICredentialTestFunctions,
ICredentialsDecrypted,
} from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import { Amqp } from './Amqp.node';
// Mock the entire rhea module
const mockSender = {
close: jest.fn(),
send: jest.fn().mockReturnValue({ id: 'test-message-id' }),
};
const mockConnection = {
close: jest.fn(),
open_sender: jest.fn().mockReturnValue(mockSender),
options: { reconnect: true },
};
const mockContainer = {
connect: jest.fn().mockReturnValue(mockConnection),
on: jest.fn(),
once: jest.fn(),
};
jest.mock('rhea', () => ({
create_container: jest.fn(() => mockContainer),
}));
describe('AMQP Node', () => {
const credentials = mock<ICredentialDataDecryptedObject>({
hostname: 'localhost',
port: 5672,
username: 'testuser',
password: 'testpass',
transportType: 'tcp',
});
const executeFunctions = mock<IExecuteFunctions>({
getNode: jest.fn().mockReturnValue({ name: 'AMQP Test Node' }),
continueOnFail: jest.fn().mockReturnValue(false),
});
beforeEach(() => {
jest.clearAllMocks();
executeFunctions.getCredentials.calledWith('amqp').mockResolvedValue(credentials);
executeFunctions.getInputData.mockReturnValue([{ json: { testing: true } }]);
executeFunctions.getNodeParameter.calledWith('sink', 0).mockReturnValue('test/queue');
executeFunctions.getNodeParameter.calledWith('headerParametersJson', 0).mockReturnValue({});
executeFunctions.getNodeParameter.calledWith('options', 0).mockReturnValue({});
// Setup container event mocking
mockContainer.once.mockImplementation((event: string, callback: any) => {
if (event === 'sendable') {
// Call the callback immediately to simulate successful connection
callback({ sender: mockSender });
}
});
// Mock successful credential validation by making the connection open immediately
mockContainer.on.mockImplementation((event: string, callback: any) => {
if (event === 'connection_open') {
setImmediate(() => callback({}));
}
});
});
it('should throw error when sink is empty', async () => {
executeFunctions.getNodeParameter.calledWith('sink', 0).mockReturnValue('');
await expect(new Amqp().execute.call(executeFunctions)).rejects.toThrow(
new NodeOperationError(executeFunctions.getNode(), 'Queue or Topic required!'),
);
});
it('should send message successfully', async () => {
const result = await new Amqp().execute.call(executeFunctions);
expect(result).toEqual([[{ json: { id: 'test-message-id' }, pairedItems: { item: 0 } }]]);
expect(executeFunctions.getCredentials).toHaveBeenCalledWith('amqp');
expect(mockContainer.connect).toHaveBeenCalled();
expect(mockConnection.open_sender).toHaveBeenCalledWith('test/queue');
expect(mockSender.send).toHaveBeenCalledWith({
application_properties: {},
body: '{"testing":true}',
});
expect(mockSender.close).toHaveBeenCalled();
expect(mockConnection.close).toHaveBeenCalled();
});
it('should send message with custom headers', async () => {
executeFunctions.getNodeParameter
.calledWith('headerParametersJson', 0)
.mockReturnValue('{"custom":"header","priority":1}');
await new Amqp().execute.call(executeFunctions);
expect(mockSender.send).toHaveBeenCalledWith({
application_properties: { custom: 'header', priority: 1 },
body: '{"testing":true}',
});
});
it('should send only specific property when configured', async () => {
executeFunctions.getNodeParameter.calledWith('options', 0).mockReturnValue({
sendOnlyProperty: 'testing',
});
executeFunctions.getInputData.mockReturnValue([{ json: { testing: 'specific-value' } }]);
await new Amqp().execute.call(executeFunctions);
expect(mockSender.send).toHaveBeenCalledWith({
application_properties: {},
body: '"specific-value"',
});
});
it('should send data as object when configured', async () => {
executeFunctions.getNodeParameter.calledWith('options', 0).mockReturnValue({
dataAsObject: true,
});
await new Amqp().execute.call(executeFunctions);
expect(mockSender.send).toHaveBeenCalledWith({
application_properties: {},
body: { testing: true },
});
});
it('should handle multiple input items', async () => {
executeFunctions.getInputData.mockReturnValue([{ json: { item: 1 } }, { json: { item: 2 } }]);
const result = await new Amqp().execute.call(executeFunctions);
expect(result).toEqual([
[
{ json: { id: 'test-message-id' }, pairedItems: { item: 0 } },
{ json: { id: 'test-message-id' }, pairedItems: { item: 1 } },
],
]);
expect(mockSender.send).toHaveBeenCalledTimes(2);
expect(mockSender.send).toHaveBeenNthCalledWith(1, {
application_properties: {},
body: '{"item":1}',
});
expect(mockSender.send).toHaveBeenNthCalledWith(2, {
application_properties: {},
body: '{"item":2}',
});
});
it('should continue on fail when configured', async () => {
executeFunctions.continueOnFail.mockReturnValue(true);
executeFunctions.getNodeParameter.calledWith('sink', 0).mockReturnValue('');
const result = await new Amqp().execute.call(executeFunctions);
expect(result).toEqual([
[{ json: { error: 'Queue or Topic required!' }, pairedItems: { item: 0 } }],
]);
});
describe('credential test', () => {
it('should return success for valid credentials', async () => {
const amqp = new Amqp();
const testFunctions = mock<ICredentialTestFunctions>();
// Mock successful connection
mockContainer.on.mockImplementation((event: string, callback: any) => {
if (event === 'connection_open') {
setImmediate(() => callback({}));
}
});
const result = await amqp.methods.credentialTest.amqpConnectionTest.call(testFunctions, {
data: credentials,
id: 'test',
name: 'test',
type: 'amqp',
} as ICredentialsDecrypted);
expect(result).toEqual({
status: 'OK',
message: 'Connection successful!',
});
});
it('should return error for invalid credentials', async () => {
const amqp = new Amqp();
const testFunctions = mock<ICredentialTestFunctions>();
// Mock failed connection
mockContainer.on.mockImplementation((event: string, callback: any) => {
if (event === 'disconnected') {
setImmediate(() => callback({ error: new Error('Authentication failed') }));
}
});
const result = await amqp.methods.credentialTest.amqpConnectionTest.call(testFunctions, {
data: credentials,
id: 'test',
name: 'test',
type: 'amqp',
} as ICredentialsDecrypted);
expect(result).toEqual({
status: 'Error',
message: 'Authentication failed',
});
});
});
});
+277
View File
@@ -0,0 +1,277 @@
import type {
IExecuteFunctions,
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
ICredentialTestFunctions,
INodeCredentialTestResult,
ICredentialsDecrypted,
ICredentialDataDecryptedObject,
} from 'n8n-workflow';
import { NodeConnectionTypes, NodeOperationError } from 'n8n-workflow';
import type { Connection, ConnectionOptions, Dictionary, EventContext, Sender } from 'rhea';
import { create_container } from 'rhea';
import type { AmqpCredential } from './types';
async function checkIfCredentialsValid(
credentials: IDataObject,
): Promise<INodeCredentialTestResult> {
const connectOptions: ConnectionOptions = {
reconnect: false,
host: credentials.hostname as string,
hostname: credentials.hostname as string,
port: credentials.port as number,
username: credentials.username ? (credentials.username as string) : undefined,
password: credentials.password ? (credentials.password as string) : undefined,
transport: credentials.transportType ? (credentials.transportType as 'tcp' | 'tls') : undefined,
} as unknown as ConnectionOptions;
let conn: Connection | undefined = undefined;
try {
const container = create_container();
await new Promise<void>((resolve, reject) => {
container.on('connection_open', function (_context: EventContext) {
resolve();
});
container.on('disconnected', function (context: EventContext) {
reject(context.error ?? new Error('unknown error'));
});
conn = container.connect(connectOptions);
});
} catch (error) {
return {
status: 'Error',
message: (error as Error).message,
};
} finally {
if (conn) (conn as Connection).close();
}
return {
status: 'OK',
message: 'Connection successful!',
};
}
export class Amqp implements INodeType {
description: INodeTypeDescription = {
displayName: 'AMQP Sender',
name: 'amqp',
icon: 'file:amqp.svg',
group: ['transform'],
version: 1,
description: 'Sends a raw-message via AMQP 1.0, executed once per item',
defaults: {
name: 'AMQP Sender',
},
usableAsTool: true,
inputs: [NodeConnectionTypes.Main],
outputs: [NodeConnectionTypes.Main],
credentials: [
{
name: 'amqp',
required: true,
testedBy: 'amqpConnectionTest',
},
],
properties: [
{
displayName: 'Queue / Topic',
name: 'sink',
type: 'string',
default: '',
placeholder: 'e.g. 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.',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
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',
type: 'boolean',
default: false,
description: 'Whether to send the data as an object',
},
{
displayName: 'Reconnect',
name: 'reconnect',
type: 'boolean',
default: true,
description: 'Whether to automatically reconnect if disconnected',
},
{
displayName: 'Reconnect Limit',
name: 'reconnectLimit',
type: 'number',
default: 50,
description: 'Maximum number of reconnect attempts',
},
{
displayName: 'Send Property',
name: 'sendOnlyProperty',
type: 'string',
default: '',
description: 'The only property to send. If empty the whole item will be sent.',
},
],
},
],
};
methods = {
credentialTest: {
async amqpConnectionTest(
this: ICredentialTestFunctions,
credential: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> {
const credentials = credential.data as ICredentialDataDecryptedObject;
return await checkIfCredentialsValid(credentials);
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const container = create_container();
let connection: Connection | undefined = undefined;
let sender: Sender | undefined = undefined;
try {
const credentials = await this.getCredentials<AmqpCredential>('amqp');
// check if credentials are valid to avoid unnecessary reconnects
const credentialsTestResult = await checkIfCredentialsValid(credentials);
if (credentialsTestResult.status === 'Error') {
throw new NodeOperationError(this.getNode(), credentialsTestResult.message, {
description: 'Check your credentials and try again',
});
}
const sink = this.getNodeParameter('sink', 0, '') as string;
const applicationProperties = this.getNodeParameter('headerParametersJson', 0, {}) as
| string
| object;
const options = this.getNodeParameter('options', 0, {});
const containerId = options.containerId as string;
const containerReconnect = (options.reconnect as boolean) || true;
const containerReconnectLimit = (options.reconnectLimit as number) || 50;
let headerProperties: Dictionary<any>;
if (typeof applicationProperties === 'string' && applicationProperties !== '') {
headerProperties = JSON.parse(applicationProperties);
} else {
headerProperties = applicationProperties as object;
}
if (sink === '') {
throw new NodeOperationError(this.getNode(), 'Queue or Topic required!');
}
/*
Values are documented here: https://github.com/amqp/rhea#container
*/
const connectOptions: ConnectionOptions = {
host: credentials.hostname,
hostname: credentials.hostname,
port: credentials.port,
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,
reconnect: containerReconnect,
reconnect_limit: containerReconnectLimit,
} as unknown as ConnectionOptions;
const node = this.getNode();
const responseData: INodeExecutionData[] = await new Promise((resolve, reject) => {
connection = container.connect(connectOptions);
sender = connection.open_sender(sink);
let limit = containerReconnectLimit;
container.on('disconnected', function (context: EventContext) {
//handling this manually as container, despite reconnect_limit, does reconnect on disconnect
if (limit <= 0) {
connection!.options.reconnect = false;
const error = new NodeOperationError(
node,
((context.error as Error) ?? {}).message ?? 'Disconnected',
{
description: `Check your credentials${options.reconnect ? '' : ', and consider enabling reconnect in the options'}`,
itemIndex: 0,
},
);
reject(error);
}
limit--;
});
container.once('sendable', (context: EventContext) => {
const returnData: INodeExecutionData[] = [];
const items = this.getInputData();
for (let i = 0; i < items.length; i++) {
const item = items[i];
let body: IDataObject | string = item.json;
const sendOnlyProperty = options.sendOnlyProperty as string;
if (sendOnlyProperty) {
body = body[sendOnlyProperty] as string;
}
if (options.dataAsObject !== true) {
body = JSON.stringify(body);
}
const result = context.sender?.send({
application_properties: headerProperties,
body,
});
returnData.push({ json: { id: result?.id }, pairedItems: { item: i } });
}
resolve(returnData);
});
});
return [responseData];
} catch (error) {
if (this.continueOnFail()) {
return [[{ json: { error: error.message }, pairedItems: { item: 0 } }]];
} else {
throw error;
}
} finally {
if (sender) (sender as Sender).close();
if (connection) (connection as Connection).close();
}
}
}
@@ -0,0 +1,25 @@
{
"node": "n8n-nodes-base.amqpTrigger",
"nodeVersion": "1.0",
"codexVersion": "1.0",
"categories": ["Development", "Communication"],
"resources": {
"credentialDocumentation": [
{
"url": "https://docs.n8n.io/integrations/builtin/credentials/amqp/"
}
],
"primaryDocumentation": [
{
"url": "https://docs.n8n.io/integrations/builtin/trigger-nodes/n8n-nodes-base.amqptrigger/"
}
],
"generic": [
{
"label": "Learn to Automate Your Factory's Incident Reporting: A Step by Step Guide",
"icon": "🏭",
"url": "https://n8n.io/blog/learn-to-automate-your-factorys-incident-reporting-a-step-by-step-guide/"
}
]
}
}
@@ -0,0 +1,309 @@
import { testTriggerNode } from '@test/nodes/TriggerHelpers';
import { mockDeep } from 'jest-mock-extended';
import { NodeOperationError } from 'n8n-workflow';
import type { ITriggerFunctions } from 'n8n-workflow';
import { AmqpTrigger } from './AmqpTrigger.node';
let eventHandlers: Record<string, (...args: unknown[]) => void> = {};
const mockAddCredit = jest.fn();
const mockClose = jest.fn();
const mockOpenReceiver = jest.fn();
const mockEmitExecutionError = jest.fn();
const mockConnection = {
open_receiver: mockOpenReceiver,
close: mockClose,
};
jest.mock('rhea', () => ({
create_container: jest.fn(() => ({
on: (event: string, handler: (...args: unknown[]) => void) => {
eventHandlers[event] = handler;
},
removeAllListeners: jest.fn((event: string) => {
delete eventHandlers[event];
}),
connect: jest.fn(() => mockConnection),
})),
}));
describe('AMQP Trigger Node', () => {
beforeEach(() => {
jest.clearAllMocks();
eventHandlers = {};
mockEmitExecutionError.mockClear();
});
it('should throw if no sink provided', async () => {
await expect(
testTriggerNode(AmqpTrigger, {
mode: 'trigger',
node: { parameters: { sink: '' } },
credential: { hostname: 'localhost', port: 5672 },
}),
).rejects.toThrow(NodeOperationError);
});
it('should emit a full message in trigger mode', async () => {
const { emit, close } = await testTriggerNode(AmqpTrigger, {
mode: 'trigger',
node: { parameters: { sink: 'queue://test' } },
credential: { hostname: 'localhost', port: 5672 },
});
eventHandlers['receiver_open']({ receiver: { add_credit: mockAddCredit } });
expect(mockAddCredit).toHaveBeenCalledWith(100);
const message = { body: 'hello', message_id: 1 };
eventHandlers['message']({
message,
});
expect(emit).toHaveBeenCalledWith([[{ json: message }]]);
await close();
expect(mockClose).toHaveBeenCalled();
});
it('should parse JSON body when jsonParseBody = true', async () => {
const { emit } = await testTriggerNode(AmqpTrigger, {
mode: 'trigger',
node: { parameters: { sink: 'queue://test', options: { jsonParseBody: true } } },
credential: { hostname: 'localhost', port: 5672 },
});
eventHandlers['message']({
message: { body: '{"foo":"bar"}', message_id: 2 },
});
expect(emit).toHaveBeenCalledWith([[{ json: { body: { foo: 'bar' }, message_id: 2 } }]]);
});
it('should return only body when onlyBody = true', async () => {
const { emit } = await testTriggerNode(AmqpTrigger, {
mode: 'trigger',
node: { parameters: { sink: 'queue://test', options: { onlyBody: true } } },
credential: { hostname: 'localhost', port: 5672 },
});
eventHandlers['message']({
message: { body: { nested: true }, message_id: 3 },
});
expect(emit).toHaveBeenCalledWith([[{ json: { nested: true } }]]);
});
it('should reject in manual mode after 15s with no message', async () => {
const timeoutSpy = jest.spyOn(global, 'setTimeout').mockImplementation((fn) => {
fn(); // fire immediately
return 1 as unknown as NodeJS.Timeout;
});
const { manualTriggerFunction } = await testTriggerNode(AmqpTrigger, {
mode: 'manual',
node: { parameters: { sink: 'queue://test' } },
credential: { hostname: 'localhost', port: 5672 },
});
await expect(manualTriggerFunction?.()).rejects.toThrow(
'Aborted because no message received within 15 seconds',
);
timeoutSpy.mockRestore();
});
it('should resolve in manual mode when a message arrives', async () => {
const { manualTriggerFunction, emit } = await testTriggerNode(AmqpTrigger, {
mode: 'manual',
node: { parameters: { sink: 'queue://test' } },
credential: { hostname: 'localhost', port: 5672 },
});
const manualTriggerPromise = manualTriggerFunction?.();
eventHandlers['message']({
message: { body: '{"foo":"bar"}', message_id: 2 },
});
await manualTriggerPromise;
expect(emit).toHaveBeenCalledWith([[{ json: { body: '{"foo":"bar"}', message_id: 2 } }]]);
});
it('should call saveFailedExecution when handleMessage throws an error in trigger mode', async () => {
const trigger = new AmqpTrigger();
const emit = jest.fn();
const saveFailedExecution = jest.fn();
const triggerFunctions = mockDeep<ITriggerFunctions>();
Object.assign(triggerFunctions, { emit, saveFailedExecution });
triggerFunctions.getNode.mockReturnValue({
id: 'test',
name: 'Test Node',
type: 'amqpTrigger',
typeVersion: 1,
position: [0, 0],
parameters: { sink: 'queue://test', options: { jsonParseBody: true } },
} as any);
triggerFunctions.getCredentials.mockResolvedValue({ hostname: 'localhost', port: 5672 } as any);
triggerFunctions.getMode.mockReturnValue('trigger');
triggerFunctions.getNodeParameter.mockImplementation((param: string) => {
if (param === 'sink') return 'queue://test';
if (param === 'options') return { jsonParseBody: true };
if (param === 'options.parallelProcessing') return true;
if (param === 'options.jsonParseBody') return true;
return undefined;
});
triggerFunctions.getWorkflowStaticData.mockReturnValue({});
await trigger.trigger.call(triggerFunctions);
eventHandlers['message']({
message: { body: 'invalid json {', message_id: 1 },
receiver: {
has_credit: jest.fn().mockReturnValue(true),
},
});
await new Promise((resolve) => setTimeout(resolve, 10));
expect(saveFailedExecution).toHaveBeenCalledWith(expect.any(NodeOperationError));
});
it('should handle errors in manual mode and reject the promise', async () => {
const trigger = new AmqpTrigger();
const emit = jest.fn();
const triggerFunctions = mockDeep<ITriggerFunctions>();
Object.assign(triggerFunctions, { emit });
triggerFunctions.getNode.mockReturnValue({
id: 'test',
name: 'Test Node',
type: 'amqpTrigger',
typeVersion: 1,
position: [0, 0],
parameters: { sink: 'queue://test', options: { jsonParseBody: true } },
} as any);
triggerFunctions.getCredentials.mockResolvedValue({ hostname: 'localhost', port: 5672 } as any);
triggerFunctions.getMode.mockReturnValue('manual');
triggerFunctions.getNodeParameter.mockImplementation((param: string) => {
if (param === 'sink') return 'queue://test';
if (param === 'options') return { jsonParseBody: true };
if (param === 'options.parallelProcessing') return true;
if (param === 'options.jsonParseBody') return true;
return undefined;
});
triggerFunctions.getWorkflowStaticData.mockReturnValue({});
const result = await trigger.trigger.call(triggerFunctions);
const manualTriggerPromise = result?.manualTriggerFunction?.();
eventHandlers['message']({
message: { body: 'invalid json {', message_id: 1 },
});
await expect(manualTriggerPromise).rejects.toThrow();
});
it('should handle duplicate messages correctly', async () => {
const { emit } = await testTriggerNode(AmqpTrigger, {
mode: 'trigger',
node: { parameters: { sink: 'queue://test' } },
credential: { hostname: 'localhost', port: 5672 },
});
await Promise.resolve(
eventHandlers['message']({
message: { body: 'hello', message_id: 1 },
}),
);
expect(emit).toHaveBeenCalledTimes(1);
await Promise.resolve(
eventHandlers['message']({
message: { body: 'hello', message_id: 1 },
}),
);
expect(emit).toHaveBeenCalledTimes(1);
});
it('should handle messages with jsonConvertByteArrayToString option', async () => {
const { emit } = await testTriggerNode(AmqpTrigger, {
mode: 'trigger',
node: {
parameters: {
sink: 'queue://test',
options: { jsonConvertByteArrayToString: true },
},
},
credential: { hostname: 'localhost', port: 5672 },
});
const message = {
body: {
content: {
data: [72, 101, 108, 108, 111],
},
},
message_id: 1,
};
eventHandlers['message']({
message,
});
expect(emit).toHaveBeenCalled();
});
it('should handle parallel processing correctly', async () => {
const { emit } = await testTriggerNode(AmqpTrigger, {
mode: 'trigger',
node: {
parameters: {
sink: 'queue://test',
options: { parallelProcessing: false },
},
},
credential: { hostname: 'localhost', port: 5672 },
});
const message = { body: 'hello', message_id: 1 };
eventHandlers['message']({
message,
receiver: {
has_credit: jest.fn().mockReturnValue(true),
},
});
expect(emit).toHaveBeenCalled();
});
it('should add credit when receiver has no credit', async () => {
const addCreditSpy = jest.fn();
await testTriggerNode(AmqpTrigger, {
mode: 'trigger',
node: {
parameters: {
sink: 'queue://test',
options: { sleepTime: 5 },
},
},
credential: { hostname: 'localhost', port: 5672 },
});
jest.useFakeTimers();
const message = { body: 'hello', message_id: 1 };
eventHandlers['message']({
message,
receiver: {
has_credit: jest.fn().mockReturnValue(false),
add_credit: addCreditSpy,
},
});
jest.advanceTimersByTime(10);
jest.useRealTimers();
expect(addCreditSpy).toHaveBeenCalledWith(100);
});
});
@@ -0,0 +1,284 @@
import type {
IDataObject,
INodeType,
INodeTypeDescription,
ITriggerFunctions,
ITriggerResponse,
} from 'n8n-workflow';
import { NodeConnectionTypes, NodeOperationError } from 'n8n-workflow';
import type { ConnectionOptions, EventContext, ReceiverOptions } from 'rhea';
import { create_container } from 'rhea';
import { handleMessage } from './helpers/handleMessage';
import type { AmqpCredential } from './types';
export class AmqpTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'AMQP Trigger',
name: 'amqpTrigger',
icon: 'file:amqp.svg',
group: ['trigger'],
version: 1,
description: 'Listens to AMQP 1.0 Messages',
defaults: {
name: 'AMQP Trigger',
},
inputs: [],
outputs: [NodeConnectionTypes.Main],
credentials: [
{
name: 'amqp',
required: true,
},
],
properties: [
// Node properties which the user gets displayed and
// can change on the node.
{
displayName: 'Queue / Topic',
name: 'sink',
type: 'string',
default: '',
placeholder: 'topic://sourcename.something',
description: 'Name of the queue of topic to listen to',
},
{
displayName: 'Clientname',
name: 'clientname',
type: 'string',
default: '',
placeholder: 'e.g. n8n',
description: 'Leave empty for non-durable topic subscriptions or queues',
hint: 'for durable/persistent topic subscriptions',
},
{
displayName: 'Subscription',
name: 'subscription',
type: 'string',
default: '',
placeholder: 'e.g. order-worker',
description: 'Leave empty for non-durable topic subscriptions or queues',
hint: 'for durable/persistent topic subscriptions',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
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',
type: 'boolean',
default: false,
description:
'Whether to convert JSON Body content (["body"]["content"]) from Byte Array to string. Needed for Azure Service Bus.',
},
{
displayName: 'JSON Parse Body',
name: 'jsonParseBody',
type: 'boolean',
default: false,
description: 'Whether to 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',
type: 'boolean',
default: false,
description: 'Whether to return only the body property',
},
{
displayName: 'Parallel Processing',
name: 'parallelProcessing',
type: 'boolean',
default: true,
description: 'Whether to process messages in parallel',
},
{
displayName: 'Reconnect',
name: 'reconnect',
type: 'boolean',
default: true,
description: 'Whether to automatically reconnect if disconnected',
},
{
displayName: 'Reconnect Limit',
name: 'reconnectLimit',
type: 'number',
default: 50,
description: 'Maximum number of reconnect attempts',
},
{
displayName: 'Sleep Time',
name: 'sleepTime',
type: 'number',
default: 10,
description: 'Milliseconds to sleep after every cicle',
},
],
},
],
};
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
const credentials = await this.getCredentials<AmqpCredential>('amqp');
const sink = this.getNodeParameter('sink', '') as string;
const clientname = this.getNodeParameter('clientname', '') as string;
const subscription = this.getNodeParameter('subscription', '') as string;
const options = this.getNodeParameter('options', {}) as IDataObject;
const parallelProcessing = this.getNodeParameter('options.parallelProcessing', true) as boolean;
const pullMessagesNumber = (options.pullMessagesNumber as number) || 100;
const containerId = options.containerId as string;
const containerReconnect = (options.reconnect as boolean) || true;
// Keep reconnecting (exponential backoff) forever unless user sets a limit
const containerReconnectLimit = (options.reconnectLimit as number) ?? undefined;
if (sink === '') {
throw new NodeOperationError(this.getNode(), 'Queue or Topic required!');
}
let durable = false;
if (subscription && clientname) {
durable = true;
}
const container = create_container();
let lastMsgId: string | number | Buffer | undefined = undefined;
container.on('receiver_open', (context: EventContext) => {
context.receiver?.add_credit(pullMessagesNumber);
});
container.on('message', async (context: EventContext) => {
try {
const result = await handleMessage.call(this, context, {
lastMessageId: lastMsgId,
pullMessagesNumber,
jsonConvertByteArrayToString: options.jsonConvertByteArrayToString as boolean,
jsonParseBody: options.jsonParseBody as boolean,
onlyBody: options.onlyBody as boolean,
parallelProcessing,
sleepTime: options.sleepTime as number,
});
if (result) {
lastMsgId = result.messageId;
}
} catch (error) {
this.saveFailedExecution(new NodeOperationError(this.getNode(), error as Error));
}
});
/*
Values are documented here: https://github.com/amqp/rhea#container
*/
const connectOptions: ConnectionOptions = {
host: credentials.hostname,
hostname: credentials.hostname,
port: credentials.port,
reconnect: containerReconnect,
reconnect_limit: containerReconnectLimit,
// Try reconnection even if caused by a fatal error
all_errors_non_fatal: true,
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,
} as unknown as ConnectionOptions;
const connection = container.connect(connectOptions);
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);
// The "closeFunction" function gets called by n8n whenever
// the workflow gets deactivated and can so clean up.
async function closeFunction() {
container.removeAllListeners('receiver_open');
container.removeAllListeners('message');
connection.close();
}
// The "manualTriggerFunction" function gets called by n8n
// when a user is in the workflow editor and starts the
// workflow manually.
// for AMQP it doesn't make much sense to wait here but
// for a new user who doesn't know how this works, it's better to wait and show a respective info message
const manualTriggerFunction = async () => {
await new Promise((resolve, reject) => {
// remove the default message listener, setup our own for test trigger
container.removeAllListeners('message');
const timeoutHandler = setTimeout(() => {
container.removeAllListeners('receiver_open');
container.removeAllListeners('message');
connection.close();
reject(
new NodeOperationError(
this.getNode(),
'Aborted because no message received within 15 seconds',
{
description:
'This 15sec timeout is only set for "manually triggered execution". Active Workflows will listen indefinitely.',
},
),
);
}, 15000);
container.on('message', async (context: EventContext) => {
try {
const result = await handleMessage.call(this, context, {
lastMessageId: lastMsgId,
pullMessagesNumber,
jsonConvertByteArrayToString: options.jsonConvertByteArrayToString as boolean,
jsonParseBody: options.jsonParseBody as boolean,
onlyBody: options.onlyBody as boolean,
parallelProcessing,
sleepTime: options.sleepTime as number,
});
if (result) {
lastMsgId = result.messageId;
}
clearTimeout(timeoutHandler);
resolve(true);
} catch (error) {
reject(error as Error);
} finally {
clearTimeout(timeoutHandler);
}
});
});
};
return {
closeFunction,
manualTriggerFunction,
};
}
}
+9
View File
@@ -0,0 +1,9 @@
<svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect width="40" height="40" fill="white"/>
<path d="M2.88135 27.6271H27.6271V2.88135H37.1186V37.1186H2.88135V27.6271Z" fill="black"/>
<path d="M2.88135 2.88135V12.3729H12.3729V2.88135H2.88135Z" fill="black"/>
<path d="M24.7458 2.88135H15.2542V15.2542H2.88135V24.7458H24.7458V2.88135Z" fill="#002585"/>
<path d="M37.1186 2.88135H27.6271V27.6271L37.1186 37.1186V2.88135Z" fill="#CACCCE"/>
<path d="M12.3729 2.88135H2.88135L12.3729 12.3729V2.88135Z" fill="#CACCCE"/>
<path d="M24.7459 24.7458V2.88135H15.2544V15.2542L24.7459 24.7458Z" fill="#A2B0D9"/>
</svg>

After

Width:  |  Height:  |  Size: 653 B

@@ -0,0 +1,425 @@
import { mockDeep } from 'jest-mock-extended';
import type { ITriggerFunctions, IDeferredPromise, IRun } from 'n8n-workflow';
import type { EventContext } from 'rhea';
import { handleMessage } from './handleMessage';
interface MockReceiver {
has_credit: jest.Mock<boolean>;
add_credit: jest.Mock;
}
describe('handleMessage', () => {
let mockTriggerFunctions: jest.Mocked<ITriggerFunctions>;
let mockContext: EventContext;
let mockReceiver: MockReceiver;
let mockDeferredPromise: jest.Mocked<IDeferredPromise<IRun>>;
beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers();
mockDeferredPromise = {
promise: Promise.resolve({} as IRun),
resolve: jest.fn(),
reject: jest.fn(),
} as jest.Mocked<IDeferredPromise<IRun>>;
mockReceiver = {
has_credit: jest.fn<boolean, []>().mockReturnValue(true),
add_credit: jest.fn(),
};
mockContext = {
message: {
body: 'test message',
message_id: 1,
},
receiver: mockReceiver as unknown as EventContext['receiver'],
} as EventContext;
mockTriggerFunctions = mockDeep<ITriggerFunctions>({
helpers: {
createDeferredPromise: jest.fn().mockReturnValue(mockDeferredPromise),
returnJsonArray: jest.fn((data) => data),
},
emit: jest.fn(),
});
});
afterEach(() => {
jest.useRealTimers();
});
describe('message handling', () => {
it('should return null when context has no message', async () => {
mockContext.message = null as unknown as EventContext['message'];
const result = await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
});
expect(result).toBeNull();
expect(mockTriggerFunctions.emit).not.toHaveBeenCalled();
});
it('should return null for duplicate messages', async () => {
const result = await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: 1,
pullMessagesNumber: 100,
});
expect(result).toBeNull();
expect(mockTriggerFunctions.emit).not.toHaveBeenCalled();
});
it('should emit message data correctly', async () => {
const result = await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
});
expect(result).toEqual({ messageId: 1 });
expect(mockTriggerFunctions.emit).toHaveBeenCalledWith(
[[mockContext.message]],
undefined,
expect.anything(),
);
});
it('should return messageId from processed message', async () => {
mockContext.message = {
body: 'test',
message_id: 'test-id-123',
} as EventContext['message'];
const result = await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
});
expect(result).toEqual({ messageId: 'test-id-123' });
});
});
describe('jsonParseBody option', () => {
it('should parse JSON body when jsonParseBody is true', async () => {
mockContext.message = {
body: '{"foo":"bar","number":123}',
message_id: 2,
} as EventContext['message'];
await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
jsonParseBody: true,
});
expect(mockTriggerFunctions.emit).toHaveBeenCalledWith(
[
[
{
body: { foo: 'bar', number: 123 },
message_id: 2,
},
],
],
undefined,
expect.anything(),
);
});
it('should not parse JSON body when jsonParseBody is false', async () => {
mockContext.message = {
body: '{"foo":"bar"}',
message_id: 3,
} as EventContext['message'];
await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
jsonParseBody: false,
});
expect(mockTriggerFunctions.emit).toHaveBeenCalledWith(
[
[
{
body: '{"foo":"bar"}',
message_id: 3,
},
],
],
undefined,
expect.anything(),
);
});
});
describe('onlyBody option', () => {
it('should return only body when onlyBody is true', async () => {
mockContext.message = {
body: { nested: { data: 'value' } },
message_id: 4,
otherProperty: 'should be ignored',
} as EventContext['message'];
await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
onlyBody: true,
});
expect(mockTriggerFunctions.emit).toHaveBeenCalledWith(
[[{ nested: { data: 'value' } }]],
undefined,
expect.anything(),
);
});
it('should return full message when onlyBody is false', async () => {
mockContext.message = {
body: { nested: { data: 'value' } },
message_id: 5,
otherProperty: 'should be included',
} as EventContext['message'];
await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
onlyBody: false,
});
expect(mockTriggerFunctions.emit).toHaveBeenCalledWith(
[[mockContext.message]],
undefined,
expect.anything(),
);
});
});
describe('jsonConvertByteArrayToString option', () => {
it('should convert byte array to string when jsonConvertByteArrayToString is true', async () => {
mockContext.message = {
body: {
content: {
data: [72, 101, 108, 108, 111],
},
},
message_id: 6,
} as EventContext['message'];
await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
jsonConvertByteArrayToString: true,
});
expect(mockTriggerFunctions.emit).toHaveBeenCalled();
const callArgs = mockTriggerFunctions.emit.mock.calls[0][0];
expect(callArgs[0][0].body).toBe('Hello');
});
it('should not convert when jsonConvertByteArrayToString is false', async () => {
mockContext.message = {
body: {
content: {
data: [72, 101, 108, 108, 111],
},
},
message_id: 7,
} as EventContext['message'];
await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
jsonConvertByteArrayToString: false,
});
expect(mockTriggerFunctions.emit).toHaveBeenCalledWith(
[[mockContext.message]],
undefined,
expect.anything(),
);
});
});
describe('parallelProcessing option', () => {
it('should create deferred promise when parallelProcessing is false', async () => {
await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
parallelProcessing: false,
});
expect(mockTriggerFunctions.helpers.createDeferredPromise).toHaveBeenCalled();
expect(mockTriggerFunctions.emit).toHaveBeenCalledWith(
[[mockContext.message]],
undefined,
mockDeferredPromise,
);
});
it('should not create deferred promise when parallelProcessing is true', async () => {
await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
parallelProcessing: true,
});
expect(mockTriggerFunctions.helpers.createDeferredPromise).not.toHaveBeenCalled();
expect(mockTriggerFunctions.emit).toHaveBeenCalledWith([[mockContext.message]]);
});
it('should await promise when parallelProcessing is false', async () => {
let promiseResolved = false;
mockDeferredPromise.promise = new Promise<IRun>((resolve) => {
setTimeout(() => {
promiseResolved = true;
resolve({} as IRun);
}, 100);
});
const handlePromise = handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
parallelProcessing: false,
});
jest.advanceTimersByTime(100);
await handlePromise;
expect(promiseResolved).toBe(true);
});
});
describe('receiver credit management', () => {
it('should add credit when receiver has no credit', async () => {
mockReceiver.has_credit.mockReturnValue(false);
await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 50,
sleepTime: 20,
});
jest.advanceTimersByTime(25);
expect(mockReceiver.add_credit).toHaveBeenCalledWith(50);
});
it('should not add credit when receiver has credit', async () => {
mockReceiver.has_credit.mockReturnValue(true);
await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
});
jest.advanceTimersByTime(20);
expect(mockReceiver.add_credit).not.toHaveBeenCalled();
});
it('should use default sleepTime of 10ms when not provided', async () => {
mockReceiver.has_credit.mockReturnValue(false);
await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
});
jest.advanceTimersByTime(15);
expect(mockReceiver.add_credit).toHaveBeenCalledWith(100);
});
it('should use custom sleepTime when provided', async () => {
mockReceiver.has_credit.mockReturnValue(false);
await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
sleepTime: 50,
});
jest.advanceTimersByTime(30);
expect(mockReceiver.add_credit).not.toHaveBeenCalled();
jest.advanceTimersByTime(25);
expect(mockReceiver.add_credit).toHaveBeenCalledWith(100);
});
it('should handle missing receiver gracefully', async () => {
mockContext.receiver = undefined;
await expect(
handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
}),
).resolves.toEqual({ messageId: 1 });
});
});
describe('edge cases', () => {
it('should handle message with undefined message_id', async () => {
mockContext.message = {
body: 'test',
message_id: undefined,
} as EventContext['message'];
const result = await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
});
expect(result).toEqual({ messageId: undefined });
expect(mockTriggerFunctions.emit).toHaveBeenCalled();
});
it('should handle message with Buffer message_id', async () => {
const bufferId = Buffer.from('test-id');
mockContext.message = {
body: 'test',
message_id: bufferId,
} as EventContext['message'];
const result = await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
});
expect(result).toEqual({ messageId: bufferId });
});
it('should handle message with string message_id', async () => {
mockContext.message = {
body: 'test',
message_id: 'string-id-123',
} as EventContext['message'];
const result = await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
});
expect(result).toEqual({ messageId: 'string-id-123' });
});
it('should handle message with number message_id', async () => {
mockContext.message = {
body: 'test',
message_id: 999,
} as EventContext['message'];
const result = await handleMessage.call(mockTriggerFunctions, mockContext, {
lastMessageId: undefined,
pullMessagesNumber: 100,
});
expect(result).toEqual({ messageId: 999 });
});
});
});
@@ -0,0 +1,78 @@
import {
deepCopy,
type IDeferredPromise,
type IRun,
type ITriggerFunctions,
jsonParse,
} from 'n8n-workflow';
import type { EventContext } from 'rhea';
type MessageId = string | number | Buffer | undefined;
interface HandleMessageOptions {
lastMessageId: MessageId;
pullMessagesNumber: number;
jsonConvertByteArrayToString?: boolean;
jsonParseBody?: boolean;
onlyBody?: boolean;
parallelProcessing?: boolean;
sleepTime?: number;
}
export async function handleMessage(
this: ITriggerFunctions,
context: EventContext,
options: HandleMessageOptions,
): Promise<{ messageId: MessageId } | null> {
// No message in the context
if (!context.message) {
return null;
}
// 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 === options.lastMessageId) {
return null;
}
let data = context.message;
if (options.jsonConvertByteArrayToString === true && data.body.content !== undefined) {
// The buffer is not ready... Stringify and parse back to load it.
const cont = deepCopy(data.body.content);
data.body = String.fromCharCode.apply(null, cont.data as number[]);
}
if (options.jsonConvertByteArrayToString === true && data.body.content !== undefined) {
// The buffer is not ready... Stringify and parse back to load it.
const content = deepCopy(data.body.content);
data.body = String.fromCharCode.apply(null, content.data as number[]);
}
if (options.jsonParseBody === true) {
data.body = jsonParse(data.body as string);
}
if (options.onlyBody === true) {
data = data.body;
}
let responsePromise: IDeferredPromise<IRun> | undefined = undefined;
if (!options.parallelProcessing) {
responsePromise = this.helpers.createDeferredPromise();
}
if (responsePromise) {
this.emit([this.helpers.returnJsonArray([data as any])], undefined, responsePromise);
await responsePromise.promise;
} else {
this.emit([this.helpers.returnJsonArray([data as any])]);
}
if (!context.receiver?.has_credit()) {
setTimeout(
() => {
context.receiver?.add_credit(options.pullMessagesNumber);
},
(options.sleepTime as number) || 10,
);
}
return { messageId: context.message.message_id };
}
+7
View File
@@ -0,0 +1,7 @@
export type AmqpCredential = {
hostname: string;
port: number;
username?: string;
password?: string;
transportType?: 'tcp' | 'tls';
};