pull:初次提交

This commit is contained in:
Yep_Q
2025-09-08 04:48:28 +08:00
parent 5c0619656d
commit f64f498365
11751 changed files with 1953723 additions and 0 deletions

View File

@@ -0,0 +1,76 @@
import { connect, type IClientOptions, type MqttClient } from 'mqtt';
import { ApplicationError, randomString } from 'n8n-workflow';
import { formatPrivateKey } from '@utils/utilities';
interface BaseMqttCredential {
protocol: 'mqtt' | 'mqtts' | 'ws';
host: string;
port: number;
username: string;
password: string;
clean: boolean;
clientId: string;
passwordless?: boolean;
}
type NonSslMqttCredential = BaseMqttCredential & {
ssl: false;
};
type SslMqttCredential = BaseMqttCredential & {
ssl: true;
ca: string;
cert: string;
key: string;
rejectUnauthorized?: boolean;
};
export type MqttCredential = NonSslMqttCredential | SslMqttCredential;
export const createClient = async (credentials: MqttCredential): Promise<MqttClient> => {
const { protocol, host, port, clean, clientId, username, password } = credentials;
const clientOptions: IClientOptions = {
protocol,
host,
port,
clean,
clientId: clientId || `mqttjs_${randomString(8).toLowerCase()}`,
};
if (username && password) {
clientOptions.username = username;
clientOptions.password = password;
}
if (credentials.ssl) {
clientOptions.ca = formatPrivateKey(credentials.ca);
clientOptions.cert = formatPrivateKey(credentials.cert);
clientOptions.key = formatPrivateKey(credentials.key);
clientOptions.rejectUnauthorized = credentials.rejectUnauthorized;
}
return await new Promise((resolve, reject) => {
const client = connect(clientOptions);
const onConnect = () => {
client.removeListener('connect', onConnect);
client.removeListener('error', onError);
resolve(client);
};
const onError = (error: Error) => {
client.removeListener('connect', onConnect);
client.removeListener('error', onError);
// mqtt client has an automatic reconnect mechanism that will
// keep trying to reconnect until it succeeds unless we
// explicitly close the client
client.end();
reject(new ApplicationError(error.message));
};
client.once('connect', onConnect);
client.once('error', onError);
});
};

View File

@@ -0,0 +1,18 @@
{
"node": "n8n-nodes-base.mqtt",
"nodeVersion": "1.0",
"codexVersion": "1.0",
"categories": ["Development", "Communication"],
"resources": {
"credentialDocumentation": [
{
"url": "https://docs.n8n.io/integrations/builtin/credentials/mqtt/"
}
],
"primaryDocumentation": [
{
"url": "https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.mqtt/"
}
]
}
}

View File

