diff --git a/javascript/net/grpc/web/grpc_generator.cc b/javascript/net/grpc/web/grpc_generator.cc index 34fcb799e..4673bebdb 100644 --- a/javascript/net/grpc/web/grpc_generator.cc +++ b/javascript/net/grpc/web/grpc_generator.cc @@ -100,7 +100,6 @@ string UppercaseFirstLetter(string s) { return s; } - // The following 5 functions were copied from // google/protobuf/src/google/protobuf/stubs/strutil.h @@ -154,7 +153,6 @@ string StripProto(const string& filename) { } } - // The following 6 functions were copied from // google/protobuf/src/google/protobuf/compiler/js/js_generator.cc @@ -353,8 +351,7 @@ void PrintES6Imports(Printer* printer, const FileDescriptor* file) { void PrintTypescriptFile(Printer* printer, const FileDescriptor* file, std::map vars) { PrintES6Imports(printer, file); - for (int service_index = 0; - service_index < file->service_count(); + for (int service_index = 0; service_index < file->service_count(); ++service_index) { printer->Print("export class "); const ServiceDescriptor* service = file->service(service_index); @@ -372,21 +369,17 @@ void PrintTypescriptFile(Printer* printer, const FileDescriptor* file, printer->Indent(); printer->Print("if (!options) options = {};\n"); if (vars["mode"] == GetModeVar(Mode::GRPCWEB)) { - printer->Print( - vars, - "options['format'] = '$format$';\n\n"); + printer->Print(vars, "options['format'] = '$format$';\n\n"); } - printer->Print( - vars, - "this.client_ = new grpcWeb.$mode$ClientBase(options);\n" - "this.hostname_ = hostname;\n" - "this.credentials_ = credentials;\n" - "this.options_ = options;\n"); + printer->Print(vars, + "this.client_ = new grpcWeb.$mode$ClientBase(options);\n" + "this.hostname_ = hostname;\n" + "this.credentials_ = credentials;\n" + "this.options_ = options;\n"); printer->Outdent(); printer->Print("}\n\n"); - for (int method_index = 0; - method_index < service->method_count(); + for (int method_index = 0; method_index < service->method_count(); ++method_index) { const MethodDescriptor* method = service->method(method_index); vars["js_method_name"] = LowercaseFirstLetter(method->name()); @@ -396,62 +389,52 @@ void PrintTypescriptFile(Printer* printer, const FileDescriptor* file, vars["serialize_func_name"] = GetSerializeMethodName(vars["mode"]); vars["deserialize_func_name"] = GetDeserializeMethodName(vars["mode"]); if (!method->client_streaming()) { - printer->Print( - vars, - "methodInfo_$method_name$ = " - "new grpcWeb.AbstractClientBase.MethodInfo(\n"); + printer->Print(vars, + "methodInfo_$method_name$ = " + "new grpcWeb.AbstractClientBase.MethodInfo(\n"); printer->Indent(); - printer->Print( - vars, - "$output_type$,\n" - "function(request: $input_type$) {\n" - " return request.$serialize_func_name$();\n" - "},\n" - "$output_type$.$deserialize_func_name$\n"); + printer->Print(vars, + "$output_type$,\n" + "function(request: $input_type$) {\n" + " return request.$serialize_func_name$();\n" + "},\n" + "$output_type$.$deserialize_func_name$\n"); printer->Outdent(); printer->Print(");\n\n"); if (method->server_streaming()) { printer->Print(vars, "$js_method_name$(\n"); printer->Indent(); - printer->Print( - vars, - "request: $input_type$,\n" - "metadata: grpcWeb.Metadata) {\n"); - printer->Print( - vars, - "return this.client_.serverStreaming(\n"); + printer->Print(vars, + "request: $input_type$,\n" + "metadata: grpcWeb.Metadata) {\n"); + printer->Print(vars, "return this.client_.serverStreaming(\n"); printer->Indent(); - printer->Print( - vars, - "this.hostname_ +\n" - " '/$package_dot$$service_name$/$method_name$',\n" - "request,\n" - "metadata,\n" - "this.methodInfo_$method_name$);\n"); + printer->Print(vars, + "this.hostname_ +\n" + " '/$package_dot$$service_name$/$method_name$',\n" + "request,\n" + "metadata,\n" + "this.methodInfo_$method_name$);\n"); printer->Outdent(); printer->Outdent(); printer->Print("}\n\n"); } else { printer->Print(vars, "$js_method_name$(\n"); printer->Indent(); - printer->Print( - vars, - "request: $input_type$,\n" - "metadata: grpcWeb.Metadata,\n" - "callback: (err: grpcWeb.Error,\n" - " response: $output_type$) => void) {\n"); - printer->Print( - vars, - "return this.client_.rpcCall(\n"); + printer->Print(vars, + "request: $input_type$,\n" + "metadata: grpcWeb.Metadata,\n" + "callback: (err: grpcWeb.Error,\n" + " response: $output_type$) => void) {\n"); + printer->Print(vars, "return this.client_.rpcCall(\n"); printer->Indent(); - printer->Print( - vars, - "this.hostname_ +\n" - " '/$package_dot$$service_name$/$method_name$',\n" - "request,\n" - "metadata,\n" - "this.methodInfo_$method_name$,\n" - "callback);\n"); + printer->Print(vars, + "this.hostname_ +\n" + " '/$package_dot$$service_name$/$method_name$',\n" + "request,\n" + "metadata,\n" + "this.methodInfo_$method_name$,\n" + "callback);\n"); printer->Outdent(); printer->Outdent(); printer->Print("}\n\n"); @@ -466,8 +449,7 @@ void PrintTypescriptFile(Printer* printer, const FileDescriptor* file, void PrintGrpcWebDtsFile(Printer* printer, const FileDescriptor* file) { PrintES6Imports(printer, file); std::map vars; - for (int service_index = 0; - service_index < file->service_count(); + for (int service_index = 0; service_index < file->service_count(); ++service_index) { printer->Print("export class "); const ServiceDescriptor* service = file->service(service_index); @@ -478,8 +460,7 @@ void PrintGrpcWebDtsFile(Printer* printer, const FileDescriptor* file) { "constructor (hostname: string,\n" " credentials: any,\n" " options: any);\n\n"); - for (int method_index = 0; - method_index < service->method_count(); + for (int method_index = 0; method_index < service->method_count(); ++method_index) { const MethodDescriptor* method = service->method(method_index); vars["js_method_name"] = LowercaseFirstLetter(method->name()); @@ -489,21 +470,19 @@ void PrintGrpcWebDtsFile(Printer* printer, const FileDescriptor* file) { if (method->server_streaming()) { printer->Print(vars, "$js_method_name$(\n"); printer->Indent(); - printer->Print( - vars, - "request: $input_type$,\n" - "metadata: grpcWeb.Metadata\n"); + printer->Print(vars, + "request: $input_type$,\n" + "metadata: grpcWeb.Metadata\n"); printer->Outdent(); printer->Print("): grpcWeb.ClientReadableStream;\n\n"); } else { printer->Print(vars, "$js_method_name$(\n"); printer->Indent(); - printer->Print( - vars, - "request: $input_type$,\n" - "metadata: grpcWeb.Metadata,\n" - "callback: (err: grpcWeb.Error,\n" - " response: $output_type$) => void\n"); + printer->Print(vars, + "request: $input_type$,\n" + "metadata: grpcWeb.Metadata,\n" + "callback: (err: grpcWeb.Error,\n" + " response: $output_type$) => void\n"); printer->Outdent(); printer->Print("): grpcWeb.ClientReadableStream;\n\n"); } @@ -558,8 +537,8 @@ void PrintProtoDtsFile(Printer* printer, const FileDescriptor* file) { printer->Print(vars, "set$js_field_name$(a: $js_field_type$): void;\n"); } printer->Print(vars, - "serializeBinary(): Uint8Array;\n" - "static deserializeBinary: (bytes: any) => $class_name$;\n"); + "serializeBinary(): Uint8Array;\n" + "static deserializeBinary: (bytes: any) => $class_name$;\n"); printer->Outdent(); printer->Print("}\n\n"); } @@ -798,8 +777,8 @@ class GrpcCodeGenerator : public CodeGenerator { generate_dts = true; } else if (import_style_str == "typescript") { import_style = ImportStyle::TYPESCRIPT; - file_name = UppercaseFirstLetter(StripProto(file->name())) + - "ServiceClientPb.ts"; + file_name = + UppercaseFirstLetter(StripProto(file->name())) + "ServiceClientPb.ts"; } else { *error = "options: invalid import_style - " + import_style_str; return false; @@ -813,10 +792,9 @@ class GrpcCodeGenerator : public CodeGenerator { if (import_style == ImportStyle::TYPESCRIPT) { PrintTypescriptFile(&printer, file, vars); - string proto_dts_file_name = StripProto(file->name()) + - "_pb.d.ts"; + string proto_dts_file_name = StripProto(file->name()) + "_pb.d.ts"; std::unique_ptr proto_dts_output( - context->Open(proto_dts_file_name)); + context->Open(proto_dts_file_name)); Printer proto_dts_printer(proto_dts_output.get(), '$'); PrintProtoDtsFile(&proto_dts_printer, file); @@ -907,19 +885,18 @@ class GrpcCodeGenerator : public CodeGenerator { } if (generate_dts) { - string grpcweb_dts_file_name = StripProto(file->name()) + - "_grpc_web_pb.d.ts"; - string proto_dts_file_name = StripProto(file->name()) + - "_pb.d.ts"; + string grpcweb_dts_file_name = + StripProto(file->name()) + "_grpc_web_pb.d.ts"; + string proto_dts_file_name = StripProto(file->name()) + "_pb.d.ts"; std::unique_ptr grpcweb_dts_output( - context->Open(grpcweb_dts_file_name)); + context->Open(grpcweb_dts_file_name)); Printer grpcweb_dts_printer(grpcweb_dts_output.get(), '$'); PrintGrpcWebDtsFile(&grpcweb_dts_printer, file); std::unique_ptr proto_dts_output( - context->Open(proto_dts_file_name)); + context->Open(proto_dts_file_name)); Printer proto_dts_printer(proto_dts_output.get(), '$'); PrintProtoDtsFile(&proto_dts_printer, file); diff --git a/net/grpc/gateway/examples/echo/ts-example/client.ts b/net/grpc/gateway/examples/echo/ts-example/client.ts index a459bf891..502d647bf 100644 --- a/net/grpc/gateway/examples/echo/ts-example/client.ts +++ b/net/grpc/gateway/examples/echo/ts-example/client.ts @@ -18,18 +18,17 @@ import * as grpcWeb from 'grpc-web'; import * as $ from 'jquery'; -import {EchoRequest, EchoResponse, - ServerStreamingEchoRequest, - ServerStreamingEchoResponse} from './echo_pb'; // Uncomment either one of the following: // Option 1: import_style=commonjs+dts import {EchoServiceClient} from './echo_grpc_web_pb'; +import {EchoRequest, EchoResponse, ServerStreamingEchoRequest, ServerStreamingEchoResponse} from './echo_pb'; + // Option 2: import_style=typescript // import {EchoServiceClient} from './EchoServiceClientPb'; class EchoApp { - static readonly INTERVAL = 500; // ms + static readonly INTERVAL = 500; // ms static readonly MAX_STREAM_MESSAGES = 50; echoService_: EchoServiceClient; @@ -38,18 +37,16 @@ class EchoApp { } static addMessage(message: string, cssClass: string) { - $("#first").after( - $("
").addClass("row").append( - $("

