mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
update(@nestjs) update to 4.4.0
This commit is contained in:
62
gulpfile.js
62
gulpfile.js
@@ -1,35 +1,59 @@
|
|||||||
const gulp = require('gulp');
|
const gulp = require('gulp');
|
||||||
const ts = require('gulp-typescript');
|
const ts = require('gulp-typescript');
|
||||||
const gulpSequence = require('gulp-sequence')
|
const gulpSequence = require('gulp-sequence');
|
||||||
|
|
||||||
const packages = {
|
const packages = {
|
||||||
common: ts.createProject('src/common/tsconfig.json'),
|
common: ts.createProject('src/common/tsconfig.json'),
|
||||||
core: ts.createProject('src/core/tsconfig.json'),
|
core: ts.createProject('src/core/tsconfig.json'),
|
||||||
microservices: ts.createProject('src/microservices/tsconfig.json'),
|
microservices: ts.createProject('src/microservices/tsconfig.json'),
|
||||||
websockets: ts.createProject('src/websockets/tsconfig.json'),
|
websockets: ts.createProject('src/websockets/tsconfig.json'),
|
||||||
testing: ts.createProject('src/testing/tsconfig.json')
|
testing: ts.createProject('src/testing/tsconfig.json'),
|
||||||
};
|
};
|
||||||
const modules = Object.keys(packages);
|
const modules = Object.keys(packages);
|
||||||
const source = 'src';
|
const source = 'src';
|
||||||
const distId = process.argv.indexOf('--dist');
|
const distId = process.argv.indexOf('--dist');
|
||||||
const dist = distId < 0 ? 'node_modules/@nestjs' : process.argv[distId + 1];
|
const dist = distId < 0 ? 'node_modules/@nestjs' : process.argv[distId + 1];
|
||||||
|
|
||||||
gulp.task('default', function () {
|
gulp.task('default', function() {
|
||||||
modules.forEach((module) => {
|
modules.forEach(module => {
|
||||||
gulp.watch([`${source}/${module}/**/*.ts`, `${source}/${module}/*.ts`], [module]);
|
gulp.watch(
|
||||||
});
|
[`${source}/${module}/**/*.ts`, `${source}/${module}/*.ts`],
|
||||||
|
[module]
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
modules.forEach((module) => {
|
modules.forEach(module => {
|
||||||
gulp.task(module, () => {
|
gulp.task(module, () => {
|
||||||
return packages[module].src()
|
return packages[module]
|
||||||
.pipe(packages[module]())
|
.src()
|
||||||
.pipe(gulp.dest(`${dist}/${module}`));
|
.pipe(packages[module]())
|
||||||
});
|
.pipe(gulp.dest(`${dist}/${module}`));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
gulp.task('build', function (cb) {
|
gulp.task('build', function(cb) {
|
||||||
gulpSequence(modules, cb);
|
gulpSequence(modules, cb);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
gulp.task('move', function() {
|
||||||
|
gulp.src(['node_modules/@nestjs/**/*']).pipe(
|
||||||
|
gulp.dest('examples/01-cats-app/node_modules/@nestjs')
|
||||||
|
).pipe(
|
||||||
|
gulp.dest('examples/02-gateways/node_modules/@nestjs')
|
||||||
|
).pipe(
|
||||||
|
gulp.dest('examples/03-microservices/node_modules/@nestjs')
|
||||||
|
).pipe(
|
||||||
|
gulp.dest('examples/04-injector/node_modules/@nestjs')
|
||||||
|
).pipe(
|
||||||
|
gulp.dest('examples/05-sql-typeorm/node_modules/@nestjs')
|
||||||
|
).pipe(
|
||||||
|
gulp.dest('examples/06-mongoose/node_modules/@nestjs')
|
||||||
|
).pipe(
|
||||||
|
gulp.dest('examples/07-sequelize/node_modules/@nestjs')
|
||||||
|
).pipe(
|
||||||
|
gulp.dest('examples/08-passport/node_modules/@nestjs')
|
||||||
|
).pipe(
|
||||||
|
gulp.dest('examples/09-babel-example/node_modules/@nestjs')
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
"start:live": "nodemon -e ts --watch src index.js",
|
"start:live": "nodemon -e ts --watch src index.js",
|
||||||
"test": "nyc --require ts-node/register mocha src/**/*.spec.ts --reporter spec",
|
"test": "nyc --require ts-node/register mocha src/**/*.spec.ts --reporter spec",
|
||||||
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
||||||
"build": "gulp build",
|
"build": "gulp build && gulp move",
|
||||||
"build:lib": "gulp build --dist lib",
|
"build:lib": "gulp build --dist lib",
|
||||||
"prepublish": "npm run build:lib",
|
"prepublish": "npm run build:lib",
|
||||||
"publish": "./node_modules/.bin/lerna publish --skip-git"
|
"publish": "./node_modules/.bin/lerna publish --skip-git"
|
||||||
|
|||||||
@@ -6,6 +6,6 @@ export class ExpressAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static createRouter(): any {
|
public static createRouter(): any {
|
||||||
return express.Router();
|
return express.Router({ mergeParams: true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,13 +20,13 @@ import { ApplicationConfig } from './../application-config';
|
|||||||
import { RouterExceptionFilters } from './../router/router-exception-filters';
|
import { RouterExceptionFilters } from './../router/router-exception-filters';
|
||||||
|
|
||||||
export class MiddlewaresModule {
|
export class MiddlewaresModule {
|
||||||
private static readonly routesMapper = new RoutesMapper();
|
private readonly routesMapper = new RoutesMapper();
|
||||||
private static readonly routerProxy = new RouterProxy();
|
private readonly routerProxy = new RouterProxy();
|
||||||
private static readonly routerMethodFactory = new RouterMethodFactory();
|
private readonly routerMethodFactory = new RouterMethodFactory();
|
||||||
private static routerExceptionFilter: RouterExceptionFilters;
|
private routerExceptionFilter: RouterExceptionFilters;
|
||||||
private static resolver: MiddlewaresResolver;
|
private resolver: MiddlewaresResolver;
|
||||||
|
|
||||||
public static async setup(
|
public async setup(
|
||||||
middlewaresContainer: MiddlewaresContainer,
|
middlewaresContainer: MiddlewaresContainer,
|
||||||
container: NestContainer,
|
container: NestContainer,
|
||||||
config: ApplicationConfig,
|
config: ApplicationConfig,
|
||||||
@@ -38,7 +38,7 @@ export class MiddlewaresModule {
|
|||||||
await this.resolveMiddlewares(middlewaresContainer, modules);
|
await this.resolveMiddlewares(middlewaresContainer, modules);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async resolveMiddlewares(
|
public async resolveMiddlewares(
|
||||||
middlewaresContainer: MiddlewaresContainer,
|
middlewaresContainer: MiddlewaresContainer,
|
||||||
modules: Map<string, Module>,
|
modules: Map<string, Module>,
|
||||||
) {
|
) {
|
||||||
@@ -50,7 +50,7 @@ export class MiddlewaresModule {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static loadConfiguration(
|
public loadConfiguration(
|
||||||
middlewaresContainer: MiddlewaresContainer,
|
middlewaresContainer: MiddlewaresContainer,
|
||||||
instance: NestModule,
|
instance: NestModule,
|
||||||
module: string,
|
module: string,
|
||||||
@@ -66,7 +66,7 @@ export class MiddlewaresModule {
|
|||||||
middlewaresContainer.addConfig(config, module);
|
middlewaresContainer.addConfig(config, module);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async setupMiddlewares(middlewaresContainer: MiddlewaresContainer, app) {
|
public async setupMiddlewares(middlewaresContainer: MiddlewaresContainer, app) {
|
||||||
const configs = middlewaresContainer.getConfigs();
|
const configs = middlewaresContainer.getConfigs();
|
||||||
await Promise.all([...configs.entries()].map(async ([module, moduleConfigs]) => {
|
await Promise.all([...configs.entries()].map(async ([module, moduleConfigs]) => {
|
||||||
await Promise.all([...moduleConfigs].map(async (config: MiddlewareConfiguration) => {
|
await Promise.all([...moduleConfigs].map(async (config: MiddlewareConfiguration) => {
|
||||||
@@ -75,7 +75,7 @@ export class MiddlewaresModule {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async setupMiddlewareConfig(
|
public async setupMiddlewareConfig(
|
||||||
middlewaresContainer: MiddlewaresContainer,
|
middlewaresContainer: MiddlewaresContainer,
|
||||||
config: MiddlewareConfiguration,
|
config: MiddlewareConfiguration,
|
||||||
module: string,
|
module: string,
|
||||||
@@ -87,7 +87,7 @@ export class MiddlewaresModule {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async setupRouteMiddleware(
|
public async setupRouteMiddleware(
|
||||||
middlewaresContainer: MiddlewaresContainer,
|
middlewaresContainer: MiddlewaresContainer,
|
||||||
route: ControllerMetadata & { method: RequestMethod },
|
route: ControllerMetadata & { method: RequestMethod },
|
||||||
config: MiddlewareConfiguration,
|
config: MiddlewareConfiguration,
|
||||||
@@ -109,7 +109,7 @@ export class MiddlewaresModule {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async setupHandler(
|
private async setupHandler(
|
||||||
instance: NestMiddleware,
|
instance: NestMiddleware,
|
||||||
metatype: Metatype<NestMiddleware>,
|
metatype: Metatype<NestMiddleware>,
|
||||||
app: any,
|
app: any,
|
||||||
@@ -134,7 +134,7 @@ export class MiddlewaresModule {
|
|||||||
setupWithProxy(middleware);
|
setupWithProxy(middleware);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static setupHandlerWithProxy(
|
private setupHandlerWithProxy(
|
||||||
exceptionsHandler: ExceptionsHandler,
|
exceptionsHandler: ExceptionsHandler,
|
||||||
router: (...args) => void,
|
router: (...args) => void,
|
||||||
middleware: (req, res, next) => void,
|
middleware: (req, res, next) => void,
|
||||||
|
|||||||
@@ -31,8 +31,16 @@ const { NestMicroservice } = optional('@nestjs/microservices/nest-microservice')
|
|||||||
const { IoAdapter } = optional('@nestjs/websockets/adapters/io-adapter') || {} as any;
|
const { IoAdapter } = optional('@nestjs/websockets/adapters/io-adapter') || {} as any;
|
||||||
|
|
||||||
export class NestApplication implements INestApplication {
|
export class NestApplication implements INestApplication {
|
||||||
private readonly middlewaresContainer = new MiddlewaresContainer();
|
|
||||||
private readonly logger = new Logger(NestApplication.name, true);
|
private readonly logger = new Logger(NestApplication.name, true);
|
||||||
|
private readonly middlewaresModule = new MiddlewaresModule();
|
||||||
|
private readonly middlewaresContainer = new MiddlewaresContainer();
|
||||||
|
private readonly microservicesModule = MicroservicesModule
|
||||||
|
? new MicroservicesModule()
|
||||||
|
: null;
|
||||||
|
private readonly socketModule = SocketModule
|
||||||
|
? new SocketModule()
|
||||||
|
: null;
|
||||||
|
|
||||||
private readonly httpServer: http.Server = null;
|
private readonly httpServer: http.Server = null;
|
||||||
private readonly routesResolver: Resolver = null;
|
private readonly routesResolver: Resolver = null;
|
||||||
private readonly config: ApplicationConfig;
|
private readonly config: ApplicationConfig;
|
||||||
@@ -59,13 +67,13 @@ export class NestApplication implements INestApplication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async setupModules() {
|
public async setupModules() {
|
||||||
SocketModule && SocketModule.setup(this.container, this.config);
|
this.socketModule && this.socketModule.setup(this.container, this.config);
|
||||||
|
|
||||||
if (MicroservicesModule) {
|
if (this.microservicesModule) {
|
||||||
MicroservicesModule.setup(this.container, this.config);
|
this.microservicesModule.setup(this.container, this.config);
|
||||||
MicroservicesModule.setupClients(this.container);
|
this.microservicesModule.setupClients(this.container);
|
||||||
}
|
}
|
||||||
await MiddlewaresModule.setup(
|
await this.middlewaresModule.setup(
|
||||||
this.middlewaresContainer,
|
this.middlewaresContainer,
|
||||||
this.container,
|
this.container,
|
||||||
this.config,
|
this.config,
|
||||||
@@ -93,7 +101,6 @@ export class NestApplication implements INestApplication {
|
|||||||
if (!NestMicroservice) {
|
if (!NestMicroservice) {
|
||||||
throw new MicroservicesPackageNotFoundException();
|
throw new MicroservicesPackageNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
const instance = new NestMicroservice(this.container as any, config as any);
|
const instance = new NestMicroservice(this.container as any, config as any);
|
||||||
instance.setupListeners();
|
instance.setupListeners();
|
||||||
instance.setIsInitialized(true);
|
instance.setIsInitialized(true);
|
||||||
@@ -137,7 +144,7 @@ export class NestApplication implements INestApplication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public close() {
|
public close() {
|
||||||
SocketModule && SocketModule.close();
|
this.socketModule && this.socketModule.close();
|
||||||
this.httpServer && this.httpServer.close();
|
this.httpServer && this.httpServer.close();
|
||||||
this.microservices.forEach((microservice) => {
|
this.microservices.forEach((microservice) => {
|
||||||
microservice.setIsTerminated(true);
|
microservice.setIsTerminated(true);
|
||||||
@@ -171,7 +178,7 @@ export class NestApplication implements INestApplication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async setupMiddlewares(instance) {
|
private async setupMiddlewares(instance) {
|
||||||
await MiddlewaresModule.setupMiddlewares(this.middlewaresContainer, instance);
|
await this.middlewaresModule.setupMiddlewares(this.middlewaresContainer, instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
private listenToPromise(microservice: INestMicroservice) {
|
private listenToPromise(microservice: INestMicroservice) {
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ import { ApplicationConfig } from '../../application-config';
|
|||||||
import { MiddlewaresContainer } from "../../middlewares/container";
|
import { MiddlewaresContainer } from "../../middlewares/container";
|
||||||
|
|
||||||
describe('MiddlewaresModule', () => {
|
describe('MiddlewaresModule', () => {
|
||||||
|
let middlewaresModule: MiddlewaresModule;
|
||||||
|
|
||||||
@Controller('test')
|
@Controller('test')
|
||||||
class AnotherRoute { }
|
class AnotherRoute { }
|
||||||
|
|
||||||
@@ -36,7 +38,8 @@ describe('MiddlewaresModule', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
(MiddlewaresModule as any).routerExceptionFilter = new RouterExceptionFilters(
|
middlewaresModule = new MiddlewaresModule();
|
||||||
|
(middlewaresModule as any).routerExceptionFilter = new RouterExceptionFilters(
|
||||||
new ApplicationConfig(),
|
new ApplicationConfig(),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -49,7 +52,7 @@ describe('MiddlewaresModule', () => {
|
|||||||
configure: configureSpy,
|
configure: configureSpy,
|
||||||
};
|
};
|
||||||
|
|
||||||
MiddlewaresModule.loadConfiguration(new MiddlewaresContainer(), mockModule as any, 'Test' as any);
|
middlewaresModule.loadConfiguration(new MiddlewaresContainer(), mockModule as any, 'Test' as any);
|
||||||
|
|
||||||
expect(configureSpy.calledOnce).to.be.true;
|
expect(configureSpy.calledOnce).to.be.true;
|
||||||
expect(configureSpy.calledWith(new MiddlewareBuilder(new RoutesMapper()))).to.be.true;
|
expect(configureSpy.calledWith(new MiddlewareBuilder(new RoutesMapper()))).to.be.true;
|
||||||
@@ -69,7 +72,7 @@ describe('MiddlewaresModule', () => {
|
|||||||
const app = { use: useSpy };
|
const app = { use: useSpy };
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
MiddlewaresModule.setupRouteMiddleware(new MiddlewaresContainer(), route as any, configuration, 'Test' as any, app as any),
|
middlewaresModule.setupRouteMiddleware(new MiddlewaresContainer(), route as any, configuration, 'Test' as any, app as any),
|
||||||
).to.eventually.be.rejectedWith(RuntimeException);
|
).to.eventually.be.rejectedWith(RuntimeException);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -97,7 +100,7 @@ describe('MiddlewaresModule', () => {
|
|||||||
} as any);
|
} as any);
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
MiddlewaresModule.setupRouteMiddleware(container, route as any, configuration, moduleKey, app as any),
|
middlewaresModule.setupRouteMiddleware(container, route as any, configuration, moduleKey, app as any),
|
||||||
).to.be.rejectedWith(InvalidMiddlewareException);
|
).to.be.rejectedWith(InvalidMiddlewareException);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -123,7 +126,7 @@ describe('MiddlewaresModule', () => {
|
|||||||
instance,
|
instance,
|
||||||
});
|
});
|
||||||
|
|
||||||
MiddlewaresModule.setupRouteMiddleware(container, route, configuration, moduleKey, app as any);
|
middlewaresModule.setupRouteMiddleware(container, route, configuration, moduleKey, app as any);
|
||||||
expect(useSpy.calledOnce).to.be.true;
|
expect(useSpy.calledOnce).to.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ import { InterceptorsContextCreator } from '@nestjs/core/interceptors/intercepto
|
|||||||
import { InterceptorsConsumer } from '@nestjs/core/interceptors/interceptors-consumer';
|
import { InterceptorsConsumer } from '@nestjs/core/interceptors/interceptors-consumer';
|
||||||
|
|
||||||
export class MicroservicesModule {
|
export class MicroservicesModule {
|
||||||
private static readonly clientsContainer = new ClientsContainer();
|
private readonly clientsContainer = new ClientsContainer();
|
||||||
private static listenersController: ListenersController;
|
private listenersController: ListenersController;
|
||||||
|
|
||||||
public static setup(container, config) {
|
public setup(container, config) {
|
||||||
const contextCreator = new RpcContextCreator(
|
const contextCreator = new RpcContextCreator(
|
||||||
new RpcProxy(),
|
new RpcProxy(),
|
||||||
new ExceptionFiltersContext(config),
|
new ExceptionFiltersContext(config),
|
||||||
@@ -31,12 +31,12 @@ export class MicroservicesModule {
|
|||||||
new InterceptorsConsumer(),
|
new InterceptorsConsumer(),
|
||||||
);
|
);
|
||||||
this.listenersController = new ListenersController(
|
this.listenersController = new ListenersController(
|
||||||
MicroservicesModule.clientsContainer,
|
this.clientsContainer,
|
||||||
contextCreator,
|
contextCreator,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static setupListeners(container, server: Server & CustomTransportStrategy) {
|
public setupListeners(container, server: Server & CustomTransportStrategy) {
|
||||||
if (!this.listenersController) {
|
if (!this.listenersController) {
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
@@ -44,7 +44,7 @@ export class MicroservicesModule {
|
|||||||
modules.forEach(({ routes }, module) => this.bindListeners(routes, server, module));
|
modules.forEach(({ routes }, module) => this.bindListeners(routes, server, module));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static setupClients(container) {
|
public setupClients(container) {
|
||||||
if (!this.listenersController) {
|
if (!this.listenersController) {
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
@@ -55,7 +55,7 @@ export class MicroservicesModule {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bindListeners(
|
public bindListeners(
|
||||||
controllers: Map<string, InstanceWrapper<Controller>>,
|
controllers: Map<string, InstanceWrapper<Controller>>,
|
||||||
server: Server & CustomTransportStrategy,
|
server: Server & CustomTransportStrategy,
|
||||||
module: string) {
|
module: string) {
|
||||||
@@ -65,13 +65,13 @@ export class MicroservicesModule {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bindClients(controllers: Map<string, InstanceWrapper<Controller>>) {
|
public bindClients(controllers: Map<string, InstanceWrapper<Controller>>) {
|
||||||
controllers.forEach(({ instance, isNotMetatype }) => {
|
controllers.forEach(({ instance, isNotMetatype }) => {
|
||||||
!isNotMetatype && this.listenersController.bindClientsToProperties(instance);
|
!isNotMetatype && this.listenersController.bindClientsToProperties(instance);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static close() {
|
public close() {
|
||||||
const clients = this.clientsContainer.getAllClients();
|
const clients = this.clientsContainer.getAllClients();
|
||||||
clients.forEach((client) => client.close());
|
clients.forEach((client) => client.close());
|
||||||
this.clientsContainer.clear();
|
this.clientsContainer.clear();
|
||||||
|
|||||||
@@ -20,6 +20,11 @@ const { IoAdapter } = optional('@nestjs/websockets/adapters/io-adapter') || {} a
|
|||||||
|
|
||||||
export class NestMicroservice implements INestMicroservice {
|
export class NestMicroservice implements INestMicroservice {
|
||||||
private readonly logger = new Logger(NestMicroservice.name, true);
|
private readonly logger = new Logger(NestMicroservice.name, true);
|
||||||
|
private readonly microservicesModule = new MicroservicesModule();
|
||||||
|
private readonly socketModule = SocketModule
|
||||||
|
? new SocketModule()
|
||||||
|
: null;
|
||||||
|
|
||||||
private readonly microserviceConfig: MicroserviceConfiguration;
|
private readonly microserviceConfig: MicroserviceConfiguration;
|
||||||
private readonly server: Server & CustomTransportStrategy;
|
private readonly server: Server & CustomTransportStrategy;
|
||||||
private readonly config: ApplicationConfig;
|
private readonly config: ApplicationConfig;
|
||||||
@@ -34,7 +39,7 @@ export class NestMicroservice implements INestMicroservice {
|
|||||||
const ioAdapter = IoAdapter ? new IoAdapter() : null;
|
const ioAdapter = IoAdapter ? new IoAdapter() : null;
|
||||||
this.config = new ApplicationConfig(ioAdapter);
|
this.config = new ApplicationConfig(ioAdapter);
|
||||||
|
|
||||||
MicroservicesModule.setup(container, this.config);
|
this.microservicesModule.setup(container, this.config);
|
||||||
this.microserviceConfig = {
|
this.microserviceConfig = {
|
||||||
transport: Transport.TCP,
|
transport: Transport.TCP,
|
||||||
...config,
|
...config,
|
||||||
@@ -44,8 +49,8 @@ export class NestMicroservice implements INestMicroservice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public setupModules() {
|
public setupModules() {
|
||||||
SocketModule && SocketModule.setup(this.container, this.config);
|
this.socketModule && this.socketModule.setup(this.container, this.config);
|
||||||
MicroservicesModule.setupClients(this.container);
|
this.microservicesModule.setupClients(this.container);
|
||||||
|
|
||||||
this.setupListeners();
|
this.setupListeners();
|
||||||
this.setIsInitialized(true);
|
this.setIsInitialized(true);
|
||||||
@@ -54,7 +59,7 @@ export class NestMicroservice implements INestMicroservice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public setupListeners() {
|
public setupListeners() {
|
||||||
MicroservicesModule.setupListeners(this.container, this.server);
|
this.microservicesModule.setupListeners(this.container, this.server);
|
||||||
}
|
}
|
||||||
|
|
||||||
public useWebSocketAdapter(adapter: WebSocketAdapter) {
|
public useWebSocketAdapter(adapter: WebSocketAdapter) {
|
||||||
@@ -102,7 +107,7 @@ export class NestMicroservice implements INestMicroservice {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private closeApplication() {
|
private closeApplication() {
|
||||||
SocketModule && SocketModule.close();
|
this.socketModule && this.socketModule.close();
|
||||||
|
|
||||||
this.callDestroyHook();
|
this.callDestroyHook();
|
||||||
this.setIsTerminated(true);
|
this.setIsTerminated(true);
|
||||||
|
|||||||
@@ -18,24 +18,23 @@ import { InterceptorsContextCreator } from '@nestjs/core/interceptors/intercepto
|
|||||||
import { InterceptorsConsumer } from '@nestjs/core/interceptors/interceptors-consumer';
|
import { InterceptorsConsumer } from '@nestjs/core/interceptors/interceptors-consumer';
|
||||||
|
|
||||||
export class SocketModule {
|
export class SocketModule {
|
||||||
private static socketsContainer = new SocketsContainer();
|
private socketsContainer = new SocketsContainer();
|
||||||
private static webSocketsController: WebSocketsController;
|
private webSocketsController: WebSocketsController;
|
||||||
|
|
||||||
public static setup(container, config) {
|
public setup(container, config) {
|
||||||
this.webSocketsController = new WebSocketsController(
|
this.webSocketsController = new WebSocketsController(
|
||||||
new SocketServerProvider(this.socketsContainer, config), container, config,
|
new SocketServerProvider(this.socketsContainer, config), container, config,
|
||||||
this.getContextCreator(container),
|
this.getContextCreator(container),
|
||||||
);
|
);
|
||||||
|
|
||||||
const modules = container.getModules();
|
const modules = container.getModules();
|
||||||
modules.forEach(({ components }, moduleName) => this.hookGatewaysIntoServers(components, moduleName));
|
modules.forEach(({ components }, moduleName) => this.hookGatewaysIntoServers(components, moduleName));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static hookGatewaysIntoServers(components: Map<string, InstanceWrapper<Injectable>>, moduleName: string) {
|
public hookGatewaysIntoServers(components: Map<string, InstanceWrapper<Injectable>>, moduleName: string) {
|
||||||
components.forEach((wrapper) => this.hookGatewayIntoServer(wrapper, moduleName));
|
components.forEach((wrapper) => this.hookGatewayIntoServer(wrapper, moduleName));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static hookGatewayIntoServer(wrapper: InstanceWrapper<Injectable>, moduleName: string) {
|
public hookGatewayIntoServer(wrapper: InstanceWrapper<Injectable>, moduleName: string) {
|
||||||
const { instance, metatype, isNotMetatype } = wrapper;
|
const { instance, metatype, isNotMetatype } = wrapper;
|
||||||
if (isNotMetatype) {
|
if (isNotMetatype) {
|
||||||
return;
|
return;
|
||||||
@@ -51,13 +50,13 @@ export class SocketModule {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static close() {
|
public close() {
|
||||||
const servers = this.socketsContainer.getAllServers();
|
const servers = this.socketsContainer.getAllServers();
|
||||||
servers.forEach(({ server }) => server.close());
|
servers.forEach(({ server }) => server.close());
|
||||||
this.socketsContainer.clear();
|
this.socketsContainer.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static getContextCreator(container): WsContextCreator {
|
private getContextCreator(container): WsContextCreator {
|
||||||
return new WsContextCreator(
|
return new WsContextCreator(
|
||||||
new WsProxy(),
|
new WsProxy(),
|
||||||
new ExceptionFiltersContext(),
|
new ExceptionFiltersContext(),
|
||||||
|
|||||||
Reference in New Issue
Block a user