diff --git a/javascript/net/grpc/web/abstractclientbase.js b/javascript/net/grpc/web/abstractclientbase.js index d39befcba..e1e85f8f7 100644 --- a/javascript/net/grpc/web/abstractclientbase.js +++ b/javascript/net/grpc/web/abstractclientbase.js @@ -46,16 +46,17 @@ const AbstractClientBase = function() {}; * @param {function(new: RESPONSE, ...)} responseType * @param {function(REQUEST): ?} requestSerializeFn * @param {function(?): RESPONSE} responseDeserializeFn + * @export */ AbstractClientBase.MethodInfo = function( responseType, requestSerializeFn, responseDeserializeFn) { - /** @const */ + /** @const @export */ this.responseType = responseType; - /** @const */ + /** @const @export */ this.requestSerializeFn = requestSerializeFn; - /** @const */ + /** @const @export */ this.responseDeserializeFn = responseDeserializeFn; }; @@ -81,6 +82,7 @@ AbstractClientBase.MethodInfo = function( * callback A callback function which takes (error, response) * @return {!ClientReadableStream|undefined} * The Client Readable Stream + * @export */ AbstractClientBase.prototype.rpcCall = goog.abstractMethod; @@ -93,6 +95,7 @@ AbstractClientBase.prototype.rpcCall = goog.abstractMethod; * @param {!AbstractClientBase.MethodInfo} * methodInfo Information of this RPC method * @return {!ClientReadableStream} The Client Readable Stream + * @export */ AbstractClientBase.prototype.serverStreaming = goog.abstractMethod; diff --git a/javascript/net/grpc/web/clientreadablestream.js b/javascript/net/grpc/web/clientreadablestream.js index 0f3f0bb6d..a28d151bf 100644 --- a/javascript/net/grpc/web/clientreadablestream.js +++ b/javascript/net/grpc/web/clientreadablestream.js @@ -49,6 +49,7 @@ const ClientReadableStream = function() {}; * @param {function(?)} callback The call back to handle the event with * an optional input object * @return {!ClientReadableStream} this object + * @export */ ClientReadableStream.prototype.on = goog.abstractMethod; @@ -56,6 +57,7 @@ ClientReadableStream.prototype.on = goog.abstractMethod; /** * Close the stream. + * @export */ ClientReadableStream.prototype.cancel = goog.abstractMethod; diff --git a/javascript/net/grpc/web/error.js b/javascript/net/grpc/web/error.js index 79f8ec26f..fe50c3ff1 100644 --- a/javascript/net/grpc/web/error.js +++ b/javascript/net/grpc/web/error.js @@ -26,14 +26,13 @@ goog.module('grpc.web.Error'); goog.module.declareLegacyNamespace(); +/** @record */ +function Error() {} +/** @export @type {(number|undefined)} */ +Error.prototype.code; -/** - * @typedef {{ - * code: (number|undefined), - * message: (string|undefined), - * }} - */ -let Error; +/** @export @type {(string|undefined)} */ +Error.prototype.message; exports = Error; diff --git a/javascript/net/grpc/web/gatewayclientbase.js b/javascript/net/grpc/web/gatewayclientbase.js index 461003eb3..28b36b9be 100644 --- a/javascript/net/grpc/web/gatewayclientbase.js +++ b/javascript/net/grpc/web/gatewayclientbase.js @@ -46,6 +46,7 @@ const {Status} = goog.require('grpc.web.Status'); * @param {?Object=} opt_options * @constructor * @implements {AbstractClientBase} + * @export */ const GatewayClientBase = function(opt_options) { }; @@ -53,6 +54,7 @@ const GatewayClientBase = function(opt_options) { /** * @override + * @export */ GatewayClientBase.prototype.rpcCall = function( method, request, metadata, methodInfo, callback) { @@ -90,6 +92,7 @@ GatewayClientBase.prototype.rpcCall = function( /** * @override + * @export */ GatewayClientBase.prototype.serverStreaming = function( method, request, metadata, methodInfo) { @@ -176,6 +179,7 @@ GatewayClientBase.parseRpcStatus_ = function(data) { pair.getSecond_asU8()); metadata[first] = second; } + /** @type {!Status} */ var status = { code: rpcStatus.getCode(), details: rpcStatus.getMessage(), diff --git a/javascript/net/grpc/web/grpcwebclientbase.js b/javascript/net/grpc/web/grpcwebclientbase.js index d5ae0ef42..9836e66be 100644 --- a/javascript/net/grpc/web/grpcwebclientbase.js +++ b/javascript/net/grpc/web/grpcwebclientbase.js @@ -61,6 +61,7 @@ const GrpcWebClientBase = function(opt_options) { /** * @override + * @export */ GrpcWebClientBase.prototype.rpcCall = function( method, request, metadata, methodInfo, callback) { @@ -116,6 +117,7 @@ GrpcWebClientBase.prototype.rpcCall = function( /** * @override + * @export */ GrpcWebClientBase.prototype.serverStreaming = function( method, request, metadata, methodInfo) { diff --git a/javascript/net/grpc/web/grpcwebclientreadablestream.js b/javascript/net/grpc/web/grpcwebclientreadablestream.js index a4149a7f6..eb40f141c 100644 --- a/javascript/net/grpc/web/grpcwebclientreadablestream.js +++ b/javascript/net/grpc/web/grpcwebclientreadablestream.js @@ -42,7 +42,7 @@ const events = goog.require('goog.events'); const googCrypt = goog.require('goog.crypt.base64'); const googString = goog.require('goog.string'); const {GenericTransportInterface} = goog.require('grpc.web.GenericTransportInterface'); -const {Status} = goog.require('grpc.web.Status'); +const Status = goog.require('grpc.web.Status'); @@ -164,11 +164,11 @@ const GrpcWebClientReadableStream = function(genericTransportInterface) { grpcStatusMessage = trailers[GRPC_STATUS_MESSAGE]; } if (self.onStatusCallback_) { - self.onStatusCallback_({ + self.onStatusCallback_(/** @type {!Status} */({ code: Number(grpcStatusCode), details: grpcStatusMessage, metadata: trailers, - }); + })); } } } @@ -207,6 +207,7 @@ const GrpcWebClientReadableStream = function(genericTransportInterface) { /** * @override + * @export */ GrpcWebClientReadableStream.prototype.on = function( eventType, callback) { @@ -229,6 +230,7 @@ GrpcWebClientReadableStream.prototype.on = function( * * @param {function(?):!RESPONSE} responseDeserializeFn The deserialize * function for the proto + * @export */ GrpcWebClientReadableStream.prototype.setResponseDeserializeFn = function(responseDeserializeFn) { @@ -238,6 +240,7 @@ GrpcWebClientReadableStream.prototype.setResponseDeserializeFn = /** * @override + * @export */ GrpcWebClientReadableStream.prototype.cancel = function() { this.xhr_.abort(); diff --git a/javascript/net/grpc/web/grpcwebstreamparser.js b/javascript/net/grpc/web/grpcwebstreamparser.js index ace94dd88..5c561f522 100644 --- a/javascript/net/grpc/web/grpcwebstreamparser.js +++ b/javascript/net/grpc/web/grpcwebstreamparser.js @@ -131,6 +131,7 @@ Parser.State_ = { /** * Possible frame byte * @enum {number} + * @export */ GrpcWebStreamParser.FrameType = { DATA: 0x00, // expecting a data frame diff --git a/javascript/net/grpc/web/status.js b/javascript/net/grpc/web/status.js index f8ac7bd2b..04541e7d4 100644 --- a/javascript/net/grpc/web/status.js +++ b/javascript/net/grpc/web/status.js @@ -26,13 +26,16 @@ goog.module('grpc.web.Status'); goog.module.declareLegacyNamespace(); +/** @record */ +function Status() {} +/** @export @type {number} */ +Status.prototype.code; -/** - * @typedef {{ - * code: number, - * details: string, - * metadata: (!Object|undefined) - * }} - */ -exports.Status; +/** @export @type {string} */ +Status.prototype.details; + +/** @export @type {(!Object|undefined)} */ +Status.prototype.metadata; + +exports = Status; diff --git a/javascript/net/grpc/web/statuscode.js b/javascript/net/grpc/web/statuscode.js index 8cac2ff5c..9347263eb 100644 --- a/javascript/net/grpc/web/statuscode.js +++ b/javascript/net/grpc/web/statuscode.js @@ -146,6 +146,7 @@ const StatusCode = { * Convert HTTP Status code to gRPC Status code * @param {number} http_status HTTP Status Code * @return {number} gRPC Status Code + * @export */ StatusCode.fromHttpStatus = function(http_status) { switch (http_status) { diff --git a/javascript/net/grpc/web/streambodyclientreadablestream.js b/javascript/net/grpc/web/streambodyclientreadablestream.js index b4bb25c0c..b1336bcb8 100644 --- a/javascript/net/grpc/web/streambodyclientreadablestream.js +++ b/javascript/net/grpc/web/streambodyclientreadablestream.js @@ -51,6 +51,7 @@ const {Status} = goog.require('grpc.web.Status'); * @final * @param {!GenericTransportInterface} genericTransportInterface The * GenericTransportInterface + * @export */ const StreamBodyClientReadableStream = function(genericTransportInterface) { /** @@ -139,6 +140,7 @@ const StreamBodyClientReadableStream = function(genericTransportInterface) { /** * @override + * @export */ StreamBodyClientReadableStream.prototype.on = function( eventType, callback) { @@ -161,6 +163,7 @@ StreamBodyClientReadableStream.prototype.on = function( * * @param {function(?): RESPONSE} responseDeserializeFn The deserialize * function for the proto + * @export */ StreamBodyClientReadableStream.prototype.setResponseDeserializeFn = function(responseDeserializeFn) { @@ -174,6 +177,7 @@ StreamBodyClientReadableStream.prototype.setResponseDeserializeFn = * * @param {function(?):!Status} rpcStatusParseFn A function to parse * the RPC status response + * @export */ StreamBodyClientReadableStream.prototype.setRpcStatusParseFn = function(rpcStatusParseFn) { this.rpcStatusParseFn_ = rpcStatusParseFn; @@ -182,6 +186,7 @@ StreamBodyClientReadableStream.prototype.setRpcStatusParseFn = function(rpcStatu /** * @override + * @export */ StreamBodyClientReadableStream.prototype.cancel = function() { this.xhr_.abort(); diff --git a/javascript/net/grpc/web/util/genericpbjsclient.js b/javascript/net/grpc/web/util/genericpbjsclient.js index d4fc31c57..0f05dcf08 100644 --- a/javascript/net/grpc/web/util/genericpbjsclient.js +++ b/javascript/net/grpc/web/util/genericpbjsclient.js @@ -40,6 +40,7 @@ var GatewayClientBase = goog.require('grpc.web.GatewayClientBase'); * @constructor * @struct * @final + * @export */ var GenericPbjsClient = function(hostname) { @@ -62,6 +63,7 @@ var GenericPbjsClient = function(hostname) { * * @param {!Object} method The method (a Protobuf.js Method object) * @return {string} The full name of the service containing the method + * @export */ function getServiceName(method) { var fullName = method.parent.fullName; @@ -80,6 +82,7 @@ function getServiceName(method) { * @param {!Object} metadata User defined call metadata * @param {function(?Error, ?Object)} callback A callback function * which takes (error, response) + * @export */ GenericPbjsClient.prototype.rpcCall = function( method, request, metadata, callback) { diff --git a/packages/grpc-web/exports.js b/packages/grpc-web/exports.js new file mode 100644 index 000000000..f3e958a6a --- /dev/null +++ b/packages/grpc-web/exports.js @@ -0,0 +1,21 @@ +goog.module('grpc.web.Exports'); + +const AbstractClientBase = goog.require('grpc.web.AbstractClientBase'); +const ClientReadableStream = goog.require('grpc.web.ClientReadableStream'); +const Error = goog.require('grpc.web.Error'); +const GrpcWebClientBase = goog.require('grpc.web.GrpcWebClientBase'); +const GrpcWebClientReadableStream = goog.require('grpc.web.GrpcWebClientReadableStream'); +const GrpcWebStreamParser = goog.require('grpc.web.GrpcWebStreamParser'); +const Status = goog.require('grpc.web.Status'); +const StatusCode = goog.require('grpc.web.StatusCode'); + +const exports = module['exports']; +exports['AbstractClientBase'] = AbstractClientBase; +exports['AbstractClientBase']['MethodInfo'] = AbstractClientBase.MethodInfo; +exports['ClientReadableStream'] = ClientReadableStream; +exports['Error'] = Error; +exports['GrpcWebClientBase'] = GrpcWebClientBase; +exports['GrpcWebClientReadableStream'] = GrpcWebClientReadableStream; +exports['GrpcWebStreamParser'] = GrpcWebStreamParser; +exports['Status'] = Status; +exports['StatusCode'] = StatusCode; diff --git a/packages/grpc-web/module.js b/packages/grpc-web/module.js new file mode 100644 index 000000000..a035483a7 --- /dev/null +++ b/packages/grpc-web/module.js @@ -0,0 +1,2 @@ +// Tells Closure Compiler that "module" is externally defined +let module; diff --git a/packages/grpc-web/scripts/build.js b/packages/grpc-web/scripts/build.js index f8d52f90a..8fe25dd74 100644 --- a/packages/grpc-web/scripts/build.js +++ b/packages/grpc-web/scripts/build.js @@ -24,29 +24,21 @@ const cwd = process.cwd(); const indexPath = path.relative(cwd, path.resolve(__dirname, "../index.js")); const jsPaths = [ + "../exports.js", "../../../javascript", "../../../third_party/closure-library", - "../../../third_party/grpc/third_party/protobuf/js", ].map(jsPath => path.relative(cwd, path.resolve(__dirname, jsPath))); -const grpcWebExports = [ - "grpc.web.AbstractClientBase", - "grpc.web.ClientReadableStream", - "grpc.web.Error", - "grpc.web.GrpcWebClientBase", - "grpc.web.GrpcWebClientReadableStream", - "grpc.web.GrpcWebStreamParser", - "grpc.web.Status", - "grpc.web.StatusCode", -]; - const closureArgs = [].concat( jsPaths.map(jsPath => `--js=${jsPath}`), - grpcWebExports.map(grpcWebExport => `--entry_point=${grpcWebExport}`), [ + `--entry_point=grpc.web.Exports`, + `--externs=module.js`, `--dependency_mode=STRICT`, + `--compilation_level=ADVANCED_OPTIMIZATIONS`, + `--generate_exports`, + `--export_local_property_definitions`, `--js_output_file=${indexPath}`, - `--output_wrapper="%output%module.exports = grpc.web;"`, ] );