Skip to content

Commit

Permalink
Merge branch 'master' into policy-bot-e0860c50-329f-4dc1-a67a-10257c5…
Browse files Browse the repository at this point in the history
…7dc4f
  • Loading branch information
alexander-fenster authored Apr 29, 2021
2 parents bce5b26 + 10a0a0d commit 1dd0fa7
Show file tree
Hide file tree
Showing 13 changed files with 1,534 additions and 55 deletions.
13 changes: 13 additions & 0 deletions .github/generated-files-bot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
generatedFiles:
- path: '.kokoro/**'
message: '`.kokoro` files are templated and should be updated in [`synthtool`](https://github.com/googleapis/synthtool)'
- path: '.github/CODEOWNERS'
message: 'CODEOWNERS should instead be modified via the `codeowner_team` property in .repo-metadata.json'
- path: '.github/workflows/**'
message: '`.github/workflows` (GitHub Actions) should be updated in [`synthtool`](https://github.com/googleapis/synthtool)'
- path: '.github/generated-files-bot.+(yml|yaml)'
message: '`.github/generated-files-bot.(yml|yaml)` should be updated in [`synthtool`](https://github.com/googleapis/synthtool)'
- path: 'README.md'
message: '`README.md` is managed by [`synthtool`](https://github.com/googleapis/synthtool). However, a partials file can be used to update the README, e.g.: https://github.com/googleapis/nodejs-storage/blob/master/.readme-partials.yaml'
- path: 'samples/README.md'
message: '`samples/README.md` is managed by [`synthtool`](https://github.com/googleapis/synthtool). However, a partials file can be used to update the README, e.g.: https://github.com/googleapis/nodejs-storage/blob/master/.readme-partials.yaml'
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
on:
push:
branches:
- $default-branch
- master
pull_request:
name: ci
jobs:
Expand Down
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@

[1]: https://www.npmjs.com/package/gax-nodejs?activeTab=versions

## [2.12.0](https://www.github.com/googleapis/gax-nodejs/compare/v2.11.3...v2.12.0) (2021-04-29)


### Features