@@ -0,0 +1,161 @@
import type { IClientPublishOptions } from 'mqtt';
import {
type IExecuteFunctions,
type ICredentialsDecrypted,
type ICredentialTestFunctions,
type INodeCredentialTestResult,
type INodeExecutionData,
type INodeType,
type INodeTypeDescription,
NodeConnectionTypes,
ensureError,
} from 'n8n-workflow';
import { createClient, type MqttCredential } from './GenericFunctions';
type PublishOption = Pick<IClientPublishOptions, 'qos' | 'retain'>;
export class Mqtt implements INodeType {
description: INodeTypeDescription = {
displayName: 'MQTT',
name: 'mqtt',
icon: 'file:mqtt.svg',
group: ['input'],
version: 1,
description: 'Push messages to MQTT',
defaults: {
name: 'MQTT',
},
usableAsTool: true,
inputs: [NodeConnectionTypes.Main],
outputs: [NodeConnectionTypes.Main],
credentials: [
{
name: 'mqtt',
required: true,
testedBy: 'mqttConnectionTest',
},
],
properties: [
{
displayName: 'Topic',
name: 'topic',
type: 'string',
required: true,
default: '',
description: 'The topic to publish to',
},
{
displayName: 'Send Input Data',
name: 'sendInputData',
type: 'boolean',
default: true,
description: 'Whether to send the data the node receives as JSON',
},
{
displayName: 'Message',
name: 'message',
type: 'string',
required: true,
displayOptions: {
show: {
sendInputData: [false],
},
},
default: '',
description: 'The message to publish',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add option',
default: {},
options: [
{
displayName: 'QoS',
name: 'qos',
type: 'options',
options: [
{
name: 'Received at Most Once',
value: 0,
},
{
name: 'Received at Least Once',
value: 1,
},
{
name: 'Exactly Once',
value: 2,
},
],
default: 0,
description: 'QoS subscription level',
},
{
displayName: 'Retain',
name: 'retain',
type: 'boolean',
default: false,
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
description:
'Normally if a publisher publishes a message to a topic, and no one is subscribed to that topic the message is simply discarded by the broker. However the publisher can tell the broker to keep the last message on that topic by setting the retain flag to true.',
},
],
},
],
};
methods = {
credentialTest: {
async mqttConnectionTest(
this: ICredentialTestFunctions,
credential: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> {
const credentials = credential.data as unknown as MqttCredential;
try {
const client = await createClient(credentials);
client.end();
} catch (e) {
const error = ensureError(e);
return {
status: 'Error',
message: error.message,
};
}
return {
status: 'OK',
message: 'Connection successful!',
};
},
},
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const credentials = await this.getCredentials<MqttCredential>('mqtt');
const client = await createClient(credentials);
const publishPromises = [];
const items = this.getInputData();
for (let i = 0; i < items.length; i++) {
const topic = this.getNodeParameter('topic', i) as string;
const options = this.getNodeParameter('options', i) as unknown as PublishOption;
const sendInputData = this.getNodeParameter('sendInputData', i) as boolean;
const message = sendInputData
? JSON.stringify(items[i].json)
: (this.getNodeParameter('message', i) as string);
publishPromises.push(client.publishAsync(topic, message, options));
}
await Promise.all(publishPromises);
// wait for the in-flight messages to be acked.
// needed for messages with QoS 1 & 2
await client.endAsync();
return [items];
}
}

View File

@@ -0,0 +1,18 @@
{
"node": "n8n-nodes-base.mqttTrigger",
"nodeVersion": "1.0",
"codexVersion": "1.0",
"categories": ["Communication", "Development"],
"resources": {
"credentialDocumentation": [
{
"url": "https://docs.n8n.io/integrations/builtin/credentials/mqtt/"
}
],
"primaryDocumentation": [
{
"url": "https://docs.n8n.io/integrations/builtin/trigger-nodes/n8n-nodes-base.mqtttrigger/"
}
]
}
}

View File

@@ -0,0 +1,161 @@
import type { ISubscriptionMap } from 'mqtt';
import type { QoS } from 'mqtt-packet';
import type {
ITriggerFunctions,
IDataObject,
INodeType,
INodeTypeDescription,
ITriggerResponse,
IRun,
} from 'n8n-workflow';
import { NodeConnectionTypes, NodeOperationError } from 'n8n-workflow';
import { createClient, type MqttCredential } from './GenericFunctions';
interface Options {
jsonParseBody: boolean;
onlyMessage: boolean;
parallelProcessing: boolean;
}
export class MqttTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'MQTT Trigger',
name: 'mqttTrigger',
icon: 'file:mqtt.svg',
group: ['trigger'],
version: 1,
description: 'Listens to MQTT events',
eventTriggerDescription: '',
defaults: {
name: 'MQTT Trigger',
},
triggerPanel: {
header: '',
executionsHelp: {
inactive:
"<b>While building your workflow</b>, click the 'execute step' button, then trigger an MQTT event. This will trigger an execution, which will show up in this editor.<br /> <br /><b>Once you're happy with your workflow</b>, <a data-key='activate'>activate</a> it. Then every time a change is detected, the workflow will execute. These executions will show up in the <a data-key='executions'>executions list</a>, but not in the editor.",
active:
"<b>While building your workflow</b>, click the 'execute step' button, then trigger an MQTT event. This will trigger an execution, which will show up in this editor.<br /> <br /><b>Your workflow will also execute automatically</b>, since it's activated. Every time a change is detected, this node will trigger an execution. These executions will show up in the <a data-key='executions'>executions list</a>, but not in the editor.",
},
activationHint:
"Once youve finished building your workflow, <a data-key='activate'>activate</a> it to have it also listen continuously (you just wont see those executions here).",
},
inputs: [],
outputs: [NodeConnectionTypes.Main],
credentials: [
{
name: 'mqtt',
required: true,
},
],
properties: [
{
displayName: 'Topics',
name: 'topics',
type: 'string',
default: '',
description:
'Topics to subscribe to, multiple can be defined with comma. Wildcard characters are supported (+ - for single level and # - for multi level). By default all subscription used QoS=0. To set a different QoS, write the QoS desired after the topic preceded by a colom. For Example: topicA:1,topicB:2',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add option',
default: {},
options: [
{
displayName: 'JSON Parse Body',
name: 'jsonParseBody',
type: 'boolean',
default: false,
description: 'Whether to try parse the message to an object',
},
{
displayName: 'Only Message',
name: 'onlyMessage',
type: 'boolean',
default: false,
description: 'Whether to return only the message property',
},
{
displayName: 'Parallel Processing',
name: 'parallelProcessing',
type: 'boolean',
default: true,
description:
'Whether to process messages in parallel or by keeping the message in order',
},
],
},
],
};
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
const topics = (this.getNodeParameter('topics') as string).split(',');
if (!topics?.length) {
throw new NodeOperationError(this.getNode(), 'Topics are mandatory!');
}
const topicsQoS: ISubscriptionMap = {};
for (const data of topics) {
const [topic, qosString] = data.split(':');
let qos = qosString ? parseInt(qosString, 10) : 0;
if (qos < 0 || qos > 2) qos = 0;
topicsQoS[topic] = { qos: qos as QoS };
}
const options = this.getNodeParameter('options') as Options;
const credentials = await this.getCredentials<MqttCredential>('mqtt');
const client = await createClient(credentials);
const parsePayload = (topic: string, payload: Buffer) => {
let message = payload.toString();
if (options.jsonParseBody) {
try {
message = JSON.parse(message);
} catch (e) {}
}
let result: IDataObject = { message, topic };
if (options.onlyMessage) {
//@ts-ignore
result = [message];
}
return [this.helpers.returnJsonArray([result])];
};
const manualTriggerFunction = async () =>
await new Promise<void>(async (resolve) => {
client.once('message', (topic, payload) => {
this.emit(parsePayload(topic, payload));
resolve();
});
await client.subscribeAsync(topicsQoS);
});
if (this.getMode() === 'trigger') {
const donePromise = !options.parallelProcessing
? this.helpers.createDeferredPromise<IRun>()
: undefined;
client.on('message', async (topic, payload) => {
this.emit(parsePayload(topic, payload), undefined, donePromise);
await donePromise?.promise;
});
await client.subscribeAsync(topicsQoS);
}
async function closeFunction() {
await client.endAsync();
}
return {
closeFunction,
manualTriggerFunction,
};
}
}

