Skip to content

Commit

Permalink
Packages codependency refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 1, 2017
1 parent ed8d2e6 commit 2a18249
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 41 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"iterare": "0.0.8",
"json-socket": "^0.2.1",
"opencollective": "^1.0.3",
"optional": "^0.1.4",
"redis": "^2.7.1",
"reflect-metadata": "^0.1.10",
"rxjs": "^5.4.2",
Expand Down
3 changes: 2 additions & 1 deletion src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export const FILTER_CATCH_EXCEPTIONS = '__filterCatchExceptions__';
export const PIPES_METADATA = '__pipes__';
export const GUARDS_METADATA = '__guards__';
export const INTERCEPTORS_METADATA = '__interceptors__';
export const HTTP_CODE_METADATA = '__httpCode__';
export const HTTP_CODE_METADATA = '__httpCode__';
export const GATEWAY_MIDDLEWARES = '__gatewayMiddlewares';
4 changes: 4 additions & 0 deletions src/common/enums/transport.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Transport {
TCP = 0,
REDIS = 1,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface CustomTransportStrategy {
listen(callback: () => void): any;
close(): any;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { CustomTransportStrategy } from './custom-transport-strategy.interface';
import { Transport } from './../../enums/transport.enum';

export interface MicroserviceConfiguration {
transport?: Transport;
url?: string;
port?: number;
host?: string;
strategy?: CustomTransportStrategy;
}
6 changes: 4 additions & 2 deletions src/core/application-config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { IoAdapter } from '@nestjs/websockets/adapters/io-adapter';
import * as optional from 'optional';
import { PipeTransform, WebSocketAdapter, ExceptionFilter, NestInterceptor, CanActivate } from '@nestjs/common';
import { ConfigurationProvider } from '@nestjs/common/interfaces/configuration-provider.interface';

const { IoAdapter } = optional('@nestjs/websockets/adapters/io-adapter') || {} as any;

export class ApplicationConfig implements ConfigurationProvider {
private globalPipes: PipeTransform<any>[] = [];
private globalFilters: ExceptionFilter[] = [];
private globalInterceptors: NestInterceptor[] = [];
private globalGuards: CanActivate[] = [];
private ioAdapter: WebSocketAdapter = new IoAdapter();
private ioAdapter: WebSocketAdapter = IoAdapter ? new IoAdapter() : null;
private globalPrefix = '';

public setGlobalPrefix(prefix: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RuntimeException } from './runtime.exception';
import { MICROSERVICES_PACKAGE_NOT_FOUND_EXCEPTION } from '../messages';

export class MicroservicesPackageNotFoundException extends RuntimeException {
constructor() {
super(MICROSERVICES_PACKAGE_NOT_FOUND_EXCEPTION);
}
}
3 changes: 2 additions & 1 deletion src/core/errors/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export const UnknownExportMessage = (name: string) =>
export const INVALID_MIDDLEWARE_CONFIGURATION = `Invalid middleware configuration passed inside the module 'configure()' method.`;
export const UNKNOWN_REQUEST_MAPPING = `Request mapping properties not defined in the @RequestMapping() annotation!`;
export const UNHANDLED_RUNTIME_EXCEPTION = `Unhandled Runtime Exception.`;
export const INVALID_EXCEPTION_FILTER = `Invalid exception filters (@ExceptionFilters()).`;
export const INVALID_EXCEPTION_FILTER = `Invalid exception filters (@ExceptionFilters()).`;
export const MICROSERVICES_PACKAGE_NOT_FOUND_EXCEPTION = `Unable to load @nestjs/microservices packages (please, make sure whether it's installed already).`
3 changes: 1 addition & 2 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ export { MiddlewareBuilder } from './middlewares/builder';
export { ModuleRef } from './injector/module-ref';
export * from './services/reflector.service';
export * from './nest-factory';
export * from './nest-application';
export * from './nest-microservice';
export * from './nest-application';
32 changes: 21 additions & 11 deletions src/core/nest-application.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import * as optional from 'optional';
import iterate from 'iterare';
import {
CanActivate,
ExceptionFilter,
NestInterceptor,
OnModuleDestroy,
PipeTransform,
WebSocketAdapter
WebSocketAdapter,
} from '@nestjs/common';
import { INestApplication, INestMicroservice, OnModuleInit } from '@nestjs/common';
import { Logger } from '@nestjs/common/services/logger.service';
import { isNil, isUndefined, validatePath } from '@nestjs/common/utils/shared.utils';
import { MicroserviceConfiguration } from '@nestjs/microservices';
import { MicroservicesModule } from '@nestjs/microservices/microservices-module';
import { SocketModule } from '@nestjs/websockets/socket-module';
import iterate from 'iterare';
import { MicroserviceConfiguration } from '@nestjs/common/interfaces/microservices/microservice-configuration.interface';
import { ExpressAdapter } from './adapters/express-adapter';
import { ApplicationConfig } from './application-config';
import { messages } from './constants';
import { NestMicroservice } from './index';
import { NestContainer } from './injector/container';
import { Module } from './injector/module';
import { MiddlewaresModule } from './middlewares/middlewares-module';
import { Resolver } from './router/interfaces/resolver.interface';
import { RoutesResolver } from './router/routes-resolver';
import { MicroservicesPackageNotFoundException } from './errors/exceptions/microservices-package-not-found.exception';

const { SocketModule } = optional('@nestjs/websockets/socket-module') || {} as any;
const { MicroservicesModule } = optional('@nestjs/microservices/microservices-module') || {} as any;
const { NestMicroservice } = optional('@nestjs/microservices/nest-microservice') || {} as any;

export class NestApplication implements INestApplication {
private readonly config = new ApplicationConfig();
Expand All @@ -41,9 +44,12 @@ export class NestApplication implements INestApplication {
}

public async setupModules() {
SocketModule.setup(this.container, this.config);
MicroservicesModule.setup(this.container, this.config);
MicroservicesModule.setupClients(this.container);
SocketModule && SocketModule.setup(this.container, this.config);

if (MicroservicesModule) {
MicroservicesModule.setup(this.container, this.config);
MicroservicesModule.setupClients(this.container);
}
await MiddlewaresModule.setup(this.container, this.config);
}

Expand All @@ -65,7 +71,11 @@ export class NestApplication implements INestApplication {
}

public connectMicroservice(config: MicroserviceConfiguration): INestMicroservice {
const instance = new NestMicroservice(this.container, config);
if (!NestMicroservice) {
throw new MicroservicesPackageNotFoundException();
}

const instance = new NestMicroservice(this.container as any, config as any);
instance.setupListeners();
instance.setIsInitialized(true);
instance.setIsInitHookCalled(true);
Expand Down Expand Up @@ -108,7 +118,7 @@ export class NestApplication implements INestApplication {
}

public close() {
SocketModule.close();
SocketModule && SocketModule.close();
this.server && this.server.close();
this.microservices.forEach((microservice) => {
microservice.setIsTerminated(true);
Expand Down
15 changes: 11 additions & 4 deletions src/core/nest-factory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as optional from 'optional';
import { DependenciesScanner } from './scanner';
import { InstanceLoader } from './injector/instance-loader';
import { NestContainer } from './injector/container';
Expand All @@ -6,12 +7,14 @@ import { NestModuleMetatype } from '@nestjs/common/interfaces/modules/module-met
import { Logger } from '@nestjs/common/services/logger.service';
import { messages } from './constants';
import { NestApplication } from './nest-application';
import { NestMicroservice } from './nest-microservice';
import { isFunction } from '@nestjs/common/utils/shared.utils';
import { MicroserviceConfiguration } from '@nestjs/microservices/interfaces/microservice-configuration.interface';
import { MicroserviceConfiguration } from '@nestjs/common/interfaces/microservices/microservice-configuration.interface';
import { ExpressAdapter } from './adapters/express-adapter';
import { INestApplication, INestMicroservice } from '@nestjs/common';
import { MetadataScanner } from './metadata-scanner';
import { MicroservicesPackageNotFoundException } from './errors/exceptions/microservices-package-not-found.exception';

const { NestMicroservice } = optional('@nestjs/microservices/nest-microservice') || {} as any;

export class NestFactoryStatic {
private container = new NestContainer();
Expand Down Expand Up @@ -46,9 +49,13 @@ export class NestFactoryStatic {
module,
config?: MicroserviceConfiguration): Promise<INestMicroservice> {

if (!NestMicroservice) {
throw new MicroservicesPackageNotFoundException();
}

await this.initialize(module);
return this.createNestInstance<NestMicroservice>(
new NestMicroservice(this.container, config),
return this.createNestInstance<INestMicroservice>(
new NestMicroservice(this.container, config as any),
);
}

Expand Down
2 changes: 0 additions & 2 deletions src/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
},
"peerDependencies": {
"@nestjs/common": "^4.*",
"@nestjs/websockets": "^4.*",
"@nestjs/microservices": "^4.*",
"reflect-metadata": "0.1.10",
"rxjs": "^5.4.2"
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import 'reflect-metadata';
import { NestContainer } from './injector/container';
import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface';
import { Injectable } from '@nestjs/common/interfaces/injectable.interface';
import { metadata, EXCEPTION_FILTERS_METADATA, GUARDS_METADATA, INTERCEPTORS_METADATA } from '@nestjs/common/constants';
import { metadata, GATEWAY_MIDDLEWARES, EXCEPTION_FILTERS_METADATA, GUARDS_METADATA, INTERCEPTORS_METADATA } from '@nestjs/common/constants';
import { NestModuleMetatype } from '@nestjs/common/interfaces/modules/module-metatype.interface';
import { Metatype } from '@nestjs/common/interfaces/metatype.interface';
import { GATEWAY_MIDDLEWARES } from '@nestjs/websockets/constants';
import { MetadataScanner } from '../core/metadata-scanner';

export class DependenciesScanner {
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@
export * from './common';
export * from './core';
export * from './core/nest-factory';
export * from './core/nest-application';
export * from './core/nest-microservice';
export * from './core/nest-application';
3 changes: 2 additions & 1 deletion src/microservices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export * from './interfaces';
export * from './client';
export * from './enums';
export * from './server';
export * from './exceptions';
export * from './exceptions';
export * from './nest-microservice';
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import * as optional from 'optional';
import iterate from 'iterare';
import { NestContainer } from './injector/container';
import { MicroservicesModule } from '@nestjs/microservices/microservices-module';
import { messages } from './constants';
import { NestContainer } from '@nestjs/core/injector/container';
import { MicroservicesModule } from './microservices-module';
import { messages } from '@nestjs/core/constants';
import { Logger } from '@nestjs/common/services/logger.service';
import { Server } from '@nestjs/microservices/server/server';
import { MicroserviceConfiguration } from '@nestjs/microservices/interfaces/microservice-configuration.interface';
import { ServerFactory } from '@nestjs/microservices/server/server-factory';
import { Transport } from '@nestjs/microservices/enums/transport.enum';
import { Server } from './server/server';
import { MicroserviceConfiguration } from './interfaces/microservice-configuration.interface';
import { ServerFactory } from './server/server-factory';
import { Transport } from './enums/transport.enum';
import { INestMicroservice, WebSocketAdapter, CanActivate, PipeTransform, NestInterceptor, ExceptionFilter, OnModuleInit } from '@nestjs/common';
import { ApplicationConfig } from './application-config';
import { SocketModule } from '@nestjs/websockets/socket-module';
import { ApplicationConfig } from '@nestjs/core/application-config';
import { CustomTransportStrategy } from '@nestjs/microservices';
import { Module } from './injector/module';
import { Module } from '@nestjs/core/injector/module';
import { isNil, isUndefined } from '@nestjs/common/utils/shared.utils';
import { OnModuleDestroy } from '@nestjs/common/interfaces';

const { SocketModule } = optional('@nestjs/websockets/socket-module') || {} as any;

export class NestMicroservice implements INestMicroservice {
private readonly config = new ApplicationConfig();
private readonly logger = new Logger(NestMicroservice.name, true);
Expand All @@ -38,7 +40,7 @@ export class NestMicroservice implements INestMicroservice {
}

public setupModules() {
SocketModule.setup(this.container, this.config);
SocketModule && SocketModule.setup(this.container, this.config);
MicroservicesModule.setupClients(this.container);

this.setupListeners();
Expand Down Expand Up @@ -96,7 +98,7 @@ export class NestMicroservice implements INestMicroservice {
}

private closeApplication() {
SocketModule.close();
SocketModule && SocketModule.close();

this.callDestroyHook();
this.setIsTerminated(true);
Expand Down
2 changes: 1 addition & 1 deletion src/websockets/adapters/io-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as io from 'socket.io';
import { MessageMappingProperties } from '../gateway-metadata-explorer';
import { CONNECTION_EVENT, DISCONNECT_EVENT } from './../constants';
import { CONNECTION_EVENT, DISCONNECT_EVENT } from '../constants';
import { WebSocketAdapter } from '@nestjs/common';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromEvent';
Expand Down

0 comments on commit 2a18249

Please sign in to comment.