@nathapp/nestjs-microservices 1.0.0-rc.10 → 1.0.0-rc.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter/adapter.interface.d.ts +178 -0
- package/dist/adapter/adapter.interface.js +2 -0
- package/dist/adapter/http-service.adapter.d.ts +5 -1
- package/dist/adapter/http-service.adapter.js +8 -0
- package/dist/adapter/index.d.ts +3 -1
- package/dist/adapter/index.js +3 -1
- package/dist/adapter/micro-service-adapter-factory.d.ts +3 -2
- package/dist/adapter/micro-service-adapter-factory.js +32 -0
- package/dist/adapter/micro-service.adapter.d.ts +11 -7
- package/dist/adapter/nestjs-micro-service.adapter.d.ts +11 -0
- package/dist/adapter/nestjs-micro-service.adapter.js +21 -0
- package/dist/client/custom-client-rmq.d.ts +42 -0
- package/dist/client/custom-client-rmq.js +197 -0
- package/dist/client/index.d.ts +2 -1
- package/dist/client/index.js +3 -1
- package/dist/client/service-client.d.ts +5 -3
- package/dist/client/service-client.js +1 -0
- package/dist/enum/transport.enum.d.ts +8 -1
- package/dist/enum/transport.enum.js +8 -1
- package/dist/external/grpc-options.interface.d.ts +16 -0
- package/dist/external/grpc-options.interface.js +2 -0
- package/dist/external/kafka.interface.d.ts +1118 -0
- package/dist/external/kafka.interface.js +77 -0
- package/dist/external/mqtt-client.interface.d.ts +32 -0
- package/dist/external/mqtt-client.interface.js +3 -0
- package/dist/external/mqtt-options.interface.d.ts +55 -0
- package/dist/external/mqtt-options.interface.js +2 -0
- package/dist/external/redis.interface.d.ts +53 -0
- package/dist/external/redis.interface.js +2 -0
- package/dist/external/rmq-url.interface.d.ts +28 -0
- package/dist/external/rmq-url.interface.js +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/server/custom-server-rmq.d.ts +27 -0
- package/dist/server/custom-server-rmq.js +159 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +7 -0
- package/dist/server/server-keepalive.d.ts +7 -0
- package/dist/server/server-keepalive.js +21 -0
- package/dist/strategy/microservice-jwt-strategy-provider.d.ts +2 -5
- package/lib/external/README.md +1 -0
- package/package.json +50 -6
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Type } from "@nestjs/common";
|
|
3
|
+
import { Transport } from "../enum";
|
|
4
|
+
import { ConnectionOptions } from 'tls';
|
|
5
|
+
import { Serializer, Deserializer, TcpSocket } from "@nestjs/microservices";
|
|
6
|
+
import { IORedisOptions } from "../external/redis.interface";
|
|
7
|
+
import { MqttClientOptions, QoS } from "../external/mqtt-options.interface";
|
|
8
|
+
import { RmqUrl } from "../external/rmq-url.interface";
|
|
9
|
+
import { KafkaConfig, ConsumerConfig, ConsumerRunConfig, ConsumerSubscribeTopics, ProducerConfig, ProducerRecord, ConsumerSubscribeTopic } from "../external/kafka.interface";
|
|
10
|
+
import { ChannelOptions } from "../external/grpc-options.interface";
|
|
11
|
+
export interface GrpcOptions {
|
|
12
|
+
transport?: Transport.GRPC;
|
|
13
|
+
options: {
|
|
14
|
+
url?: string;
|
|
15
|
+
maxSendMessageLength?: number;
|
|
16
|
+
maxReceiveMessageLength?: number;
|
|
17
|
+
maxMetadataSize?: number;
|
|
18
|
+
keepalive?: {
|
|
19
|
+
keepaliveTimeMs?: number;
|
|
20
|
+
keepaliveTimeoutMs?: number;
|
|
21
|
+
keepalivePermitWithoutCalls?: number;
|
|
22
|
+
http2MaxPingsWithoutData?: number;
|
|
23
|
+
http2MinTimeBetweenPingsMs?: number;
|
|
24
|
+
http2MinPingIntervalWithoutDataMs?: number;
|
|
25
|
+
http2MaxPingStrikes?: number;
|
|
26
|
+
};
|
|
27
|
+
channelOptions?: ChannelOptions;
|
|
28
|
+
credentials?: any;
|
|
29
|
+
protoPath: string | string[];
|
|
30
|
+
package: string | string[];
|
|
31
|
+
protoLoader?: string;
|
|
32
|
+
packageDefinition?: any;
|
|
33
|
+
loader?: {
|
|
34
|
+
keepCase?: boolean;
|
|
35
|
+
alternateCommentMode?: boolean;
|
|
36
|
+
longs?: Function;
|
|
37
|
+
enums?: Function;
|
|
38
|
+
bytes?: Function;
|
|
39
|
+
defaults?: boolean;
|
|
40
|
+
arrays?: boolean;
|
|
41
|
+
objects?: boolean;
|
|
42
|
+
oneofs?: boolean;
|
|
43
|
+
json?: boolean;
|
|
44
|
+
includeDirs?: string[];
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export interface TcpOptions {
|
|
49
|
+
transport?: Transport.TCP;
|
|
50
|
+
options?: {
|
|
51
|
+
host?: string;
|
|
52
|
+
port?: number;
|
|
53
|
+
retryAttempts?: number;
|
|
54
|
+
retryDelay?: number;
|
|
55
|
+
serializer?: Serializer;
|
|
56
|
+
tlsOptions?: ConnectionOptions;
|
|
57
|
+
deserializer?: Deserializer;
|
|
58
|
+
socketClass?: Type<TcpSocket>;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export interface TcpClientOptions {
|
|
62
|
+
transport: Transport.TCP;
|
|
63
|
+
options?: {
|
|
64
|
+
host?: string;
|
|
65
|
+
port?: number;
|
|
66
|
+
serializer?: Serializer;
|
|
67
|
+
deserializer?: Deserializer;
|
|
68
|
+
tlsOptions?: ConnectionOptions;
|
|
69
|
+
socketClass?: Type<TcpSocket>;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
export interface RedisOptions {
|
|
73
|
+
transport?: Transport.REDIS;
|
|
74
|
+
options?: {
|
|
75
|
+
host?: string;
|
|
76
|
+
port?: number;
|
|
77
|
+
retryAttempts?: number;
|
|
78
|
+
retryDelay?: number;
|
|
79
|
+
serializer?: Serializer;
|
|
80
|
+
deserializer?: Deserializer;
|
|
81
|
+
} & IORedisOptions;
|
|
82
|
+
}
|
|
83
|
+
export interface MqttOptions {
|
|
84
|
+
transport?: Transport.MQTT;
|
|
85
|
+
options?: MqttClientOptions & {
|
|
86
|
+
url?: string;
|
|
87
|
+
serializer?: Serializer;
|
|
88
|
+
deserializer?: Deserializer;
|
|
89
|
+
subscribeOptions?: {
|
|
90
|
+
qos: QoS;
|
|
91
|
+
nl?: boolean;
|
|
92
|
+
rap?: boolean;
|
|
93
|
+
rh?: number;
|
|
94
|
+
};
|
|
95
|
+
userProperties?: Record<string, string | string[]>;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
export interface NatsOptions {
|
|
99
|
+
transport?: Transport.NATS;
|
|
100
|
+
options?: {
|
|
101
|
+
headers?: Record<string, string>;
|
|
102
|
+
authenticator?: any;
|
|
103
|
+
debug?: boolean;
|
|
104
|
+
ignoreClusterUpdates?: boolean;
|
|
105
|
+
inboxPrefix?: string;
|
|
106
|
+
encoding?: string;
|
|
107
|
+
name?: string;
|
|
108
|
+
user?: string;
|
|
109
|
+
pass?: string;
|
|
110
|
+
maxPingOut?: number;
|
|
111
|
+
maxReconnectAttempts?: number;
|
|
112
|
+
reconnectTimeWait?: number;
|
|
113
|
+
reconnectJitter?: number;
|
|
114
|
+
reconnectJitterTLS?: number;
|
|
115
|
+
reconnectDelayHandler?: any;
|
|
116
|
+
servers?: string[] | string;
|
|
117
|
+
nkey?: any;
|
|
118
|
+
reconnect?: boolean;
|
|
119
|
+
pedantic?: boolean;
|
|
120
|
+
tls?: any;
|
|
121
|
+
queue?: string;
|
|
122
|
+
serializer?: Serializer;
|
|
123
|
+
deserializer?: Deserializer;
|
|
124
|
+
userJWT?: string;
|
|
125
|
+
nonceSigner?: any;
|
|
126
|
+
userCreds?: any;
|
|
127
|
+
useOldRequestStyle?: boolean;
|
|
128
|
+
pingInterval?: number;
|
|
129
|
+
preserveBuffers?: boolean;
|
|
130
|
+
waitOnFirstConnect?: boolean;
|
|
131
|
+
verbose?: boolean;
|
|
132
|
+
noEcho?: boolean;
|
|
133
|
+
noRandomize?: boolean;
|
|
134
|
+
timeout?: number;
|
|
135
|
+
token?: string;
|
|
136
|
+
yieldTime?: number;
|
|
137
|
+
tokenHandler?: any;
|
|
138
|
+
[key: string]: any;
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
export interface RmqOptions {
|
|
142
|
+
transport?: Transport.RMQ;
|
|
143
|
+
options?: {
|
|
144
|
+
urls?: string[] | RmqUrl[];
|
|
145
|
+
queue?: string;
|
|
146
|
+
prefetchCount?: number;
|
|
147
|
+
isGlobalPrefetchCount?: boolean;
|
|
148
|
+
queueOptions?: any;
|
|
149
|
+
socketOptions?: any;
|
|
150
|
+
noAck?: boolean;
|
|
151
|
+
serializer?: Serializer;
|
|
152
|
+
deserializer?: Deserializer;
|
|
153
|
+
replyQueue?: string;
|
|
154
|
+
persistent?: boolean;
|
|
155
|
+
headers?: Record<string, string>;
|
|
156
|
+
noAssert?: boolean;
|
|
157
|
+
maxConnectionAttempts?: number;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
export interface KafkaParserConfig {
|
|
161
|
+
keepBinary?: boolean;
|
|
162
|
+
}
|
|
163
|
+
export interface KafkaOptions {
|
|
164
|
+
transport?: Transport.KAFKA;
|
|
165
|
+
options?: {
|
|
166
|
+
postfixId?: string;
|
|
167
|
+
client?: KafkaConfig;
|
|
168
|
+
consumer?: ConsumerConfig;
|
|
169
|
+
run?: Omit<ConsumerRunConfig, 'eachBatch' | 'eachMessage'>;
|
|
170
|
+
subscribe?: Omit<ConsumerSubscribeTopics | ConsumerSubscribeTopic, 'topic'>;
|
|
171
|
+
producer?: ProducerConfig;
|
|
172
|
+
send?: Omit<ProducerRecord, 'topic' | 'messages'>;
|
|
173
|
+
serializer?: Serializer;
|
|
174
|
+
deserializer?: Deserializer;
|
|
175
|
+
parser?: KafkaParserConfig;
|
|
176
|
+
producerOnlyMode?: boolean;
|
|
177
|
+
};
|
|
178
|
+
}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { MicroServiceAdapter, HttpOption, MicroServiceRequest } from "./micro-service.adapter";
|
|
2
|
+
import { Observable } from "rxjs";
|
|
2
3
|
export declare class HttpServiceAdapter implements MicroServiceAdapter {
|
|
3
4
|
private readonly logger;
|
|
4
5
|
private readonly axiosClient;
|
|
5
6
|
private readonly options;
|
|
6
7
|
private readonly jwtProvider;
|
|
7
8
|
constructor(options: HttpOption);
|
|
8
|
-
|
|
9
|
+
connect(): Promise<any>;
|
|
10
|
+
emit<TResult = any, TInput = any>(request: MicroServiceRequest<TInput>): Promise<TResult> | Observable<TResult>;
|
|
11
|
+
close(): void;
|
|
12
|
+
execute<TResult = any, TInput = any>(request: MicroServiceRequest<TInput>): Promise<TResult>;
|
|
9
13
|
private isValidHttpUrl;
|
|
10
14
|
}
|
|
@@ -22,6 +22,14 @@ class HttpServiceAdapter {
|
|
|
22
22
|
timeout: requestTimeout,
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
|
+
connect() {
|
|
26
|
+
return Promise.resolve();
|
|
27
|
+
}
|
|
28
|
+
emit(request) {
|
|
29
|
+
return this.execute(request);
|
|
30
|
+
}
|
|
31
|
+
close() {
|
|
32
|
+
}
|
|
25
33
|
async execute(request) {
|
|
26
34
|
const { payload, overrides, method } = request;
|
|
27
35
|
const { url, requestTimeout } = this.options.options;
|
package/dist/adapter/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { GrpcOptions, KafkaOptions, KafkaParserConfig, MqttOptions, NatsOptions, RedisOptions, RmqOptions, TcpClientOptions, TcpOptions } from "./adapter.interface";
|
|
1
2
|
import { HttpServiceAdapter } from "./http-service.adapter";
|
|
2
3
|
import { MicroServiceAdapterFactory } from "./micro-service-adapter-factory";
|
|
3
4
|
import { MicroServiceAdapter, HttpOption, MicroServiceRequest } from "./micro-service.adapter";
|
|
4
|
-
|
|
5
|
+
import { NestJsMicroServiceAdapter } from "./nestjs-micro-service.adapter";
|
|
6
|
+
export { MicroServiceAdapter, HttpServiceAdapter, HttpOption, MicroServiceRequest, MicroServiceAdapterFactory, NestJsMicroServiceAdapter, GrpcOptions, TcpOptions, TcpClientOptions, RedisOptions, MqttOptions, NatsOptions, RmqOptions, KafkaOptions, KafkaParserConfig, };
|
package/dist/adapter/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MicroServiceAdapterFactory = exports.HttpServiceAdapter = void 0;
|
|
3
|
+
exports.NestJsMicroServiceAdapter = exports.MicroServiceAdapterFactory = exports.HttpServiceAdapter = void 0;
|
|
4
4
|
const http_service_adapter_1 = require("./http-service.adapter");
|
|
5
5
|
Object.defineProperty(exports, "HttpServiceAdapter", { enumerable: true, get: function () { return http_service_adapter_1.HttpServiceAdapter; } });
|
|
6
6
|
const micro_service_adapter_factory_1 = require("./micro-service-adapter-factory");
|
|
7
7
|
Object.defineProperty(exports, "MicroServiceAdapterFactory", { enumerable: true, get: function () { return micro_service_adapter_factory_1.MicroServiceAdapterFactory; } });
|
|
8
|
+
const nestjs_micro_service_adapter_1 = require("./nestjs-micro-service.adapter");
|
|
9
|
+
Object.defineProperty(exports, "NestJsMicroServiceAdapter", { enumerable: true, get: function () { return nestjs_micro_service_adapter_1.NestJsMicroServiceAdapter; } });
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MicroServiceAdapter } from ".";
|
|
2
2
|
import { ServiceClientOptions } from "../client";
|
|
3
3
|
export interface IMicroServiceAdapterFactory {
|
|
4
|
-
createAdapter(clientOptions: ServiceClientOptions):
|
|
4
|
+
createAdapter(clientOptions: ServiceClientOptions): MicroServiceAdapter;
|
|
5
5
|
}
|
|
6
6
|
export declare class MicroServiceAdapterFactory {
|
|
7
7
|
static createAdapter(clientOptions: ServiceClientOptions): MicroServiceAdapter;
|
|
8
|
+
private static createClientProxy;
|
|
8
9
|
}
|
|
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.MicroServiceAdapterFactory = void 0;
|
|
4
4
|
const _1 = require(".");
|
|
5
5
|
const enum_1 = require("../enum");
|
|
6
|
+
const microservices_1 = require("@nestjs/microservices");
|
|
7
|
+
const nestjs_micro_service_adapter_1 = require("./nestjs-micro-service.adapter");
|
|
6
8
|
class MicroServiceAdapterFactory {
|
|
7
9
|
static createAdapter(clientOptions) {
|
|
8
10
|
const { transport } = clientOptions || {};
|
|
@@ -11,11 +13,41 @@ class MicroServiceAdapterFactory {
|
|
|
11
13
|
case enum_1.Transport.HTTP:
|
|
12
14
|
adapter = new _1.HttpServiceAdapter(clientOptions);
|
|
13
15
|
break;
|
|
16
|
+
case enum_1.Transport.REDIS:
|
|
17
|
+
case enum_1.Transport.NATS:
|
|
18
|
+
case enum_1.Transport.MQTT:
|
|
19
|
+
case enum_1.Transport.GRPC:
|
|
20
|
+
case enum_1.Transport.RMQ:
|
|
21
|
+
case enum_1.Transport.KAFKA:
|
|
22
|
+
case enum_1.Transport.TCP:
|
|
23
|
+
adapter = new nestjs_micro_service_adapter_1.NestJsMicroServiceAdapter(this.createClientProxy(clientOptions));
|
|
24
|
+
break;
|
|
14
25
|
default:
|
|
15
26
|
adapter = new _1.HttpServiceAdapter(clientOptions);
|
|
16
27
|
break;
|
|
17
28
|
}
|
|
18
29
|
return adapter;
|
|
19
30
|
}
|
|
31
|
+
static createClientProxy(clientOptions) {
|
|
32
|
+
const { transport } = clientOptions || {};
|
|
33
|
+
switch (transport) {
|
|
34
|
+
case enum_1.Transport.REDIS:
|
|
35
|
+
return new microservices_1.ClientRedis(clientOptions);
|
|
36
|
+
case enum_1.Transport.NATS:
|
|
37
|
+
return new microservices_1.ClientNats(clientOptions);
|
|
38
|
+
case enum_1.Transport.MQTT:
|
|
39
|
+
return new microservices_1.ClientMqtt(clientOptions);
|
|
40
|
+
case enum_1.Transport.GRPC:
|
|
41
|
+
return new microservices_1.ClientGrpcProxy(clientOptions);
|
|
42
|
+
case enum_1.Transport.RMQ:
|
|
43
|
+
return new microservices_1.ClientRMQ(clientOptions);
|
|
44
|
+
case enum_1.Transport.KAFKA:
|
|
45
|
+
return new microservices_1.ClientKafka(clientOptions);
|
|
46
|
+
case enum_1.Transport.TCP:
|
|
47
|
+
return new microservices_1.ClientTCP(clientOptions);
|
|
48
|
+
default:
|
|
49
|
+
return new microservices_1.ClientTCP(clientOptions);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
20
52
|
}
|
|
21
53
|
exports.MicroServiceAdapterFactory = MicroServiceAdapterFactory;
|
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
import { JwtOptions } from "@nathapp/auth";
|
|
2
|
-
import {
|
|
2
|
+
import { Observable } from "rxjs";
|
|
3
|
+
import { Transport } from "../enum/transport.enum";
|
|
3
4
|
export interface HttpOption {
|
|
4
|
-
transport
|
|
5
|
-
options
|
|
6
|
-
url
|
|
5
|
+
transport?: Transport.HTTP;
|
|
6
|
+
options?: {
|
|
7
|
+
url?: string;
|
|
7
8
|
jwtOptions?: JwtOptions;
|
|
8
9
|
requestTimeout?: number;
|
|
9
10
|
};
|
|
10
11
|
}
|
|
11
|
-
export interface MicroServiceRequest {
|
|
12
|
+
export interface MicroServiceRequest<TInput = any> {
|
|
12
13
|
method: string;
|
|
13
|
-
payload?:
|
|
14
|
+
payload?: TInput;
|
|
14
15
|
overrides?: any;
|
|
15
16
|
version?: string;
|
|
16
17
|
}
|
|
17
18
|
export interface MicroServiceAdapter {
|
|
18
|
-
|
|
19
|
+
connect(): Promise<any>;
|
|
20
|
+
execute<TResult = any, TInput = any>(request: MicroServiceRequest<TInput>): Promise<TResult> | Observable<TResult>;
|
|
21
|
+
emit<TResult = any, TInput = any>(request: MicroServiceRequest<TInput>): Promise<TResult> | Observable<TResult>;
|
|
22
|
+
close(): any;
|
|
19
23
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
import { MicroServiceAdapter, MicroServiceRequest } from "./micro-service.adapter";
|
|
3
|
+
import { ClientProxy, ClientGrpcProxy } from '@nestjs/microservices';
|
|
4
|
+
export declare class NestJsMicroServiceAdapter implements MicroServiceAdapter {
|
|
5
|
+
private readonly clientProxy;
|
|
6
|
+
constructor(clientProxy: ClientProxy | ClientGrpcProxy);
|
|
7
|
+
connect(): Promise<any>;
|
|
8
|
+
execute<TResult = any, TInput = any>(request: MicroServiceRequest<TInput>): Promise<TResult> | Observable<TResult>;
|
|
9
|
+
emit<TResult = any, TInput = any>(request: MicroServiceRequest<TInput>): Promise<TResult> | Observable<TResult>;
|
|
10
|
+
close(): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NestJsMicroServiceAdapter = void 0;
|
|
4
|
+
class NestJsMicroServiceAdapter {
|
|
5
|
+
constructor(clientProxy) {
|
|
6
|
+
this.clientProxy = clientProxy;
|
|
7
|
+
}
|
|
8
|
+
connect() {
|
|
9
|
+
return this.clientProxy.connect();
|
|
10
|
+
}
|
|
11
|
+
execute(request) {
|
|
12
|
+
return this.clientProxy.send(request.method, request.payload);
|
|
13
|
+
}
|
|
14
|
+
emit(request) {
|
|
15
|
+
return this.clientProxy.emit(request.method, request.payload);
|
|
16
|
+
}
|
|
17
|
+
close() {
|
|
18
|
+
this.close();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.NestJsMicroServiceAdapter = NestJsMicroServiceAdapter;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Logger } from '@nestjs/common/services/logger.service';
|
|
3
|
+
import { EventEmitter } from 'events';
|
|
4
|
+
import { Observable, ReplaySubject } from 'rxjs';
|
|
5
|
+
import { RmqUrl } from '@nestjs/microservices/external/rmq-url.interface';
|
|
6
|
+
import { ClientProxy, RmqOptions, WritePacket, ReadPacket } from '@nestjs/microservices';
|
|
7
|
+
type Channel = any;
|
|
8
|
+
type ChannelWrapper = any;
|
|
9
|
+
type AmqpConnectionManager = any;
|
|
10
|
+
export declare class CustomClientRMQ extends ClientProxy {
|
|
11
|
+
protected readonly options: RmqOptions['options'];
|
|
12
|
+
protected readonly logger: Logger;
|
|
13
|
+
protected connection$: ReplaySubject<any>;
|
|
14
|
+
protected connection: Promise<any>;
|
|
15
|
+
protected client: AmqpConnectionManager;
|
|
16
|
+
protected channel: ChannelWrapper;
|
|
17
|
+
protected urls: string[] | RmqUrl[];
|
|
18
|
+
protected queue: string;
|
|
19
|
+
protected queueOptions: Record<string, any>;
|
|
20
|
+
protected responseEmitter: EventEmitter;
|
|
21
|
+
protected replyQueue: string;
|
|
22
|
+
protected persistent: boolean;
|
|
23
|
+
protected noAssert: boolean;
|
|
24
|
+
constructor(options: RmqOptions['options']);
|
|
25
|
+
close(): void;
|
|
26
|
+
connect(): Promise<any>;
|
|
27
|
+
createChannel(): Promise<void>;
|
|
28
|
+
createClient<T = any>(): T;
|
|
29
|
+
mergeDisconnectEvent<T = any>(instance: any, source$: Observable<T>): Observable<T>;
|
|
30
|
+
convertConnectionToPromise(): Promise<any>;
|
|
31
|
+
setupChannel(channel: Channel, resolve: Function): Promise<void>;
|
|
32
|
+
consumeChannel(channel: any): Promise<void>;
|
|
33
|
+
handleError(client: any): void;
|
|
34
|
+
handleDisconnectError(client: AmqpConnectionManager): void;
|
|
35
|
+
handleMessage(packet: unknown, callback: (packet: WritePacket) => any): any;
|
|
36
|
+
handleMessage(packet: unknown, options: Record<string, unknown>, callback: (packet: WritePacket) => any): any;
|
|
37
|
+
protected publish(message: ReadPacket, callback: (packet: WritePacket) => any): () => void;
|
|
38
|
+
protected dispatchEvent(packet: ReadPacket): Promise<any>;
|
|
39
|
+
protected initializeSerializer(options: RmqOptions['options']): void;
|
|
40
|
+
protected mergeHeaders(requestHeaders?: Record<string, string>): Record<string, string> | undefined;
|
|
41
|
+
}
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CustomClientRMQ = void 0;
|
|
4
|
+
const logger_service_1 = require("@nestjs/common/services/logger.service");
|
|
5
|
+
const load_package_util_1 = require("@nestjs/common/utils/load-package.util");
|
|
6
|
+
const random_string_generator_util_1 = require("@nestjs/common/utils/random-string-generator.util");
|
|
7
|
+
const events_1 = require("events");
|
|
8
|
+
const rxjs_1 = require("rxjs");
|
|
9
|
+
const operators_1 = require("rxjs/operators");
|
|
10
|
+
const constants_1 = require("@nestjs/microservices/constants");
|
|
11
|
+
const rmq_record_serializer_1 = require("@nestjs/microservices/serializers/rmq-record.serializer");
|
|
12
|
+
const microservices_1 = require("@nestjs/microservices");
|
|
13
|
+
const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
|
14
|
+
const JSONbig = require("json-bigint");
|
|
15
|
+
let rqmPackage = {};
|
|
16
|
+
const REPLY_QUEUE = 'amq.rabbitmq.reply-to';
|
|
17
|
+
class CustomClientRMQ extends microservices_1.ClientProxy {
|
|
18
|
+
constructor(options) {
|
|
19
|
+
super();
|
|
20
|
+
this.options = options;
|
|
21
|
+
this.logger = new logger_service_1.Logger(microservices_1.ClientProxy.name);
|
|
22
|
+
this.client = null;
|
|
23
|
+
this.channel = null;
|
|
24
|
+
this.urls = this.getOptionsProp(this.options, 'urls') || [constants_1.RQM_DEFAULT_URL];
|
|
25
|
+
this.queue =
|
|
26
|
+
this.getOptionsProp(this.options, 'queue') || constants_1.RQM_DEFAULT_QUEUE;
|
|
27
|
+
this.queueOptions =
|
|
28
|
+
this.getOptionsProp(this.options, 'queueOptions') ||
|
|
29
|
+
constants_1.RQM_DEFAULT_QUEUE_OPTIONS;
|
|
30
|
+
this.replyQueue =
|
|
31
|
+
this.getOptionsProp(this.options, 'replyQueue') || REPLY_QUEUE;
|
|
32
|
+
this.persistent =
|
|
33
|
+
this.getOptionsProp(this.options, 'persistent') || constants_1.RQM_DEFAULT_PERSISTENT;
|
|
34
|
+
this.noAssert =
|
|
35
|
+
this.getOptionsProp(this.options, 'noAssert') || constants_1.RQM_DEFAULT_NO_ASSERT;
|
|
36
|
+
(0, load_package_util_1.loadPackage)('amqplib', CustomClientRMQ.name, () => require('amqplib'));
|
|
37
|
+
rqmPackage = (0, load_package_util_1.loadPackage)('amqp-connection-manager', CustomClientRMQ.name, () => require('amqp-connection-manager'));
|
|
38
|
+
this.initializeSerializer(options);
|
|
39
|
+
this.initializeDeserializer(options);
|
|
40
|
+
}
|
|
41
|
+
close() {
|
|
42
|
+
this.channel && this.channel.close();
|
|
43
|
+
this.client && this.client.close();
|
|
44
|
+
this.channel = null;
|
|
45
|
+
this.client = null;
|
|
46
|
+
}
|
|
47
|
+
connect() {
|
|
48
|
+
if (this.client) {
|
|
49
|
+
return this.convertConnectionToPromise();
|
|
50
|
+
}
|
|
51
|
+
this.client = this.createClient();
|
|
52
|
+
this.handleError(this.client);
|
|
53
|
+
this.handleDisconnectError(this.client);
|
|
54
|
+
this.responseEmitter = new events_1.EventEmitter();
|
|
55
|
+
this.responseEmitter.setMaxListeners(0);
|
|
56
|
+
const connect$ = this.connect$(this.client);
|
|
57
|
+
const withDisconnect$ = this.mergeDisconnectEvent(this.client, connect$).pipe((0, operators_1.switchMap)(() => this.createChannel()));
|
|
58
|
+
const withReconnect$ = (0, rxjs_1.fromEvent)(this.client, constants_1.CONNECT_EVENT).pipe((0, operators_1.skip)(1));
|
|
59
|
+
const source$ = (0, rxjs_1.merge)(withDisconnect$, withReconnect$);
|
|
60
|
+
this.connection$ = new rxjs_1.ReplaySubject(1);
|
|
61
|
+
source$.subscribe(this.connection$);
|
|
62
|
+
return this.convertConnectionToPromise();
|
|
63
|
+
}
|
|
64
|
+
createChannel() {
|
|
65
|
+
return new Promise(resolve => {
|
|
66
|
+
this.channel = this.client.createChannel({
|
|
67
|
+
json: false,
|
|
68
|
+
setup: (channel) => this.setupChannel(channel, resolve),
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
createClient() {
|
|
73
|
+
const socketOptions = this.getOptionsProp(this.options, 'socketOptions');
|
|
74
|
+
return rqmPackage.connect(this.urls, {
|
|
75
|
+
connectionOptions: socketOptions,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
mergeDisconnectEvent(instance, source$) {
|
|
79
|
+
const eventToError = (eventType) => (0, rxjs_1.fromEvent)(instance, eventType).pipe((0, operators_1.map)((err) => {
|
|
80
|
+
throw err;
|
|
81
|
+
}));
|
|
82
|
+
const disconnect$ = eventToError(constants_1.DISCONNECT_EVENT);
|
|
83
|
+
const urls = this.getOptionsProp(this.options, 'urls', []);
|
|
84
|
+
const connectFailed$ = eventToError(constants_1.CONNECT_FAILED_EVENT).pipe((0, operators_1.retryWhen)(e => e.pipe((0, operators_1.scan)((errorCount, error) => {
|
|
85
|
+
if (urls.indexOf(error.url) >= urls.length - 1) {
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
return errorCount + 1;
|
|
89
|
+
}, 0))));
|
|
90
|
+
return (0, rxjs_1.merge)(source$, disconnect$, connectFailed$).pipe((0, operators_1.first)());
|
|
91
|
+
}
|
|
92
|
+
async convertConnectionToPromise() {
|
|
93
|
+
try {
|
|
94
|
+
return await (0, rxjs_1.firstValueFrom)(this.connection$);
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
if (err instanceof rxjs_1.EmptyError) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
throw err;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
async setupChannel(channel, resolve) {
|
|
104
|
+
const prefetchCount = this.getOptionsProp(this.options, 'prefetchCount') ||
|
|
105
|
+
constants_1.RQM_DEFAULT_PREFETCH_COUNT;
|
|
106
|
+
const isGlobalPrefetchCount = this.getOptionsProp(this.options, 'isGlobalPrefetchCount') ||
|
|
107
|
+
constants_1.RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT;
|
|
108
|
+
if (!this.queueOptions.noAssert) {
|
|
109
|
+
await channel.assertQueue(this.queue, this.queueOptions);
|
|
110
|
+
}
|
|
111
|
+
await channel.prefetch(prefetchCount, isGlobalPrefetchCount);
|
|
112
|
+
await this.consumeChannel(channel);
|
|
113
|
+
resolve();
|
|
114
|
+
}
|
|
115
|
+
async consumeChannel(channel) {
|
|
116
|
+
const noAck = this.getOptionsProp(this.options, 'noAck', constants_1.RQM_DEFAULT_NOACK);
|
|
117
|
+
await channel.consume(this.replyQueue, (msg) => this.responseEmitter.emit(msg.properties.correlationId, msg), {
|
|
118
|
+
noAck,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
handleError(client) {
|
|
122
|
+
client.addListener(constants_1.ERROR_EVENT, (err) => this.logger.error(err));
|
|
123
|
+
}
|
|
124
|
+
handleDisconnectError(client) {
|
|
125
|
+
client.addListener(constants_1.DISCONNECT_EVENT, (err) => {
|
|
126
|
+
this.logger.error(constants_1.DISCONNECTED_RMQ_MESSAGE);
|
|
127
|
+
this.logger.error(err);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
async handleMessage(packet, options, callback) {
|
|
131
|
+
if ((0, shared_utils_1.isFunction)(options)) {
|
|
132
|
+
callback = options;
|
|
133
|
+
options = undefined;
|
|
134
|
+
}
|
|
135
|
+
const { err, response, isDisposed } = await this.deserializer.deserialize(packet, options);
|
|
136
|
+
if (isDisposed || err) {
|
|
137
|
+
callback({
|
|
138
|
+
err,
|
|
139
|
+
response,
|
|
140
|
+
isDisposed: true,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
callback({
|
|
144
|
+
err,
|
|
145
|
+
response,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
publish(message, callback) {
|
|
149
|
+
try {
|
|
150
|
+
const correlationId = (0, random_string_generator_util_1.randomStringGenerator)();
|
|
151
|
+
const listener = ({ content, options, }) => this.handleMessage(JSONbig.parse(content.toString()), options, callback);
|
|
152
|
+
Object.assign(message, { id: correlationId });
|
|
153
|
+
const serializedPacket = this.serializer.serialize(message);
|
|
154
|
+
const options = serializedPacket.options;
|
|
155
|
+
delete serializedPacket.options;
|
|
156
|
+
this.responseEmitter.on(correlationId, listener);
|
|
157
|
+
this.channel.sendToQueue(this.queue, Buffer.from(JSONbig.stringify(serializedPacket.data)), {
|
|
158
|
+
replyTo: this.replyQueue,
|
|
159
|
+
persistent: this.persistent,
|
|
160
|
+
...options,
|
|
161
|
+
headers: this.mergeHeaders(options === null || options === void 0 ? void 0 : options.headers),
|
|
162
|
+
correlationId,
|
|
163
|
+
})
|
|
164
|
+
.catch(err => callback({ err }));
|
|
165
|
+
;
|
|
166
|
+
return () => this.responseEmitter.removeListener(correlationId, listener);
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
callback({ err });
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
dispatchEvent(packet) {
|
|
173
|
+
const serializedPacket = this.serializer.serialize(packet);
|
|
174
|
+
const options = serializedPacket.options;
|
|
175
|
+
delete serializedPacket.options;
|
|
176
|
+
return new Promise((resolve, reject) => this.channel.sendToQueue(this.queue, Buffer.from(JSONbig.stringify(serializedPacket.data)), {
|
|
177
|
+
persistent: this.persistent,
|
|
178
|
+
...options,
|
|
179
|
+
headers: this.mergeHeaders(options === null || options === void 0 ? void 0 : options.headers),
|
|
180
|
+
}, (err) => (err ? reject(err) : resolve())));
|
|
181
|
+
}
|
|
182
|
+
initializeSerializer(options) {
|
|
183
|
+
var _a;
|
|
184
|
+
this.serializer = (_a = options === null || options === void 0 ? void 0 : options.serializer) !== null && _a !== void 0 ? _a : new rmq_record_serializer_1.RmqRecordSerializer();
|
|
185
|
+
}
|
|
186
|
+
mergeHeaders(requestHeaders) {
|
|
187
|
+
var _a, _b;
|
|
188
|
+
if (!requestHeaders && !((_a = this.options) === null || _a === void 0 ? void 0 : _a.headers)) {
|
|
189
|
+
return undefined;
|
|
190
|
+
}
|
|
191
|
+
return {
|
|
192
|
+
...(_b = this.options) === null || _b === void 0 ? void 0 : _b.headers,
|
|
193
|
+
...requestHeaders,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
exports.CustomClientRMQ = CustomClientRMQ;
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Closeable } from "./closeable.interface";
|
|
2
|
+
import { CustomClientRMQ } from "./custom-client-rmq";
|
|
2
3
|
import { ServiceClient, DefaultServiceClient, ServiceClientOptions, AuthenticateOptions } from "./service-client";
|
|
3
4
|
import { IServiceClientFactory, ServiceClientFactory } from "./service-client-factory";
|
|
4
|
-
export { ServiceClientOptions, AuthenticateOptions, ServiceClient, DefaultServiceClient, Closeable, IServiceClientFactory, ServiceClientFactory, };
|
|
5
|
+
export { CustomClientRMQ, ServiceClientOptions, AuthenticateOptions, ServiceClient, DefaultServiceClient, Closeable, IServiceClientFactory, ServiceClientFactory, };
|
package/dist/client/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ServiceClientFactory = exports.DefaultServiceClient = void 0;
|
|
3
|
+
exports.ServiceClientFactory = exports.DefaultServiceClient = exports.CustomClientRMQ = void 0;
|
|
4
|
+
const custom_client_rmq_1 = require("./custom-client-rmq");
|
|
5
|
+
Object.defineProperty(exports, "CustomClientRMQ", { enumerable: true, get: function () { return custom_client_rmq_1.CustomClientRMQ; } });
|
|
4
6
|
const service_client_1 = require("./service-client");
|
|
5
7
|
Object.defineProperty(exports, "DefaultServiceClient", { enumerable: true, get: function () { return service_client_1.DefaultServiceClient; } });
|
|
6
8
|
const service_client_factory_1 = require("./service-client-factory");
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { JwtOptions } from "@nathapp/auth";
|
|
2
2
|
import { HttpOption, MicroServiceAdapter, MicroServiceRequest } from "../adapter";
|
|
3
3
|
import { Closeable } from "./closeable.interface";
|
|
4
|
+
import { Observable } from "rxjs";
|
|
5
|
+
import { RedisOptions, NatsOptions, MqttOptions, GrpcOptions, KafkaOptions, TcpClientOptions, RmqOptions } from "../adapter/adapter.interface";
|
|
4
6
|
export type AuthenticateOptions = JwtOptions;
|
|
5
|
-
export type ServiceClientOptions = HttpOption;
|
|
7
|
+
export type ServiceClientOptions = RedisOptions | NatsOptions | MqttOptions | GrpcOptions | KafkaOptions | TcpClientOptions | RmqOptions | HttpOption;
|
|
6
8
|
export interface ServiceClient {
|
|
7
|
-
execute(request: MicroServiceRequest): Promise<any>;
|
|
9
|
+
execute(request: MicroServiceRequest): Promise<any> | Observable<any>;
|
|
8
10
|
}
|
|
9
11
|
export declare class DefaultServiceClient implements ServiceClient, Closeable {
|
|
10
12
|
private readonly adapter;
|
|
11
13
|
constructor(adapter: MicroServiceAdapter);
|
|
12
14
|
close(): void;
|
|
13
|
-
execute(request: MicroServiceRequest): Promise<any>;
|
|
15
|
+
execute(request: MicroServiceRequest): Promise<any> | Observable<any>;
|
|
14
16
|
}
|