@nestjs/microservices
Version:
Nest - modern, fast, powerful node.js web framework (@microservices)
338 lines (337 loc) • 13.8 kB
JavaScript
import { createRequire } from 'module';
import { EmptyError, fromEvent, lastValueFrom, merge } from 'rxjs';
import { first, map, share, tap } from 'rxjs/operators';
import { ECONNREFUSED, ENOTFOUND, MQTT_DEFAULT_URL } from '../constants.js';
import { MqttRecord, } from '../record-builders/mqtt.record-builder.js';
import { MqttRecordSerializer } from '../serializers/mqtt-record.serializer.js';
import { ClientProxy } from './client-proxy.js';
import { Logger } from '@nestjs/common';
import { loadPackageSync, isObject } from '@nestjs/common/internal';
let mqttPackage = {};
/**
* @publicApi
*/
export class ClientMqtt extends ClientProxy {
options;
logger = new Logger(ClientProxy.name);
subscriptionsCount = new Map();
url;
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */
mqttClient = null;
connectionPromise = null;
isInitialConnection = false;
isReconnecting = false;
pendingEventListeners = [];
constructor(options) {
super();
this.options = options;
this.url = this.getOptionsProp(this.options, 'url') ?? MQTT_DEFAULT_URL;
mqttPackage = loadPackageSync('mqtt', ClientMqtt.name, () => createRequire(import.meta.url)('mqtt'));
this.initializeSerializer(options);
this.initializeDeserializer(options);
}
getRequestPattern(pattern) {
return pattern;
}
getResponsePattern(pattern) {
return `${pattern}/reply`;
}
async close() {
this.handleClose();
if (this.mqttClient) {
await this.mqttClient.endAsync();
}
this.mqttClient = null;
this.connectionPromise = null;
this.isInitialConnection = false;
this.subscriptionsCount.clear();
this.pendingEventListeners = [];
}
handleClose() {
if (this.routingMap.size > 0) {
const err = new Error('Connection closed');
const callbacks = [...this.routingMap.values()];
this.routingMap.clear();
for (const callback of callbacks) {
try {
callback({ err });
}
catch (callbackErr) {
// A failing callback must not keep the remaining requests pending
// nor prevent the connection from being closed.
this.logger.error(callbackErr);
}
}
}
}
async connect() {
if (this.mqttClient) {
return this.connectionPromise;
}
const mqttClient = this.createClient();
this.mqttClient = mqttClient;
this.registerErrorListener(mqttClient);
this.registerOfflineListener(mqttClient);
this.registerReconnectListener(mqttClient);
this.registerConnectListener(mqttClient);
this.registerDisconnectListener(mqttClient);
this.registerCloseListener(mqttClient);
this.pendingEventListeners.forEach(({ event, callback }) => mqttClient.on(event, callback));
const connect$ = this.connect$(mqttClient);
const connectionPromise = lastValueFrom(this.mergeCloseEvent(mqttClient, connect$).pipe(share())).catch(err => {
if (err instanceof EmptyError) {
return;
}
// A client that does not reconnect on its own (reconnectPeriod 0) would
// otherwise stay cached as the rejected attempt, so every later
// connect() call replays the rejection instead of trying again.
if (this.connectionPromise === connectionPromise &&
mqttClient.options?.reconnectPeriod === 0) {
this.discardClient(mqttClient);
}
throw err;
});
this.connectionPromise = connectionPromise;
return connectionPromise;
}
/**
* Drops `client` when it is still the current one, so the next `connect()`
* call starts over with a new client. Events a dropped client emits from now
* on are ignored.
*/
discardClient(client) {
if (client !== this.mqttClient) {
return;
}
// Requests sent through the dropped client can no longer get a reply
this.handleClose();
this.mqttClient = null;
this.connectionPromise = null;
// The next client has to attach the response listener and subscribe to
// the reply channels again
this.isInitialConnection = false;
this.subscriptionsCount.clear();
}
mergeCloseEvent(instance, source$) {
const close$ = fromEvent(instance, "close" /* MqttEventsMap.CLOSE */).pipe(tap({
next: () => {
this._status$.next("closed" /* MqttStatus.CLOSED */);
},
}), map((err) => {
throw err;
}));
return merge(source$, close$).pipe(first());
}
createClient() {
return mqttPackage.connect(this.url, this.options);
}
registerErrorListener(client) {
client.on("error" /* MqttEventsMap.ERROR */, (err) => {
if (err.code === ECONNREFUSED || err.code === ENOTFOUND) {
return;
}
this.logger.error(err);
});
}
registerOfflineListener(client) {
client.on("offline" /* MqttEventsMap.OFFLINE */, () => {
this.connectionPromise = Promise.reject('Error: Connection lost. Trying to reconnect...');
// Prevent unhandled rejections
this.connectionPromise.catch(() => { });
this.logger.error('MQTT broker went offline.');
});
}
registerReconnectListener(client) {
client.on("reconnect" /* MqttEventsMap.RECONNECT */, () => {
this.isReconnecting = true;
this._status$.next("reconnecting" /* MqttStatus.RECONNECTING */);
this.logger.log('MQTT connection lost. Trying to reconnect...');
});
}
registerDisconnectListener(client) {
client.on("disconnect" /* MqttEventsMap.DISCONNECT */, () => {
this._status$.next("disconnected" /* MqttStatus.DISCONNECTED */);
});
}
registerCloseListener(client) {
client.on("close" /* MqttEventsMap.CLOSE */, () => {
this._status$.next("closed" /* MqttStatus.CLOSED */);
// mqtt reconnects on the same client only while reconnectPeriod is set.
// Once it gives up, drop the client so the next connect() call starts
// over instead of returning the promise of a connection that is gone.
if (client === this.mqttClient && !client.reconnecting) {
this.discardClient(client);
}
});
}
registerConnectListener(client) {
client.on("connect" /* MqttEventsMap.CONNECT */, () => {
this.isReconnecting = false;
this._status$.next("connected" /* MqttStatus.CONNECTED */);
this.logger.log('Connected to MQTT broker');
this.connectionPromise = Promise.resolve();
if (!this.isInitialConnection) {
this.isInitialConnection = true;
client.on('message', this.createResponseCallback());
}
});
}
on(event, callback) {
// Kept until `close()`, so the clients created later (e.g., after a
// failed connect) get it as well
this.pendingEventListeners.push({ event, callback });
if (this.mqttClient) {
this.mqttClient.on(event, callback);
}
}
unwrap() {
if (!this.mqttClient) {
throw new Error('Not initialized. Please call the "connect" method first.');
}
return this.mqttClient;
}
createResponseCallback() {
return async (channel, buffer) => {
let packet;
try {
packet = JSON.parse(buffer.toString());
}
catch (err) {
this.logger.debug('MQTT response packet is not in json format, bypassing...');
packet = buffer.toString();
}
let incomingResponse;
try {
incomingResponse = await this.deserializer.deserialize(packet);
}
catch (error) {
// Only the deserializer knows the id, so no request can be failed here.
// Nobody awaits this listener, so a rejection would crash the process.
this.logger.error(`Dropped a response that the deserializer failed on: ${error}`, error?.stack);
return;
}
const { err, response, isDisposed, id } = incomingResponse;
const callback = this.routingMap.get(id);
if (!callback) {
return undefined;
}
if (isDisposed || err) {
return callback({
err,
response,
isDisposed: true,
});
}
callback({
err,
response,
});
};
}
publish(partialPacket, callback) {
try {
const packet = this.assignPacketId(partialPacket);
const pattern = this.normalizePattern(partialPacket.pattern);
const responseChannel = this.getResponsePattern(pattern);
let subscriptionsCount = this.subscriptionsCount.get(responseChannel) || 0;
let isPublished = false;
let isTornDown = false;
const undoBookkeeping = () => {
isTornDown = true;
isPublished = false;
this.subscriptionsCount.set(responseChannel, (this.subscriptionsCount.get(responseChannel) || 1) - 1);
this.routingMap.delete(packet.id);
};
const publishPacket = () => {
if (isTornDown) {
return;
}
subscriptionsCount = this.subscriptionsCount.get(responseChannel) || 0;
this.subscriptionsCount.set(responseChannel, subscriptionsCount + 1);
this.routingMap.set(packet.id, callback);
isPublished = true;
try {
const options = isObject(packet?.data) && packet.data instanceof MqttRecord
? packet.data.options
: undefined;
const serializedPacket = this.serializer.serialize(packet);
this.mqttClient.publish(this.getRequestPattern(pattern), serializedPacket, this.mergePacketOptions(options), (err) => {
if (err && !isTornDown) {
undoBookkeeping();
callback({ err });
}
});
}
catch (err) {
// The broker can acknowledge the subscription later, so this runs
// outside the outer catch and has to undo its own work. Only the
// bookkeeping though: a concurrent request on this pattern may still
// be waiting for its own subscribe reply, so the broker subscription
// is left to self-heal, as in #17671.
undoBookkeeping();
callback({ err });
}
};
const cleanup = () => {
isTornDown = true;
if (!isPublished) {
return;
}
isPublished = false;
this.unsubscribeFromChannel(responseChannel);
this.routingMap.delete(packet.id);
};
if (subscriptionsCount <= 0) {
this.mqttClient.subscribe(responseChannel, (err) => err ? callback({ err }) : publishPacket());
}
else {
publishPacket();
}
return cleanup;
}
catch (err) {
callback({ err });
return () => { };
}
}
dispatchEvent(packet) {
const pattern = this.normalizePattern(packet.pattern);
const options = isObject(packet?.data) && packet.data instanceof MqttRecord
? packet.data.options
: undefined;
const serializedPacket = this.serializer.serialize(packet);
return new Promise((resolve, reject) => this.mqttClient.publish(pattern, serializedPacket, this.mergePacketOptions(options), (err) => (err ? reject(err) : resolve())));
}
unsubscribeFromChannel(channel) {
const subscriptionCount = this.subscriptionsCount.get(channel);
this.subscriptionsCount.set(channel, subscriptionCount - 1);
if (subscriptionCount - 1 <= 0) {
this.mqttClient.unsubscribe(channel);
}
}
initializeSerializer(options) {
this.serializer = options?.serializer ?? new MqttRecordSerializer();
}
mergePacketOptions(requestOptions) {
if (!requestOptions && !this.options?.userProperties) {
return undefined;
}
// Cant just spread objects as MQTT won't deliver
// any message with empty object as "userProperties" field
// @url https://github.com/nestjs/nest/issues/14079
let options = {};
if (requestOptions) {
options = { ...requestOptions };
}
if (this.options?.userProperties) {
options.properties = {
...options.properties,
userProperties: {
...this.options?.userProperties,
...options.properties?.userProperties,
},
};
}
return options;
}
}