@midwayjs/core 3.1.2 → 3.1.7-alpha.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.
- package/dist/common/serviceFactory.d.ts +1 -0
- package/dist/common/serviceFactory.js +3 -0
- package/dist/interface.d.ts +0 -1
- package/dist/setup.d.ts +9 -0
- package/dist/setup.js +50 -38
- package/dist/util/httpclient.d.ts +6 -0
- package/dist/util/httpclient.js +79 -69
- package/dist/util/index.d.ts +1 -0
- package/dist/util/index.js +2 -1
- package/dist/util/pathToRegexp.js +1 -1
- package/package.json +3 -3
|
@@ -6,6 +6,7 @@ export declare abstract class ServiceFactory<T> {
|
|
|
6
6
|
protected options: {};
|
|
7
7
|
protected initClients(options?: any): Promise<void>;
|
|
8
8
|
get<U = T>(id?: string): U;
|
|
9
|
+
has(id: string): boolean;
|
|
9
10
|
createInstance(config: any, clientName?: any): Promise<T | void>;
|
|
10
11
|
abstract getName(): string;
|
|
11
12
|
protected abstract createClient(config: any, clientName: any): Promise<T | void> | (T | void);
|
|
@@ -28,6 +28,9 @@ class ServiceFactory {
|
|
|
28
28
|
get(id = 'default') {
|
|
29
29
|
return this.clients.get(id);
|
|
30
30
|
}
|
|
31
|
+
has(id) {
|
|
32
|
+
return this.clients.has(id);
|
|
33
|
+
}
|
|
31
34
|
async createInstance(config, clientName) {
|
|
32
35
|
// options.default will be merge in to options.clients[id]
|
|
33
36
|
config = Object.assign({}, this.options['default'], config);
|
package/dist/interface.d.ts
CHANGED
|
@@ -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.
|
|
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 {};
|
package/dist/util/httpclient.js
CHANGED
|
@@ -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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
62
|
-
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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;
|
package/dist/util/index.d.ts
CHANGED
|
@@ -36,6 +36,7 @@ export declare function joinURLPath(...strArray: any[]): string;
|
|
|
36
36
|
* 代理目标所有的原型方法,不包括构造器和内部隐藏方法
|
|
37
37
|
* @param derivedCtor
|
|
38
38
|
* @param constructors
|
|
39
|
+
* @param otherMethods
|
|
39
40
|
* @since 2.0.0
|
|
40
41
|
*/
|
|
41
42
|
export declare function delegateTargetPrototypeMethod(derivedCtor: any, constructors: any[], otherMethods?: string[]): void;
|
package/dist/util/index.js
CHANGED
|
@@ -86,7 +86,7 @@ exports.safelyGet = safelyGet;
|
|
|
86
86
|
*/
|
|
87
87
|
function parsePrefix(provideId) {
|
|
88
88
|
if (provideId.includes('@')) {
|
|
89
|
-
return provideId.
|
|
89
|
+
return provideId.slice(1);
|
|
90
90
|
}
|
|
91
91
|
return provideId;
|
|
92
92
|
}
|
|
@@ -112,6 +112,7 @@ exports.joinURLPath = joinURLPath;
|
|
|
112
112
|
* 代理目标所有的原型方法,不包括构造器和内部隐藏方法
|
|
113
113
|
* @param derivedCtor
|
|
114
114
|
* @param constructors
|
|
115
|
+
* @param otherMethods
|
|
115
116
|
* @since 2.0.0
|
|
116
117
|
*/
|
|
117
118
|
function delegateTargetPrototypeMethod(derivedCtor, constructors, otherMethods) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/core",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.7-alpha.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.
|
|
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": "
|
|
48
|
+
"gitHead": "8cb6db25f7eb794d9c61aaf69a43ac6506a289b5"
|
|
49
49
|
}
|