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