Skip to content

Commit fbd1ff8

Browse files
committed
feat(cmd-api-server): support grpc web services hyperledger-cacti#1189
WORK IN PROGRESS! Fixes hyperledger-cacti#1189 Signed-off-by: Peter Somogyvari <[email protected]>
1 parent b0688e8 commit fbd1ff8

File tree

9 files changed

+1376
-4
lines changed

9 files changed

+1376
-4
lines changed

.cspell.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@
7272
"OpenAPI",
7373
"openethereum",
7474
"organisation",
75+
"parameterizable",
76+
"pbjs",
77+
"pbts",
78+
"proto",
79+
"protoc",
7580
"protos",
7681
"RUSTC",
7782
"Secp",
@@ -91,8 +96,7 @@
9196
"uuidv",
9297
"vscc",
9398
"wasm",
94-
"Xdai",
95-
"parameterizable"
99+
"Xdai"
96100
],
97101
"dictionaries": [
98102
"typescript,node,npm,go,rust"

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"fs-extra": "10.0.0",
101101
"git-cz": "4.7.6",
102102
"globby": "10.0.2",
103+
"grpc-tools": "1.11.2",
103104
"husky": "4.2.5",
104105
"inquirer": "8.1.1",
105106
"json5": "2.2.0",
@@ -114,6 +115,7 @@
114115
"npm-run-all": "4.1.5",
115116
"npm-watch": "0.7.0",
116117
"prettier": "2.0.5",
118+
"protobufjs": "6.11.2",
117119
"run-time-error": "1.4.0",
118120
"secp256k1": "4.0.0",
119121
"shebang-loader": "0.0.1",
@@ -124,6 +126,7 @@
124126
"tape-promise": "4.0.0",
125127
"ts-loader": "6.2.1",
126128
"ts-node": "10.1.0",
129+
"ts-protoc-gen": "0.15.0",
127130
"typescript": "4.2.4",
128131
"webpack": "5.36.2",
129132
"webpack-bundle-analyzer": "4.4.2",

packages/cactus-cmd-api-server/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
"scripts": {
1515
"generate-sdk": "openapi-generator-cli generate -i ./src/main/json/openapi.json -g typescript-axios -o ./src/main/typescript/generated/openapi/typescript-axios/ --reserved-words-mappings protected=protected",
1616
"codegen:openapi": "npm run generate-sdk",
17+
"proto:pbjs": "yarn pbjs --es6 --target static --wrap=es6 --out ./src/main/typescript/generated/proto/pbjs.js ./src/main/proto/healthcheck.proto",
18+
"proto:pbts": "yarn pbts --out ./src/main/typescript/generated/proto/pbts.d.ts ./src/main/typescript/generated/proto/pbjs.js",
19+
"proto:protoc": "yarn grpc_tools_node_protoc ./src/main/proto/healthcheck.proto",
20+
"codegen:proto": "run-s 'protoc:*'",
1721
"codegen": "run-p 'codegen:*'",
1822
"watch": "npm-watch",
1923
"webpack": "npm-run-all webpack:dev webpack:prod",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
syntax = "proto3";
2+
3+
package org.hyperledger.cactus.cmd_api_server;
4+
5+
service Healthcheck {
6+
// Obtains the memory usage statistics of the API server's NodeJS process.
7+
rpc GetHealtcheck(HealthcheckRequest) returns (stream HealthcheckResponse) {}
8+
}
9+
10+
message MemoryUsage {
11+
int64 rss = 1;
12+
int64 heapTotal = 2;
13+
int64 heapUsed = 3;
14+
int64 external = 4;
15+
int64 arrayBuffers = 5;
16+
}
17+
18+
message HealthcheckRequest {
19+
}
20+
21+
message HealthcheckResponse {
22+
bool success = 1;
23+
string createdAt = 2;
24+
MemoryUsage memoryUsage = 3;
25+
}

packages/cactus-cmd-api-server/src/main/typescript/generated/proto/pbjs.js

+864
Large diffs are not rendered by default.

packages/cactus-cmd-api-server/src/main/typescript/generated/proto/pbts.d.ts

+361
Large diffs are not rendered by default.

packages/cactus-cmd-api-server/src/main/typescript/public-api.ts

+2
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ export {
3232
} from "./authzn/authorizer-factory";
3333
export { IAuthorizationConfig } from "./authzn/i-authorization-config";
3434
export { AuthorizationProtocol } from "./config/authorization-protocol";
35+
36+
export * from "./generated/proto/pbts";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { Socket as SocketIoSocket } from "socket.io";
2+
3+
import { Logger, Checks } from "@hyperledger/cactus-common";
4+
import { LogLevelDesc, LoggerProvider } from "@hyperledger/cactus-common";
5+
6+
import { org } from "../../generated/proto/pbts";
7+
import { WatchHealthcheckV1 } from "../../generated/openapi/typescript-axios/api";
8+
9+
const { HealthcheckResponse } = org.hyperledger.cactus.cmd_api_server;
10+
11+
export interface IWatchHealthcheckV1EndpointOptions {
12+
logLevel?: LogLevelDesc;
13+
socket: SocketIoSocket;
14+
process: NodeJS.Process;
15+
}
16+
17+
export class WatchHealthcheckV1Endpoint {
18+
public static readonly CLASS_NAME = "WatchHealthcheckV1Endpoint";
19+
20+
private readonly log: Logger;
21+
private readonly process: NodeJS.Process;
22+
23+
public get className(): string {
24+
return WatchHealthcheckV1Endpoint.CLASS_NAME;
25+
}
26+
27+
constructor(public readonly options: IWatchHealthcheckV1EndpointOptions) {
28+
const fnTag = `${this.className}#constructor()`;
29+
Checks.truthy(options, `${fnTag} arg options`);
30+
Checks.truthy(options.process, `${fnTag} arg options.process`);
31+
32+
this.process = options.process;
33+
34+
const level = this.options.logLevel || "INFO";
35+
const label = this.className;
36+
this.log = LoggerProvider.getOrCreate({ level, label });
37+
}
38+
39+
public async subscribe(): Promise<void> {
40+
const { log } = this;
41+
log.debug(`${WatchHealthcheckV1.Subscribe} => FIXME - connection ID here`);
42+
43+
const timerId = setInterval(() => {
44+
try {
45+
const next: org.hyperledger.cactus.cmd_api_server.IHealthcheckResponse = HealthcheckResponse.create(
46+
{
47+
createdAt: new Date().toJSON(),
48+
memoryUsage: this.process.memoryUsage(),
49+
success: true,
50+
},
51+
);
52+
53+
const validationMessage = HealthcheckResponse.verify(next);
54+
if (validationMessage) {
55+
// socket.emit(WatchHealthcheckV1.Error, validationMessage);
56+
} else {
57+
// socket.emit(WatchHealthcheckV1.Next, next);
58+
}
59+
} catch (ex) {
60+
log.error(`Failed to construct health check response:`, ex);
61+
// socket.emit(WatchHealthcheckV1.Error, ex);
62+
clearInterval(timerId);
63+
}
64+
}, 1000);
65+
66+
// socket.on("disconnect", async (reason: string) => {
67+
// log.debug("WebSocket:disconnect reason=%o", reason);
68+
// clearInterval(timerId);
69+
// });
70+
71+
// socket.on(WatchHealthcheckV1.Unsubscribe, () => {
72+
// clearInterval(timerId);
73+
// });
74+
}
75+
}

yarn.lock

+36-2
Original file line numberDiff line numberDiff line change
@@ -2703,6 +2703,21 @@
27032703
npmlog "^4.1.2"
27042704
write-file-atomic "^3.0.3"
27052705

2706+
"@mapbox/node-pre-gyp@^1.0.5":
2707+
version "1.0.5"
2708+
resolved "https://registry.yarnpkg.com/@mapbox/node-pre-gyp/-/node-pre-gyp-1.0.5.tgz#2a0b32fcb416fb3f2250fd24cb2a81421a4f5950"
2709+
integrity sha512-4srsKPXWlIxp5Vbqz5uLfBN+du2fJChBoYn/f2h991WLdk7jUvcSk/McVLSv/X+xQIPI8eGD5GjrnygdyHnhPA==
2710+
dependencies:
2711+
detect-libc "^1.0.3"
2712+
https-proxy-agent "^5.0.0"
2713+
make-dir "^3.1.0"
2714+
node-fetch "^2.6.1"
2715+
nopt "^5.0.0"
2716+
npmlog "^4.1.2"
2717+
rimraf "^3.0.2"
2718+
semver "^7.3.4"
2719+
tar "^6.1.0"
2720+
27062721
"@multiformats/base-x@^4.0.1":
27072722
version "4.0.1"
27082723
resolved "https://registry.npmjs.org/@multiformats/base-x/-/base-x-4.0.1.tgz"
@@ -10151,6 +10166,11 @@ google-p12-pem@^3.0.3:
1015110166
dependencies:
1015210167
node-forge "^0.10.0"
1015310168

10169+
google-protobuf@^3.15.5:
10170+
version "3.17.3"
10171+
resolved "https://registry.yarnpkg.com/google-protobuf/-/google-protobuf-3.17.3.tgz#f87595073545a77946c8f0b67c302c5f7646d700"
10172+
integrity sha512-OVPzcSWIAJ+d5yiHyeaLrdufQtrvaBrF4JQg+z8ynTkbO3uFcujqXszTumqg1cGsAsjkWnI+M5B1xZ19yR4Wyg==
10173+
1015410174
[email protected], got@^9.6.0:
1015510175
version "9.6.0"
1015610176
resolved "https://registry.npmjs.org/got/-/got-9.6.0.tgz"
@@ -10237,6 +10257,13 @@ [email protected]:
1023710257
resolved "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz"
1023810258
integrity sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==
1023910259

10260+
10261+
version "1.11.2"
10262+
resolved "https://registry.yarnpkg.com/grpc-tools/-/grpc-tools-1.11.2.tgz#22d802d40012510ccc6591d11f9c94109ac07aab"
10263+
integrity sha512-4+EgpnnkJraamY++oyBCw5Hp9huRYfgakjNVKbiE3PgO9Tv5ydVlRo7ZyGJ0C0SEiA7HhbVc1sNNtIyK7FiEtg==
10264+
dependencies:
10265+
"@mapbox/node-pre-gyp" "^1.0.5"
10266+
1024010267
gtoken@^5.0.4:
1024110268
version "5.3.0"
1024210269
resolved "https://registry.yarnpkg.com/gtoken/-/gtoken-5.3.0.tgz#6536eb2880d9829f0b9d78f756795d4d9064b217"
@@ -16584,9 +16611,9 @@ proto-list@~1.2.1:
1658416611
resolved "https://registry.npmjs.org/proto-list/-/proto-list-1.2.4.tgz"
1658516612
integrity sha1-IS1b/hMYMGpCD2QCuOJv85ZHqEk=
1658616613

16587-
protobufjs@^6.10.0, protobufjs@^6.10.2, protobufjs@^6.11.2:
16614+
protobufjs@6.11.2, protobufjs@^6.10.0, protobufjs@^6.10.2, protobufjs@^6.11.2:
1658816615
version "6.11.2"
16589-
resolved "https://registry.npmjs.org/protobufjs/-/protobufjs-6.11.2.tgz"
16616+
resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-6.11.2.tgz#de39fabd4ed32beaa08e9bb1e30d08544c1edf8b"
1659016617
integrity sha512-4BQJoPooKJl2G9j3XftkIXjoC9C0Av2NOrWmbLWT1vH32GcSUHjM0Arra6UfTsVyfMAuFzaLucXn1sadxJydAw==
1659116618
dependencies:
1659216619
"@protobufjs/aspromise" "^1.1.2"
@@ -19624,6 +19651,13 @@ [email protected]:
1962419651
source-map-support "^0.5.6"
1962519652
yn "^3.0.0"
1962619653

19654+
19655+
version "0.15.0"
19656+
resolved "https://registry.yarnpkg.com/ts-protoc-gen/-/ts-protoc-gen-0.15.0.tgz#2fec5930b46def7dcc9fa73c060d770b7b076b7b"
19657+
integrity sha512-TycnzEyrdVDlATJ3bWFTtra3SCiEP0W0vySXReAuEygXCUr1j2uaVyL0DhzjwuUdQoW5oXPwk6oZWeA0955V+g==
19658+
dependencies:
19659+
google-protobuf "^3.15.5"
19660+
1962719661
tsconfig-paths@^3.9.0:
1962819662
version "3.10.1"
1962919663
resolved "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.10.1.tgz"

0 commit comments

Comments
 (0)