@midwayjs/core 4.0.0-beta.11 → 4.0.0-beta.13
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/baseFramework.js +18 -1
- package/dist/common/asyncContextManager.js +0 -1
- package/dist/common/dataSourceManager.d.ts +1 -1
- package/dist/common/fileDetector.d.ts +1 -0
- package/dist/common/fileDetector.js +26 -0
- package/dist/common/webGenerator.js +3 -1
- package/dist/config/config.default.d.ts +2 -2
- package/dist/config/config.default.js +6 -1
- package/dist/context/componentLoader.js +1 -1
- package/dist/context/container.js +7 -0
- package/dist/decorator/common/tracer.d.ts +9 -0
- package/dist/decorator/common/tracer.js +18 -0
- package/dist/decorator/constant.d.ts +1 -0
- package/dist/decorator/constant.js +2 -1
- package/dist/decorator/index.d.ts +1 -0
- package/dist/decorator/index.js +1 -0
- package/dist/decorator/metadataManager.js +0 -2
- package/dist/decorator/web/requestMapping.d.ts +4 -0
- package/dist/decorator/web/requestMapping.js +14 -3
- package/dist/error/framework.d.ts +13 -1
- package/dist/error/framework.js +3 -1
- package/dist/functional/adapter.d.ts +4 -0
- package/dist/functional/adapter.js +7 -0
- package/dist/functional/api.d.ts +68 -0
- package/dist/functional/api.js +262 -0
- package/dist/functional/constants.d.ts +3 -0
- package/dist/functional/constants.js +6 -0
- package/dist/functional/hooks.js +0 -1
- package/dist/functional/index.d.ts +3 -0
- package/dist/functional/index.js +7 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/dist/interface.d.ts +29 -3
- package/dist/interface.js +1 -1
- package/dist/service/configService.js +0 -1
- package/dist/service/lifeCycleService.js +3 -1
- package/dist/service/traceService.d.ts +49 -0
- package/dist/service/traceService.js +306 -0
- package/dist/service/webRouterService.d.ts +40 -0
- package/dist/service/webRouterService.js +197 -45
- package/dist/setup.js +4 -0
- package/dist/util/index.d.ts +1 -0
- package/dist/util/index.js +78 -2
- package/package.json +3 -2
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defineApi = defineApi;
|
|
4
|
+
const decorator_1 = require("../decorator");
|
|
5
|
+
const error_1 = require("../error");
|
|
6
|
+
const metadataManager_1 = require("../decorator/metadataManager");
|
|
7
|
+
const util_1 = require("util");
|
|
8
|
+
const crypto_1 = require("crypto");
|
|
9
|
+
const constants_1 = require("./constants");
|
|
10
|
+
const debug = (0, util_1.debuglog)('midway:debug');
|
|
11
|
+
function normalizeClassNameSegment(input, fallback = 'root') {
|
|
12
|
+
const normalized = (input || '')
|
|
13
|
+
.replace(/[^a-zA-Z0-9_$]+/g, '_')
|
|
14
|
+
.replace(/^(\d)/, '_$1')
|
|
15
|
+
.replace(/^_+|_+$/g, '')
|
|
16
|
+
.slice(0, 40);
|
|
17
|
+
return normalized || fallback;
|
|
18
|
+
}
|
|
19
|
+
function createNamedFunctionalController(prefix, routeNames) {
|
|
20
|
+
const prefixPart = normalizeClassNameSegment(prefix);
|
|
21
|
+
const hash = (0, crypto_1.createHash)('sha1')
|
|
22
|
+
.update(`${prefix}|${routeNames.join('|')}`)
|
|
23
|
+
.digest('hex')
|
|
24
|
+
.slice(0, 8);
|
|
25
|
+
const className = `FunctionalApi_${prefixPart}_${hash}`;
|
|
26
|
+
return {
|
|
27
|
+
[className]: class {
|
|
28
|
+
},
|
|
29
|
+
}[className];
|
|
30
|
+
}
|
|
31
|
+
const HTTP_METHODS = [
|
|
32
|
+
decorator_1.RequestMethod.GET,
|
|
33
|
+
decorator_1.RequestMethod.POST,
|
|
34
|
+
decorator_1.RequestMethod.PUT,
|
|
35
|
+
decorator_1.RequestMethod.DELETE,
|
|
36
|
+
decorator_1.RequestMethod.PATCH,
|
|
37
|
+
decorator_1.RequestMethod.OPTIONS,
|
|
38
|
+
decorator_1.RequestMethod.HEAD,
|
|
39
|
+
decorator_1.RequestMethod.ALL,
|
|
40
|
+
];
|
|
41
|
+
function createRouteBuilder(method, path = '/') {
|
|
42
|
+
const route = {
|
|
43
|
+
method,
|
|
44
|
+
path,
|
|
45
|
+
options: {
|
|
46
|
+
middleware: [],
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
const builder = {
|
|
50
|
+
__isRouteBuilder: true,
|
|
51
|
+
input(schema) {
|
|
52
|
+
route.options.input = schema;
|
|
53
|
+
return builder;
|
|
54
|
+
},
|
|
55
|
+
output(schema) {
|
|
56
|
+
route.options.output = schema;
|
|
57
|
+
return builder;
|
|
58
|
+
},
|
|
59
|
+
middleware(mw) {
|
|
60
|
+
route.options.middleware = mw || [];
|
|
61
|
+
return builder;
|
|
62
|
+
},
|
|
63
|
+
meta(options) {
|
|
64
|
+
route.options = {
|
|
65
|
+
...route.options,
|
|
66
|
+
...(options || {}),
|
|
67
|
+
};
|
|
68
|
+
return builder;
|
|
69
|
+
},
|
|
70
|
+
handle(fn) {
|
|
71
|
+
route.handle = fn;
|
|
72
|
+
return builder.__build();
|
|
73
|
+
},
|
|
74
|
+
__build() {
|
|
75
|
+
if (!route.handle) {
|
|
76
|
+
throw new Error('Functional route is missing handler, call .handle(fn) to finish route definition');
|
|
77
|
+
}
|
|
78
|
+
return route;
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
return builder;
|
|
82
|
+
}
|
|
83
|
+
function getInputFromContext(ctx) {
|
|
84
|
+
return {
|
|
85
|
+
params: ctx?.params,
|
|
86
|
+
query: ctx?.query,
|
|
87
|
+
body: ctx?.request?.body,
|
|
88
|
+
headers: ctx?.headers ?? ctx?.request?.headers,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
async function runSchemaValidation(schema, value, label) {
|
|
92
|
+
if (!schema) {
|
|
93
|
+
return value;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
if (typeof schema.safeParseAsync === 'function') {
|
|
97
|
+
const result = await schema.safeParseAsync(value);
|
|
98
|
+
if (!result.success) {
|
|
99
|
+
throw result.error;
|
|
100
|
+
}
|
|
101
|
+
return result.data;
|
|
102
|
+
}
|
|
103
|
+
if (typeof schema.safeParse === 'function') {
|
|
104
|
+
const result = schema.safeParse(value);
|
|
105
|
+
if (!result.success) {
|
|
106
|
+
throw result.error;
|
|
107
|
+
}
|
|
108
|
+
return result.data;
|
|
109
|
+
}
|
|
110
|
+
if (typeof schema.parseAsync === 'function') {
|
|
111
|
+
return await schema.parseAsync(value);
|
|
112
|
+
}
|
|
113
|
+
if (typeof schema.parse === 'function') {
|
|
114
|
+
return schema.parse(value);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
throw new error_1.MidwayCommonError(`Functional API ${label} validation failed: ${err?.message || err}`);
|
|
119
|
+
}
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
122
|
+
async function validateInput(schema, input) {
|
|
123
|
+
if (!schema) {
|
|
124
|
+
return input;
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
params: await runSchemaValidation(schema.params, input.params, 'input.params'),
|
|
128
|
+
query: await runSchemaValidation(schema.query, input.query, 'input.query'),
|
|
129
|
+
body: await runSchemaValidation(schema.body, input.body, 'input.body'),
|
|
130
|
+
headers: await runSchemaValidation(schema.headers, input.headers, 'input.headers'),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function normalizeRouteDefinition(routeName, routeValue) {
|
|
134
|
+
const route = routeValue?.__isRouteBuilder
|
|
135
|
+
? routeValue.__build()
|
|
136
|
+
: routeValue;
|
|
137
|
+
if (!route || typeof route !== 'object') {
|
|
138
|
+
throw new Error(`Functional route "${routeName}" must be a route definition`);
|
|
139
|
+
}
|
|
140
|
+
if (typeof route.handle !== 'function') {
|
|
141
|
+
throw new Error(`Functional route "${routeName}" is missing handler, call .handle(fn)`);
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
...route,
|
|
145
|
+
path: route.path || '/',
|
|
146
|
+
options: {
|
|
147
|
+
middleware: [],
|
|
148
|
+
...route.options,
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
function defineApi(prefix, factory, controllerOptions = {
|
|
153
|
+
middleware: [],
|
|
154
|
+
sensitive: true,
|
|
155
|
+
}) {
|
|
156
|
+
const routeFactory = {
|
|
157
|
+
get(path = '/') {
|
|
158
|
+
return createRouteBuilder(decorator_1.RequestMethod.GET, path);
|
|
159
|
+
},
|
|
160
|
+
post(path = '/') {
|
|
161
|
+
return createRouteBuilder(decorator_1.RequestMethod.POST, path);
|
|
162
|
+
},
|
|
163
|
+
put(path = '/') {
|
|
164
|
+
return createRouteBuilder(decorator_1.RequestMethod.PUT, path);
|
|
165
|
+
},
|
|
166
|
+
delete(path = '/') {
|
|
167
|
+
return createRouteBuilder(decorator_1.RequestMethod.DELETE, path);
|
|
168
|
+
},
|
|
169
|
+
patch(path = '/') {
|
|
170
|
+
return createRouteBuilder(decorator_1.RequestMethod.PATCH, path);
|
|
171
|
+
},
|
|
172
|
+
options(path = '/') {
|
|
173
|
+
return createRouteBuilder(decorator_1.RequestMethod.OPTIONS, path);
|
|
174
|
+
},
|
|
175
|
+
head(path = '/') {
|
|
176
|
+
return createRouteBuilder(decorator_1.RequestMethod.HEAD, path);
|
|
177
|
+
},
|
|
178
|
+
all(path = '/') {
|
|
179
|
+
return createRouteBuilder(decorator_1.RequestMethod.ALL, path);
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
const definedRoutes = factory(routeFactory);
|
|
183
|
+
const normalizedRoutes = {};
|
|
184
|
+
const routeNames = Object.keys(definedRoutes || {});
|
|
185
|
+
const FunctionalApiController = createNamedFunctionalController(prefix, routeNames);
|
|
186
|
+
const controllerClassName = FunctionalApiController.name;
|
|
187
|
+
debug(`[functional-api] begin register routes, controller="${controllerClassName}", prefix="${prefix}", count=${routeNames.length}`);
|
|
188
|
+
for (const routeName of routeNames) {
|
|
189
|
+
const route = normalizeRouteDefinition(routeName, definedRoutes[routeName]);
|
|
190
|
+
normalizedRoutes[routeName] = route;
|
|
191
|
+
if (!HTTP_METHODS.includes(route.method)) {
|
|
192
|
+
throw new Error(`Functional route "${routeName}" has unsupported http method "${route.method}"`);
|
|
193
|
+
}
|
|
194
|
+
Object.defineProperty(FunctionalApiController.prototype, routeName, {
|
|
195
|
+
value: async function (ctx, next) {
|
|
196
|
+
const rawInput = getInputFromContext(ctx);
|
|
197
|
+
const validatedInput = await validateInput(route.options.input, rawInput);
|
|
198
|
+
const result = await route.handle({
|
|
199
|
+
input: validatedInput,
|
|
200
|
+
ctx,
|
|
201
|
+
next,
|
|
202
|
+
});
|
|
203
|
+
return runSchemaValidation(route.options.output, result, 'output');
|
|
204
|
+
},
|
|
205
|
+
writable: false,
|
|
206
|
+
enumerable: false,
|
|
207
|
+
configurable: false,
|
|
208
|
+
});
|
|
209
|
+
const descriptor = Object.getOwnPropertyDescriptor(FunctionalApiController.prototype, routeName);
|
|
210
|
+
(0, decorator_1.RequestMapping)({
|
|
211
|
+
path: route.path,
|
|
212
|
+
requestMethod: route.method,
|
|
213
|
+
routerName: route.options.routerName,
|
|
214
|
+
middleware: route.options.middleware || [],
|
|
215
|
+
summary: route.options.summary,
|
|
216
|
+
description: route.options.description,
|
|
217
|
+
ignoreGlobalPrefix: route.options.ignoreGlobalPrefix,
|
|
218
|
+
})(FunctionalApiController.prototype, routeName, descriptor);
|
|
219
|
+
debug(`[functional-api] register route, controller="${controllerClassName}", prefix="${prefix}", method=${String(route.method).toUpperCase()}, path="${String(route.path)}", routeName="${routeName}", routerName="${route.options.routerName || routeName}"`);
|
|
220
|
+
}
|
|
221
|
+
(0, decorator_1.Controller)(prefix, controllerOptions)(FunctionalApiController);
|
|
222
|
+
// Keep compatibility with mock container module filtering:
|
|
223
|
+
// class-based controllers are marked via container.bindClass(onBeforeBind),
|
|
224
|
+
// while defineApi creates an internal class that may not be bound directly.
|
|
225
|
+
// If mock bind map exists, mark this generated controller as bound.
|
|
226
|
+
const bindModuleMap = decorator_1.DecoratorManager?._bindModuleMap;
|
|
227
|
+
if (bindModuleMap && typeof bindModuleMap.set === 'function') {
|
|
228
|
+
bindModuleMap.set(FunctionalApiController, true);
|
|
229
|
+
}
|
|
230
|
+
// In some integration paths (e.g. mixed runtime loaders), make sure
|
|
231
|
+
// functional controllers are visible to the active/global decorator manager.
|
|
232
|
+
decorator_1.DecoratorManager.saveModule(decorator_1.CONTROLLER_KEY, FunctionalApiController);
|
|
233
|
+
const globalDecoratorManager = globalThis['MIDWAY_GLOBAL_DECORATOR_MANAGER'];
|
|
234
|
+
if (globalDecoratorManager &&
|
|
235
|
+
globalDecoratorManager !== decorator_1.DecoratorManager &&
|
|
236
|
+
typeof globalDecoratorManager.saveModule === 'function') {
|
|
237
|
+
globalDecoratorManager.saveModule(decorator_1.CONTROLLER_KEY, FunctionalApiController);
|
|
238
|
+
}
|
|
239
|
+
metadataManager_1.MetadataManager.defineMetadata(decorator_1.FUNCTIONAL_API_CONTROLLER_KEY, true, FunctionalApiController);
|
|
240
|
+
Object.defineProperty(normalizedRoutes, constants_1.FUNCTIONAL_API_MODULE_META_KEY, {
|
|
241
|
+
value: {
|
|
242
|
+
prefix,
|
|
243
|
+
ignoreGlobalPrefix: controllerOptions?.ignoreGlobalPrefix,
|
|
244
|
+
version: controllerOptions?.version,
|
|
245
|
+
versionType: controllerOptions?.versionType,
|
|
246
|
+
versionPrefix: controllerOptions?.versionPrefix,
|
|
247
|
+
},
|
|
248
|
+
enumerable: false,
|
|
249
|
+
configurable: false,
|
|
250
|
+
writable: false,
|
|
251
|
+
});
|
|
252
|
+
metadataManager_1.MetadataManager.defineMetadata(constants_1.FUNCTIONAL_API_CONTROLLER_CLASS_KEY, FunctionalApiController, normalizedRoutes);
|
|
253
|
+
// Keep hidden property for compatibility with existing ecosystem reads.
|
|
254
|
+
Object.defineProperty(normalizedRoutes, constants_1.FUNCTIONAL_API_CONTROLLER_CLASS_KEY, {
|
|
255
|
+
value: FunctionalApiController,
|
|
256
|
+
enumerable: false,
|
|
257
|
+
configurable: false,
|
|
258
|
+
writable: false,
|
|
259
|
+
});
|
|
260
|
+
return normalizedRoutes;
|
|
261
|
+
}
|
|
262
|
+
//# sourceMappingURL=api.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FUNCTIONAL_API_CONTROLLER_CLASS_KEY = exports.FUNCTIONAL_API_MODULE_META_KEY = void 0;
|
|
4
|
+
exports.FUNCTIONAL_API_MODULE_META_KEY = '__midwayApiMeta';
|
|
5
|
+
exports.FUNCTIONAL_API_CONTROLLER_CLASS_KEY = '__midwayApiControllerClass';
|
|
6
|
+
//# sourceMappingURL=constants.js.map
|
package/dist/functional/hooks.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
export { defineConfiguration, FunctionalConfiguration } from './configuration';
|
|
2
|
+
export { defineApi, FunctionalControllerOptions, FunctionalRouteDefinition, FunctionalRouteInput, FunctionalRouteOptions, RouteBuilder, } from './api';
|
|
3
|
+
export { FUNCTIONAL_API_CONTROLLER_CLASS_KEY, FUNCTIONAL_API_MODULE_META_KEY, } from './constants';
|
|
2
4
|
export * from './hooks';
|
|
5
|
+
export * from './adapter';
|
|
3
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/functional/index.js
CHANGED
|
@@ -14,9 +14,15 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.FunctionalConfiguration = exports.defineConfiguration = void 0;
|
|
17
|
+
exports.FUNCTIONAL_API_MODULE_META_KEY = exports.FUNCTIONAL_API_CONTROLLER_CLASS_KEY = exports.defineApi = exports.FunctionalConfiguration = exports.defineConfiguration = void 0;
|
|
18
18
|
var configuration_1 = require("./configuration");
|
|
19
19
|
Object.defineProperty(exports, "defineConfiguration", { enumerable: true, get: function () { return configuration_1.defineConfiguration; } });
|
|
20
20
|
Object.defineProperty(exports, "FunctionalConfiguration", { enumerable: true, get: function () { return configuration_1.FunctionalConfiguration; } });
|
|
21
|
+
var api_1 = require("./api");
|
|
22
|
+
Object.defineProperty(exports, "defineApi", { enumerable: true, get: function () { return api_1.defineApi; } });
|
|
23
|
+
var constants_1 = require("./constants");
|
|
24
|
+
Object.defineProperty(exports, "FUNCTIONAL_API_CONTROLLER_CLASS_KEY", { enumerable: true, get: function () { return constants_1.FUNCTIONAL_API_CONTROLLER_CLASS_KEY; } });
|
|
25
|
+
Object.defineProperty(exports, "FUNCTIONAL_API_MODULE_META_KEY", { enumerable: true, get: function () { return constants_1.FUNCTIONAL_API_MODULE_META_KEY; } });
|
|
21
26
|
__exportStar(require("./hooks"), exports);
|
|
27
|
+
__exportStar(require("./adapter"), exports);
|
|
22
28
|
//# sourceMappingURL=index.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ export { MidwayMockService } from './service/mockService';
|
|
|
17
17
|
export { MidwayHealthService } from './service/healthService';
|
|
18
18
|
export { RouterInfo, DynamicRouterInfo, RouterPriority, RouterCollectorOptions, MidwayWebRouterService, } from './service/webRouterService';
|
|
19
19
|
export { MidwayServerlessFunctionService } from './service/slsFunctionService';
|
|
20
|
+
export { MidwayTraceService } from './service/traceService';
|
|
20
21
|
export { DataSourceManager } from './common/dataSourceManager';
|
|
21
22
|
export { DEFAULT_PRIORITY, MidwayPriorityManager, } from './common/priorityManager';
|
|
22
23
|
export * from './common/loggerFactory';
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.DynamicMidwayContainer = exports.MidwayPerformanceManager = exports.TypedResourceManager = exports.HttpServerResponse = exports.ServerResponse = exports.NetworkUtils = exports.FORMAT = exports.FileUtils = exports.PathFileUtils = exports.Types = exports.retryWith = exports.retryWithAsync = exports.extend = exports.Utils = exports.sleep = exports.isTypeScriptEnvironment = exports.wrapAsync = exports.wrapMiddleware = exports.pathMatching = exports.transformRequestObjectByType = exports.deprecatedOutput = exports.delegateTargetAllPrototypeMethod = exports.delegateTargetProperties = exports.delegateTargetMethod = exports.delegateTargetPrototypeMethod = exports.loadModule = exports.safeRequire = exports.safelyGet = exports.ASYNC_ROOT_CONTEXT = exports.MidwayPriorityManager = exports.DEFAULT_PRIORITY = exports.DataSourceManager = exports.MidwayServerlessFunctionService = exports.MidwayWebRouterService = exports.MidwayHealthService = exports.MidwayMockService = exports.MidwayDecoratorService = exports.MidwayMiddlewareService = exports.MidwayLifeCycleService = exports.MidwayAspectService = exports.MidwayFrameworkService = exports.MidwayLoggerService = exports.MidwayInformationService = exports.MidwayEnvironmentService = exports.MidwayConfigService = exports.BaseFramework = exports.MidwayRequestContainer = void 0;
|
|
17
|
+
exports.DynamicMidwayContainer = exports.MidwayPerformanceManager = exports.TypedResourceManager = exports.HttpServerResponse = exports.ServerResponse = exports.NetworkUtils = exports.FORMAT = exports.FileUtils = exports.PathFileUtils = exports.Types = exports.retryWith = exports.retryWithAsync = exports.extend = exports.Utils = exports.sleep = exports.isTypeScriptEnvironment = exports.wrapAsync = exports.wrapMiddleware = exports.pathMatching = exports.transformRequestObjectByType = exports.deprecatedOutput = exports.delegateTargetAllPrototypeMethod = exports.delegateTargetProperties = exports.delegateTargetMethod = exports.delegateTargetPrototypeMethod = exports.loadModule = exports.safeRequire = exports.safelyGet = exports.ASYNC_ROOT_CONTEXT = exports.MidwayPriorityManager = exports.DEFAULT_PRIORITY = exports.DataSourceManager = exports.MidwayTraceService = exports.MidwayServerlessFunctionService = exports.MidwayWebRouterService = exports.MidwayHealthService = exports.MidwayMockService = exports.MidwayDecoratorService = exports.MidwayMiddlewareService = exports.MidwayLifeCycleService = exports.MidwayAspectService = exports.MidwayFrameworkService = exports.MidwayLoggerService = exports.MidwayInformationService = exports.MidwayEnvironmentService = exports.MidwayConfigService = exports.BaseFramework = exports.MidwayRequestContainer = void 0;
|
|
18
18
|
__exportStar(require("./interface"), exports);
|
|
19
19
|
__exportStar(require("./context/container"), exports);
|
|
20
20
|
var requestContainer_1 = require("./context/requestContainer");
|
|
@@ -49,6 +49,8 @@ var webRouterService_1 = require("./service/webRouterService");
|
|
|
49
49
|
Object.defineProperty(exports, "MidwayWebRouterService", { enumerable: true, get: function () { return webRouterService_1.MidwayWebRouterService; } });
|
|
50
50
|
var slsFunctionService_1 = require("./service/slsFunctionService");
|
|
51
51
|
Object.defineProperty(exports, "MidwayServerlessFunctionService", { enumerable: true, get: function () { return slsFunctionService_1.MidwayServerlessFunctionService; } });
|
|
52
|
+
var traceService_1 = require("./service/traceService");
|
|
53
|
+
Object.defineProperty(exports, "MidwayTraceService", { enumerable: true, get: function () { return traceService_1.MidwayTraceService; } });
|
|
52
54
|
var dataSourceManager_1 = require("./common/dataSourceManager");
|
|
53
55
|
Object.defineProperty(exports, "DataSourceManager", { enumerable: true, get: function () { return dataSourceManager_1.DataSourceManager; } });
|
|
54
56
|
var priorityManager_1 = require("./common/priorityManager");
|
package/dist/interface.d.ts
CHANGED
|
@@ -386,7 +386,32 @@ export interface MidwayCoreDefaultConfig {
|
|
|
386
386
|
serverReadyTimeout?: number;
|
|
387
387
|
stopTimeout?: number;
|
|
388
388
|
};
|
|
389
|
+
tracing?: {
|
|
390
|
+
enable?: boolean;
|
|
391
|
+
onError?: 'throw' | 'ignore';
|
|
392
|
+
logOnError?: boolean;
|
|
393
|
+
};
|
|
389
394
|
}
|
|
395
|
+
export type TraceMetaDirection = 'entry' | 'exit';
|
|
396
|
+
export type TraceMetaValue = string | number | boolean | undefined | null;
|
|
397
|
+
export type TraceMetaRecord = Record<string, TraceMetaValue>;
|
|
398
|
+
export interface TraceMetaResolverArgs {
|
|
399
|
+
direction: TraceMetaDirection;
|
|
400
|
+
protocol: string;
|
|
401
|
+
spanName: string;
|
|
402
|
+
span?: unknown;
|
|
403
|
+
ctx?: unknown;
|
|
404
|
+
carrier?: unknown;
|
|
405
|
+
request?: unknown;
|
|
406
|
+
response?: unknown;
|
|
407
|
+
error?: Error;
|
|
408
|
+
custom?: Record<string, unknown>;
|
|
409
|
+
}
|
|
410
|
+
export type TraceMetaResolver = TraceMetaRecord | ((args: TraceMetaResolverArgs) => TraceMetaRecord) | {
|
|
411
|
+
common?: TraceMetaRecord | ((args: TraceMetaResolverArgs) => TraceMetaRecord);
|
|
412
|
+
entry?: TraceMetaRecord | ((args: TraceMetaResolverArgs) => TraceMetaRecord);
|
|
413
|
+
exit?: TraceMetaRecord | ((args: TraceMetaResolverArgs) => TraceMetaRecord);
|
|
414
|
+
};
|
|
390
415
|
export type ServiceFactoryConfigOption<OPTIONS> = {
|
|
391
416
|
default?: PowerPartial<OPTIONS>;
|
|
392
417
|
client?: PowerPartial<OPTIONS>;
|
|
@@ -622,7 +647,7 @@ export interface IMidwayContainer extends IObjectFactory {
|
|
|
622
647
|
/**
|
|
623
648
|
* Get IoC identifier
|
|
624
649
|
*/
|
|
625
|
-
getIdentifier(identifier:
|
|
650
|
+
getIdentifier(identifier: ClassType | string): string;
|
|
626
651
|
/**
|
|
627
652
|
* Get instance IoC container scope
|
|
628
653
|
* @param instance
|
|
@@ -663,6 +688,7 @@ export interface Context {
|
|
|
663
688
|
* Custom properties.
|
|
664
689
|
*/
|
|
665
690
|
requestContext: IMidwayContainer;
|
|
691
|
+
traceId?: string;
|
|
666
692
|
logger: ILogger;
|
|
667
693
|
getLogger(name?: string): ILogger;
|
|
668
694
|
startTime: number;
|
|
@@ -1010,7 +1036,7 @@ export declare const LoadBalancerType: {
|
|
|
1010
1036
|
readonly RANDOM: "random";
|
|
1011
1037
|
readonly ROUND_ROBIN: "roundRobin";
|
|
1012
1038
|
};
|
|
1013
|
-
export type LoadBalancerType = typeof LoadBalancerType[keyof typeof LoadBalancerType];
|
|
1039
|
+
export type LoadBalancerType = (typeof LoadBalancerType)[keyof typeof LoadBalancerType];
|
|
1014
1040
|
export declare const ServiceDiscoveryHealthCheckType: {
|
|
1015
1041
|
readonly SELF: "self";
|
|
1016
1042
|
readonly TTL: "ttl";
|
|
@@ -1018,7 +1044,7 @@ export declare const ServiceDiscoveryHealthCheckType: {
|
|
|
1018
1044
|
readonly TCP: "tcp";
|
|
1019
1045
|
readonly CUSTOM: "custom";
|
|
1020
1046
|
};
|
|
1021
|
-
export type ServiceDiscoveryHealthCheckType = typeof ServiceDiscoveryHealthCheckType[keyof typeof ServiceDiscoveryHealthCheckType];
|
|
1047
|
+
export type ServiceDiscoveryHealthCheckType = (typeof ServiceDiscoveryHealthCheckType)[keyof typeof ServiceDiscoveryHealthCheckType];
|
|
1022
1048
|
/**
|
|
1023
1049
|
* 基础健康检查配置
|
|
1024
1050
|
*/
|
package/dist/interface.js
CHANGED
|
@@ -133,7 +133,6 @@ let MidwayConfigService = class MidwayConfigService {
|
|
|
133
133
|
for (const [idx, filename] of [...defaultSet, ...currentEnvSet].entries()) {
|
|
134
134
|
let config = this.loadConfig(filename);
|
|
135
135
|
if (types_1.Types.isFunction(config)) {
|
|
136
|
-
// eslint-disable-next-line prefer-spread
|
|
137
136
|
config = config.apply(null, [this.appInfo, target]);
|
|
138
137
|
}
|
|
139
138
|
if (!config) {
|
|
@@ -47,7 +47,9 @@ let MidwayLifeCycleService = class MidwayLifeCycleService {
|
|
|
47
47
|
debug(`[core]: Lifecycle run ${cycle.namespace} init`);
|
|
48
48
|
cycle.instance = await this.applicationContext.getAsync(cycle.target);
|
|
49
49
|
}
|
|
50
|
-
cycle.instance
|
|
50
|
+
if (cycle.instance) {
|
|
51
|
+
this.lifecycleInstanceList.push(cycle);
|
|
52
|
+
}
|
|
51
53
|
}
|
|
52
54
|
// init health check service
|
|
53
55
|
await this.healthService.init(this.lifecycleInstanceList);
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Span, SpanKind, TextMapGetter, TextMapSetter } from '@opentelemetry/api';
|
|
2
|
+
import { ILogger, IMidwayApplication, TraceMetaRecord, TraceMetaResolver, TraceMetaResolverArgs } from '../interface';
|
|
3
|
+
import { MidwayDecoratorService } from './decoratorService';
|
|
4
|
+
export declare class MidwayTraceService {
|
|
5
|
+
private currentTracerName;
|
|
6
|
+
private tracingConfig;
|
|
7
|
+
protected app: IMidwayApplication;
|
|
8
|
+
protected decoratorService: MidwayDecoratorService;
|
|
9
|
+
protected tracingOptions: {
|
|
10
|
+
enable?: boolean;
|
|
11
|
+
onError?: 'throw' | 'ignore';
|
|
12
|
+
logOnError?: boolean;
|
|
13
|
+
};
|
|
14
|
+
protected logger: ILogger;
|
|
15
|
+
protected init(): void;
|
|
16
|
+
private getCurrentSpan;
|
|
17
|
+
getTraceId(): string;
|
|
18
|
+
createSpan(name: string, callback: (span: Span) => unknown): unknown;
|
|
19
|
+
private readonly defaultGetter;
|
|
20
|
+
private readonly defaultSetter;
|
|
21
|
+
private getProtocolFromAttributes;
|
|
22
|
+
private isProtocolEnabled;
|
|
23
|
+
private handleTraceError;
|
|
24
|
+
private pickTraceMetaValue;
|
|
25
|
+
resolveTraceMeta(resolver: TraceMetaResolver | undefined, args: TraceMetaResolverArgs): TraceMetaRecord;
|
|
26
|
+
runWithEntrySpan<T = unknown>(name: string, options: {
|
|
27
|
+
enable?: boolean;
|
|
28
|
+
carrier?: any;
|
|
29
|
+
responseCarrier?: any;
|
|
30
|
+
getter?: TextMapGetter<any>;
|
|
31
|
+
setter?: TextMapSetter<any>;
|
|
32
|
+
kind?: SpanKind;
|
|
33
|
+
attributes?: Record<string, any>;
|
|
34
|
+
meta?: TraceMetaResolver;
|
|
35
|
+
metaArgs?: Omit<TraceMetaResolverArgs, 'direction' | 'protocol' | 'spanName'>;
|
|
36
|
+
}, callback: (span: Span) => Promise<T> | T): Promise<T>;
|
|
37
|
+
runWithExitSpan<T = unknown>(name: string, options: {
|
|
38
|
+
enable?: boolean;
|
|
39
|
+
carrier?: any;
|
|
40
|
+
getter?: TextMapGetter<any>;
|
|
41
|
+
setter?: TextMapSetter<any>;
|
|
42
|
+
kind?: SpanKind;
|
|
43
|
+
attributes?: Record<string, any>;
|
|
44
|
+
meta?: TraceMetaResolver;
|
|
45
|
+
metaArgs?: Omit<TraceMetaResolverArgs, 'direction' | 'protocol' | 'spanName'>;
|
|
46
|
+
}, callback: (span: Span) => Promise<T> | T): Promise<T>;
|
|
47
|
+
injectContext(carrier: any, setter?: TextMapSetter<any>): any;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=traceService.d.ts.map
|