View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" baseProfile="tiny" overflow="visible" version="1.2" viewBox="0 0 320 320"><path fill="#FFF" d="M7.1 133.9v46.7c73.8.1 134 59.3 135 132.4h45.5c-1.1-98.4-81.5-178.2-180.5-179.1"/><path fill="#FFF" d="M7.1 37.3V84c127.4.1 231.1 102.5 232.1 228.9h45.5c-1-151.5-125-274.6-277.6-275.6"/><path fill="#FFF" d="M312.9 193.5V97.6c-11.8-16.1-25.9-33.4-40.4-47.8-16-15.9-34.1-30.1-52.3-42.7H119c88.3 31.8 159.1 100.1 193.9 186.4"/><path fill="#606" d="M7.1 180.6v117.1c0 8.4 6.8 15.3 15.3 15.3H142c-1-73.2-61.1-132.3-134.9-132.4m0-96.5v49.8c99 .9 179.4 80.7 180.4 179.1h51.7C238.2 186.6 134.5 84.2 7.1 84.1m305.8 213.5V193.5C278.1 107.2 207.3 38.9 119 7.1H22.4C14 7.1 7.1 13.9 7.1 22.4v15c152.6.9 276.6 124 277.6 275.6h13c8.4-.1 15.2-6.9 15.2-15.4M272.6 49.8c14.5 14.4 28.6 31.7 40.4 47.8V22.4c0-8.4-6.8-15.3-15.3-15.3h-77.3c18 12.6 36.2 26.8 52.2 42.7"/></svg>

After

Width:  |  Height:  |  Size: 889 B

View File

