From 86a0ce26935b7cb0272e46af79772816d0a883dd Mon Sep 17 00:00:00 2001 From: Yoshi Automation Bot Date: Tue, 3 Sep 2019 14:05:29 -0700 Subject: [PATCH] feat: load protos from JSON, grpc-fallback support * [CHANGE ME] Re-generated to pick up changes in the API or client library generator. * fixes * fix webpack.config.js --- .../google-cloud-asset/protos/protos.json | 457 +++++++++++++++++- packages/google-cloud-asset/src/browser.js | 21 + .../src/service_proto_list.json | 1 - .../src/v1/asset_service_client.js | 91 ++-- .../src/v1/asset_service_proto_list.json | 3 + .../src/v1beta1/asset_service_client.js | 91 ++-- .../src/v1beta1/asset_service_proto_list.json | 3 + .../src/v1p2beta1/asset_service_client.js | 93 ++-- .../v1p2beta1/asset_service_proto_list.json | 3 + packages/google-cloud-asset/synth.metadata | 10 +- packages/google-cloud-asset/synth.py | 1 + packages/google-cloud-asset/test/gapic-v1.js | 5 + .../google-cloud-asset/test/gapic-v1beta1.js | 5 + .../test/gapic-v1p2beta1.js | 7 + packages/google-cloud-asset/webpack.config.js | 46 ++ 15 files changed, 704 insertions(+), 133 deletions(-) create mode 100644 packages/google-cloud-asset/src/browser.js delete mode 100644 packages/google-cloud-asset/src/service_proto_list.json create mode 100644 packages/google-cloud-asset/src/v1/asset_service_proto_list.json create mode 100644 packages/google-cloud-asset/src/v1beta1/asset_service_proto_list.json create mode 100644 packages/google-cloud-asset/src/v1p2beta1/asset_service_proto_list.json create mode 100644 packages/google-cloud-asset/webpack.config.js diff --git a/packages/google-cloud-asset/protos/protos.json b/packages/google-cloud-asset/protos/protos.json index a5c387ffe3d..4244094a8a8 100644 --- a/packages/google-cloud-asset/protos/protos.json +++ b/packages/google-cloud-asset/protos/protos.json @@ -6,17 +6,139 @@ "nested": { "asset": { "nested": { - "v1p2beta1": { + "v1": { "options": { - "cc_enable_arenas": true, - "csharp_namespace": "Google.Cloud.Asset.V1p2Beta1", - "go_package": "google.golang.org/genproto/googleapis/cloud/asset/v1p2beta1;asset", + "csharp_namespace": "Google.Cloud.Asset.V1", + "go_package": "google.golang.org/genproto/googleapis/cloud/asset/v1;asset", "java_multiple_files": true, - "java_outer_classname": "AssetServiceProto", - "java_package": "com.google.cloud.asset.v1p2beta1", - "php_namespace": "Google\\Cloud\\Asset\\V1p2Beta1" + "java_outer_classname": "AssetProto", + "java_package": "com.google.cloud.asset.v1", + "php_namespace": "Google\\Cloud\\Asset\\V1" }, "nested": { + "AssetService": { + "methods": { + "ExportAssets": { + "requestType": "ExportAssetsRequest", + "responseType": "google.longrunning.Operation", + "options": { + "(google.api.http).post": "/v1/{parent=*/*}:exportAssets", + "(google.api.http).body": "*" + } + }, + "BatchGetAssetsHistory": { + "requestType": "BatchGetAssetsHistoryRequest", + "responseType": "BatchGetAssetsHistoryResponse", + "options": { + "(google.api.http).get": "/v1/{parent=*/*}:batchGetAssetsHistory" + } + } + } + }, + "ExportAssetsRequest": { + "fields": { + "parent": { + "type": "string", + "id": 1 + }, + "readTime": { + "type": "google.protobuf.Timestamp", + "id": 2 + }, + "assetTypes": { + "rule": "repeated", + "type": "string", + "id": 3 + }, + "contentType": { + "type": "ContentType", + "id": 4 + }, + "outputConfig": { + "type": "OutputConfig", + "id": 5 + } + } + }, + "ExportAssetsResponse": { + "fields": { + "readTime": { + "type": "google.protobuf.Timestamp", + "id": 1 + }, + "outputConfig": { + "type": "OutputConfig", + "id": 2 + } + } + }, + "BatchGetAssetsHistoryRequest": { + "fields": { + "parent": { + "type": "string", + "id": 1 + }, + "assetNames": { + "rule": "repeated", + "type": "string", + "id": 2 + }, + "contentType": { + "type": "ContentType", + "id": 3 + }, + "readTimeWindow": { + "type": "TimeWindow", + "id": 4 + } + } + }, + "BatchGetAssetsHistoryResponse": { + "fields": { + "assets": { + "rule": "repeated", + "type": "TemporalAsset", + "id": 1 + } + } + }, + "OutputConfig": { + "oneofs": { + "destination": { + "oneof": [ + "gcsDestination" + ] + } + }, + "fields": { + "gcsDestination": { + "type": "GcsDestination", + "id": 1 + } + } + }, + "GcsDestination": { + "oneofs": { + "objectUri": { + "oneof": [ + "uri" + ] + } + }, + "fields": { + "uri": { + "type": "string", + "id": 1 + } + } + }, + "ContentType": { + "values": { + "CONTENT_TYPE_UNSPECIFIED": 0, + "RESOURCE": 1, + "IAM_POLICY": 2 + } + }, "TemporalAsset": { "fields": { "window": { @@ -62,15 +184,220 @@ "iamPolicy": { "type": "google.iam.v1.Policy", "id": 4 + } + } + }, + "Resource": { + "fields": { + "version": { + "type": "string", + "id": 1 }, - "iamPolicyName": { - "type": "bytes", + "discoveryDocumentUri": { + "type": "string", + "id": 2 + }, + "discoveryName": { + "type": "string", + "id": 3 + }, + "resourceUrl": { + "type": "string", + "id": 4 + }, + "parent": { + "type": "string", "id": 5 }, - "ancestors": { + "data": { + "type": "google.protobuf.Struct", + "id": 6 + } + } + } + } + }, + "v1beta1": { + "options": { + "csharp_namespace": "Google.Cloud.Asset.V1Beta1", + "go_package": "google.golang.org/genproto/googleapis/cloud/asset/v1beta1;asset", + "java_multiple_files": true, + "java_outer_classname": "AssetProto", + "java_package": "com.google.cloud.asset.v1beta1", + "php_namespace": "Google\\Cloud\\Asset\\V1beta1" + }, + "nested": { + "AssetService": { + "methods": { + "ExportAssets": { + "requestType": "ExportAssetsRequest", + "responseType": "google.longrunning.Operation", + "options": { + "(google.api.http).post": "/v1beta1/{parent=projects/*}:exportAssets", + "(google.api.http).body": "*", + "(google.api.http).additional_bindings.post": "/v1beta1/{parent=organizations/*}:exportAssets", + "(google.api.http).additional_bindings.body": "*" + } + }, + "BatchGetAssetsHistory": { + "requestType": "BatchGetAssetsHistoryRequest", + "responseType": "BatchGetAssetsHistoryResponse", + "options": { + "(google.api.http).get": "/v1beta1/{parent=projects/*}:batchGetAssetsHistory", + "(google.api.http).additional_bindings.get": "/v1beta1/{parent=organizations/*}:batchGetAssetsHistory" + } + } + } + }, + "ExportAssetsRequest": { + "fields": { + "parent": { + "type": "string", + "id": 1 + }, + "readTime": { + "type": "google.protobuf.Timestamp", + "id": 2 + }, + "assetTypes": { "rule": "repeated", "type": "string", - "id": 6 + "id": 3 + }, + "contentType": { + "type": "ContentType", + "id": 4 + }, + "outputConfig": { + "type": "OutputConfig", + "id": 5 + } + } + }, + "ExportAssetsResponse": { + "fields": { + "readTime": { + "type": "google.protobuf.Timestamp", + "id": 1 + }, + "outputConfig": { + "type": "OutputConfig", + "id": 2 + } + } + }, + "BatchGetAssetsHistoryRequest": { + "fields": { + "parent": { + "type": "string", + "id": 1 + }, + "assetNames": { + "rule": "repeated", + "type": "string", + "id": 2 + }, + "contentType": { + "type": "ContentType", + "id": 3 + }, + "readTimeWindow": { + "type": "TimeWindow", + "id": 4 + } + } + }, + "BatchGetAssetsHistoryResponse": { + "fields": { + "assets": { + "rule": "repeated", + "type": "TemporalAsset", + "id": 1 + } + } + }, + "OutputConfig": { + "oneofs": { + "destination": { + "oneof": [ + "gcsDestination" + ] + } + }, + "fields": { + "gcsDestination": { + "type": "GcsDestination", + "id": 1 + } + } + }, + "GcsDestination": { + "oneofs": { + "objectUri": { + "oneof": [ + "uri" + ] + } + }, + "fields": { + "uri": { + "type": "string", + "id": 1 + } + } + }, + "ContentType": { + "values": { + "CONTENT_TYPE_UNSPECIFIED": 0, + "RESOURCE": 1, + "IAM_POLICY": 2 + } + }, + "TemporalAsset": { + "fields": { + "window": { + "type": "TimeWindow", + "id": 1 + }, + "deleted": { + "type": "bool", + "id": 2 + }, + "asset": { + "type": "Asset", + "id": 3 + } + } + }, + "TimeWindow": { + "fields": { + "startTime": { + "type": "google.protobuf.Timestamp", + "id": 1 + }, + "endTime": { + "type": "google.protobuf.Timestamp", + "id": 2 + } + } + }, + "Asset": { + "fields": { + "name": { + "type": "string", + "id": 1 + }, + "assetType": { + "type": "string", + "id": 2 + }, + "resource": { + "type": "Resource", + "id": 3 + }, + "iamPolicy": { + "type": "google.iam.v1.Policy", + "id": 4 } } }, @@ -99,13 +426,22 @@ "data": { "type": "google.protobuf.Struct", "id": 6 - }, - "internalData": { - "type": "google.protobuf.Any", - "id": 7 } } - }, + } + } + }, + "v1p2beta1": { + "options": { + "csharp_namespace": "Google.Cloud.Asset.v1p2beta1", + "go_package": "google.golang.org/genproto/googleapis/cloud/asset/v1p2beta1;asset", + "java_multiple_files": true, + "java_outer_classname": "AssetProto", + "java_package": "com.google.cloud.asset.v1p2beta1", + "php_namespace": "Google\\Cloud\\Asset\\v1p2beta1", + "cc_enable_arenas": true + }, + "nested": { "AssetService": { "options": { "(google.api.default_host)": "cloudasset.googleapis.com", @@ -408,6 +744,95 @@ "id": 5 } } + }, + "TemporalAsset": { + "fields": { + "window": { + "type": "TimeWindow", + "id": 1 + }, + "deleted": { + "type": "bool", + "id": 2 + }, + "asset": { + "type": "Asset", + "id": 3 + } + } + }, + "TimeWindow": { + "fields": { + "startTime": { + "type": "google.protobuf.Timestamp", + "id": 1 + }, + "endTime": { + "type": "google.protobuf.Timestamp", + "id": 2 + } + } + }, + "Asset": { + "fields": { + "name": { + "type": "string", + "id": 1 + }, + "assetType": { + "type": "string", + "id": 2 + }, + "resource": { + "type": "Resource", + "id": 3 + }, + "iamPolicy": { + "type": "google.iam.v1.Policy", + "id": 4 + }, + "iamPolicyName": { + "type": "bytes", + "id": 5 + }, + "ancestors": { + "rule": "repeated", + "type": "string", + "id": 6 + } + } + }, + "Resource": { + "fields": { + "version": { + "type": "string", + "id": 1 + }, + "discoveryDocumentUri": { + "type": "string", + "id": 2 + }, + "discoveryName": { + "type": "string", + "id": 3 + }, + "resourceUrl": { + "type": "string", + "id": 4 + }, + "parent": { + "type": "string", + "id": 5 + }, + "data": { + "type": "google.protobuf.Struct", + "id": 6 + }, + "internalData": { + "type": "google.protobuf.Any", + "id": 7 + } + } } } } diff --git a/packages/google-cloud-asset/src/browser.js b/packages/google-cloud-asset/src/browser.js new file mode 100644 index 00000000000..ddbcd7ecb9a --- /dev/null +++ b/packages/google-cloud-asset/src/browser.js @@ -0,0 +1,21 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +// Set a flag that we are running in a browser bundle. +global.isBrowser = true; + +// Re-export all exports from ./index.js. +module.exports = require('./index'); diff --git a/packages/google-cloud-asset/src/service_proto_list.json b/packages/google-cloud-asset/src/service_proto_list.json deleted file mode 100644 index 797307b2f66..00000000000 --- a/packages/google-cloud-asset/src/service_proto_list.json +++ /dev/null @@ -1 +0,0 @@ -["../protos/google/cloud/asset/v1p2beta1/assets.proto", "../protos/google/cloud/asset/v1p2beta1/asset_service.proto"] \ No newline at end of file diff --git a/packages/google-cloud-asset/src/v1/asset_service_client.js b/packages/google-cloud-asset/src/v1/asset_service_client.js index f35ddd1ea1b..552ea2f983a 100644 --- a/packages/google-cloud-asset/src/v1/asset_service_client.js +++ b/packages/google-cloud-asset/src/v1/asset_service_client.js @@ -17,7 +17,6 @@ const gapicConfig = require('./asset_service_client_config.json'); const gax = require('google-gax'); const path = require('path'); -const protobuf = require('protobufjs'); const VERSION = require('../../package.json').version; @@ -59,6 +58,16 @@ class AssetServiceClient { opts = opts || {}; this._descriptors = {}; + if (global.isBrowser) { + // If we're in browser, we use gRPC fallback. + opts.fallback = true; + } + + // If we are in browser, we are already using fallback because of the + // "browser" field in package.json. + // But if we were explicitly requested to use fallback, let's do it now. + const gaxModule = !global.isBrowser && opts.fallback ? gax.fallback : gax; + const servicePath = opts.servicePath || opts.apiEndpoint || this.constructor.servicePath; @@ -75,50 +84,58 @@ class AssetServiceClient { // Create a `gaxGrpc` object, with any grpc-specific options // sent to the client. opts.scopes = this.constructor.scopes; - const gaxGrpc = new gax.GrpcClient(opts); + const gaxGrpc = new gaxModule.GrpcClient(opts); // Save the auth object to the client, for use by other methods. this.auth = gaxGrpc.auth; // Determine the client header string. - const clientHeader = [ - `gl-node/${process.versions.node}`, - `grpc/${gaxGrpc.grpcVersion}`, - `gax/${gax.version}`, - `gapic/${VERSION}`, - ]; + const clientHeader = []; + + if (typeof process !== 'undefined' && 'versions' in process) { + clientHeader.push(`gl-node/${process.versions.node}`); + } + clientHeader.push(`gax/${gaxModule.version}`); + if (opts.fallback) { + clientHeader.push(`gl-web/${gaxModule.version}`); + } else { + clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`); + } + clientHeader.push(`gapic/${VERSION}`); if (opts.libName && opts.libVersion) { clientHeader.push(`${opts.libName}/${opts.libVersion}`); } // Load the applicable protos. + // For Node.js, pass the path to JSON proto file. + // For browsers, pass the JSON content. + + const nodejsProtoPath = path.join( + __dirname, + '..', + '..', + 'protos', + 'protos.json' + ); const protos = gaxGrpc.loadProto( - path.join(__dirname, '..', '..', 'protos'), - ['google/cloud/asset/v1/asset_service.proto'] + opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath ); // This API contains "path templates"; forward-slash-separated // identifiers to uniquely identify resources within the API. // Create useful helper objects for these. this._pathTemplates = { - projectPathTemplate: new gax.PathTemplate('projects/{project}'), + projectPathTemplate: new gaxModule.PathTemplate('projects/{project}'), }; - let protoFilesRoot = new gax.GoogleProtoFilesRoot(); - protoFilesRoot = protobuf.loadSync( - path.join( - __dirname, - '..', - '..', - 'protos', - 'google/cloud/asset/v1/asset_service.proto' - ), - protoFilesRoot - ); + + const protoFilesRoot = opts.fallback + ? gaxModule.protobuf.Root.fromJSON(require('../../protos/protos.json')) + : gaxModule.protobuf.loadSync(nodejsProtoPath); // This API contains "long-running operations", which return a // an Operation object that allows for tracking of the operation, // rather than holding a request open. - this.operationsClient = new gax.lro({ + this.operationsClient = new gaxModule.lro({ auth: gaxGrpc.auth, grpc: gaxGrpc.grpc, }).operationsClient(opts); @@ -131,7 +148,7 @@ class AssetServiceClient { ); this._descriptors.longrunning = { - exportAssets: new gax.LongrunningDescriptor( + exportAssets: new gaxModule.LongrunningDescriptor( this.operationsClient, exportAssetsResponse.decode.bind(exportAssetsResponse), exportAssetsMetadata.decode.bind(exportAssetsMetadata) @@ -154,7 +171,9 @@ class AssetServiceClient { // Put together the "service stub" for // google.cloud.asset.v1.AssetService. const assetServiceStub = gaxGrpc.createStub( - protos.google.cloud.asset.v1.AssetService, + opts.fallback + ? protos.lookupService('google.cloud.asset.v1.AssetService') + : protos.google.cloud.asset.v1.AssetService, opts ); @@ -162,18 +181,16 @@ class AssetServiceClient { // and create an API call method for each. const assetServiceStubMethods = ['exportAssets', 'batchGetAssetsHistory']; for (const methodName of assetServiceStubMethods) { - this._innerApiCalls[methodName] = gax.createApiCall( - assetServiceStub.then( - stub => - function() { - const args = Array.prototype.slice.call(arguments, 0); - return stub[methodName].apply(stub, args); - }, - err => - function() { - throw err; - } - ), + const innerCallPromise = assetServiceStub.then( + stub => (...args) => { + return stub[methodName].apply(stub, args); + }, + err => () => { + throw err; + } + ); + this._innerApiCalls[methodName] = gaxModule.createApiCall( + innerCallPromise, defaults[methodName], this._descriptors.longrunning[methodName] ); diff --git a/packages/google-cloud-asset/src/v1/asset_service_proto_list.json b/packages/google-cloud-asset/src/v1/asset_service_proto_list.json new file mode 100644 index 00000000000..53ef75b9756 --- /dev/null +++ b/packages/google-cloud-asset/src/v1/asset_service_proto_list.json @@ -0,0 +1,3 @@ +[ + "../../protos/google/cloud/asset/v1/asset_service.proto" +] diff --git a/packages/google-cloud-asset/src/v1beta1/asset_service_client.js b/packages/google-cloud-asset/src/v1beta1/asset_service_client.js index a767eaab383..41da17c3fdb 100644 --- a/packages/google-cloud-asset/src/v1beta1/asset_service_client.js +++ b/packages/google-cloud-asset/src/v1beta1/asset_service_client.js @@ -17,7 +17,6 @@ const gapicConfig = require('./asset_service_client_config.json'); const gax = require('google-gax'); const path = require('path'); -const protobuf = require('protobufjs'); const VERSION = require('../../package.json').version; @@ -59,6 +58,16 @@ class AssetServiceClient { opts = opts || {}; this._descriptors = {}; + if (global.isBrowser) { + // If we're in browser, we use gRPC fallback. + opts.fallback = true; + } + + // If we are in browser, we are already using fallback because of the + // "browser" field in package.json. + // But if we were explicitly requested to use fallback, let's do it now. + const gaxModule = !global.isBrowser && opts.fallback ? gax.fallback : gax; + const servicePath = opts.servicePath || opts.apiEndpoint || this.constructor.servicePath; @@ -75,50 +84,58 @@ class AssetServiceClient { // Create a `gaxGrpc` object, with any grpc-specific options // sent to the client. opts.scopes = this.constructor.scopes; - const gaxGrpc = new gax.GrpcClient(opts); + const gaxGrpc = new gaxModule.GrpcClient(opts); // Save the auth object to the client, for use by other methods. this.auth = gaxGrpc.auth; // Determine the client header string. - const clientHeader = [ - `gl-node/${process.versions.node}`, - `grpc/${gaxGrpc.grpcVersion}`, - `gax/${gax.version}`, - `gapic/${VERSION}`, - ]; + const clientHeader = []; + + if (typeof process !== 'undefined' && 'versions' in process) { + clientHeader.push(`gl-node/${process.versions.node}`); + } + clientHeader.push(`gax/${gaxModule.version}`); + if (opts.fallback) { + clientHeader.push(`gl-web/${gaxModule.version}`); + } else { + clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`); + } + clientHeader.push(`gapic/${VERSION}`); if (opts.libName && opts.libVersion) { clientHeader.push(`${opts.libName}/${opts.libVersion}`); } // Load the applicable protos. + // For Node.js, pass the path to JSON proto file. + // For browsers, pass the JSON content. + + const nodejsProtoPath = path.join( + __dirname, + '..', + '..', + 'protos', + 'protos.json' + ); const protos = gaxGrpc.loadProto( - path.join(__dirname, '..', '..', 'protos'), - ['google/cloud/asset/v1beta1/asset_service.proto'] + opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath ); // This API contains "path templates"; forward-slash-separated // identifiers to uniquely identify resources within the API. // Create useful helper objects for these. this._pathTemplates = { - projectPathTemplate: new gax.PathTemplate('projects/{project}'), + projectPathTemplate: new gaxModule.PathTemplate('projects/{project}'), }; - let protoFilesRoot = new gax.GoogleProtoFilesRoot(); - protoFilesRoot = protobuf.loadSync( - path.join( - __dirname, - '..', - '..', - 'protos', - 'google/cloud/asset/v1beta1/asset_service.proto' - ), - protoFilesRoot - ); + + const protoFilesRoot = opts.fallback + ? gaxModule.protobuf.Root.fromJSON(require('../../protos/protos.json')) + : gaxModule.protobuf.loadSync(nodejsProtoPath); // This API contains "long-running operations", which return a // an Operation object that allows for tracking of the operation, // rather than holding a request open. - this.operationsClient = new gax.lro({ + this.operationsClient = new gaxModule.lro({ auth: gaxGrpc.auth, grpc: gaxGrpc.grpc, }).operationsClient(opts); @@ -131,7 +148,7 @@ class AssetServiceClient { ); this._descriptors.longrunning = { - exportAssets: new gax.LongrunningDescriptor( + exportAssets: new gaxModule.LongrunningDescriptor( this.operationsClient, exportAssetsResponse.decode.bind(exportAssetsResponse), exportAssetsMetadata.decode.bind(exportAssetsMetadata) @@ -154,7 +171,9 @@ class AssetServiceClient { // Put together the "service stub" for // google.cloud.asset.v1beta1.AssetService. const assetServiceStub = gaxGrpc.createStub( - protos.google.cloud.asset.v1beta1.AssetService, + opts.fallback + ? protos.lookupService('google.cloud.asset.v1beta1.AssetService') + : protos.google.cloud.asset.v1beta1.AssetService, opts ); @@ -162,18 +181,16 @@ class AssetServiceClient { // and create an API call method for each. const assetServiceStubMethods = ['exportAssets', 'batchGetAssetsHistory']; for (const methodName of assetServiceStubMethods) { - this._innerApiCalls[methodName] = gax.createApiCall( - assetServiceStub.then( - stub => - function() { - const args = Array.prototype.slice.call(arguments, 0); - return stub[methodName].apply(stub, args); - }, - err => - function() { - throw err; - } - ), + const innerCallPromise = assetServiceStub.then( + stub => (...args) => { + return stub[methodName].apply(stub, args); + }, + err => () => { + throw err; + } + ); + this._innerApiCalls[methodName] = gaxModule.createApiCall( + innerCallPromise, defaults[methodName], this._descriptors.longrunning[methodName] ); diff --git a/packages/google-cloud-asset/src/v1beta1/asset_service_proto_list.json b/packages/google-cloud-asset/src/v1beta1/asset_service_proto_list.json new file mode 100644 index 00000000000..499bd902e39 --- /dev/null +++ b/packages/google-cloud-asset/src/v1beta1/asset_service_proto_list.json @@ -0,0 +1,3 @@ +[ + "../../protos/google/cloud/asset/v1beta1/asset_service.proto" +] diff --git a/packages/google-cloud-asset/src/v1p2beta1/asset_service_client.js b/packages/google-cloud-asset/src/v1p2beta1/asset_service_client.js index 4c538083e40..1830f9aabe7 100644 --- a/packages/google-cloud-asset/src/v1p2beta1/asset_service_client.js +++ b/packages/google-cloud-asset/src/v1p2beta1/asset_service_client.js @@ -17,7 +17,6 @@ const gapicConfig = require('./asset_service_client_config.json'); const gax = require('google-gax'); const path = require('path'); -const protobuf = require('protobufjs'); const VERSION = require('../../package.json').version; @@ -59,6 +58,16 @@ class AssetServiceClient { opts = opts || {}; this._descriptors = {}; + if (global.isBrowser) { + // If we're in browser, we use gRPC fallback. + opts.fallback = true; + } + + // If we are in browser, we are already using fallback because of the + // "browser" field in package.json. + // But if we were explicitly requested to use fallback, let's do it now. + const gaxModule = !global.isBrowser && opts.fallback ? gax.fallback : gax; + const servicePath = opts.servicePath || opts.apiEndpoint || this.constructor.servicePath; @@ -75,50 +84,60 @@ class AssetServiceClient { // Create a `gaxGrpc` object, with any grpc-specific options // sent to the client. opts.scopes = this.constructor.scopes; - const gaxGrpc = new gax.GrpcClient(opts); + const gaxGrpc = new gaxModule.GrpcClient(opts); // Save the auth object to the client, for use by other methods. this.auth = gaxGrpc.auth; // Determine the client header string. - const clientHeader = [ - `gl-node/${process.versions.node}`, - `grpc/${gaxGrpc.grpcVersion}`, - `gax/${gax.version}`, - `gapic/${VERSION}`, - ]; + const clientHeader = []; + + if (typeof process !== 'undefined' && 'versions' in process) { + clientHeader.push(`gl-node/${process.versions.node}`); + } + clientHeader.push(`gax/${gaxModule.version}`); + if (opts.fallback) { + clientHeader.push(`gl-web/${gaxModule.version}`); + } else { + clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`); + } + clientHeader.push(`gapic/${VERSION}`); if (opts.libName && opts.libVersion) { clientHeader.push(`${opts.libName}/${opts.libVersion}`); } // Load the applicable protos. + // For Node.js, pass the path to JSON proto file. + // For browsers, pass the JSON content. + + const nodejsProtoPath = path.join( + __dirname, + '..', + '..', + 'protos', + 'protos.json' + ); const protos = gaxGrpc.loadProto( - path.join(__dirname, '..', '..', 'protos'), - ['google/cloud/asset/v1p2beta1/asset_service.proto'] + opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath ); // This API contains "path templates"; forward-slash-separated // identifiers to uniquely identify resources within the API. // Create useful helper objects for these. this._pathTemplates = { - feedPathTemplate: new gax.PathTemplate('projects/{project}/feeds/{feed}'), - }; - let protoFilesRoot = new gax.GoogleProtoFilesRoot(); - protoFilesRoot = protobuf.loadSync( - path.join( - __dirname, - '..', - '..', - 'protos', - 'google/cloud/asset/v1p2beta1/asset_service.proto' + feedPathTemplate: new gaxModule.PathTemplate( + 'projects/{project}/feeds/{feed}' ), - protoFilesRoot - ); + }; + + const protoFilesRoot = opts.fallback + ? gaxModule.protobuf.Root.fromJSON(require('../../protos/protos.json')) + : gaxModule.protobuf.loadSync(nodejsProtoPath); // This API contains "long-running operations", which return a // an Operation object that allows for tracking of the operation, // rather than holding a request open. - this.operationsClient = new gax.lro({ + this.operationsClient = new gaxModule.lro({ auth: gaxGrpc.auth, grpc: gaxGrpc.grpc, }).operationsClient(opts); @@ -131,7 +150,7 @@ class AssetServiceClient { ); this._descriptors.longrunning = { - exportAssets: new gax.LongrunningDescriptor( + exportAssets: new gaxModule.LongrunningDescriptor( this.operationsClient, exportAssetsResponse.decode.bind(exportAssetsResponse), exportAssetsMetadata.decode.bind(exportAssetsMetadata) @@ -154,7 +173,9 @@ class AssetServiceClient { // Put together the "service stub" for // google.cloud.asset.v1p2beta1.AssetService. const assetServiceStub = gaxGrpc.createStub( - protos.google.cloud.asset.v1p2beta1.AssetService, + opts.fallback + ? protos.lookupService('google.cloud.asset.v1p2beta1.AssetService') + : protos.google.cloud.asset.v1p2beta1.AssetService, opts ); @@ -170,18 +191,16 @@ class AssetServiceClient { 'deleteFeed', ]; for (const methodName of assetServiceStubMethods) { - this._innerApiCalls[methodName] = gax.createApiCall( - assetServiceStub.then( - stub => - function() { - const args = Array.prototype.slice.call(arguments, 0); - return stub[methodName].apply(stub, args); - }, - err => - function() { - throw err; - } - ), + const innerCallPromise = assetServiceStub.then( + stub => (...args) => { + return stub[methodName].apply(stub, args); + }, + err => () => { + throw err; + } + ); + this._innerApiCalls[methodName] = gaxModule.createApiCall( + innerCallPromise, defaults[methodName], this._descriptors.longrunning[methodName] ); diff --git a/packages/google-cloud-asset/src/v1p2beta1/asset_service_proto_list.json b/packages/google-cloud-asset/src/v1p2beta1/asset_service_proto_list.json new file mode 100644 index 00000000000..ac74955ea29 --- /dev/null +++ b/packages/google-cloud-asset/src/v1p2beta1/asset_service_proto_list.json @@ -0,0 +1,3 @@ +[ + "../../protos/google/cloud/asset/v1p2beta1/asset_service.proto" +] diff --git a/packages/google-cloud-asset/synth.metadata b/packages/google-cloud-asset/synth.metadata index 86f396990a2..f259606460d 100644 --- a/packages/google-cloud-asset/synth.metadata +++ b/packages/google-cloud-asset/synth.metadata @@ -1,19 +1,19 @@ { - "updateTime": "2019-08-28T11:07:54.519824Z", + "updateTime": "2019-08-31T11:07:20.415455Z", "sources": [ { "generator": { "name": "artman", - "version": "0.35.1", - "dockerImage": "googleapis/artman@sha256:b11c7ea0d0831c54016fb50f4b796d24d1971439b30fbc32a369ba1ac887c384" + "version": "0.36.1", + "dockerImage": "googleapis/artman@sha256:7c20f006c7a62d9d782e2665647d52290c37a952ef3cd134624d5dd62b3f71bd" } }, { "git": { "name": "googleapis", "remote": "https://github.com/googleapis/googleapis.git", - "sha": "dbd38035c35083507e2f0b839985cf17e212cb1c", - "internalRef": "265796259" + "sha": "82809578652607c8ee29d9e199c21f28f81a03e0", + "internalRef": "266247326" } }, { diff --git a/packages/google-cloud-asset/synth.py b/packages/google-cloud-asset/synth.py index 7058295330a..6f78852b9c7 100644 --- a/packages/google-cloud-asset/synth.py +++ b/packages/google-cloud-asset/synth.py @@ -55,3 +55,4 @@ # Node.js specific cleanup subprocess.run(['npm', 'install']) subprocess.run(['npm', 'run', 'fix']) +subprocess.run(['npx', 'compileProtos', 'src']) diff --git a/packages/google-cloud-asset/test/gapic-v1.js b/packages/google-cloud-asset/test/gapic-v1.js index 1fe67016ecb..7e8e6d0dd8c 100644 --- a/packages/google-cloud-asset/test/gapic-v1.js +++ b/packages/google-cloud-asset/test/gapic-v1.js @@ -44,6 +44,11 @@ describe('AssetServiceClient', () => { assert(client); }); + it('should create a client with gRPC fallback', () => { + const client = new assetModule.v1.AssetServiceClient({fallback: true}); + assert(client); + }); + describe('exportAssets', function() { it('invokes exportAssets without error', done => { const client = new assetModule.v1.AssetServiceClient({ diff --git a/packages/google-cloud-asset/test/gapic-v1beta1.js b/packages/google-cloud-asset/test/gapic-v1beta1.js index c3a67c17c66..c30f666dcef 100644 --- a/packages/google-cloud-asset/test/gapic-v1beta1.js +++ b/packages/google-cloud-asset/test/gapic-v1beta1.js @@ -44,6 +44,11 @@ describe('AssetServiceClient', () => { assert(client); }); + it('should create a client with gRPC fallback', () => { + const client = new assetModule.v1beta1.AssetServiceClient({fallback: true}); + assert(client); + }); + describe('exportAssets', function() { it('invokes exportAssets without error', done => { const client = new assetModule.v1beta1.AssetServiceClient({ diff --git a/packages/google-cloud-asset/test/gapic-v1p2beta1.js b/packages/google-cloud-asset/test/gapic-v1p2beta1.js index 25e297cd4b6..c82d95e591c 100644 --- a/packages/google-cloud-asset/test/gapic-v1p2beta1.js +++ b/packages/google-cloud-asset/test/gapic-v1p2beta1.js @@ -44,6 +44,13 @@ describe('AssetServiceClient', () => { assert(client); }); + it('should create a client with gRPC fallback', () => { + const client = new assetModule.v1p2beta1.AssetServiceClient({ + fallback: true, + }); + assert(client); + }); + describe('exportAssets', function() { it('invokes exportAssets without error', done => { const client = new assetModule.v1p2beta1.AssetServiceClient({ diff --git a/packages/google-cloud-asset/webpack.config.js b/packages/google-cloud-asset/webpack.config.js new file mode 100644 index 00000000000..12c1d1b4116 --- /dev/null +++ b/packages/google-cloud-asset/webpack.config.js @@ -0,0 +1,46 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +module.exports = { + entry: './src/browser.js', + output: { + library: 'asset', + filename: './asset.js', + }, + node: { + child_process: 'empty', + fs: 'empty', + crypto: 'empty', + }, + resolve: { + extensions: ['.js', '.json'], + }, + module: { + rules: [ + { + test: /node_modules[\\/]retry-request[\\/]/, + use: 'null-loader', + }, + { + test: /node_modules[\\/]https-proxy-agent[\\/]/, + use: 'null-loader', + }, + { + test: /node_modules[\\/]gtoken[\\/]/, + use: 'null-loader', + }, + ], + }, + mode: 'production', +};