@midwayjs/core 3.4.3 → 3.4.7

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.
@@ -28,7 +28,7 @@ export declare abstract class DataSourceManager<T> {
28
28
  abstract getName(): string;
29
29
  protected abstract createDataSource(config: any, dataSourceName: string): Promise<T | void> | (T | void);
30
30
  protected abstract checkConnected(dataSource: T): Promise<boolean>;
31
- protected destroyDataSource(dataSource: T): Promise<void>;
31
+ protected abstract destroyDataSource(dataSource: T): Promise<void>;
32
32
  stop(): Promise<void>;
33
33
  }
34
34
  export declare function globModels(globString: string, appDir: string): any[];
@@ -9,7 +9,7 @@ const error_1 = require("../error");
9
9
  const glob_1 = require("@midwayjs/glob");
10
10
  const path_1 = require("path");
11
11
  const decorator_1 = require("@midwayjs/decorator");
12
- const DEFAULT_PATTERN = ['**/**.ts', '**/**.js'];
12
+ const interface_1 = require("../interface");
13
13
  class DataSourceManager {
14
14
  constructor() {
15
15
  this.dataSource = new Map();
@@ -91,11 +91,11 @@ class DataSourceManager {
91
91
  getDataSourceNameByModel(modelOrRepository) {
92
92
  return this.modelMapping.get(modelOrRepository);
93
93
  }
94
- async destroyDataSource(dataSource) { }
95
94
  async stop() {
96
- for (const value of this.dataSource.values()) {
97
- await this.destroyDataSource(value);
98
- }
95
+ const arr = Array.from(this.dataSource.values());
96
+ await Promise.all(arr.map(dbh => {
97
+ return this.destroyDataSource(dbh);
98
+ }));
99
99
  this.dataSource.clear();
100
100
  }
101
101
  }
@@ -104,8 +104,9 @@ function globModels(globString, appDir) {
104
104
  const cwd = (0, path_1.join)(appDir, globString);
105
105
  const models = [];
106
106
  // string will be glob file
107
- const files = (0, glob_1.run)(DEFAULT_PATTERN, {
107
+ const files = (0, glob_1.run)(interface_1.DEFAULT_PATTERN, {
108
108
  cwd,
109
+ ignore: interface_1.IGNORE_PATTERN,
109
110
  });
110
111
  for (const file of files) {
111
112
  const exports = require(file);
@@ -4,6 +4,7 @@ exports.CustomModuleDetector = exports.DirectoryFileDetector = exports.AbstractF
4
4
  const decorator_1 = require("@midwayjs/decorator");
5
5
  const glob_1 = require("@midwayjs/glob");
6
6
  const error_1 = require("../error");
7
+ const interface_1 = require("../interface");
7
8
  class AbstractFileDetector {
8
9
  constructor(options) {
9
10
  this.options = options;
@@ -14,9 +15,8 @@ class AbstractFileDetector {
14
15
  }
15
16
  }
16
17
  exports.AbstractFileDetector = AbstractFileDetector;
17
- const DEFAULT_PATTERN = ['**/**.ts', '**/**.tsx', '**/**.js'];
18
+ const DEFAULT_GLOB_PATTERN = ['**/**.tsx'].concat(interface_1.DEFAULT_PATTERN);
18
19
  const DEFAULT_IGNORE_PATTERN = [
19
- '**/**.d.ts',
20
20
  '**/logs/**',
21
21
  '**/run/**',
22
22
  '**/public/**',
@@ -27,7 +27,7 @@ const DEFAULT_IGNORE_PATTERN = [
27
27
  '**/**.test.ts',
28
28
  '**/**.test.js',
29
29
  '**/__test__/**',
30
- ];
30
+ ].concat(interface_1.IGNORE_PATTERN);
31
31
  class DirectoryFileDetector extends AbstractFileDetector {
32
32
  constructor() {
33
33
  super(...arguments);
@@ -39,7 +39,7 @@ class DirectoryFileDetector extends AbstractFileDetector {
39
39
  .concat(this.options.loadDir || [])
40
40
  .concat(this.extraDetectorOptions.loadDir || []);
41
41
  for (const dir of loadDirs) {
42
- const fileResults = (0, glob_1.run)(DEFAULT_PATTERN.concat(this.options.pattern || []).concat(this.extraDetectorOptions.pattern || []), {
42
+ const fileResults = (0, glob_1.run)(DEFAULT_GLOB_PATTERN.concat(this.options.pattern || []).concat(this.extraDetectorOptions.pattern || []), {
43
43
  cwd: dir,
44
44
  ignore: DEFAULT_IGNORE_PATTERN.concat(this.options.ignore || []).concat(this.extraDetectorOptions.ignore || []),
45
45
  });
@@ -60,6 +60,8 @@ export declare class ContextMiddlewareManager<CTX extends IMidwayContext, R, N>
60
60
  * @param middlewareOrNameOrIdx
61
61
  */
62
62
  remove(middlewareOrNameOrIdx: CommonMiddleware<CTX, R, N> | string | number): CommonMiddleware<CTX, R, N>;
63
+ push(...items: any[]): number;
64
+ unshift(...items: any[]): number;
63
65
  /**
64
66
  * get middleware name list
65
67
  */
@@ -170,6 +170,22 @@ class ContextMiddlewareManager extends Array {
170
170
  }
171
171
  }
172
172
  }
173
+ push(...items) {
174
+ items.forEach(item => {
175
+ if (typeof item !== 'string' && !this.getMiddlewareName(item)) {
176
+ item._name = 'anonymous';
177
+ }
178
+ });
179
+ return super.push(...items);
180
+ }
181
+ unshift(...items) {
182
+ items.forEach(item => {
183
+ if (typeof item !== 'string' && !this.getMiddlewareName(item)) {
184
+ item._name = 'anonymous';
185
+ }
186
+ });
187
+ return super.unshift(...items);
188
+ }
173
189
  /**
174
190
  * get middleware name list
175
191
  */
@@ -61,6 +61,7 @@ class ContainerConfiguration {
61
61
  this.addImports(configurationOptions.imports);
62
62
  this.addImportObjects(configurationOptions.importObjects);
63
63
  this.addImportConfigs(configurationOptions.importConfigs);
64
+ this.addImportConfigFilter(configurationOptions.importConfigFilter);
64
65
  this.bindConfigurationClass(configurationExport, namespace);
65
66
  }
66
67
  }
@@ -79,6 +80,11 @@ class ContainerConfiguration {
79
80
  }
80
81
  }
81
82
  }
83
+ addImportConfigFilter(importConfigFilter) {
84
+ if (importConfigFilter) {
85
+ this.container.get(configService_1.MidwayConfigService).addFilter(importConfigFilter);
86
+ }
87
+ }
82
88
  addImports(imports = []) {
83
89
  var _a;
84
90
  // 处理 imports
@@ -483,5 +483,7 @@ export interface MidwayConfig extends FileConfigOption<typeof _default> {
483
483
  }
484
484
  export declare const ASYNC_CONTEXT_KEY: unique symbol;
485
485
  export declare const ASYNC_CONTEXT_MANAGER_KEY = "MIDWAY_ASYNC_CONTEXT_MANAGER_KEY";
486
+ export declare const DEFAULT_PATTERN: string[];
487
+ export declare const IGNORE_PATTERN: string[];
486
488
  export {};
487
489
  //# sourceMappingURL=interface.d.ts.map
package/dist/interface.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ASYNC_CONTEXT_MANAGER_KEY = exports.ASYNC_CONTEXT_KEY = exports.MIDWAY_LOGGER_WRITEABLE_DIR = exports.MidwayProcessTypeEnum = exports.REQUEST_CTX_LOGGER_CACHE_KEY = exports.HTTP_SERVER_KEY = exports.REQUEST_OBJ_CTX_KEY = exports.REQUEST_CTX_KEY = exports.ObjectLifeCycleEvent = void 0;
3
+ exports.IGNORE_PATTERN = exports.DEFAULT_PATTERN = exports.ASYNC_CONTEXT_MANAGER_KEY = exports.ASYNC_CONTEXT_KEY = exports.MIDWAY_LOGGER_WRITEABLE_DIR = exports.MidwayProcessTypeEnum = exports.REQUEST_CTX_LOGGER_CACHE_KEY = exports.HTTP_SERVER_KEY = exports.REQUEST_OBJ_CTX_KEY = exports.REQUEST_CTX_KEY = exports.ObjectLifeCycleEvent = void 0;
4
4
  var ObjectLifeCycleEvent;
5
5
  (function (ObjectLifeCycleEvent) {
6
6
  ObjectLifeCycleEvent["BEFORE_BIND"] = "beforeBind";
@@ -21,4 +21,13 @@ var MidwayProcessTypeEnum;
21
21
  exports.MIDWAY_LOGGER_WRITEABLE_DIR = 'MIDWAY_LOGGER_WRITEABLE_DIR';
22
22
  exports.ASYNC_CONTEXT_KEY = Symbol('ASYNC_CONTEXT_KEY');
23
23
  exports.ASYNC_CONTEXT_MANAGER_KEY = 'MIDWAY_ASYNC_CONTEXT_MANAGER_KEY';
24
+ exports.DEFAULT_PATTERN = [
25
+ '**/**.ts',
26
+ '**/**.js',
27
+ '**/**.mts',
28
+ '**/**.mjs',
29
+ '**/**.cts',
30
+ '**/**.cjs'
31
+ ];
32
+ exports.IGNORE_PATTERN = ['**/**.d.ts', '**/**.d.mts', '**/**.d.cts'];
24
33
  //# sourceMappingURL=interface.js.map
@@ -14,6 +14,7 @@ export declare class MidwayConfigService implements IConfigService {
14
14
  protected isReady: boolean;
15
15
  protected externalObject: Record<string, unknown>[];
16
16
  protected appInfo: MidwayAppInfo;
17
+ protected configFilterList: Array<(config: Record<string, any>) => Record<string, any> | undefined>;
17
18
  protected environmentService: MidwayEnvironmentService;
18
19
  protected informationService: MidwayInformationService;
19
20
  protected init(): void;
@@ -27,6 +28,12 @@ export declare class MidwayConfigService implements IConfigService {
27
28
  private loadConfig;
28
29
  clearAllConfig(): void;
29
30
  clearConfigMergeOrder(): void;
31
+ /**
32
+ * add a config filter
33
+ * @param filter
34
+ */
35
+ addFilter(filter: (config: Record<string, any>) => Record<string, any>): void;
36
+ protected runWithFilter(config: Record<string, any>): Record<string, any>;
30
37
  }
31
38
  export {};
32
39
  //# sourceMappingURL=configService.d.ts.map
@@ -30,6 +30,7 @@ let MidwayConfigService = class MidwayConfigService {
30
30
  this.configMergeOrder = [];
31
31
  this.isReady = false;
32
32
  this.externalObject = [];
33
+ this.configFilterList = [];
33
34
  }
34
35
  init() {
35
36
  this.appInfo = {
@@ -80,6 +81,11 @@ let MidwayConfigService = class MidwayConfigService {
80
81
  }
81
82
  addObject(obj, reverse = false) {
82
83
  if (this.isReady) {
84
+ obj = this.runWithFilter(obj);
85
+ if (!obj) {
86
+ debug('[config]: Filter config and got undefined will be drop it');
87
+ return;
88
+ }
83
89
  this.configMergeOrder.push({
84
90
  env: 'default',
85
91
  extraPath: '',
@@ -131,6 +137,11 @@ let MidwayConfigService = class MidwayConfigService {
131
137
  if (!config) {
132
138
  continue;
133
139
  }
140
+ config = this.runWithFilter(config);
141
+ if (!config) {
142
+ debug('[config]: Filter config and got undefined will be drop it');
143
+ continue;
144
+ }
134
145
  if (typeof filename === 'string') {
135
146
  debug('[config]: Loaded config %s, %j', filename, config);
136
147
  }
@@ -147,8 +158,9 @@ let MidwayConfigService = class MidwayConfigService {
147
158
  (0, extend_1.extend)(true, target, config);
148
159
  }
149
160
  if (this.externalObject.length) {
150
- for (const externalObject of this.externalObject) {
161
+ for (let externalObject of this.externalObject) {
151
162
  if (externalObject) {
163
+ externalObject = this.runWithFilter(externalObject);
152
164
  debug('[config]: Loaded external object %j', externalObject);
153
165
  (0, extend_1.extend)(true, target, externalObject);
154
166
  this.configMergeOrder.push({
@@ -192,6 +204,21 @@ let MidwayConfigService = class MidwayConfigService {
192
204
  clearConfigMergeOrder() {
193
205
  this.configMergeOrder.length = 0;
194
206
  }
207
+ /**
208
+ * add a config filter
209
+ * @param filter
210
+ */
211
+ addFilter(filter) {
212
+ this.configFilterList.push(filter);
213
+ }
214
+ runWithFilter(config) {
215
+ for (const filter of this.configFilterList) {
216
+ debug(`[config]: Filter config by filter = "${filter.name || 'anonymous filter'}"`);
217
+ config = filter(config);
218
+ debug('[config]: Filter config result = %j', config);
219
+ }
220
+ return config;
221
+ }
195
222
  };
196
223
  __decorate([
197
224
  (0, decorator_1.Inject)(),
@@ -30,6 +30,7 @@ let MidwayServerlessFunctionService = class MidwayServerlessFunctionService exte
30
30
  for (const routerInfo of this.routes.values()) {
31
31
  for (const info of routerInfo) {
32
32
  if (info.requestMethod === 'all') {
33
+ info.functionTriggerMetadata = info.functionTriggerMetadata || {};
33
34
  info.functionTriggerMetadata.method = [
34
35
  'get',
35
36
  'post',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.4.3",
3
+ "version": "3.4.7",
4
4
  "description": "midway core",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -21,12 +21,12 @@
21
21
  ],
22
22
  "license": "MIT",
23
23
  "devDependencies": {
24
- "@midwayjs/decorator": "^3.4.1",
24
+ "@midwayjs/decorator": "^3.4.4",
25
25
  "koa": "2.13.4",
26
26
  "midway-test-component": "*",
27
27
  "mm": "3.2.0",
28
28
  "raw-body": "2.5.1",
29
- "sinon": "13.0.2"
29
+ "sinon": "14.0.0"
30
30
  },
31
31
  "peerDependencies": {
32
32
  "@midwayjs/decorator": "*"
@@ -45,5 +45,5 @@
45
45
  "engines": {
46
46
  "node": ">=12"
47
47
  },
48
- "gitHead": "0d79293f56d01a3ef3589e6a6ef53c582ac0c46f"
48
+ "gitHead": "3c60619a65c6776d99fbf0b30ac454d3cb6926b9"
49
49
  }