@midwayjs/core 3.0.6 → 3.0.10

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,8 +1,10 @@
1
1
  import { IFileDetector, IMidwayContainer } from '../interface';
2
2
  export declare abstract class AbstractFileDetector<T> implements IFileDetector {
3
3
  options: T;
4
+ extraDetectorOptions: T;
4
5
  constructor(options: any);
5
6
  abstract run(container: IMidwayContainer): any;
7
+ setExtraDetectorOptions(detectorOptions: T): void;
6
8
  }
7
9
  export declare class DirectoryFileDetector extends AbstractFileDetector<{
8
10
  loadDir: string | string[];
@@ -6,6 +6,10 @@ const glob_1 = require("@midwayjs/glob");
6
6
  class AbstractFileDetector {
7
7
  constructor(options) {
8
8
  this.options = options;
9
+ this.extraDetectorOptions = {};
10
+ }
11
+ setExtraDetectorOptions(detectorOptions) {
12
+ this.extraDetectorOptions = detectorOptions;
9
13
  }
10
14
  }
11
15
  exports.AbstractFileDetector = AbstractFileDetector;
@@ -29,11 +33,13 @@ class DirectoryFileDetector extends AbstractFileDetector {
29
33
  this.directoryFilterArray = [];
30
34
  }
31
35
  run(container) {
32
- const loadDirs = [].concat(this.options.loadDir || []);
36
+ const loadDirs = []
37
+ .concat(this.options.loadDir || [])
38
+ .concat(this.extraDetectorOptions.loadDir || []);
33
39
  for (const dir of loadDirs) {
34
- const fileResults = (0, glob_1.run)(DEFAULT_PATTERN.concat(this.options.pattern || []), {
40
+ const fileResults = (0, glob_1.run)(DEFAULT_PATTERN.concat(this.options.pattern || []).concat(this.extraDetectorOptions.pattern || []), {
35
41
  cwd: dir,
36
- ignore: DEFAULT_IGNORE_PATTERN.concat(this.options.ignore || []),
42
+ ignore: DEFAULT_IGNORE_PATTERN.concat(this.options.ignore || []).concat(this.extraDetectorOptions.ignore || []),
37
43
  });
38
44
  for (const file of fileResults) {
39
45
  if (this.directoryFilterArray.length) {
@@ -14,6 +14,7 @@ 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");
17
18
  const debug = util.debuglog('midway:debug');
18
19
  const debugBind = util.debuglog('midway:bind');
19
20
  class ContainerConfiguration {
@@ -21,6 +22,7 @@ class ContainerConfiguration {
21
22
  this.container = container;
22
23
  this.loadedMap = new WeakMap();
23
24
  this.namespaceList = [];
25
+ this.detectorOptionsList = [];
24
26
  }
25
27
  load(module) {
26
28
  let namespace = decorator_1.MAIN_MODULE_KEY;
@@ -51,6 +53,9 @@ class ContainerConfiguration {
51
53
  namespace = configurationOptions.namespace;
52
54
  this.namespaceList.push(namespace);
53
55
  }
56
+ if (configurationOptions.detectorOptions) {
57
+ this.detectorOptionsList.push(configurationOptions.detectorOptions);
58
+ }
54
59
  debug(`[core]: load configuration in namespace="${namespace}"`);
55
60
  this.addImports(configurationOptions.imports);
56
61
  this.addImportObjects(configurationOptions.importObjects);
@@ -164,6 +169,9 @@ class ContainerConfiguration {
164
169
  getNamespaceList() {
165
170
  return this.namespaceList;
166
171
  }
172
+ getDetectorOptionsList() {
173
+ return this.detectorOptionsList;
174
+ }
167
175
  }
168
176
  class MidwayContainer {
169
177
  constructor(parent) {
@@ -219,6 +227,7 @@ class MidwayContainer {
219
227
  return this._namespaceSet;
220
228
  }
221
229
  load(module) {
230
+ var _a;
222
231
  this.isLoad = true;
223
232
  if (module) {
224
233
  // load configuration
@@ -228,6 +237,11 @@ class MidwayContainer {
228
237
  this.namespaceSet.add(ns);
229
238
  debug(`[core]: load configuration in namespace="${ns}" complete`);
230
239
  }
240
+ const detectorOptionsMerged = {};
241
+ for (const detectorOptions of configuration.getDetectorOptionsList()) {
242
+ (0, extend_1.extend)(true, detectorOptionsMerged, detectorOptions);
243
+ }
244
+ (_a = this.fileDetector) === null || _a === void 0 ? void 0 : _a.setExtraDetectorOptions(detectorOptionsMerged);
231
245
  }
232
246
  }
233
247
  loadDefinitions() {
@@ -13,6 +13,7 @@ export declare const FrameworkErrorEnum: {
13
13
  readonly USE_WRONG_METHOD: "MIDWAY_10009";
14
14
  readonly SINGLETON_INJECT_REQUEST: "MIDWAY_10010";
15
15
  readonly MISSING_IMPORTS: "MIDWAY_10011";
16
+ readonly UTIL_HTTP_TIMEOUT: "MIDWAY_10012";
16
17
  };
17
18
  export declare class MidwayCommonError extends MidwayError {
18
19
  constructor(message: string);
@@ -50,4 +51,7 @@ export declare class MidwaySingletonInjectRequestError extends MidwayError {
50
51
  export declare class MidwayMissingImportComponentError extends MidwayError {
51
52
  constructor(originName: string);
52
53
  }
54
+ export declare class MidwayUtilHttpClientTimeoutError extends MidwayError {
55
+ constructor(message: string);
56
+ }
53
57
  //# sourceMappingURL=framework.d.ts.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MidwayMissingImportComponentError = exports.MidwaySingletonInjectRequestError = exports.MidwayUseWrongMethodError = exports.MidwayDuplicateRouteError = exports.MidwayResolverMissingError = exports.MidwayConfigMissingError = exports.MidwayFeatureNotImplementedError = exports.MidwayFeatureNoLongerSupportedError = exports.MidwayDefinitionNotFoundError = exports.MidwayParameterError = exports.MidwayCommonError = exports.FrameworkErrorEnum = void 0;
3
+ exports.MidwayUtilHttpClientTimeoutError = exports.MidwayMissingImportComponentError = exports.MidwaySingletonInjectRequestError = exports.MidwayUseWrongMethodError = exports.MidwayDuplicateRouteError = exports.MidwayResolverMissingError = exports.MidwayConfigMissingError = exports.MidwayFeatureNotImplementedError = exports.MidwayFeatureNoLongerSupportedError = exports.MidwayDefinitionNotFoundError = exports.MidwayParameterError = exports.MidwayCommonError = exports.FrameworkErrorEnum = void 0;
4
4
  const base_1 = require("./base");
5
5
  exports.FrameworkErrorEnum = (0, base_1.registerErrorCode)('midway', {
6
6
  UNKNOWN: 10000,
@@ -15,6 +15,7 @@ exports.FrameworkErrorEnum = (0, base_1.registerErrorCode)('midway', {
15
15
  USE_WRONG_METHOD: 10009,
16
16
  SINGLETON_INJECT_REQUEST: 10010,
17
17
  MISSING_IMPORTS: 10011,
18
+ UTIL_HTTP_TIMEOUT: 10012,
18
19
  });
19
20
  class MidwayCommonError extends base_1.MidwayError {
20
21
  constructor(message) {
@@ -100,4 +101,10 @@ class MidwayMissingImportComponentError extends base_1.MidwayError {
100
101
  }
101
102
  }
102
103
  exports.MidwayMissingImportComponentError = MidwayMissingImportComponentError;
104
+ class MidwayUtilHttpClientTimeoutError extends base_1.MidwayError {
105
+ constructor(message) {
106
+ super(message, exports.FrameworkErrorEnum.UTIL_HTTP_TIMEOUT);
107
+ }
108
+ }
109
+ exports.MidwayUtilHttpClientTimeoutError = MidwayUtilHttpClientTimeoutError;
103
110
  //# sourceMappingURL=framework.js.map
package/dist/index.d.ts CHANGED
@@ -28,6 +28,7 @@ export * from './common/fileDetector';
28
28
  export * from './common/webGenerator';
29
29
  export * from './common/middlewareManager';
30
30
  export * from './util/pathToRegexp';
31
+ export * from './util/httpclient';
31
32
  export * from './common/filterManager';
32
33
  export * from './common/applicationManager';
33
34
  export * from './setup';
package/dist/index.js CHANGED
@@ -63,6 +63,7 @@ __exportStar(require("./common/fileDetector"), exports);
63
63
  __exportStar(require("./common/webGenerator"), exports);
64
64
  __exportStar(require("./common/middlewareManager"), exports);
65
65
  __exportStar(require("./util/pathToRegexp"), exports);
66
+ __exportStar(require("./util/httpclient"), exports);
66
67
  __exportStar(require("./common/filterManager"), exports);
67
68
  __exportStar(require("./common/applicationManager"), exports);
68
69
  __exportStar(require("./setup"), exports);
@@ -243,7 +243,8 @@ export interface IMidwayContainer extends IObjectFactory, IObjectLifeCycle {
243
243
  */
244
244
  export declare type IApplicationContext = IMidwayContainer;
245
245
  export interface IFileDetector {
246
- run(container: IMidwayContainer): any;
246
+ run(container: IMidwayContainer, fileDetectorOptions?: Record<string, any>): any;
247
+ setExtraDetectorOptions(detectorOptions: Record<string, any>): any;
247
248
  }
248
249
  export interface IConfigService {
249
250
  add(configFilePaths: any[]): any;
@@ -0,0 +1,21 @@
1
+ /// <reference types="node" />
2
+ import http = require('http');
3
+ declare type MethodType = 'GET' | 'POST';
4
+ declare type MimeType = 'text' | 'json' | undefined;
5
+ interface IOptions {
6
+ method?: MethodType;
7
+ headers?: any;
8
+ contentType?: MimeType;
9
+ dataType?: MimeType;
10
+ data?: any;
11
+ timeout?: number;
12
+ }
13
+ export interface IResponse extends http.IncomingMessage {
14
+ status: number;
15
+ data: Buffer | string | any;
16
+ }
17
+ export declare class HttpClient {
18
+ request(url: string, options?: IOptions): Promise<IResponse>;
19
+ }
20
+ export {};
21
+ //# sourceMappingURL=httpclient.d.ts.map
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpClient = void 0;
4
+ const http = require("http");
5
+ const https = require("https");
6
+ const url = require("url");
7
+ const util_1 = require("util");
8
+ const error_1 = require("../error");
9
+ const debug = (0, util_1.debuglog)('request-client');
10
+ const URL = url.URL;
11
+ const mimeMap = {
12
+ text: 'application/text',
13
+ json: 'application/json',
14
+ octet: 'application/octet-stream',
15
+ };
16
+ class HttpClient {
17
+ async request(url, options = {}) {
18
+ debug(`request '${url}'`);
19
+ const whatwgUrl = new URL(url);
20
+ const client = whatwgUrl.protocol === 'https:' ? https : http;
21
+ const contentType = options.contentType;
22
+ const dataType = options.dataType;
23
+ const method = options.method || 'GET';
24
+ const timeout = options.timeout || 5000;
25
+ const headers = {
26
+ Accept: mimeMap[dataType] || mimeMap.octet,
27
+ ...options.headers,
28
+ };
29
+ let data;
30
+ if (method === 'GET' && options.data) {
31
+ for (const key of Object.keys(options.data)) {
32
+ whatwgUrl.searchParams.set(key, options.data[key]);
33
+ }
34
+ headers['Content-Length'] = 0;
35
+ }
36
+ else if (options.data) {
37
+ data = Buffer.from(JSON.stringify(options.data));
38
+ headers['Content-Type'] = mimeMap[contentType] || mimeMap.octet;
39
+ headers['Content-Length'] = data.byteLength;
40
+ }
41
+ return new Promise((resolve, reject) => {
42
+ const req = client.request(whatwgUrl.toString(), {
43
+ method,
44
+ headers,
45
+ }, res => {
46
+ res.setTimeout(timeout, () => {
47
+ res.destroy(new Error('Response Timeout'));
48
+ });
49
+ res.on('error', error => {
50
+ reject(error);
51
+ });
52
+ const chunks = [];
53
+ res.on('data', chunk => {
54
+ chunks.push(chunk);
55
+ });
56
+ res.on('end', () => {
57
+ let data = Buffer.concat(chunks);
58
+ if (dataType === 'text' || dataType === 'json') {
59
+ data = data.toString('utf8');
60
+ }
61
+ if (dataType === 'json') {
62
+ try {
63
+ data = JSON.parse(data);
64
+ }
65
+ catch (e) {
66
+ return reject(new Error('[httpclient] Unable to parse response data'));
67
+ }
68
+ }
69
+ Object.assign(res, {
70
+ status: res.statusCode,
71
+ data,
72
+ });
73
+ debug(`request '${url}' resolved with status ${res.statusCode}`);
74
+ resolve(res);
75
+ });
76
+ });
77
+ req.setTimeout(timeout, () => {
78
+ req.destroy(new error_1.MidwayUtilHttpClientTimeoutError('Request Timeout'));
79
+ });
80
+ req.on('error', error => {
81
+ reject(error);
82
+ });
83
+ if (method !== 'GET') {
84
+ req.end(data);
85
+ }
86
+ else {
87
+ req.end();
88
+ }
89
+ });
90
+ }
91
+ }
92
+ exports.HttpClient = HttpClient;
93
+ //# sourceMappingURL=httpclient.js.map
@@ -38,7 +38,7 @@ export declare function joinURLPath(...strArray: any[]): string;
38
38
  * @param constructors
39
39
  * @since 2.0.0
40
40
  */
41
- export declare function delegateTargetPrototypeMethod(derivedCtor: any, constructors: any[]): void;
41
+ export declare function delegateTargetPrototypeMethod(derivedCtor: any, constructors: any[], otherMethods?: string[]): void;
42
42
  /**
43
43
  * 代理目标原型上的特定方法
44
44
  * @param derivedCtor
@@ -114,7 +114,7 @@ exports.joinURLPath = joinURLPath;
114
114
  * @param constructors
115
115
  * @since 2.0.0
116
116
  */
117
- function delegateTargetPrototypeMethod(derivedCtor, constructors) {
117
+ function delegateTargetPrototypeMethod(derivedCtor, constructors, otherMethods) {
118
118
  constructors.forEach(baseCtor => {
119
119
  Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
120
120
  if (name !== 'constructor' && !/^_/.test(name)) {
@@ -124,6 +124,9 @@ function delegateTargetPrototypeMethod(derivedCtor, constructors) {
124
124
  }
125
125
  });
126
126
  });
127
+ if (otherMethods) {
128
+ delegateTargetMethod(derivedCtor, otherMethods);
129
+ }
127
130
  }
128
131
  exports.delegateTargetPrototypeMethod = delegateTargetPrototypeMethod;
129
132
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.0.6",
3
+ "version": "3.0.10",
4
4
  "description": "midway core",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -21,10 +21,11 @@
21
21
  ],
22
22
  "license": "MIT",
23
23
  "devDependencies": {
24
- "@midwayjs/decorator": "^3.0.6",
24
+ "@midwayjs/decorator": "^3.0.10",
25
25
  "koa": "2.13.4",
26
26
  "midway-test-component": "*",
27
27
  "mm": "3.2.0",
28
+ "raw-body": "2.5.0",
28
29
  "sinon": "13.0.1"
29
30
  },
30
31
  "peerDependencies": {
@@ -44,5 +45,5 @@
44
45
  "engines": {
45
46
  "node": ">=12"
46
47
  },
47
- "gitHead": "afaa5b59a2be85e915233a9268c0e05965dd5c61"
48
+ "gitHead": "ab02b6b57395d7187c85e1a32767e128c0128e7a"
48
49
  }