Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.

Commit 3cc8989

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
1 parent d54da3d commit 3cc8989

12 files changed

+605
-80
lines changed

protos/protos.json

Lines changed: 408 additions & 0 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/speech_client.js

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
const gapicConfig = require('./speech_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 SpeechClient {
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,51 +84,59 @@ class SpeechClient {
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/cloud/speech/v1/cloud_speech.proto']
121+
opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath
98122
);
99123

100124
// Some of the methods on this service provide streaming responses.
101125
// Provide descriptors for these.
102126
this._descriptors.stream = {
103-
streamingRecognize: new gax.StreamDescriptor(
127+
streamingRecognize: new gaxModule.StreamDescriptor(
104128
gax.StreamType.BIDI_STREAMING
105129
),
106130
};
107-
let protoFilesRoot = new gax.GoogleProtoFilesRoot();
108-
protoFilesRoot = protobuf.loadSync(
109-
path.join(
110-
__dirname,
111-
'..',
112-
'..',
113-
'protos',
114-
'google/cloud/speech/v1/cloud_speech.proto'
115-
),
116-
protoFilesRoot
117-
);
131+
132+
const protoFilesRoot = opts.fallback
133+
? gaxModule.protobuf.Root.fromJSON(require('../../protos/protos.json'))
134+
: gaxModule.protobuf.loadSync(nodejsProtoPath);
118135

119136
// This API contains "long-running operations", which return a
120137
// an Operation object that allows for tracking of the operation,
121138
// rather than holding a request open.
122-
this.operationsClient = new gax.lro({
139+
this.operationsClient = new gaxModule.lro({
123140
auth: gaxGrpc.auth,
124141
grpc: gaxGrpc.grpc,
125142
}).operationsClient(opts);
@@ -132,7 +149,7 @@ class SpeechClient {
132149
);
133150

134151
this._descriptors.longrunning = {
135-
longRunningRecognize: new gax.LongrunningDescriptor(
152+
longRunningRecognize: new gaxModule.LongrunningDescriptor(
136153
this.operationsClient,
137154
longRunningRecognizeResponse.decode.bind(longRunningRecognizeResponse),
138155
longRunningRecognizeMetadata.decode.bind(longRunningRecognizeMetadata)
@@ -155,7 +172,9 @@ class SpeechClient {
155172
// Put together the "service stub" for
156173
// google.cloud.speech.v1.Speech.
157174
const speechStub = gaxGrpc.createStub(
158-
protos.google.cloud.speech.v1.Speech,
175+
opts.fallback
176+
? protos.lookupService('google.cloud.speech.v1.Speech')
177+
: protos.google.cloud.speech.v1.Speech,
159178
opts
160179
);
161180

@@ -167,18 +186,16 @@ class SpeechClient {
167186
'streamingRecognize',
168187
];
169188
for (const methodName of speechStubMethods) {
170-
this._innerApiCalls[methodName] = gax.createApiCall(
171-
speechStub.then(
172-
stub =>
173-
function() {
174-
const args = Array.prototype.slice.call(arguments, 0);
175-
return stub[methodName].apply(stub, args);
176-
},
177-
err =>
178-
function() {
179-
throw err;
180-
}
181-
),
189+
const innerCallPromise = speechStub.then(
190+
stub => (...args) => {
191+
return stub[methodName].apply(stub, args);
192+
},
193+
err => () => {
194+
throw err;
195+
}
196+
);
197+
this._innerApiCalls[methodName] = gaxModule.createApiCall(
198+
innerCallPromise,
182199
defaults[methodName],
183200
this._descriptors.stream[methodName] ||
184201
this._descriptors.longrunning[methodName]

src/v1/speech_proto_list.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"../../protos/google/cloud/speech/v1/cloud_speech.proto"
3+
]

src/v1p1beta1/speech_client.js

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
const gapicConfig = require('./speech_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 SpeechClient {
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,51 +84,59 @@ class SpeechClient {
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/cloud/speech/v1p1beta1/cloud_speech.proto']
121+
opts.fallback ? require('../../protos/protos.json') : nodejsProtoPath
98122
);
99123

100124
// Some of the methods on this service provide streaming responses.
101125
// Provide descriptors for these.
102126
this._descriptors.stream = {
103-
streamingRecognize: new gax.StreamDescriptor(
127+
streamingRecognize: new gaxModule.StreamDescriptor(
104128
gax.StreamType.BIDI_STREAMING
105129
),
106130
};
107-
let protoFilesRoot = new gax.GoogleProtoFilesRoot();
108-
protoFilesRoot = protobuf.loadSync(
109-
path.join(
110-
__dirname,
111-
'..',
112-
'..',
113-
'protos',
114-
'google/cloud/speech/v1p1beta1/cloud_speech.proto'
115-
),
116-
protoFilesRoot
117-
);
131+
132+
const protoFilesRoot = opts.fallback
133+
? gaxModule.protobuf.Root.fromJSON(require('../../protos/protos.json'))
134+
: gaxModule.protobuf.loadSync(nodejsProtoPath);
118135

119136
// This API contains "long-running operations", which return a
120137
// an Operation object that allows for tracking of the operation,
121138
// rather than holding a request open.
122-
this.operationsClient = new gax.lro({
139+
this.operationsClient = new gaxModule.lro({
123140
auth: gaxGrpc.auth,
124141
grpc: gaxGrpc.grpc,
125142
}).operationsClient(opts);
@@ -132,7 +149,7 @@ class SpeechClient {
132149
);
133150

134151
this._descriptors.longrunning = {
135-
longRunningRecognize: new gax.LongrunningDescriptor(
152+
longRunningRecognize: new gaxModule.LongrunningDescriptor(
136153
this.operationsClient,
137154
longRunningRecognizeResponse.decode.bind(longRunningRecognizeResponse),
138155
longRunningRecognizeMetadata.decode.bind(longRunningRecognizeMetadata)
@@ -155,7 +172,9 @@ class SpeechClient {
155172
// Put together the "service stub" for
156173
// google.cloud.speech.v1p1beta1.Speech.
157174
const speechStub = gaxGrpc.createStub(
158-
protos.google.cloud.speech.v1p1beta1.Speech,
175+
opts.fallback
176+
? protos.lookupService('google.cloud.speech.v1p1beta1.Speech')
177+
: protos.google.cloud.speech.v1p1beta1.Speech,
159178
opts
160179
);
161180

@@ -167,18 +186,16 @@ class SpeechClient {
167186
'streamingRecognize',
168187
];
169188
for (const methodName of speechStubMethods) {
170-
this._innerApiCalls[methodName] = gax.createApiCall(
171-
speechStub.then(
172-
stub =>
173-
function() {
174-
const args = Array.prototype.slice.call(arguments, 0);
175-
return stub[methodName].apply(stub, args);
176-
},
177-
err =>
178-
function() {
179-
throw err;
180-
}
181-
),
189+
const innerCallPromise = speechStub.then(
190+
stub => (...args) => {
191+
return stub[methodName].apply(stub, args);
192+
},
193+
err => () => {
194+
throw err;
195+
}
196+
);
197+
this._innerApiCalls[methodName] = gaxModule.createApiCall(
198+
innerCallPromise,
182199
defaults[methodName],
183200
this._descriptors.stream[methodName] ||
184201
this._descriptors.longrunning[methodName]

src/v1p1beta1/speech_proto_list.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"../../protos/google/cloud/speech/v1p1beta1/cloud_speech.proto"
3+
]

synth.metadata

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
2-
"updateTime": "2019-08-28T11:21:37.744438Z",
2+
"updateTime": "2019-08-31T11:19:24.627851Z",
33
"sources": [
44
{
55
"generator": {
66
"name": "artman",
7-
"version": "0.35.1",
8-
"dockerImage": "googleapis/artman@sha256:b11c7ea0d0831c54016fb50f4b796d24d1971439b30fbc32a369ba1ac887c384"
7+
"version": "0.36.1",
8+
"dockerImage": "googleapis/artman@sha256:7c20f006c7a62d9d782e2665647d52290c37a952ef3cd134624d5dd62b3f71bd"
99
}
1010
},
1111
{
1212
"git": {
1313
"name": "googleapis",
1414
"remote": "https://github.com/googleapis/googleapis.git",
15-
"sha": "dbd38035c35083507e2f0b839985cf17e212cb1c",
16-
"internalRef": "265796259"
15+
"sha": "82809578652607c8ee29d9e199c21f28f81a03e0",
16+
"internalRef": "266247326"
1717
}
1818
},
1919
{

synth.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@
6060
#
6161
subprocess.run(['npm', 'install'])
6262
subprocess.run(['npm', 'run', 'fix'])
63+
subprocess.run(['npx', 'compileProtos', 'src'])

test/gapic-v1.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ describe('SpeechClient', () => {
4545
assert(client);
4646
});
4747

48+
it('should create a client with gRPC fallback', () => {
49+
const client = new speechModule.v1.SpeechClient({fallback: true});
50+
assert(client);
51+
});
52+
4853
describe('recognize', () => {
4954
it('invokes recognize without error', done => {
5055
const client = new speechModule.v1.SpeechClient({

0 commit comments

Comments
 (0)