-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (48 loc) · 1.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const grpc = require("@grpc/grpc-js");
const protobufjs = require("protobufjs");
const protoLoader = require("@grpc/proto-loader");
const fs = require("fs");
const MOCK_SERVER_ADDRESS = "127.0.0.1:50051";
const MOCKED_JSON_FILE = "./mock-response.json";
const protoDirectory = "./proto";
const mockedData = JSON.parse(fs.readFileSync(MOCKED_JSON_FILE));
const protoFiles = fs.readdirSync(protoDirectory);
const options = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
};
const server = new grpc.Server();
protoFiles.forEach((eachService) => {
const fullProtoAddress = `${protoDirectory}/${eachService}`;
const loadedProtoFile = protoLoader.loadSync(fullProtoAddress);
const service = grpc.loadPackageDefinition(loadedProtoFile, options);
const jsonProto = protobufjs.loadSync(fullProtoAddress).toJSON().nested;
const serviceName = Object.keys(jsonProto)[0];
const serviceMethods = jsonProto[serviceName].methods;
if (!serviceMethods) return;
server.addService(
service[serviceName].service,
(() => {
const services = [];
for (const method in serviceMethods) {
const responseTypeName = serviceMethods[method].responseType;
services[method] = (_, res) => {
res(null, mockedData[responseTypeName] || {});
};
}
return services;
})()
);
console.log("Loaded ", eachService);
});
server.bindAsync(
MOCK_SERVER_ADDRESS,
grpc.ServerCredentials.createInsecure(),
(error, port) => {
console.log(`Server running at ${MOCK_SERVER_ADDRESS}`);
server.start();
}
);