@flexent/protocomm-http 2.9.5 → 2.10.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.
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { HttpContext, HttpHandler, HttpNext } from '@flexent/http-server';
|
|
2
2
|
import { Logger } from '@flexent/logger';
|
|
3
|
-
import {
|
|
3
|
+
import { DomainMethodHook, ProtocolIndex, ProtocolMethodDesc } from '@flexent/protocomm';
|
|
4
4
|
import { Event } from 'nanoevent';
|
|
5
5
|
export declare abstract class HttpProtocolHandler<P> extends HttpHandler {
|
|
6
6
|
protected logger: Logger;
|
|
7
|
-
methodStats: Event<DomainMethodStat>;
|
|
8
7
|
prefix: string;
|
|
8
|
+
onMethod: Event<DomainMethodHook>;
|
|
9
9
|
abstract get protocol(): ProtocolIndex<P>;
|
|
10
10
|
abstract get protocolImpl(): P;
|
|
11
11
|
handle(ctx: HttpContext, next: HttpNext): Promise<void>;
|
|
12
|
-
protected handleProtocolMethod(ctx: HttpContext,
|
|
12
|
+
protected handleProtocolMethod(ctx: HttpContext, domainName: string, methodName: string, methodDesc: ProtocolMethodDesc): Promise<void>;
|
|
13
13
|
protected parsePath(path: string): [string, string];
|
|
14
14
|
protected parseParams(ctx: HttpContext): Promise<unknown>;
|
|
15
15
|
}
|
|
@@ -16,56 +16,60 @@ import { Event } from 'nanoevent';
|
|
|
16
16
|
export class HttpProtocolHandler extends HttpHandler {
|
|
17
17
|
constructor() {
|
|
18
18
|
super(...arguments);
|
|
19
|
-
this.methodStats = new Event();
|
|
20
19
|
this.prefix = '';
|
|
20
|
+
this.onMethod = new Event();
|
|
21
21
|
}
|
|
22
22
|
async handle(ctx, next) {
|
|
23
23
|
if (!ctx.path.startsWith(this.prefix)) {
|
|
24
24
|
return await next();
|
|
25
25
|
}
|
|
26
26
|
const [domainName, methodName] = this.parsePath(ctx.path);
|
|
27
|
+
const methodDesc = this.protocol.lookupMethod(domainName, methodName);
|
|
28
|
+
const expectedMethod = methodDesc.methodDef.type === 'query' ? 'GET' : 'POST';
|
|
29
|
+
if (ctx.method !== expectedMethod) {
|
|
30
|
+
return await next();
|
|
31
|
+
}
|
|
32
|
+
await this.handleProtocolMethod(ctx, domainName, methodName, methodDesc);
|
|
33
|
+
}
|
|
34
|
+
async handleProtocolMethod(ctx, domainName, methodName, methodDesc) {
|
|
27
35
|
const startedAt = Date.now();
|
|
36
|
+
let decodedParams;
|
|
28
37
|
try {
|
|
29
|
-
|
|
30
|
-
this.
|
|
38
|
+
const { methodDef, reqSchema, resSchema } = methodDesc;
|
|
39
|
+
const params = await this.parseParams(ctx);
|
|
40
|
+
decodedParams = reqSchema.decode(params, { strictRequired: true });
|
|
41
|
+
this.logger.debug(`>>> ${domainName}.${methodName}`, { params: decodedParams });
|
|
42
|
+
const domainImpl = this.protocolImpl[domainName];
|
|
43
|
+
const methodImpl = domainImpl?.[methodName];
|
|
44
|
+
if (!methodImpl) {
|
|
45
|
+
throw new MethodNotFound(`${domainName}.${methodName}`);
|
|
46
|
+
}
|
|
47
|
+
ctx.state.domainName = domainName;
|
|
48
|
+
ctx.state.methodName = methodName;
|
|
49
|
+
ctx.state.methodDef = methodDef;
|
|
50
|
+
ctx.state.params = decodedParams;
|
|
51
|
+
const res = await methodImpl.call(domainImpl, decodedParams);
|
|
52
|
+
const decodedRes = resSchema.decode(res);
|
|
53
|
+
this.logger.debug(`<<< ${domainName}.${methodName}`, { result: decodedRes });
|
|
54
|
+
ctx.status = 200;
|
|
55
|
+
ctx.responseBody = decodedRes;
|
|
56
|
+
this.onMethod.emit({
|
|
31
57
|
domain: domainName,
|
|
32
58
|
method: methodName,
|
|
59
|
+
params: decodedParams,
|
|
60
|
+
result: decodedRes,
|
|
33
61
|
latency: Date.now() - startedAt,
|
|
34
62
|
});
|
|
35
63
|
}
|
|
36
64
|
catch (error) {
|
|
37
|
-
this.
|
|
65
|
+
this.onMethod.emit({
|
|
38
66
|
domain: domainName,
|
|
39
67
|
method: methodName,
|
|
68
|
+
params: decodedParams,
|
|
69
|
+
error,
|
|
40
70
|
latency: Date.now() - startedAt,
|
|
41
|
-
error: error.name,
|
|
42
71
|
});
|
|
43
|
-
throw error;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
async handleProtocolMethod(ctx, next, domainName, methodName) {
|
|
47
|
-
const { methodDef, reqSchema, resSchema } = this.protocol.lookupMethod(domainName, methodName);
|
|
48
|
-
const expectedMethod = methodDef.type === 'query' ? 'GET' : 'POST';
|
|
49
|
-
if (ctx.method !== expectedMethod) {
|
|
50
|
-
return await next();
|
|
51
|
-
}
|
|
52
|
-
const params = await this.parseParams(ctx);
|
|
53
|
-
const decodedParams = reqSchema.decode(params, { strictRequired: true });
|
|
54
|
-
this.logger.debug(`>>> ${domainName}.${methodName}`, { params: decodedParams });
|
|
55
|
-
const domainImpl = this.protocolImpl[domainName];
|
|
56
|
-
const methodImpl = domainImpl?.[methodName];
|
|
57
|
-
if (!methodImpl) {
|
|
58
|
-
throw new MethodNotFound(`${domainName}.${methodName}`);
|
|
59
72
|
}
|
|
60
|
-
ctx.state.domainName = domainName;
|
|
61
|
-
ctx.state.methodName = methodName;
|
|
62
|
-
ctx.state.methodDef = methodDef;
|
|
63
|
-
ctx.state.params = decodedParams;
|
|
64
|
-
const res = await methodImpl.call(domainImpl, decodedParams);
|
|
65
|
-
const decodedRes = resSchema.decode(res);
|
|
66
|
-
this.logger.debug(`<<< ${domainName}.${methodName}`, { result: decodedRes });
|
|
67
|
-
ctx.status = 200;
|
|
68
|
-
ctx.responseBody = decodedRes;
|
|
69
73
|
}
|
|
70
74
|
parsePath(path) {
|
|
71
75
|
if (!path.startsWith(this.prefix)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HttpProtocolHandler.js","sourceRoot":"","sources":["../../src/main/HttpProtocolHandler.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;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C,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAChC,CAAC;IAES,KAAK,CAAC,WAAW,CAAC,GAAgB;QACxC,IAAI,GAAG,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;YACvB,OAAO,GAAG,CAAC,KAAK,CAAC;QACrB,CAAC;QACD,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,eAAe,EAAE,CAAC;QACzC,OAAO,IAAI,IAAI,EAAE,CAAC;IACtB,CAAC;CAEJ;AAxFoB;IAAhB,GAAG,EAAE;8BAAoB,MAAM;mDAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flexent/protocomm-http",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.10.0",
|
|
4
4
|
"description": "HTTP handler for protocomm RPC protocols",
|
|
5
5
|
"author": "Boris Okunskiy",
|
|
6
6
|
"license": "MIT",
|
|
@@ -16,10 +16,10 @@
|
|
|
16
16
|
"test": "NODE_ENV=test mocha"
|
|
17
17
|
},
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"@flexent/errors": "^2.
|
|
20
|
-
"@flexent/http-server": "^2.
|
|
21
|
-
"@flexent/logger": "^2.
|
|
22
|
-
"@flexent/protocomm": "^2.
|
|
19
|
+
"@flexent/errors": "^2.10.0",
|
|
20
|
+
"@flexent/http-server": "^2.10.0",
|
|
21
|
+
"@flexent/logger": "^2.10.0",
|
|
22
|
+
"@flexent/protocomm": "^2.10.0",
|
|
23
23
|
"mesh-ioc": "^4.3.1",
|
|
24
24
|
"nanoevent": "^1.0.0"
|
|
25
25
|
}
|