@eggjs/core 6.4.1 → 6.6.0-beta.2

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.
Files changed (87) hide show
  1. package/README.md +23 -27
  2. package/dist/base_context_class.d.ts +21 -0
  3. package/dist/base_context_class.js +40 -0
  4. package/dist/egg.d.ts +248 -0
  5. package/dist/egg.js +358 -0
  6. package/dist/index.d.ts +12 -0
  7. package/dist/index.js +12 -0
  8. package/dist/lifecycle.d.ts +84 -0
  9. package/dist/lifecycle.js +280 -0
  10. package/dist/loader/context_loader.d.ts +39 -0
  11. package/dist/loader/context_loader.js +79 -0
  12. package/dist/loader/egg_loader.d.ts +374 -0
  13. package/dist/loader/egg_loader.js +1184 -0
  14. package/dist/loader/file_loader.d.ts +105 -0
  15. package/dist/loader/file_loader.js +198 -0
  16. package/dist/singleton.d.ts +33 -0
  17. package/dist/singleton.js +107 -0
  18. package/{src/types.ts → dist/types.d.ts} +7 -7
  19. package/dist/utils/index.d.ts +19 -0
  20. package/dist/utils/index.js +103 -0
  21. package/dist/utils/sequencify.d.ts +16 -0
  22. package/dist/utils/sequencify.js +46 -0
  23. package/dist/utils/timing.d.ts +24 -0
  24. package/dist/utils/timing.js +85 -0
  25. package/package.json +35 -63
  26. package/dist/commonjs/base_context_class.d.ts +0 -16
  27. package/dist/commonjs/base_context_class.js +0 -41
  28. package/dist/commonjs/egg.d.ts +0 -247
  29. package/dist/commonjs/egg.js +0 -401
  30. package/dist/commonjs/index.d.ts +0 -12
  31. package/dist/commonjs/index.js +0 -32
  32. package/dist/commonjs/lifecycle.d.ts +0 -78
  33. package/dist/commonjs/lifecycle.js +0 -312
  34. package/dist/commonjs/loader/context_loader.d.ts +0 -35
  35. package/dist/commonjs/loader/context_loader.js +0 -110
  36. package/dist/commonjs/loader/egg_loader.d.ts +0 -369
  37. package/dist/commonjs/loader/egg_loader.js +0 -1543
  38. package/dist/commonjs/loader/file_loader.d.ts +0 -100
  39. package/dist/commonjs/loader/file_loader.js +0 -262
  40. package/dist/commonjs/package.json +0 -3
  41. package/dist/commonjs/singleton.d.ts +0 -29
  42. package/dist/commonjs/singleton.js +0 -125
  43. package/dist/commonjs/types.d.ts +0 -53
  44. package/dist/commonjs/types.js +0 -3
  45. package/dist/commonjs/utils/index.d.ts +0 -17
  46. package/dist/commonjs/utils/index.js +0 -117
  47. package/dist/commonjs/utils/sequencify.d.ts +0 -13
  48. package/dist/commonjs/utils/sequencify.js +0 -59
  49. package/dist/commonjs/utils/timing.d.ts +0 -21
  50. package/dist/commonjs/utils/timing.js +0 -100
  51. package/dist/esm/base_context_class.d.ts +0 -16
  52. package/dist/esm/base_context_class.js +0 -37
  53. package/dist/esm/egg.d.ts +0 -247
  54. package/dist/esm/egg.js +0 -388
  55. package/dist/esm/index.d.ts +0 -12
  56. package/dist/esm/index.js +0 -12
  57. package/dist/esm/lifecycle.d.ts +0 -78
  58. package/dist/esm/lifecycle.js +0 -305
  59. package/dist/esm/loader/context_loader.d.ts +0 -35
  60. package/dist/esm/loader/context_loader.js +0 -102
  61. package/dist/esm/loader/egg_loader.d.ts +0 -369
  62. package/dist/esm/loader/egg_loader.js +0 -1536
  63. package/dist/esm/loader/file_loader.d.ts +0 -100
  64. package/dist/esm/loader/file_loader.js +0 -255
  65. package/dist/esm/package.json +0 -3
  66. package/dist/esm/singleton.d.ts +0 -29
  67. package/dist/esm/singleton.js +0 -118
  68. package/dist/esm/types.d.ts +0 -53
  69. package/dist/esm/types.js +0 -2
  70. package/dist/esm/utils/index.d.ts +0 -17
  71. package/dist/esm/utils/index.js +0 -112
  72. package/dist/esm/utils/sequencify.d.ts +0 -13
  73. package/dist/esm/utils/sequencify.js +0 -56
  74. package/dist/esm/utils/timing.d.ts +0 -21
  75. package/dist/esm/utils/timing.js +0 -93
  76. package/dist/package.json +0 -4
  77. package/src/base_context_class.ts +0 -39
  78. package/src/egg.ts +0 -517
  79. package/src/index.ts +0 -14
  80. package/src/lifecycle.ts +0 -399
  81. package/src/loader/context_loader.ts +0 -121
  82. package/src/loader/egg_loader.ts +0 -1722
  83. package/src/loader/file_loader.ts +0 -308
  84. package/src/singleton.ts +0 -149
  85. package/src/utils/index.ts +0 -125
  86. package/src/utils/sequencify.ts +0 -70
  87. package/src/utils/timing.ts +0 -114
