Skip to content

Commit 7bac0b1

Browse files
yoshi-automationJustinBeckwith
authored andcommitted
feat: load protos from JSON, grpc-fallback support (#330)
1 parent b4af7b4 commit 7bac0b1

File tree

6 files changed

+146
-42
lines changed

6 files changed

+146
-42
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// Set a flag that we are running in a browser bundle.
18+
global.isBrowser = true;
19+
20+
// Re-export all exports from ./index.js.
21+
module.exports = require('./index');

packages/google-privacy-dlp/src/v2/dlp_service_client.js

+66-37
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ class DlpServiceClient {
6666
opts = opts || {};
6767
this._descriptors = {};
6868

69+
if (global.isBrowser) {
70+
// If we're in browser, we use gRPC fallback.
71+
opts.fallback = true;
72+
}
73+
74+
// If we are in browser, we are already using fallback because of the
75+
// "browser" field in package.json.
76+
// But if we were explicitly requested to use fallback, let's do it now.
77+
const gaxModule = !global.isBrowser && opts.fallback ? gax.fallback : gax;
78+
6979
const servicePath =
7080
opts.servicePath || opts.apiEndpoint || this.constructor.servicePath;
7181

@@ -82,58 +92,73 @@ class DlpServiceClient {
8292
// Create a `gaxGrpc` object, with any grpc-specific options
8393
// sent to the client.
8494
opts.scopes = this.constructor.scopes;
85-
const gaxGrpc = new gax.GrpcClient(opts);
95+
const gaxGrpc = new gaxModule.GrpcClient(opts);
8696

8797
// Save the auth object to the client, for use by other methods.
8898
this.auth = gaxGrpc.auth;
8999

90100
// Determine the client header string.
91-
const clientHeader = [
92-
`gl-node/${process.versions.node}`,
93-
`grpc/${gaxGrpc.grpcVersion}`,
94-
`gax/${gax.version}`,
95-
`gapic/${VERSION}`,
96-
];
101+
const clientHeader = [];
102+
103+
if (typeof process !== 'undefined' && 'versions' in process) {
104+
clientHeader.push(`gl-node/${process.versions.node}`);
105+
}
106+
clientHeader.push(`gax/${gaxModule.version}`);
107+
if (opts.fallback) {
108+
clientHeader.push(`gl-web/${gaxModule.version}`);
109+
} else {
110+
clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`);
111+
}
112+
clientHeader.push(`gapic/${VERSION}`);
97113
if (opts.libName && opts.libVersion) {
98114
clientHeader.push(`${opts.libName}/${opts.libVersion}`);
99115
}
100116

101117
// Load the applicable protos.
118+
// For Node.js, pass the path to JSON proto file.
119+
// For browsers, pass the JSON content.
120+
121+
const nodejsProtoPath = path.join(
122+
__dirname,
123+
'..',
124+
'..',
125+
'protos',
126+
'protos.json'
127+
);
102128
const protos = gaxGrpc.loadProto(
103-
path.join(__dirname, '..', '..', 'protos'),
104-
['google/privacy/dlp/v2/dlp.proto']
129+
opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath
105130
);
106131

107132
// This API contains "path templates"; forward-slash-separated
108133
// identifiers to uniquely identify resources within the API.
109134
// Create useful helper objects for these.
110135
this._pathTemplates = {
111-
dlpJobPathTemplate: new gax.PathTemplate(
136+
dlpJobPathTemplate: new gaxModule.PathTemplate(
112137
'projects/{project}/dlpJobs/{dlp_job}'
113138
),
114-
organizationPathTemplate: new gax.PathTemplate(
139+
organizationPathTemplate: new gaxModule.PathTemplate(
115140
'organizations/{organization}'
116141
),
117-
organizationDeidentifyTemplatePathTemplate: new gax.PathTemplate(
142+
organizationDeidentifyTemplatePathTemplate: new gaxModule.PathTemplate(
118143
'organizations/{organization}/deidentifyTemplates/{deidentify_template}'
119144
),
120-
organizationInspectTemplatePathTemplate: new gax.PathTemplate(
145+
organizationInspectTemplatePathTemplate: new gaxModule.PathTemplate(
121146
'organizations/{organization}/inspectTemplates/{inspect_template}'
122147
),
123-
organizationStoredInfoTypePathTemplate: new gax.PathTemplate(
148+
organizationStoredInfoTypePathTemplate: new gaxModule.PathTemplate(
124149
'organizations/{organization}/storedInfoTypes/{stored_info_type}'
125150
),
126-
projectPathTemplate: new gax.PathTemplate('projects/{project}'),
127-
projectDeidentifyTemplatePathTemplate: new gax.PathTemplate(
151+
projectPathTemplate: new gaxModule.PathTemplate('projects/{project}'),
152+
projectDeidentifyTemplatePathTemplate: new gaxModule.PathTemplate(
128153
'projects/{project}/deidentifyTemplates/{deidentify_template}'
129154
),
130-
projectInspectTemplatePathTemplate: new gax.PathTemplate(
155+
projectInspectTemplatePathTemplate: new gaxModule.PathTemplate(
131156
'projects/{project}/inspectTemplates/{inspect_template}'
132157
),
133-
projectJobTriggerPathTemplate: new gax.PathTemplate(
158+
projectJobTriggerPathTemplate: new gaxModule.PathTemplate(
134159
'projects/{project}/jobTriggers/{job_trigger}'
135160
),
136-
projectStoredInfoTypePathTemplate: new gax.PathTemplate(
161+
projectStoredInfoTypePathTemplate: new gaxModule.PathTemplate(
137162
'projects/{project}/storedInfoTypes/{stored_info_type}'
138163
),
139164
};
@@ -142,23 +167,27 @@ class DlpServiceClient {
142167
// (e.g. 50 results at a time, with tokens to get subsequent
143168
// pages). Denote the keys used for pagination and results.
144169
this._descriptors.page = {
145-
listInspectTemplates: new gax.PageDescriptor(
170+
listInspectTemplates: new gaxModule.PageDescriptor(
146171
'pageToken',
147172
'nextPageToken',
148173
'inspectTemplates'
149174
),
150-
listDeidentifyTemplates: new gax.PageDescriptor(
175+
listDeidentifyTemplates: new gaxModule.PageDescriptor(
151176
'pageToken',
152177
'nextPageToken',
153178
'deidentifyTemplates'
154179
),
155-
listDlpJobs: new gax.PageDescriptor('pageToken', 'nextPageToken', 'jobs'),
156-
listJobTriggers: new gax.PageDescriptor(
180+
listDlpJobs: new gaxModule.PageDescriptor(
181+
'pageToken',
182+
'nextPageToken',
183+
'jobs'
184+
),
185+
listJobTriggers: new gaxModule.PageDescriptor(
157186
'pageToken',
158187
'nextPageToken',
159188
'jobTriggers'
160189
),
161-
listStoredInfoTypes: new gax.PageDescriptor(
190+
listStoredInfoTypes: new gaxModule.PageDescriptor(
162191
'pageToken',
163192
'nextPageToken',
164193
'storedInfoTypes'
@@ -181,7 +210,9 @@ class DlpServiceClient {
181210
// Put together the "service stub" for
182211
// google.privacy.dlp.v2.DlpService.
183212
const dlpServiceStub = gaxGrpc.createStub(
184-
protos.google.privacy.dlp.v2.DlpService,
213+
opts.fallback
214+
? protos.lookupService('google.privacy.dlp.v2.DlpService')
215+
: protos.google.privacy.dlp.v2.DlpService,
185216
opts
186217
);
187218

@@ -220,18 +251,16 @@ class DlpServiceClient {
220251
'deleteStoredInfoType',
221252
];
222253
for (const methodName of dlpServiceStubMethods) {
223-
this._innerApiCalls[methodName] = gax.createApiCall(
224-
dlpServiceStub.then(
225-
stub =>
226-
function() {
227-
const args = Array.prototype.slice.call(arguments, 0);
228-
return stub[methodName].apply(stub, args);
229-
},
230-
err =>
231-
function() {
232-
throw err;
233-
}
234-
),
254+
const innerCallPromise = dlpServiceStub.then(
255+
stub => (...args) => {
256+
return stub[methodName].apply(stub, args);
257+
},
258+
err => () => {
259+
throw err;
260+
}
261+
);
262+
this._innerApiCalls[methodName] = gaxModule.createApiCall(
263+
innerCallPromise,
235264
defaults[methodName],
236265
this._descriptors.page[methodName]
237266
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"../../protos/google/privacy/dlp/v2/dlp.proto"
3+
]

packages/google-privacy-dlp/synth.metadata

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
2-
"updateTime": "2019-08-29T21:07:45.619702Z",
2+
"updateTime": "2019-09-04T11:14:33.690208Z",
33
"sources": [
44
{
55
"generator": {
66
"name": "artman",
7-
"version": "0.35.1",
8-
"dockerImage": "googleapis/artman@sha256:b11c7ea0d0831c54016fb50f4b796d24d1971439b30fbc32a369ba1ac887c384"
7+
"version": "0.36.2",
8+
"dockerImage": "googleapis/artman@sha256:0e6f3a668cd68afc768ecbe08817cf6e56a0e64fcbdb1c58c3b97492d12418a1"
99
}
1010
},
1111
{
1212
"git": {
1313
"name": "googleapis",
1414
"remote": "https://github.com/googleapis/googleapis.git",
15-
"sha": "e121a35579e73377f998c11bcc09ba0486736404",
16-
"internalRef": "265953539"
15+
"sha": "a2158681f6e30c5fd9446eb1fd7b5021a6d48bfa",
16+
"internalRef": "266999433"
1717
}
1818
},
1919
{

packages/google-privacy-dlp/test/gapic-v2.js

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ describe('DlpServiceClient', () => {
4444
assert(client);
4545
});
4646

47+
it('should create a client with gRPC fallback', () => {
48+
const client = new dlpModule.v2.DlpServiceClient({fallback: true});
49+
assert(client);
50+
});
51+
4752
describe('inspectContent', () => {
4853
it('invokes inspectContent without error', done => {
4954
const client = new dlpModule.v2.DlpServiceClient({
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
module.exports = {
16+
entry: './src/browser.js',
17+
output: {
18+
library: 'dlp',
19+
filename: './dlp.js',
20+
},
21+
node: {
22+
child_process: 'empty',
23+
fs: 'empty',
24+
crypto: 'empty',
25+
},
26+
resolve: {
27+
extensions: ['.js', '.json'],
28+
},
29+
module: {
30+
rules: [
31+
{
32+
test: /node_modules[\\/]retry-request[\\/]/,
33+
use: 'null-loader',
34+
},
35+
{
36+
test: /node_modules[\\/]https-proxy-agent[\\/]/,
37+
use: 'null-loader',
38+
},
39+
{
40+
test: /node_modules[\\/]gtoken[\\/]/,
41+
use: 'null-loader',
42+
},
43+
],
44+
},
45+
mode: 'production',
46+
};

0 commit comments

Comments
 (0)