@@ -0,0 +1,66 @@
import { mock } from 'jest-mock-extended';
import { MqttClient } from 'mqtt';
import { ApplicationError } from '@n8n/errors';
import { createClient, type MqttCredential } from '../GenericFunctions';
describe('createClient', () => {
beforeEach(() => jest.clearAllMocks());
it('should create a client with minimal credentials', async () => {
const mockConnect = jest.spyOn(MqttClient.prototype, 'connect').mockImplementation(function (
this: MqttClient,
) {
setImmediate(() => this.emit('connect', mock()));
return this;
});
const credentials = mock<MqttCredential>({
protocol: 'mqtt',
host: 'localhost',
port: 1883,
clean: true,
clientId: 'testClient',
ssl: false,
});
const client = await createClient(credentials);
expect(mockConnect).toBeCalledTimes(1);
expect(client).toBeDefined();
expect(client).toBeInstanceOf(MqttClient);
expect(client.options).toMatchObject({
protocol: 'mqtt',
host: 'localhost',
port: 1883,
clean: true,
clientId: 'testClient',
});
});
it('should reject with ApplicationError on connection error and close connection', async () => {
const mockConnect = jest.spyOn(MqttClient.prototype, 'connect').mockImplementation(function (
this: MqttClient,
) {
setImmediate(() => this.emit('error', new Error('Connection failed')));
return this;
});
const mockEnd = jest.spyOn(MqttClient.prototype, 'end').mockImplementation();
const credentials: MqttCredential = {
protocol: 'mqtt',
host: 'localhost',
port: 1883,
clean: true,
clientId: 'testClientId',
username: 'testUser',
password: 'testPass',
ssl: false,
};
const clientPromise = createClient(credentials);
await expect(clientPromise).rejects.toThrow(ApplicationError);
expect(mockConnect).toBeCalledTimes(1);
expect(mockEnd).toBeCalledTimes(1);
});
});

View File

@@ -0,0 +1,56 @@
import { mock } from 'jest-mock-extended';
import type { MqttClient } from 'mqtt';
import type { ICredentialDataDecryptedObject, IExecuteFunctions } from 'n8n-workflow';
import { createClient } from '../GenericFunctions';
import { Mqtt } from '../Mqtt.node';
jest.mock('../GenericFunctions', () => {
const mockMqttClient = mock<MqttClient>();
return {
createClient: jest.fn().mockResolvedValue(mockMqttClient),
};
});
describe('MQTT Node', () => {
const credentials = mock<ICredentialDataDecryptedObject>();
const executeFunctions = mock<IExecuteFunctions>();
beforeEach(() => {
jest.clearAllMocks();
executeFunctions.getCredentials.calledWith('mqtt').mockResolvedValue(credentials);
executeFunctions.getInputData.mockReturnValue([{ json: { testing: true } }]);
executeFunctions.getNodeParameter.calledWith('topic', 0).mockReturnValue('test/topic');
executeFunctions.getNodeParameter.calledWith('options', 0).mockReturnValue({});
});
it('should publish input data', async () => {
executeFunctions.getNodeParameter.calledWith('sendInputData', 0).mockReturnValue(true);
const result = await new Mqtt().execute.call(executeFunctions);
expect(result).toEqual([[{ json: { testing: true } }]]);
expect(executeFunctions.getCredentials).toHaveBeenCalledTimes(1);
expect(executeFunctions.getNodeParameter).toHaveBeenCalledTimes(3);
const mockMqttClient = await createClient(mock());
expect(mockMqttClient.publishAsync).toHaveBeenCalledWith('test/topic', '{"testing":true}', {});
expect(mockMqttClient.endAsync).toHaveBeenCalledTimes(1);
});
it('should publish a custom message', async () => {
executeFunctions.getNodeParameter.calledWith('sendInputData', 0).mockReturnValue(false);
executeFunctions.getNodeParameter.calledWith('message', 0).mockReturnValue('Hello, MQTT!');
const result = await new Mqtt().execute.call(executeFunctions);
expect(result).toEqual([[{ json: { testing: true } }]]);
expect(executeFunctions.getCredentials).toHaveBeenCalledTimes(1);
expect(executeFunctions.getNodeParameter).toHaveBeenCalledTimes(4);
const mockMqttClient = await createClient(mock());
expect(mockMqttClient.publishAsync).toHaveBeenCalledWith('test/topic', 'Hello, MQTT!', {});
expect(mockMqttClient.endAsync).toHaveBeenCalledTimes(1);
});
});

View File

