mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
refactor() improve naming, remove inconsistency
This commit is contained in:
@@ -3,7 +3,7 @@ import { RuntimeException } from './runtime.exception';
|
||||
export class UnknownModuleException extends RuntimeException {
|
||||
constructor() {
|
||||
super(
|
||||
'Nest cannot select given module (it does not exist in current context)',
|
||||
'Nest could not select the given module (it does not exist in current context)',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,12 +29,10 @@ export class ExternalExceptionsHandler extends ExternalExceptionFilter {
|
||||
if (isEmpty(this.filters)) {
|
||||
return null;
|
||||
}
|
||||
const isInstanceOf = metatype => exception instanceof metatype;
|
||||
const filter = this.filters.find(({ exceptionMetatypes, func }) => {
|
||||
const hasMetatype =
|
||||
!exceptionMetatypes.length ||
|
||||
exceptionMetatypes.some(
|
||||
ExceptionMetatype => exception instanceof ExceptionMetatype,
|
||||
);
|
||||
!exceptionMetatypes.length || exceptionMetatypes.some(isInstanceOf);
|
||||
return hasMetatype;
|
||||
});
|
||||
return filter ? filter.func(exception, host) : null;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Module } from '../injector/module';
|
||||
import { BeforeApplicationShutdown } from '@nestjs/common';
|
||||
import { isNil } from '@nestjs/common/utils/shared.utils';
|
||||
import iterate from 'iterare';
|
||||
import { BeforeApplicationShutdown } from '@nestjs/common';
|
||||
import { InstanceWrapper } from '../injector/instance-wrapper';
|
||||
import { Module } from '../injector/module';
|
||||
import {
|
||||
getNonTransientInstances,
|
||||
getTransientInstances,
|
||||
@@ -59,7 +59,10 @@ export async function callBeforeAppShutdownHook(
|
||||
const transientInstances = getTransientInstances(instances);
|
||||
await Promise.all(callOperator(transientInstances, signal));
|
||||
|
||||
if (moduleClassInstance && hasBeforeApplicationShutdownHook(moduleClassInstance)) {
|
||||
if (
|
||||
moduleClassInstance &&
|
||||
hasBeforeApplicationShutdownHook(moduleClassInstance)
|
||||
) {
|
||||
await (moduleClassInstance as BeforeApplicationShutdown).beforeApplicationShutdown(
|
||||
signal,
|
||||
);
|
||||
|
||||
@@ -16,9 +16,7 @@ import {
|
||||
function hasOnAppBootstrapHook(
|
||||
instance: unknown,
|
||||
): instance is OnApplicationBootstrap {
|
||||
return !isNil(
|
||||
(instance as OnApplicationBootstrap).onApplicationBootstrap,
|
||||
);
|
||||
return !isNil((instance as OnApplicationBootstrap).onApplicationBootstrap);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,15 +19,16 @@ export class MetadataScanner {
|
||||
}
|
||||
|
||||
*getAllFilteredMethodNames(prototype: any): IterableIterator<string> {
|
||||
const isMethod = (prop: string) => {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(prototype, prop);
|
||||
if (descriptor.set || descriptor.get) {
|
||||
return false;
|
||||
}
|
||||
return !isConstructor(prop) && isFunction(prototype[prop]);
|
||||
};
|
||||
do {
|
||||
yield* iterate(Object.getOwnPropertyNames(prototype))
|
||||
.filter(prop => {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(prototype, prop);
|
||||
if (descriptor.set || descriptor.get) {
|
||||
return false;
|
||||
}
|
||||
return !isConstructor(prop) && isFunction(prototype[prop]);
|
||||
})
|
||||
.filter(isMethod)
|
||||
.toArray();
|
||||
} while (
|
||||
// tslint:disable-next-line:no-conditional-assignment
|
||||
|
||||
@@ -63,8 +63,9 @@ export class MiddlewareModule {
|
||||
middlewareContainer: MiddlewareContainer,
|
||||
modules: Map<string, Module>,
|
||||
) {
|
||||
const moduleEntries = [...modules.entries()];
|
||||
await Promise.all(
|
||||
[...modules.entries()].map(async ([name, module]) => {
|
||||
moduleEntries.map(async ([name, module]) => {
|
||||
const instance = module.instance;
|
||||
|
||||
await this.loadConfiguration(middlewareContainer, instance, name);
|
||||
@@ -109,11 +110,11 @@ export class MiddlewareModule {
|
||||
);
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
[...configs.entries()].map(async ([module, moduleConfigs]) => {
|
||||
await Promise.all(registerAllConfigs(module, [...moduleConfigs]));
|
||||
}),
|
||||
);
|
||||
const configEntries = [...configs.entries()];
|
||||
const registerModuleConfigs = async ([module, moduleConfigs]) => {
|
||||
await Promise.all(registerAllConfigs(module, [...moduleConfigs]));
|
||||
};
|
||||
await Promise.all(configEntries.map(registerModuleConfigs));
|
||||
}
|
||||
|
||||
public async registerMiddlewareConfig(
|
||||
|
||||
@@ -37,7 +37,7 @@ export class ListenersController {
|
||||
private readonly exceptionFiltersContext: ExceptionFiltersContext,
|
||||
) {}
|
||||
|
||||
public bindPatternHandlers(
|
||||
public registerPatternHandlers(
|
||||
instanceWrapper: InstanceWrapper<Controller>,
|
||||
server: Server & CustomTransportStrategy,
|
||||
moduleKey: string,
|
||||
@@ -70,7 +70,7 @@ export class ListenersController {
|
||||
);
|
||||
}
|
||||
|
||||
public bindClientsToProperties(instance: Controller) {
|
||||
public assignClientsToProperties(instance: Controller) {
|
||||
for (const {
|
||||
property,
|
||||
metadata,
|
||||
|
||||
@@ -79,14 +79,14 @@ export class MicroservicesModule {
|
||||
module: string,
|
||||
) {
|
||||
controllers.forEach(wrapper =>
|
||||
this.listenersController.bindPatternHandlers(wrapper, server, module),
|
||||
this.listenersController.registerPatternHandlers(wrapper, server, module),
|
||||
);
|
||||
}
|
||||
|
||||
public bindClients(items: Map<string, InstanceWrapper<Controller>>) {
|
||||
items.forEach(({ instance, isNotMetatype }) => {
|
||||
!isNotMetatype &&
|
||||
this.listenersController.bindClientsToProperties(instance);
|
||||
this.listenersController.assignClientsToProperties(instance);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export * from './gateway-metadata.interface';
|
||||
export * from './hooks';
|
||||
export * from './observable-socket-server.interface';
|
||||
export * from './socket-events-host.interface';
|
||||
export * from './web-socket-server.interface';
|
||||
export * from './ws-response.interface';
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ReplaySubject, Subject } from 'rxjs';
|
||||
|
||||
export interface ObservableSocketServer<T = any> {
|
||||
export interface SocketEventsHost<T = any> {
|
||||
server: T;
|
||||
init: ReplaySubject<T>;
|
||||
connection: Subject<any>;
|
||||
@@ -1,8 +1,8 @@
|
||||
import { ReplaySubject, Subject } from 'rxjs';
|
||||
import { ObservableSocketServer } from './interfaces/observable-socket-server.interface';
|
||||
import { SocketEventsHost } from './interfaces/socket-events-host.interface';
|
||||
|
||||
export class ObservableSocket {
|
||||
public static create<T = any>(server: T): ObservableSocketServer<T> {
|
||||
export class SocketEventsHostFactory {
|
||||
public static create<T = any>(server: T): SocketEventsHost<T> {
|
||||
const init = new ReplaySubject<T>();
|
||||
init.next(server);
|
||||
|
||||
@@ -11,12 +11,12 @@ import { PipesConsumer } from '@nestjs/core/pipes/pipes-consumer';
|
||||
import { PipesContextCreator } from '@nestjs/core/pipes/pipes-context-creator';
|
||||
import iterate from 'iterare';
|
||||
import { GATEWAY_METADATA } from './constants';
|
||||
import { SocketsContainer } from './container';
|
||||
import { ExceptionFiltersContext } from './context/exception-filters-context';
|
||||
import { WsContextCreator } from './context/ws-context-creator';
|
||||
import { WsProxy } from './context/ws-proxy';
|
||||
import { NestGateway } from './interfaces/nest-gateway.interface';
|
||||
import { SocketServerProvider } from './socket-server-provider';
|
||||
import { SocketsContainer } from './sockets-container';
|
||||
import { WebSocketsController } from './web-sockets-controller';
|
||||
|
||||
export class SocketModule<HttpServer = any> {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { validatePath } from '@nestjs/common/utils/shared.utils';
|
||||
import { ApplicationConfig } from '@nestjs/core/application-config';
|
||||
import { isString } from 'util';
|
||||
import { SocketsContainer } from './container';
|
||||
import { GatewayMetadata } from './interfaces/gateway-metadata.interface';
|
||||
import { ObservableSocketServer } from './interfaces/observable-socket-server.interface';
|
||||
import { ObservableSocket } from './observable-socket';
|
||||
import { SocketEventsHost } from './interfaces/socket-events-host.interface';
|
||||
import { SocketEventsHostFactory } from './socket-events-host-factory';
|
||||
import { SocketsContainer } from './sockets-container';
|
||||
|
||||
export class SocketServerProvider {
|
||||
constructor(
|
||||
@@ -15,7 +15,7 @@ export class SocketServerProvider {
|
||||
public scanForSocketServer<T extends GatewayMetadata>(
|
||||
options: T,
|
||||
port: number,
|
||||
): ObservableSocketServer {
|
||||
): SocketEventsHost {
|
||||
const observableServer = this.socketsContainer.getServerByPort(port);
|
||||
return observableServer
|
||||
? this.createWithNamespace(options, port, observableServer)
|
||||
@@ -25,11 +25,11 @@ export class SocketServerProvider {
|
||||
private createSocketServer<T extends GatewayMetadata>(
|
||||
options: T,
|
||||
port: number,
|
||||
): ObservableSocketServer {
|
||||
): SocketEventsHost {
|
||||
const { namespace, server, ...opt } = options as any;
|
||||
const adapter = this.applicationConfig.getIoAdapter();
|
||||
const ioServer = adapter.create(port, opt);
|
||||
const observableSocket = ObservableSocket.create(ioServer);
|
||||
const observableSocket = SocketEventsHostFactory.create(ioServer);
|
||||
|
||||
this.socketsContainer.addServer(null, port, observableSocket);
|
||||
return this.createWithNamespace(options, port, observableSocket);
|
||||
@@ -38,8 +38,8 @@ export class SocketServerProvider {
|
||||
private createWithNamespace<T extends GatewayMetadata>(
|
||||
options: T,
|
||||
port: number,
|
||||
observableSocket: ObservableSocketServer,
|
||||
): ObservableSocketServer {
|
||||
observableSocket: SocketEventsHost,
|
||||
): SocketEventsHost {
|
||||
const { namespace } = options;
|
||||
if (!namespace) {
|
||||
return observableSocket;
|
||||
@@ -49,7 +49,9 @@ export class SocketServerProvider {
|
||||
port,
|
||||
observableSocket.server,
|
||||
);
|
||||
const observableNamespaceSocket = ObservableSocket.create(namespaceServer);
|
||||
const observableNamespaceSocket = SocketEventsHostFactory.create(
|
||||
namespaceServer,
|
||||
);
|
||||
this.socketsContainer.addServer(namespace, port, observableNamespaceSocket);
|
||||
return observableNamespaceSocket;
|
||||
}
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
import { ObservableSocketServer } from './interfaces';
|
||||
import { SocketEventsHost } from './interfaces';
|
||||
|
||||
export class SocketsContainer {
|
||||
private readonly observableServers = new Map<
|
||||
string | RegExp,
|
||||
ObservableSocketServer
|
||||
SocketEventsHost
|
||||
>();
|
||||
|
||||
public getAllServers(): Map<string | RegExp, ObservableSocketServer> {
|
||||
public getAllServers(): Map<string | RegExp, SocketEventsHost> {
|
||||
return this.observableServers;
|
||||
}
|
||||
|
||||
public getServerByPort(port: number): ObservableSocketServer {
|
||||
public getServerByPort(port: number): SocketEventsHost {
|
||||
return this.observableServers.get(`${port}`);
|
||||
}
|
||||
|
||||
public addServer(
|
||||
namespace: string | RegExp,
|
||||
port: number,
|
||||
server: ObservableSocketServer,
|
||||
server: SocketEventsHost,
|
||||
) {
|
||||
this.observableServers.set(
|
||||
namespace ? `${namespace}:${port}` : `${port}`,
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
MessageMappingProperties,
|
||||
} from './gateway-metadata-explorer';
|
||||
import { NestGateway } from './interfaces/nest-gateway.interface';
|
||||
import { ObservableSocketServer } from './interfaces/observable-socket-server.interface';
|
||||
import { SocketEventsHost } from './interfaces/socket-events-host.interface';
|
||||
import { SocketServerProvider } from './socket-server-provider';
|
||||
|
||||
export class WebSocketsController {
|
||||
@@ -37,10 +37,10 @@ export class WebSocketsController {
|
||||
if (!Number.isInteger(port)) {
|
||||
throw new InvalidSocketPortException(port, metatype);
|
||||
}
|
||||
this.subscribeObservableServer(instance, options, port, module);
|
||||
this.subscribeToServerEvents(instance, options, port, module);
|
||||
}
|
||||
|
||||
public subscribeObservableServer(
|
||||
public subscribeToServerEvents(
|
||||
instance: NestGateway,
|
||||
options: any,
|
||||
port: number,
|
||||
@@ -57,14 +57,14 @@ export class WebSocketsController {
|
||||
options,
|
||||
port,
|
||||
);
|
||||
this.hookServerToProperties(instance, observableServer.server);
|
||||
this.assignServerToProperties(instance, observableServer.server);
|
||||
this.subscribeEvents(instance, messageHandlers, observableServer);
|
||||
}
|
||||
|
||||
public subscribeEvents(
|
||||
instance: NestGateway,
|
||||
subscribersMap: MessageMappingProperties[],
|
||||
observableServer: ObservableSocketServer,
|
||||
observableServer: SocketEventsHost,
|
||||
) {
|
||||
const { init, disconnect, connection, server } = observableServer;
|
||||
const adapter = this.config.getIoAdapter();
|
||||
@@ -154,7 +154,10 @@ export class WebSocketsController {
|
||||
return of(result);
|
||||
}
|
||||
|
||||
private hookServerToProperties<T = any>(instance: NestGateway, server: any) {
|
||||
private assignServerToProperties<T = any>(
|
||||
instance: NestGateway,
|
||||
server: any,
|
||||
) {
|
||||
for (const propertyKey of this.metadataExplorer.scanForServerHooks(
|
||||
instance,
|
||||
)) {
|
||||
|
||||
Reference in New Issue
Block a user