UNPKG

@midwayjs/web

Version:

Midway Web Framework for Egg.js

331 lines 13.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createEggAgent = exports.createEggApplication = exports.createAgentWorkerLoader = exports.createAppWorkerLoader = void 0; const utils_1 = require("./utils"); const core_1 = require("@midwayjs/core"); const path_1 = require("path"); const fs_1 = require("fs"); const logger_1 = require("./logger"); const router_1 = require("@eggjs/router"); const util_1 = require("util"); const lifecycle_1 = require("./framework/lifecycle"); const web_1 = require("./framework/web"); const interface_1 = require("./interface"); const ROUTER = Symbol('EggCore#router'); const EGG_LOADER = Symbol.for('egg#loader'); const EGG_PATH = Symbol.for('egg#eggPath'); const LOGGERS = Symbol('EggApplication#loggers'); const debug = (0, util_1.debuglog)('midway:debug'); let customFramework = null; function getFramework() { if (customFramework) return customFramework; /** * create real egg loader and application object */ const pkg = (0, core_1.safeRequire)((0, path_1.join)(process.env.MIDWAY_PROJECT_APPDIR || process.cwd(), 'package.json'), false); customFramework = (0, core_1.safelyGet)('egg.framework', pkg); if (customFramework) { return customFramework; } else { return 'egg'; } } const createAppWorkerLoader = () => { var _a; const AppWorkerLoader = ((_a = require(getFramework())) === null || _a === void 0 ? void 0 : _a.AppWorkerLoader) || require('egg').AppWorkerLoader; class EggAppWorkerLoader extends AppWorkerLoader { constructor() { super(...arguments); this.useEggSocketIO = false; } getEggPaths() { if (!this.appDir) { // 这里的逻辑是为了兼容老 cluster 模式 if (this.app.options.typescript || this.app.options.isTsMode) { process.env.EGG_TYPESCRIPT = 'true'; } const result = (0, utils_1.parseNormalDir)(this.app.options['baseDir'], this.app.options.isTsMode); this.baseDir = result.baseDir; this.options.baseDir = this.baseDir; this.appDir = this.app.appDir = result.appDir; } const result = super.getEggPaths(); const monorepoRoot = (0, utils_1.findLernaRoot)(); if (monorepoRoot) { result.push(monorepoRoot); } if (process.env.MIDWAY_EGG_PLUGIN_PATH) { result.push(process.env.MIDWAY_EGG_PLUGIN_PATH); } if (process.cwd() !== this.appDir) { result.push(this.appDir); } const pathSet = new Set(result); return Array.from(pathSet); } getAppInfo() { if (!this.appInfo) { const appInfo = super.getAppInfo(); // ROOT == HOME in prod env this.appInfo = (0, core_1.extend)(true, appInfo, { root: appInfo.env === 'local' || appInfo.env === 'unittest' ? this.appDir : appInfo.root, appDir: this.appDir, }); } return this.appInfo; } getServerEnv() { // 这里和 egg 不同的是,一是修改了根路径,二是增加了环境变量 let serverEnv = this.options.env; let envPath = (0, path_1.join)(this.appDir, 'config/env'); if (!serverEnv && (0, fs_1.existsSync)(envPath)) { serverEnv = (0, fs_1.readFileSync)(envPath, 'utf8').trim(); } envPath = (0, path_1.join)(this.appDir, 'config/serverEnv'); if (!serverEnv && (0, fs_1.existsSync)(envPath)) { serverEnv = (0, fs_1.readFileSync)(envPath, 'utf8').trim(); } if (!serverEnv) { serverEnv = process.env.MIDWAY_SERVER_ENV || process.env.EGG_SERVER_ENV; } if (!serverEnv) { serverEnv = super.getServerEnv(); } else { serverEnv = serverEnv.trim(); } if (serverEnv && !process.env.MIDWAY_SERVER_ENV) { process.env.MIDWAY_SERVER_ENV = serverEnv; } return serverEnv; } load() { /** * 由于使用了新的 hook 方式,这个时候 midway 已经初始化了一部分 * 但是我们把初始化分为了两个部分,framework 相关的留到这里初始化 * 避免 app 不存在,也能尽可能和单进程模式执行一样的逻辑 */ // lazy initialize framework if (process.env['EGG_CLUSTER_MODE'] === 'true') { this.app.beforeStart(async () => { debug('[egg]: start "initialize framework service with lazy in app.load"'); const applicationContext = (0, core_1.getCurrentApplicationContext)(); applicationContext.bind(lifecycle_1.MidwayWebLifeCycleService); /** * 这里 logger service 已经被 get loggers() 初始化过了,就不需要在这里初始化了 */ // framework/config/plugin/logger/app decorator support await applicationContext.getAsync(core_1.MidwayFrameworkService, [ applicationContext, { application: this.app, }, ]); this.app.once('server', async (server) => { this.framework.setServer(server); // register httpServer to applicationContext applicationContext.registerObject(core_1.HTTP_SERVER_KEY, server); await this.lifecycleService.afterInit(); }); // 这里生命周期走到 onReady this.lifecycleService = await applicationContext.getAsync(lifecycle_1.MidwayWebLifeCycleService, [applicationContext]); // 执行加载框架初始化 this.framework = await applicationContext.getAsync(web_1.MidwayWebFramework); }); } } loadOrigin() { debug('[egg]: application: run load()'); super.load(); } loadConfig() { super.loadConfig(); const configService = (0, core_1.getCurrentApplicationContext)().get(core_1.MidwayConfigService); configService.addObject(this.config, true); Object.defineProperty(this, 'config', { get() { return configService.getConfiguration(); }, }); } loadMiddleware() { super.loadMiddleware(); if (this.plugins['io']) { this.useEggSocketIO = true; const sessionMiddleware = this.app.middlewares['session'](this.app.config['session'], this.app); sessionMiddleware._name = 'session'; this.app.use(sessionMiddleware); } } } return EggAppWorkerLoader; }; exports.createAppWorkerLoader = createAppWorkerLoader; const createAgentWorkerLoader = () => { var _a; const AppWorkerLoader = ((_a = require(getFramework())) === null || _a === void 0 ? void 0 : _a.AgentWorkerLoader) || require('egg').AgentWorkerLoader; class EggAgentWorkerLoader extends AppWorkerLoader { getEggPaths() { if (!this.appDir) { if (this.app.options.typescript || this.app.options.isTsMode) { process.env.EGG_TYPESCRIPT = 'true'; } const result = (0, utils_1.parseNormalDir)(this.app.options['baseDir'], this.app.options.isTsMode); this.baseDir = result.baseDir; this.options.baseDir = this.baseDir; this.appDir = this.app.appDir = result.appDir; } const result = super.getEggPaths(); const monorepoRoot = (0, utils_1.findLernaRoot)(); if (monorepoRoot) { result.push(monorepoRoot); } if (process.env.MIDWAY_EGG_PLUGIN_PATH) { result.push(process.env.MIDWAY_EGG_PLUGIN_PATH); } if (process.cwd() !== this.appDir) { result.push(this.appDir); } const pathSet = new Set(result); return Array.from(pathSet); } getAppInfo() { if (!this.appInfo) { const appInfo = super.getAppInfo(); // ROOT == HOME in prod env this.appInfo = (0, core_1.extend)(true, appInfo, { root: appInfo.env === 'local' || appInfo.env === 'unittest' ? this.appDir : appInfo.root, appDir: this.appDir, }); } return this.appInfo; } getServerEnv() { // 这里和 egg 不同的是,一是修改了根路径,二是增加了环境变量 let serverEnv = this.options.env; let envPath = (0, path_1.join)(this.appDir, 'config/env'); if (!serverEnv && (0, fs_1.existsSync)(envPath)) { serverEnv = (0, fs_1.readFileSync)(envPath, 'utf8').trim(); } envPath = (0, path_1.join)(this.appDir, 'config/serverEnv'); if (!serverEnv && (0, fs_1.existsSync)(envPath)) { serverEnv = (0, fs_1.readFileSync)(envPath, 'utf8').trim(); } if (!serverEnv) { serverEnv = process.env.MIDWAY_SERVER_ENV || process.env.EGG_SERVER_ENV; } if (!serverEnv) { serverEnv = super.getServerEnv(); } else { serverEnv = serverEnv.trim(); } if (serverEnv && !process.env.MIDWAY_SERVER_ENV) { process.env.MIDWAY_SERVER_ENV = serverEnv; } return serverEnv; } load() { this.app.beforeStart(async () => { debug('[egg]: start "initializeAgentApplicationContext"'); await (0, utils_1.initializeAgentApplicationContext)(this.app); super.load(); debug('[egg]: start runAgent decorator'); const runInAgentModules = (0, core_1.listModule)(interface_1.RUN_IN_AGENT_KEY); for (const module of runInAgentModules) { await this.app.applicationContext.getAsync(module); } debug('[egg]: agent load run complete'); }); } loadConfig() { super.loadConfig(); if ((0, core_1.getCurrentApplicationContext)()) { const configService = (0, core_1.getCurrentApplicationContext)().get(core_1.MidwayConfigService); configService.addObject(this.config, true); Object.defineProperty(this, 'config', { get() { return configService.getConfiguration(); }, }); } } } return EggAgentWorkerLoader; }; exports.createAgentWorkerLoader = createAgentWorkerLoader; const createEggApplication = () => { var _a; const Application = ((_a = require(getFramework())) === null || _a === void 0 ? void 0 : _a.Application) || require('egg').Application; class EggApplication extends Application { constructor(options) { // eslint-disable-next-line constructor-super super(options); } get [EGG_LOADER]() { return null; } get [EGG_PATH]() { return __dirname; } get loggers() { if (!this[LOGGERS]) { this[LOGGERS] = (0, logger_1.createLoggers)(this, 'app'); } return this[LOGGERS]; } get router() { if (this[ROUTER]) { return this[ROUTER]; } const router = (this[ROUTER] = new router_1.EggRouter({ sensitive: true }, this)); return router; } dumpConfig() { var _a, _b; if (((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.egg) === null || _b === void 0 ? void 0 : _b.dumpConfig) !== false) { super.dumpConfig(); } } } return EggApplication; }; exports.createEggApplication = createEggApplication; const createEggAgent = () => { var _a; const Agent = ((_a = require(getFramework())) === null || _a === void 0 ? void 0 : _a.Agent) || require('egg').Agent; class EggAgent extends Agent { constructor(options) { // eslint-disable-next-line constructor-super super(options); } get [EGG_LOADER]() { return null; } get [EGG_PATH]() { return __dirname; } get loggers() { if (!this[LOGGERS]) { this[LOGGERS] = (0, logger_1.createLoggers)(this, 'agent'); } return this[LOGGERS]; } dumpConfig() { var _a, _b; if (((_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.egg) === null || _b === void 0 ? void 0 : _b.dumpConfig) !== false) { super.dumpConfig(); } } } return EggAgent; }; exports.createEggAgent = createEggAgent; //# sourceMappingURL=base.js.map