mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
chore: resolve conflicts
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { Logger } from '@nestjs/common/services/logger.service';
|
import { Logger } from '@nestjs/common/services/logger.service';
|
||||||
import { loadPackage } from '@nestjs/common/utils/load-package.util';
|
import { loadPackage } from '@nestjs/common/utils/load-package.util';
|
||||||
|
import { isObject } from '@nestjs/common/utils/shared.utils';
|
||||||
import { EmptyError, fromEvent, lastValueFrom, merge, Observable } from 'rxjs';
|
import { EmptyError, fromEvent, lastValueFrom, merge, Observable } from 'rxjs';
|
||||||
import { first, map, share, tap } from 'rxjs/operators';
|
import { first, map, share, tap } from 'rxjs/operators';
|
||||||
import { ECONNREFUSED, ENOTFOUND, MQTT_DEFAULT_URL } from '../constants';
|
import { ECONNREFUSED, ENOTFOUND, MQTT_DEFAULT_URL } from '../constants';
|
||||||
@@ -222,10 +223,8 @@ export class ClientMqtt extends ClientProxy<MqttEvents, MqttStatus> {
|
|||||||
try {
|
try {
|
||||||
const packet = this.assignPacketId(partialPacket);
|
const packet = this.assignPacketId(partialPacket);
|
||||||
const pattern = this.normalizePattern(partialPacket.pattern);
|
const pattern = this.normalizePattern(partialPacket.pattern);
|
||||||
const serializedPacket: ReadPacket & Partial<MqttRecord> =
|
|
||||||
this.serializer.serialize(packet);
|
|
||||||
|
|
||||||
const responseChannel = this.getResponsePattern(pattern);
|
const responseChannel = this.getResponsePattern(pattern);
|
||||||
|
|
||||||
let subscriptionsCount =
|
let subscriptionsCount =
|
||||||
this.subscriptionsCount.get(responseChannel) || 0;
|
this.subscriptionsCount.get(responseChannel) || 0;
|
||||||
|
|
||||||
@@ -234,12 +233,17 @@ export class ClientMqtt extends ClientProxy<MqttEvents, MqttStatus> {
|
|||||||
this.subscriptionsCount.set(responseChannel, subscriptionsCount + 1);
|
this.subscriptionsCount.set(responseChannel, subscriptionsCount + 1);
|
||||||
this.routingMap.set(packet.id, callback);
|
this.routingMap.set(packet.id, callback);
|
||||||
|
|
||||||
const options = serializedPacket.options;
|
const options =
|
||||||
delete serializedPacket.options;
|
isObject(packet?.data) && packet.data instanceof MqttRecord
|
||||||
|
? (packet.data as MqttRecord)?.options
|
||||||
|
: undefined;
|
||||||
|
delete packet?.data?.options;
|
||||||
|
const serializedPacket: string | Buffer =
|
||||||
|
this.serializer.serialize(packet);
|
||||||
|
|
||||||
this.mqttClient!.publish(
|
this.mqttClient!.publish(
|
||||||
this.getRequestPattern(pattern),
|
this.getRequestPattern(pattern),
|
||||||
JSON.stringify(serializedPacket),
|
serializedPacket,
|
||||||
this.mergePacketOptions(options),
|
this.mergePacketOptions(options),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -265,16 +269,17 @@ export class ClientMqtt extends ClientProxy<MqttEvents, MqttStatus> {
|
|||||||
|
|
||||||
protected dispatchEvent(packet: ReadPacket): Promise<any> {
|
protected dispatchEvent(packet: ReadPacket): Promise<any> {
|
||||||
const pattern = this.normalizePattern(packet.pattern);
|
const pattern = this.normalizePattern(packet.pattern);
|
||||||
const serializedPacket: ReadPacket & Partial<MqttRecord> =
|
const options =
|
||||||
this.serializer.serialize(packet);
|
isObject(packet?.data) && packet.data instanceof MqttRecord
|
||||||
|
? (packet.data as MqttRecord)?.options
|
||||||
const options = serializedPacket.options;
|
: undefined;
|
||||||
delete serializedPacket.options;
|
delete packet?.data?.options;
|
||||||
|
|
||||||
|
const serializedPacket: string | Buffer = this.serializer.serialize(packet);
|
||||||
return new Promise<void>((resolve, reject) =>
|
return new Promise<void>((resolve, reject) =>
|
||||||
this.mqttClient!.publish(
|
this.mqttClient!.publish(
|
||||||
pattern,
|
pattern,
|
||||||
JSON.stringify(serializedPacket),
|
serializedPacket,
|
||||||
this.mergePacketOptions(options),
|
this.mergePacketOptions(options),
|
||||||
(err: any) => (err ? reject(err) : resolve()),
|
(err: any) => (err ? reject(err) : resolve()),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -2,22 +2,15 @@ import { isObject } from '@nestjs/common/utils/shared.utils';
|
|||||||
import { ReadPacket, Serializer } from '../interfaces';
|
import { ReadPacket, Serializer } from '../interfaces';
|
||||||
import { MqttRecord } from '../record-builders';
|
import { MqttRecord } from '../record-builders';
|
||||||
|
|
||||||
export class MqttRecordSerializer
|
export class MqttRecordSerializer implements Serializer<ReadPacket, string> {
|
||||||
implements Serializer<ReadPacket, ReadPacket & Partial<MqttRecord>>
|
serialize(packet: ReadPacket): string {
|
||||||
{
|
if (isObject(packet?.data) && packet.data instanceof MqttRecord) {
|
||||||
serialize(packet: ReadPacket): ReadPacket & Partial<MqttRecord> {
|
const record = packet.data as MqttRecord;
|
||||||
if (
|
return JSON.stringify({
|
||||||
packet?.data &&
|
|
||||||
isObject(packet.data) &&
|
|
||||||
packet.data instanceof MqttRecord
|
|
||||||
) {
|
|
||||||
const record = packet.data;
|
|
||||||
return {
|
|
||||||
...packet,
|
...packet,
|
||||||
data: record.data,
|
data: record.data,
|
||||||
options: record.options,
|
});
|
||||||
};
|
|
||||||
}
|
}
|
||||||
return packet;
|
return JSON.stringify(packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { isUndefined } from '@nestjs/common/utils/shared.utils';
|
import { isObject, isUndefined } from '@nestjs/common/utils/shared.utils';
|
||||||
import {
|
import {
|
||||||
MQTT_DEFAULT_URL,
|
MQTT_DEFAULT_URL,
|
||||||
MQTT_SEPARATOR,
|
MQTT_SEPARATOR,
|
||||||
@@ -149,14 +149,18 @@ export class ServerMqtt extends Server<MqttEvents, MqttStatus> {
|
|||||||
public getPublisher(client: MqttClient, pattern: any, id: string): any {
|
public getPublisher(client: MqttClient, pattern: any, id: string): any {
|
||||||
return (response: any) => {
|
return (response: any) => {
|
||||||
Object.assign(response, { id });
|
Object.assign(response, { id });
|
||||||
const outgoingResponse: Partial<MqttRecord> =
|
|
||||||
this.serializer.serialize(response);
|
|
||||||
const options = outgoingResponse.options;
|
|
||||||
delete outgoingResponse.options;
|
|
||||||
|
|
||||||
|
const options =
|
||||||
|
isObject(response?.data) && response.data instanceof MqttRecord
|
||||||
|
? (response.data as MqttRecord)?.options
|
||||||
|
: {};
|
||||||
|
delete response?.data?.options;
|
||||||
|
|
||||||
|
const outgoingResponse: string | Buffer =
|
||||||
|
this.serializer.serialize(response);
|
||||||
return client.publish(
|
return client.publish(
|
||||||
this.getReplyPattern(pattern),
|
this.getReplyPattern(pattern),
|
||||||
JSON.stringify(outgoingResponse),
|
outgoingResponse,
|
||||||
options,
|
options,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ describe('MqttRecordSerializer', () => {
|
|||||||
instance = new MqttRecordSerializer();
|
instance = new MqttRecordSerializer();
|
||||||
});
|
});
|
||||||
describe('serialize', () => {
|
describe('serialize', () => {
|
||||||
it('should parse mqtt record instance', () => {
|
it('should parse mqtt record instance to a string, ignoring options', () => {
|
||||||
const mqttMessage = new MqttRecordBuilder()
|
const mqttMessage = new MqttRecordBuilder()
|
||||||
.setData({ value: 'string' })
|
.setData({ value: 'string' })
|
||||||
.setQoS(1)
|
.setQoS(1)
|
||||||
@@ -22,18 +22,19 @@ describe('MqttRecordSerializer', () => {
|
|||||||
pattern: 'pattern',
|
pattern: 'pattern',
|
||||||
data: mqttMessage,
|
data: mqttMessage,
|
||||||
}),
|
}),
|
||||||
).to.deep.eq({
|
).to.deep.eq(
|
||||||
pattern: 'pattern',
|
JSON.stringify({
|
||||||
options: { qos: 1, retain: true, dup: true, properties: {} },
|
pattern: 'pattern',
|
||||||
data: { value: 'string' },
|
data: { value: 'string' },
|
||||||
});
|
}),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
it('should act as an identity function if msg is not an instance of MqttRecord class', () => {
|
it('should act as an identity function if msg is not an instance of MqttRecord class', () => {
|
||||||
const packet = {
|
const packet = {
|
||||||
pattern: 'pattern',
|
pattern: 'pattern',
|
||||||
data: { random: true },
|
data: { random: true },
|
||||||
};
|
};
|
||||||
expect(instance.serialize(packet)).to.eq(packet);
|
expect(instance.serialize(packet)).to.eq(JSON.stringify(packet));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user