mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-24 20:24:05 -08:00
fix(MQTT Node): Node hangs forever on failed connection (#10048)
This commit is contained in:
parent
8a53d6127e
commit
76c290655d
|
@ -1,5 +1,5 @@
|
||||||
import { connectAsync, type IClientOptions, type MqttClient } from 'mqtt';
|
import { connect, type IClientOptions, type MqttClient } from 'mqtt';
|
||||||
import { randomString } from 'n8n-workflow';
|
import { ApplicationError, randomString } from 'n8n-workflow';
|
||||||
import { formatPrivateKey } from '@utils/utilities';
|
import { formatPrivateKey } from '@utils/utilities';
|
||||||
|
|
||||||
interface BaseMqttCredential {
|
interface BaseMqttCredential {
|
||||||
|
@ -49,5 +49,23 @@ export const createClient = async (credentials: MqttCredential): Promise<MqttCli
|
||||||
clientOptions.rejectUnauthorized = credentials.rejectUnauthorized;
|
clientOptions.rejectUnauthorized = credentials.rejectUnauthorized;
|
||||||
}
|
}
|
||||||
|
|
||||||
return await connectAsync(clientOptions);
|
return await new Promise((resolve, reject) => {
|
||||||
|
const client = connect(clientOptions);
|
||||||
|
|
||||||
|
const onConnect = () => {
|
||||||
|
client.removeListener('connect', onConnect);
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||||
|
client.removeListener('error', onError);
|
||||||
|
resolve(client);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onError = (error: Error) => {
|
||||||
|
client.removeListener('connect', onConnect);
|
||||||
|
client.removeListener('error', onError);
|
||||||
|
reject(new ApplicationError(error.message));
|
||||||
|
};
|
||||||
|
|
||||||
|
client.once('connect', onConnect);
|
||||||
|
client.once('error', onError);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue