@@ -66,6 +66,16 @@ class DlpServiceClient {
66
66
opts = opts || { } ;
67
67
this . _descriptors = { } ;
68
68
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
+
69
79
const servicePath =
70
80
opts . servicePath || opts . apiEndpoint || this . constructor . servicePath ;
71
81
@@ -82,58 +92,73 @@ class DlpServiceClient {
82
92
// Create a `gaxGrpc` object, with any grpc-specific options
83
93
// sent to the client.
84
94
opts . scopes = this . constructor . scopes ;
85
- const gaxGrpc = new gax . GrpcClient ( opts ) ;
95
+ const gaxGrpc = new gaxModule . GrpcClient ( opts ) ;
86
96
87
97
// Save the auth object to the client, for use by other methods.
88
98
this . auth = gaxGrpc . auth ;
89
99
90
100
// 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 } ` ) ;
97
113
if ( opts . libName && opts . libVersion ) {
98
114
clientHeader . push ( `${ opts . libName } /${ opts . libVersion } ` ) ;
99
115
}
100
116
101
117
// 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
+ ) ;
102
128
const protos = gaxGrpc . loadProto (
103
- path . join ( __dirname , '..' , '..' , 'protos' ) ,
104
- [ 'google/privacy/dlp/v2/dlp.proto' ]
129
+ opts . fallback ? require ( '../../protos/protos.json' ) : nodejsProtoPath
105
130
) ;
106
131
107
132
// This API contains "path templates"; forward-slash-separated
108
133
// identifiers to uniquely identify resources within the API.
109
134
// Create useful helper objects for these.
110
135
this . _pathTemplates = {
111
- dlpJobPathTemplate : new gax . PathTemplate (
136
+ dlpJobPathTemplate : new gaxModule . PathTemplate (
112
137
'projects/{project}/dlpJobs/{dlp_job}'
113
138
) ,
114
- organizationPathTemplate : new gax . PathTemplate (
139
+ organizationPathTemplate : new gaxModule . PathTemplate (
115
140
'organizations/{organization}'
116
141
) ,
117
- organizationDeidentifyTemplatePathTemplate : new gax . PathTemplate (
142
+ organizationDeidentifyTemplatePathTemplate : new gaxModule . PathTemplate (
118
143
'organizations/{organization}/deidentifyTemplates/{deidentify_template}'
119
144
) ,
120
- organizationInspectTemplatePathTemplate : new gax . PathTemplate (
145
+ organizationInspectTemplatePathTemplate : new gaxModule . PathTemplate (
121
146
'organizations/{organization}/inspectTemplates/{inspect_template}'
122
147
) ,
123
- organizationStoredInfoTypePathTemplate : new gax . PathTemplate (
148
+ organizationStoredInfoTypePathTemplate : new gaxModule . PathTemplate (
124
149
'organizations/{organization}/storedInfoTypes/{stored_info_type}'
125
150
) ,
126
- projectPathTemplate : new gax . PathTemplate ( 'projects/{project}' ) ,
127
- projectDeidentifyTemplatePathTemplate : new gax . PathTemplate (
151
+ projectPathTemplate : new gaxModule . PathTemplate ( 'projects/{project}' ) ,
152
+ projectDeidentifyTemplatePathTemplate : new gaxModule . PathTemplate (
128
153
'projects/{project}/deidentifyTemplates/{deidentify_template}'
129
154
) ,
130
- projectInspectTemplatePathTemplate : new gax . PathTemplate (
155
+ projectInspectTemplatePathTemplate : new gaxModule . PathTemplate (
131
156
'projects/{project}/inspectTemplates/{inspect_template}'
132
157
) ,
133
- projectJobTriggerPathTemplate : new gax . PathTemplate (
158
+ projectJobTriggerPathTemplate : new gaxModule . PathTemplate (
134
159
'projects/{project}/jobTriggers/{job_trigger}'
135
160
) ,
136
- projectStoredInfoTypePathTemplate : new gax . PathTemplate (
161
+ projectStoredInfoTypePathTemplate : new gaxModule . PathTemplate (
137
162
'projects/{project}/storedInfoTypes/{stored_info_type}'
138
163
) ,
139
164
} ;
@@ -142,23 +167,27 @@ class DlpServiceClient {
142
167
// (e.g. 50 results at a time, with tokens to get subsequent
143
168
// pages). Denote the keys used for pagination and results.
144
169
this . _descriptors . page = {
145
- listInspectTemplates : new gax . PageDescriptor (
170
+ listInspectTemplates : new gaxModule . PageDescriptor (
146
171
'pageToken' ,
147
172
'nextPageToken' ,
148
173
'inspectTemplates'
149
174
) ,
150
- listDeidentifyTemplates : new gax . PageDescriptor (
175
+ listDeidentifyTemplates : new gaxModule . PageDescriptor (
151
176
'pageToken' ,
152
177
'nextPageToken' ,
153
178
'deidentifyTemplates'
154
179
) ,
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 (
157
186
'pageToken' ,
158
187
'nextPageToken' ,
159
188
'jobTriggers'
160
189
) ,
161
- listStoredInfoTypes : new gax . PageDescriptor (
190
+ listStoredInfoTypes : new gaxModule . PageDescriptor (
162
191
'pageToken' ,
163
192
'nextPageToken' ,
164
193
'storedInfoTypes'
@@ -181,7 +210,9 @@ class DlpServiceClient {
181
210
// Put together the "service stub" for
182
211
// google.privacy.dlp.v2.DlpService.
183
212
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 ,
185
216
opts
186
217
) ;
187
218
@@ -220,18 +251,16 @@ class DlpServiceClient {
220
251
'deleteStoredInfoType' ,
221
252
] ;
222
253
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 ,
235
264
defaults [ methodName ] ,
236
265
this . _descriptors . page [ methodName ]
237
266
) ;
0 commit comments