-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathgrpc-service.js
More file actions
84 lines (66 loc) · 2.89 KB
/
Copy pathgrpc-service.js
File metadata and controls
84 lines (66 loc) · 2.89 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// this example demonstrates how to use reflected services with gRPC through the
// `@grpc/grpc-js` npm package, covering both the server and the client side.
/*eslint-disable strict, no-console*/
const grpc = require("@grpc/grpc-js"); // npm install @grpc/grpc-js
const protobuf = require(".."); // require("protobufjs")
const root = protobuf.parse(`
syntax = "proto3";
package example;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
`).root.resolveAll();
const service = root.lookupService("example.Greeter");
// A gRPC service definition can be built entirely from the reflected service,
// reusing each method's gRPC-style path and its request and response types:
function serviceDefinition(service) {
const definition = {};
for (const method of service.methodsArray) {
definition[method.name] = {
path: method.path,
requestStream: Boolean(method.requestStream),
responseStream: Boolean(method.responseStream),
requestSerialize: (message) => method.resolvedRequestType.encode(message).finish(),
requestDeserialize: (data) => method.resolvedRequestType.decode(data),
responseSerialize: (message) => method.resolvedResponseType.encode(message).finish(),
responseDeserialize: (data) => method.resolvedResponseType.decode(data)
};
}
return definition;
}
(async function main() {
// Server: register the service definition with a plain implementation object
const server = new grpc.Server();
server.addService(serviceDefinition(service), {
SayHello(call, callback) {
callback(null, { message: "Hello, " + call.request.name + "!" });
}
});
const port = await new Promise((resolve, reject) => {
server.bindAsync("127.0.0.1:0", grpc.ServerCredentials.createInsecure(),
(err, port) => err ? reject(err) : resolve(port));
});
// Client: connect a reflected service client to a generic gRPC client. Since
// protobuf.js already handles message encoding and decoding, requests and
// responses pass through as raw data:
const Client = grpc.makeGenericClientConstructor({});
const client = new Client("127.0.0.1:" + port, grpc.credentials.createInsecure());
const greeter = service.create(function rpcImpl(method, requestData, callback) {
if (!method) { // signals that the service has been ended
client.close();
return;
}
client.makeUnaryRequest(method.path, (data) => data, (data) => data, requestData, callback);
});
// Service methods support both callbacks and promises:
const reply = await greeter.sayHello({ name: "world" });
console.log(reply.message);
greeter.end();
server.forceShutdown();
})();