Skip to content

Commit

Permalink
feat: do not use legacy grpc loadObject (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-fenster committed May 1, 2019
1 parent b9e7073 commit 0ed2084
Showing 1 changed file with 2 additions and 34 deletions.
36 changes: 2 additions & 34 deletions src/grpc.ts
Expand Up @@ -78,7 +78,6 @@ export interface Metadata {

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

export interface ClientStubOptions {
Expand Down Expand Up @@ -128,11 +127,9 @@ export class GrpcClient {
if (semver.gte(process.version, '8.13.0') &&
!!process.env.GOOGLE_CLOUD_USE_GRPC_JS) {
this.grpc = require('@grpc/grpc-js');
this.grpc.isLegacy = false;
this.grpcVersion = require('@grpc/grpc-js/package.json').version;
} else {
this.grpc = require('grpc');
this.grpc.isLegacy = true;
this.grpcVersion = require('grpc/package.json').version;
}
}
Expand Down Expand Up @@ -172,28 +169,13 @@ export class GrpcClient {

/**
* Load grpc proto service from a filename hooking in googleapis common protos
* when necessary. Uses legacy grpc.loadObject.
* when necessary.
* @param {String} protoPath - The directory to search for the protofile.
* @param {String} filename - The filename of the proto to be loaded.
* @return {Object<string, *>} The gRPC loaded result (the toplevel namespace
* object).
*/
protected loadProtoLegacy(protoPath: string, filename: string) {
const resolvedPath = GrpcClient._resolveFile(protoPath, filename);
const retval = this.grpc.loadObject(
protobuf.loadSync(resolvedPath, new GoogleProtoFilesRoot()));
return retval;
}

/**
* Load grpc proto service from a filename hooking in googleapis common protos
* when necessary. Uses the new shiny @grpc/proto-loader.
* @param {String} protoPath - The directory to search for the protofile.
* @param {String} filename - The filename of the proto to be loaded.
* @return {Object<string, *>} The gRPC loaded result (the toplevel namespace
* object).
*/
protected loadProtoGrpcJs(protoPath: string, filename: string) {
loadProto(protoPath: string, filename: string) {
// This set of @grpc/proto-loader options
// 'closely approximates the existing behavior of grpc.load'
const includeDirs = INCLUDE_DIRS.slice();
Expand All @@ -209,20 +191,6 @@ export class GrpcClient {
return this.loadFromProto(filename, options);
}

/**
* Load grpc proto service from a filename hooking in googleapis common protos
* when necessary. Can use either legacy grpc.loadProto, or new
* @grpc/proto-loader, depending on which gRPC backend is used.
* @param {String} protoPath - The directory to search for the protofile.
* @param {String} filename - The filename of the proto to be loaded.
* @return {Object<string, *>} The gRPC loaded result (the toplevel namespace
* object).
*/
loadProto(protoPath: string, filename: string) {
return this.grpc.isLegacy ? this.loadProtoLegacy(protoPath, filename) :
this.loadProtoGrpcJs(protoPath, filename);
}

static _resolveFile(protoPath: string, filename: string) {
if (fs.existsSync(path.join(protoPath, filename))) {
return path.join(protoPath, filename);
Expand Down

0 comments on commit 0ed2084

Please sign in to comment.