@eggjs/core 6.0.2 → 6.1.0
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/README.md +1 -0
- package/dist/commonjs/egg.d.ts +4 -3
- package/dist/commonjs/egg.js +8 -19
- package/dist/commonjs/lifecycle.d.ts +2 -1
- package/dist/commonjs/lifecycle.js +10 -30
- package/dist/commonjs/loader/egg_loader.js +36 -16
- package/dist/commonjs/utils/index.d.ts +3 -1
- package/dist/commonjs/utils/index.js +49 -32
- package/dist/esm/egg.d.ts +4 -3
- package/dist/esm/egg.js +7 -18
- package/dist/esm/lifecycle.d.ts +2 -1
- package/dist/esm/lifecycle.js +9 -6
- package/dist/esm/loader/egg_loader.js +36 -16
- package/dist/esm/utils/index.d.ts +3 -1
- package/dist/esm/utils/index.js +49 -32
- package/dist/package.json +4 -0
- package/package.json +5 -6
- package/src/egg.ts +8 -6
- package/src/lifecycle.ts +10 -5
- package/src/loader/egg_loader.ts +43 -17
- package/src/utils/index.ts +50 -33
package/dist/esm/utils/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { debuglog } from 'node:util';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import fs from 'node:fs';
|
|
4
|
+
import { stat } from 'node:fs/promises';
|
|
4
5
|
import BuiltinModule from 'node:module';
|
|
5
6
|
import { importResolve, importModule } from '@eggjs/utils';
|
|
6
7
|
const debug = debuglog('@eggjs/core:utils');
|
|
@@ -12,12 +13,57 @@ const Module = typeof module !== 'undefined' && module.constructor.length > 1
|
|
|
12
13
|
const extensions = Module._extensions;
|
|
13
14
|
const extensionNames = Object.keys(extensions).concat(['.cjs', '.mjs']);
|
|
14
15
|
debug('Module extensions: %j', extensionNames);
|
|
16
|
+
function getCalleeFromStack(withLine, stackIndex) {
|
|
17
|
+
stackIndex = stackIndex === undefined ? 2 : stackIndex;
|
|
18
|
+
const limit = Error.stackTraceLimit;
|
|
19
|
+
const prep = Error.prepareStackTrace;
|
|
20
|
+
Error.prepareStackTrace = prepareObjectStackTrace;
|
|
21
|
+
Error.stackTraceLimit = 5;
|
|
22
|
+
// capture the stack
|
|
23
|
+
const obj = {};
|
|
24
|
+
Error.captureStackTrace(obj);
|
|
25
|
+
let callSite = obj.stack[stackIndex];
|
|
26
|
+
let fileName = '';
|
|
27
|
+
if (callSite) {
|
|
28
|
+
// egg-mock will create a proxy
|
|
29
|
+
// https://github.com/eggjs/egg-mock/blob/master/lib/app.js#L174
|
|
30
|
+
fileName = callSite.getFileName();
|
|
31
|
+
/* istanbul ignore if */
|
|
32
|
+
if (fileName && fileName.endsWith('egg-mock/lib/app.js')) {
|
|
33
|
+
// TODO: add test
|
|
34
|
+
callSite = obj.stack[stackIndex + 1];
|
|
35
|
+
fileName = callSite.getFileName();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
Error.prepareStackTrace = prep;
|
|
39
|
+
Error.stackTraceLimit = limit;
|
|
40
|
+
if (!callSite || !fileName)
|
|
41
|
+
return '<anonymous>';
|
|
42
|
+
if (!withLine)
|
|
43
|
+
return fileName;
|
|
44
|
+
return `${fileName}:${callSite.getLineNumber()}:${callSite.getColumnNumber()}`;
|
|
45
|
+
}
|
|
15
46
|
export default {
|
|
16
47
|
deprecated(message) {
|
|
17
|
-
|
|
48
|
+
if (debug.enabled) {
|
|
49
|
+
console.trace('[@eggjs/core:deprecated] %s', message);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
console.warn('[@eggjs/core:deprecated] %s', message);
|
|
53
|
+
console.warn('[@eggjs/core:deprecated] set NODE_DEBUG=@eggjs/core:utils can show call stack');
|
|
54
|
+
}
|
|
18
55
|
},
|
|
19
56
|
extensions,
|
|
20
57
|
extensionNames,
|
|
58
|
+
async existsPath(filepath) {
|
|
59
|
+
try {
|
|
60
|
+
await stat(filepath);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
},
|
|
21
67
|
async loadFile(filepath) {
|
|
22
68
|
try {
|
|
23
69
|
// if not js module, just return content buffer
|
|
@@ -45,36 +91,7 @@ export default {
|
|
|
45
91
|
return;
|
|
46
92
|
return ctx ? fn.call(ctx, ...args) : fn(...args);
|
|
47
93
|
},
|
|
48
|
-
getCalleeFromStack
|
|
49
|
-
stackIndex = stackIndex === undefined ? 2 : stackIndex;
|
|
50
|
-
const limit = Error.stackTraceLimit;
|
|
51
|
-
const prep = Error.prepareStackTrace;
|
|
52
|
-
Error.prepareStackTrace = prepareObjectStackTrace;
|
|
53
|
-
Error.stackTraceLimit = 5;
|
|
54
|
-
// capture the stack
|
|
55
|
-
const obj = {};
|
|
56
|
-
Error.captureStackTrace(obj);
|
|
57
|
-
let callSite = obj.stack[stackIndex];
|
|
58
|
-
let fileName = '';
|
|
59
|
-
if (callSite) {
|
|
60
|
-
// egg-mock will create a proxy
|
|
61
|
-
// https://github.com/eggjs/egg-mock/blob/master/lib/app.js#L174
|
|
62
|
-
fileName = callSite.getFileName();
|
|
63
|
-
/* istanbul ignore if */
|
|
64
|
-
if (fileName && fileName.endsWith('egg-mock/lib/app.js')) {
|
|
65
|
-
// TODO: add test
|
|
66
|
-
callSite = obj.stack[stackIndex + 1];
|
|
67
|
-
fileName = callSite.getFileName();
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
Error.prepareStackTrace = prep;
|
|
71
|
-
Error.stackTraceLimit = limit;
|
|
72
|
-
if (!callSite || !fileName)
|
|
73
|
-
return '<anonymous>';
|
|
74
|
-
if (!withLine)
|
|
75
|
-
return fileName;
|
|
76
|
-
return `${fileName}:${callSite.getLineNumber()}:${callSite.getColumnNumber()}`;
|
|
77
|
-
},
|
|
94
|
+
getCalleeFromStack,
|
|
78
95
|
getResolvedFilename(filepath, baseDir) {
|
|
79
96
|
const reg = /[/\\]/g;
|
|
80
97
|
return filepath.replace(baseDir + path.sep, '').replace(reg, '/');
|
|
@@ -87,4 +104,4 @@ export default {
|
|
|
87
104
|
function prepareObjectStackTrace(_obj, stack) {
|
|
88
105
|
return stack;
|
|
89
106
|
}
|
|
90
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
107
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvdXRpbHMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFFBQVEsRUFBRSxNQUFNLFdBQVcsQ0FBQztBQUNyQyxPQUFPLElBQUksTUFBTSxXQUFXLENBQUM7QUFDN0IsT0FBTyxFQUFFLE1BQU0sU0FBUyxDQUFDO0FBQ3pCLE9BQU8sRUFBRSxJQUFJLEVBQUUsTUFBTSxrQkFBa0IsQ0FBQztBQUN4QyxPQUFPLGFBQWEsTUFBTSxhQUFhLENBQUM7QUFDeEMsT0FBTyxFQUFFLGFBQWEsRUFBRSxZQUFZLEVBQUUsTUFBTSxjQUFjLENBQUM7QUFFM0QsTUFBTSxLQUFLLEdBQUcsUUFBUSxDQUFDLG1CQUFtQixDQUFDLENBQUM7QUFJNUMsbURBQW1EO0FBQ25ELE1BQU0sTUFBTSxHQUFHLE9BQU8sTUFBTSxLQUFLLFdBQVcsSUFBSSxNQUFNLENBQUMsV0FBVyxDQUFDLE1BQU0sR0FBRyxDQUFDO0lBQzNFLENBQUMsQ0FBQyxNQUFNLENBQUMsV0FBVztJQUNwQiwwQkFBMEI7SUFDMUIsQ0FBQyxDQUFDLGFBQWEsQ0FBQztBQUVsQixNQUFNLFVBQVUsR0FBSSxNQUFjLENBQUMsV0FBVyxDQUFDO0FBQy9DLE1BQU0sY0FBYyxHQUFHLE1BQU0sQ0FBQyxJQUFJLENBQUMsVUFBVSxDQUFDLENBQUMsTUFBTSxDQUFDLENBQUUsTUFBTSxFQUFFLE1BQU0sQ0FBRSxDQUFDL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HQUFHLElBQUksSUFBSSxFQUFFLENBQUM7UUFDbEIsSUFBSSxPQUFPLEVBQUUsS0FBSyxVQUFVO1lBQUUsT0FBTztRQUNyQyxPQUFPLEdBQUcsQ0FBQyxDQUFDLENBQUMsRUFBRSxDQUFDLElBQUksQ0FBQyxHQUFHLEVBQUUsR0FBRyxJQUFJLENBQUMsQ0FBQyxDQUFDLENBQUMsRUFBRSxDQUFDLEdBQUcsSUFBSSxDQUFDLENBQUM7SUFDbkQsQ0FBQztJQUVELGtCQUFrQjtJQUVsQixtQkFBbUIsQ0FBQyxRQUFnQixFQUFFLE9BQWU7UUFDbkQsTUFBTSxHQUFHLEdBQUcsUUFBUSxDQUFDO1FBQ3JCLE9BQU8sUUFBUSxDQUFDLE9BQU8sQ0FBQyxPQUFPLEdBQUcsSUFBSSxDQUFDLEdBQUcsRUFBRSxFQUFFLENBQUMsQ0FBQyxPQUFPLENBQUMsR0FBRyxFQUFFLEdBQUcsQ0FBQyxDQUFDO0lBQ3BFLENBQUM7Q0FDRixDQUFDO0FBRUY7OztHQUdHO0FBQ0gsU0FBUyx1QkFBdUIsQ0FBQyxJQUFTLEVBQUUsS0FBVTtJQUNwRCxPQUFPLEtBQUssQ0FBQztBQUNmLENBQUMifQ==
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eggjs/core",
|
|
3
|
-
"version": "6.0
|
|
3
|
+
"version": "6.1.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"homepage": "https://github.com/eggjs/egg-core#readme",
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@eggjs/koa": "^2.
|
|
38
|
+
"@eggjs/koa": "^2.19.2",
|
|
39
39
|
"@eggjs/router": "^3.0.5",
|
|
40
40
|
"@eggjs/utils": "^4.0.2",
|
|
41
41
|
"egg-logger": "^3.5.0",
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"mm": "3",
|
|
67
67
|
"supertest": "7",
|
|
68
68
|
"ts-node": "10",
|
|
69
|
-
"tshy": "
|
|
69
|
+
"tshy": "3",
|
|
70
70
|
"tshy-after": "1",
|
|
71
71
|
"typescript": "5",
|
|
72
72
|
"urllib": "4"
|
|
@@ -86,17 +86,16 @@
|
|
|
86
86
|
"./package.json": "./package.json",
|
|
87
87
|
".": {
|
|
88
88
|
"import": {
|
|
89
|
-
"source": "./src/index.ts",
|
|
90
89
|
"types": "./dist/esm/index.d.ts",
|
|
91
90
|
"default": "./dist/esm/index.js"
|
|
92
91
|
},
|
|
93
92
|
"require": {
|
|
94
|
-
"source": "./src/index.ts",
|
|
95
93
|
"types": "./dist/commonjs/index.d.ts",
|
|
96
94
|
"default": "./dist/commonjs/index.js"
|
|
97
95
|
}
|
|
98
96
|
}
|
|
99
97
|
},
|
|
100
98
|
"main": "./dist/commonjs/index.js",
|
|
101
|
-
"types": "./dist/commonjs/index.d.ts"
|
|
99
|
+
"types": "./dist/commonjs/index.d.ts",
|
|
100
|
+
"module": "./dist/esm/index.js"
|
|
102
101
|
}
|
package/src/egg.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/* eslint-disable prefer-spread */
|
|
2
2
|
import assert from 'node:assert';
|
|
3
3
|
import { debuglog } from 'node:util';
|
|
4
|
-
import
|
|
5
|
-
import KoaApplication from '@eggjs/koa';
|
|
4
|
+
import { Application as KoaApplication } from '@eggjs/koa';
|
|
6
5
|
import type { ContextDelegation, MiddlewareFunc } from '@eggjs/koa';
|
|
7
6
|
import { EggConsoleLogger } from 'egg-logger';
|
|
8
7
|
import { RegisterOptions, ResourcesController, EggRouter as Router } from '@eggjs/router';
|
|
@@ -157,10 +156,8 @@ export class EggCore extends KoaApplication {
|
|
|
157
156
|
* override koa's app.use, support generator function
|
|
158
157
|
* @since 1.0.0
|
|
159
158
|
*/
|
|
160
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
161
|
-
// @ts-ignore
|
|
162
159
|
use(fn: MiddlewareFunc) {
|
|
163
|
-
assert(
|
|
160
|
+
assert(typeof fn === 'function', 'app.use() requires a function');
|
|
164
161
|
debug('[use] add middleware: %o', fn._name || fn.name || '-');
|
|
165
162
|
this.middleware.push(fn);
|
|
166
163
|
return this;
|
|
@@ -226,7 +223,7 @@ export class EggCore extends KoaApplication {
|
|
|
226
223
|
* Execute scope after loaded and before app start.
|
|
227
224
|
*
|
|
228
225
|
* Notice:
|
|
229
|
-
* This method is now NOT
|
|
226
|
+
* This method is now NOT recommended and regarded as a deprecated one,
|
|
230
227
|
* For plugin development, we should use `didLoad` instead.
|
|
231
228
|
* For application development, we should use `willReady` instead.
|
|
232
229
|
*
|
|
@@ -251,7 +248,12 @@ export class EggCore extends KoaApplication {
|
|
|
251
248
|
* console.log('done');
|
|
252
249
|
* });
|
|
253
250
|
*/
|
|
251
|
+
ready(): Promise<void>;
|
|
252
|
+
ready(flagOrFunction: ReadyFunctionArg): void;
|
|
254
253
|
ready(flagOrFunction?: ReadyFunctionArg) {
|
|
254
|
+
if (flagOrFunction === undefined) {
|
|
255
|
+
return this.lifecycle.ready();
|
|
256
|
+
}
|
|
255
257
|
return this.lifecycle.ready(flagOrFunction);
|
|
256
258
|
}
|
|
257
259
|
|
package/src/lifecycle.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import assert from 'node:assert';
|
|
2
2
|
import { EventEmitter } from 'node:events';
|
|
3
3
|
import { debuglog } from 'node:util';
|
|
4
|
-
import
|
|
5
|
-
import ReadyObject from 'get-ready';
|
|
4
|
+
import { isClass } from 'is-type-of';
|
|
5
|
+
import { Ready as ReadyObject } from 'get-ready';
|
|
6
6
|
import type { ReadyFunctionArg } from 'get-ready';
|
|
7
7
|
import { Ready } from 'ready-callback';
|
|
8
8
|
import { EggConsoleLogger } from 'egg-logger';
|
|
@@ -109,8 +109,13 @@ export class Lifecycle extends EventEmitter {
|
|
|
109
109
|
});
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
ready(
|
|
113
|
-
|
|
112
|
+
ready(): Promise<void>;
|
|
113
|
+
ready(flagOrFunction: ReadyFunctionArg): void;
|
|
114
|
+
ready(flagOrFunction?: ReadyFunctionArg) {
|
|
115
|
+
if (flagOrFunction === undefined) {
|
|
116
|
+
return this.#readyObject.ready();
|
|
117
|
+
}
|
|
118
|
+
return this.#readyObject.ready(flagOrFunction);
|
|
114
119
|
}
|
|
115
120
|
|
|
116
121
|
get app() {
|
|
@@ -184,7 +189,7 @@ export class Lifecycle extends EventEmitter {
|
|
|
184
189
|
}
|
|
185
190
|
|
|
186
191
|
registerBeforeClose(fn: Fun) {
|
|
187
|
-
assert(
|
|
192
|
+
assert(typeof fn === 'function', 'argument should be function');
|
|
188
193
|
assert(this.#isClosed === false, 'app has been closed');
|
|
189
194
|
this.#closeFunctionSet.add(fn);
|
|
190
195
|
}
|
package/src/loader/egg_loader.ts
CHANGED
|
@@ -3,9 +3,9 @@ import path from 'node:path';
|
|
|
3
3
|
import assert from 'node:assert';
|
|
4
4
|
import { debuglog, inspect } from 'node:util';
|
|
5
5
|
import { isAsyncFunction, isClass, isGeneratorFunction, isObject } from 'is-type-of';
|
|
6
|
-
import homedir from 'node-homedir';
|
|
6
|
+
import { homedir } from 'node-homedir';
|
|
7
7
|
import type { Logger } from 'egg-logger';
|
|
8
|
-
import { getParamNames, readJSONSync } from 'utility';
|
|
8
|
+
import { getParamNames, readJSONSync, readJSON } from 'utility';
|
|
9
9
|
import { extend } from 'extend2';
|
|
10
10
|
import { Request, Response, Context, Application } from '@eggjs/koa';
|
|
11
11
|
import { pathMatching, type PathMatchingOptions } from 'egg-path-matching';
|
|
@@ -462,7 +462,7 @@ export class EggLoader {
|
|
|
462
462
|
plugin.path = this.getPluginPath(plugin);
|
|
463
463
|
|
|
464
464
|
// read plugin information from ${plugin.path}/package.json
|
|
465
|
-
this.#mergePluginConfig(plugin);
|
|
465
|
+
await this.#mergePluginConfig(plugin);
|
|
466
466
|
|
|
467
467
|
// disable the plugin that not match the serverEnv
|
|
468
468
|
if (env && plugin.env.length > 0 && !plugin.env.includes(env)) {
|
|
@@ -538,7 +538,7 @@ export class EggLoader {
|
|
|
538
538
|
for (const name in customPlugins) {
|
|
539
539
|
this.#normalizePluginConfig(customPlugins, name, configPath);
|
|
540
540
|
}
|
|
541
|
-
debug('Loaded custom plugins: %
|
|
541
|
+
debug('Loaded custom plugins: %o', customPlugins);
|
|
542
542
|
}
|
|
543
543
|
return customPlugins;
|
|
544
544
|
}
|
|
@@ -623,16 +623,18 @@ export class EggLoader {
|
|
|
623
623
|
// "strict": true, whether check plugin name, default to true.
|
|
624
624
|
// }
|
|
625
625
|
// }
|
|
626
|
-
#mergePluginConfig(plugin: EggPluginInfo) {
|
|
626
|
+
async #mergePluginConfig(plugin: EggPluginInfo) {
|
|
627
627
|
let pkg;
|
|
628
628
|
let config;
|
|
629
629
|
const pluginPackage = path.join(plugin.path!, 'package.json');
|
|
630
|
-
if (
|
|
631
|
-
pkg =
|
|
630
|
+
if (await utils.existsPath(pluginPackage)) {
|
|
631
|
+
pkg = await readJSON(pluginPackage);
|
|
632
632
|
config = pkg.eggPlugin;
|
|
633
633
|
if (pkg.version) {
|
|
634
634
|
plugin.version = pkg.version;
|
|
635
635
|
}
|
|
636
|
+
// support commonjs and esm dist files
|
|
637
|
+
plugin.path = this.#formatPluginPathFromPackageJSON(plugin.path!, pkg);
|
|
636
638
|
}
|
|
637
639
|
|
|
638
640
|
const logger = this.options.logger;
|
|
@@ -712,9 +714,9 @@ export class EggLoader {
|
|
|
712
714
|
}
|
|
713
715
|
|
|
714
716
|
// Following plugins will be enabled implicitly.
|
|
715
|
-
// - configclient required by [
|
|
716
|
-
// -
|
|
717
|
-
// - diamond required by [
|
|
717
|
+
// - configclient required by [rpcClient]
|
|
718
|
+
// - monitor required by [rpcClient]
|
|
719
|
+
// - diamond required by [rpcClient]
|
|
718
720
|
if (implicitEnabledPlugins.length) {
|
|
719
721
|
let message = implicitEnabledPlugins
|
|
720
722
|
.map(name => ` - ${name} required by [${requireMap[name]}]`)
|
|
@@ -769,7 +771,6 @@ export class EggLoader {
|
|
|
769
771
|
|
|
770
772
|
#resolvePluginPath(plugin: EggPluginInfo) {
|
|
771
773
|
const name = plugin.package || plugin.name;
|
|
772
|
-
|
|
773
774
|
try {
|
|
774
775
|
// should find the plugin directory
|
|
775
776
|
// pnpm will lift the node_modules to the sibling directory
|
|
@@ -777,12 +778,36 @@ export class EggLoader {
|
|
|
777
778
|
// 'node_modules/.pnpm/yadan@2.0.0/node_modules', <- this is the sibling directory
|
|
778
779
|
// 'node_modules/.pnpm/egg@2.33.1/node_modules/egg/node_modules',
|
|
779
780
|
// 'node_modules/.pnpm/egg@2.33.1/node_modules', <- this is the sibling directory
|
|
780
|
-
const
|
|
781
|
-
return path.dirname(
|
|
782
|
-
} catch (err
|
|
781
|
+
const pluginPkgFile = utils.resolvePath(`${name}/package.json`, { paths: [ ...this.lookupDirs ] });
|
|
782
|
+
return path.dirname(pluginPkgFile);
|
|
783
|
+
} catch (err) {
|
|
783
784
|
debug('[resolvePluginPath] error: %o', err);
|
|
784
|
-
throw new Error(`Can not find plugin ${name} in "${[ ...this.lookupDirs ].join(', ')}"
|
|
785
|
+
throw new Error(`Can not find plugin ${name} in "${[ ...this.lookupDirs ].join(', ')}"`, {
|
|
786
|
+
cause: err,
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
#formatPluginPathFromPackageJSON(pluginPath: string, pluginPkg: {
|
|
792
|
+
eggPlugin?: {
|
|
793
|
+
exports?: {
|
|
794
|
+
import?: string;
|
|
795
|
+
require?: string;
|
|
796
|
+
};
|
|
797
|
+
};
|
|
798
|
+
}) {
|
|
799
|
+
if (pluginPkg.eggPlugin?.exports) {
|
|
800
|
+
if (typeof require === 'function') {
|
|
801
|
+
if (pluginPkg.eggPlugin.exports.require) {
|
|
802
|
+
pluginPath = path.join(pluginPath, pluginPkg.eggPlugin.exports.require);
|
|
803
|
+
}
|
|
804
|
+
} else {
|
|
805
|
+
if (pluginPkg.eggPlugin.exports.import) {
|
|
806
|
+
pluginPath = path.join(pluginPath, pluginPkg.eggPlugin.exports.import);
|
|
807
|
+
}
|
|
808
|
+
}
|
|
785
809
|
}
|
|
810
|
+
return pluginPath;
|
|
786
811
|
}
|
|
787
812
|
|
|
788
813
|
#extendPlugins(targets: Record<string, EggPluginInfo>, plugins: Record<string, EggPluginInfo>) {
|
|
@@ -1036,9 +1061,10 @@ export class EggLoader {
|
|
|
1036
1061
|
debug('loadExtend %s, filepaths: %j', name, filepaths);
|
|
1037
1062
|
|
|
1038
1063
|
const mergeRecord = new Map();
|
|
1039
|
-
for (
|
|
1040
|
-
filepath = this.resolveModule(
|
|
1064
|
+
for (const rawFilepath of filepaths) {
|
|
1065
|
+
const filepath = this.resolveModule(rawFilepath)!;
|
|
1041
1066
|
if (!filepath) {
|
|
1067
|
+
debug('loadExtend %o not found', rawFilepath);
|
|
1042
1068
|
continue;
|
|
1043
1069
|
}
|
|
1044
1070
|
if (filepath.endsWith('/index.js')) {
|
package/src/utils/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { debuglog } from 'node:util';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import fs from 'node:fs';
|
|
4
|
+
import { stat } from 'node:fs/promises';
|
|
4
5
|
import BuiltinModule from 'node:module';
|
|
5
6
|
import { importResolve, importModule } from '@eggjs/utils';
|
|
6
7
|
|
|
@@ -18,14 +19,61 @@ const extensions = (Module as any)._extensions;
|
|
|
18
19
|
const extensionNames = Object.keys(extensions).concat([ '.cjs', '.mjs' ]);
|
|
19
20
|
debug('Module extensions: %j', extensionNames);
|
|
20
21
|
|
|
22
|
+
function getCalleeFromStack(withLine?: boolean, stackIndex?: number) {
|
|
23
|
+
stackIndex = stackIndex === undefined ? 2 : stackIndex;
|
|
24
|
+
const limit = Error.stackTraceLimit;
|
|
25
|
+
const prep = Error.prepareStackTrace;
|
|
26
|
+
|
|
27
|
+
Error.prepareStackTrace = prepareObjectStackTrace;
|
|
28
|
+
Error.stackTraceLimit = 5;
|
|
29
|
+
|
|
30
|
+
// capture the stack
|
|
31
|
+
const obj: any = {};
|
|
32
|
+
Error.captureStackTrace(obj);
|
|
33
|
+
let callSite = obj.stack[stackIndex];
|
|
34
|
+
let fileName = '';
|
|
35
|
+
if (callSite) {
|
|
36
|
+
// egg-mock will create a proxy
|
|
37
|
+
// https://github.com/eggjs/egg-mock/blob/master/lib/app.js#L174
|
|
38
|
+
fileName = callSite.getFileName();
|
|
39
|
+
/* istanbul ignore if */
|
|
40
|
+
if (fileName && fileName.endsWith('egg-mock/lib/app.js')) {
|
|
41
|
+
// TODO: add test
|
|
42
|
+
callSite = obj.stack[stackIndex + 1];
|
|
43
|
+
fileName = callSite.getFileName();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
Error.prepareStackTrace = prep;
|
|
48
|
+
Error.stackTraceLimit = limit;
|
|
49
|
+
|
|
50
|
+
if (!callSite || !fileName) return '<anonymous>';
|
|
51
|
+
if (!withLine) return fileName;
|
|
52
|
+
return `${fileName}:${callSite.getLineNumber()}:${callSite.getColumnNumber()}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
21
55
|
export default {
|
|
22
56
|
deprecated(message: string) {
|
|
23
|
-
|
|
57
|
+
if (debug.enabled) {
|
|
58
|
+
console.trace('[@eggjs/core:deprecated] %s', message);
|
|
59
|
+
} else {
|
|
60
|
+
console.warn('[@eggjs/core:deprecated] %s', message);
|
|
61
|
+
console.warn('[@eggjs/core:deprecated] set NODE_DEBUG=@eggjs/core:utils can show call stack');
|
|
62
|
+
}
|
|
24
63
|
},
|
|
25
64
|
|
|
26
65
|
extensions,
|
|
27
66
|
extensionNames,
|
|
28
67
|
|
|
68
|
+
async existsPath(filepath: string) {
|
|
69
|
+
try {
|
|
70
|
+
await stat(filepath);
|
|
71
|
+
return true;
|
|
72
|
+
} catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
|
|
29
77
|
async loadFile(filepath: string) {
|
|
30
78
|
try {
|
|
31
79
|
// if not js module, just return content buffer
|
|
@@ -55,38 +103,7 @@ export default {
|
|
|
55
103
|
return ctx ? fn.call(ctx, ...args) : fn(...args);
|
|
56
104
|
},
|
|
57
105
|
|
|
58
|
-
getCalleeFromStack
|
|
59
|
-
stackIndex = stackIndex === undefined ? 2 : stackIndex;
|
|
60
|
-
const limit = Error.stackTraceLimit;
|
|
61
|
-
const prep = Error.prepareStackTrace;
|
|
62
|
-
|
|
63
|
-
Error.prepareStackTrace = prepareObjectStackTrace;
|
|
64
|
-
Error.stackTraceLimit = 5;
|
|
65
|
-
|
|
66
|
-
// capture the stack
|
|
67
|
-
const obj: any = {};
|
|
68
|
-
Error.captureStackTrace(obj);
|
|
69
|
-
let callSite = obj.stack[stackIndex];
|
|
70
|
-
let fileName = '';
|
|
71
|
-
if (callSite) {
|
|
72
|
-
// egg-mock will create a proxy
|
|
73
|
-
// https://github.com/eggjs/egg-mock/blob/master/lib/app.js#L174
|
|
74
|
-
fileName = callSite.getFileName();
|
|
75
|
-
/* istanbul ignore if */
|
|
76
|
-
if (fileName && fileName.endsWith('egg-mock/lib/app.js')) {
|
|
77
|
-
// TODO: add test
|
|
78
|
-
callSite = obj.stack[stackIndex + 1];
|
|
79
|
-
fileName = callSite.getFileName();
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
Error.prepareStackTrace = prep;
|
|
84
|
-
Error.stackTraceLimit = limit;
|
|
85
|
-
|
|
86
|
-
if (!callSite || !fileName) return '<anonymous>';
|
|
87
|
-
if (!withLine) return fileName;
|
|
88
|
-
return `${fileName}:${callSite.getLineNumber()}:${callSite.getColumnNumber()}`;
|
|
89
|
-
},
|
|
106
|
+
getCalleeFromStack,
|
|
90
107
|
|
|
91
108
|
getResolvedFilename(filepath: string, baseDir: string) {
|
|
92
109
|
const reg = /[/\\]/g;
|