Skip to content

Commit 0b3fb90

Browse files
yoshi-automationalexander-fenster
authored andcommitted
feat: load protos from JSON, grpc-fallback support
* [CHANGE ME] Re-generated to pick up changes in the API or client library generator. * fixes * fix webpack.config.js * fix protos * gts fix
1 parent 7bc58cf commit 0b3fb90

13 files changed

+1652
-257
lines changed

protos/protos.json

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

src/browser.js

Lines changed: 21 additions & 0 deletions
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');

src/service_proto_list.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/v1/database_admin_client.js

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
const gapicConfig = require('./database_admin_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

@@ -63,6 +62,16 @@ class DatabaseAdminClient {
6362
opts = opts || {};
6463
this._descriptors = {};
6564

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

@@ -79,36 +88,51 @@ class DatabaseAdminClient {
7988
// Create a `gaxGrpc` object, with any grpc-specific options
8089
// sent to the client.
8190
opts.scopes = this.constructor.scopes;
82-
const gaxGrpc = new gax.GrpcClient(opts);
91+
const gaxGrpc = new gaxModule.GrpcClient(opts);
8392

8493
// Save the auth object to the client, for use by other methods.
8594
this.auth = gaxGrpc.auth;
8695

8796
// Determine the client header string.
88-
const clientHeader = [
89-
`gl-node/${process.versions.node}`,
90-
`grpc/${gaxGrpc.grpcVersion}`,
91-
`gax/${gax.version}`,
92-
`gapic/${VERSION}`,
93-
];
97+
const clientHeader = [];
98+
99+
if (typeof process !== 'undefined' && 'versions' in process) {
100+
clientHeader.push(`gl-node/${process.versions.node}`);
101+
}
102+
clientHeader.push(`gax/${gaxModule.version}`);
103+
if (opts.fallback) {
104+
clientHeader.push(`gl-web/${gaxModule.version}`);
105+
} else {
106+
clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`);
107+
}
108+
clientHeader.push(`gapic/${VERSION}`);
94109
if (opts.libName && opts.libVersion) {
95110
clientHeader.push(`${opts.libName}/${opts.libVersion}`);
96111
}
97112

98113
// Load the applicable protos.
114+
// For Node.js, pass the path to JSON proto file.
115+
// For browsers, pass the JSON content.
116+
117+
const nodejsProtoPath = path.join(
118+
__dirname,
119+
'..',
120+
'..',
121+
'protos',
122+
'protos.json'
123+
);
99124
const protos = gaxGrpc.loadProto(
100-
path.join(__dirname, '..', '..', 'protos'),
101-
['google/spanner/admin/database/v1/spanner_database_admin.proto']
125+
opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath
102126
);
103127

104128
// This API contains "path templates"; forward-slash-separated
105129
// identifiers to uniquely identify resources within the API.
106130
// Create useful helper objects for these.
107131
this._pathTemplates = {
108-
databasePathTemplate: new gax.PathTemplate(
132+
databasePathTemplate: new gaxModule.PathTemplate(
109133
'projects/{project}/instances/{instance}/databases/{database}'
110134
),
111-
instancePathTemplate: new gax.PathTemplate(
135+
instancePathTemplate: new gaxModule.PathTemplate(
112136
'projects/{project}/instances/{instance}'
113137
),
114138
};
@@ -117,28 +141,21 @@ class DatabaseAdminClient {
117141
// (e.g. 50 results at a time, with tokens to get subsequent
118142
// pages). Denote the keys used for pagination and results.
119143
this._descriptors.page = {
120-
listDatabases: new gax.PageDescriptor(
144+
listDatabases: new gaxModule.PageDescriptor(
121145
'pageToken',
122146
'nextPageToken',
123147
'databases'
124148
),
125149
};
126-
let protoFilesRoot = new gax.GoogleProtoFilesRoot();
127-
protoFilesRoot = protobuf.loadSync(
128-
path.join(
129-
__dirname,
130-
'..',
131-
'..',
132-
'protos',
133-
'google/spanner/admin/database/v1/spanner_database_admin.proto'
134-
),
135-
protoFilesRoot
136-
);
150+
151+
const protoFilesRoot = opts.fallback
152+
? gaxModule.protobuf.Root.fromJSON(require('../../protos/protos.json'))
153+
: gaxModule.protobuf.loadSync(nodejsProtoPath);
137154

138155
// This API contains "long-running operations", which return a
139156
// an Operation object that allows for tracking of the operation,
140157
// rather than holding a request open.
141-
this.operationsClient = new gax.lro({
158+
this.operationsClient = new gaxModule.lro({
142159
auth: gaxGrpc.auth,
143160
grpc: gaxGrpc.grpc,
144161
}).operationsClient(opts);
@@ -157,12 +174,12 @@ class DatabaseAdminClient {
157174
);
158175

159176
this._descriptors.longrunning = {
160-
createDatabase: new gax.LongrunningDescriptor(
177+
createDatabase: new gaxModule.LongrunningDescriptor(
161178
this.operationsClient,
162179
createDatabaseResponse.decode.bind(createDatabaseResponse),
163180
createDatabaseMetadata.decode.bind(createDatabaseMetadata)
164181
),
165-
updateDatabaseDdl: new gax.LongrunningDescriptor(
182+
updateDatabaseDdl: new gaxModule.LongrunningDescriptor(
166183
this.operationsClient,
167184
updateDatabaseDdlResponse.decode.bind(updateDatabaseDdlResponse),
168185
updateDatabaseDdlMetadata.decode.bind(updateDatabaseDdlMetadata)
@@ -185,7 +202,9 @@ class DatabaseAdminClient {
185202
// Put together the "service stub" for
186203
// google.spanner.admin.database.v1.DatabaseAdmin.
187204
const databaseAdminStub = gaxGrpc.createStub(
188-
protos.google.spanner.admin.database.v1.DatabaseAdmin,
205+
opts.fallback
206+
? protos.lookupService('google.spanner.admin.database.v1.DatabaseAdmin')
207+
: protos.google.spanner.admin.database.v1.DatabaseAdmin,
189208
opts
190209
);
191210

@@ -203,18 +222,16 @@ class DatabaseAdminClient {
203222
'testIamPermissions',
204223
];
205224
for (const methodName of databaseAdminStubMethods) {
206-
this._innerApiCalls[methodName] = gax.createApiCall(
207-
databaseAdminStub.then(
208-
stub =>
209-
function() {
210-
const args = Array.prototype.slice.call(arguments, 0);
211-
return stub[methodName].apply(stub, args);
212-
},
213-
err =>
214-
function() {
215-
throw err;
216-
}
217-
),
225+
const innerCallPromise = databaseAdminStub.then(
226+
stub => (...args) => {
227+
return stub[methodName].apply(stub, args);
228+
},
229+
err => () => {
230+
throw err;
231+
}
232+
);
233+
this._innerApiCalls[methodName] = gaxModule.createApiCall(
234+
innerCallPromise,
218235
defaults[methodName],
219236
this._descriptors.page[methodName] ||
220237
this._descriptors.longrunning[methodName]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["../../protos/google/spanner/admin/database/v1/spanner_database_admin.proto"]

src/v1/instance_admin_client.js

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
const gapicConfig = require('./instance_admin_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

@@ -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]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["../../protos/google/spanner/admin/instance/v1/spanner_instance_admin.proto"]

0 commit comments

Comments
 (0)