@eggjs/core 6.5.0 → 6.6.0-beta.11

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 +1 -5
  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 +39 -75
  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 -246
  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 -315
  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 -1558
  38. package/dist/commonjs/loader/file_loader.d.ts +0 -100
  39. package/dist/commonjs/loader/file_loader.js +0 -260
  40. package/dist/commonjs/package.json +0 -3
  41. package/dist/commonjs/singleton.d.ts +0 -29
  42. package/dist/commonjs/singleton.js +0 -124
  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 -64
  49. package/dist/commonjs/utils/timing.d.ts +0 -21
  50. package/dist/commonjs/utils/timing.js +0 -106
  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 -246
  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 -308
  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 -1551
  63. package/dist/esm/loader/file_loader.d.ts +0 -100
  64. package/dist/esm/loader/file_loader.js +0 -253
  65. package/dist/esm/package.json +0 -3
  66. package/dist/esm/singleton.d.ts +0 -29
  67. package/dist/esm/singleton.js +0 -117
  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 -61
  74. package/dist/esm/utils/timing.d.ts +0 -21
  75. package/dist/esm/utils/timing.js +0 -99
  76. package/dist/package.json +0 -4
  77. package/src/base_context_class.ts +0 -39
  78. package/src/egg.ts +0 -617
  79. package/src/index.ts +0 -14
  80. package/src/lifecycle.ts +0 -438
  81. package/src/loader/context_loader.ts +0 -123
  82. package/src/loader/egg_loader.ts +0 -1984
  83. package/src/loader/file_loader.ts +0 -349
  84. package/src/singleton.ts +0 -187
  85. package/src/utils/index.ts +0 -132
  86. package/src/utils/sequencify.ts +0 -105
  87. package/src/utils/timing.ts +0 -128
