11import { Logger } from '@nestjs/common/services/logger.service' ;
22import { loadPackage } from '@nestjs/common/utils/load-package.util' ;
3- import { share } from 'rxjs/operators ' ;
4- import { CONN_ERR , ERROR_EVENT , NATS_DEFAULT_URL } from '../constants ' ;
5- import { Client } from '../external/nats-client.interface' ;
3+ import { NATS_DEFAULT_URL } from '../constants ' ;
4+ import { NatsResponseJSONDeserializer } from '../deserializers/nats-response-json.deserializer ' ;
5+ import { Client , NatsMsg } from '../external/nats-client.interface' ;
66import { NatsOptions , PacketId , ReadPacket , WritePacket } from '../interfaces' ;
7+ import { NatsJSONSerializer } from '../serializers/nats-json.serializer' ;
78import { ClientProxy } from './client-proxy' ;
89
9- let natsPackage : any = { } ;
10+ let natsPackage = { } as any ;
1011
1112export class ClientNats extends ClientProxy {
1213 protected readonly logger = new Logger ( ClientProxy . name ) ;
13- protected readonly url : string ;
1414 protected natsClient : Client ;
15- protected connection : Promise < any > ;
1615
1716 constructor ( protected readonly options : NatsOptions [ 'options' ] ) {
1817 super ( ) ;
19- this . url = this . getOptionsProp ( this . options , 'url' ) || NATS_DEFAULT_URL ;
2018 natsPackage = loadPackage ( 'nats' , ClientNats . name , ( ) => require ( 'nats' ) ) ;
2119
2220 this . initializeSerializer ( options ) ;
2321 this . initializeDeserializer ( options ) ;
2422 }
2523
26- public close ( ) {
27- this . natsClient && this . natsClient . close ( ) ;
24+ public async close ( ) {
25+ await this . natsClient ? .close ( ) ;
2826 this . natsClient = null ;
29- this . connection = null ;
3027 }
3128
3229 public async connect ( ) : Promise < any > {
3330 if ( this . natsClient ) {
34- return this . connection ;
31+ return this . natsClient ;
3532 }
36- this . natsClient = this . createClient ( ) ;
37- this . handleError ( this . natsClient ) ;
38-
39- this . connection = await this . connect$ ( this . natsClient )
40- . pipe ( share ( ) )
41- . toPromise ( ) ;
42- return this . connection ;
33+ this . natsClient = await this . createClient ( ) ;
34+ this . handleStatusUpdates ( this . natsClient ) ;
35+ return this . natsClient ;
4336 }
4437
45- public createClient ( ) : Client {
38+ public createClient ( ) : Promise < Client > {
4639 const options : any = this . options || ( { } as NatsOptions ) ;
4740 return natsPackage . connect ( {
41+ servers : NATS_DEFAULT_URL ,
4842 ...options ,
49- url : this . url ,
50- json : true ,
5143 } ) ;
5244 }
5345
54- public handleError ( client : Client ) {
55- client . addListener (
56- ERROR_EVENT ,
57- ( err : any ) => err . code !== CONN_ERR && this . logger . error ( err ) ,
58- ) ;
46+ public async handleStatusUpdates ( client : Client ) {
47+ for await ( const status of client . status ( ) ) {
48+ const data =
49+ status . data && typeof status . data === 'object'
50+ ? JSON . stringify ( status . data )
51+ : status . data ;
52+ if ( status . type === 'disconnect' || status . type === 'error' ) {
53+ this . logger . error (
54+ `NatsError: type: "${ status . type } ", data: "${ data } ".` ,
55+ ) ;
56+ } else {
57+ this . logger . log ( `NatsStatus: type: "${ status . type } ", data: "${ data } ".` ) ;
58+ }
59+ }
5960 }
6061
6162 public createSubscriptionHandler (
6263 packet : ReadPacket & PacketId ,
6364 callback : ( packet : WritePacket ) => any ,
64- ) : Function {
65- return ( rawPacket : unknown ) => {
65+ ) {
66+ return ( error : unknown | undefined , natsMsg : NatsMsg ) => {
67+ if ( error ) {
68+ return callback ( {
69+ err : error ,
70+ } ) ;
71+ }
72+ const rawPacket = natsMsg . data ;
6673 const message = this . deserializer . deserialize ( rawPacket ) ;
6774 if ( message . id && message . id !== packet . id ) {
6875 return undefined ;
@@ -95,12 +102,14 @@ export class ClientNats extends ClientProxy {
95102 packet ,
96103 callback ,
97104 ) ;
98- const subscriptionId = this . natsClient . request (
99- channel ,
100- serializedPacket as any ,
101- subscriptionHandler ,
102- ) ;
103- return ( ) => this . natsClient . unsubscribe ( subscriptionId ) ;
105+ this . natsClient . publish ( channel , serializedPacket , {
106+ reply : packet . id ,
107+ } ) ;
108+ const subscription = this . natsClient . subscribe ( packet . id , {
109+ callback : subscriptionHandler ,
110+ } ) ;
111+
112+ return ( ) => subscription . unsubscribe ( ) ;
104113 } catch ( err ) {
105114 callback ( { err } ) ;
106115 }
@@ -110,10 +119,22 @@ export class ClientNats extends ClientProxy {
110119 const pattern = this . normalizePattern ( packet . pattern ) ;
111120 const serializedPacket = this . serializer . serialize ( packet ) ;
112121
113- return new Promise < void > ( ( resolve , reject ) =>
114- this . natsClient . publish ( pattern , serializedPacket as any , err =>
115- err ? reject ( err ) : resolve ( ) ,
116- ) ,
117- ) ;
122+ return new Promise < void > ( ( resolve , reject ) => {
123+ try {
124+ this . natsClient . publish ( pattern , serializedPacket ) ;
125+ resolve ( ) ;
126+ } catch ( err ) {
127+ reject ( err ) ;
128+ }
129+ } ) ;
130+ }
131+
132+ protected initializeSerializer ( options : NatsOptions [ 'options' ] ) {
133+ this . serializer = options ?. serializer ?? new NatsJSONSerializer ( ) ;
134+ }
135+
136+ protected initializeDeserializer ( options : NatsOptions [ 'options' ] ) {
137+ this . deserializer =
138+ options ?. deserializer ?? new NatsResponseJSONDeserializer ( ) ;
118139 }
119140}
0 commit comments