@nestjs/microservices
Advanced tools
Comparing version 3.0.1 to 3.0.2
@@ -6,1 +6,2 @@ export * from './utils'; | ||
export * from './server'; | ||
export * from './exceptions'; |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
/* | ||
@@ -7,3 +8,2 @@ * Nest @microservices | ||
*/ | ||
"use strict"; | ||
function __export(m) { | ||
@@ -17,1 +17,2 @@ for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
__export(require("./server")); | ||
__export(require("./exceptions")); |
import { Observable } from 'rxjs/Observable'; | ||
export interface MessageHandlers { | ||
[pattern: string]: (data) => Observable<any>; | ||
[pattern: string]: (data) => Promise<Observable<any>>; | ||
} |
@@ -14,10 +14,10 @@ "use strict"; | ||
exploreMethodMetadata(instance, instancePrototype, methodName) { | ||
const callbackMethod = instancePrototype[methodName]; | ||
const isPattern = Reflect.getMetadata(constants_1.PATTERN_HANDLER_METADATA, callbackMethod); | ||
const targetCallback = instancePrototype[methodName]; | ||
const isPattern = Reflect.getMetadata(constants_1.PATTERN_HANDLER_METADATA, targetCallback); | ||
if (shared_utils_1.isUndefined(isPattern)) { | ||
return null; | ||
} | ||
const pattern = Reflect.getMetadata(constants_1.PATTERN_METADATA, callbackMethod); | ||
const pattern = Reflect.getMetadata(constants_1.PATTERN_METADATA, targetCallback); | ||
return { | ||
targetCallback: callbackMethod.bind(instance), | ||
targetCallback, | ||
pattern, | ||
@@ -24,0 +24,0 @@ }; |
@@ -6,8 +6,10 @@ import 'reflect-metadata'; | ||
import { ClientsContainer } from './container'; | ||
import { RpcContextCreator } from './context/rpc-context-creator'; | ||
export declare class ListenersController { | ||
private readonly clientsContainer; | ||
private readonly contextCreator; | ||
private readonly metadataExplorer; | ||
constructor(clientsContainer: ClientsContainer); | ||
bindPatternHandlers(instance: Controller, server: Server & CustomTransportStrategy): void; | ||
constructor(clientsContainer: ClientsContainer, contextCreator: RpcContextCreator); | ||
bindPatternHandlers(instance: Controller, server: Server & CustomTransportStrategy, module: string): void; | ||
bindClientsToProperties(instance: Controller): void; | ||
} |
@@ -8,9 +8,13 @@ "use strict"; | ||
class ListenersController { | ||
constructor(clientsContainer) { | ||
constructor(clientsContainer, contextCreator) { | ||
this.clientsContainer = clientsContainer; | ||
this.contextCreator = contextCreator; | ||
this.metadataExplorer = new listener_metadata_explorer_1.ListenerMetadataExplorer(new metadata_scanner_1.MetadataScanner()); | ||
} | ||
bindPatternHandlers(instance, server) { | ||
bindPatternHandlers(instance, server, module) { | ||
const patternHandlers = this.metadataExplorer.explore(instance); | ||
patternHandlers.forEach(({ pattern, targetCallback }) => server.add(pattern, targetCallback)); | ||
patternHandlers.forEach(({ pattern, targetCallback }) => { | ||
const proxy = this.contextCreator.create(instance, targetCallback, module); | ||
server.add(pattern, proxy); | ||
}); | ||
} | ||
@@ -17,0 +21,0 @@ bindClientsToProperties(instance) { |
@@ -7,8 +7,9 @@ import { InstanceWrapper } from '@nestjs/core/injector/container'; | ||
private static readonly clientsContainer; | ||
private static readonly listenersController; | ||
private static listenersController; | ||
static setup(container: any): void; | ||
static setupListeners(container: any, server: Server & CustomTransportStrategy): void; | ||
static setupClients(container: any): void; | ||
static bindListeners(controllers: Map<string, InstanceWrapper<Controller>>, server: Server & CustomTransportStrategy): void; | ||
static bindListeners(controllers: Map<string, InstanceWrapper<Controller>>, server: Server & CustomTransportStrategy, module: string): void; | ||
static bindClients(controllers: Map<string, InstanceWrapper<Controller>>): void; | ||
static close(): void; | ||
} |
@@ -5,8 +5,25 @@ "use strict"; | ||
const container_1 = require("./container"); | ||
const rpc_context_creator_1 = require("./context/rpc-context-creator"); | ||
const rpc_proxy_1 = require("./context/rpc-proxy"); | ||
const exception_filters_context_1 = require("./context/exception-filters-context"); | ||
const pipes_context_creator_1 = require("@nestjs/core/pipes/pipes-context-creator"); | ||
const pipes_consumer_1 = require("@nestjs/core/pipes/pipes-consumer"); | ||
const guards_context_creator_1 = require("@nestjs/core/guards/guards-context-creator"); | ||
const runtime_exception_1 = require("@nestjs/core/errors/exceptions/runtime.exception"); | ||
const guards_consumer_1 = require("@nestjs/core/guards/guards-consumer"); | ||
class MicroservicesModule { | ||
static setup(container) { | ||
this.listenersController = new listeners_controller_1.ListenersController(MicroservicesModule.clientsContainer, new rpc_context_creator_1.RpcContextCreator(new rpc_proxy_1.RpcProxy(), new exception_filters_context_1.ExceptionFiltersContext(), new pipes_context_creator_1.PipesContextCreator(), new pipes_consumer_1.PipesConsumer(), new guards_context_creator_1.GuardsContextCreator(container), new guards_consumer_1.GuardsConsumer())); | ||
} | ||
static setupListeners(container, server) { | ||
if (!this.listenersController) { | ||
throw new runtime_exception_1.RuntimeException(); | ||
} | ||
const modules = container.getModules(); | ||
modules.forEach(({ routes }) => this.bindListeners(routes, server)); | ||
modules.forEach(({ routes }, module) => this.bindListeners(routes, server, module)); | ||
} | ||
static setupClients(container) { | ||
if (!this.listenersController) { | ||
throw new runtime_exception_1.RuntimeException(); | ||
} | ||
const modules = container.getModules(); | ||
@@ -18,5 +35,5 @@ modules.forEach(({ routes, components }) => { | ||
} | ||
static bindListeners(controllers, server) { | ||
static bindListeners(controllers, server, module) { | ||
controllers.forEach(({ instance }) => { | ||
this.listenersController.bindPatternHandlers(instance, server); | ||
this.listenersController.bindPatternHandlers(instance, server, module); | ||
}); | ||
@@ -36,3 +53,2 @@ } | ||
MicroservicesModule.clientsContainer = new container_1.ClientsContainer(); | ||
MicroservicesModule.listenersController = new listeners_controller_1.ListenersController(MicroservicesModule.clientsContainer); | ||
exports.MicroservicesModule = MicroservicesModule; |
{ | ||
"name": "@nestjs/microservices", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "Nest - modern, fast, powerful node.js web framework (@microservices)", | ||
@@ -16,4 +16,4 @@ "author": "Kamil Mysliwiec", | ||
"reflect-metadata": "0.1.10", | ||
"rxjs": "5.0.3" | ||
"rxjs": "5.4.2" | ||
} | ||
} |
@@ -18,4 +18,4 @@ import * as redis from 'redis'; | ||
handleConnection(callback: any, sub: any, pub: any): void; | ||
getMessageHandler(pub: any): (channel: any, buffer: any) => void; | ||
handleMessage(channel: any, buffer: any, pub: any): void; | ||
getMessageHandler(pub: any): (channel: any, buffer: any) => Promise<void>; | ||
handleMessage(channel: any, buffer: any, pub: any): Promise<void>; | ||
getPublisher(pub: any, pattern: any): (respond: any) => void; | ||
@@ -22,0 +22,0 @@ tryParse(content: any): any; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -44,15 +52,18 @@ const redis = require("redis"); | ||
getMessageHandler(pub) { | ||
return (channel, buffer) => this.handleMessage(channel, buffer, pub); | ||
return (channel, buffer) => __awaiter(this, void 0, void 0, function* () { return yield this.handleMessage(channel, buffer, pub); }); | ||
} | ||
handleMessage(channel, buffer, pub) { | ||
const msg = this.tryParse(buffer); | ||
const pattern = channel.replace(/_ack$/, ''); | ||
const publish = this.getPublisher(pub, pattern); | ||
if (!this.messageHandlers[pattern]) { | ||
publish({ err: constants_1.NO_PATTERN_MESSAGE }); | ||
return; | ||
} | ||
const handler = this.messageHandlers[pattern]; | ||
const response$ = handler(msg.data); | ||
response$ && this.send(response$, publish); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const msg = this.tryParse(buffer); | ||
const pattern = channel.replace(/_ack$/, ''); | ||
const publish = this.getPublisher(pub, pattern); | ||
const status = 'error'; | ||
if (!this.messageHandlers[pattern]) { | ||
publish({ status, error: constants_1.NO_PATTERN_MESSAGE }); | ||
return; | ||
} | ||
const handler = this.messageHandlers[pattern]; | ||
const response$ = yield handler(msg.data); | ||
response$ && this.send(response$, publish); | ||
}); | ||
} | ||
@@ -59,0 +70,0 @@ getPublisher(pub, pattern) { |
@@ -16,5 +16,5 @@ import { Server } from './server'; | ||
data: {}; | ||
}): void; | ||
}): Promise<void>; | ||
private init(); | ||
private getSocketInstance(socket); | ||
} |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -27,13 +35,16 @@ const net = require("net"); | ||
const sock = this.getSocketInstance(socket); | ||
sock.on(MESSAGE_EVENT, msg => this.handleMessage(sock, msg)); | ||
sock.on(MESSAGE_EVENT, (msg) => __awaiter(this, void 0, void 0, function* () { return yield this.handleMessage(sock, msg); })); | ||
} | ||
handleMessage(socket, msg) { | ||
const pattern = JSON.stringify(msg.pattern); | ||
if (!this.messageHandlers[pattern]) { | ||
socket.sendMessage({ err: constants_1.NO_PATTERN_MESSAGE }); | ||
return; | ||
} | ||
const handler = this.messageHandlers[pattern]; | ||
const response$ = handler(msg.data); | ||
response$ && this.send(response$, socket.sendMessage.bind(socket)); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const pattern = JSON.stringify(msg.pattern); | ||
const status = 'error'; | ||
if (!this.messageHandlers[pattern]) { | ||
socket.sendMessage({ status, error: constants_1.NO_PATTERN_MESSAGE }); | ||
return; | ||
} | ||
const handler = this.messageHandlers[pattern]; | ||
const response$ = yield handler(msg.data); | ||
response$ && this.send(response$, socket.sendMessage.bind(socket)); | ||
}); | ||
} | ||
@@ -40,0 +51,0 @@ init() { |
@@ -5,2 +5,6 @@ import { Logger } from '@nestjs/common/services/logger.service'; | ||
import { MicroserviceResponse } from '../index'; | ||
import { Subscription } from 'rxjs/Subscription'; | ||
import 'rxjs/add/operator/catch'; | ||
import 'rxjs/add/operator/finally'; | ||
import 'rxjs/add/observable/empty'; | ||
export declare abstract class Server { | ||
@@ -10,5 +14,5 @@ protected readonly messageHandlers: MessageHandlers; | ||
getHandlers(): MessageHandlers; | ||
add(pattern: any, callback: (data) => Observable<any>): void; | ||
send(stream$: Observable<any>, respond: (data: MicroserviceResponse) => void): void; | ||
add(pattern: any, callback: (data) => Promise<Observable<any>>): void; | ||
send(stream$: Observable<any>, respond: (data: MicroserviceResponse) => void): Subscription; | ||
protected handleError(error: string): void; | ||
} |
@@ -5,2 +5,5 @@ "use strict"; | ||
const Observable_1 = require("rxjs/Observable"); | ||
require("rxjs/add/operator/catch"); | ||
require("rxjs/add/operator/finally"); | ||
require("rxjs/add/observable/empty"); | ||
class Server { | ||
@@ -18,3 +21,3 @@ constructor() { | ||
send(stream$, respond) { | ||
stream$.catch((err) => { | ||
return stream$.catch((err) => { | ||
respond({ err, response: null }); | ||
@@ -21,0 +24,0 @@ return Observable_1.Observable.empty(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
93410
154
2057
4