diff --git a/packages/google-privacy-dlp/src/browser.js b/packages/google-privacy-dlp/src/browser.js new file mode 100644 index 00000000000..ddbcd7ecb9a --- /dev/null +++ b/packages/google-privacy-dlp/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-privacy-dlp/src/v2/dlp_service_client.js b/packages/google-privacy-dlp/src/v2/dlp_service_client.js index 2f32cd6e383..d36d42837ba 100644 --- a/packages/google-privacy-dlp/src/v2/dlp_service_client.js +++ b/packages/google-privacy-dlp/src/v2/dlp_service_client.js @@ -66,6 +66,16 @@ class DlpServiceClient { 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; @@ -82,58 +92,73 @@ class DlpServiceClient { // 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/privacy/dlp/v2/dlp.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 = { - dlpJobPathTemplate: new gax.PathTemplate( + dlpJobPathTemplate: new gaxModule.PathTemplate( 'projects/{project}/dlpJobs/{dlp_job}' ), - organizationPathTemplate: new gax.PathTemplate( + organizationPathTemplate: new gaxModule.PathTemplate( 'organizations/{organization}' ), - organizationDeidentifyTemplatePathTemplate: new gax.PathTemplate( + organizationDeidentifyTemplatePathTemplate: new gaxModule.PathTemplate( 'organizations/{organization}/deidentifyTemplates/{deidentify_template}' ), - organizationInspectTemplatePathTemplate: new gax.PathTemplate( + organizationInspectTemplatePathTemplate: new gaxModule.PathTemplate( 'organizations/{organization}/inspectTemplates/{inspect_template}' ), - organizationStoredInfoTypePathTemplate: new gax.PathTemplate( + organizationStoredInfoTypePathTemplate: new gaxModule.PathTemplate( 'organizations/{organization}/storedInfoTypes/{stored_info_type}' ), - projectPathTemplate: new gax.PathTemplate('projects/{project}'), - projectDeidentifyTemplatePathTemplate: new gax.PathTemplate( + projectPathTemplate: new gaxModule.PathTemplate('projects/{project}'), + projectDeidentifyTemplatePathTemplate: new gaxModule.PathTemplate( 'projects/{project}/deidentifyTemplates/{deidentify_template}' ), - projectInspectTemplatePathTemplate: new gax.PathTemplate( + projectInspectTemplatePathTemplate: new gaxModule.PathTemplate( 'projects/{project}/inspectTemplates/{inspect_template}' ), - projectJobTriggerPathTemplate: new gax.PathTemplate( + projectJobTriggerPathTemplate: new gaxModule.PathTemplate( 'projects/{project}/jobTriggers/{job_trigger}' ), - projectStoredInfoTypePathTemplate: new gax.PathTemplate( + projectStoredInfoTypePathTemplate: new gaxModule.PathTemplate( 'projects/{project}/storedInfoTypes/{stored_info_type}' ), }; @@ -142,23 +167,27 @@ class DlpServiceClient { // (e.g. 50 results at a time, with tokens to get subsequent // pages). Denote the keys used for pagination and results. this._descriptors.page = { - listInspectTemplates: new gax.PageDescriptor( + listInspectTemplates: new gaxModule.PageDescriptor( 'pageToken', 'nextPageToken', 'inspectTemplates' ), - listDeidentifyTemplates: new gax.PageDescriptor( + listDeidentifyTemplates: new gaxModule.PageDescriptor( 'pageToken', 'nextPageToken', 'deidentifyTemplates' ), - listDlpJobs: new gax.PageDescriptor('pageToken', 'nextPageToken', 'jobs'), - listJobTriggers: new gax.PageDescriptor( + listDlpJobs: new gaxModule.PageDescriptor( + 'pageToken', + 'nextPageToken', + 'jobs' + ), + listJobTriggers: new gaxModule.PageDescriptor( 'pageToken', 'nextPageToken', 'jobTriggers' ), - listStoredInfoTypes: new gax.PageDescriptor( + listStoredInfoTypes: new gaxModule.PageDescriptor( 'pageToken', 'nextPageToken', 'storedInfoTypes' @@ -181,7 +210,9 @@ class DlpServiceClient { // Put together the "service stub" for // google.privacy.dlp.v2.DlpService. const dlpServiceStub = gaxGrpc.createStub( - protos.google.privacy.dlp.v2.DlpService, + opts.fallback + ? protos.lookupService('google.privacy.dlp.v2.DlpService') + : protos.google.privacy.dlp.v2.DlpService, opts ); @@ -220,18 +251,16 @@ class DlpServiceClient { 'deleteStoredInfoType', ]; for (const methodName of dlpServiceStubMethods) { - this._innerApiCalls[methodName] = gax.createApiCall( - dlpServiceStub.then( - stub => - function() { - const args = Array.prototype.slice.call(arguments, 0); - return stub[methodName].apply(stub, args); - }, - err => - function() { - throw err; - } - ), + const innerCallPromise = dlpServiceStub.then( + stub => (...args) => { + return stub[methodName].apply(stub, args); + }, + err => () => { + throw err; + } + ); + this._innerApiCalls[methodName] = gaxModule.createApiCall( + innerCallPromise, defaults[methodName], this._descriptors.page[methodName] ); diff --git a/packages/google-privacy-dlp/src/v2/dlp_service_proto_list.json b/packages/google-privacy-dlp/src/v2/dlp_service_proto_list.json new file mode 100644 index 00000000000..a73c6ba5ae7 --- /dev/null +++ b/packages/google-privacy-dlp/src/v2/dlp_service_proto_list.json @@ -0,0 +1,3 @@ +[ + "../../protos/google/privacy/dlp/v2/dlp.proto" +] diff --git a/packages/google-privacy-dlp/synth.metadata b/packages/google-privacy-dlp/synth.metadata index 39f83c06bc0..534f451d5c6 100644 --- a/packages/google-privacy-dlp/synth.metadata +++ b/packages/google-privacy-dlp/synth.metadata @@ -1,19 +1,19 @@ { - "updateTime": "2019-08-29T21:07:45.619702Z", + "updateTime": "2019-09-04T11:14:33.690208Z", "sources": [ { "generator": { "name": "artman", - "version": "0.35.1", - "dockerImage": "googleapis/artman@sha256:b11c7ea0d0831c54016fb50f4b796d24d1971439b30fbc32a369ba1ac887c384" + "version": "0.36.2", + "dockerImage": "googleapis/artman@sha256:0e6f3a668cd68afc768ecbe08817cf6e56a0e64fcbdb1c58c3b97492d12418a1" } }, { "git": { "name": "googleapis", "remote": "https://github.com/googleapis/googleapis.git", - "sha": "e121a35579e73377f998c11bcc09ba0486736404", - "internalRef": "265953539" + "sha": "a2158681f6e30c5fd9446eb1fd7b5021a6d48bfa", + "internalRef": "266999433" } }, { diff --git a/packages/google-privacy-dlp/test/gapic-v2.js b/packages/google-privacy-dlp/test/gapic-v2.js index c0105e65cbe..df6836c3aa7 100644 --- a/packages/google-privacy-dlp/test/gapic-v2.js +++ b/packages/google-privacy-dlp/test/gapic-v2.js @@ -44,6 +44,11 @@ describe('DlpServiceClient', () => { assert(client); }); + it('should create a client with gRPC fallback', () => { + const client = new dlpModule.v2.DlpServiceClient({fallback: true}); + assert(client); + }); + describe('inspectContent', () => { it('invokes inspectContent without error', done => { const client = new dlpModule.v2.DlpServiceClient({ diff --git a/packages/google-privacy-dlp/webpack.config.js b/packages/google-privacy-dlp/webpack.config.js new file mode 100644 index 00000000000..5fa6d9aad6f --- /dev/null +++ b/packages/google-privacy-dlp/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: 'dlp', + filename: './dlp.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', +};