@midwayjs/web 3.0.0-alpha.9 → 3.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +54 -0
- package/agent.js +13 -6
- package/app/extend/agent.js +0 -4
- package/app/extend/application.js +0 -4
- package/app.js +13 -12
- package/config/config.default.js +13 -8
- package/config/plugin.js +6 -4
- package/dist/application.js +6 -6
- package/dist/base.js +94 -62
- package/dist/configuration.d.ts +13 -0
- package/dist/configuration.js +98 -0
- package/dist/framework/web.d.ts +27 -26
- package/dist/framework/web.js +182 -120
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -4
- package/dist/interface.d.ts +53 -12
- package/dist/logger.d.ts +35 -2
- package/dist/logger.js +84 -102
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +132 -9
- package/package.json +13 -14
- package/dist/framework/singleProcess.d.ts +0 -34
- package/dist/framework/singleProcess.js +0 -122
- package/dist/starter.d.ts +0 -12
- package/dist/starter.js +0 -23
package/dist/logger.js
CHANGED
|
@@ -1,78 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createLoggers = void 0;
|
|
4
|
-
const egg_logger_1 = require("egg-logger");
|
|
3
|
+
exports.MidwayEggContextLogger = exports.createLoggers = void 0;
|
|
5
4
|
const logger_1 = require("@midwayjs/logger");
|
|
6
5
|
const path_1 = require("path");
|
|
7
6
|
const fs_1 = require("fs");
|
|
8
|
-
const core_1 = require("@midwayjs/core");
|
|
9
7
|
const utils_1 = require("./utils");
|
|
10
8
|
const os = require("os");
|
|
9
|
+
const core_1 = require("@midwayjs/core");
|
|
10
|
+
const extend = require("extend2");
|
|
11
|
+
const util_1 = require("util");
|
|
12
|
+
const debug = (0, util_1.debuglog)('midway:debug');
|
|
11
13
|
const isWindows = os.platform() === 'win32';
|
|
12
|
-
const levelTransform = level => {
|
|
13
|
-
switch (level) {
|
|
14
|
-
case 'NONE':
|
|
15
|
-
case Infinity: // egg logger 的 none 是这个等级
|
|
16
|
-
return null;
|
|
17
|
-
case 0:
|
|
18
|
-
case 'DEBUG':
|
|
19
|
-
case 'debug':
|
|
20
|
-
return 'debug';
|
|
21
|
-
case 1:
|
|
22
|
-
case 'INFO':
|
|
23
|
-
case 'info':
|
|
24
|
-
return 'info';
|
|
25
|
-
case 2:
|
|
26
|
-
case 'WARN':
|
|
27
|
-
case 'warn':
|
|
28
|
-
return 'warn';
|
|
29
|
-
case 3:
|
|
30
|
-
case 'ERROR':
|
|
31
|
-
case 'error':
|
|
32
|
-
return 'error';
|
|
33
|
-
default:
|
|
34
|
-
return 'silly';
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
14
|
function isEmptyFile(p) {
|
|
38
|
-
const content = fs_1.readFileSync(p, {
|
|
15
|
+
const content = (0, fs_1.readFileSync)(p, {
|
|
39
16
|
encoding: 'utf8',
|
|
40
17
|
});
|
|
41
18
|
return content === null || content === undefined || content === '';
|
|
42
19
|
}
|
|
43
20
|
function checkEggLoggerExistsAndBackup(dir, fileName) {
|
|
44
|
-
const file = path_1.isAbsolute(fileName) ? fileName : path_1.join(dir, fileName);
|
|
45
|
-
if (fs_1.existsSync(file) && !fs_1.lstatSync(file).isSymbolicLink()) {
|
|
21
|
+
const file = (0, path_1.isAbsolute)(fileName) ? fileName : (0, path_1.join)(dir, fileName);
|
|
22
|
+
if ((0, fs_1.existsSync)(file) && !(0, fs_1.lstatSync)(file).isSymbolicLink()) {
|
|
46
23
|
// 如果是空文件,则直接删了,否则加入备份队列
|
|
47
24
|
if (isEmptyFile(file)) {
|
|
48
25
|
// midway 的软链在 windows 底下也不会创建出来,在 windows 底下就不做文件删除了
|
|
49
26
|
if (!isWindows) {
|
|
50
|
-
fs_1.unlinkSync(file);
|
|
27
|
+
(0, fs_1.unlinkSync)(file);
|
|
51
28
|
}
|
|
52
29
|
}
|
|
53
30
|
else {
|
|
54
|
-
const timeFormat = utils_1.getCurrentDateString();
|
|
55
|
-
fs_1.renameSync(file, file + '.' + timeFormat + '_eggjs_bak');
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
function removeSymbol(dir, fileName) {
|
|
60
|
-
const file = path_1.isAbsolute(fileName) ? fileName : path_1.join(dir, fileName);
|
|
61
|
-
if (fs_1.existsSync(file) && fs_1.lstatSync(file).isSymbolicLink()) {
|
|
62
|
-
if (!isWindows) {
|
|
63
|
-
fs_1.unlinkSync(file);
|
|
31
|
+
const timeFormat = (0, utils_1.getCurrentDateString)();
|
|
32
|
+
(0, fs_1.renameSync)(file, file + '.' + timeFormat + '_eggjs_bak');
|
|
64
33
|
}
|
|
65
34
|
}
|
|
66
35
|
}
|
|
67
|
-
function checkMidwayLoggerSymbolExistsAndRemove(appConfig) {
|
|
68
|
-
removeSymbol(appConfig.logger['dir'], appConfig.logger['appLogName']);
|
|
69
|
-
removeSymbol(appConfig.logger['dir'], appConfig.logger['coreLogName']);
|
|
70
|
-
removeSymbol(appConfig.logger['dir'], appConfig.logger['agentLogName']);
|
|
71
|
-
removeSymbol(appConfig.logger['dir'], appConfig.logger['errorLogName']);
|
|
72
|
-
for (const loggerOption in appConfig['customLogger']) {
|
|
73
|
-
removeSymbol(appConfig.logger['dir'], appConfig['customLogger'][loggerOption].file);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
36
|
class MidwayLoggers extends Map {
|
|
77
37
|
/**
|
|
78
38
|
* @constructor
|
|
@@ -95,9 +55,16 @@ class MidwayLoggers extends Map {
|
|
|
95
55
|
* @param options
|
|
96
56
|
* @param app
|
|
97
57
|
*/
|
|
98
|
-
constructor(options, app) {
|
|
99
|
-
var _a, _b
|
|
58
|
+
constructor(options, app, processType) {
|
|
59
|
+
var _a, _b;
|
|
100
60
|
super();
|
|
61
|
+
/**
|
|
62
|
+
* 日志创建的几种场景,主要考虑 2,4
|
|
63
|
+
* 1、单进程,使用 egg logger
|
|
64
|
+
* 2、单进程,使用 midway logger
|
|
65
|
+
* 3、egg-scripts 多进程,使用 egg-logger
|
|
66
|
+
* 4、egg-scripts 多进程,使用 midway logger
|
|
67
|
+
*/
|
|
101
68
|
// 这么改是为了防止 egg 日志切割时遍历属性,导致报错
|
|
102
69
|
Object.defineProperty(this, 'app', {
|
|
103
70
|
value: app,
|
|
@@ -114,49 +81,48 @@ class MidwayLoggers extends Map {
|
|
|
114
81
|
]) {
|
|
115
82
|
checkEggLoggerExistsAndBackup(options.logger.dir, name);
|
|
116
83
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
84
|
+
/**
|
|
85
|
+
* 走到这里,有几种情况
|
|
86
|
+
* 1、egg-scripts 启动,并使用了 midway logger,egg 先启动了
|
|
87
|
+
* 2、单进程启动,但是没有 configuration,midway 没读到配置,日志服务没初始化
|
|
88
|
+
* 3、再走一遍日志创建,把 egg 插件配置的日志再初始化一遍
|
|
89
|
+
*/
|
|
90
|
+
const loggerService = new core_1.MidwayLoggerService(null);
|
|
91
|
+
loggerService.configService = {
|
|
92
|
+
getConfiguration(configKey) {
|
|
93
|
+
if (configKey) {
|
|
94
|
+
return (0, core_1.safelyGet)(configKey, options);
|
|
95
|
+
}
|
|
96
|
+
return this.configuration;
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
const midwayLoggerConfig = loggerService.transformEggLogger(options);
|
|
100
|
+
extend(true, options, midwayLoggerConfig);
|
|
101
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.midwayLogger) === null || _a === void 0 ? void 0 : _a.clients) {
|
|
102
|
+
// 从 egg 过来,这里有可能没有 dir
|
|
103
|
+
if (!((_b = options.midwayLogger['default']) === null || _b === void 0 ? void 0 : _b.dir)) {
|
|
104
|
+
options.midwayLogger.default['dir'] = options.logger.dir;
|
|
105
|
+
}
|
|
106
|
+
for (const id of Object.keys(options.midwayLogger.clients)) {
|
|
107
|
+
const config = Object.assign({}, options.midwayLogger['default'], options.midwayLogger.clients[id]);
|
|
108
|
+
this.createLogger(config, id);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (!this['logger']) {
|
|
112
|
+
this['logger'] = logger_1.loggers.getLogger('appLogger');
|
|
113
|
+
this.set('logger', logger_1.loggers.getLogger('appLogger'));
|
|
125
114
|
}
|
|
126
|
-
|
|
127
|
-
this.createLogger('coreLogger'
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
consoleLevel: (_h = (_g = options.logger) === null || _g === void 0 ? void 0 : _g.coreLogger) === null || _h === void 0 ? void 0 : _h.consoleLevel,
|
|
131
|
-
}, options.logger, 'coreLogger');
|
|
132
|
-
this.createLogger('logger', { file: options.logger.appLogName }, options.logger, 'logger');
|
|
115
|
+
if (logger_1.loggers.has('coreLogger')) {
|
|
116
|
+
this.createLogger({}, 'coreLogger');
|
|
117
|
+
this.createLogger({}, 'appLogger');
|
|
118
|
+
this.createLogger({}, 'agentLogger');
|
|
133
119
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const customLogger = options.customLogger[loggerKey];
|
|
137
|
-
checkEggLoggerExistsAndBackup(customLogger['dir'] || options.logger.dir, customLogger['file']);
|
|
138
|
-
this.createLogger(loggerKey, customLogger, options.logger);
|
|
139
|
-
}
|
|
120
|
+
else {
|
|
121
|
+
console.log('-----请在 logger 里配置 clients');
|
|
140
122
|
}
|
|
141
123
|
}
|
|
142
|
-
createLogger(
|
|
143
|
-
const
|
|
144
|
-
? levelTransform(options.level)
|
|
145
|
-
: levelTransform(defaultLoggerOptions.level);
|
|
146
|
-
const consoleLevel = options.consoleLevel
|
|
147
|
-
? levelTransform(options.consoleLevel)
|
|
148
|
-
: levelTransform(defaultLoggerOptions.consoleLevel);
|
|
149
|
-
const dir = options['dir'] || defaultLoggerOptions.dir;
|
|
150
|
-
const logger = logger_1.loggers.createLogger(createLoggerKey || loggerKey, {
|
|
151
|
-
dir,
|
|
152
|
-
fileLogName: options.file,
|
|
153
|
-
errorLogName: defaultLoggerOptions.errorLogName,
|
|
154
|
-
level,
|
|
155
|
-
consoleLevel,
|
|
156
|
-
disableFile: level === null,
|
|
157
|
-
disableConsole: consoleLevel === null,
|
|
158
|
-
errorDir: dir,
|
|
159
|
-
});
|
|
124
|
+
createLogger(options, loggerKey) {
|
|
125
|
+
const logger = logger_1.loggers.createLogger(loggerKey, options);
|
|
160
126
|
// overwrite values for pandora collect
|
|
161
127
|
logger.values = () => {
|
|
162
128
|
return [];
|
|
@@ -185,7 +151,7 @@ class MidwayLoggers extends Map {
|
|
|
185
151
|
}
|
|
186
152
|
}
|
|
187
153
|
}
|
|
188
|
-
const createLoggers = (app) => {
|
|
154
|
+
const createLoggers = (app, processType) => {
|
|
189
155
|
const loggerConfig = app.config.logger;
|
|
190
156
|
loggerConfig.type = app.type;
|
|
191
157
|
if (app.config.env === 'prod' &&
|
|
@@ -193,22 +159,38 @@ const createLoggers = (app) => {
|
|
|
193
159
|
!loggerConfig.allowDebugAtProd) {
|
|
194
160
|
loggerConfig.level = 'INFO';
|
|
195
161
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
loggers = new MidwayLoggers(app.config, app);
|
|
199
|
-
}
|
|
200
|
-
else {
|
|
201
|
-
checkMidwayLoggerSymbolExistsAndRemove(app.config);
|
|
202
|
-
loggers = new egg_logger_1.EggLoggers(app.config);
|
|
203
|
-
}
|
|
162
|
+
// 现在只走 midway logger
|
|
163
|
+
const loggers = new MidwayLoggers(app.config, app, processType);
|
|
204
164
|
// won't print to console after started, except for local and unittest
|
|
205
165
|
app.ready(() => {
|
|
206
166
|
if (loggerConfig.disableConsoleAfterReady) {
|
|
207
167
|
loggers.disableConsole();
|
|
208
168
|
}
|
|
209
169
|
});
|
|
210
|
-
|
|
170
|
+
debug(`[egg]: create loggers in ${processType}`);
|
|
171
|
+
loggers['coreLogger'].info('[egg:logger] init all loggers with options: %j', loggerConfig);
|
|
211
172
|
return loggers;
|
|
212
173
|
};
|
|
213
174
|
exports.createLoggers = createLoggers;
|
|
175
|
+
class MidwayEggContextLogger extends logger_1.MidwayContextLogger {
|
|
176
|
+
formatContextLabel() {
|
|
177
|
+
const ctx = this.ctx;
|
|
178
|
+
// format: '[$userId/$ip/$traceId/$use_ms $method $url]'
|
|
179
|
+
const userId = ctx.userId || '-';
|
|
180
|
+
const traceId = (ctx.tracer && ctx.tracer.traceId) || '-';
|
|
181
|
+
const use = Date.now() - ctx.startTime;
|
|
182
|
+
return (userId +
|
|
183
|
+
'/' +
|
|
184
|
+
ctx.ip +
|
|
185
|
+
'/' +
|
|
186
|
+
traceId +
|
|
187
|
+
'/' +
|
|
188
|
+
use +
|
|
189
|
+
'ms ' +
|
|
190
|
+
ctx.method +
|
|
191
|
+
' ' +
|
|
192
|
+
ctx.url);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
exports.MidwayEggContextLogger = MidwayEggContextLogger;
|
|
214
196
|
//# sourceMappingURL=logger.js.map
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import { IMidwayBootstrapOptions } from '@midwayjs/core';
|
|
2
|
+
export declare function isTypeScriptEnvironment(): boolean;
|
|
1
3
|
export declare const parseNormalDir: (baseDir: string, isTypescript?: boolean) => {
|
|
2
4
|
baseDir: string;
|
|
3
5
|
appDir: string;
|
|
4
6
|
};
|
|
5
7
|
export declare const findLernaRoot: (findRoot?: string) => string;
|
|
6
8
|
export declare const getCurrentDateString: (timestamp?: number) => string;
|
|
9
|
+
export declare function initializeAgentApplicationContext(agent: any, globalOptions: Omit<IMidwayBootstrapOptions, 'applicationContext'>): Promise<import("@midwayjs/core").IMidwayContainer>;
|
|
7
10
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.js
CHANGED
|
@@ -1,26 +1,39 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getCurrentDateString = exports.findLernaRoot = exports.parseNormalDir = void 0;
|
|
4
|
-
const bootstrap_1 = require("@midwayjs/bootstrap");
|
|
3
|
+
exports.initializeAgentApplicationContext = exports.getCurrentDateString = exports.findLernaRoot = exports.parseNormalDir = exports.isTypeScriptEnvironment = void 0;
|
|
5
4
|
const path_1 = require("path");
|
|
6
5
|
const find_up_1 = require("find-up");
|
|
7
6
|
const fs_1 = require("fs");
|
|
7
|
+
const core_1 = require("@midwayjs/core");
|
|
8
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
9
|
+
const web_1 = require("./framework/web");
|
|
10
|
+
const util_1 = require("util");
|
|
11
|
+
const debug = (0, util_1.debuglog)('midway:debug');
|
|
12
|
+
function isTypeScriptEnvironment() {
|
|
13
|
+
const TS_MODE_PROCESS_FLAG = process.env.MIDWAY_TS_MODE;
|
|
14
|
+
if ('false' === TS_MODE_PROCESS_FLAG) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
// eslint-disable-next-line node/no-deprecated-api
|
|
18
|
+
return TS_MODE_PROCESS_FLAG === 'true' || !!require.extensions['.ts'];
|
|
19
|
+
}
|
|
20
|
+
exports.isTypeScriptEnvironment = isTypeScriptEnvironment;
|
|
8
21
|
const parseNormalDir = (baseDir, isTypescript = true) => {
|
|
9
22
|
if (isTypescript) {
|
|
10
23
|
// 这里要么就是 src 目录,要么就已经是根目录
|
|
11
|
-
if (!fs_1.existsSync(path_1.join(baseDir, 'package.json'))) {
|
|
12
|
-
baseDir = path_1.basename(baseDir);
|
|
24
|
+
if (!(0, fs_1.existsSync)((0, path_1.join)(baseDir, 'package.json'))) {
|
|
25
|
+
baseDir = (0, path_1.basename)(baseDir);
|
|
13
26
|
}
|
|
14
|
-
const isTypeScriptEnv =
|
|
27
|
+
const isTypeScriptEnv = isTypeScriptEnvironment();
|
|
15
28
|
if (isTypeScriptEnv) {
|
|
16
29
|
return {
|
|
17
|
-
baseDir: path_1.join(baseDir, 'src'),
|
|
30
|
+
baseDir: (0, path_1.join)(baseDir, 'src'),
|
|
18
31
|
appDir: baseDir,
|
|
19
32
|
};
|
|
20
33
|
}
|
|
21
34
|
else {
|
|
22
35
|
return {
|
|
23
|
-
baseDir: path_1.join(baseDir, 'dist'),
|
|
36
|
+
baseDir: (0, path_1.join)(baseDir, 'dist'),
|
|
24
37
|
appDir: baseDir,
|
|
25
38
|
};
|
|
26
39
|
}
|
|
@@ -36,8 +49,8 @@ const parseNormalDir = (baseDir, isTypescript = true) => {
|
|
|
36
49
|
exports.parseNormalDir = parseNormalDir;
|
|
37
50
|
const findLernaRoot = (findRoot = process.cwd()) => {
|
|
38
51
|
const userHome = process.env.HOME;
|
|
39
|
-
return find_up_1.sync(directory => {
|
|
40
|
-
if (find_up_1.sync.exists(path_1.join(directory, 'lerna.json'))) {
|
|
52
|
+
return (0, find_up_1.sync)(directory => {
|
|
53
|
+
if (find_up_1.sync.exists((0, path_1.join)(directory, 'lerna.json'))) {
|
|
41
54
|
return directory;
|
|
42
55
|
}
|
|
43
56
|
if (directory === userHome) {
|
|
@@ -53,4 +66,114 @@ const getCurrentDateString = (timestamp = Date.now()) => {
|
|
|
53
66
|
.padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}`;
|
|
54
67
|
};
|
|
55
68
|
exports.getCurrentDateString = getCurrentDateString;
|
|
69
|
+
async function initializeAgentApplicationContext(agent, globalOptions) {
|
|
70
|
+
var _a, _b, _c;
|
|
71
|
+
if (!(0, core_1.getCurrentApplicationContext)()) {
|
|
72
|
+
debug('[egg]: start "initializeGlobalApplicationContext"');
|
|
73
|
+
const appDir = (_a = globalOptions.appDir) !== null && _a !== void 0 ? _a : '';
|
|
74
|
+
const baseDir = (_b = globalOptions.baseDir) !== null && _b !== void 0 ? _b : '';
|
|
75
|
+
// new container
|
|
76
|
+
const applicationContext = new core_1.MidwayContainer();
|
|
77
|
+
debug('[egg]: delegate module map from decoratorManager');
|
|
78
|
+
(0, decorator_1.bindContainer)(applicationContext);
|
|
79
|
+
global['MIDWAY_APPLICATION_CONTEXT'] = applicationContext;
|
|
80
|
+
// register baseDir and appDir
|
|
81
|
+
applicationContext.registerObject('baseDir', baseDir);
|
|
82
|
+
applicationContext.registerObject('appDir', appDir);
|
|
83
|
+
if (globalOptions.moduleDirector !== false) {
|
|
84
|
+
if (globalOptions.moduleDetector === undefined ||
|
|
85
|
+
globalOptions.moduleDetector === 'file') {
|
|
86
|
+
applicationContext.setFileDetector(new core_1.DirectoryFileDetector({
|
|
87
|
+
loadDir: baseDir,
|
|
88
|
+
ignore: (_c = globalOptions.ignore) !== null && _c !== void 0 ? _c : [],
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
else if (globalOptions.moduleDetector) {
|
|
92
|
+
applicationContext.setFileDetector(globalOptions.moduleDetector);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// bind inner service
|
|
96
|
+
applicationContext.bindClass(core_1.MidwayEnvironmentService);
|
|
97
|
+
applicationContext.bindClass(core_1.MidwayInformationService);
|
|
98
|
+
applicationContext.bindClass(core_1.MidwayDecoratorService);
|
|
99
|
+
applicationContext.bindClass(core_1.MidwayConfigService);
|
|
100
|
+
applicationContext.bindClass(core_1.MidwayAspectService);
|
|
101
|
+
applicationContext.bindClass(core_1.MidwayLoggerService);
|
|
102
|
+
// bind preload module
|
|
103
|
+
if (globalOptions.preloadModules && globalOptions.preloadModules.length) {
|
|
104
|
+
for (const preloadModule of globalOptions.preloadModules) {
|
|
105
|
+
applicationContext.bindClass(preloadModule);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// init default config
|
|
109
|
+
const configService = await applicationContext.getAsync(core_1.MidwayConfigService);
|
|
110
|
+
// add egg config here, it will be ignore midway and component config
|
|
111
|
+
configService.add([
|
|
112
|
+
{
|
|
113
|
+
default: agent.config,
|
|
114
|
+
},
|
|
115
|
+
]);
|
|
116
|
+
// init aop support
|
|
117
|
+
const aspectService = await applicationContext.getAsync(core_1.MidwayAspectService, [applicationContext]);
|
|
118
|
+
// init decorator service
|
|
119
|
+
const decoratorService = await applicationContext.getAsync(core_1.MidwayDecoratorService, [applicationContext]);
|
|
120
|
+
if (!globalOptions.configurationModule) {
|
|
121
|
+
globalOptions.configurationModule = [
|
|
122
|
+
(0, core_1.safeRequire)((0, path_1.join)(globalOptions.baseDir, 'configuration')),
|
|
123
|
+
];
|
|
124
|
+
}
|
|
125
|
+
for (const configurationModule of [].concat(globalOptions.configurationModule)) {
|
|
126
|
+
// load configuration and component
|
|
127
|
+
applicationContext.load(configurationModule);
|
|
128
|
+
}
|
|
129
|
+
// bind user code module
|
|
130
|
+
await applicationContext.ready();
|
|
131
|
+
// merge config
|
|
132
|
+
await configService.load();
|
|
133
|
+
debug('[core]: Current config = %j', configService.getConfiguration());
|
|
134
|
+
// init logger
|
|
135
|
+
await applicationContext.getAsync(core_1.MidwayLoggerService, [
|
|
136
|
+
applicationContext,
|
|
137
|
+
]);
|
|
138
|
+
// framework/config/plugin/logger/app decorator support
|
|
139
|
+
// register base config hook
|
|
140
|
+
decoratorService.registerPropertyHandler(decorator_1.CONFIG_KEY, (propertyName, meta) => {
|
|
141
|
+
var _a;
|
|
142
|
+
if (meta.identifier === decorator_1.ALL) {
|
|
143
|
+
return this.configService.getConfiguration();
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
return this.configService.getConfiguration((_a = meta.identifier) !== null && _a !== void 0 ? _a : propertyName);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
// register @Logger decorator handler
|
|
150
|
+
decoratorService.registerPropertyHandler(decorator_1.LOGGER_KEY, (propertyName, meta) => {
|
|
151
|
+
var _a;
|
|
152
|
+
return this.loggerService.getLogger((_a = meta.identifier) !== null && _a !== void 0 ? _a : propertyName);
|
|
153
|
+
});
|
|
154
|
+
decoratorService.registerPropertyHandler(decorator_1.PIPELINE_IDENTIFIER, (key, meta, instance) => {
|
|
155
|
+
var _a, _b;
|
|
156
|
+
return new core_1.MidwayPipelineService((_b = (_a = instance[core_1.REQUEST_OBJ_CTX_KEY]) === null || _a === void 0 ? void 0 : _a.requestContext) !== null && _b !== void 0 ? _b : this.applicationContext, meta.valves);
|
|
157
|
+
});
|
|
158
|
+
// register @App decorator handler
|
|
159
|
+
decoratorService.registerPropertyHandler(decorator_1.APPLICATION_KEY, (propertyName, mete) => {
|
|
160
|
+
return agent;
|
|
161
|
+
});
|
|
162
|
+
decoratorService.registerPropertyHandler(decorator_1.PLUGIN_KEY, (key, target) => {
|
|
163
|
+
return this.agent[key];
|
|
164
|
+
});
|
|
165
|
+
// init aspect module
|
|
166
|
+
await aspectService.loadAspect();
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
debug('[egg]: "initializeAgentApplicationContext" ignore re-init in single process');
|
|
170
|
+
}
|
|
171
|
+
const applicationContext = (0, core_1.getCurrentApplicationContext)();
|
|
172
|
+
const agentFramework = new web_1.MidwayWebFramework(applicationContext);
|
|
173
|
+
agentFramework.app = agent;
|
|
174
|
+
agentFramework.configService = applicationContext.get(core_1.MidwayConfigService);
|
|
175
|
+
agentFramework.overwriteApplication('agent');
|
|
176
|
+
return applicationContext;
|
|
177
|
+
}
|
|
178
|
+
exports.initializeAgentApplicationContext = initializeAgentApplicationContext;
|
|
56
179
|
//# sourceMappingURL=utils.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/web",
|
|
3
|
-
"version": "3.0.0-
|
|
3
|
+
"version": "3.0.0-beta.4",
|
|
4
4
|
"description": "Midway Web Framework for Egg.js",
|
|
5
5
|
"main": "dist/index",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -27,11 +27,13 @@
|
|
|
27
27
|
],
|
|
28
28
|
"license": "MIT",
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@midwayjs/decorator": "^3.0.0-
|
|
31
|
-
"@midwayjs/mock": "^3.0.0-
|
|
32
|
-
"dayjs": "^1.10.
|
|
30
|
+
"@midwayjs/decorator": "^3.0.0-beta.4",
|
|
31
|
+
"@midwayjs/mock": "^3.0.0-beta.4",
|
|
32
|
+
"dayjs": "^1.10.7",
|
|
33
|
+
"egg-logger": "^2.4.2",
|
|
34
|
+
"egg-mock": "^3.26.0",
|
|
33
35
|
"egg-socket.io": "^4.1.6",
|
|
34
|
-
"egg-view-nunjucks": "^2.
|
|
36
|
+
"egg-view-nunjucks": "^2.3.0",
|
|
35
37
|
"fake-egg": "1.0.0",
|
|
36
38
|
"fs-extra": "^8.0.1",
|
|
37
39
|
"mm": "3",
|
|
@@ -41,17 +43,11 @@
|
|
|
41
43
|
},
|
|
42
44
|
"dependencies": {
|
|
43
45
|
"@eggjs/router": "^2.0.0",
|
|
44
|
-
"@midwayjs/
|
|
45
|
-
"@midwayjs/
|
|
46
|
-
"@midwayjs/koa": "^3.0.0-alpha.9+d5acc750",
|
|
47
|
-
"@midwayjs/logger": "^2.11.3",
|
|
48
|
-
"debug": "^4.1.1",
|
|
46
|
+
"@midwayjs/core": "^3.0.0-beta.4",
|
|
47
|
+
"@midwayjs/logger": "^3.0.0-beta.4",
|
|
49
48
|
"egg": "^2.28.0",
|
|
50
|
-
"egg-logger": "^2.4.2",
|
|
51
|
-
"egg-path-matching": "^1.0.1",
|
|
52
49
|
"extend2": "^1.0.0",
|
|
53
50
|
"find-up": "^5.0.0",
|
|
54
|
-
"midway-schedule": "^3.0.0-alpha.9+d5acc750",
|
|
55
51
|
"mkdirp": "^1.0.4"
|
|
56
52
|
},
|
|
57
53
|
"author": "Harry Chen <czy88840616@gmail.com>",
|
|
@@ -59,5 +55,8 @@
|
|
|
59
55
|
"type": "git",
|
|
60
56
|
"url": "http://github.com/midwayjs/midway.git"
|
|
61
57
|
},
|
|
62
|
-
"
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=12"
|
|
60
|
+
},
|
|
61
|
+
"gitHead": "02e2144e302f807770b512b0d89da3145b1cbf2e"
|
|
63
62
|
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { IMidwayBootstrapOptions, IMidwayContainer, IMidwayFramework, MidwayFrameworkType } from '@midwayjs/core';
|
|
3
|
-
import { IMidwayWebConfigurationOptions, IMidwayWebApplication } from '../interface';
|
|
4
|
-
import { Server } from 'net';
|
|
5
|
-
import { LoggerOptions } from '@midwayjs/logger';
|
|
6
|
-
import { MidwayKoaContextLogger } from '@midwayjs/koa';
|
|
7
|
-
export declare class MidwayWebSingleProcessFramework implements IMidwayFramework<IMidwayWebApplication, IMidwayWebConfigurationOptions> {
|
|
8
|
-
app: IMidwayWebApplication;
|
|
9
|
-
agent: any;
|
|
10
|
-
configurationOptions: IMidwayWebConfigurationOptions;
|
|
11
|
-
private isTsMode;
|
|
12
|
-
private server;
|
|
13
|
-
getApplication(): IMidwayWebApplication;
|
|
14
|
-
getFrameworkType(): MidwayFrameworkType;
|
|
15
|
-
run(): Promise<void>;
|
|
16
|
-
configure(options?: IMidwayWebConfigurationOptions): MidwayWebSingleProcessFramework;
|
|
17
|
-
getApplicationContext(): IMidwayContainer;
|
|
18
|
-
getConfiguration(key?: string): any;
|
|
19
|
-
getCurrentEnvironment(): string;
|
|
20
|
-
initialize(options: Partial<IMidwayBootstrapOptions>): Promise<void>;
|
|
21
|
-
loadExtension(): Promise<void>;
|
|
22
|
-
stop(): Promise<void>;
|
|
23
|
-
getBaseDir(): string;
|
|
24
|
-
getAppDir(): string;
|
|
25
|
-
getLogger(name?: string): import("@midwayjs/logger").ILogger;
|
|
26
|
-
getCoreLogger(): import("egg-logger").EggLogger;
|
|
27
|
-
getProjectName(): string;
|
|
28
|
-
createLogger(name: string, options?: LoggerOptions): import("@midwayjs/logger").ILogger;
|
|
29
|
-
getServer(): Server;
|
|
30
|
-
getFrameworkName(): string;
|
|
31
|
-
getDefaultContextLoggerClass(): typeof MidwayKoaContextLogger;
|
|
32
|
-
loadLifeCycles(): void;
|
|
33
|
-
}
|
|
34
|
-
//# sourceMappingURL=singleProcess.d.ts.map
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MidwayWebSingleProcessFramework = void 0;
|
|
4
|
-
const core_1 = require("@midwayjs/core");
|
|
5
|
-
const path_1 = require("path");
|
|
6
|
-
const koa_1 = require("@midwayjs/koa");
|
|
7
|
-
class MidwayWebSingleProcessFramework {
|
|
8
|
-
getApplication() {
|
|
9
|
-
return this.app;
|
|
10
|
-
}
|
|
11
|
-
getFrameworkType() {
|
|
12
|
-
return core_1.MidwayFrameworkType.WEB;
|
|
13
|
-
}
|
|
14
|
-
async run() {
|
|
15
|
-
// trigger server didReady
|
|
16
|
-
this.app.messenger.emit('egg-ready');
|
|
17
|
-
if (this.configurationOptions.port) {
|
|
18
|
-
new Promise(resolve => {
|
|
19
|
-
const args = [this.configurationOptions.port];
|
|
20
|
-
if (this.configurationOptions.hostname) {
|
|
21
|
-
args.push(this.configurationOptions.hostname);
|
|
22
|
-
}
|
|
23
|
-
args.push(() => {
|
|
24
|
-
resolve();
|
|
25
|
-
});
|
|
26
|
-
this.server.listen(...args);
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
configure(options = {}) {
|
|
31
|
-
this.configurationOptions = options;
|
|
32
|
-
return this;
|
|
33
|
-
}
|
|
34
|
-
getApplicationContext() {
|
|
35
|
-
return this.app.getApplicationContext();
|
|
36
|
-
}
|
|
37
|
-
getConfiguration(key) {
|
|
38
|
-
return this.app.getConfig(key);
|
|
39
|
-
}
|
|
40
|
-
getCurrentEnvironment() {
|
|
41
|
-
return this.app.getEnv();
|
|
42
|
-
}
|
|
43
|
-
async initialize(options) {
|
|
44
|
-
const opts = {
|
|
45
|
-
baseDir: options.appDir,
|
|
46
|
-
framework: path_1.resolve(__dirname, '../application'),
|
|
47
|
-
plugins: this.configurationOptions.plugins,
|
|
48
|
-
mode: 'single',
|
|
49
|
-
isTsMode: this.isTsMode || true,
|
|
50
|
-
applicationContext: options.applicationContext,
|
|
51
|
-
midwaySingleton: true,
|
|
52
|
-
};
|
|
53
|
-
const Agent = require(opts.framework).Agent;
|
|
54
|
-
const Application = require(opts.framework).Application;
|
|
55
|
-
const agent = (this.agent = new Agent(Object.assign({}, opts)));
|
|
56
|
-
await agent.ready();
|
|
57
|
-
const application = (this.app = new Application(Object.assign({}, opts)));
|
|
58
|
-
application.agent = agent;
|
|
59
|
-
agent.application = application;
|
|
60
|
-
// https config
|
|
61
|
-
if (this.configurationOptions.key && this.configurationOptions.cert) {
|
|
62
|
-
this.configurationOptions.key = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.key);
|
|
63
|
-
this.configurationOptions.cert = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.cert);
|
|
64
|
-
this.configurationOptions.ca = core_1.PathFileUtil.getFileContentSync(this.configurationOptions.ca);
|
|
65
|
-
this.server = require('https').createServer(this.configurationOptions, this.app.callback());
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
this.server = require('http').createServer(this.app.callback());
|
|
69
|
-
}
|
|
70
|
-
if (options.isMainFramework === undefined) {
|
|
71
|
-
await this.loadExtension();
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
async loadExtension() {
|
|
75
|
-
// 延迟加载 egg 的 load 方法
|
|
76
|
-
await this.app.loader.loadOrigin();
|
|
77
|
-
await this.app.ready();
|
|
78
|
-
// emit egg-ready message in agent and application
|
|
79
|
-
this.app.messenger.broadcast('egg-ready', undefined);
|
|
80
|
-
// emit `server` event in app
|
|
81
|
-
this.app.emit('server', this.server);
|
|
82
|
-
// register httpServer to applicationContext
|
|
83
|
-
this.getApplicationContext().registerObject(core_1.HTTP_SERVER_KEY, this.server);
|
|
84
|
-
}
|
|
85
|
-
async stop() {
|
|
86
|
-
await new Promise(resolve => {
|
|
87
|
-
this.server.close(resolve);
|
|
88
|
-
});
|
|
89
|
-
await this.app.close();
|
|
90
|
-
await this.agent.close();
|
|
91
|
-
}
|
|
92
|
-
getBaseDir() {
|
|
93
|
-
return this.app.getBaseDir();
|
|
94
|
-
}
|
|
95
|
-
getAppDir() {
|
|
96
|
-
return this.app.getAppDir();
|
|
97
|
-
}
|
|
98
|
-
getLogger(name) {
|
|
99
|
-
return this.app.getLogger(name);
|
|
100
|
-
}
|
|
101
|
-
getCoreLogger() {
|
|
102
|
-
return this.app.coreLogger;
|
|
103
|
-
}
|
|
104
|
-
getProjectName() {
|
|
105
|
-
return this.app.getProjectName();
|
|
106
|
-
}
|
|
107
|
-
createLogger(name, options) {
|
|
108
|
-
return this.app.createLogger(name, options);
|
|
109
|
-
}
|
|
110
|
-
getServer() {
|
|
111
|
-
return this.server;
|
|
112
|
-
}
|
|
113
|
-
getFrameworkName() {
|
|
114
|
-
return 'midway:web';
|
|
115
|
-
}
|
|
116
|
-
getDefaultContextLoggerClass() {
|
|
117
|
-
return koa_1.MidwayKoaContextLogger;
|
|
118
|
-
}
|
|
119
|
-
loadLifeCycles() { }
|
|
120
|
-
}
|
|
121
|
-
exports.MidwayWebSingleProcessFramework = MidwayWebSingleProcessFramework;
|
|
122
|
-
//# sourceMappingURL=singleProcess.js.map
|
package/dist/starter.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { BootstrapStarter } from '@midwayjs/bootstrap';
|
|
2
|
-
interface WebStarterOptions {
|
|
3
|
-
isWorker: boolean;
|
|
4
|
-
applicationContext?: any;
|
|
5
|
-
}
|
|
6
|
-
export declare class WebBootstrapStarter extends BootstrapStarter {
|
|
7
|
-
options: WebStarterOptions;
|
|
8
|
-
constructor(options: WebStarterOptions);
|
|
9
|
-
init(): Promise<void>;
|
|
10
|
-
}
|
|
11
|
-
export {};
|
|
12
|
-
//# sourceMappingURL=starter.d.ts.map
|