1717const gapicConfig = require ( './instance_admin_client_config.json' ) ;
1818const gax = require ( 'google-gax' ) ;
1919const path = require ( 'path' ) ;
20- const protobuf = require ( 'protobufjs' ) ;
2120
2221const VERSION = require ( '../../../package.json' ) . version ;
2322
@@ -79,6 +78,16 @@ class InstanceAdminClient {
7978 opts = opts || { } ;
8079 this . _descriptors = { } ;
8180
81+ if ( global . isBrowser ) {
82+ // If we're in browser, we use gRPC fallback.
83+ opts . fallback = true ;
84+ }
85+
86+ // If we are in browser, we are already using fallback because of the
87+ // "browser" field in package.json.
88+ // But if we were explicitly requested to use fallback, let's do it now.
89+ const gaxModule = ! global . isBrowser && opts . fallback ? gax . fallback : gax ;
90+
8291 const servicePath =
8392 opts . servicePath || opts . apiEndpoint || this . constructor . servicePath ;
8493
@@ -95,72 +104,80 @@ class InstanceAdminClient {
95104 // Create a `gaxGrpc` object, with any grpc-specific options
96105 // sent to the client.
97106 opts . scopes = this . constructor . scopes ;
98- const gaxGrpc = new gax . GrpcClient ( opts ) ;
107+ const gaxGrpc = new gaxModule . GrpcClient ( opts ) ;
99108
100109 // Save the auth object to the client, for use by other methods.
101110 this . auth = gaxGrpc . auth ;
102111
103112 // Determine the client header string.
104- const clientHeader = [
105- `gl-node/${ process . versions . node } ` ,
106- `grpc/${ gaxGrpc . grpcVersion } ` ,
107- `gax/${ gax . version } ` ,
108- `gapic/${ VERSION } ` ,
109- ] ;
113+ const clientHeader = [ ] ;
114+
115+ if ( typeof process !== 'undefined' && 'versions' in process ) {
116+ clientHeader . push ( `gl-node/${ process . versions . node } ` ) ;
117+ }
118+ clientHeader . push ( `gax/${ gaxModule . version } ` ) ;
119+ if ( opts . fallback ) {
120+ clientHeader . push ( `gl-web/${ gaxModule . version } ` ) ;
121+ } else {
122+ clientHeader . push ( `grpc/${ gaxGrpc . grpcVersion } ` ) ;
123+ }
124+ clientHeader . push ( `gapic/${ VERSION } ` ) ;
110125 if ( opts . libName && opts . libVersion ) {
111126 clientHeader . push ( `${ opts . libName } /${ opts . libVersion } ` ) ;
112127 }
113128
114129 // Load the applicable protos.
130+ // For Node.js, pass the path to JSON proto file.
131+ // For browsers, pass the JSON content.
132+
133+ const nodejsProtoPath = path . join (
134+ __dirname ,
135+ '..' ,
136+ '..' ,
137+ 'protos' ,
138+ 'protos.json'
139+ ) ;
115140 const protos = gaxGrpc . loadProto (
116- path . join ( __dirname , '..' , '..' , 'protos' ) ,
117- [ 'google/spanner/admin/instance/v1/spanner_instance_admin.proto' ]
141+ opts . fallback ? require ( '../../protos/protos.json' ) : nodejsProtoPath
118142 ) ;
119143
120144 // This API contains "path templates"; forward-slash-separated
121145 // identifiers to uniquely identify resources within the API.
122146 // Create useful helper objects for these.
123147 this . _pathTemplates = {
124- instancePathTemplate : new gax . PathTemplate (
148+ instancePathTemplate : new gaxModule . PathTemplate (
125149 'projects/{project}/instances/{instance}'
126150 ) ,
127- instanceConfigPathTemplate : new gax . PathTemplate (
151+ instanceConfigPathTemplate : new gaxModule . PathTemplate (
128152 'projects/{project}/instanceConfigs/{instance_config}'
129153 ) ,
130- projectPathTemplate : new gax . PathTemplate ( 'projects/{project}' ) ,
154+ projectPathTemplate : new gaxModule . PathTemplate ( 'projects/{project}' ) ,
131155 } ;
132156
133157 // Some of the methods on this service return "paged" results,
134158 // (e.g. 50 results at a time, with tokens to get subsequent
135159 // pages). Denote the keys used for pagination and results.
136160 this . _descriptors . page = {
137- listInstanceConfigs : new gax . PageDescriptor (
161+ listInstanceConfigs : new gaxModule . PageDescriptor (
138162 'pageToken' ,
139163 'nextPageToken' ,
140164 'instanceConfigs'
141165 ) ,
142- listInstances : new gax . PageDescriptor (
166+ listInstances : new gaxModule . PageDescriptor (
143167 'pageToken' ,
144168 'nextPageToken' ,
145169 'instances'
146170 ) ,
147171 } ;
148- let protoFilesRoot = new gax . GoogleProtoFilesRoot ( ) ;
149- protoFilesRoot = protobuf . loadSync (
150- path . join (
151- __dirname ,
152- '..' ,
153- '..' ,
154- 'protos' ,
155- 'google/spanner/admin/instance/v1/spanner_instance_admin.proto'
156- ) ,
157- protoFilesRoot
158- ) ;
172+
173+ const protoFilesRoot = opts . fallback
174+ ? gaxModule . protobuf . Root . fromJSON ( require ( '../../protos/protos.json' ) )
175+ : gaxModule . protobuf . loadSync ( nodejsProtoPath ) ;
159176
160177 // This API contains "long-running operations", which return a
161178 // an Operation object that allows for tracking of the operation,
162179 // rather than holding a request open.
163- this . operationsClient = new gax . lro ( {
180+ this . operationsClient = new gaxModule . lro ( {
164181 auth : gaxGrpc . auth ,
165182 grpc : gaxGrpc . grpc ,
166183 } ) . operationsClient ( opts ) ;
@@ -179,12 +196,12 @@ class InstanceAdminClient {
179196 ) ;
180197
181198 this . _descriptors . longrunning = {
182- createInstance : new gax . LongrunningDescriptor (
199+ createInstance : new gaxModule . LongrunningDescriptor (
183200 this . operationsClient ,
184201 createInstanceResponse . decode . bind ( createInstanceResponse ) ,
185202 createInstanceMetadata . decode . bind ( createInstanceMetadata )
186203 ) ,
187- updateInstance : new gax . LongrunningDescriptor (
204+ updateInstance : new gaxModule . LongrunningDescriptor (
188205 this . operationsClient ,
189206 updateInstanceResponse . decode . bind ( updateInstanceResponse ) ,
190207 updateInstanceMetadata . decode . bind ( updateInstanceMetadata )
@@ -207,7 +224,9 @@ class InstanceAdminClient {
207224 // Put together the "service stub" for
208225 // google.spanner.admin.instance.v1.InstanceAdmin.
209226 const instanceAdminStub = gaxGrpc . createStub (
210- protos . google . spanner . admin . instance . v1 . InstanceAdmin ,
227+ opts . fallback
228+ ? protos . lookupService ( 'google.spanner.admin.instance.v1.InstanceAdmin' )
229+ : protos . google . spanner . admin . instance . v1 . InstanceAdmin ,
211230 opts
212231 ) ;
213232
@@ -226,18 +245,16 @@ class InstanceAdminClient {
226245 'testIamPermissions' ,
227246 ] ;
228247 for ( const methodName of instanceAdminStubMethods ) {
229- this . _innerApiCalls [ methodName ] = gax . createApiCall (
230- instanceAdminStub . then (
231- stub =>
232- function ( ) {
233- const args = Array . prototype . slice . call ( arguments , 0 ) ;
234- return stub [ methodName ] . apply ( stub , args ) ;
235- } ,
236- err =>
237- function ( ) {
238- throw err ;
239- }
240- ) ,
248+ const innerCallPromise = instanceAdminStub . then (
249+ stub => ( ...args ) => {
250+ return stub [ methodName ] . apply ( stub , args ) ;
251+ } ,
252+ err => ( ) => {
253+ throw err ;
254+ }
255+ ) ;
256+ this . _innerApiCalls [ methodName ] = gaxModule . createApiCall (
257+ innerCallPromise ,
241258 defaults [ methodName ] ,
242259 this . _descriptors . page [ methodName ] ||
243260 this . _descriptors . longrunning [ methodName ]
0 commit comments