From 71f1c3aa644dcd392980d6a7722411743ddfa5ca Mon Sep 17 00:00:00 2001 From: Jonny Reeves Date: Wed, 12 Sep 2018 13:45:36 +0100 Subject: [PATCH] Replace usage of `Object.assign` The grpc-web service stub code generated by ts-protoc-gen is invoking an ES6 function (`Object.assign`) which is tricking webpack into treating the modules as ES6 modules instead of ES5 modules. Replacing the `Object.assign` invocation with some vanilla ES5 results in webpack correctly identify the module as a CommonJS module and thereby exposing the `exports` object to the scope of the function. Fixes #103 --- .../proto/examplecom/simple_service_pb_service.js | 10 ++++++++-- examples/generated/proto/orphan_pb_service.js | 5 ++++- src/service/grpcweb.ts | 5 ++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/examples/generated/proto/examplecom/simple_service_pb_service.js b/examples/generated/proto/examplecom/simple_service_pb_service.js index 14a5a407..bee0f019 100644 --- a/examples/generated/proto/examplecom/simple_service_pb_service.js +++ b/examples/generated/proto/examplecom/simple_service_pb_service.js @@ -77,7 +77,10 @@ SimpleServiceClient.prototype.doUnary = function doUnary(requestMessage, metadat onEnd: function (response) { if (callback) { if (response.status !== grpc.Code.OK) { - callback(Object.assign(new Error(response.statusMessage), { code: response.status, metadata: response.trailers }), null); + var err = new Error(response.statusMessage); + err.code = response.status; + err.metadata = response.trailers; + callback(err, null); } else { callback(null, response.message); } @@ -224,7 +227,10 @@ SimpleServiceClient.prototype.delete = function pb_delete(requestMessage, metada onEnd: function (response) { if (callback) { if (response.status !== grpc.Code.OK) { - callback(Object.assign(new Error(response.statusMessage), { code: response.status, metadata: response.trailers }), null); + var err = new Error(response.statusMessage); + err.code = response.status; + err.metadata = response.trailers; + callback(err, null); } else { callback(null, response.message); } diff --git a/examples/generated/proto/orphan_pb_service.js b/examples/generated/proto/orphan_pb_service.js index 979be1ea..f2f6c77e 100644 --- a/examples/generated/proto/orphan_pb_service.js +++ b/examples/generated/proto/orphan_pb_service.js @@ -48,7 +48,10 @@ OrphanServiceClient.prototype.doUnary = function doUnary(requestMessage, metadat onEnd: function (response) { if (callback) { if (response.status !== grpc.Code.OK) { - callback(Object.assign(new Error(response.statusMessage), { code: response.status, metadata: response.trailers }), null); + var err = new Error(response.statusMessage); + err.code = response.status; + err.metadata = response.trailers; + callback(err, null); } else { callback(null, response.message); } diff --git a/src/service/grpcweb.ts b/src/service/grpcweb.ts index b08a070f..d79b1af8 100644 --- a/src/service/grpcweb.ts +++ b/src/service/grpcweb.ts @@ -334,7 +334,10 @@ function printUnaryStubMethod(printer: CodePrinter, method: RPCMethodDescriptor) .printLn(`onEnd: function (response) {`) .indent().printLn(`if (callback) {`) .indent().printLn(`if (response.status !== grpc.Code.OK) {`) - .indent().printLn(`callback(Object.assign(new Error(response.statusMessage), { code: response.status, metadata: response.trailers }), null);`) + .indent().printLn(`var err = new Error(response.statusMessage);`) + .printLn(`err.code = response.status;`) + .printLn(`err.metadata = response.trailers;`) + .printLn(`callback(err, null);`) .dedent().printLn(`} else {`) .indent().printLn(`callback(null, response.message);`) .dedent().printLn(`}`)