Skip to content

Commit

Permalink
feat: Improve AuthClient Compatibility (#1641)
Browse files Browse the repository at this point in the history
* feat: Improve `AuthClient` Compatibility

* chore: fix export

* refactor: Use `AnyAuthClient` for backwards compatibility

* refactor: Improve `AuthClient` compat

* chore: Pin `cheerio`

* refactor: further improvements

* fix: type compile
  • Loading branch information
danielbankhead authored Aug 27, 2024
1 parent d74fa40 commit 4edd33d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 58 deletions.
1 change: 1 addition & 0 deletions gax/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/sinon": "^17.0.0",
"@types/uglify-js": "^3.17.0",
"c8": "^9.0.0",
"cheerio": "1.0.0-rc.12",
"codecov": "^3.1.0",
"execa": "^5.0.0",
"glob": "10.4.5",
Expand Down
4 changes: 2 additions & 2 deletions gax/src/clientInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import {GrpcClientOptions, ClientStubOptions} from './grpc';
import * as gax from './gax';
import {GoogleAuthOptions} from 'google-auth-library';
import {AuthClient, GoogleAuthOptions} from 'google-auth-library';
import {
BundleDescriptor,
LongrunningDescriptor,
Expand All @@ -30,7 +30,7 @@ import * as operationProtos from '../protos/operations';

export interface ClientOptions
extends GrpcClientOptions,
GoogleAuthOptions,
GoogleAuthOptions<AuthClient>,
ClientStubOptions {
libName?: string;
libVersion?: string;
Expand Down
48 changes: 17 additions & 31 deletions gax/src/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,7 @@ import * as protobuf from 'protobufjs';
import * as gax from './gax';
import * as routingHeader from './routingHeader';
import {Status} from './status';
import {
GoogleAuth,
OAuth2Client,
Compute,
JWT,
UserRefreshClient,
GoogleAuthOptions,
BaseExternalAccountClient,
} from 'google-auth-library';
import {GoogleAuth, AuthClient, AnyAuthClient} from 'google-auth-library';
import {OperationsClientBuilder} from './operationsClient';
import type {GrpcClientOptions, ClientStubOptions} from './grpc';
import {GaxCall, GRPCCall} from './apitypes';
Expand All @@ -45,6 +37,7 @@ import * as IamProtos from '../protos/iam_service';
import * as LocationProtos from '../protos/locations';
import * as operationsProtos from '../protos/operations';

export {AnyAuthClient as AuthClient};
export {FallbackServiceError};
export {PathTemplate} from './pathTemplate';
export {routingHeader};
Expand Down Expand Up @@ -85,15 +78,8 @@ export interface ServiceMethods {
[name: string]: protobuf.Method;
}

export type AuthClient =
| OAuth2Client
| Compute
| JWT
| UserRefreshClient
| BaseExternalAccountClient;

export class GrpcClient {
auth?: OAuth2Client | GoogleAuth;
auth?: AuthClient | GoogleAuth<AuthClient>;
authClient?: AuthClient;
fallback: boolean;
grpcVersion: string;
Expand All @@ -113,33 +99,33 @@ export class GrpcClient {
* gRPC-fallback version of GrpcClient
* Implements GrpcClient API for a browser using grpc-fallback protocol (sends serialized protobuf to HTTP/1 $rpc endpoint).
*
* @param {Object=} options.auth - An instance of OAuth2Client to use in browser, or an instance of GoogleAuth from google-auth-library
* @param {Object=} options.auth - An instance of AuthClient to use in browser, or an instance of GoogleAuth from google-auth-library
* to use in Node.js. Required for browser, optional for Node.js.
* @constructor
*/

constructor(
options: (GrpcClientOptions | {auth: OAuth2Client}) & {
options: (GrpcClientOptions | {auth: AuthClient}) & {
/**
* Fallback mode to use instead of gRPC.
* A string is accepted for compatibility, all non-empty string values enable the HTTP REST fallback.
*/
fallback?: boolean | string;
} = {}
) {
if (!isNodeJS()) {
if (!options.auth) {
throw new Error(
JSON.stringify(options) +
'You need to pass auth instance to use gRPC-fallback client in browser or other non-Node.js environments. Use OAuth2Client from google-auth-library.'
);
}
this.auth = options.auth as OAuth2Client;
if (options.auth) {
this.auth = options.auth;
} else if ('authClient' in options) {
this.auth = options.authClient;
} else if (!isNodeJS()) {
throw new Error(
JSON.stringify(options) +
'You need to pass auth instance to use gRPC-fallback client in browser or other non-Node.js environments. Provide a `GoogleAuth` or `AuthClient` instance from `google-auth-library`.'
);
} else {
this.auth =
(options.auth as GoogleAuth) ||
new GoogleAuth(options as GoogleAuthOptions);
this.auth = new GoogleAuth(options as GrpcClientOptions);
}

this.fallback = options.fallback ? true : false;
this.grpcVersion = require('../../package.json').version;
this.httpRules = (options as GrpcClientOptions).httpRules;
Expand Down Expand Up @@ -264,7 +250,7 @@ export class GrpcClient {

/**
* gRPC-fallback version of createStub
* Creates a gRPC-fallback stub with authentication headers built from supplied OAuth2Client instance
* Creates a gRPC-fallback stub with authentication headers built from supplied AuthClient instance
*
* @param {function} CreateStub - The constructor function of the stub.
* @param {Object} service - A protobufjs Service object (as returned by lookupService)
Expand Down
8 changes: 4 additions & 4 deletions gax/src/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as grpcProtoLoader from '@grpc/proto-loader';
import {execFile} from 'child_process';
import * as fs from 'fs';
import {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';
import {GoogleAuth, GoogleAuthOptions, AuthClient} from 'google-auth-library';
import * as grpc from '@grpc/grpc-js';
import * as os from 'os';
import {join} from 'path';
Expand All @@ -44,8 +44,8 @@ const COMMON_PROTO_FILES: string[] = commonProtoFiles.map(file =>
file.replace(/[/\\]/g, path.sep)
);

export interface GrpcClientOptions extends GoogleAuthOptions {
auth?: GoogleAuth;
export interface GrpcClientOptions extends GoogleAuthOptions<AuthClient> {
auth?: GoogleAuth<AuthClient>;
grpc?: GrpcModule;
protoJson?: protobuf.Root;
httpRules?: Array<google.api.IHttpRule>;
Expand Down Expand Up @@ -113,7 +113,7 @@ export class ClientStub extends grpc.Client {
}

export class GrpcClient {
auth: GoogleAuth;
auth: GoogleAuth<AuthClient>;
grpc: GrpcModule;
grpcVersion: string;
fallback: boolean | 'rest' | 'proto';
Expand Down
4 changes: 2 additions & 2 deletions gax/src/iamService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import * as gax from './gax';
import type {GrpcClient, ClientStubOptions} from './grpc';
import type {GrpcClient as FallbackGrpcClient} from './fallback';
import {createApiCall} from './createApiCall';
import {GoogleAuth, OAuth2Client} from 'google-auth-library';
import {GoogleAuth, AuthClient} from 'google-auth-library';
import {ProjectIdCallback} from 'google-auth-library/build/src/auth/googleauth';
import * as routingHeader from './routingHeader';
import * as gapicConfig from './iam_policy_service_client_config.json';
Expand All @@ -40,7 +40,7 @@ export class IamClient {
private _defaults: {[method: string]: gax.CallSettings};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _protos: any;
auth?: GoogleAuth | OAuth2Client;
auth?: GoogleAuth<AuthClient> | AuthClient;
descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}};
innerApiCalls: {[name: string]: Function} = {};
iamPolicyStub?: Promise<{[name: string]: Function}>;
Expand Down
4 changes: 2 additions & 2 deletions gax/src/operationsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type {GoogleAuth, OAuth2Client} from 'google-auth-library';
import {GoogleAuth, AuthClient} from 'google-auth-library';
import {ProjectIdCallback} from 'google-auth-library/build/src/auth/googleauth';
import type {ClientOptions, Callback} from './clientInterface';

Expand Down Expand Up @@ -62,7 +62,7 @@ export const ALL_SCOPES: string[] = [];
* @class
*/
export class OperationsClient {
auth?: GoogleAuth | OAuth2Client;
auth?: GoogleAuth<AuthClient> | AuthClient;
innerApiCalls: {[name: string]: Function};
descriptor: {[method: string]: PageDescriptor};
operationsStub: Promise<{[method: string]: Function}>;
Expand Down
19 changes: 2 additions & 17 deletions gax/test/unit/regapic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,12 @@ import * as stream from 'stream';
import echoProtoJson = require('../fixtures/echo.json');
import {GrpcClient} from '../../src/fallback';
import * as transcoding from '../../src/transcoding';
import {OAuth2Client} from 'google-auth-library';
import {GrpcClientOptions} from '../../src';
import {PassThroughClient} from 'google-auth-library';
import {StreamArrayParser} from '../../src/streamArrayParser';

const authClient = {
async getRequestHeaders() {
return {Authorization: 'Bearer SOME_TOKEN'};
},
};

const authStub = {
async getClient() {
return authClient;
},
};

const opts = {
auth: authStub,
authClient: new PassThroughClient(),
fallback: 'rest', // enabling REGAPIC
} as unknown as (GrpcClientOptions | {auth: OAuth2Client}) & {
fallback?: boolean | 'rest' | 'proto';
};

describe('REGAPIC', () => {
Expand Down

0 comments on commit 4edd33d

Please sign in to comment.