sdg
Version:
pomelo ts
237 lines (236 loc) • 10.1 kB
TypeScript
/// <reference types="node" />
import { EventEmitter } from 'events';
import { IModule, IModuleFactory } from 'sdg-admin';
import Base from './base';
import * as Constants from './util/constants';
import { IComponent } from './interfaces/IComponent';
import { IComponents, ISettings, IServer, IServerMap, TServerType, IServerInfo, IServerId } from './interfaces/IApplication';
import { IApplicationOpts } from './interfaces/opts';
import { ObjectType } from './interfaces/define';
import SessionService from './common/service/sessionService';
import BackendSessionService from './common/service/backendSessionService';
import { IAfterHandlerFilter, IBeforeHandlerFilter, IHandlerFilter } from './interfaces/IHandlerFilter';
import { IChannelServiceOptions } from './interfaces/IChannelService';
import BackendSessionComponent from './components/backendSession';
import { IRemoteComponentOptions } from './interfaces/IRemote';
import { IConnectorComponentOptions } from './interfaces/IConnector';
import { IFrontendOrBackendSession, ISession, ISessionServiceOptions } from './interfaces/ISession';
import { IProxyComponentOptions, IRouteFunction } from './interfaces/IProxy';
import { IResponseErrorHandler, IServerOptions } from './interfaces/IServer';
import { IDictionaryComponentOptions } from './interfaces/IDictionary';
import { IProtobufComponentOptions } from './interfaces/IProtobuf';
import { IPlugin } from './interfaces/IPlugin';
import ChannelService from './common/service/channelService';
import { IMasterServerOpts } from './interfaces/IAdmin';
export declare type IConfigureFunc = () => Promise<void>;
declare global {
interface UserRpc {
test(): void;
}
interface SysRpc {
[serverType: string]: {
msgRemote: {
forwardMessage: (routeParam: IFrontendOrBackendSession, msg: any, session: ISession) => Promise<void>;
};
};
}
}
export default class Application extends Base {
opts: IApplicationOpts;
servers: IServer;
serverTypeMaps: IServerMap;
rpc: UserRpc;
sysRpc: SysRpc;
rpcInvoke: Function;
sessionService: SessionService;
channelService: ChannelService;
backendSessionService: BackendSessionService;
serverInfo: IServerInfo;
serverId: IServerId;
serverType: string;
startTime: number;
usedPlugins: IPlugin[];
private readonly loaded;
components: IComponents;
settings: ISettings;
event: EventEmitter;
constructor(opts: IApplicationOpts);
private initCurServerInfo;
getCurServer(): IServerInfo;
private initEvent;
restore(): void;
start(): Promise<void>;
afterStart(): Promise<void>;
/**
* server管理器,这里只管理serverInfo,具体逻辑交给proxy
* @param servers
*/
addServers(servers: IServerInfo[]): void;
removeServers(ids: IServerId[]): void;
replaceServers(servers: IServer): void;
/**
* 更新serverInfo的配置信息
* @param serverInfo
*/
updateServerInfo(serverInfo: IServerInfo): void;
/**
* 基础设置
* @param setting
* @param val
* @param attach 是否将配置写到实例上
*/
set(setting: 'sessionService', val: SessionService, attach?: boolean): Application;
set(setting: 'backendSessionService', val: BackendSessionComponent, attach?: boolean): Application;
set(setting: 'channelService', val: ChannelService, attach?: boolean): Application;
set(setting: 'channelConfig', val: IChannelServiceOptions, attach?: boolean): Application;
set(setting: 'remoteConfig', val: IRemoteComponentOptions, attach?: boolean): Application;
set(setting: 'sessionConfig', val: ISessionServiceOptions, attach?: boolean): Application;
set(setting: 'proxyConfig', val: IProxyComponentOptions, attach?: boolean): Application;
set(setting: 'serverConfig', val: IServerOptions, attach?: boolean): Application;
set(setting: 'connectorConfig', val: IConnectorComponentOptions, attach?: boolean): Application;
set(setting: 'dictionaryConfig', val: IDictionaryComponentOptions, attach?: boolean): Application;
set(setting: 'protobufConfig', val: IProtobufComponentOptions, attach?: boolean): Application;
set(setting: Constants.KEYWORDS.BEFORE_FILTER, val: IBeforeHandlerFilter[], attach?: boolean): Application;
set(setting: Constants.KEYWORDS.AFTER_FILTER, val: IAfterHandlerFilter[], attach?: boolean): Application;
set(setting: Constants.KEYWORDS.GLOBAL_BEFORE_FILTER, val: IBeforeHandlerFilter[], attach?: boolean): Application;
set(setting: Constants.KEYWORDS.GLOBAL_AFTER_FILTER, val: IAfterHandlerFilter[], attach?: boolean): Application;
set(setting: Constants.RESERVED.BASE, val: string, attach?: boolean): Application;
set(setting: Constants.RESERVED.ENV, val: string, attach?: boolean): Application;
set(setting: Constants.RESERVED.GLOBAL_ERROR_HANDLER, val: IResponseErrorHandler, attach?: boolean): Application;
set(setting: Constants.RESERVED.ERROR_HANDLER, val: IResponseErrorHandler, attach?: boolean): Application;
set(setting: Constants.RESERVED.MASTER, val: IMasterServerOpts): Application;
set(setting: string, val: string | any, attach?: boolean): Application;
get(setting: 'channelService'): ChannelService;
get(setting: 'sessionService'): SessionService;
get(setting: 'backendSessionService'): BackendSessionComponent;
get(setting: 'channelConfig'): IChannelServiceOptions;
get(setting: 'remoteConfig'): IRemoteComponentOptions;
get(setting: 'connectorConfig'): IConnectorComponentOptions;
get(setting: 'sessionConfig'): ISessionServiceOptions;
get(setting: 'proxyConfig'): IProxyComponentOptions;
get(setting: 'serverConfig'): IServerOptions;
get(setting: 'dictionaryConfig'): IDictionaryComponentOptions;
get(setting: 'protobufConfig'): IProtobufComponentOptions;
get(setting: Constants.RESERVED.GLOBAL_ERROR_HANDLER): IResponseErrorHandler;
get(setting: Constants.RESERVED.ERROR_HANDLER): IResponseErrorHandler;
get(setting: Constants.RESERVED.MASTER): IMasterServerOpts;
get(setting: string): string | any;
/**
* Set the route function for the specified server type.
*
* Examples:
*
* app.route('area', routeFunc);
*
* let routeFunc = function(session, msg, app, cb) {
* // all request to area would be route to the first area server
* let areas = app.getServersByType('area');
* cb(null, areas[0].id);
* };
*
* @param {String} serverType server type string
* @param {Function} routeFunc route function. routeFunc(session, msg, app, cb)
* @return {Object} current application instance for chain invoking
* @memberOf Application
*/
route(routeFunc: IRouteFunction, serverType?: string): this;
configure(fn: IConfigureFunc): Application;
configure(env: string, fn: IConfigureFunc): Application;
configure(env: string, type: string, fn: IConfigureFunc): Application;
/**
* 注册管理组件
* @param module
* @param opts
*/
registerAdmin(module: IModule, opts?: any): any;
registerAdmin(moduleId: string, module?: IModule, opts?: any): any;
registerAdmin(module: IModuleFactory, opts?: any): any;
registerAdmin(moduleId: string, module?: IModuleFactory, opts?: any): any;
/**
* Check if `setting` is enabled.
*
* @param {String} setting application setting
* @return {Boolean}
* @memberOf Application
*/
enabled(setting: string): boolean;
/**
* Check if `setting` is disabled.
*
* @param {String} setting application setting
* @return {Boolean}
* @memberOf Application
*/
disabled(setting: string): boolean;
get serverTypes(): TServerType;
getBase(): string;
getServerIds(): string[];
getServers(): IServer;
getServerType(serverInfo?: IServerInfo): string;
getServerId(serverInfo?: IServerInfo): IServerId;
getServerById(id: IServerId): IServerInfo | null;
getServersByType(type: string): IServerInfo[];
isFrontend(server?: IServerInfo): boolean;
/**
* 加载配置 config目录下
* @param key
* @param val
* @param reload
*/
loadConfigBaseApp(key: string, val: string, reload?: boolean): void;
/**
* Use plugin.
* @param {Object} plugin plugin instance
* @param {[type]} opts (optional) construct parameters for the factory function
* @memberOf Application
*/
use(plugin: IPlugin, opts?: any): void;
/**
* add a filter to before and after filter
*/
filter(filter: IHandlerFilter): void;
/**
* Add before filter.
* @param {Object|Function} bf before filter, bf(msg, session, next)
* @memberOf Application
*/
before(bf: IBeforeHandlerFilter): void;
/**
* Add after filter.
* @param {Object|Function} af after filter, `af(err, msg, session, resp, next)`
* @memberOf Application
*/
after(af: IAfterHandlerFilter): void;
/**
* add a global filter to before and after global filter
*/
globalFilter(filter: IHandlerFilter): void;
/**
* Add global before filter.
*
* @param {Object|Function} bf before filter, bf(msg, session, next)
* @memberOf Application
*/
globalBefore(bf: IBeforeHandlerFilter): void;
/**
* Add global after filter.
*/
globalAfter(af: IAfterHandlerFilter): void;
load<T extends IComponent>(component: ObjectType<T>, opts?: any): T;
load<T extends IComponent>(name: string, component: ObjectType<T>, opts?: any): T;
load<T extends IComponent>(component: T, opts?: any): T;
load<T extends IComponent>(name: string, component: T, opts?: any): T;
private replaceServer;
private addFilter;
/**
* 加载master服务器配置
*/
private loadMaster;
/**
* 加载一个事件侦听
* @param Event
* @param opts
*/
private loadEvent;
private contains;
}