").append( - $("").addClass("label " + cssClass).text(message)))); + $('#first').after($('
').addClass('row').append($('

').append( + $('').addClass('label ' + cssClass).text(message)))); } static addLeftMessage(message: string) { - this.addMessage(message, "label-primary pull-left"); + this.addMessage(message, 'label-primary pull-left'); } static addRightMessage(message: string) { - this.addMessage(message, "label-default pull-right"); + this.addMessage(message, 'label-default pull-right'); } echo(msg: string) { @@ -57,23 +54,22 @@ class EchoApp { const request = new EchoRequest(); request.setMessage(msg); const call = this.echoService_.echo( - request, - {"custom-header-1": "value1"}, - (err: grpcWeb.Error, response: EchoResponse) => { - if (err) { - if (err.code !== grpcWeb.StatusCode.OK) { - EchoApp.addRightMessage('Error code: '+err.code+' "'+ - err.message+'"'); + request, {'custom-header-1': 'value1'}, + (err: grpcWeb.Error, response: EchoResponse) => { + if (err) { + if (err.code !== grpcWeb.StatusCode.OK) { + EchoApp.addRightMessage( + 'Error code: ' + err.code + ' "' + err.message + '"'); + } + } else { + setTimeout(() => { + EchoApp.addRightMessage(response.getMessage()); + }, EchoApp.INTERVAL); } - } else { - setTimeout(() => { - EchoApp.addRightMessage(response.getMessage()); - }, EchoApp.INTERVAL); - } - }); + }); call.on('status', (status: grpcWeb.Status) => { if (status.metadata) { - console.log("Received metadata"); + console.log('Received metadata'); console.log(status.metadata); } }); @@ -84,16 +80,14 @@ class EchoApp { const request = new EchoRequest(); request.setMessage(msg); this.echoService_.echoAbort( - request, {}, - (err: grpcWeb.Error, - response: EchoResponse) => { - if (err) { - if (err.code !== grpcWeb.StatusCode.OK) { - EchoApp.addRightMessage('Error code: '+err.code+' "'+ - err.message+'"'); - } - } - }); + request, {}, (err: grpcWeb.Error, response: EchoResponse) => { + if (err) { + if (err.code !== grpcWeb.StatusCode.OK) { + EchoApp.addRightMessage( + 'Error code: ' + err.code + ' "' + err.message + '"'); + } + } + }); } repeatEcho(msg: string, count: number) { @@ -107,32 +101,31 @@ class EchoApp { request.setMessageInterval(EchoApp.INTERVAL); const stream: grpcWeb.ClientReadableStream = - this.echoService_.serverStreamingEcho( - request, - {"custom-header-1": "value1"}); + this.echoService_.serverStreamingEcho( + request, {'custom-header-1': 'value1'}); const self = this; stream.on('data', (response: ServerStreamingEchoResponse) => { EchoApp.addRightMessage(response.getMessage()); }); stream.on('status', (status: grpcWeb.Status) => { if (status.metadata) { - console.log("Received metadata"); + console.log('Received metadata'); console.log(status.metadata); } }); stream.on('error', (err: grpcWeb.Error) => { - EchoApp.addRightMessage('Error code: '+err.code+' "'+ - err.message+'"'); + EchoApp.addRightMessage( + 'Error code: ' + err.code + ' "' + err.message + '"'); }); stream.on('end', () => { - console.log("stream end signal received"); + console.log('stream end signal received'); }); } send(e: {}) { - const _msg: string = $("#msg").val() as string; + const _msg: string = $('#msg').val() as string; const msg = _msg.trim(); - $("#msg").val(''); // clear the text box + $('#msg').val(''); // clear the text box if (!msg) return false; if (msg.indexOf(' ') > 0) { @@ -153,13 +146,13 @@ class EchoApp { const self = this; $(document).ready(() => { // event handlers - $("#send").click(self.send.bind(self)); - $("#msg").keyup((e) => { - if (e.keyCode === 13) self.send(e); // enter key + $('#send').click(self.send.bind(self)); + $('#msg').keyup((e) => { + if (e.keyCode === 13) self.send(e); // enter key return false; }); - $("#msg").focus(); + $('#msg').focus(); }); } } diff --git a/net/grpc/gateway/examples/echo/ts-example/webpack.config.js b/net/grpc/gateway/examples/echo/ts-example/webpack.config.js index 47b5cc556..6831e7b48 100644 --- a/net/grpc/gateway/examples/echo/ts-example/webpack.config.js +++ b/net/grpc/gateway/examples/echo/ts-example/webpack.config.js @@ -1,4 +1,4 @@ module.exports = { - mode: "production", - entry: "./dist/client.js", + mode: 'production', + entry: './dist/client.js', };