Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: use @grpc/grpc-js instead of grpc #484

Merged
merged 8 commits into from
May 4, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ Application code will rarely need to use most of the classes within this library
$ npm install google-gax
```

## Supporting older version of Node.js

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
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
on a legacy gRPC library ([grpc](https://www.npmjs.com/package/grpc)) and pass the instance of gRPC to the client constructor:

```js
const grpc = require('grpc');
const client = new APIClient({ grpc }); // APIClient is the client class you use, e.g. SpeechClient, etc.
```

## Contributing
Contributions to this library are always welcome and highly encouraged. See the [CONTRIBUTING][contributing] documentation for more information on how to get started.

Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
"@grpc/proto-loader": "^0.5.0",
"duplexify": "^3.6.0",
"google-auth-library": "^3.0.0",
"grpc": "^1.20.2",
"grpc-gcp": "^0.1.1",
"is-stream-ended": "^0.1.4",
"lodash.at": "^4.6.0",
"lodash.has": "^4.5.2",
Expand All @@ -41,7 +39,6 @@
"@types/through2": "^2.0.33",
"chai": "*",
"codecov": "^3.1.0",
"cross-env": "^5.2.0",
"eslint": "^5.9.0",
"eslint-config-prettier": "^4.0.0",
"eslint-plugin-node": "^9.0.0",
Expand Down Expand Up @@ -71,7 +68,7 @@
"docs": "compodoc src/",
"gen-parser": "pegjs src/pathTemplateParser.pegjs",
"pretest": "npm run prepare",
"test": "cross-env GOOGLE_CLOUD_USE_GRPC_JS=1 nyc mocha build/test && mocha build/test",
"test": "nyc mocha build/test",
"lint": "gts check && eslint samples/*.js samples/**/*.js",
"clean": "gts clean",
"compile": "tsc -p . && cp src/*.json build/src && cp src/*.js build/src",
Expand Down
2 changes: 1 addition & 1 deletion src/bundlingCalls/bundleExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

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

import {SimpleCallbackFunction} from '../apitypes';
import {GoogleError} from '../googleError';
Expand Down
2 changes: 1 addition & 1 deletion src/bundlingCalls/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

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

import {APICallback, GRPCCallResult, SimpleCallbackFunction} from '../apitypes';
import {GoogleError} from '../googleError';
Expand Down
2 changes: 1 addition & 1 deletion src/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

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

import {
APICallback,
Expand Down
2 changes: 1 addition & 1 deletion src/gax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ export function constructSettings(
serviceName: string,
clientConfig: ClientConfig,
configOverrides: ClientConfig,
retryNames: {[index: string]: number},
retryNames: {},
otherArgs?: {},
promise?: PromiseConstructor
) {
Expand Down
2 changes: 1 addition & 1 deletion src/googleError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

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

export class GoogleError extends Error {
code?: status;
Expand Down
53 changes: 19 additions & 34 deletions src/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@
*
*/

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

export {GrpcObject} from 'grpc';

export interface GrpcClientOptions extends GoogleAuthOptions {
auth?: GoogleAuth;
promise?: PromiseConstructor;
Expand All @@ -76,17 +73,17 @@ export interface Metadata {
get: (key: {}) => {};
}

export type GrpcModule = typeof grpcTypes & {
status: {[index: string]: number};
};
export type GrpcModule = typeof grpc;

export interface ClientStubOptions {
servicePath: string;
port: number;
sslCreds?: grpcTypes.ChannelCredentials;
// TODO: use sslCreds?: grpc.ChannelCredentials;
// tslint:disable-next-line no-any
sslCreds?: any;
}

export class ClientStub extends grpcTypes.Client {
export class ClientStub extends grpc.Client {
[name: string]: Function;
}

Expand All @@ -95,7 +92,6 @@ export class GrpcClient {
promise: PromiseConstructor;
grpc: GrpcModule;
grpcVersion: string;
grpcProtoLoader: typeof grpcProtoLoaderTypes;

/**
* A class which keeps the context of gRPC and auth for the gRPC.
Expand All @@ -122,20 +118,17 @@ export class GrpcClient {
this.grpc = options.grpc!;
this.grpcVersion = '';
} else {
// EXPERIMENTAL: If GOOGLE_CLOUD_USE_GRPC_JS is set, use the JS-based
// implementation of the gRPC client instead. Requires http2 (Node 8+).
if (
semver.gte(process.version, '8.13.0') &&
!!process.env.GOOGLE_CLOUD_USE_GRPC_JS
) {
this.grpc = require('@grpc/grpc-js');
if (semver.gte(process.version, '8.13.0')) {
this.grpc = grpc;
this.grpcVersion = require('@grpc/grpc-js/package.json').version;
} else {
this.grpc = require('grpc');
this.grpcVersion = require('grpc/package.json').version;
const errorMessage =
'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. ' +
'https://github.com/googleapis/gax-nodejs/blob/master/README.md';
console.error(errorMessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to write to the console if we're throwing, do we?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to communicate the error in all the ways possible :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Justin, throw is probably sufficient, as it will end up in stderr and output essentially the same message (but with stack trace).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed console.error.

throw new Error(errorMessage);
}
}
this.grpcProtoLoader = require('@grpc/proto-loader');
}

/**
Expand Down Expand Up @@ -166,8 +159,8 @@ export class GrpcClient {
* @param filename The path to the proto file.
* @param options Options for loading the proto file.
*/
loadFromProto(filename: string, options: grpcProtoLoaderTypes.Options) {
const packageDef = grpcProtoLoaderTypes.loadSync(filename, options);
loadFromProto(filename: string, options: grpcProtoLoader.Options) {
const packageDef = grpcProtoLoader.loadSync(filename, options);
return this.grpc.loadPackageDefinition(packageDef);
}

Expand Down Expand Up @@ -289,20 +282,12 @@ export class GrpcClient {
async createStub(CreateStub: typeof ClientStub, options: ClientStubOptions) {
const serviceAddress = options.servicePath + ':' + options.port;
const creds = await this._getCredentials(options);
const grpcOptions: {[index: string]: {}} = {};
const grpcOptions: {[index: string]: string} = {};
Object.keys(options).forEach(key => {
if (key.indexOf('grpc.') === 0) {
grpcOptions[key] = options[key];
if (key.startsWith('grpc.')) {
grpcOptions[key.replace(/^grpc\./, '')] = options[key];
}
});
const apiConfigDefinition = options['grpc_gcp.apiConfig'];
if (apiConfigDefinition) {
const apiConfig = grpcGcp.createGcpApiConfig(apiConfigDefinition);
grpcOptions['channelFactoryOverride'] = grpcGcp.gcpChannelFactoryOverride;
grpcOptions['callInvocationTransformer'] =
grpcGcp.gcpCallInvocationTransformer;
grpcOptions['gcpApiConfig'] = apiConfig;
}
const stub = new CreateStub(serviceAddress, creds, grpcOptions);
return stub;
}
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export {
GrpcClient,
GrpcClientOptions,
GrpcModule,
GrpcObject,
Metadata,
MetadataValue,
} from './grpc';
Expand Down
2 changes: 1 addition & 1 deletion src/longRunningCalls/longrunning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/

import {EventEmitter} from 'events';
import {status} from 'grpc';
import {status} from '@grpc/grpc-js';

import {GaxCallPromise, ResultTuple} from '../apitypes';
import {CancellablePromise} from '../call';
Expand Down
2 changes: 1 addition & 1 deletion src/normalCalls/retries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

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

import {
APICallback,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@
'use strict';

const assert = require('assert');

let grpc;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like this update eliminates a lot of cruft.

if (process.env['GOOGLE_CLOUD_USE_GRPC_JS']) {
grpc = require('@grpc/grpc-js');
} else {
grpc = require('grpc');
}
const grpc = require('@grpc/grpc-js');

// Import the clients for each version supported by this package.
const gapic = Object.freeze({
Expand Down
145 changes: 0 additions & 145 deletions system-test/test.clientlibs.ts

This file was deleted.

Loading