@@ -0,0 +1,374 @@
1
+ import { EggAppConfig, EggAppInfo, EggPluginInfo } from "../types.js";
2
+ import { Timing } from "../utils/timing.js";
3
+ import { Lifecycle } from "../lifecycle.js";
4
+ import { FileLoader, FileLoaderOptions } from "./file_loader.js";
5
+ import { ContextLoader, ContextLoaderOptions } from "./context_loader.js";
6
+ import { EggCore } from "../egg.js";
7
+ import * as egg_logger0 from "egg-logger";
8
+ import { Logger } from "egg-logger";
9
+
10
+ //#region src/loader/egg_loader.d.ts
11
+ interface EggLoaderOptions {
12
+ /** server env */
13
+ env: string;
14
+ /** Application instance */
15
+ app: EggCore;
16
+ EggCoreClass?: typeof EggCore;
17
+ /** the directory of application */
18
+ baseDir: string;
19
+ /** egg logger */
20
+ logger: Logger;
21
+ /** server scope */
22
+ serverScope?: string;
23
+ /** custom plugins */
24
+ plugins?: Record<string, EggPluginInfo>;
25
+ }
26
+ type EggDirInfoType = 'app' | 'plugin' | 'framework';
27
+ interface EggDirInfo {
28
+ path: string;
29
+ type: EggDirInfoType;
30
+ }
31
+ declare class EggLoader {
32
+ #private;
33
+ readonly options: EggLoaderOptions;
34
+ readonly timing: Timing;
35
+ readonly pkg: Record<string, any>;
36
+ readonly eggPaths: string[];
37
+ readonly serverEnv: string;
38
+ readonly serverScope: string;
39
+ readonly appInfo: EggAppInfo;
40
+ dirs?: EggDirInfo[];
41
+ /**
42
+ * @class
43
+ * @param {Object} options - options
44
+ * @param {String} options.baseDir - the directory of application
45
+ * @param {EggCore} options.app - Application instance
46
+ * @param {Logger} options.logger - logger
47
+ * @param {Object} [options.plugins] - custom plugins
48
+ * @since 1.0.0
49
+ */
50
+ constructor(options: EggLoaderOptions);
51
+ get app(): EggCore;
52
+ get lifecycle(): Lifecycle;
53
+ get logger(): Logger<egg_logger0.EggLoggerOptions>;
54
+ /**
55
+ * Get {@link AppInfo#env}
56
+ * @returns {String} env
57
+ * @see AppInfo#env
58
+ * @private
59
+ * @since 1.0.0
60
+ */
61
+ protected getServerEnv(): string;
62
+ /**
63
+ * Get {@link AppInfo#scope}
64
+ * @returns {String} serverScope
65
+ * @private
66
+ */
67
+ protected getServerScope(): string;
68
+ /**
69
+ * Get {@link AppInfo#name}
70
+ * @returns {String} appname
71
+ * @private
72
+ * @since 1.0.0
73
+ */
74
+ getAppname(): string;
75
+ /**
76
+ * Get home directory
77
+ * @returns {String} home directory
78
+ * @since 3.4.0
79
+ */
80
+ getHomedir(): string;
81
+ /**
82
+ * Get app info
83
+ * @returns {AppInfo} appInfo
84
+ * @since 1.0.0
85
+ */
86
+ protected getAppInfo(): EggAppInfo;
87
+ /**
88
+ * Get {@link EggLoader#eggPaths}
89
+ * @returns {Array} framework directories
90
+ * @see {@link EggLoader#eggPaths}
91
+ * @private
92
+ * @since 1.0.0
93
+ */
94
+ protected getEggPaths(): string[];
95
+ /** start Plugin loader */
96
+ lookupDirs: Set<string>;
97
+ eggPlugins: Record<string, EggPluginInfo>;
98
+ appPlugins: Record<string, EggPluginInfo>;
99
+ customPlugins: Record<string, EggPluginInfo>;
100
+ allPlugins: Record<string, EggPluginInfo>;
101
+ orderPlugins: EggPluginInfo[];
102
+ /** enable plugins */
103
+ plugins: Record<string, EggPluginInfo>;
104
+ /**
105
+ * Load config/plugin.js from {EggLoader#loadUnits}
106
+ *
107
+ * plugin.js is written below
108
+ *
109
+ * ```js
110
+ * {
111
+ * 'xxx-client': {
112
+ * enable: true,
113
+ * package: 'xxx-client',
114
+ * dep: [],
115
+ * env: [],
116
+ * },
117
+ * // short hand
118
+ * 'rds': false,
119
+ * 'depd': {
120
+ * enable: true,
121
+ * path: 'path/to/depd'
122
+ * }
123
+ * }
124
+ * ```
125
+ *
126
+ * If the plugin has path, Loader will find the module from it.
127
+ *
128
+ * Otherwise Loader will lookup follow the order by packageName
129
+ *
130
+ * 1. $APP_BASE/node_modules/${package}
131
+ * 2. $EGG_BASE/node_modules/${package}
132
+ *
133
+ * You can call `loader.plugins` that retrieve enabled plugins.
134
+ *
135
+ * ```js
136
+ * loader.plugins['xxx-client'] = {
137
+ * name: 'xxx-client', // the plugin name, it can be used in `dep`
138
+ * package: 'xxx-client', // the package name of plugin
139
+ * enable: true, // whether enabled
140
+ * path: 'path/to/xxx-client', // the directory of the plugin package
141
+ * dep: [], // the dependent plugins, you can use the plugin name
142
+ * env: [ 'local', 'unittest' ], // specify the serverEnv that only enable the plugin in it
143
+ * }
144
+ * ```
145
+ *
146
+ * `loader.allPlugins` can be used when retrieve all plugins.
147
+ * @function EggLoader#loadPlugin
148
+ * @since 1.0.0
149
+ */
150
+ loadPlugin(): Promise<void>;
151
+ protected loadAppPlugins(): Promise<Record<string, EggPluginInfo>>;
152
+ protected loadEggPlugins(): Promise<Record<string, EggPluginInfo>>;
153
+ protected loadCustomPlugins(): Record<string, EggPluginInfo>;
154
+ protected readPluginConfigs(configPaths: string[] | string): Promise<Record<string, EggPluginInfo>>;
155
+ protected getOrderPlugins(allPlugins: Record<string, EggPluginInfo>, enabledPluginNames: string[], appPlugins: Record<string, EggPluginInfo>): EggPluginInfo[];
156
+ protected getLookupDirs(): Set<string>;
157
+ protected getPluginPath(plugin: EggPluginInfo): string;
158
+ /** end Plugin loader */
159
+ /** start Config loader */
160
+ configMeta: Record<string, any>;
161
+ config: EggAppConfig;
162
+ /**
163
+ * Load config/config.js
164
+ *
165
+ * Will merge config.default.js 和 config.${env}.js
166
+ *
167
+ * @function EggLoader#loadConfig
168
+ * @since 1.0.0
169
+ */
170
+ loadConfig(): Promise<void>;
171
+ /** end Config loader */
172
+ /** start Extend loader */
173
+ /**
174
+ * mixin Agent.prototype
175
+ * @function EggLoader#loadAgentExtend
176
+ * @since 1.0.0
177
+ */
178
+ loadAgentExtend(): Promise<void>;
179
+ /**
180
+ * mixin Application.prototype
181
+ * @function EggLoader#loadApplicationExtend
182
+ * @since 1.0.0
183
+ */
184
+ loadApplicationExtend(): Promise<void>;
185
+ /**
186
+ * mixin Request.prototype
187
+ * @function EggLoader#loadRequestExtend
188
+ * @since 1.0.0
189
+ */
190
+ loadRequestExtend(): Promise<void>;
191
+ /**
192
+ * mixin Response.prototype
193
+ * @function EggLoader#loadResponseExtend
194
+ * @since 1.0.0
195
+ */
196
+ loadResponseExtend(): Promise<void>;
197
+ /**
198
+ * mixin Context.prototype
199
+ * @function EggLoader#loadContextExtend
200
+ * @since 1.0.0
201
+ */
202
+ loadContextExtend(): Promise<void>;
203
+ /**
204
+ * mixin app.Helper.prototype
205
+ * @function EggLoader#loadHelperExtend
206
+ * @since 1.0.0
207
+ */
208
+ loadHelperExtend(): Promise<void>;
209
+ /**
210
+ * Find all extend file paths by name
211
+ * can be override in top level framework to support load `app/extends/{name}.js`
212
+ *
213
+ * @param {String} name - filename which may be `app/extend/{name}.js`
214
+ * @returns {Array} filepaths extend file paths
215
+ * @private
216
+ */
217
+ protected getExtendFilePaths(name: string): string[];
218
+ /**
219
+ * Loader app/extend/xx.js to `prototype`,
220
+ * @function loadExtend
221
+ * @param {String} name - filename which may be `app/extend/{name}.js`
222
+ * @param {Object} proto - prototype that mixed
223
+ * @since 1.0.0
224
+ */
225
+ loadExtend(name: string, proto: object): Promise<void>;
226
+ /** end Extend loader */
227
+ /** start Custom loader */
228
+ /**
229
+ * load app.js
230
+ *
231
+ * @example
232
+ * - old:
233
+ *
234
+ * ```js
235
+ * module.exports = function(app) {
236
+ * doSomething();
237
+ * }
238
+ * ```
239
+ *
240
+ * - new:
241
+ *
242
+ * ```js
243
+ * module.exports = class Boot {
244
+ * constructor(app) {
245
+ * this.app = app;
246
+ * }
247
+ * configDidLoad() {
248
+ * doSomething();
249
+ * }
250
+ * }
251
+ * @since 1.0.0
252
+ */
253
+ loadCustomApp(): Promise<void>;
254
+ /**
255
+ * Load agent.js, same as {@link EggLoader#loadCustomApp}
256
+ */
257
+ loadCustomAgent(): Promise<void>;
258
+ loadBootHook(): void;
259
+ /** end Custom loader */
260
+ /** start Service loader */
261
+ /**
262
+ * Load app/service
263
+ * @function EggLoader#loadService
264
+ * @param {Object} options - LoaderOptions
265
+ * @since 1.0.0
266
+ */
267
+ loadService(options?: Partial<ContextLoaderOptions>): Promise<void>;
268
+ /** end Service loader */
269
+ /** start Middleware loader */
270
+ /**
271
+ * Load app/middleware
272
+ *
273
+ * app.config.xx is the options of the middleware xx that has same name as config
274
+ *
275
+ * @function EggLoader#loadMiddleware
276
+ * @param {Object} opt - LoaderOptions
277
+ * @example
278
+ * ```js
279
+ * // app/middleware/status.js
280
+ * module.exports = function(options, app) {
281
+ * // options == app.config.status
282
+ * return async next => {
283
+ * await next();
284
+ * }
285
+ * }
286
+ * ```
287
+ * @since 1.0.0
288
+ */
289
+ loadMiddleware(opt?: Partial<FileLoaderOptions>): Promise<void>;
290
+ /** end Middleware loader */
291
+ /** start Controller loader */
292
+ /**
293
+ * Load app/controller
294
+ * @param {Object} opt - LoaderOptions
295
+ * @since 1.0.0
296
+ */
297
+ loadController(opt?: Partial<FileLoaderOptions>): Promise<void>;
298
+ /** end Controller loader */
299
+ /** start Router loader */
300
+ /**
301
+ * Load app/router.js
302
+ * @function EggLoader#loadRouter
303
+ * @since 1.0.0
304
+ */
305
+ loadRouter(): Promise<void>;
306
+ /** end Router loader */
307
+ /** start CustomLoader loader */
308
+ loadCustomLoader(): Promise<void>;
309
+ /** end CustomLoader loader */
310
+ /**
311
+ * Load single file, will invoke when export is function
312
+ *
313
+ * @param {String} filepath - fullpath
314
+ * @param {Array} inject - pass rest arguments into the function when invoke
315
+ * @returns {Object} exports
316
+ * @example
317
+ * ```js
318
+ * app.loader.loadFile(path.join(app.options.baseDir, 'config/router.js'));
319
+ * ```
320
+ * @since 1.0.0
321
+ */
322
+ loadFile(filepath: string, ...inject: unknown[]): Promise<any>;
323
+ /**
324
+ * @param {String} filepath - fullpath
325
+ * @private
326
+ */
327
+ requireFile(filepath: string): Promise<any>;
328
+ /**
329
+ * Get all loadUnit
330
+ *
331
+ * loadUnit is a directory that can be loaded by EggLoader, it has the same structure.
332
+ * loadUnit has a path and a type(app, framework, plugin).
333
+ *
334
+ * The order of the loadUnits:
335
+ *
336
+ * 1. plugin
337
+ * 2. framework
338
+ * 3. app
339
+ *
340
+ * @returns {Array} loadUnits
341
+ * @since 1.0.0
342
+ */
343
+ getLoadUnits(): EggDirInfo[];
344
+ /**
345
+ * Load files using {@link FileLoader}, inject to {@link Application}
346
+ * @param {String|Array} directory - see {@link FileLoader}
347
+ * @param {String} property - see {@link FileLoader}, e.g.: 'controller', 'middlewares'
348
+ * @param {Object} options - see {@link FileLoader}
349
+ * @since 1.0.0
350
+ */
351
+ loadToApp(directory: string | string[], property: string | symbol, options?: Omit<FileLoaderOptions, 'inject' | 'target'>): Promise<void>;
352
+ /**
353
+ * Load files using {@link ContextLoader}
354
+ * @param {String|Array} directory - see {@link ContextLoader}
355
+ * @param {String} property - see {@link ContextLoader}
356
+ * @param {Object} options - see {@link ContextLoader}
357
+ * @since 1.0.0
358
+ */
359
+ loadToContext(directory: string | string[], property: string | symbol, options?: Omit<ContextLoaderOptions, 'inject' | 'property'>): Promise<void>;
360
+ /**
361
+ * @member {FileLoader} EggLoader#FileLoader
362
+ * @since 1.0.0
363
+ */
364
+ get FileLoader(): typeof FileLoader;
365
+ /**
366
+ * @member {ContextLoader} EggLoader#ContextLoader
367
+ * @since 1.0.0
368
+ */
369
+ get ContextLoader(): typeof ContextLoader;
370
+ getTypeFiles(filename: string): string[];
371
+ resolveModule(filepath: string): string | undefined;
372
+ }
373
+ //#endregion
374
+ export { EggDirInfo, EggDirInfoType, EggLoader, EggLoaderOptions };