17
17
const gapicConfig = require ( './logging_service_v2_client_config.json' ) ;
18
18
const gax = require ( 'google-gax' ) ;
19
19
const path = require ( 'path' ) ;
20
- const protobuf = require ( 'protobufjs' ) ;
21
20
22
21
const VERSION = require ( '../../../package.json' ) . version ;
23
22
@@ -59,6 +58,16 @@ class LoggingServiceV2Client {
59
58
opts = opts || { } ;
60
59
this . _descriptors = { } ;
61
60
61
+ if ( global . isBrowser ) {
62
+ // If we're in browser, we use gRPC fallback.
63
+ opts . fallback = true ;
64
+ }
65
+
66
+ // If we are in browser, we are already using fallback because of the
67
+ // "browser" field in package.json.
68
+ // But if we were explicitly requested to use fallback, let's do it now.
69
+ const gaxModule = ! global . isBrowser && opts . fallback ? gax . fallback : gax ;
70
+
62
71
const servicePath =
63
72
opts . servicePath || opts . apiEndpoint || this . constructor . servicePath ;
64
73
@@ -75,88 +84,98 @@ class LoggingServiceV2Client {
75
84
// Create a `gaxGrpc` object, with any grpc-specific options
76
85
// sent to the client.
77
86
opts . scopes = this . constructor . scopes ;
78
- const gaxGrpc = new gax . GrpcClient ( opts ) ;
87
+ const gaxGrpc = new gaxModule . GrpcClient ( opts ) ;
79
88
80
89
// Save the auth object to the client, for use by other methods.
81
90
this . auth = gaxGrpc . auth ;
82
91
83
92
// Determine the client header string.
84
- const clientHeader = [
85
- `gl-node/${ process . versions . node } ` ,
86
- `grpc/${ gaxGrpc . grpcVersion } ` ,
87
- `gax/${ gax . version } ` ,
88
- `gapic/${ VERSION } ` ,
89
- ] ;
93
+ const clientHeader = [ ] ;
94
+
95
+ if ( typeof process !== 'undefined' && 'versions' in process ) {
96
+ clientHeader . push ( `gl-node/${ process . versions . node } ` ) ;
97
+ }
98
+ clientHeader . push ( `gax/${ gaxModule . version } ` ) ;
99
+ if ( opts . fallback ) {
100
+ clientHeader . push ( `gl-web/${ gaxModule . version } ` ) ;
101
+ } else {
102
+ clientHeader . push ( `grpc/${ gaxGrpc . grpcVersion } ` ) ;
103
+ }
104
+ clientHeader . push ( `gapic/${ VERSION } ` ) ;
90
105
if ( opts . libName && opts . libVersion ) {
91
106
clientHeader . push ( `${ opts . libName } /${ opts . libVersion } ` ) ;
92
107
}
93
108
94
109
// Load the applicable protos.
110
+ // For Node.js, pass the path to JSON proto file.
111
+ // For browsers, pass the JSON content.
112
+
113
+ const nodejsProtoPath = path . join (
114
+ __dirname ,
115
+ '..' ,
116
+ '..' ,
117
+ 'protos' ,
118
+ 'protos.json'
119
+ ) ;
95
120
const protos = gaxGrpc . loadProto (
96
- path . join ( __dirname , '..' , '..' , 'protos' ) ,
97
- [ 'google/logging/v2/logging.proto' ]
121
+ opts . fallback ? require ( '../../protos/protos.json' ) : nodejsProtoPath
98
122
) ;
99
123
100
124
// This API contains "path templates"; forward-slash-separated
101
125
// identifiers to uniquely identify resources within the API.
102
126
// Create useful helper objects for these.
103
127
this . _pathTemplates = {
104
- billingPathTemplate : new gax . PathTemplate (
128
+ billingPathTemplate : new gaxModule . PathTemplate (
105
129
'billingAccounts/{billing_account}'
106
130
) ,
107
- billingLogPathTemplate : new gax . PathTemplate (
131
+ billingLogPathTemplate : new gaxModule . PathTemplate (
108
132
'billingAccounts/{billing_account}/logs/{log}'
109
133
) ,
110
- folderPathTemplate : new gax . PathTemplate ( 'folders/{folder}' ) ,
111
- folderLogPathTemplate : new gax . PathTemplate (
134
+ folderPathTemplate : new gaxModule . PathTemplate ( 'folders/{folder}' ) ,
135
+ folderLogPathTemplate : new gaxModule . PathTemplate (
112
136
'folders/{folder}/logs/{log}'
113
137
) ,
114
- logPathTemplate : new gax . PathTemplate ( 'projects/{project}/logs/{log}' ) ,
115
- organizationPathTemplate : new gax . PathTemplate (
138
+ logPathTemplate : new gaxModule . PathTemplate (
139
+ 'projects/{project}/logs/{log}'
140
+ ) ,
141
+ organizationPathTemplate : new gaxModule . PathTemplate (
116
142
'organizations/{organization}'
117
143
) ,
118
- organizationLogPathTemplate : new gax . PathTemplate (
144
+ organizationLogPathTemplate : new gaxModule . PathTemplate (
119
145
'organizations/{organization}/logs/{log}'
120
146
) ,
121
- projectPathTemplate : new gax . PathTemplate ( 'projects/{project}' ) ,
147
+ projectPathTemplate : new gaxModule . PathTemplate ( 'projects/{project}' ) ,
122
148
} ;
123
149
124
150
// Some of the methods on this service return "paged" results,
125
151
// (e.g. 50 results at a time, with tokens to get subsequent
126
152
// pages). Denote the keys used for pagination and results.
127
153
this . _descriptors . page = {
128
- listLogEntries : new gax . PageDescriptor (
154
+ listLogEntries : new gaxModule . PageDescriptor (
129
155
'pageToken' ,
130
156
'nextPageToken' ,
131
157
'entries'
132
158
) ,
133
- listMonitoredResourceDescriptors : new gax . PageDescriptor (
159
+ listMonitoredResourceDescriptors : new gaxModule . PageDescriptor (
134
160
'pageToken' ,
135
161
'nextPageToken' ,
136
162
'resourceDescriptors'
137
163
) ,
138
- listLogs : new gax . PageDescriptor (
164
+ listLogs : new gaxModule . PageDescriptor (
139
165
'pageToken' ,
140
166
'nextPageToken' ,
141
167
'logNames'
142
168
) ,
143
169
} ;
144
- let protoFilesRoot = new gax . GoogleProtoFilesRoot ( ) ;
145
- protoFilesRoot = protobuf . loadSync (
146
- path . join (
147
- __dirname ,
148
- '..' ,
149
- '..' ,
150
- 'protos' ,
151
- 'google/logging/v2/logging.proto'
152
- ) ,
153
- protoFilesRoot
154
- ) ;
170
+
171
+ const protoFilesRoot = opts . fallback
172
+ ? gaxModule . protobuf . Root . fromJSON ( require ( '../../protos/protos.json' ) )
173
+ : gaxModule . protobuf . loadSync ( nodejsProtoPath ) ;
155
174
156
175
// Some methods on this API support automatically batching
157
176
// requests; denote this.
158
177
this . _descriptors . batching = {
159
- writeLogEntries : new gax . BundleDescriptor (
178
+ writeLogEntries : new gaxModule . BundleDescriptor (
160
179
'entries' ,
161
180
[ 'logName' , 'resource' , 'labels' ] ,
162
181
null ,
@@ -182,7 +201,9 @@ class LoggingServiceV2Client {
182
201
// Put together the "service stub" for
183
202
// google.logging.v2.LoggingServiceV2.
184
203
const loggingServiceV2Stub = gaxGrpc . createStub (
185
- protos . google . logging . v2 . LoggingServiceV2 ,
204
+ opts . fallback
205
+ ? protos . lookupService ( 'google.logging.v2.LoggingServiceV2' )
206
+ : protos . google . logging . v2 . LoggingServiceV2 ,
186
207
opts
187
208
) ;
188
209
@@ -196,18 +217,16 @@ class LoggingServiceV2Client {
196
217
'listLogs' ,
197
218
] ;
198
219
for ( const methodName of loggingServiceV2StubMethods ) {
199
- this . _innerApiCalls [ methodName ] = gax . createApiCall (
200
- loggingServiceV2Stub . then (
201
- stub =>
202
- function ( ) {
203
- const args = Array . prototype . slice . call ( arguments , 0 ) ;
204
- return stub [ methodName ] . apply ( stub , args ) ;
205
- } ,
206
- err =>
207
- function ( ) {
208
- throw err ;
209
- }
210
- ) ,
220
+ const innerCallPromise = loggingServiceV2Stub . then (
221
+ stub => ( ...args ) => {
222
+ return stub [ methodName ] . apply ( stub , args ) ;
223
+ } ,
224
+ err => ( ) => {
225
+ throw err ;
226
+ }
227
+ ) ;
228
+ this . _innerApiCalls [ methodName ] = gaxModule . createApiCall (
229
+ innerCallPromise ,
211
230
defaults [ methodName ] ,
212
231
this . _descriptors . page [ methodName ] ||
213
232
this . _descriptors . batching [ methodName ]
0 commit comments