@midwayjs/core 3.11.6 → 3.11.12-beta.1

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.
@@ -1,7 +1,7 @@
1
1
  export declare abstract class DataListener<T> {
2
2
  private innerData;
3
3
  protected init(): Promise<void>;
4
- abstract initData(): T;
4
+ abstract initData(): T | Promise<T>;
5
5
  abstract onData(callback: (data: T) => void): any;
6
6
  protected setData(data: T): void;
7
7
  getData(): T;
@@ -3,10 +3,13 @@ export declare abstract class AbstractFileDetector<T> implements IFileDetector {
3
3
  options: T;
4
4
  extraDetectorOptions: T;
5
5
  constructor(options: any);
6
- abstract run(container: IMidwayContainer): any;
6
+ abstract run(container: IMidwayContainer): void | Promise<void>;
7
7
  setExtraDetectorOptions(detectorOptions: T): void;
8
8
  }
9
- export declare class DirectoryFileDetector extends AbstractFileDetector<{
9
+ /**
10
+ * CommonJS module loader
11
+ */
12
+ export declare class CommonJSFileDetector extends AbstractFileDetector<{
10
13
  loadDir?: string | string[];
11
14
  pattern?: string | string[];
12
15
  ignore?: string | string[];
@@ -14,12 +17,19 @@ export declare class DirectoryFileDetector extends AbstractFileDetector<{
14
17
  conflictCheck?: boolean;
15
18
  }> {
16
19
  private duplicateModuleCheckSet;
17
- run(container: any): void;
20
+ run(container: any): Promise<void>;
21
+ getType(): 'commonjs' | 'module';
22
+ }
23
+ /**
24
+ * ES module loader
25
+ */
26
+ export declare class ESModuleFileDetector extends CommonJSFileDetector {
27
+ getType(): 'commonjs' | 'module';
18
28
  }
19
29
  export declare class CustomModuleDetector extends AbstractFileDetector<{
20
30
  modules?: any[];
21
31
  namespace?: string;
22
32
  }> {
23
- run(container: any): void;
33
+ run(container: any): Promise<void>;
24
34
  }
25
35
  //# sourceMappingURL=fileDetector.d.ts.map
@@ -1,11 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CustomModuleDetector = exports.DirectoryFileDetector = exports.AbstractFileDetector = void 0;
3
+ exports.CustomModuleDetector = exports.ESModuleFileDetector = exports.CommonJSFileDetector = exports.AbstractFileDetector = void 0;
4
4
  const types_1 = require("../util/types");
5
5
  const glob_1 = require("@midwayjs/glob");
6
6
  const error_1 = require("../error");
7
7
  const constants_1 = require("../constants");
8
8
  const decorator_1 = require("../decorator");
9
+ const path_1 = require("path");
10
+ async function loadModule(modulePath, type) {
11
+ var _a;
12
+ const ext = (0, path_1.extname)(modulePath);
13
+ if (ext === '.mjs' || type === 'module') {
14
+ return await (_a = modulePath, Promise.resolve().then(() => require(_a)));
15
+ }
16
+ else {
17
+ return require(modulePath);
18
+ }
19
+ }
9
20
  class AbstractFileDetector {
10
21
  constructor(options) {
11
22
  this.options = options;
@@ -29,15 +40,17 @@ const DEFAULT_IGNORE_PATTERN = [
29
40
  '**/**.test.js',
30
41
  '**/__test__/**',
31
42
  ].concat(constants_1.IGNORE_PATTERN);
32
- class DirectoryFileDetector extends AbstractFileDetector {
43
+ /**
44
+ * CommonJS module loader
45
+ */
46
+ class CommonJSFileDetector extends AbstractFileDetector {
33
47
  constructor() {
34
48
  super(...arguments);
35
49
  this.duplicateModuleCheckSet = new Map();
36
50
  }
37
- run(container) {
38
- const loadDirs = []
39
- .concat(this.options.loadDir || [])
40
- .concat(this.extraDetectorOptions.loadDir || []);
51
+ async run(container) {
52
+ var _a;
53
+ const loadDirs = [].concat((_a = this.options.loadDir) !== null && _a !== void 0 ? _a : container.get('baseDir'));
41
54
  for (const dir of loadDirs) {
42
55
  const fileResults = (0, glob_1.run)(DEFAULT_GLOB_PATTERN.concat(this.options.pattern || []).concat(this.extraDetectorOptions.pattern || []), {
43
56
  cwd: dir,
@@ -60,7 +73,7 @@ class DirectoryFileDetector extends AbstractFileDetector {
60
73
  }
61
74
  };
62
75
  for (const file of fileResults) {
63
- const exports = require(file);
76
+ const exports = await loadModule(file, this.getType());
64
77
  // add module to set
65
78
  container.bindClass(exports, {
66
79
  namespace: this.options.namespace,
@@ -73,10 +86,22 @@ class DirectoryFileDetector extends AbstractFileDetector {
73
86
  // check end
74
87
  this.duplicateModuleCheckSet.clear();
75
88
  }
89
+ getType() {
90
+ return 'commonjs';
91
+ }
92
+ }
93
+ exports.CommonJSFileDetector = CommonJSFileDetector;
94
+ /**
95
+ * ES module loader
96
+ */
97
+ class ESModuleFileDetector extends CommonJSFileDetector {
98
+ getType() {
99
+ return 'module';
100
+ }
76
101
  }
77
- exports.DirectoryFileDetector = DirectoryFileDetector;
102
+ exports.ESModuleFileDetector = ESModuleFileDetector;
78
103
  class CustomModuleDetector extends AbstractFileDetector {
79
- run(container) {
104
+ async run(container) {
80
105
  for (const module of this.options.modules) {
81
106
  container.bindClass(module, {
82
107
  namespace: this.options.namespace,
@@ -13,7 +13,6 @@ export declare class MidwayContainer implements IMidwayContainer, IModuleStore {
13
13
  private fileDetector;
14
14
  private attrMap;
15
15
  private _namespaceSet;
16
- private isLoad;
17
16
  constructor(parent?: IMidwayContainer);
18
17
  protected init(): void;
19
18
  get objectCreateEventTarget(): EventEmitter;
@@ -22,8 +21,8 @@ export declare class MidwayContainer implements IMidwayContainer, IModuleStore {
22
21
  get managedResolverFactory(): ManagedResolverFactory;
23
22
  get identifierMapping(): IIdentifierRelationShip;
24
23
  get namespaceSet(): Set<string>;
25
- load(module?: any): void;
26
- protected loadDefinitions(): void;
24
+ load(module: any): void;
25
+ protected loadDefinitions(): void | Promise<void>;
27
26
  bindClass(exports: any, options?: Partial<IObjectDefinition>): void;
28
27
  bind<T>(target: T, options?: Partial<IObjectDefinition>): void;
29
28
  bind<T>(identifier: ObjectIdentifier, target: T, options?: Partial<IObjectDefinition>): void;
@@ -35,7 +34,7 @@ export declare class MidwayContainer implements IMidwayContainer, IModuleStore {
35
34
  protected getIdentifier(target: any): string;
36
35
  protected getManagedResolverFactory(): ManagedResolverFactory;
37
36
  stop(): Promise<void>;
38
- ready(): void;
37
+ ready(): void | Promise<void>;
39
38
  get<T>(identifier: {
40
39
  new (...args: any[]): T;
41
40
  }, args?: any[], objectContext?: ObjectContext): T;
@@ -14,7 +14,6 @@ const environmentService_1 = require("../service/environmentService");
14
14
  const configService_1 = require("../service/configService");
15
15
  const EventEmitter = require("events");
16
16
  const error_1 = require("../error");
17
- const extend_1 = require("../util/extend");
18
17
  const types_1 = require("../util/types");
19
18
  const util_1 = require("../util");
20
19
  const debug = util.debuglog('midway:debug');
@@ -25,7 +24,7 @@ class ContainerConfiguration {
25
24
  this.container = container;
26
25
  this.loadedMap = new WeakMap();
27
26
  this.namespaceList = [];
28
- this.detectorOptionsList = [];
27
+ this.configurationOptionsList = [];
29
28
  }
30
29
  load(module) {
31
30
  let namespace = decorator_1.MAIN_MODULE_KEY;
@@ -56,10 +55,7 @@ class ContainerConfiguration {
56
55
  namespace = configurationOptions.namespace;
57
56
  this.namespaceList.push(namespace);
58
57
  }
59
- this.detectorOptionsList.push({
60
- conflictCheck: configurationOptions.conflictCheck,
61
- ...configurationOptions.detectorOptions,
62
- });
58
+ this.configurationOptionsList.push(configurationOptions);
63
59
  debug(`[core]: load configuration in namespace="${namespace}"`);
64
60
  this.addImports(configurationOptions.imports);
65
61
  this.addImportObjects(configurationOptions.importObjects);
@@ -179,8 +175,8 @@ class ContainerConfiguration {
179
175
  getNamespaceList() {
180
176
  return this.namespaceList;
181
177
  }
182
- getDetectorOptionsList() {
183
- return this.detectorOptionsList;
178
+ getConfigurationOptionsList() {
179
+ return this.configurationOptionsList;
184
180
  }
185
181
  }
186
182
  class MidwayContainer {
@@ -194,7 +190,6 @@ class MidwayContainer {
194
190
  this.ctx = {};
195
191
  this.attrMap = new Map();
196
192
  this._namespaceSet = null;
197
- this.isLoad = false;
198
193
  this.parent = parent;
199
194
  this.init();
200
195
  }
@@ -237,30 +232,35 @@ class MidwayContainer {
237
232
  return this._namespaceSet;
238
233
  }
239
234
  load(module) {
240
- var _a;
241
- if (module) {
242
- // load configuration
243
- const configuration = new ContainerConfiguration(this);
244
- configuration.load(module);
245
- for (const ns of configuration.getNamespaceList()) {
246
- this.namespaceSet.add(ns);
247
- debug(`[core]: load configuration in namespace="${ns}" complete`);
248
- }
249
- const detectorOptionsMerged = {};
250
- for (const detectorOptions of configuration.getDetectorOptionsList()) {
251
- (0, extend_1.extend)(true, detectorOptionsMerged, detectorOptions);
252
- }
253
- (_a = this.fileDetector) === null || _a === void 0 ? void 0 : _a.setExtraDetectorOptions(detectorOptionsMerged);
254
- this.isLoad = true;
235
+ var _a, _b, _c;
236
+ if (!Array.isArray(module)) {
237
+ module = [module];
238
+ }
239
+ // load configuration
240
+ const configuration = new ContainerConfiguration(this);
241
+ for (const mod of module) {
242
+ configuration.load(mod);
243
+ }
244
+ for (const ns of configuration.getNamespaceList()) {
245
+ this.namespaceSet.add(ns);
246
+ debug(`[core]: load configuration in namespace="${ns}" complete`);
247
+ }
248
+ const configurationOptionsList = (_a = configuration.getConfigurationOptionsList()) !== null && _a !== void 0 ? _a : [];
249
+ // find user code configuration it's without namespace
250
+ const userCodeConfiguration = (_b = configurationOptionsList.find(options => !options.namespace)) !== null && _b !== void 0 ? _b : {};
251
+ this.fileDetector = (_c = userCodeConfiguration.detector) !== null && _c !== void 0 ? _c : this.fileDetector;
252
+ if (this.fileDetector) {
253
+ this.fileDetector.setExtraDetectorOptions({
254
+ conflictCheck: userCodeConfiguration.conflictCheck,
255
+ ...userCodeConfiguration.detectorOptions,
256
+ });
255
257
  }
256
258
  }
257
259
  loadDefinitions() {
258
- var _a;
259
- if (!this.isLoad) {
260
- this.load();
261
- }
262
260
  // load project file
263
- (_a = this.fileDetector) === null || _a === void 0 ? void 0 : _a.run(this);
261
+ if (this.fileDetector) {
262
+ return this.fileDetector.run(this);
263
+ }
264
264
  }
265
265
  bindClass(exports, options) {
266
266
  if (types_1.Types.isClass(exports) || types_1.Types.isFunction(exports)) {
@@ -411,7 +411,7 @@ class MidwayContainer {
411
411
  this.registry.clearAll();
412
412
  }
413
413
  ready() {
414
- this.loadDefinitions();
414
+ return this.loadDefinitions();
415
415
  }
416
416
  get(identifier, args, objectContext) {
417
417
  var _a;
@@ -6,7 +6,7 @@ export declare class MidwayRequestContainer extends MidwayContainer {
6
6
  init(): void;
7
7
  get<T = any>(identifier: any, args?: any): T;
8
8
  getAsync<T = any>(identifier: any, args?: any): Promise<T>;
9
- ready(): void;
9
+ ready(): Promise<void>;
10
10
  getContext(): {};
11
11
  }
12
12
  //# sourceMappingURL=requestContainer.d.ts.map
@@ -67,7 +67,7 @@ class MidwayRequestContainer extends container_1.MidwayContainer {
67
67
  return this.parent.getAsync(identifier, args);
68
68
  }
69
69
  }
70
- ready() {
70
+ async ready() {
71
71
  // ignore other things
72
72
  }
73
73
  getContext() {
@@ -1,3 +1,4 @@
1
+ import { IFileDetector } from '../../interface';
1
2
  export interface IComponentInfo {
2
3
  component: any;
3
4
  enabledEnvironment?: string[];
@@ -17,6 +18,7 @@ export interface InjectionConfigurationOptions {
17
18
  }> | Record<string, any>;
18
19
  importConfigFilter?: (config: Record<string, any>) => Record<string, any>;
19
20
  namespace?: string;
21
+ detector?: IFileDetector | false;
20
22
  detectorOptions?: Record<string, any>;
21
23
  conflictCheck?: boolean;
22
24
  }
@@ -569,10 +569,10 @@ export interface IMidwayContainer extends IObjectFactory, WithFn<IObjectLifeCycl
569
569
  parent: IMidwayContainer;
570
570
  identifierMapping: IIdentifierRelationShip;
571
571
  objectCreateEventTarget: EventEmitter;
572
- ready(): any;
572
+ ready(): void | Promise<void>;
573
573
  stop(): Promise<void>;
574
574
  registerObject(identifier: ObjectIdentifier, target: any): any;
575
- load(module?: any): any;
575
+ load(module: any | any[]): any;
576
576
  hasNamespace(namespace: string): boolean;
577
577
  getNamespaceList(): string[];
578
578
  hasDefinition(identifier: ObjectIdentifier): any;
@@ -599,7 +599,7 @@ export interface IMidwayContainer extends IObjectFactory, WithFn<IObjectLifeCycl
599
599
  */
600
600
  export type IApplicationContext = IMidwayContainer;
601
601
  export interface IFileDetector {
602
- run(container: IMidwayContainer, fileDetectorOptions?: Record<string, any>): any;
602
+ run(container: IMidwayContainer, fileDetectorOptions?: Record<string, any>): void | Promise<void>;
603
603
  setExtraDetectorOptions(detectorOptions: Record<string, any>): any;
604
604
  }
605
605
  export interface IConfigService {
@@ -810,8 +810,11 @@ export interface IMidwayBootstrapOptions {
810
810
  */
811
811
  configurationModule?: any | any[];
812
812
  imports?: any | any[];
813
- moduleDetector?: 'file' | IFileDetector | false;
813
+ moduleDetector?: IFileDetector | false;
814
814
  logger?: boolean | ILogger;
815
+ /**
816
+ * @deprecated please set it from '@Configuration' decorator
817
+ */
815
818
  ignore?: string[];
816
819
  globalConfig?: Array<{
817
820
  [environmentName: string]: Record<string, any>;
@@ -203,7 +203,7 @@ class WebRouterCollector {
203
203
  if (this.baseDir) {
204
204
  const container = new container_1.MidwayContainer();
205
205
  (0, decorator_1.bindContainer)(container);
206
- container.setFileDetector(new fileDetector_1.DirectoryFileDetector({
206
+ container.setFileDetector(new fileDetector_1.CommonJSFileDetector({
207
207
  loadDir: this.baseDir,
208
208
  }));
209
209
  await container.ready();
package/dist/setup.d.ts CHANGED
@@ -9,5 +9,5 @@ export declare function destroyGlobalApplicationContext(applicationContext: IMid
9
9
  * prepare applicationContext, it use in egg framework.
10
10
  * @param globalOptions
11
11
  */
12
- export declare function prepareGlobalApplicationContext(globalOptions: IMidwayBootstrapOptions): IMidwayContainer | MidwayContainer;
12
+ export declare function prepareGlobalApplicationContext(globalOptions: IMidwayBootstrapOptions): IMidwayContainer | MidwayContainer | Promise<IMidwayContainer | MidwayContainer>;
13
13
  //# sourceMappingURL=setup.d.ts.map
package/dist/setup.js CHANGED
@@ -7,6 +7,7 @@ const decorator_1 = require("./decorator");
7
7
  const util = require("util");
8
8
  const path_1 = require("path");
9
9
  const slsFunctionService_1 = require("./service/slsFunctionService");
10
+ const types_1 = require("./util/types");
10
11
  const debug = util.debuglog('midway:debug');
11
12
  let stepIdx = 1;
12
13
  function printStepDebugInfo(stepInfo) {
@@ -17,7 +18,7 @@ function printStepDebugInfo(stepInfo) {
17
18
  * @param globalOptions
18
19
  */
19
20
  async function initializeGlobalApplicationContext(globalOptions) {
20
- const applicationContext = prepareGlobalApplicationContext(globalOptions);
21
+ const applicationContext = await prepareGlobalApplicationContext(globalOptions);
21
22
  printStepDebugInfo('Init logger');
22
23
  // init logger
23
24
  const loggerService = await applicationContext.getAsync(_1.MidwayLoggerService, [
@@ -89,10 +90,8 @@ function prepareGlobalApplicationContext(globalOptions) {
89
90
  applicationContext.registerObject('appDir', appDir);
90
91
  printStepDebugInfo('Ready module detector');
91
92
  if (globalOptions.moduleDetector !== false) {
92
- if (globalOptions.moduleDetector === undefined ||
93
- globalOptions.moduleDetector === 'file') {
94
- applicationContext.setFileDetector(new _1.DirectoryFileDetector({
95
- loadDir: baseDir,
93
+ if (globalOptions.moduleDetector === undefined) {
94
+ applicationContext.setFileDetector(new _1.CommonJSFileDetector({
96
95
  ignore: (_d = globalOptions.ignore) !== null && _d !== void 0 ? _d : [],
97
96
  }));
98
97
  }
@@ -140,32 +139,33 @@ function prepareGlobalApplicationContext(globalOptions) {
140
139
  (0, _1.safeRequire)((0, path_1.join)(globalOptions.baseDir, 'configuration')),
141
140
  ];
142
141
  }
143
- for (const configurationModule of []
144
- .concat(globalOptions.imports)
145
- .concat(globalOptions.configurationModule)) {
146
- // load configuration and component
147
- if (configurationModule) {
148
- applicationContext.load(configurationModule);
142
+ applicationContext.load([].concat(globalOptions.imports).concat(globalOptions.configurationModule));
143
+ printStepDebugInfo('Run applicationContext ready method');
144
+ function afterLoad() {
145
+ if (globalOptions.globalConfig) {
146
+ if (Array.isArray(globalOptions.globalConfig)) {
147
+ configService.add(globalOptions.globalConfig);
148
+ }
149
+ else {
150
+ configService.addObject(globalOptions.globalConfig);
151
+ }
149
152
  }
153
+ printStepDebugInfo('Load config file');
154
+ // merge config
155
+ configService.load();
156
+ debug('[core]: Current config = %j', configService.getConfiguration());
157
+ // middleware support
158
+ applicationContext.get(_1.MidwayMiddlewareService, [applicationContext]);
159
+ return applicationContext;
150
160
  }
151
- printStepDebugInfo('Run applicationContext ready method');
152
161
  // bind user code module
153
- applicationContext.ready();
154
- if (globalOptions.globalConfig) {
155
- if (Array.isArray(globalOptions.globalConfig)) {
156
- configService.add(globalOptions.globalConfig);
157
- }
158
- else {
159
- configService.addObject(globalOptions.globalConfig);
160
- }
162
+ const readyDefer = applicationContext.ready();
163
+ if ((0, types_1.isPromise)(readyDefer)) {
164
+ return readyDefer.then(afterLoad);
165
+ }
166
+ else {
167
+ return afterLoad();
161
168
  }
162
- printStepDebugInfo('Load config file');
163
- // merge config
164
- configService.load();
165
- debug('[core]: Current config = %j', configService.getConfiguration());
166
- // middleware support
167
- applicationContext.get(_1.MidwayMiddlewareService, [applicationContext]);
168
- return applicationContext;
169
169
  }
170
170
  exports.prepareGlobalApplicationContext = prepareGlobalApplicationContext;
171
171
  //# sourceMappingURL=setup.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.11.6",
3
+ "version": "3.11.12-beta.1",
4
4
  "description": "midway core",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -23,9 +23,9 @@
23
23
  "license": "MIT",
24
24
  "devDependencies": {
25
25
  "koa": "2.14.1",
26
- "mm": "3.2.2",
26
+ "mm": "3.3.0",
27
27
  "raw-body": "2.5.2",
28
- "sinon": "15.0.4"
28
+ "sinon": "15.1.2"
29
29
  },
30
30
  "dependencies": {
31
31
  "@midwayjs/glob": "^1.0.2",
@@ -42,5 +42,5 @@
42
42
  "engines": {
43
43
  "node": ">=12"
44
44
  },
45
- "gitHead": "d1ca8ca3ee5223c1c034ad3c97d335348931404b"
45
+ "gitHead": "a603d2348d6141f8f723901498f03a162a037708"
46
46
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2013 - Now midwayjs
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.