@nestjs/microservices
Advanced tools
Comparing version 5.5.0 to 6.0.0-alpha.1
@@ -13,5 +13,5 @@ import { Logger } from '@nestjs/common/services/logger.service'; | ||
getService<T extends {}>(name: string): T; | ||
createServiceMethod(client: any, methodName: string): (...args) => Observable<any>; | ||
createStreamServiceMethod(client: any, methodName: string): (...args) => Observable<any>; | ||
createUnaryServiceMethod(client: any, methodName: string): (...args) => Observable<any>; | ||
createServiceMethod(client: any, methodName: string): (...args: any[]) => Observable<any>; | ||
createStreamServiceMethod(client: any, methodName: string): (...args: any[]) => Observable<any>; | ||
createUnaryServiceMethod(client: any, methodName: string): (...args: any[]) => Observable<any>; | ||
createClient(): any; | ||
@@ -23,3 +23,3 @@ loadProto(): any; | ||
send<TResult = any, TInput = any>(pattern: any, data: TInput): Observable<TResult>; | ||
protected publish(partialPacket: any, callback: (packet) => any): void; | ||
protected publish(partialPacket: any, callback: (packet: any) => any): void; | ||
} |
@@ -8,5 +8,5 @@ "use strict"; | ||
const constants_1 = require("../constants"); | ||
const invalid_grpc_package_exception_1 = require("../exceptions/errors/invalid-grpc-package.exception"); | ||
const invalid_grpc_service_exception_1 = require("../exceptions/errors/invalid-grpc-service.exception"); | ||
const invalid_proto_definition_exception_1 = require("../exceptions/errors/invalid-proto-definition.exception"); | ||
const invalid_grpc_package_exception_1 = require("../errors/invalid-grpc-package.exception"); | ||
const invalid_grpc_service_exception_1 = require("../errors/invalid-grpc-service.exception"); | ||
const invalid_proto_definition_exception_1 = require("../errors/invalid-proto-definition.exception"); | ||
const client_proxy_1 = require("./client-proxy"); | ||
@@ -23,3 +23,4 @@ const constants_2 = require("./constants"); | ||
this.getOptionsProp(options, 'url') || constants_1.GRPC_DEFAULT_URL; | ||
const protoLoader = this.getOptionsProp(options, 'protoLoader') || constants_1.GRPC_DEFAULT_PROTO_LOADER; | ||
const protoLoader = this.getOptionsProp(options, 'protoLoader') || | ||
constants_1.GRPC_DEFAULT_PROTO_LOADER; | ||
grpcPackage = load_package_util_1.loadPackage('grpc', ClientGrpcProxy.name); | ||
@@ -26,0 +27,0 @@ grpcProtoLoaderPackage = load_package_util_1.loadPackage(protoLoader, ClientGrpcProxy.name); |
@@ -0,1 +1,2 @@ | ||
/// <reference types="node" /> | ||
import { Logger } from '@nestjs/common/services/logger.service'; | ||
@@ -21,4 +22,4 @@ import { Observable } from 'rxjs'; | ||
handleError(client: MqttClient): void; | ||
createResponseCallback(): (channel: string, buffer) => any; | ||
createResponseCallback(): (channel: string, buffer: Buffer) => any; | ||
protected publish(partialPacket: ReadPacket, callback: (packet: WritePacket) => any): Function; | ||
} |
@@ -44,3 +44,3 @@ "use strict"; | ||
mergeCloseEvent(instance, source$) { | ||
const close$ = rxjs_1.fromEvent(instance, constants_1.CLOSE_EVENT).pipe(operators_1.map(err => { | ||
const close$ = rxjs_1.fromEvent(instance, constants_1.CLOSE_EVENT).pipe(operators_1.map((err) => { | ||
throw err; | ||
@@ -54,3 +54,3 @@ })); | ||
handleError(client) { | ||
client.addListener(constants_1.ERROR_EVENT, err => err.code !== constants_2.ECONNREFUSED && this.logger.error(err)); | ||
client.addListener(constants_1.ERROR_EVENT, (err) => err.code !== constants_2.ECONNREFUSED && this.logger.error(err)); | ||
} | ||
@@ -82,3 +82,3 @@ createResponseCallback() { | ||
const responseChannel = this.getResPatternName(pattern); | ||
this.mqttClient.subscribe(responseChannel, err => { | ||
this.mqttClient.subscribe(responseChannel, (err) => { | ||
if (err) { | ||
@@ -85,0 +85,0 @@ return; |
@@ -40,3 +40,3 @@ "use strict"; | ||
handleError(client) { | ||
client.addListener(constants_1.ERROR_EVENT, err => err.code !== constants_2.CONN_ERR && this.logger.error(err)); | ||
client.addListener(constants_1.ERROR_EVENT, (err) => err.code !== constants_2.CONN_ERR && this.logger.error(err)); | ||
} | ||
@@ -43,0 +43,0 @@ createSubscriptionHandler(packet, callback) { |
import { ClientOptions } from '../interfaces/client-metadata.interface'; | ||
import { Closeable } from '../interfaces/closeable.interface'; | ||
import { ClientProxy } from './client-proxy'; | ||
export interface IClientProxyFactory { | ||
create(clientOptions: ClientOptions): ClientProxy & Closeable; | ||
} | ||
export declare class ClientProxyFactory { | ||
static create(clientOptions: ClientOptions): ClientProxy & Closeable; | ||
} |
@@ -8,4 +8,4 @@ "use strict"; | ||
const client_redis_1 = require("./client-redis"); | ||
const client_rmq_1 = require("./client-rmq"); | ||
const client_tcp_1 = require("./client-tcp"); | ||
const client_rmq_1 = require("./client-rmq"); | ||
class ClientProxyFactory { | ||
@@ -12,0 +12,0 @@ static create(clientOptions) { |
@@ -13,5 +13,5 @@ import { Observable, Observer } from 'rxjs'; | ||
protected getOptionsProp<T extends { | ||
options?; | ||
options?: any; | ||
}>(obj: ClientOptions['options'], prop: keyof T['options'], defaultValue?: any): any; | ||
protected normalizePattern<T = any>(pattern: T): string; | ||
} |
@@ -8,3 +8,3 @@ "use strict"; | ||
const constants_1 = require("../constants"); | ||
const invalid_message_exception_1 = require("../exceptions/errors/invalid-message.exception"); | ||
const invalid_message_exception_1 = require("../errors/invalid-message.exception"); | ||
class ClientProxy { | ||
@@ -39,3 +39,3 @@ constructor() { | ||
connect$(instance, errorEvent = constants_1.ERROR_EVENT, connectEvent = constants_1.CONNECT_EVENT) { | ||
const error$ = rxjs_1.fromEvent(instance, errorEvent).pipe(operators_1.map(err => { | ||
const error$ = rxjs_1.fromEvent(instance, errorEvent).pipe(operators_1.map((err) => { | ||
throw err; | ||
@@ -42,0 +42,0 @@ })); |
@@ -24,4 +24,4 @@ import { Logger } from '@nestjs/common/services/logger.service'; | ||
createRetryStrategy(options: RetryStrategyOptions, error$: Subject<Error>): undefined | number | Error; | ||
createResponseCallback(): Function; | ||
createResponseCallback(): (channel: string, buffer: string) => void; | ||
protected publish(partialPacket: ReadPacket, callback: (packet: WritePacket) => any): Function; | ||
} |
@@ -53,6 +53,6 @@ "use strict"; | ||
handleError(client) { | ||
client.addListener(constants_1.ERROR_EVENT, err => this.logger.error(err)); | ||
client.addListener(constants_1.ERROR_EVENT, (err) => this.logger.error(err)); | ||
} | ||
getClientOptions(error$) { | ||
const retry_strategy = options => this.createRetryStrategy(options, error$); | ||
const retry_strategy = (options) => this.createRetryStrategy(options, error$); | ||
return { | ||
@@ -101,3 +101,3 @@ retry_strategy, | ||
this.routingMap.set(packet.id, callback); | ||
this.subClient.subscribe(responseChannel, err => { | ||
this.subClient.subscribe(responseChannel, (err) => { | ||
if (err) { | ||
@@ -104,0 +104,0 @@ return; |
@@ -42,3 +42,3 @@ "use strict"; | ||
consumeChannel() { | ||
this.channel.addSetup(channel => channel.consume(this.replyQueue, msg => this.responseEmitter.emit(msg.properties.correlationId, msg), { noAck: true })); | ||
this.channel.addSetup((channel) => channel.consume(this.replyQueue, (msg) => this.responseEmitter.emit(msg.properties.correlationId, msg), { noAck: true })); | ||
} | ||
@@ -61,3 +61,3 @@ connect() { | ||
json: false, | ||
setup: channel => this.setupChannel(channel, resolve), | ||
setup: (channel) => this.setupChannel(channel, resolve), | ||
}); | ||
@@ -70,3 +70,3 @@ }); | ||
mergeDisconnectEvent(instance, source$) { | ||
const close$ = rxjs_1.fromEvent(instance, constants_1.DISCONNECT_EVENT).pipe(operators_1.map(err => { | ||
const close$ = rxjs_1.fromEvent(instance, constants_1.DISCONNECT_EVENT).pipe(operators_1.map((err) => { | ||
throw err; | ||
@@ -117,5 +117,5 @@ })); | ||
handleError(client) { | ||
client.addListener(constants_1.ERROR_EVENT, err => this.logger.error(err)); | ||
client.addListener(constants_1.ERROR_EVENT, (err) => this.logger.error(err)); | ||
} | ||
} | ||
exports.ClientRMQ = ClientRMQ; |
@@ -14,3 +14,3 @@ import * as JsonSocket from 'json-socket'; | ||
connect(): Promise<any>; | ||
handleResponse(buffer: WritePacket & PacketId): any; | ||
handleResponse(buffer: WritePacket & PacketId): void; | ||
createSocket(): JsonSocket; | ||
@@ -17,0 +17,0 @@ close(): void; |
@@ -62,3 +62,3 @@ "use strict"; | ||
bindEvents(socket) { | ||
socket.on(constants_1.ERROR_EVENT, err => err.code !== constants_2.ECONNREFUSED && this.handleError(err)); | ||
socket.on(constants_1.ERROR_EVENT, (err) => err.code !== constants_2.ECONNREFUSED && this.handleError(err)); | ||
socket.on(constants_1.CLOSE_EVENT, () => this.handleClose()); | ||
@@ -65,0 +65,0 @@ } |
@@ -5,4 +5,4 @@ export * from './client-grpc'; | ||
export * from './client-proxy'; | ||
export * from './client-proxy-factory'; | ||
export { ClientProxyFactory } from './client-proxy-factory'; | ||
export * from './client-redis'; | ||
export * from './client-tcp'; |
@@ -10,4 +10,5 @@ "use strict"; | ||
__export(require("./client-proxy")); | ||
__export(require("./client-proxy-factory")); | ||
var client_proxy_factory_1 = require("./client-proxy-factory"); | ||
exports.ClientProxyFactory = client_proxy_factory_1.ClientProxyFactory; | ||
__export(require("./client-redis")); | ||
__export(require("./client-tcp")); |
@@ -23,4 +23,4 @@ export declare const TCP_DEFAULT_PORT = 3000; | ||
export declare const RQM_DEFAULT_PREFETCH_COUNT = 0; | ||
export declare const RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT: boolean; | ||
export declare const RQM_DEFAULT_IS_GLOBAL_PREFETCH_COUNT = false; | ||
export declare const RQM_DEFAULT_QUEUE_OPTIONS: {}; | ||
export declare const GRPC_DEFAULT_PROTO_LOADER = "@grpc/proto-loader"; |
@@ -10,4 +10,4 @@ import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface'; | ||
constructor(container: NestContainer, config: ApplicationConfig); | ||
create(instance: Controller, callback: (data) => Observable<any>, module: string): RpcExceptionsHandler; | ||
create(instance: Controller, callback: <T = any>(data: T) => Observable<any>, module: string): RpcExceptionsHandler; | ||
getGlobalMetadata<T extends any[]>(): T; | ||
} |
@@ -21,6 +21,6 @@ import { Controller } from '@nestjs/common/interfaces'; | ||
constructor(rpcProxy: RpcProxy, exceptionFiltersContext: ExceptionFiltersContext, pipesCreator: PipesContextCreator, pipesConsumer: PipesConsumer, guardsContextCreator: GuardsContextCreator, guardsConsumer: GuardsConsumer, interceptorsContextCreator: InterceptorsContextCreator, interceptorsConsumer: InterceptorsConsumer); | ||
create(instance: Controller, callback: (data) => Observable<any>, module: any): (...args) => Promise<Observable<any>>; | ||
reflectCallbackParamtypes(instance: Controller, callback: (...args) => any): any[]; | ||
getDataMetatype(instance: any, callback: any): any; | ||
createGuardsFn(guards: any[], instance: Controller, callback: (...args) => any): Function | null; | ||
create(instance: Controller, callback: (data: any, ...args: any[]) => Observable<any>, module: string): (...args: any[]) => Promise<Observable<any>>; | ||
reflectCallbackParamtypes(instance: Controller, callback: (...args: any[]) => any): any[]; | ||
getDataMetatype(instance: Controller, callback: (...args: any[]) => any): any; | ||
createGuardsFn(guards: any[], instance: Controller, callback: (...args: any[]) => any): Function | null; | ||
} |
import { Observable } from 'rxjs'; | ||
import { RpcExceptionsHandler } from '../exceptions/rpc-exceptions-handler'; | ||
export declare class RpcProxy { | ||
create(targetCallback: (...args) => Promise<Observable<any>>, exceptionsHandler: RpcExceptionsHandler): (...args) => Promise<Observable<any>>; | ||
create(targetCallback: (...args: any[]) => Promise<Observable<any>>, exceptionsHandler: RpcExceptionsHandler): (...args: any[]) => Promise<Observable<any>>; | ||
handleError<T>(exceptionsHandler: RpcExceptionsHandler, args: any[], error: T): Observable<any>; | ||
isObservable(result: any): boolean; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const shared_utils_1 = require("@nestjs/common/utils/shared.utils"); | ||
const execution_context_host_1 = require("@nestjs/core/helpers/execution-context.host"); | ||
const execution_context_host_1 = require("@nestjs/core/helpers/execution-context-host"); | ||
const operators_1 = require("rxjs/operators"); | ||
@@ -6,0 +6,0 @@ class RpcProxy { |
@@ -9,7 +9,7 @@ import { PatternMetadata } from '../interfaces/pattern-metadata.interface'; | ||
*/ | ||
export declare function GrpcMethod(service?: string): any; | ||
export declare function GrpcMethod(service: string, method?: string): any; | ||
export declare function createMethodMetadata(target: any, key: string, service: string | undefined, method: string | undefined): { | ||
export declare function GrpcMethod(service?: string): MethodDecorator; | ||
export declare function GrpcMethod(service: string, method?: string): MethodDecorator; | ||
export declare function createMethodMetadata(target: any, key: string | symbol, service: string | undefined, method: string | undefined): { | ||
service: any; | ||
rpc: string; | ||
}; |
@@ -7,3 +7,3 @@ export declare enum Transport { | ||
GRPC = 4, | ||
RMQ = 5, | ||
RMQ = 5 | ||
} |
@@ -6,2 +6,3 @@ import { ArgumentsHost, RpcExceptionFilter } from '@nestjs/common'; | ||
catch(exception: T, host: ArgumentsHost): Observable<R>; | ||
isError(exception: any): exception is Error; | ||
} |
@@ -13,8 +13,4 @@ "use strict"; | ||
const errorMessage = constants_1.MESSAGES.UNKNOWN_EXCEPTION_MESSAGE; | ||
const isError = shared_utils_1.isObject(exception) && exception.message; | ||
const loggerArgs = isError | ||
? [ | ||
exception.message, | ||
exception.stack, | ||
] | ||
const loggerArgs = this.isError(exception) | ||
? [exception.message, exception.stack] | ||
: [exception]; | ||
@@ -29,4 +25,7 @@ const logger = BaseRpcExceptionFilter.logger; | ||
} | ||
isError(exception) { | ||
return !!(shared_utils_1.isObject(exception) && exception.message); | ||
} | ||
} | ||
BaseRpcExceptionFilter.logger = new common_1.Logger('RpcExceptionsHandler'); | ||
exports.BaseRpcExceptionFilter = BaseRpcExceptionFilter; |
@@ -10,3 +10,3 @@ import { RpcExceptionFilterMetadata } from '@nestjs/common/interfaces/exceptions'; | ||
setCustomFilters(filters: RpcExceptionFilterMetadata[]): void; | ||
invokeCustomFilters(exception: any, host: ArgumentsHost): Observable<any> | null; | ||
invokeCustomFilters<T = any>(exception: T, host: ArgumentsHost): Observable<any> | null; | ||
} |
import { Transport } from './../enums/transport.enum'; | ||
import { RedisOptions, NatsOptions, MqttOptions, GrpcOptions, RmqOptions } from './microservice-configuration.interface'; | ||
import { GrpcOptions, MqttOptions, NatsOptions, RedisOptions, RmqOptions } from './microservice-configuration.interface'; | ||
export declare type ClientOptions = RedisOptions | NatsOptions | MqttOptions | GrpcOptions | TcpClientOptions | RmqOptions; | ||
@@ -4,0 +4,0 @@ export interface TcpClientOptions { |
@@ -0,8 +1,8 @@ | ||
export * from './client-grpc.interface'; | ||
export * from './client-metadata.interface'; | ||
export * from './closeable.interface'; | ||
export * from './custom-transport-strategy.interface'; | ||
export * from './message-handler.interface'; | ||
export * from './microservice-configuration.interface'; | ||
export * from './packet.interface'; | ||
export * from './pattern-metadata.interface'; | ||
export * from './custom-transport-strategy.interface'; | ||
export * from './message-handlers.interface'; | ||
export * from './closeable.interface'; | ||
export * from './packet.interface'; | ||
export * from './client-grpc.interface'; |
import { Observable } from 'rxjs'; | ||
export interface MessageHandlers { | ||
[pattern: string]: (data) => Promise<Observable<any>>; | ||
[pattern: string]: (data: any) => Promise<Observable<any>>; | ||
} |
@@ -18,4 +18,2 @@ import { MqttClientOptions } from '@nestjs/common/interfaces/external/mqtt-options.interface'; | ||
protoLoader?: string; | ||
/** @deprecated */ | ||
root?: string; | ||
loader?: { | ||
@@ -22,0 +20,0 @@ keepCase?: boolean; |
import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface'; | ||
import { MetadataScanner } from '@nestjs/core/metadata-scanner'; | ||
import { ClientOptions } from './interfaces/client-metadata.interface'; | ||
import { PatternMetadata } from './interfaces/pattern-metadata.interface'; | ||
import { ClientOptions } from './interfaces/client-metadata.interface'; | ||
import { MetadataScanner } from '@nestjs/core/metadata-scanner'; | ||
export declare class ListenerMetadataExplorer { | ||
private readonly metadataScanner; | ||
constructor(metadataScanner: MetadataScanner); | ||
explore(instance: Controller): PatternProperties[]; | ||
exploreMethodMetadata(instance: any, instancePrototype: any, methodName: string): PatternProperties; | ||
scanForClientHooks(instance: Controller): IterableIterator<ClientProperties>; | ||
} | ||
export interface ClientProperties { | ||
@@ -18,3 +11,11 @@ property: string; | ||
pattern: PatternMetadata; | ||
targetCallback: (...args) => any; | ||
methodKey: string; | ||
targetCallback: (...args: any[]) => any; | ||
} | ||
export declare class ListenerMetadataExplorer { | ||
private readonly metadataScanner; | ||
constructor(metadataScanner: MetadataScanner); | ||
explore(instance: Controller): PatternProperties[]; | ||
exploreMethodMetadata(instance: object, instancePrototype: any, methodKey: string): PatternProperties; | ||
scanForClientHooks(instance: Controller): IterableIterator<ClientProperties>; | ||
} |
@@ -13,4 +13,4 @@ "use strict"; | ||
} | ||
exploreMethodMetadata(instance, instancePrototype, methodName) { | ||
const targetCallback = instancePrototype[methodName]; | ||
exploreMethodMetadata(instance, instancePrototype, methodKey) { | ||
const targetCallback = instancePrototype[methodKey]; | ||
const isPattern = Reflect.getMetadata(constants_1.PATTERN_HANDLER_METADATA, targetCallback); | ||
@@ -22,2 +22,3 @@ if (shared_utils_1.isUndefined(isPattern)) { | ||
return { | ||
methodKey, | ||
targetCallback, | ||
@@ -29,8 +30,10 @@ pattern, | ||
for (const propertyKey in instance) { | ||
if (shared_utils_1.isFunction(propertyKey)) | ||
if (shared_utils_1.isFunction(propertyKey)) { | ||
continue; | ||
} | ||
const property = String(propertyKey); | ||
const isClient = Reflect.getMetadata(constants_1.CLIENT_METADATA, instance, property); | ||
if (shared_utils_1.isUndefined(isClient)) | ||
if (shared_utils_1.isUndefined(isClient)) { | ||
continue; | ||
} | ||
const metadata = Reflect.getMetadata(constants_1.CLIENT_CONFIGURATION_METADATA, instance, property); | ||
@@ -37,0 +40,0 @@ yield { property, metadata }; |
import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface'; | ||
import { Server } from './server/server'; | ||
import { CustomTransportStrategy } from './interfaces'; | ||
import { NestContainer } from '@nestjs/core/injector/container'; | ||
import { Injector } from '@nestjs/core/injector/injector'; | ||
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper'; | ||
import { IClientProxyFactory } from './client/client-proxy-factory'; | ||
import { ClientsContainer } from './container'; | ||
import { RpcContextCreator } from './context/rpc-context-creator'; | ||
import { CustomTransportStrategy } from './interfaces'; | ||
import { Server } from './server/server'; | ||
export declare class ListenersController { | ||
private readonly clientsContainer; | ||
private readonly contextCreator; | ||
private readonly container; | ||
private readonly injector; | ||
private readonly clientFactory; | ||
private readonly metadataExplorer; | ||
constructor(clientsContainer: ClientsContainer, contextCreator: RpcContextCreator); | ||
bindPatternHandlers(instance: Controller, server: Server & CustomTransportStrategy, module: string): void; | ||
constructor(clientsContainer: ClientsContainer, contextCreator: RpcContextCreator, container: NestContainer, injector: Injector, clientFactory: IClientProxyFactory); | ||
bindPatternHandlers(instanceWrapper: InstanceWrapper<Controller>, server: Server & CustomTransportStrategy, moduleKey: string): void; | ||
bindClientsToProperties(instance: Controller): void; | ||
assignClientToInstance<T = any>(instance: Controller, property: string, client: T): void; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const context_id_factory_1 = require("@nestjs/core/helpers/context-id-factory"); | ||
const metadata_scanner_1 = require("@nestjs/core/metadata-scanner"); | ||
const listener_metadata_explorer_1 = require("./listener-metadata-explorer"); | ||
const client_proxy_factory_1 = require("./client/client-proxy-factory"); | ||
const metadata_scanner_1 = require("@nestjs/core/metadata-scanner"); | ||
class ListenersController { | ||
constructor(clientsContainer, contextCreator) { | ||
constructor(clientsContainer, contextCreator, container, injector, clientFactory) { | ||
this.clientsContainer = clientsContainer; | ||
this.contextCreator = contextCreator; | ||
this.container = container; | ||
this.injector = injector; | ||
this.clientFactory = clientFactory; | ||
this.metadataExplorer = new listener_metadata_explorer_1.ListenerMetadataExplorer(new metadata_scanner_1.MetadataScanner()); | ||
} | ||
bindPatternHandlers(instance, server, module) { | ||
bindPatternHandlers(instanceWrapper, server, moduleKey) { | ||
const { instance } = instanceWrapper; | ||
const isStatic = instanceWrapper.isDependencyTreeStatic(); | ||
const patternHandlers = this.metadataExplorer.explore(instance); | ||
patternHandlers.forEach(({ pattern, targetCallback }) => { | ||
const proxy = this.contextCreator.create(instance, targetCallback, module); | ||
server.addHandler(pattern, proxy); | ||
const module = this.container.getModuleByKey(moduleKey); | ||
const collection = module.controllers; | ||
patternHandlers.forEach(({ pattern, targetCallback, methodKey }) => { | ||
if (isStatic) { | ||
const proxy = this.contextCreator.create(instance, targetCallback, moduleKey); | ||
return server.addHandler(pattern, proxy); | ||
} | ||
server.addHandler(pattern, data => { | ||
const contextId = context_id_factory_1.createContextId(); | ||
const contextInstance = this.injector.loadPerContext(instance, module, collection, contextId); | ||
const proxy = this.contextCreator.create(contextInstance, contextInstance[methodKey], moduleKey); | ||
return proxy(data); | ||
}); | ||
}); | ||
@@ -21,8 +36,11 @@ } | ||
for (const { property, metadata, } of this.metadataExplorer.scanForClientHooks(instance)) { | ||
const client = client_proxy_factory_1.ClientProxyFactory.create(metadata); | ||
const client = this.clientFactory.create(metadata); | ||
this.clientsContainer.addClient(client); | ||
Reflect.set(instance, property, client); | ||
this.assignClientToInstance(instance, property, client); | ||
} | ||
} | ||
assignClientToInstance(instance, property, client) { | ||
Reflect.set(instance, property, client); | ||
} | ||
} | ||
exports.ListenersController = ListenersController; |
import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface'; | ||
import { InstanceWrapper } from '@nestjs/core/injector/container'; | ||
import { ApplicationConfig } from '@nestjs/core/application-config'; | ||
import { NestContainer } from '@nestjs/core/injector/container'; | ||
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper'; | ||
import { CustomTransportStrategy } from './interfaces'; | ||
@@ -8,5 +10,5 @@ import { Server } from './server/server'; | ||
private listenersController; | ||
register(container: any, config: any): void; | ||
setupListeners(container: any, server: Server & CustomTransportStrategy): void; | ||
setupClients(container: any): void; | ||
register(container: NestContainer, config: ApplicationConfig): void; | ||
setupListeners(container: NestContainer, server: Server & CustomTransportStrategy): void; | ||
setupClients(container: NestContainer): void; | ||
bindListeners(controllers: Map<string, InstanceWrapper<Controller>>, server: Server & CustomTransportStrategy, module: string): void; | ||
@@ -13,0 +15,0 @@ bindClients(items: Map<string, InstanceWrapper<Controller>>): void; |
@@ -6,2 +6,3 @@ "use strict"; | ||
const guards_context_creator_1 = require("@nestjs/core/guards/guards-context-creator"); | ||
const injector_1 = require("@nestjs/core/injector/injector"); | ||
const interceptors_consumer_1 = require("@nestjs/core/interceptors/interceptors-consumer"); | ||
@@ -11,2 +12,3 @@ const interceptors_context_creator_1 = require("@nestjs/core/interceptors/interceptors-context-creator"); | ||
const pipes_context_creator_1 = require("@nestjs/core/pipes/pipes-context-creator"); | ||
const client_1 = require("./client"); | ||
const container_1 = require("./container"); | ||
@@ -23,3 +25,3 @@ const exception_filters_context_1 = require("./context/exception-filters-context"); | ||
const contextCreator = new rpc_context_creator_1.RpcContextCreator(new rpc_proxy_1.RpcProxy(), new exception_filters_context_1.ExceptionFiltersContext(container, config), new pipes_context_creator_1.PipesContextCreator(container, config), new pipes_consumer_1.PipesConsumer(), new guards_context_creator_1.GuardsContextCreator(container, config), new guards_consumer_1.GuardsConsumer(), new interceptors_context_creator_1.InterceptorsContextCreator(container, config), new interceptors_consumer_1.InterceptorsConsumer()); | ||
this.listenersController = new listeners_controller_1.ListenersController(this.clientsContainer, contextCreator); | ||
this.listenersController = new listeners_controller_1.ListenersController(this.clientsContainer, contextCreator, container, new injector_1.Injector(), client_1.ClientProxyFactory); | ||
} | ||
@@ -31,3 +33,3 @@ setupListeners(container, server) { | ||
const modules = container.getModules(); | ||
modules.forEach(({ routes }, module) => this.bindListeners(routes, server, module)); | ||
modules.forEach(({ controllers }, module) => this.bindListeners(controllers, server, module)); | ||
} | ||
@@ -39,9 +41,9 @@ setupClients(container) { | ||
const modules = container.getModules(); | ||
modules.forEach(({ routes, components }) => { | ||
this.bindClients(routes); | ||
this.bindClients(components); | ||
modules.forEach(({ controllers, providers }) => { | ||
this.bindClients(controllers); | ||
this.bindClients(providers); | ||
}); | ||
} | ||
bindListeners(controllers, server, module) { | ||
controllers.forEach(({ instance }) => this.listenersController.bindPatternHandlers(instance, server, module)); | ||
controllers.forEach(wrapper => this.listenersController.bindPatternHandlers(wrapper, server, module)); | ||
} | ||
@@ -48,0 +50,0 @@ bindClients(items) { |
@@ -11,6 +11,5 @@ "use strict"; | ||
const { SocketModule } = optional('@nestjs/websockets/socket-module') || {}; | ||
const { IoAdapter } = optional('@nestjs/websockets/adapters/io-adapter') || {}; | ||
class NestMicroservice extends nest_application_context_1.NestApplicationContext { | ||
constructor(container, config = {}, applicationConfig) { | ||
super(container, [], null); | ||
super(container); | ||
this.applicationConfig = applicationConfig; | ||
@@ -29,2 +28,3 @@ this.logger = new logger_service_1.Logger(NestMicroservice.name, true); | ||
registerWsAdapter() { | ||
const { IoAdapter } = optional('@nestjs/platform-socket.io') || {}; | ||
const ioAdapter = IoAdapter ? new IoAdapter() : null; | ||
@@ -31,0 +31,0 @@ this.applicationConfig.setIoAdapter(ioAdapter); |
{ | ||
"name": "@nestjs/microservices", | ||
"version": "5.5.0", | ||
"version": "6.0.0-alpha.1", | ||
"description": "Nest - modern, fast, powerful node.js web framework (@microservices)", | ||
@@ -5,0 +5,0 @@ "author": "Kamil Mysliwiec", |
@@ -16,4 +16,4 @@ import { CustomTransportStrategy } from '../interfaces'; | ||
createServiceMethod(methodHandler: Function, protoNativeHandler: any): Function; | ||
createUnaryServiceMethod(methodHandler: any): Function; | ||
createStreamServiceMethod(methodHandler: any): Function; | ||
createUnaryServiceMethod(methodHandler: Function): Function; | ||
createStreamServiceMethod(methodHandler: Function): Function; | ||
close(): void; | ||
@@ -20,0 +20,0 @@ deserialize(obj: any): any; |
@@ -6,4 +6,4 @@ "use strict"; | ||
const constants_1 = require("../constants"); | ||
const invalid_grpc_package_exception_1 = require("../exceptions/errors/invalid-grpc-package.exception"); | ||
const invalid_proto_definition_exception_1 = require("../exceptions/errors/invalid-proto-definition.exception"); | ||
const invalid_grpc_package_exception_1 = require("../errors/invalid-grpc-package.exception"); | ||
const invalid_proto_definition_exception_1 = require("../errors/invalid-proto-definition.exception"); | ||
const server_1 = require("./server"); | ||
@@ -18,3 +18,4 @@ let grpcPackage = {}; | ||
this.getOptionsProp(options, 'url') || constants_1.GRPC_DEFAULT_URL; | ||
const protoLoader = this.getOptionsProp(options, 'protoLoader') || constants_1.GRPC_DEFAULT_PROTO_LOADER; | ||
const protoLoader = this.getOptionsProp(options, 'protoLoader') || | ||
constants_1.GRPC_DEFAULT_PROTO_LOADER; | ||
grpcPackage = this.loadPackage('grpc', ServerGrpc.name); | ||
@@ -52,3 +53,3 @@ grpcProtoLoaderPackage = this.loadPackage(protoLoader, ServerGrpc.name); | ||
for (const methodName in grpcService.prototype) { | ||
const methodHandler = this.messageHandlers[this.createPattern(name, methodName)]; | ||
const methodHandler = this.getHandlerByPattern(this.createPattern(name, methodName)); | ||
if (!methodHandler) { | ||
@@ -75,3 +76,3 @@ continue; | ||
const handler = methodHandler(call.request, call.metadata); | ||
this.transformToObservable(await handler).subscribe(data => callback(null, data), err => callback(err)); | ||
this.transformToObservable(await handler).subscribe(data => callback(null, data), (err) => callback(err)); | ||
}; | ||
@@ -78,0 +79,0 @@ } |
@@ -16,3 +16,3 @@ /// <reference types="node" /> | ||
createMqttClient(): MqttClient; | ||
getMessageHandler(pub: MqttClient): any; | ||
getMessageHandler(pub: MqttClient): Function; | ||
handleMessage(channel: string, buffer: Buffer, pub: MqttClient): Promise<any>; | ||
@@ -19,0 +19,0 @@ getPublisher(client: MqttClient, pattern: any, id: string): any; |
@@ -25,3 +25,3 @@ "use strict"; | ||
mqttClient.on(constants_1.MESSAGE_EVENT, this.getMessageHandler(mqttClient).bind(this)); | ||
const registeredPatterns = Object.keys(this.messageHandlers); | ||
const registeredPatterns = [...this.messageHandlers.keys()]; | ||
registeredPatterns.forEach(pattern => mqttClient.subscribe(this.getAckQueueName(pattern))); | ||
@@ -42,7 +42,7 @@ } | ||
const publish = this.getPublisher(pub, pattern, packet.id); | ||
const status = 'error'; | ||
if (!this.messageHandlers[pattern]) { | ||
const handler = this.getHandlerByPattern(pattern); | ||
if (!handler) { | ||
const status = 'error'; | ||
return publish({ id: packet.id, status, err: constants_1.NO_PATTERN_MESSAGE }); | ||
} | ||
const handler = this.messageHandlers[pattern]; | ||
const response$ = this.transformToObservable(await handler(packet.data)); | ||
@@ -52,3 +52,3 @@ response$ && this.send(response$, publish); | ||
getPublisher(client, pattern, id) { | ||
return response => client.publish(this.getResQueueName(pattern), JSON.stringify(Object.assign(response, { id }))); | ||
return (response) => client.publish(this.getResQueueName(pattern), JSON.stringify(Object.assign(response, { id }))); | ||
} | ||
@@ -70,5 +70,5 @@ deserialize(content) { | ||
handleError(stream) { | ||
stream.on(constants_1.ERROR_EVENT, err => this.logger.error(err)); | ||
stream.on(constants_1.ERROR_EVENT, (err) => this.logger.error(err)); | ||
} | ||
} | ||
exports.ServerMqtt = ServerMqtt; |
@@ -16,3 +16,3 @@ import { Client } from '../external/nats-client.interface'; | ||
createNatsClient(): Client; | ||
getMessageHandler(channel: string, client: Client): (buffer: any, replyTo: string) => Promise<void>; | ||
getMessageHandler(channel: string, client: Client): Function; | ||
handleMessage(channel: string, message: ReadPacket & PacketId, client: Client, replyTo: string): Promise<void>; | ||
@@ -19,0 +19,0 @@ getPublisher(publisher: Client, replyTo: string, id: string): (response: any) => void; |
@@ -25,9 +25,6 @@ "use strict"; | ||
const queue = this.getOptionsProp(this.options, 'queue'); | ||
const subscribe = (channel) => { | ||
if (queue) { | ||
return client.subscribe(channel, { queue }, this.getMessageHandler(channel, client).bind(this)); | ||
} | ||
client.subscribe(channel, this.getMessageHandler(channel, client).bind(this)); | ||
}; | ||
const registeredPatterns = Object.keys(this.messageHandlers); | ||
const subscribe = queue | ||
? (channel) => client.subscribe(channel, { queue }, this.getMessageHandler(channel, client).bind(this)) | ||
: (channel) => client.subscribe(channel, this.getMessageHandler(channel, client).bind(this)); | ||
const registeredPatterns = [...this.messageHandlers.keys()]; | ||
registeredPatterns.forEach(channel => subscribe(channel)); | ||
@@ -48,7 +45,7 @@ } | ||
const publish = this.getPublisher(client, replyTo, message.id); | ||
const status = 'error'; | ||
if (!this.messageHandlers[channel]) { | ||
const handler = this.getHandlerByPattern(channel); | ||
if (!handler) { | ||
const status = 'error'; | ||
return publish({ id: message.id, status, err: constants_1.NO_PATTERN_MESSAGE }); | ||
} | ||
const handler = this.messageHandlers[channel]; | ||
const response$ = this.transformToObservable(await handler(message.data)); | ||
@@ -58,3 +55,3 @@ response$ && this.send(response$, publish); | ||
getPublisher(publisher, replyTo, id) { | ||
return response => publisher.publish(replyTo, Object.assign(response, { | ||
return (response) => publisher.publish(replyTo, Object.assign(response, { | ||
id, | ||
@@ -64,5 +61,5 @@ })); | ||
handleError(stream) { | ||
stream.on(constants_1.ERROR_EVENT, err => this.logger.error(err)); | ||
stream.on(constants_1.ERROR_EVENT, (err) => this.logger.error(err)); | ||
} | ||
} | ||
exports.ServerNats = ServerNats; |
@@ -17,4 +17,4 @@ import { ClientOpts, RedisClient, RetryStrategyOptions } from '../external/redis.interface'; | ||
createRedisClient(): RedisClient; | ||
getMessageHandler(pub: RedisClient): (channel: any, buffer: any) => Promise<boolean>; | ||
handleMessage(channel: any, buffer: string | any, pub: RedisClient): Promise<boolean>; | ||
getMessageHandler(pub: RedisClient): (channel: string, buffer: any) => Promise<boolean>; | ||
handleMessage(channel: string, buffer: string | any, pub: RedisClient): Promise<boolean>; | ||
getPublisher(pub: RedisClient, pattern: any, id: string): (response: any) => boolean; | ||
@@ -21,0 +21,0 @@ deserialize(content: any): ReadPacket & PacketId; |
@@ -29,3 +29,3 @@ "use strict"; | ||
subClient.on(constants_1.MESSAGE_EVENT, this.getMessageHandler(pubClient).bind(this)); | ||
const subscribePatterns = Object.keys(this.messageHandlers); | ||
const subscribePatterns = [...this.messageHandlers.keys()]; | ||
subscribePatterns.forEach(pattern => subClient.subscribe(this.getAckQueueName(pattern))); | ||
@@ -48,7 +48,7 @@ } | ||
const publish = this.getPublisher(pub, pattern, packet.id); | ||
const status = 'error'; | ||
if (!this.messageHandlers[pattern]) { | ||
const handler = this.getHandlerByPattern(pattern); | ||
if (!handler) { | ||
const status = 'error'; | ||
return publish({ id: packet.id, status, err: constants_1.NO_PATTERN_MESSAGE }); | ||
} | ||
const handler = this.messageHandlers[pattern]; | ||
const response$ = this.transformToObservable(await handler(packet.data)); | ||
@@ -58,3 +58,3 @@ response$ && this.send(response$, publish); | ||
getPublisher(pub, pattern, id) { | ||
return response => pub.publish(this.getResQueueName(pattern), JSON.stringify(Object.assign(response, { id }))); | ||
return (response) => pub.publish(this.getResQueueName(pattern), JSON.stringify(Object.assign(response, { id }))); | ||
} | ||
@@ -76,6 +76,6 @@ deserialize(content) { | ||
handleError(stream) { | ||
stream.on(constants_1.ERROR_EVENT, err => this.logger.error(err)); | ||
stream.on(constants_1.ERROR_EVENT, (err) => this.logger.error(err)); | ||
} | ||
getClientOptions() { | ||
const retry_strategy = options => this.createRetryStrategy(options); | ||
const retry_strategy = (options) => this.createRetryStrategy(options); | ||
return { | ||
@@ -82,0 +82,0 @@ retry_strategy, |
@@ -40,9 +40,9 @@ "use strict"; | ||
this.server = this.createClient(); | ||
this.server.on(constants_1.CONNECT_EVENT, _ => { | ||
this.server.on(constants_1.CONNECT_EVENT, (_) => { | ||
this.channel = this.server.createChannel({ | ||
json: false, | ||
setup: channel => this.setupChannel(channel, callback), | ||
setup: (channel) => this.setupChannel(channel, callback), | ||
}); | ||
}); | ||
this.server.on(constants_1.DISCONNECT_EVENT, err => { | ||
this.server.on(constants_1.DISCONNECT_EVENT, (err) => { | ||
this.logger.error(constants_1.DISCONNECTED_RMQ_MESSAGE); | ||
@@ -57,3 +57,3 @@ }); | ||
await channel.prefetch(this.prefetchCount, this.isGlobalPrefetchCount); | ||
channel.consume(this.queue, msg => this.handleMessage(msg), { | ||
channel.consume(this.queue, (msg) => this.handleMessage(msg), { | ||
noAck: true, | ||
@@ -73,3 +73,3 @@ }); | ||
const response$ = this.transformToObservable(await handler(packet.data)); | ||
const publish = data => this.sendMessage(data, properties.replyTo, properties.correlationId); | ||
const publish = (data) => this.sendMessage(data, properties.replyTo, properties.correlationId); | ||
response$ && this.send(response$, publish); | ||
@@ -76,0 +76,0 @@ } |
@@ -14,7 +14,7 @@ /// <reference types="node" /> | ||
close(): void; | ||
bindHandler(socket: any): void; | ||
handleMessage(socket: any, packet: ReadPacket & PacketId): Promise<any>; | ||
bindHandler<T extends Record<string, any>>(socket: T): void; | ||
handleMessage<T extends Record<string, any>>(socket: T, packet: ReadPacket & PacketId): Promise<any>; | ||
handleClose(): undefined | number | NodeJS.Timer; | ||
private init(); | ||
private getSocketInstance(socket); | ||
private init; | ||
private getSocketInstance; | ||
} |
@@ -33,4 +33,5 @@ "use strict"; | ||
: packet.pattern; | ||
const status = 'error'; | ||
if (!this.messageHandlers[pattern]) { | ||
const handler = this.getHandlerByPattern(pattern); | ||
if (!handler) { | ||
const status = 'error'; | ||
return socket.sendMessage({ | ||
@@ -42,3 +43,2 @@ id: packet.id, | ||
} | ||
const handler = this.messageHandlers[pattern]; | ||
const response$ = this.transformToObservable(await handler(packet.data)); | ||
@@ -45,0 +45,0 @@ response$ && |
import { Logger } from '@nestjs/common/services/logger.service'; | ||
import { Observable, Subscription } from 'rxjs'; | ||
import { MicroserviceOptions, WritePacket } from '../interfaces'; | ||
import { MessageHandlers } from '../interfaces/message-handlers.interface'; | ||
import { MessageHandler, MicroserviceOptions, WritePacket } from '../interfaces'; | ||
export declare abstract class Server { | ||
protected readonly messageHandlers: MessageHandlers; | ||
protected readonly messageHandlers: Map<string, MessageHandler<any, any>>; | ||
protected readonly logger: Logger; | ||
addHandler(pattern: any, callback: (data) => Promise<Observable<any>>): void; | ||
getHandlers(): MessageHandlers; | ||
getHandlerByPattern(pattern: string): (data) => Promise<Observable<any>> | null; | ||
addHandler(pattern: any, callback: <T>(data: T) => Promise<Observable<any>>): void; | ||
getHandlers(): Map<string, MessageHandler>; | ||
getHandlerByPattern(pattern: string): MessageHandler | null; | ||
send(stream$: Observable<any>, respond: (data: WritePacket) => void): Subscription; | ||
transformToObservable<T = any>(resultOrDeffered: any): Observable<T>; | ||
getOptionsProp<T extends { | ||
options?; | ||
options?: any; | ||
}>(obj: MicroserviceOptions['options'], prop: keyof T['options'], defaultValue?: any): any; | ||
protected handleError(error: string): void; | ||
protected loadPackage(name: string, ctx: string): any; | ||
protected loadPackage<T = any>(name: string, ctx: string): T; | ||
} |
@@ -10,3 +10,3 @@ "use strict"; | ||
constructor() { | ||
this.messageHandlers = {}; | ||
this.messageHandlers = new Map(); | ||
this.logger = new logger_service_1.Logger(Server.name); | ||
@@ -16,3 +16,3 @@ } | ||
const key = shared_utils_1.isString(pattern) ? pattern : JSON.stringify(pattern); | ||
this.messageHandlers[key] = callback; | ||
this.messageHandlers.set(key, callback); | ||
} | ||
@@ -23,11 +23,13 @@ getHandlers() { | ||
getHandlerByPattern(pattern) { | ||
return this.messageHandlers[pattern] ? this.messageHandlers[pattern] : null; | ||
return this.messageHandlers.has(pattern) | ||
? this.messageHandlers.get(pattern) | ||
: null; | ||
} | ||
send(stream$, respond) { | ||
return stream$ | ||
.pipe(operators_1.catchError(err => { | ||
.pipe(operators_1.catchError((err) => { | ||
respond({ err, response: null }); | ||
return rxjs_1.EMPTY; | ||
}), operators_1.finalize(() => respond({ isDisposed: true }))) | ||
.subscribe(response => respond({ err: null, response })); | ||
.subscribe((response) => respond({ err: null, response })); | ||
} | ||
@@ -34,0 +36,0 @@ transformToObservable(resultOrDeffered) { |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
142760
117
3212
2