Skip to content

Commit 41ef532

Browse files
yoshi-automationalexander-fenster
authored andcommitted
feat: load protos from JSON, grpc-fallback support (#571)
* [CHANGE ME] Re-generated to pick up changes in the API or client library generator. * no browser support for typescript * fix headers order in system test
1 parent 7077e64 commit 41ef532

11 files changed

+646
-540
lines changed

protos/protos.json

Lines changed: 423 additions & 423 deletions
Large diffs are not rendered by default.

src/v2/config_service_v2_client.js

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ class ConfigServiceV2Client {
5959
opts = opts || {};
6060
this._descriptors = {};
6161

62+
if (global.isBrowser) {
63+
// If we're in browser, we use gRPC fallback.
64+
opts.fallback = true;
65+
}
66+
67+
// If we are in browser, we are already using fallback because of the
68+
// "browser" field in package.json.
69+
// But if we were explicitly requested to use fallback, let's do it now.
70+
const gaxModule = !global.isBrowser && opts.fallback ? gax.fallback : gax;
71+
6272
const servicePath =
6373
opts.servicePath || opts.apiEndpoint || this.constructor.servicePath;
6474

@@ -75,70 +85,91 @@ class ConfigServiceV2Client {
7585
// Create a `gaxGrpc` object, with any grpc-specific options
7686
// sent to the client.
7787
opts.scopes = this.constructor.scopes;
78-
const gaxGrpc = new gax.GrpcClient(opts);
88+
const gaxGrpc = new gaxModule.GrpcClient(opts);
7989

8090
// Save the auth object to the client, for use by other methods.
8191
this.auth = gaxGrpc.auth;
8292

8393
// 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-
];
94+
const clientHeader = [];
95+
96+
if (typeof process !== 'undefined' && 'versions' in process) {
97+
clientHeader.push(`gl-node/${process.versions.node}`);
98+
}
99+
clientHeader.push(`gax/${gaxModule.version}`);
100+
if (opts.fallback) {
101+
clientHeader.push(`gl-web/${gaxModule.version}`);
102+
} else {
103+
clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`);
104+
}
105+
clientHeader.push(`gapic/${VERSION}`);
90106
if (opts.libName && opts.libVersion) {
91107
clientHeader.push(`${opts.libName}/${opts.libVersion}`);
92108
}
93109

94110
// Load the applicable protos.
111+
// For Node.js, pass the path to JSON proto file.
112+
// For browsers, pass the JSON content.
113+
114+
const nodejsProtoPath = path.join(
115+
__dirname,
116+
'..',
117+
'..',
118+
'protos',
119+
'protos.json'
120+
);
95121
const protos = gaxGrpc.loadProto(
96-
path.join(__dirname, '..', '..', 'protos'),
97-
['google/logging/v2/logging_config.proto']
122+
opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath
98123
);
99124

100125
// This API contains "path templates"; forward-slash-separated
101126
// identifiers to uniquely identify resources within the API.
102127
// Create useful helper objects for these.
103128
this._pathTemplates = {
104-
billingPathTemplate: new gax.PathTemplate(
129+
billingPathTemplate: new gaxModule.PathTemplate(
105130
'billingAccounts/{billing_account}'
106131
),
107-
billingExclusionPathTemplate: new gax.PathTemplate(
132+
billingExclusionPathTemplate: new gaxModule.PathTemplate(
108133
'billingAccounts/{billing_account}/exclusions/{exclusion}'
109134
),
110-
billingSinkPathTemplate: new gax.PathTemplate(
135+
billingSinkPathTemplate: new gaxModule.PathTemplate(
111136
'billingAccounts/{billing_account}/sinks/{sink}'
112137
),
113-
exclusionPathTemplate: new gax.PathTemplate(
138+
exclusionPathTemplate: new gaxModule.PathTemplate(
114139
'projects/{project}/exclusions/{exclusion}'
115140
),
116-
folderPathTemplate: new gax.PathTemplate('folders/{folder}'),
117-
folderExclusionPathTemplate: new gax.PathTemplate(
141+
folderPathTemplate: new gaxModule.PathTemplate('folders/{folder}'),
142+
folderExclusionPathTemplate: new gaxModule.PathTemplate(
118143
'folders/{folder}/exclusions/{exclusion}'
119144
),
120-
folderSinkPathTemplate: new gax.PathTemplate(
145+
folderSinkPathTemplate: new gaxModule.PathTemplate(
121146
'folders/{folder}/sinks/{sink}'
122147
),
123-
organizationPathTemplate: new gax.PathTemplate(
148+
organizationPathTemplate: new gaxModule.PathTemplate(
124149
'organizations/{organization}'
125150
),
126-
organizationExclusionPathTemplate: new gax.PathTemplate(
151+
organizationExclusionPathTemplate: new gaxModule.PathTemplate(
127152
'organizations/{organization}/exclusions/{exclusion}'
128153
),
129-
organizationSinkPathTemplate: new gax.PathTemplate(
154+
organizationSinkPathTemplate: new gaxModule.PathTemplate(
130155
'organizations/{organization}/sinks/{sink}'
131156
),
132-
projectPathTemplate: new gax.PathTemplate('projects/{project}'),
133-
sinkPathTemplate: new gax.PathTemplate('projects/{project}/sinks/{sink}'),
157+
projectPathTemplate: new gaxModule.PathTemplate('projects/{project}'),
158+
sinkPathTemplate: new gaxModule.PathTemplate(
159+
'projects/{project}/sinks/{sink}'
160+
),
134161
};
135162

136163
// Some of the methods on this service return "paged" results,
137164
// (e.g. 50 results at a time, with tokens to get subsequent
138165
// pages). Denote the keys used for pagination and results.
139166
this._descriptors.page = {
140-
listSinks: new gax.PageDescriptor('pageToken', 'nextPageToken', 'sinks'),
141-
listExclusions: new gax.PageDescriptor(
167+
listSinks: new gaxModule.PageDescriptor(
168+
'pageToken',
169+
'nextPageToken',
170+
'sinks'
171+
),
172+
listExclusions: new gaxModule.PageDescriptor(
142173
'pageToken',
143174
'nextPageToken',
144175
'exclusions'
@@ -161,7 +192,9 @@ class ConfigServiceV2Client {
161192
// Put together the "service stub" for
162193
// google.logging.v2.ConfigServiceV2.
163194
const configServiceV2Stub = gaxGrpc.createStub(
164-
protos.google.logging.v2.ConfigServiceV2,
195+
opts.fallback
196+
? protos.lookupService('google.logging.v2.ConfigServiceV2')
197+
: protos.google.logging.v2.ConfigServiceV2,
165198
opts
166199
);
167200

@@ -180,18 +213,16 @@ class ConfigServiceV2Client {
180213
'deleteExclusion',
181214
];
182215
for (const methodName of configServiceV2StubMethods) {
183-
this._innerApiCalls[methodName] = gax.createApiCall(
184-
configServiceV2Stub.then(
185-
stub =>
186-
function() {
187-
const args = Array.prototype.slice.call(arguments, 0);
188-
return stub[methodName].apply(stub, args);
189-
},
190-
err =>
191-
function() {
192-
throw err;
193-
}
194-
),
216+
const innerCallPromise = configServiceV2Stub.then(
217+
stub => (...args) => {
218+
return stub[methodName].apply(stub, args);
219+
},
220+
err => () => {
221+
throw err;
222+
}
223+
);
224+
this._innerApiCalls[methodName] = gaxModule.createApiCall(
225+
innerCallPromise,
195226
defaults[methodName],
196227
this._descriptors.page[methodName]
197228
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"../../protos/google/logging/v2/logging_config.proto"
3+
]

src/v2/logging_service_v2_client.js

Lines changed: 65 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
const gapicConfig = require('./logging_service_v2_client_config.json');
1818
const gax = require('google-gax');
1919
const path = require('path');
20-
const protobuf = require('protobufjs');
2120

2221
const VERSION = require('../../../package.json').version;
2322

@@ -59,6 +58,16 @@ class LoggingServiceV2Client {
5958
opts = opts || {};
6059
this._descriptors = {};
6160

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+
6271
const servicePath =
6372
opts.servicePath || opts.apiEndpoint || this.constructor.servicePath;
6473

@@ -75,88 +84,98 @@ class LoggingServiceV2Client {
7584
// Create a `gaxGrpc` object, with any grpc-specific options
7685
// sent to the client.
7786
opts.scopes = this.constructor.scopes;
78-
const gaxGrpc = new gax.GrpcClient(opts);
87+
const gaxGrpc = new gaxModule.GrpcClient(opts);
7988

8089
// Save the auth object to the client, for use by other methods.
8190
this.auth = gaxGrpc.auth;
8291

8392
// 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}`);
90105
if (opts.libName && opts.libVersion) {
91106
clientHeader.push(`${opts.libName}/${opts.libVersion}`);
92107
}
93108

94109
// 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+
);
95120
const protos = gaxGrpc.loadProto(
96-
path.join(__dirname, '..', '..', 'protos'),
97-
['google/logging/v2/logging.proto']
121+
opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath
98122
);
99123

100124
// This API contains "path templates"; forward-slash-separated
101125
// identifiers to uniquely identify resources within the API.
102126
// Create useful helper objects for these.
103127
this._pathTemplates = {
104-
billingPathTemplate: new gax.PathTemplate(
128+
billingPathTemplate: new gaxModule.PathTemplate(
105129
'billingAccounts/{billing_account}'
106130
),
107-
billingLogPathTemplate: new gax.PathTemplate(
131+
billingLogPathTemplate: new gaxModule.PathTemplate(
108132
'billingAccounts/{billing_account}/logs/{log}'
109133
),
110-
folderPathTemplate: new gax.PathTemplate('folders/{folder}'),
111-
folderLogPathTemplate: new gax.PathTemplate(
134+
folderPathTemplate: new gaxModule.PathTemplate('folders/{folder}'),
135+
folderLogPathTemplate: new gaxModule.PathTemplate(
112136
'folders/{folder}/logs/{log}'
113137
),
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(
116142
'organizations/{organization}'
117143
),
118-
organizationLogPathTemplate: new gax.PathTemplate(
144+
organizationLogPathTemplate: new gaxModule.PathTemplate(
119145
'organizations/{organization}/logs/{log}'
120146
),
121-
projectPathTemplate: new gax.PathTemplate('projects/{project}'),
147+
projectPathTemplate: new gaxModule.PathTemplate('projects/{project}'),
122148
};
123149

124150
// Some of the methods on this service return "paged" results,
125151
// (e.g. 50 results at a time, with tokens to get subsequent
126152
// pages). Denote the keys used for pagination and results.
127153
this._descriptors.page = {
128-
listLogEntries: new gax.PageDescriptor(
154+
listLogEntries: new gaxModule.PageDescriptor(
129155
'pageToken',
130156
'nextPageToken',
131157
'entries'
132158
),
133-
listMonitoredResourceDescriptors: new gax.PageDescriptor(
159+
listMonitoredResourceDescriptors: new gaxModule.PageDescriptor(
134160
'pageToken',
135161
'nextPageToken',
136162
'resourceDescriptors'
137163
),
138-
listLogs: new gax.PageDescriptor(
164+
listLogs: new gaxModule.PageDescriptor(
139165
'pageToken',
140166
'nextPageToken',
141167
'logNames'
142168
),
143169
};
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);
155174

156175
// Some methods on this API support automatically batching
157176
// requests; denote this.
158177
this._descriptors.batching = {
159-
writeLogEntries: new gax.BundleDescriptor(
178+
writeLogEntries: new gaxModule.BundleDescriptor(
160179
'entries',
161180
['logName', 'resource', 'labels'],
162181
null,
@@ -182,7 +201,9 @@ class LoggingServiceV2Client {
182201
// Put together the "service stub" for
183202
// google.logging.v2.LoggingServiceV2.
184203
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,
186207
opts
187208
);
188209

@@ -196,18 +217,16 @@ class LoggingServiceV2Client {
196217
'listLogs',
197218
];
198219
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,
211230
defaults[methodName],
212231
this._descriptors.page[methodName] ||
213232
this._descriptors.batching[methodName]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"../../protos/google/logging/v2/logging.proto"
3+
]

0 commit comments

Comments
 (0)