@midwayjs/core 3.1.5 → 3.2.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.
@@ -426,7 +426,6 @@ export interface IMidwayBootstrapOptions {
426
426
  globalConfig?: Array<{
427
427
  [environmentName: string]: Record<string, any>;
428
428
  }> | Record<string, any>;
429
- lazyInitializeFramework?: boolean;
430
429
  }
431
430
  export interface IConfigurationOptions {
432
431
  logger?: ILogger;
package/dist/setup.d.ts CHANGED
@@ -1,4 +1,13 @@
1
1
  import { IMidwayBootstrapOptions, IMidwayContainer } from './';
2
+ /**
3
+ * midway framework main entry, this method bootstrap all service and framework.
4
+ * @param globalOptions
5
+ */
2
6
  export declare function initializeGlobalApplicationContext(globalOptions: IMidwayBootstrapOptions): Promise<IMidwayContainer>;
3
7
  export declare function destroyGlobalApplicationContext(applicationContext: IMidwayContainer): Promise<void>;
8
+ /**
9
+ * prepare applicationContext, it use in egg framework.
10
+ * @param globalOptions
11
+ */
12
+ export declare function prepareGlobalApplicationContext(globalOptions: IMidwayBootstrapOptions): IMidwayContainer;
4
13
  //# sourceMappingURL=setup.d.ts.map
package/dist/setup.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.destroyGlobalApplicationContext = exports.initializeGlobalApplicationContext = void 0;
3
+ exports.prepareGlobalApplicationContext = exports.destroyGlobalApplicationContext = exports.initializeGlobalApplicationContext = void 0;
4
4
  const _1 = require("./");
5
5
  const config_default_1 = require("./config/config.default");
6
6
  const decorator_1 = require("@midwayjs/decorator");
@@ -8,7 +8,55 @@ const util = require("util");
8
8
  const path_1 = require("path");
9
9
  const logger_1 = require("@midwayjs/logger");
10
10
  const debug = util.debuglog('midway:debug');
11
+ /**
12
+ * midway framework main entry, this method bootstrap all service and framework.
13
+ * @param globalOptions
14
+ */
11
15
  async function initializeGlobalApplicationContext(globalOptions) {
16
+ const applicationContext = prepareGlobalApplicationContext(globalOptions);
17
+ // init logger
18
+ const loggerService = await applicationContext.getAsync(_1.MidwayLoggerService, [
19
+ applicationContext,
20
+ ]);
21
+ if (loggerService.getLogger('appLogger')) {
22
+ // register global logger
23
+ applicationContext.registerObject('logger', loggerService.getLogger('appLogger'));
24
+ }
25
+ // framework/config/plugin/logger/app decorator support
26
+ await applicationContext.getAsync(_1.MidwayFrameworkService, [
27
+ applicationContext,
28
+ globalOptions,
29
+ ]);
30
+ // lifecycle support
31
+ await applicationContext.getAsync(_1.MidwayLifeCycleService, [
32
+ applicationContext,
33
+ ]);
34
+ // some preload module init
35
+ const modules = (0, decorator_1.listPreloadModule)();
36
+ for (const module of modules) {
37
+ // preload init context
38
+ await applicationContext.getAsync(module);
39
+ }
40
+ return applicationContext;
41
+ }
42
+ exports.initializeGlobalApplicationContext = initializeGlobalApplicationContext;
43
+ async function destroyGlobalApplicationContext(applicationContext) {
44
+ // stop lifecycle
45
+ const lifecycleService = await applicationContext.getAsync(_1.MidwayLifeCycleService);
46
+ await lifecycleService.stop();
47
+ // stop container
48
+ await applicationContext.stop();
49
+ (0, decorator_1.clearBindContainer)();
50
+ logger_1.loggers.close();
51
+ global['MIDWAY_APPLICATION_CONTEXT'] = undefined;
52
+ global['MIDWAY_MAIN_FRAMEWORK'] = undefined;
53
+ }
54
+ exports.destroyGlobalApplicationContext = destroyGlobalApplicationContext;
55
+ /**
56
+ * prepare applicationContext, it use in egg framework.
57
+ * @param globalOptions
58
+ */
59
+ function prepareGlobalApplicationContext(globalOptions) {
12
60
  var _a, _b, _c, _d;
13
61
  debug('[core]: start "initializeGlobalApplicationContext"');
14
62
  debug(`[core]: bootstrap options = ${util.inspect(globalOptions)}`);
@@ -91,43 +139,7 @@ async function initializeGlobalApplicationContext(globalOptions) {
91
139
  debug('[core]: Current config = %j', configService.getConfiguration());
92
140
  // middleware support
93
141
  applicationContext.get(_1.MidwayMiddlewareService, [applicationContext]);
94
- // it will be delay framework initialize in egg cluster mode
95
- if (!globalOptions.lazyInitializeFramework) {
96
- // init logger
97
- const loggerService = await applicationContext.getAsync(_1.MidwayLoggerService, [applicationContext]);
98
- if (loggerService.getLogger('appLogger')) {
99
- // register global logger
100
- applicationContext.registerObject('logger', loggerService.getLogger('appLogger'));
101
- }
102
- // framework/config/plugin/logger/app decorator support
103
- await applicationContext.getAsync(_1.MidwayFrameworkService, [
104
- applicationContext,
105
- globalOptions,
106
- ]);
107
- // lifecycle support
108
- await applicationContext.getAsync(_1.MidwayLifeCycleService, [
109
- applicationContext,
110
- ]);
111
- // some preload module init
112
- const modules = (0, decorator_1.listPreloadModule)();
113
- for (const module of modules) {
114
- // preload init context
115
- await applicationContext.getAsync(module);
116
- }
117
- }
118
142
  return applicationContext;
119
143
  }
120
- exports.initializeGlobalApplicationContext = initializeGlobalApplicationContext;
121
- async function destroyGlobalApplicationContext(applicationContext) {
122
- // stop lifecycle
123
- const lifecycleService = await applicationContext.getAsync(_1.MidwayLifeCycleService);
124
- await lifecycleService.stop();
125
- // stop container
126
- await applicationContext.stop();
127
- (0, decorator_1.clearBindContainer)();
128
- logger_1.loggers.close();
129
- global['MIDWAY_APPLICATION_CONTEXT'] = undefined;
130
- global['MIDWAY_MAIN_FRAMEWORK'] = undefined;
131
- }
132
- exports.destroyGlobalApplicationContext = destroyGlobalApplicationContext;
144
+ exports.prepareGlobalApplicationContext = prepareGlobalApplicationContext;
133
145
  //# sourceMappingURL=setup.js.map
@@ -14,7 +14,13 @@ export interface IResponse extends http.IncomingMessage {
14
14
  status: number;
15
15
  data: Buffer | string | any;
16
16
  }
17
+ export declare function makeHttpRequest(url: string, options?: IOptions): Promise<IResponse>;
18
+ /**
19
+ * A simple http client
20
+ */
17
21
  export declare class HttpClient {
22
+ readonly defaultOptions: Pick<IOptions, 'headers' | 'timeout' | 'method'>;
23
+ constructor(defaultOptions?: Pick<IOptions, 'headers' | 'timeout' | 'method'>);
18
24
  request(url: string, options?: IOptions): Promise<IResponse>;
19
25
  }
20
26
  export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HttpClient = void 0;
3
+ exports.HttpClient = exports.makeHttpRequest = void 0;
4
4
  const http = require("http");
5
5
  const https = require("https");
6
6
  const url = require("url");
@@ -13,80 +13,90 @@ const mimeMap = {
13
13
  json: 'application/json',
14
14
  octet: 'application/octet-stream',
15
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;
16
+ async function makeHttpRequest(url, options = {}) {
17
+ debug(`request '${url}'`);
18
+ const whatwgUrl = new URL(url);
19
+ const client = whatwgUrl.protocol === 'https:' ? https : http;
20
+ const contentType = options.contentType;
21
+ const dataType = options.dataType;
22
+ const method = options.method || 'GET';
23
+ const timeout = options.timeout || 5000;
24
+ const headers = {
25
+ Accept: mimeMap[dataType] || mimeMap.octet,
26
+ ...options.headers,
27
+ };
28
+ let data;
29
+ if (method === 'GET' && options.data) {
30
+ for (const key of Object.keys(options.data)) {
31
+ whatwgUrl.searchParams.set(key, options.data[key]);
40
32
  }
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');
33
+ headers['Content-Length'] = 0;
34
+ }
35
+ else if (options.data) {
36
+ data = Buffer.from(JSON.stringify(options.data));
37
+ headers['Content-Type'] = mimeMap[contentType] || mimeMap.octet;
38
+ headers['Content-Length'] = data.byteLength;
39
+ }
40
+ return new Promise((resolve, reject) => {
41
+ const req = client.request(whatwgUrl.toString(), {
42
+ method,
43
+ headers,
44
+ }, res => {
45
+ res.setTimeout(timeout, () => {
46
+ res.destroy(new Error('Response Timeout'));
47
+ });
48
+ res.on('error', error => {
49
+ reject(error);
50
+ });
51
+ const chunks = [];
52
+ res.on('data', chunk => {
53
+ chunks.push(chunk);
54
+ });
55
+ res.on('end', () => {
56
+ let data = Buffer.concat(chunks);
57
+ if (dataType === 'text' || dataType === 'json') {
58
+ data = data.toString('utf8');
59
+ }
60
+ if (dataType === 'json') {
61
+ try {
62
+ data = JSON.parse(data);
60
63
  }
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
- }
64
+ catch (e) {
65
+ return reject(new Error('[httpclient] Unable to parse response data'));
68
66
  }
69
- Object.assign(res, {
70
- status: res.statusCode,
71
- data,
72
- });
73
- debug(`request '${url}' resolved with status ${res.statusCode}`);
74
- resolve(res);
67
+ }
68
+ Object.assign(res, {
69
+ status: res.statusCode,
70
+ data,
75
71
  });
72
+ debug(`request '${url}' resolved with status ${res.statusCode}`);
73
+ resolve(res);
76
74
  });
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
75
  });
76
+ req.setTimeout(timeout, () => {
77
+ req.destroy(new error_1.MidwayUtilHttpClientTimeoutError('Request Timeout'));
78
+ });
79
+ req.on('error', error => {
80
+ reject(error);
81
+ });
82
+ if (method !== 'GET') {
83
+ req.end(data);
84
+ }
85
+ else {
86
+ req.end();
87
+ }
88
+ });
89
+ }
90
+ exports.makeHttpRequest = makeHttpRequest;
91
+ /**
92
+ * A simple http client
93
+ */
94
+ class HttpClient {
95
+ constructor(defaultOptions = {}) {
96
+ this.defaultOptions = defaultOptions;
97
+ }
98
+ async request(url, options) {
99
+ return makeHttpRequest(url, Object.assign(this.defaultOptions, options));
90
100
  }
91
101
  }
92
102
  exports.HttpClient = HttpClient;
@@ -86,7 +86,7 @@ exports.safelyGet = safelyGet;
86
86
  */
87
87
  function parsePrefix(provideId) {
88
88
  if (provideId.includes('@')) {
89
- return provideId.substr(1);
89
+ return provideId.slice(1);
90
90
  }
91
91
  return provideId;
92
92
  }
@@ -80,7 +80,7 @@ function parse(str, options) {
80
80
  }
81
81
  // Match any characters still remaining.
82
82
  if (index < str.length) {
83
- path += str.substr(index);
83
+ path += str.slice(index);
84
84
  }
85
85
  // If the path exists, push it onto the end.
86
86
  if (path) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.1.5",
3
+ "version": "3.2.0",
4
4
  "description": "midway core",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -21,7 +21,7 @@
21
21
  ],
22
22
  "license": "MIT",
23
23
  "devDependencies": {
24
- "@midwayjs/decorator": "^3.0.10",
24
+ "@midwayjs/decorator": "^3.1.6",
25
25
  "koa": "2.13.4",
26
26
  "midway-test-component": "*",
27
27
  "mm": "3.2.0",
@@ -45,5 +45,5 @@
45
45
  "engines": {
46
46
  "node": ">=12"
47
47
  },
48
- "gitHead": "46a7851ad3ce6fc675bd7e1b7c9dff2c70009104"
48
+ "gitHead": "cee3e5ddbb4f0cc5f80176a7b1e271e809686a97"
49
49
  }