diff options
author | mustiikhalil <mustii@mmk.one> | 2021-02-20 20:31:00 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-20 20:31:00 +0300 |
commit | 3b5365762d1913abb0078b88b35213ff971dfdc8 (patch) | |
tree | 3560a647cd5546e95fead275855fa4fd6c1cf09c /grpc/examples | |
parent | e2f5438ac1847c322dc558628ea4f83c8498a071 (diff) | |
download | flatbuffers-3b5365762d1913abb0078b88b35213ff971dfdc8.tar.gz flatbuffers-3b5365762d1913abb0078b88b35213ff971dfdc8.tar.bz2 flatbuffers-3b5365762d1913abb0078b88b35213ff971dfdc8.zip |
[TS] Moves grpc code to examples folder (#6476)
Adds readme
Diffstat (limited to 'grpc/examples')
-rw-r--r-- | grpc/examples/generate.sh | 8 | ||||
-rw-r--r-- | grpc/examples/ts/greeter/README.md | 13 | ||||
-rw-r--r-- | grpc/examples/ts/greeter/package.json | 14 | ||||
-rw-r--r-- | grpc/examples/ts/greeter/src/client.ts | 34 | ||||
-rw-r--r-- | grpc/examples/ts/greeter/src/greeter_grpc.d.ts | 55 | ||||
-rw-r--r-- | grpc/examples/ts/greeter/src/greeter_grpc.js | 56 | ||||
-rw-r--r-- | grpc/examples/ts/greeter/src/models/hello-reply.ts | 58 | ||||
-rw-r--r-- | grpc/examples/ts/greeter/src/models/hello-request.ts | 58 | ||||
-rw-r--r-- | grpc/examples/ts/greeter/src/server.ts | 41 | ||||
-rw-r--r-- | grpc/examples/ts/greeter/tsconfig.json | 17 |
10 files changed, 354 insertions, 0 deletions
diff --git a/grpc/examples/generate.sh b/grpc/examples/generate.sh index 4f481919..e70f28d6 100644 --- a/grpc/examples/generate.sh +++ b/grpc/examples/generate.sh @@ -15,4 +15,12 @@ cd go/ cd greeter fbc --go ${generator} +cd ../.. + +cd ts/ + +cd greeter/src +fbc --ts ${generator} +cd .. + cd ../..
\ No newline at end of file diff --git a/grpc/examples/ts/greeter/README.md b/grpc/examples/ts/greeter/README.md new file mode 100644 index 00000000..5c7b380f --- /dev/null +++ b/grpc/examples/ts/greeter/README.md @@ -0,0 +1,13 @@ +# TS Greeter example + +The following is an example on how to run the TS grpc server. Make sure that you have `Typescript` installed + +you would need to run `npm run build` or simply use `npm install && tsc` + +## How to run Server: + +- `npm run server` + +## How to run Client: + +- `npm run client 3000`
\ No newline at end of file diff --git a/grpc/examples/ts/greeter/package.json b/grpc/examples/ts/greeter/package.json new file mode 100644 index 00000000..aab27b2a --- /dev/null +++ b/grpc/examples/ts/greeter/package.json @@ -0,0 +1,14 @@ +{ + "name": "flatbuffers-js-grpc", + "version": "1.0.0", + "author": "mustii@mmk.one", + "scripts": { + "build": "npm install && tsc", + "client": "node dist/client.js", + "server": "node dist/server.js" + }, + "dependencies": { + "flatbuffers": "^1.12.0", + "grpc": "^1.24.3" + } +} diff --git a/grpc/examples/ts/greeter/src/client.ts b/grpc/examples/ts/greeter/src/client.ts new file mode 100644 index 00000000..ebae902b --- /dev/null +++ b/grpc/examples/ts/greeter/src/client.ts @@ -0,0 +1,34 @@ +import grpc from 'grpc'; +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()); + 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) => { + console.log(response.message()); + }); + + const data = _server.SayManyHellos(buffer, null); + + data.on('data', (data) => { + console.log(data.message()); + }); +} + +const args = process.argv.slice(2) +const PORT = Number(args[0]); +const name = String(args[1] ?? "flatbuffers"); + +if (PORT) { + main(PORT, name); +} else { + throw new Error("Requires a valid port number.") +}
\ No newline at end of file diff --git a/grpc/examples/ts/greeter/src/greeter_grpc.d.ts b/grpc/examples/ts/greeter/src/greeter_grpc.d.ts new file mode 100644 index 00000000..402d1ab1 --- /dev/null +++ b/grpc/examples/ts/greeter/src/greeter_grpc.d.ts @@ -0,0 +1,55 @@ +// Generated GRPC code for FlatBuffers TS *** DO NOT EDIT *** +import { 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'; + +interface IGreeterService extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> { + SayHello: IGreeterService_ISayHello; + SayManyHellos: IGreeterService_ISayManyHellos; +} +interface IGreeterService_ISayHello extends grpc.MethodDefinition<models_HelloRequest, models_HelloReply> { + path: string; // /models.Greeter/SayHello + requestStream: boolean; // false + responseStream: boolean; // false + requestSerialize: grpc.serialize<models_HelloRequest>; + requestDeserialize: grpc.deserialize<models_HelloRequest>; + responseSerialize: grpc.serialize<models_HelloReply>; + responseDeserialize: grpc.deserialize<models_HelloReply>; +} + +interface IGreeterService_ISayManyHellos extends grpc.MethodDefinition<models_HelloRequest, models_HelloReply> { + path: string; // /models.Greeter/SayManyHellos + requestStream: boolean; // false + responseStream: boolean; // true + requestSerialize: grpc.serialize<models_HelloRequest>; + requestDeserialize: grpc.deserialize<models_HelloRequest>; + responseSerialize: grpc.serialize<models_HelloReply>; + responseDeserialize: grpc.deserialize<models_HelloReply>; +} + + +export const GreeterService: IGreeterService; + +export interface IGreeterServer { + SayHello: grpc.handleUnaryCall<models_HelloRequest, models_HelloReply>; + SayManyHellos: grpc.handleServerStreamingCall<models_HelloRequest, models_HelloReply>; +} + +export interface IGreeterClient { + SayHello(request: models_HelloRequest, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall; + SayHello(request: models_HelloRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall; + SayHello(request: models_HelloRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall; + SayManyHellos(request: models_HelloRequest, metadata: grpc.Metadata): grpc.ClientReadableStream<models_HelloReply>; + SayManyHellos(request: models_HelloRequest, options: Partial<grpc.CallOptions>): grpc.ClientReadableStream<models_HelloReply>; +} + +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; + 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>; + public SayManyHellos(request: models_HelloRequest, options: Partial<grpc.CallOptions>): grpc.ClientReadableStream<models_HelloReply>; +} + diff --git a/grpc/examples/ts/greeter/src/greeter_grpc.js b/grpc/examples/ts/greeter/src/greeter_grpc.js new file mode 100644 index 00000000..aa3af9ae --- /dev/null +++ b/grpc/examples/ts/greeter/src/greeter_grpc.js @@ -0,0 +1,56 @@ +// Generated GRPC code for FlatBuffers TS *** DO NOT EDIT *** +import { 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'); + +function serialize_models_HelloReply(buffer_args) { + if (!(buffer_args instanceof models_HelloReply)) { + throw new Error('Expected argument of type HelloReply'); + } + return buffer_args.serialize(); +} + +function deserialize_models_HelloReply(buffer) { + return models_HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(buffer)) +} + + +function serialize_models_HelloRequest(buffer_args) { + if (!(buffer_args instanceof models_HelloRequest)) { + throw new Error('Expected argument of type HelloRequest'); + } + return buffer_args.serialize(); +} + +function deserialize_models_HelloRequest(buffer) { + return models_HelloRequest.getRootAsHelloRequest(new flatbuffers.ByteBuffer(buffer)) +} + + +var GreeterService = exports.GreeterService = { + SayHello: { + path: '/models.Greeter/SayHello', + requestStream: false, + responseStream: false, + requestType: flatbuffers.ByteBuffer, + responseType: models_HelloReply, + requestSerialize: serialize_models_HelloRequest, + requestDeserialize: deserialize_models_HelloRequest, + responseSerialize: serialize_models_HelloReply, + responseDeserialize: deserialize_models_HelloReply, + }, + SayManyHellos: { + path: '/models.Greeter/SayManyHellos', + requestStream: false, + responseStream: true, + requestType: flatbuffers.ByteBuffer, + responseType: models_HelloReply, + requestSerialize: serialize_models_HelloRequest, + requestDeserialize: deserialize_models_HelloRequest, + responseSerialize: serialize_models_HelloReply, + responseDeserialize: deserialize_models_HelloReply, + }, +}; +exports.GreeterClient = grpc.makeGenericClientConstructor(GreeterService); diff --git a/grpc/examples/ts/greeter/src/models/hello-reply.ts b/grpc/examples/ts/greeter/src/models/hello-reply.ts new file mode 100644 index 00000000..e68f7a4c --- /dev/null +++ b/grpc/examples/ts/greeter/src/models/hello-reply.ts @@ -0,0 +1,58 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +import * as flatbuffers from 'flatbuffers'; + + + +export class HelloReply { + bb: flatbuffers.ByteBuffer|null = null; + bb_pos = 0; +__init(i:number, bb:flatbuffers.ByteBuffer):HelloReply { + this.bb_pos = i; + this.bb = bb; + return this; +} + +static getRootAsHelloReply(bb:flatbuffers.ByteBuffer, obj?:HelloReply):HelloReply { + return (obj || new HelloReply()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +static getSizePrefixedRootAsHelloReply(bb:flatbuffers.ByteBuffer, obj?:HelloReply):HelloReply { + bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH); + return (obj || new HelloReply()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +message():string|null +message(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null +message(optionalEncoding?:any):string|Uint8Array|null { + const offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null; +} + +static startHelloReply(builder:flatbuffers.Builder) { + builder.startObject(1); +} + +static addMessage(builder:flatbuffers.Builder, messageOffset:flatbuffers.Offset) { + builder.addFieldOffset(0, messageOffset, 0); +} + +static endHelloReply(builder:flatbuffers.Builder):flatbuffers.Offset { + const offset = builder.endObject(); + return offset; +} + +static createHelloReply(builder:flatbuffers.Builder, messageOffset:flatbuffers.Offset):flatbuffers.Offset { + HelloReply.startHelloReply(builder); + HelloReply.addMessage(builder, messageOffset); + return HelloReply.endHelloReply(builder); +} + +serialize():Uint8Array { + return this.bb!.bytes(); +} + +static deserialize(buffer: Uint8Array):HelloReply { + return HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(buffer)) +} +} diff --git a/grpc/examples/ts/greeter/src/models/hello-request.ts b/grpc/examples/ts/greeter/src/models/hello-request.ts new file mode 100644 index 00000000..3718167c --- /dev/null +++ b/grpc/examples/ts/greeter/src/models/hello-request.ts @@ -0,0 +1,58 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +import * as flatbuffers from 'flatbuffers'; + + + +export class HelloRequest { + bb: flatbuffers.ByteBuffer|null = null; + bb_pos = 0; +__init(i:number, bb:flatbuffers.ByteBuffer):HelloRequest { + this.bb_pos = i; + this.bb = bb; + return this; +} + +static getRootAsHelloRequest(bb:flatbuffers.ByteBuffer, obj?:HelloRequest):HelloRequest { + return (obj || new HelloRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +static getSizePrefixedRootAsHelloRequest(bb:flatbuffers.ByteBuffer, obj?:HelloRequest):HelloRequest { + bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH); + return (obj || new HelloRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb); +} + +name():string|null +name(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null +name(optionalEncoding?:any):string|Uint8Array|null { + const offset = this.bb!.__offset(this.bb_pos, 4); + return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null; +} + +static startHelloRequest(builder:flatbuffers.Builder) { + builder.startObject(1); +} + +static addName(builder:flatbuffers.Builder, nameOffset:flatbuffers.Offset) { + builder.addFieldOffset(0, nameOffset, 0); +} + +static endHelloRequest(builder:flatbuffers.Builder):flatbuffers.Offset { + const offset = builder.endObject(); + return offset; +} + +static createHelloRequest(builder:flatbuffers.Builder, nameOffset:flatbuffers.Offset):flatbuffers.Offset { + HelloRequest.startHelloRequest(builder); + HelloRequest.addName(builder, nameOffset); + return HelloRequest.endHelloRequest(builder); +} + +serialize():Uint8Array { + return this.bb!.bytes(); +} + +static deserialize(buffer: Uint8Array):HelloRequest { + return HelloRequest.getRootAsHelloRequest(new flatbuffers.ByteBuffer(buffer)) +} +} diff --git a/grpc/examples/ts/greeter/src/server.ts b/grpc/examples/ts/greeter/src/server.ts new file mode 100644 index 00000000..3efb5c8d --- /dev/null +++ b/grpc/examples/ts/greeter/src/server.ts @@ -0,0 +1,41 @@ +import grpc from 'grpc'; +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 { + 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> { + const name = call.request.name(); + console.log(`${call.request.name()} saying hi in different langagues`); + ['Hi', 'Hallo', 'Ciao'].forEach(element => { + const builder = new flatbuffers.Builder(); + const offset = builder.createString(`${element} ${name}`); + const root = HelloReply.createHelloReply(builder, offset); + builder.finish(root); + call.write(HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(builder.asUint8Array()))) + }); + call.end(); + } +} + +function serve(): void { + const PORT = 3000; + const server = new grpc.Server(); + server.addService<IGreeterServer>(GreeterService, new GreeterServer()); + console.log(`Listening on ${PORT}`); + server.bind(`localhost:${PORT}`, grpc.ServerCredentials.createInsecure()); + server.start(); +} + +serve();
\ No newline at end of file diff --git a/grpc/examples/ts/greeter/tsconfig.json b/grpc/examples/ts/greeter/tsconfig.json new file mode 100644 index 00000000..70763787 --- /dev/null +++ b/grpc/examples/ts/greeter/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "outDir": "./dist", + "allowJs": true, + "sourceMap": true, + "strict": true, + "noImplicitAny": false, + "strictNullChecks": false, + "esModuleInterop": true, + "baseUrl": "./", + "typeRoots": ["node_modules/@types"], + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true + } + }
\ No newline at end of file |