From 81bb5f75f77241ebc07ccf953dacdbd55cfbcdcf Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Wed, 7 Aug 2024 12:36:58 -0700 Subject: [PATCH 1/7] feat: Improve `AuthClient` Compatibility --- gax/src/fallback.ts | 33 ++++++++------------------------- gax/src/iamService.ts | 4 ++-- gax/src/operationsClient.ts | 4 ++-- gax/test/unit/regapic.ts | 4 ++-- 4 files changed, 14 insertions(+), 31 deletions(-) diff --git a/gax/src/fallback.ts b/gax/src/fallback.ts index e493bbb47..97e8134be 100644 --- a/gax/src/fallback.ts +++ b/gax/src/fallback.ts @@ -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, GoogleAuthOptions, AuthClient} from 'google-auth-library'; import {OperationsClientBuilder} from './operationsClient'; import type {GrpcClientOptions, ClientStubOptions} from './grpc'; import {GaxCall, GRPCCall} from './apitypes'; @@ -85,15 +77,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; fallback: boolean; grpcVersion: string; @@ -113,13 +98,13 @@ 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. @@ -131,14 +116,12 @@ export class GrpcClient { 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.' + '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`.' ); } - this.auth = options.auth as OAuth2Client; + this.auth = options.auth; } else { - this.auth = - (options.auth as GoogleAuth) || - new GoogleAuth(options as GoogleAuthOptions); + this.auth = options.auth || new GoogleAuth(options as GoogleAuthOptions); } this.fallback = options.fallback ? true : false; this.grpcVersion = require('../../package.json').version; @@ -264,7 +247,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) diff --git a/gax/src/iamService.ts b/gax/src/iamService.ts index 758536775..63beba82f 100644 --- a/gax/src/iamService.ts +++ b/gax/src/iamService.ts @@ -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'; @@ -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; descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}}; innerApiCalls: {[name: string]: Function} = {}; iamPolicyStub?: Promise<{[name: string]: Function}>; diff --git a/gax/src/operationsClient.ts b/gax/src/operationsClient.ts index 2941ff436..ad0fd05fa 100644 --- a/gax/src/operationsClient.ts +++ b/gax/src/operationsClient.ts @@ -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'; @@ -62,7 +62,7 @@ export const ALL_SCOPES: string[] = []; * @class */ export class OperationsClient { - auth?: GoogleAuth | OAuth2Client; + auth?: GoogleAuth | AuthClient; innerApiCalls: {[name: string]: Function}; descriptor: {[method: string]: PageDescriptor}; operationsStub: Promise<{[method: string]: Function}>; diff --git a/gax/test/unit/regapic.ts b/gax/test/unit/regapic.ts index 9ebf08019..bb9628438 100644 --- a/gax/test/unit/regapic.ts +++ b/gax/test/unit/regapic.ts @@ -24,7 +24,7 @@ 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 {AuthClient} from 'google-auth-library'; import {GrpcClientOptions} from '../../src'; import {StreamArrayParser} from '../../src/streamArrayParser'; @@ -43,7 +43,7 @@ const authStub = { const opts = { auth: authStub, fallback: 'rest', // enabling REGAPIC -} as unknown as (GrpcClientOptions | {auth: OAuth2Client}) & { +} as unknown as (GrpcClientOptions | {auth: AuthClient}) & { fallback?: boolean | 'rest' | 'proto'; }; From 69aab6c7d04570760c8d6ada7552952eeeb31087 Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Wed, 7 Aug 2024 12:44:02 -0700 Subject: [PATCH 2/7] chore: fix export --- gax/src/fallback.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/gax/src/fallback.ts b/gax/src/fallback.ts index 97e8134be..58dfdaf47 100644 --- a/gax/src/fallback.ts +++ b/gax/src/fallback.ts @@ -37,6 +37,7 @@ import * as IamProtos from '../protos/iam_service'; import * as LocationProtos from '../protos/locations'; import * as operationsProtos from '../protos/operations'; +export {AuthClient}; export {FallbackServiceError}; export {PathTemplate} from './pathTemplate'; export {routingHeader}; From 76e6d90741bf9e1113007dfc8c52cfe616efddf1 Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Thu, 22 Aug 2024 13:36:22 -0700 Subject: [PATCH 3/7] refactor: Use `AnyAuthClient` for backwards compatibility --- gax/src/fallback.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/gax/src/fallback.ts b/gax/src/fallback.ts index 58dfdaf47..cc39b2733 100644 --- a/gax/src/fallback.ts +++ b/gax/src/fallback.ts @@ -20,7 +20,12 @@ import * as protobuf from 'protobufjs'; import * as gax from './gax'; import * as routingHeader from './routingHeader'; import {Status} from './status'; -import {GoogleAuth, GoogleAuthOptions, AuthClient} from 'google-auth-library'; +import { + GoogleAuth, + GoogleAuthOptions, + AuthClient, + AnyAuthClient, +} from 'google-auth-library'; import {OperationsClientBuilder} from './operationsClient'; import type {GrpcClientOptions, ClientStubOptions} from './grpc'; import {GaxCall, GRPCCall} from './apitypes'; @@ -37,7 +42,7 @@ import * as IamProtos from '../protos/iam_service'; import * as LocationProtos from '../protos/locations'; import * as operationsProtos from '../protos/operations'; -export {AuthClient}; +export {AnyAuthClient as AuthClient}; export {FallbackServiceError}; export {PathTemplate} from './pathTemplate'; export {routingHeader}; From 1ad08404280332de03769434587be8bef6967093 Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Thu, 22 Aug 2024 13:58:22 -0700 Subject: [PATCH 4/7] refactor: Improve `AuthClient` compat --- gax/src/fallback.ts | 2 +- gax/src/grpc.ts | 6 +++--- gax/src/iamService.ts | 2 +- gax/src/operationsClient.ts | 2 +- gax/test/unit/regapic.ts | 21 ++++----------------- 5 files changed, 10 insertions(+), 23 deletions(-) diff --git a/gax/src/fallback.ts b/gax/src/fallback.ts index cc39b2733..14ab9edb6 100644 --- a/gax/src/fallback.ts +++ b/gax/src/fallback.ts @@ -84,7 +84,7 @@ export interface ServiceMethods { } export class GrpcClient { - auth?: AuthClient | GoogleAuth; + auth?: AuthClient | GoogleAuth; authClient?: AuthClient; fallback: boolean; grpcVersion: string; diff --git a/gax/src/grpc.ts b/gax/src/grpc.ts index f3db7b735..12e6a5509 100644 --- a/gax/src/grpc.ts +++ b/gax/src/grpc.ts @@ -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'; @@ -45,7 +45,7 @@ const COMMON_PROTO_FILES: string[] = commonProtoFiles.map(file => ); export interface GrpcClientOptions extends GoogleAuthOptions { - auth?: GoogleAuth; + auth?: GoogleAuth; grpc?: GrpcModule; protoJson?: protobuf.Root; httpRules?: Array; @@ -113,7 +113,7 @@ export class ClientStub extends grpc.Client { } export class GrpcClient { - auth: GoogleAuth; + auth: GoogleAuth; grpc: GrpcModule; grpcVersion: string; fallback: boolean | 'rest' | 'proto'; diff --git a/gax/src/iamService.ts b/gax/src/iamService.ts index 63beba82f..87e096e79 100644 --- a/gax/src/iamService.ts +++ b/gax/src/iamService.ts @@ -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 | AuthClient; + auth?: GoogleAuth | AuthClient; descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}}; innerApiCalls: {[name: string]: Function} = {}; iamPolicyStub?: Promise<{[name: string]: Function}>; diff --git a/gax/src/operationsClient.ts b/gax/src/operationsClient.ts index ad0fd05fa..b821b5c01 100644 --- a/gax/src/operationsClient.ts +++ b/gax/src/operationsClient.ts @@ -62,7 +62,7 @@ export const ALL_SCOPES: string[] = []; * @class */ export class OperationsClient { - auth?: GoogleAuth | AuthClient; + auth?: GoogleAuth | AuthClient; innerApiCalls: {[name: string]: Function}; descriptor: {[method: string]: PageDescriptor}; operationsStub: Promise<{[method: string]: Function}>; diff --git a/gax/test/unit/regapic.ts b/gax/test/unit/regapic.ts index bb9628438..c9a8ebd9f 100644 --- a/gax/test/unit/regapic.ts +++ b/gax/test/unit/regapic.ts @@ -24,27 +24,14 @@ import * as stream from 'stream'; import echoProtoJson = require('../fixtures/echo.json'); import {GrpcClient} from '../../src/fallback'; import * as transcoding from '../../src/transcoding'; -import {AuthClient} from 'google-auth-library'; -import {GrpcClientOptions} from '../../src'; +import {GoogleAuth, 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, + auth: new GoogleAuth({ + authClient: new PassThroughClient({}), + }), fallback: 'rest', // enabling REGAPIC -} as unknown as (GrpcClientOptions | {auth: AuthClient}) & { - fallback?: boolean | 'rest' | 'proto'; }; describe('REGAPIC', () => { From c6c8d4737939d09870fba47fe58219adcde8793e Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Thu, 22 Aug 2024 13:58:45 -0700 Subject: [PATCH 5/7] chore: Pin `cheerio` --- gax/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/gax/package.json b/gax/package.json index f0f07ca5b..0233722b6 100644 --- a/gax/package.json +++ b/gax/package.json @@ -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", From d9729873f22f5f49dba10a7bd571241fca86a661 Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Thu, 22 Aug 2024 16:00:28 -0700 Subject: [PATCH 6/7] refactor: further improvements --- gax/src/fallback.ts | 25 +++++++++++-------------- gax/src/grpc.ts | 2 +- gax/test/unit/regapic.ts | 6 ++---- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/gax/src/fallback.ts b/gax/src/fallback.ts index 14ab9edb6..cfc447a59 100644 --- a/gax/src/fallback.ts +++ b/gax/src/fallback.ts @@ -20,12 +20,7 @@ import * as protobuf from 'protobufjs'; import * as gax from './gax'; import * as routingHeader from './routingHeader'; import {Status} from './status'; -import { - GoogleAuth, - GoogleAuthOptions, - AuthClient, - AnyAuthClient, -} 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'; @@ -118,17 +113,19 @@ export class GrpcClient { 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. Provide a `GoogleAuth` or `AuthClient` instance from `google-auth-library`.' - ); - } + 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 || 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; diff --git a/gax/src/grpc.ts b/gax/src/grpc.ts index 12e6a5509..375a923a4 100644 --- a/gax/src/grpc.ts +++ b/gax/src/grpc.ts @@ -44,7 +44,7 @@ const COMMON_PROTO_FILES: string[] = commonProtoFiles.map(file => file.replace(/[/\\]/g, path.sep) ); -export interface GrpcClientOptions extends GoogleAuthOptions { +export interface GrpcClientOptions extends GoogleAuthOptions { auth?: GoogleAuth; grpc?: GrpcModule; protoJson?: protobuf.Root; diff --git a/gax/test/unit/regapic.ts b/gax/test/unit/regapic.ts index c9a8ebd9f..0569d21fe 100644 --- a/gax/test/unit/regapic.ts +++ b/gax/test/unit/regapic.ts @@ -24,13 +24,11 @@ import * as stream from 'stream'; import echoProtoJson = require('../fixtures/echo.json'); import {GrpcClient} from '../../src/fallback'; import * as transcoding from '../../src/transcoding'; -import {GoogleAuth, PassThroughClient} from 'google-auth-library'; +import {PassThroughClient} from 'google-auth-library'; import {StreamArrayParser} from '../../src/streamArrayParser'; const opts = { - auth: new GoogleAuth({ - authClient: new PassThroughClient({}), - }), + authClient: new PassThroughClient(), fallback: 'rest', // enabling REGAPIC }; From 85b7eef546e2344cdb2d0cd1aaa63fc3ea9e64e4 Mon Sep 17 00:00:00 2001 From: Daniel Bankhead Date: Thu, 22 Aug 2024 16:02:42 -0700 Subject: [PATCH 7/7] fix: type compile --- gax/src/clientInterface.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gax/src/clientInterface.ts b/gax/src/clientInterface.ts index 810892762..17d671642 100644 --- a/gax/src/clientInterface.ts +++ b/gax/src/clientInterface.ts @@ -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, @@ -30,7 +30,7 @@ import * as operationProtos from '../protos/operations'; export interface ClientOptions extends GrpcClientOptions, - GoogleAuthOptions, + GoogleAuthOptions, ClientStubOptions { libName?: string; libVersion?: string;