@@ -0,0 +1,280 @@
1
+ import utils_default from "./utils/index.js";
2
+ import { debuglog, format } from "node:util";
3
+ import assert from "node:assert";
4
+ import { EggConsoleLogger } from "egg-logger";
5
+ import { EventEmitter } from "node:events";
6
+ import { isClass } from "is-type-of";
7
+ import { Ready } from "get-ready";
8
+ import { Ready as Ready$1 } from "ready-callback";
9
+
10
+ //#region src/lifecycle.ts
11
+ const debug = debuglog("egg/core/lifecycle");
12
+ var Lifecycle = class extends EventEmitter {
13
+ #init;
14
+ #readyObject;
15
+ #bootHooks;
16
+ #boots;
17
+ #isClosed;
18
+ #closeFunctionSet;
19
+ loadReady;
20
+ bootReady;
21
+ options;
22
+ readyTimeout;
23
+ constructor(options) {
24
+ super();
25
+ options.logger = options.logger ?? new EggConsoleLogger();
26
+ this.options = options;
27
+ this.#readyObject = new Ready();
28
+ this.#bootHooks = [];
29
+ this.#boots = [];
30
+ this.#closeFunctionSet = /* @__PURE__ */ new Set();
31
+ this.#isClosed = false;
32
+ this.#init = false;
33
+ this.timing.start(`${this.options.app.type} Start`);
34
+ const eggReadyTimeoutEnv = Number.parseInt(process.env.EGG_READY_TIMEOUT_ENV || "10000");
35
+ assert(Number.isInteger(eggReadyTimeoutEnv), `process.env.EGG_READY_TIMEOUT_ENV ${process.env.EGG_READY_TIMEOUT_ENV} should be able to parseInt.`);
36
+ this.readyTimeout = eggReadyTimeoutEnv;
37
+ this.#initReady();
38
+ this.on("ready_stat", (data) => {
39
+ this.logger.info("[egg/core/lifecycle:ready_stat] end ready task %s, remain %j", data.id, data.remain);
40
+ }).on("ready_timeout", (id) => {
41
+ this.logger.warn("[egg/core/lifecycle:ready_timeout] %s seconds later %s was still unable to finish.", this.readyTimeout / 1e3, id);
42
+ });
43
+ this.ready((err) => {
44
+ this.triggerDidReady(err);
45
+ debug("app ready");
46
+ this.timing.end(`${this.options.app.type} Start`);
47
+ });
48
+ }
49
+ ready(flagOrFunction) {
50
+ if (flagOrFunction === void 0) return this.#readyObject.ready();
51
+ return this.#readyObject.ready(flagOrFunction);
52
+ }
53
+ get app() {
54
+ return this.options.app;
55
+ }
56
+ get logger() {
57
+ return this.options.logger;
58
+ }
59
+ get timing() {
60
+ return this.app.timing;
61
+ }
62
+ legacyReadyCallback(name, opt) {
63
+ const timingKeyPrefix = "readyCallback";
64
+ const timing = this.timing;
65
+ const cb = this.loadReady.readyCallback(name, opt);
66
+ const timingKey = `${timingKeyPrefix} in ` + utils_default.getResolvedFilename(name, this.app.baseDir);
67
+ this.timing.start(timingKey);
68
+ debug("register legacyReadyCallback");
69
+ return function legacyReadyCallback(...args) {
70
+ timing.end(timingKey);
71
+ debug("end legacyReadyCallback");
72
+ cb(...args);
73
+ };
74
+ }
75
+ addBootHook(bootHootOrBootClass) {
76
+ assert(this.#init === false, "do not add hook when lifecycle has been initialized");
77
+ this.#bootHooks.push(bootHootOrBootClass);
78
+ }
79
+ addFunctionAsBootHook(hook, fullPath) {
80
+ assert(this.#init === false, "do not add hook when lifecycle has been initialized");
81
+ class Boot {
82
+ static fullPath;
83
+ app;
84
+ constructor(app) {
85
+ this.app = app;
86
+ }
87
+ configDidLoad() {
88
+ hook(this.app);
89
+ }
90
+ }
91
+ Boot.fullPath = fullPath;
92
+ this.#bootHooks.push(Boot);
93
+ }
94
+ /**
95
+ * init boots and trigger config did config
96
+ */
97
+ init() {
98
+ debug("%s init lifecycle", this.app.type);
99
+ assert(this.#init === false, "lifecycle have been init");
100
+ this.#init = true;
101
+ this.#boots = this.#bootHooks.map((BootHootOrBootClass) => {
102
+ let instance = BootHootOrBootClass;
103
+ if (isClass(BootHootOrBootClass)) {
104
+ instance = new BootHootOrBootClass(this.app);
105
+ if (!instance.fullPath && "fullPath" in BootHootOrBootClass) instance.fullPath = BootHootOrBootClass.fullPath;
106
+ }
107
+ debug("[init] add boot instance: %o", instance.fullPath);
108
+ return instance;
109
+ });
110
+ }
111
+ registerBeforeStart(scope, name) {
112
+ debug("%s add registerBeforeStart, name: %o", this.options.app.type, name);
113
+ this.#registerReadyCallback({
114
+ scope,
115
+ ready: this.loadReady,
116
+ timingKeyPrefix: "Before Start",
117
+ scopeFullName: name
118
+ });
119
+ }
120
+ registerBeforeClose(fn, fullPath) {
121
+ assert(typeof fn === "function", "argument should be function");
122
+ assert(this.#isClosed === false, "app has been closed");
123
+ if (fullPath) fn.fullPath = fullPath;
124
+ this.#closeFunctionSet.add(fn);
125
+ debug("%s register beforeClose at %o, count: %d", this.app.type, fullPath, this.#closeFunctionSet.size);
126
+ }
127
+ async close() {
128
+ const closeFns = Array.from(this.#closeFunctionSet);
129
+ debug("%s start trigger %d beforeClose functions", this.app.type, closeFns.length);
130
+ for (const fn of closeFns.reverse()) {
131
+ debug("%s trigger beforeClose at %o", this.app.type, fn.fullPath);
132
+ await utils_default.callFn(fn);
133
+ this.#closeFunctionSet.delete(fn);
134
+ }
135
+ this.app.emit("close");
136
+ this.removeAllListeners();
137
+ this.app.removeAllListeners();
138
+ this.#isClosed = true;
139
+ debug("%s closed", this.app.type);
140
+ }
141
+ triggerConfigWillLoad() {
142
+ debug("trigger configWillLoad start");
143
+ for (const boot of this.#boots) if (typeof boot.configWillLoad === "function") {
144
+ debug("trigger configWillLoad at %o", boot.fullPath);
145
+ boot.configWillLoad();
146
+ }
147
+ debug("trigger configWillLoad end");
148
+ this.triggerConfigDidLoad();
149
+ }
150
+ triggerConfigDidLoad() {
151
+ debug("trigger configDidLoad start");
152
+ for (const boot of this.#boots) {
153
+ if (typeof boot.configDidLoad === "function") {
154
+ debug("trigger configDidLoad at %o", boot.fullPath);
155
+ boot.configDidLoad();
156
+ }
157
+ if (typeof boot.beforeClose === "function") {
158
+ const beforeClose = boot.beforeClose.bind(boot);
159
+ this.registerBeforeClose(beforeClose, boot.fullPath);
160
+ }
161
+ }
162
+ debug("trigger configDidLoad end");
163
+ this.triggerDidLoad();
164
+ }
165
+ triggerDidLoad() {
166
+ debug("trigger didLoad start");
167
+ debug("loadReady start");
168
+ this.loadReady.start();
169
+ for (const boot of this.#boots) if (typeof boot.didLoad === "function") {
170
+ const didLoad = boot.didLoad.bind(boot);
171
+ this.#registerReadyCallback({
172
+ scope: didLoad,
173
+ ready: this.loadReady,
174
+ timingKeyPrefix: "Did Load",
175
+ scopeFullName: boot.fullPath + ":didLoad"
176
+ });
177
+ }
178
+ }
179
+ triggerWillReady() {
180
+ debug("trigger willReady start");
181
+ debug("bootReady start");
182
+ this.bootReady.start();
183
+ for (const boot of this.#boots) if (typeof boot.willReady === "function") {
184
+ const willReady = boot.willReady.bind(boot);
185
+ this.#registerReadyCallback({
186
+ scope: willReady,
187
+ ready: this.bootReady,
188
+ timingKeyPrefix: "Will Ready",
189
+ scopeFullName: boot.fullPath + ":willReady"
190
+ });
191
+ }
192
+ }
193
+ triggerDidReady(err) {
194
+ debug("trigger didReady start");
195
+ return (async () => {
196
+ for (const boot of this.#boots) if (typeof boot.didReady === "function") {
197
+ debug("trigger didReady at %o", boot.fullPath);
198
+ try {
199
+ await boot.didReady(err);
200
+ } catch (err$1) {
201
+ debug("trigger didReady error at %o, error: %s", boot.fullPath, err$1);
202
+ this.emit("error", err$1);
203
+ }
204
+ }
205
+ debug("trigger didReady end");
206
+ })();
207
+ }
208
+ triggerServerDidReady() {
209
+ debug("trigger serverDidReady start");
210
+ return (async () => {
211
+ for (const boot of this.#boots) {
212
+ if (typeof boot.serverDidReady !== "function") continue;
213
+ debug("trigger serverDidReady at %o", boot.fullPath);
214
+ try {
215
+ await boot.serverDidReady();
216
+ } catch (err) {
217
+ debug("trigger serverDidReady error at %o, error: %s", boot.fullPath, err);
218
+ this.emit("error", err);
219
+ }
220
+ }
221
+ debug("trigger serverDidReady end");
222
+ })();
223
+ }
224
+ #initReady() {
225
+ debug("loadReady init");
226
+ this.loadReady = new Ready$1({
227
+ timeout: this.readyTimeout,
228
+ lazyStart: true
229
+ });
230
+ this.#delegateReadyEvent(this.loadReady);
231
+ this.loadReady.ready((err) => {
232
+ debug("loadReady end, err: %o", err);
233
+ debug("trigger didLoad end");
234
+ if (err) this.ready(err);
235
+ else this.triggerWillReady();
236
+ });
237
+ debug("bootReady init");
238
+ this.bootReady = new Ready$1({
239
+ timeout: this.readyTimeout,
240
+ lazyStart: true
241
+ });
242
+ this.#delegateReadyEvent(this.bootReady);
243
+ this.bootReady.ready((err) => {
244
+ debug("bootReady end, err: %o", err);
245
+ debug("trigger willReady end");
246
+ this.ready(err || true);
247
+ });
248
+ }
249
+ #delegateReadyEvent(ready) {
250
+ ready.once("error", (err) => ready.ready(err));
251
+ ready.on("ready_timeout", (id) => this.emit("ready_timeout", id));
252
+ ready.on("ready_stat", (data) => this.emit("ready_stat", data));
253
+ ready.on("error", (err) => this.emit("error", err));
254
+ }
255
+ #registerReadyCallback(args) {
256
+ const { scope, ready, timingKeyPrefix, scopeFullName } = args;
257
+ if (typeof scope !== "function") throw new TypeError("boot only support function");
258
+ const name = scopeFullName || utils_default.getCalleeFromStack(true, 4);
259
+ const timingKey = `${timingKeyPrefix} in ` + utils_default.getResolvedFilename(name, this.app.baseDir);
260
+ this.timing.start(timingKey);
261
+ debug("[registerReadyCallback] start name: %o", name);
262
+ const done = ready.readyCallback(name);
263
+ process.nextTick(async () => {
264
+ try {
265
+ await utils_default.callFn(scope);
266
+ debug("[registerReadyCallback] end name: %o", name);
267
+ done();
268
+ this.timing.end(timingKey);
269
+ } catch (e) {
270
+ let err = e;
271
+ if (!(err instanceof Error)) err = new Error(format("%s", err));
272
+ done(err);
273
+ this.timing.end(timingKey);
274
+ }
275
+ });
276
+ }
277
+ };
278
+
279
+ //#endregion
280
+ export { Lifecycle };
@@ -0,0 +1,39 @@
1
+ import { FileLoader, FileLoaderOptions } from "./file_loader.js";
2
+ import { Context } from "../egg.js";
3
+
4
+ //#region src/loader/context_loader.d.ts
5
+ interface ClassLoaderOptions {
6
+ ctx: Context;
7
+ properties: any;
8
+ }
9
+ declare class ClassLoader {
10
+ #private;
11
+ readonly _cache: Map<any, any>;
12
+ _ctx: Context;
13
+ constructor(options: ClassLoaderOptions);
14
+ }
15
+ interface ContextLoaderOptions extends Omit<FileLoaderOptions, 'target'> {
16
+ /** required inject */
17
+ inject: Record<string, any>;
18
+ /** property name defined to target */
19
+ property: string | symbol;
20
+ /** determine the field name of inject object. */
21
+ fieldClass?: string;
22
+ }
23
+ /**
24
+ * Same as {@link FileLoader}, but it will attach file to `inject[fieldClass]`.
25
+ * The exports will be lazy loaded, such as `ctx.group.repository`.
26
+ * @augments FileLoader
27
+ * @since 1.0.0
28
+ */
29
+ declare class ContextLoader extends FileLoader {
30
+ #private;
31
+ /**
32
+ * @class
33
+ * @param {Object} options - options same as {@link FileLoader}
34
+ * @param {String} options.fieldClass - determine the field name of inject object.
35
+ */
36
+ constructor(options: ContextLoaderOptions);
37
+ }
38
+ //#endregion
39
+ export { ClassLoader, ClassLoaderOptions, ContextLoader, ContextLoaderOptions };
@@ -0,0 +1,79 @@
1
+ import { EXPORTS, FileLoader } from "./file_loader.js";
2
+ import assert from "node:assert";
3
+ import { isClass, isPrimitive } from "is-type-of";
4
+
5
+ //#region src/loader/context_loader.ts
6
+ const CLASS_LOADER = Symbol("classLoader");
7
+ var ClassLoader = class {
8
+ _cache = /* @__PURE__ */ new Map();
9
+ _ctx;
10
+ constructor(options) {
11
+ assert(options.ctx, "options.ctx is required");
12
+ const properties = options.properties;
13
+ this._ctx = options.ctx;
14
+ for (const property in properties) this.#defineProperty(property, properties[property]);
15
+ }
16
+ #defineProperty(property, values) {
17
+ Object.defineProperty(this, property, { get() {
18
+ let instance = this._cache.get(property);
19
+ if (!instance) {
20
+ instance = getInstance(values, this._ctx);
21
+ this._cache.set(property, instance);
22
+ }
23
+ return instance;
24
+ } });
25
+ }
26
+ };
27
+ /**
28
+ * Same as {@link FileLoader}, but it will attach file to `inject[fieldClass]`.
29
+ * The exports will be lazy loaded, such as `ctx.group.repository`.
30
+ * @augments FileLoader
31
+ * @since 1.0.0
32
+ */
33
+ var ContextLoader = class extends FileLoader {
34
+ #inject;
35
+ /**
36
+ * @class
37
+ * @param {Object} options - options same as {@link FileLoader}
38
+ * @param {String} options.fieldClass - determine the field name of inject object.
39
+ */
40
+ constructor(options) {
41
+ assert(options.property, "options.property is required");
42
+ assert(options.inject, "options.inject is required");
43
+ const target = {};
44
+ if (options.fieldClass) options.inject[options.fieldClass] = target;
45
+ super({
46
+ ...options,
47
+ target
48
+ });
49
+ this.#inject = this.options.inject;
50
+ const app = this.#inject;
51
+ const property = options.property;
52
+ Object.defineProperty(app.context, property, { get() {
53
+ const ctx = this;
54
+ if (!ctx[CLASS_LOADER]) ctx[CLASS_LOADER] = /* @__PURE__ */ new Map();
55
+ const classLoader = ctx[CLASS_LOADER];
56
+ let instance = classLoader.get(property);
57
+ if (!instance) {
58
+ instance = getInstance(target, ctx);
59
+ classLoader.set(property, instance);
60
+ }
61
+ return instance;
62
+ } });
63
+ }
64
+ };
65
+ function getInstance(values, ctx) {
66
+ const Class = values[EXPORTS] ? values : null;
67
+ let instance;
68
+ if (Class) if (isClass(Class)) instance = new Class(ctx);
69
+ else instance = Class;
70
+ else if (isPrimitive(values)) instance = values;
71
+ else instance = new ClassLoader({
72
+ ctx,
73
+ properties: values
74
+ });
75
+ return instance;
76
+ }
77
+
78
+ //#endregion
79
+ export { ClassLoader, ContextLoader };