forked from BigfootDev/flatbuffers
[TS/JS] Updates the grpc (#6654)
* Updates go lang support to allow other languages to communicate with it * Update js grpc lib to use grpc-js Reformat code
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
"server": "node dist/server.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"flatbuffers": "^2.0.0",
|
||||
"grpc": "^1.24.3"
|
||||
"@grpc/grpc-js": "^1.3.2",
|
||||
"flatbuffers": "^2.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
import grpc from 'grpc';
|
||||
import * as grpc from '@grpc/grpc-js';
|
||||
import * as flatbuffers from 'flatbuffers';
|
||||
import { HelloReply } from './models/hello-reply';
|
||||
import { HelloRequest } from './models/hello-request';
|
||||
import { GreeterClient } from './greeter_grpc';
|
||||
import { flatbuffers } from 'flatbuffers';
|
||||
|
||||
async function main(PORT: Number, name: String) {
|
||||
const _server = new GreeterClient(`localhost:${PORT}`, grpc.credentials.createInsecure());
|
||||
async function main(PORT: Number, name: string) {
|
||||
const client = new GreeterClient(`localhost:${PORT}`, grpc.credentials.createInsecure());
|
||||
const builder = new flatbuffers.Builder();
|
||||
const offset = builder.createString(name);
|
||||
const root = HelloRequest.createHelloRequest(builder, offset);
|
||||
builder.finish(root);
|
||||
const buffer = HelloRequest.getRootAsHelloRequest(new flatbuffers.ByteBuffer(builder.asUint8Array()));
|
||||
|
||||
_server.SayHello(buffer, (err, response) => {
|
||||
client.SayHello(buffer, (err, response) => {
|
||||
console.log(response.message());
|
||||
});
|
||||
|
||||
const data = _server.SayManyHellos(buffer, null);
|
||||
const data = client.SayManyHellos(buffer, null);
|
||||
|
||||
data.on('data', (data) => {
|
||||
console.log(data.message());
|
||||
@@ -25,7 +25,7 @@ async function main(PORT: Number, name: String) {
|
||||
|
||||
const args = process.argv.slice(2)
|
||||
const PORT = Number(args[0]);
|
||||
const name = String(args[1] ?? "flatbuffers");
|
||||
const name: string = args[1] ?? "flatbuffers";
|
||||
|
||||
if (PORT) {
|
||||
main(PORT, name);
|
||||
|
||||
@@ -3,7 +3,7 @@ import * as flatbuffers from 'flatbuffers';
|
||||
import { HelloReply as models_HelloReply } from './models/hello-reply';
|
||||
import { HelloRequest as models_HelloRequest } from './models/hello-request';
|
||||
|
||||
import * as grpc from 'grpc';
|
||||
import * as grpc from '@grpc/grpc-js';
|
||||
|
||||
interface IGreeterService extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {
|
||||
SayHello: IGreeterService_ISayHello;
|
||||
@@ -32,7 +32,7 @@ interface IGreeterService_ISayManyHellos extends grpc.MethodDefinition<models_He
|
||||
|
||||
export const GreeterService: IGreeterService;
|
||||
|
||||
export interface IGreeterServer {
|
||||
export interface IGreeterServer extends grpc.UntypedServiceImplementation {
|
||||
SayHello: grpc.handleUnaryCall<models_HelloRequest, models_HelloReply>;
|
||||
SayManyHellos: grpc.handleServerStreamingCall<models_HelloRequest, models_HelloReply>;
|
||||
}
|
||||
@@ -46,7 +46,8 @@ export interface IGreeterClient {
|
||||
}
|
||||
|
||||
export class GreeterClient extends grpc.Client implements IGreeterClient {
|
||||
constructor(address: string, credentials: grpc.ChannelCredentials, options?: object); public SayHello(request: models_HelloRequest, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
|
||||
constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
|
||||
public SayHello(request: models_HelloRequest, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
|
||||
public SayHello(request: models_HelloRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
|
||||
public SayHello(request: models_HelloRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
|
||||
public SayManyHellos(request: models_HelloRequest, metadata: grpc.Metadata): grpc.ClientReadableStream<models_HelloReply>;
|
||||
|
||||
@@ -3,13 +3,13 @@ import * as flatbuffers from 'flatbuffers';
|
||||
import { HelloReply as models_HelloReply } from './models/hello-reply';
|
||||
import { HelloRequest as models_HelloRequest } from './models/hello-request';
|
||||
|
||||
var grpc = require('grpc');
|
||||
var grpc = require('@grpc/grpc-js');
|
||||
|
||||
function serialize_models_HelloReply(buffer_args) {
|
||||
if (!(buffer_args instanceof models_HelloReply)) {
|
||||
throw new Error('Expected argument of type HelloReply');
|
||||
}
|
||||
return buffer_args.serialize();
|
||||
return Buffer.from(buffer_args.serialize());
|
||||
}
|
||||
|
||||
function deserialize_models_HelloReply(buffer) {
|
||||
@@ -21,7 +21,7 @@ function serialize_models_HelloRequest(buffer_args) {
|
||||
if (!(buffer_args instanceof models_HelloRequest)) {
|
||||
throw new Error('Expected argument of type HelloRequest');
|
||||
}
|
||||
return buffer_args.serialize();
|
||||
return Buffer.from(buffer_args.serialize());
|
||||
}
|
||||
|
||||
function deserialize_models_HelloRequest(buffer) {
|
||||
|
||||
@@ -1,21 +1,19 @@
|
||||
import grpc from 'grpc';
|
||||
import * as grpc from '@grpc/grpc-js';
|
||||
import * as flatbuffers from 'flatbuffers';
|
||||
import { HelloReply } from './models/hello-reply';
|
||||
import { HelloRequest } from './models/hello-request';
|
||||
import { IGreeterServer, GreeterService } from './greeter_grpc';
|
||||
import { flatbuffers } from 'flatbuffers';
|
||||
|
||||
class GreeterServer implements IGreeterServer {
|
||||
|
||||
SayHello(call: grpc.ServerUnaryCall<HelloRequest>, callback: grpc.sendUnaryData<HelloReply>): void {
|
||||
const greeter: IGreeterServer = {
|
||||
SayHello(call: grpc.ServerUnaryCall<HelloRequest, HelloReply>, callback: grpc.sendUnaryData<HelloReply>): void {
|
||||
console.log(`SayHello ${call.request.name()}`);
|
||||
const builder = new flatbuffers.Builder();
|
||||
const offset = builder.createString(`welcome ${call.request.name()}`);
|
||||
const root = HelloReply.createHelloReply(builder, offset);
|
||||
builder.finish(root);
|
||||
callback(null, HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(builder.asUint8Array())));
|
||||
}
|
||||
|
||||
async SayManyHellos(call: grpc.ServerWritableStream<HelloRequest>): Promise<void> {
|
||||
},
|
||||
async SayManyHellos(call: grpc.ServerWritableStream<HelloRequest, HelloReply>): Promise<void> {
|
||||
const name = call.request.name();
|
||||
console.log(`${call.request.name()} saying hi in different langagues`);
|
||||
['Hi', 'Hallo', 'Ciao'].forEach(element => {
|
||||
@@ -32,10 +30,20 @@ class GreeterServer implements IGreeterServer {
|
||||
function serve(): void {
|
||||
const PORT = 3000;
|
||||
const server = new grpc.Server();
|
||||
server.addService<IGreeterServer>(GreeterService, new GreeterServer());
|
||||
server.addService(GreeterService, greeter);
|
||||
console.log(`Listening on ${PORT}`);
|
||||
server.bind(`localhost:${PORT}`, grpc.ServerCredentials.createInsecure());
|
||||
server.start();
|
||||
server.bindAsync(
|
||||
`localhost:${PORT}`,
|
||||
grpc.ServerCredentials.createInsecure(),
|
||||
(err: Error | null, port: number) => {
|
||||
if (err) {
|
||||
console.error(`Server error: ${err.message}`);
|
||||
} else {
|
||||
console.log(`Server bound on port: ${port}`);
|
||||
server.start();
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
serve();
|
||||
@@ -46,6 +46,7 @@ grpc::string ToDasherizedCase(const grpc::string pascal_case) {
|
||||
return dasherized_case;
|
||||
}
|
||||
|
||||
|
||||
grpc::string GenerateNamespace(const std::vector<std::string> namepsace,
|
||||
const std::string filename,
|
||||
const bool include_separator) {
|
||||
@@ -105,9 +106,9 @@ void GenerateImports(const grpc_generator::Service *service,
|
||||
}
|
||||
printer->Print("\n");
|
||||
if (grpc_var_import)
|
||||
printer->Print("var grpc = require('grpc');\n");
|
||||
printer->Print("var grpc = require('@grpc/grpc-js');\n");
|
||||
else
|
||||
printer->Print("import * as grpc from 'grpc';\n");
|
||||
printer->Print("import * as grpc from '@grpc/grpc-js';\n");
|
||||
printer->Print("\n");
|
||||
}
|
||||
|
||||
@@ -136,7 +137,7 @@ void GenerateSerializeMethod(grpc_generator::Printer *printer,
|
||||
"throw new Error('Expected argument of type $VALUE$');\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n");
|
||||
printer->Print(vars, "return buffer_args.serialize();\n");
|
||||
printer->Print(vars, "return Buffer.from(buffer_args.serialize());\n");
|
||||
printer->Outdent();
|
||||
printer->Print("}\n\n");
|
||||
}
|
||||
@@ -288,7 +289,9 @@ void GenerateExportedInterface(
|
||||
const grpc_generator::Service *service, grpc_generator::Printer *printer,
|
||||
std::map<grpc::string, grpc::string> *dictonary) {
|
||||
auto vars = *dictonary;
|
||||
printer->Print(vars, "export interface I$ServiceName$Server {\n");
|
||||
printer->Print(vars,
|
||||
"export interface I$ServiceName$Server extends "
|
||||
"grpc.UntypedServiceImplementation {\n");
|
||||
printer->Indent();
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
@@ -463,7 +466,7 @@ void GenerateClientClassInterface(
|
||||
printer->Indent();
|
||||
printer->Print(
|
||||
"constructor(address: string, credentials: grpc.ChannelCredentials, "
|
||||
"options?: object);");
|
||||
"options?: object);\n");
|
||||
for (auto it = 0; it < service->method_count(); it++) {
|
||||
auto method = service->method(it);
|
||||
vars["MethodName"] = method->name();
|
||||
|
||||
7
tests/monster_test_grpc.d.ts
vendored
7
tests/monster_test_grpc.d.ts
vendored
@@ -3,7 +3,7 @@ import * as flatbuffers from 'flatbuffers';
|
||||
import { Stat as MyGame_Example_Stat } from './my-game/example/stat';
|
||||
import { Monster as MyGame_Example_Monster } from './my-game/example/monster';
|
||||
|
||||
import * as grpc from 'grpc';
|
||||
import * as grpc from '@grpc/grpc-js';
|
||||
|
||||
interface IMonsterStorageService extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {
|
||||
Store: IMonsterStorageService_IStore;
|
||||
@@ -54,7 +54,7 @@ interface IMonsterStorageService_IGetMinMaxHitPoints extends grpc.MethodDefiniti
|
||||
|
||||
export const MonsterStorageService: IMonsterStorageService;
|
||||
|
||||
export interface IMonsterStorageServer {
|
||||
export interface IMonsterStorageServer extends grpc.UntypedServiceImplementation {
|
||||
Store: grpc.handleUnaryCall<MyGame_Example_Monster, MyGame_Example_Stat>;
|
||||
Retrieve: grpc.handleServerStreamingCall<MyGame_Example_Stat, MyGame_Example_Monster>;
|
||||
GetMaxHitPoint: grpc.handleClientStreamingCall<MyGame_Example_Monster, MyGame_Example_Stat>;
|
||||
@@ -77,7 +77,8 @@ export interface IMonsterStorageClient {
|
||||
}
|
||||
|
||||
export class MonsterStorageClient extends grpc.Client implements IMonsterStorageClient {
|
||||
constructor(address: string, credentials: grpc.ChannelCredentials, options?: object); public Store(request: MyGame_Example_Monster, callback: (error: grpc.ServiceError | null, response: MyGame_Example_Stat) => void): grpc.ClientUnaryCall;
|
||||
constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
|
||||
public Store(request: MyGame_Example_Monster, callback: (error: grpc.ServiceError | null, response: MyGame_Example_Stat) => void): grpc.ClientUnaryCall;
|
||||
public Store(request: MyGame_Example_Monster, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: MyGame_Example_Stat) => void): grpc.ClientUnaryCall;
|
||||
public Store(request: MyGame_Example_Monster, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: MyGame_Example_Stat) => void): grpc.ClientUnaryCall;
|
||||
public Retrieve(request: MyGame_Example_Stat, metadata: grpc.Metadata): grpc.ClientReadableStream<MyGame_Example_Monster>;
|
||||
|
||||
@@ -3,13 +3,13 @@ import * as flatbuffers from 'flatbuffers';
|
||||
import { Stat as MyGame_Example_Stat } from './my-game/example/stat';
|
||||
import { Monster as MyGame_Example_Monster } from './my-game/example/monster';
|
||||
|
||||
var grpc = require('grpc');
|
||||
var grpc = require('@grpc/grpc-js');
|
||||
|
||||
function serialize_MyGame_Example_Stat(buffer_args) {
|
||||
if (!(buffer_args instanceof MyGame_Example_Stat)) {
|
||||
throw new Error('Expected argument of type Stat');
|
||||
}
|
||||
return buffer_args.serialize();
|
||||
return Buffer.from(buffer_args.serialize());
|
||||
}
|
||||
|
||||
function deserialize_MyGame_Example_Stat(buffer) {
|
||||
@@ -21,7 +21,7 @@ function serialize_MyGame_Example_Monster(buffer_args) {
|
||||
if (!(buffer_args instanceof MyGame_Example_Monster)) {
|
||||
throw new Error('Expected argument of type Monster');
|
||||
}
|
||||
return buffer_args.serialize();
|
||||
return Buffer.from(buffer_args.serialize());
|
||||
}
|
||||
|
||||
function deserialize_MyGame_Example_Monster(buffer) {
|
||||
|
||||
Reference in New Issue
Block a user