Skip to content

Commit d2cadc1

Browse files
Merge branch 'master' into Upperfoot-feature/socket-options
2 parents 0363108 + 107b243 commit d2cadc1

9 files changed

Lines changed: 39 additions & 58 deletions

File tree

packages/microservices/client/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export * from './client-nats';
44
export * from './client-proxy';
55
export { ClientProxyFactory } from './client-proxy-factory';
66
export * from './client-redis';
7+
export * from './client-rmq';
78
export * from './client-tcp';

packages/microservices/server/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export * from './server-grpc';
33
export * from './server-mqtt';
44
export * from './server-nats';
55
export * from './server-redis';
6+
export * from './server-rmq';
67
export * from './server-tcp';

packages/microservices/server/server-grpc.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ export class ServerGrpc extends Server implements CustomTransportStrategy {
3030
private readonly url: string;
3131
private grpcClient: any;
3232

33-
constructor(private readonly options: MicroserviceOptions['options']) {
33+
constructor(private readonly options: GrpcOptions['options']) {
3434
super();
35-
this.url =
36-
this.getOptionsProp<GrpcOptions>(options, 'url') || GRPC_DEFAULT_URL;
35+
this.url = this.getOptionsProp(options, 'url') || GRPC_DEFAULT_URL;
3736

3837
const protoLoader =
39-
this.getOptionsProp<GrpcOptions>(options, 'protoLoader') ||
40-
GRPC_DEFAULT_PROTO_LOADER;
38+
this.getOptionsProp(options, 'protoLoader') || GRPC_DEFAULT_PROTO_LOADER;
4139

4240
grpcPackage = this.loadPackage('grpc', ServerGrpc.name, () =>
4341
require('grpc'),
@@ -58,10 +56,7 @@ export class ServerGrpc extends Server implements CustomTransportStrategy {
5856

5957
public async bindEvents() {
6058
const grpcContext = this.loadProto();
61-
const packageName = this.getOptionsProp<GrpcOptions>(
62-
this.options,
63-
'package',
64-
);
59+
const packageName = this.getOptionsProp(this.options, 'package');
6560
const grpcPkg = this.lookupPackage(grpcContext, packageName);
6661
if (!grpcPkg) {
6762
const invalidPackageError = new InvalidGrpcPackageException();
@@ -165,21 +160,18 @@ export class ServerGrpc extends Server implements CustomTransportStrategy {
165160

166161
public createClient(): any {
167162
const server = new grpcPackage.Server({
168-
'grpc.max_send_message_length': this.getOptionsProp<GrpcOptions>(
163+
'grpc.max_send_message_length': this.getOptionsProp(
169164
this.options,
170165
'maxSendMessageLength',
171166
GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH,
172167
),
173-
'grpc.max_receive_message_length': this.getOptionsProp<GrpcOptions>(
168+
'grpc.max_receive_message_length': this.getOptionsProp(
174169
this.options,
175170
'maxReceiveMessageLength',
176171
GRPC_DEFAULT_MAX_RECEIVE_MESSAGE_LENGTH,
177172
),
178173
});
179-
const credentials = this.getOptionsProp<GrpcOptions>(
180-
this.options,
181-
'credentials',
182-
);
174+
const credentials = this.getOptionsProp(this.options, 'credentials');
183175
server.bind(
184176
this.url,
185177
credentials || grpcPackage.ServerCredentials.createInsecure(),
@@ -198,8 +190,8 @@ export class ServerGrpc extends Server implements CustomTransportStrategy {
198190

199191
public loadProto(): any {
200192
try {
201-
const file = this.getOptionsProp<GrpcOptions>(this.options, 'protoPath');
202-
const loader = this.getOptionsProp<GrpcOptions>(this.options, 'loader');
193+
const file = this.getOptionsProp(this.options, 'protoPath');
194+
const loader = this.getOptionsProp(this.options, 'loader');
203195

204196
const packageDefinition = grpcProtoLoaderPackage.loadSync(file, loader);
205197
const packageObject = grpcPackage.loadPackageDefinition(

packages/microservices/server/server-mqtt.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ export class ServerMqtt extends Server implements CustomTransportStrategy {
2121
private readonly url: string;
2222
private mqttClient: MqttClient;
2323

24-
constructor(private readonly options: MicroserviceOptions['options']) {
24+
constructor(private readonly options: MqttOptions['options']) {
2525
super();
26-
this.url =
27-
this.getOptionsProp<MqttOptions>(options, 'url') || MQTT_DEFAULT_URL;
26+
this.url = this.getOptionsProp(options, 'url') || MQTT_DEFAULT_URL;
2827

2928
mqttPackage = this.loadPackage('mqtt', ServerMqtt.name, () =>
3029
require('mqtt'),
@@ -59,10 +58,7 @@ export class ServerMqtt extends Server implements CustomTransportStrategy {
5958
}
6059

6160
public createMqttClient(): MqttClient {
62-
return mqttPackage.connect(
63-
this.url,
64-
this.options as MqttOptions,
65-
);
61+
return mqttPackage.connect(this.url, this.options as MqttOptions);
6662
}
6763

6864
public getMessageHandler(pub: MqttClient): Function {

packages/microservices/server/server-nats.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ export class ServerNats extends Server implements CustomTransportStrategy {
2121
private readonly url: string;
2222
private natsClient: Client;
2323

24-
constructor(private readonly options: MicroserviceOptions['options']) {
24+
constructor(private readonly options: NatsOptions['options']) {
2525
super();
26-
this.url =
27-
this.getOptionsProp<NatsOptions>(this.options, 'url') || NATS_DEFAULT_URL;
26+
this.url = this.getOptionsProp(this.options, 'url') || NATS_DEFAULT_URL;
2827

2928
natsPackage = this.loadPackage('nats', ServerNats.name, () =>
3029
require('nats'),
@@ -43,7 +42,7 @@ export class ServerNats extends Server implements CustomTransportStrategy {
4342
}
4443

4544
public bindEvents(client: Client) {
46-
const queue = this.getOptionsProp<NatsOptions>(this.options, 'queue');
45+
const queue = this.getOptionsProp(this.options, 'queue');
4746
const subscribe = queue
4847
? (channel: string) =>
4948
client.subscribe(

packages/microservices/server/server-redis.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ export class ServerRedis extends Server implements CustomTransportStrategy {
2727
private pubClient: RedisClient;
2828
private isExplicitlyTerminated = false;
2929

30-
constructor(private readonly options: MicroserviceOptions['options']) {
30+
constructor(private readonly options: RedisOptions['options']) {
3131
super();
32-
this.url =
33-
this.getOptionsProp<RedisOptions>(this.options, 'url') ||
34-
REDIS_DEFAULT_URL;
32+
this.url = this.getOptionsProp(this.options, 'url') || REDIS_DEFAULT_URL;
3533

3634
redisPackage = this.loadPackage('redis', ServerRedis.name, () =>
3735
require('redis'),
@@ -148,12 +146,11 @@ export class ServerRedis extends Server implements CustomTransportStrategy {
148146
}
149147
if (
150148
this.isExplicitlyTerminated ||
151-
!this.getOptionsProp<RedisOptions>(this.options, 'retryAttempts') ||
152-
options.attempt >
153-
this.getOptionsProp<RedisOptions>(this.options, 'retryAttempts')
149+
!this.getOptionsProp(this.options, 'retryAttempts') ||
150+
options.attempt > this.getOptionsProp(this.options, 'retryAttempts')
154151
) {
155152
return undefined;
156153
}
157-
return this.getOptionsProp<RedisOptions>(this.options, 'retryDelay') || 0;
154+
return this.getOptionsProp(this.options, 'retryDelay') || 0;
158155
}
159156
}

packages/microservices/server/server-rmq.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,19 @@ export class ServerRMQ extends Server implements CustomTransportStrategy {
2626
private readonly queueOptions: any;
2727
private readonly isGlobalPrefetchCount: boolean;
2828

29-
constructor(private readonly options: MicroserviceOptions) {
29+
constructor(private readonly options: RmqOptions['options']) {
3030
super();
31-
this.urls = this.getOptionsProp<RmqOptions>(this.options, 'urls') || [
32-
RQM_DEFAULT_URL,
33-
];
31+
this.urls = this.getOptionsProp(this.options, 'urls') || [RQM_DEFAULT_URL];
3432
this.queue =
35-
this.getOptionsProp<RmqOptions>(this.options, 'queue') ||
36-
RQM_DEFAULT_QUEUE;
33+
this.getOptionsProp(this.options, 'queue') || RQM_DEFAULT_QUEUE;
3734
this.prefetchCount =
38-
this.getOptionsProp<RmqOptions>(this.options, 'prefetchCount') ||
35+
this.getOptionsProp(this.options, 'prefetchCount') ||
3936
RQM_DEFAULT_PREFETCH_COUNT;
4037
this.isGlobalPrefetchCount =
41-
this.getOptionsProp<RmqOptions>(this.options, 'isGlobalPrefetchCount') ||
38+
this.getOptionsProp(this.options, 'isGlobalPrefetchCount') ||
4239
RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT;
4340
this.queueOptions =
44-
this.getOptionsProp<RmqOptions>(this.options, 'queueOptions') ||
41+
this.getOptionsProp(this.options, 'queueOptions') ||
4542
RQM_DEFAULT_QUEUE_OPTIONS;
4643

4744
this.loadPackage('amqplib', ServerRMQ.name, () => require('amqplib'));

packages/microservices/server/server-tcp.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ export class ServerTCP extends Server implements CustomTransportStrategy {
2626
private isExplicitlyTerminated = false;
2727
private retryAttemptsCount = 0;
2828

29-
constructor(private readonly options: MicroserviceOptions['options']) {
29+
constructor(private readonly options: TcpOptions['options']) {
3030
super();
31-
this.port =
32-
this.getOptionsProp<TcpOptions>(options, 'port') || TCP_DEFAULT_PORT;
31+
this.port = this.getOptionsProp(options, 'port') || TCP_DEFAULT_PORT;
3332
this.host =
34-
this.getOptionsProp<TcpOptions>(options, 'host') || TCP_DEFAULT_HOST;
33+
this.getOptionsProp(options, 'host') || TCP_DEFAULT_HOST;
3534

3635
this.init();
3736
}
@@ -86,16 +85,16 @@ export class ServerTCP extends Server implements CustomTransportStrategy {
8685
public handleClose(): undefined | number | NodeJS.Timer {
8786
if (
8887
this.isExplicitlyTerminated ||
89-
!this.getOptionsProp<TcpOptions>(this.options, 'retryAttempts') ||
88+
!this.getOptionsProp(this.options, 'retryAttempts') ||
9089
this.retryAttemptsCount >=
91-
this.getOptionsProp<TcpOptions>(this.options, 'retryAttempts')
90+
this.getOptionsProp(this.options, 'retryAttempts')
9291
) {
9392
return undefined;
9493
}
9594
++this.retryAttemptsCount;
9695
return setTimeout(
9796
() => this.server.listen(this.port),
98-
this.getOptionsProp<TcpOptions>(this.options, 'retryDelay') || 0,
97+
this.getOptionsProp(this.options, 'retryDelay') || 0,
9998
);
10099
}
101100

packages/microservices/server/server.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,11 @@ export abstract class Server {
7373
return resultOrDeffered;
7474
}
7575

76-
public getOptionsProp<T extends { options?: any }>(
77-
obj: MicroserviceOptions['options'],
78-
prop: keyof T['options'],
79-
defaultValue: any = undefined,
80-
) {
81-
return (obj && obj[prop as string]) || defaultValue;
76+
public getOptionsProp<
77+
T extends MicroserviceOptions['options'],
78+
K extends keyof T
79+
>(obj: T, prop: K, defaultValue: T[K] = undefined) {
80+
return (obj && obj[prop]) || defaultValue;
8281
}
8382

8483
protected handleError(error: string) {

0 commit comments

Comments
 (0)