@@ -0,0 +1,123 @@
import { captor, mock } from 'jest-mock-extended';
import type { MqttClient, OnMessageCallback } from 'mqtt';
import { returnJsonArray } from 'n8n-core';
import type { ICredentialDataDecryptedObject, ITriggerFunctions } from 'n8n-workflow';
import { createClient } from '../GenericFunctions';
import { MqttTrigger } from '../MqttTrigger.node';
jest.mock('../GenericFunctions', () => {
const mockMqttClient = mock<MqttClient>();
return {
createClient: jest.fn().mockResolvedValue(mockMqttClient),
};
});
describe('MQTT Trigger Node', () => {
const topic = 'test/topic';
const payload = Buffer.from('{"testing": true}');
const credentials = mock<ICredentialDataDecryptedObject>();
const triggerFunctions = mock<ITriggerFunctions>({
helpers: { returnJsonArray },
});
beforeEach(() => {
jest.clearAllMocks();
triggerFunctions.getCredentials.calledWith('mqtt').mockResolvedValue(credentials);
triggerFunctions.getNodeParameter.calledWith('topics').mockReturnValue(topic);
});
it('should emit in manual mode', async () => {
triggerFunctions.getMode.mockReturnValue('manual');
triggerFunctions.getNodeParameter.calledWith('options').mockReturnValue({});
const response = await new MqttTrigger().trigger.call(triggerFunctions);
expect(response.manualTriggerFunction).toBeDefined();
expect(response.closeFunction).toBeDefined();
expect(triggerFunctions.getCredentials).toHaveBeenCalledTimes(1);
expect(triggerFunctions.getNodeParameter).toHaveBeenCalledTimes(2);
// manually trigger the node, like Workflow.runNode does
const triggerPromise = response.manualTriggerFunction!();
const mockMqttClient = await createClient(mock());
expect(mockMqttClient.on).not.toHaveBeenCalled();
const onMessageCaptor = captor<OnMessageCallback>();
expect(mockMqttClient.once).toHaveBeenCalledWith('message', onMessageCaptor);
expect(mockMqttClient.subscribeAsync).toHaveBeenCalledWith({ [topic]: { qos: 0 } });
expect(triggerFunctions.emit).not.toHaveBeenCalled();
// simulate a message
const onMessage = onMessageCaptor.value;
onMessage('test/topic', payload, mock());
expect(triggerFunctions.emit).toHaveBeenCalledWith([
[{ json: { message: '{"testing": true}', topic } }],
]);
// wait for the promise to resolve
await new Promise((resolve) => setImmediate(resolve));
await expect(triggerPromise).resolves.toEqual(undefined);
expect(mockMqttClient.endAsync).not.toHaveBeenCalled();
await response.closeFunction!();
expect(mockMqttClient.endAsync).toHaveBeenCalledTimes(1);
});
it('should emit in trigger mode', async () => {
triggerFunctions.getMode.mockReturnValue('trigger');
triggerFunctions.getNodeParameter.calledWith('options').mockReturnValue({});
const response = await new MqttTrigger().trigger.call(triggerFunctions);
expect(response.manualTriggerFunction).toBeDefined();
expect(response.closeFunction).toBeDefined();
expect(triggerFunctions.getCredentials).toHaveBeenCalledTimes(1);
expect(triggerFunctions.getNodeParameter).toHaveBeenCalledTimes(2);
const mockMqttClient = await createClient(mock());
expect(mockMqttClient.once).not.toHaveBeenCalled();
const onMessageCaptor = captor<OnMessageCallback>();
expect(mockMqttClient.on).toHaveBeenCalledWith('message', onMessageCaptor);
expect(mockMqttClient.subscribeAsync).toHaveBeenCalledWith({ [topic]: { qos: 0 } });
expect(triggerFunctions.emit).not.toHaveBeenCalled();
// simulate a message
const onMessage = onMessageCaptor.value;
onMessage('test/topic', payload, mock());
expect(triggerFunctions.emit).toHaveBeenCalledWith(
[[{ json: { message: '{"testing": true}', topic } }]],
undefined,
undefined,
);
expect(mockMqttClient.endAsync).not.toHaveBeenCalled();
await response.closeFunction!();
expect(mockMqttClient.endAsync).toHaveBeenCalledTimes(1);
});
it('should parse JSON messages when configured', async () => {
triggerFunctions.getMode.mockReturnValue('trigger');
triggerFunctions.getNodeParameter.calledWith('options').mockReturnValue({
jsonParseBody: true,
});
await new MqttTrigger().trigger.call(triggerFunctions);
const mockMqttClient = await createClient(mock());
const onMessageCaptor = captor<OnMessageCallback>();
expect(mockMqttClient.on).toHaveBeenCalledWith('message', onMessageCaptor);
// simulate a message
const onMessage = onMessageCaptor.value;
onMessage('test/topic', payload, mock());
expect(triggerFunctions.emit).toHaveBeenCalledWith(
[[{ json: { message: { testing: true }, topic } }]],
undefined,
undefined,
);
});
});