Skip to content

Commit b872f2b

Browse files
feat!: use @grpc/grpc-js instead of grpc (#484)
BREAKING CHANGE * feat: use @grpc/grpc-js instead of grpc * no need to have client libs test since end-to-end exists * use any instead of ts-ignore * no need to console.error * fix windows builds
1 parent b0dbafb commit b872f2b

File tree

18 files changed

+53
-245
lines changed

18 files changed

+53
-245
lines changed

.kokoro/test.bat

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@
1717
cd /d %~dp0
1818
cd ..
1919

20-
call npm install -g npm@latest || goto :error
20+
@rem The image we're currently running has a broken version of Node.js enabled
21+
@rem by nvm (v10.15.3), which has no npm bin. This hack uses the functional
22+
@rem Node v8.9.1 to install npm@latest, it then uses this version of npm to
23+
@rem install npm for v10.15.3.
24+
call nvm use v8.9.1 || goto :error
25+
call node C:\Users\kbuilder\AppData\Roaming\nvm-ps\versions\v8.9.1\node_modules\npm-bootstrap\bin\npm-cli.js i npm -g || goto :error
26+
call nvm use v10.15.3 || goto :error
27+
call node C:\Users\kbuilder\AppData\Roaming\nvm-ps\versions\v8.9.1\node_modules\npm\bin\npm-cli.js i npm -g || goto :error
28+
2129
call npm install || goto :error
2230
call npm run test || goto :error
2331

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ Application code will rarely need to use most of the classes within this library
1515
$ npm install google-gax
1616
```
1717

18+
## Supporting older version of Node.js
19+
20+
This library uses [grpc-js](https://www.npmjs.com/package/@grpc/grpc-js) package for communicating with API server, and it uses HTTP/2 functionality
21+
that is only available in Node.js v8.13.0 or newer. If you need to use this library with older versions of Node.js, you need to make your code depend
22+
on a legacy gRPC library ([grpc](https://www.npmjs.com/package/grpc)) and pass the instance of gRPC to the client constructor:
23+
24+
```js
25+
const grpc = require('grpc');
26+
const client = new APIClient({ grpc }); // APIClient is the client class you use, e.g. SpeechClient, etc.
27+
```
28+
1829
## Contributing
1930
Contributions to this library are always welcome and highly encouraged. See the [CONTRIBUTING][contributing] documentation for more information on how to get started.
2031

package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
"@grpc/proto-loader": "^0.5.0",
1414
"duplexify": "^3.6.0",
1515
"google-auth-library": "^3.0.0",
16-
"grpc": "^1.20.2",
17-
"grpc-gcp": "^0.1.1",
1816
"is-stream-ended": "^0.1.4",
1917
"lodash.at": "^4.6.0",
2018
"lodash.has": "^4.5.2",
@@ -41,7 +39,6 @@
4139
"@types/through2": "^2.0.33",
4240
"chai": "*",
4341
"codecov": "^3.1.0",
44-
"cross-env": "^5.2.0",
4542
"eslint": "^5.9.0",
4643
"eslint-config-prettier": "^4.0.0",
4744
"eslint-plugin-node": "^9.0.0",
@@ -71,7 +68,7 @@
7168
"docs": "compodoc src/",
7269
"gen-parser": "pegjs src/pathTemplateParser.pegjs",
7370
"pretest": "npm run prepare",
74-
"test": "cross-env GOOGLE_CLOUD_USE_GRPC_JS=1 nyc mocha build/test && mocha build/test",
71+
"test": "nyc mocha build/test",
7572
"lint": "gts check && eslint samples/*.js samples/**/*.js",
7673
"clean": "gts clean",
7774
"compile": "tsc -p . && cp src/*.json build/src && cp src/*.js build/src",

src/bundlingCalls/bundleExecutor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131

32-
import {status} from 'grpc';
32+
import {status} from '@grpc/grpc-js';
3333

3434
import {SimpleCallbackFunction} from '../apitypes';
3535
import {GoogleError} from '../googleError';

src/bundlingCalls/task.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131

32-
import {status} from 'grpc';
32+
import {status} from '@grpc/grpc-js';
3333

3434
import {APICallback, GRPCCallResult, SimpleCallbackFunction} from '../apitypes';
3535
import {GoogleError} from '../googleError';

src/call.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131

32-
import {status} from 'grpc';
32+
import {status} from '@grpc/grpc-js';
3333

3434
import {
3535
APICallback,

src/gax.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ export function constructSettings(
667667
serviceName: string,
668668
clientConfig: ClientConfig,
669669
configOverrides: ClientConfig,
670-
retryNames: {[index: string]: number},
670+
retryNames: {},
671671
otherArgs?: {},
672672
promise?: PromiseConstructor
673673
) {

src/googleError.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131

32-
import {status} from 'grpc';
32+
import {status} from '@grpc/grpc-js';
3333

3434
export class GoogleError extends Error {
3535
code?: status;

src/grpc.ts

+18-34
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@
3030
*
3131
*/
3232

33-
import * as grpcProtoLoaderTypes from '@grpc/proto-loader'; // for types only
33+
import * as grpcProtoLoader from '@grpc/proto-loader';
3434
import * as fs from 'fs';
3535
import {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';
36-
import * as grpcTypes from 'grpc'; // for types only
37-
import * as grpcGcp from 'grpc-gcp';
36+
import * as grpc from '@grpc/grpc-js';
3837
import {OutgoingHttpHeaders} from 'http';
3938
import * as path from 'path';
4039
import * as protobuf from 'protobufjs';
@@ -56,8 +55,6 @@ const COMMON_PROTO_FILES = walk
5655
.filter(f => path.extname(f) === '.proto')
5756
.map(f => path.normalize(f).substring(googleProtoFilesDir.length + 1));
5857

59-
export {GrpcObject} from 'grpc';
60-
6158
export interface GrpcClientOptions extends GoogleAuthOptions {
6259
auth?: GoogleAuth;
6360
promise?: PromiseConstructor;
@@ -76,17 +73,17 @@ export interface Metadata {
7673
get: (key: {}) => {};
7774
}
7875

79-
export type GrpcModule = typeof grpcTypes & {
80-
status: {[index: string]: number};
81-
};
76+
export type GrpcModule = typeof grpc;
8277

8378
export interface ClientStubOptions {
8479
servicePath: string;
8580
port: number;
86-
sslCreds?: grpcTypes.ChannelCredentials;
81+
// TODO: use sslCreds?: grpc.ChannelCredentials;
82+
// tslint:disable-next-line no-any
83+
sslCreds?: any;
8784
}
8885

89-
export class ClientStub extends grpcTypes.Client {
86+
export class ClientStub extends grpc.Client {
9087
[name: string]: Function;
9188
}
9289

@@ -95,7 +92,6 @@ export class GrpcClient {
9592
promise: PromiseConstructor;
9693
grpc: GrpcModule;
9794
grpcVersion: string;
98-
grpcProtoLoader: typeof grpcProtoLoaderTypes;
9995

10096
/**
10197
* A class which keeps the context of gRPC and auth for the gRPC.
@@ -122,20 +118,16 @@ export class GrpcClient {
122118
this.grpc = options.grpc!;
123119
this.grpcVersion = '';
124120
} else {
125-
// EXPERIMENTAL: If GOOGLE_CLOUD_USE_GRPC_JS is set, use the JS-based
126-
// implementation of the gRPC client instead. Requires http2 (Node 8+).
127-
if (
128-
semver.gte(process.version, '8.13.0') &&
129-
!!process.env.GOOGLE_CLOUD_USE_GRPC_JS
130-
) {
131-
this.grpc = require('@grpc/grpc-js');
121+
if (semver.gte(process.version, '8.13.0')) {
122+
this.grpc = grpc;
132123
this.grpcVersion = require('@grpc/grpc-js/package.json').version;
133124
} else {
134-
this.grpc = require('grpc');
135-
this.grpcVersion = require('grpc/package.json').version;
125+
const errorMessage =
126+
'To use @grpc/grpc-js you must run your code on Node.js v8.13.0 or newer. Please see README if you need to use an older version. ' +
127+
'https://github.com/googleapis/gax-nodejs/blob/master/README.md';
128+
throw new Error(errorMessage);
136129
}
137130
}
138-
this.grpcProtoLoader = require('@grpc/proto-loader');
139131
}
140132

141133
/**
@@ -166,8 +158,8 @@ export class GrpcClient {
166158
* @param filename The path to the proto file.
167159
* @param options Options for loading the proto file.
168160
*/
169-
loadFromProto(filename: string, options: grpcProtoLoaderTypes.Options) {
170-
const packageDef = grpcProtoLoaderTypes.loadSync(filename, options);
161+
loadFromProto(filename: string, options: grpcProtoLoader.Options) {
162+
const packageDef = grpcProtoLoader.loadSync(filename, options);
171163
return this.grpc.loadPackageDefinition(packageDef);
172164
}
173165

@@ -289,20 +281,12 @@ export class GrpcClient {
289281
async createStub(CreateStub: typeof ClientStub, options: ClientStubOptions) {
290282
const serviceAddress = options.servicePath + ':' + options.port;
291283
const creds = await this._getCredentials(options);
292-
const grpcOptions: {[index: string]: {}} = {};
284+
const grpcOptions: {[index: string]: string} = {};
293285
Object.keys(options).forEach(key => {
294-
if (key.indexOf('grpc.') === 0) {
295-
grpcOptions[key] = options[key];
286+
if (key.startsWith('grpc.')) {
287+
grpcOptions[key.replace(/^grpc\./, '')] = options[key];
296288
}
297289
});
298-
const apiConfigDefinition = options['grpc_gcp.apiConfig'];
299-
if (apiConfigDefinition) {
300-
const apiConfig = grpcGcp.createGcpApiConfig(apiConfigDefinition);
301-
grpcOptions['channelFactoryOverride'] = grpcGcp.gcpChannelFactoryOverride;
302-
grpcOptions['callInvocationTransformer'] =
303-
grpcGcp.gcpCallInvocationTransformer;
304-
grpcOptions['gcpApiConfig'] = apiConfig;
305-
}
306290
const stub = new CreateStub(serviceAddress, creds, grpcOptions);
307291
return stub;
308292
}

src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export {
5757
GrpcClient,
5858
GrpcClientOptions,
5959
GrpcModule,
60-
GrpcObject,
6160
Metadata,
6261
MetadataValue,
6362
} from './grpc';

src/longRunningCalls/longrunning.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131

3232
import {EventEmitter} from 'events';
33-
import {status} from 'grpc';
33+
import {status} from '@grpc/grpc-js';
3434

3535
import {GaxCallPromise, ResultTuple} from '../apitypes';
3636
import {CancellablePromise} from '../call';

src/normalCalls/retries.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131

32-
import {status} from 'grpc';
32+
import {status} from '@grpc/grpc-js';
3333

3434
import {
3535
APICallback,

system-test/fixtures/google-gax-packaging-test-app/src/index.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@
1515
'use strict';
1616

1717
const assert = require('assert');
18-
19-
let grpc;
20-
if (process.env['GOOGLE_CLOUD_USE_GRPC_JS']) {
21-
grpc = require('@grpc/grpc-js');
22-
} else {
23-
grpc = require('grpc');
24-
}
18+
const grpc = require('@grpc/grpc-js');
2519

2620
// Import the clients for each version supported by this package.
2721
const gapic = Object.freeze({

0 commit comments

Comments
 (0)