* GrpcClient.loadProtoJSON to load protobuf.js JSON proto ([#985](https://www.github.com/googleapis/gax-nodejs/issues/985)) ([819b447](https://www.github.com/googleapis/gax-nodejs/commit/819b447964a635ef2ac75769c0f6eadbd202ddfa))


### Bug Fixes

* **deps:** update dependency @grpc/grpc-js to ~1.3.0 ([#990](https://www.github.com/googleapis/gax-nodejs/issues/990)) ([f131ee0](https://www.github.com/googleapis/gax-nodejs/commit/f131ee0fb0411f57f43777ca7e3dcec6556115f2))

### [2.11.3](https://www.github.com/googleapis/gax-nodejs/compare/v2.11.2...v2.11.3) (2021-04-22)


### Bug Fixes

* fallback can now be a string ([#983](https://www.github.com/googleapis/gax-nodejs/issues/983)) ([d4e495a](https://www.github.com/googleapis/gax-nodejs/commit/d4e495af46176b8dd74595108f25a6df99ee042d))
* imports for node-fetch when using webpack ([#972](https://www.github.com/googleapis/gax-nodejs/issues/972)) ([505c6b8](https://www.github.com/googleapis/gax-nodejs/commit/505c6b8a3a53f1875617a2258869e4646a0e439b))

### [2.11.2](https://www.github.com/googleapis/gax-nodejs/compare/v2.11.1...v2.11.2) (2021-03-11)


Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "google-gax",
"version": "2.11.2",
"version": "2.12.0",
"description": "Google API Extensions",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
Expand All @@ -14,15 +14,16 @@
"compileProtos": "build/tools/compileProtos.js"
},
"dependencies": {
"@grpc/grpc-js": "~1.2.0",
"@grpc/proto-loader": "^0.5.1",
"@grpc/grpc-js": "~1.3.0",
"@grpc/proto-loader": "^0.6.1",
"@types/long": "^4.0.0",
"abort-controller": "^3.0.0",
"duplexify": "^4.0.0",
"fast-text-encoding": "^1.0.3",
"google-auth-library": "^7.0.2",
"is-stream-ended": "^0.1.4",
"node-fetch": "^2.6.1",
"object-hash": "^2.1.1",
"protobufjs": "^6.10.2",
"retry-request": "^4.0.0"
},
Expand All @@ -36,6 +37,7 @@
"@types/ncp": "^2.0.1",
"@types/node": "^10.3.2",
"@types/node-fetch": "^2.5.4",
"@types/object-hash": "^2.1.0",
"@types/proxyquire": "^1.3.28",
"@types/pumpify": "^1.4.1",
"@types/rimraf": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"*.js"
],
"dependencies": {
"google-gax": "^2.11.2"
"google-gax": "^2.12.0"
},
"devDependencies": {
"c8": "^7.0.0",
Expand Down
25 changes: 23 additions & 2 deletions src/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
GoogleAuthOptions,
BaseExternalAccountClient,
} from 'google-auth-library';
import * as objectHash from 'object-hash';
import {OperationsClientBuilder} from './operationsClient';
import {GrpcClientOptions, ClientStubOptions} from './grpc';
import {GaxCall, GRPCCall} from './apitypes';
Expand Down Expand Up @@ -104,6 +105,15 @@ export class GrpcClient {
| BaseExternalAccountClient;
fallback: boolean | 'rest' | 'proto';
grpcVersion: string;
private static protoCache = new Map<string, protobuf.Root>();

/**
* In rare cases users might need to deallocate all memory consumed by loaded protos.
* This method will delete the proto cache content.
*/
static clearProtoCache() {
GrpcClient.protoCache.clear();
}

/**
* gRPC-fallback version of GrpcClient
Expand Down Expand Up @@ -147,6 +157,17 @@ export class GrpcClient {
return rootObject;
}

loadProtoJSON(json: protobuf.INamespace, ignoreCache = false) {
const hash = objectHash(json);
const cached = GrpcClient.protoCache.get(hash);
if (cached && !ignoreCache) {
return cached;
}
const root = protobuf.Root.fromJSON(json);
GrpcClient.protoCache.set(hash, root);
return root;
}

private getServiceMethods(service: protobuf.Service) {
const methods = Object.keys(service.methods);

Expand Down Expand Up @@ -303,7 +324,7 @@ export class GrpcClient {
].apply(serviceStub, [req, callback]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let cancelController: AbortController, cancelSignal: any;
if (isBrowser && typeof AbortController !== 'undefined') {
if (isBrowser() || typeof AbortController !== 'undefined') {
// eslint-disable-next-line no-undef
cancelController = new AbortController();
} else {
Expand Down Expand Up @@ -396,7 +417,7 @@ export class GrpcClient {
: ((nodeFetch as unknown) as NodeFetchType);
const fetchRequest = {
headers,
body: data,
body: data as string | undefined,
method: httpMethod,
signal: cancelSignal,
};
Expand Down
43 changes: 31 additions & 12 deletions src/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as grpc from '@grpc/grpc-js';
import {OutgoingHttpHeaders} from 'http';
import * as path from 'path';
import * as protobuf from 'protobufjs';
import * as objectHash from 'object-hash';

import * as gax from './gax';
import {ClientOptions} from '@grpc/grpc-js/build/src/client';
Expand Down Expand Up @@ -165,6 +166,21 @@ export class GrpcClient {
return credentials;
}

private static defaultOptions() {
// This set of @grpc/proto-loader options
// 'closely approximates the existing behavior of grpc.load'
const includeDirs = INCLUDE_DIRS.slice();
const options = {
keepCase: false,
longs: String,
enums: String,
defaults: true,
oneofs: true,
includeDirs,
};
return options;
}

/**
* Loads the gRPC service from the proto file(s) at the given path and with the
* given options. Caches the loaded protos so the subsequent loads don't do
Expand Down Expand Up @@ -220,18 +236,8 @@ export class GrpcClient {
if (Array.isArray(filename) && filename.length === 0) {
return {};
}
// This set of @grpc/proto-loader options
// 'closely approximates the existing behavior of grpc.load'
const includeDirs = INCLUDE_DIRS.slice();
includeDirs.unshift(protoPath);
const options = {
keepCase: false,
longs: String,
enums: String,
defaults: true,
oneofs: true,
includeDirs,
};
const options = GrpcClient.defaultOptions();
options.includeDirs.unshift(protoPath);
return this.loadFromProto(filename, options, ignoreCache);
}

Expand All @@ -244,6 +250,19 @@ export class GrpcClient {
throw new Error(filename + ' could not be found in ' + protoPath);
}

loadProtoJSON(json: protobuf.INamespace, ignoreCache = false) {
const hash = objectHash(json);
const cached = GrpcClient.protoCache.get(hash);
if (cached && !ignoreCache) {
return cached;
}
const options = GrpcClient.defaultOptions();
const packageDefinition = grpcProtoLoader.fromJSON(json, options);
const grpcPackage = this.grpc.loadPackageDefinition(packageDefinition);
GrpcClient.protoCache.set(hash, grpcPackage);
return grpcPackage;
}

metadataBuilder(headers: OutgoingHttpHeaders) {
const Metadata = this.grpc.Metadata;
const baseMetadata = new Metadata();
Expand Down
19 changes: 2 additions & 17 deletions src/iamService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import * as routingHeader from './routingHeader';
import * as gapicConfig from './iam_policy_service_client_config.json';
import * as protos from '../protos/iam_service';
import * as fallback from './fallback';
import * as path from 'path';
import {Descriptors, ClientOptions, Callback} from './clientInterface';
let version = require('../../package.json').version;
import jsonProtos = require('../protos/iam_service.json');

/**
* Google Cloud IAM Client.
Expand Down Expand Up @@ -88,22 +88,7 @@ export class IamClient {
clientHeader.push(`${opts.libName}/${opts.libVersion}`);
}
// Load the applicable protos.
// For Node.js, pass the path to JSON proto file.
// For browsers, pass the JSON content.

const nodejsProtoPath = path.join(
__dirname,
'..',
'..',
'protos',
'iam_service.json'
);
this._protos = this._gaxGrpc.loadProto(
opts.fallback
? // eslint-disable-next-line @typescript-eslint/no-var-requires
require('../../protos/iam_service.json')
: nodejsProtoPath
);
this._protos = this._gaxGrpc.loadProtoJSON(jsonProtos);
// Put together the default options sent with requests.
this._defaults = gaxGrpc.constructSettings(
'google.iam.v1.IAMPolicy',
Expand Down
4 changes: 2 additions & 2 deletions synth.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"git": {
"name": ".",
"remote": "https://github.com/googleapis/gax-nodejs.git",
"sha": "1e15e2786b482630988cdd09705726f342e626d8"
"sha": "d4e495af46176b8dd74595108f25a6df99ee042d"
}
},
{
"git": {
"name": "synthtool",
"remote": "https://github.com/googleapis/synthtool.git",
"sha": "c6706ee5d693e9ae5967614170732646590d8374"
"sha": "e6f3d54be015a394b6ab5a25903ec09062a2b424"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,7 @@ class EchoClient {
// For Node.js, pass the path to JSON proto file.
// For browsers, pass the JSON content.

const nodejsProtoPath = path.join(
__dirname,
'..',
'..',
'protos',
'protos.json'
);
const protos = gaxGrpc.loadProto(
global.isBrowser || opts.fallback
? require('../../protos/protos.json')
: nodejsProtoPath
);
const protos = gaxGrpc.loadProtoJSON(require('../../protos/protos.json'));

// Some of the methods on this service return "paged" results,
// (e.g. 50 results at a time, with tokens to get subsequent
Expand All @@ -136,10 +125,7 @@ class EchoClient {
chat: new gaxModule.StreamDescriptor(gaxModule.StreamType.BIDI_STREAMING),
};

const protoFilesRoot =
global.isBrowser || opts.fallback
? gaxModule.protobuf.Root.fromJSON(require('../../protos/protos.json'))
: gaxModule.protobuf.loadSync(nodejsProtoPath);
const protoFilesRoot = gaxModule.protobuf.Root.fromJSON(require('../../protos/protos.json'));

// This API contains "long-running operations", which return a
// an Operation object that allows for tracking of the operation,
Expand Down
Loading

0 comments on commit 1dd0fa7

Please sign in to comment.