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!: Update google-auth-library to v10 #1702

Merged
merged 11 commits into from
Feb 27, 2025
10 changes: 5 additions & 5 deletions gax/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
"@types/long": "^5.0.0",
"abort-controller": "^3.0.0",
"duplexify": "^4.0.0",
"google-auth-library": "^9.3.0",
"google-auth-library": "^10.0.0-rc.0",
"node-fetch": "^2.7.0",
"object-hash": "^3.0.0",
"proto3-json-serializer": "^2.0.2",
"protobufjs": "^7.3.2",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was an auto-repair from npm

"retry-request": "^7.0.0",
"uuid": "^9.0.1",
"protobufjs": "^7.3.2"
"uuid": "^9.0.1"
},
"devDependencies": {
"@types/uuid": "^9.0.7",
"@babel/plugin-proposal-private-methods": "^7.18.6",
"@types/mocha": "^9.0.0",
"@types/ncp": "^2.0.1",
Expand All @@ -35,6 +34,7 @@
"@types/pumpify": "^1.4.1",
"@types/sinon": "^17.0.0",
"@types/uglify-js": "^3.17.0",
"@types/uuid": "^9.0.7",
"c8": "^9.0.0",
"cheerio": "1.0.0-rc.12",
"codecov": "^3.1.0",
Expand All @@ -47,8 +47,8 @@
"jsdoc-fresh": "^3.0.0",
"jsdoc-region-tag": "^3.0.0",
"linkinator": "^4.0.0",
"lru-cache": "10.4.3",
"long": "^5.0.0",
"lru-cache": "10.4.3",
"mkdirp": "^2.0.0",
"mocha": "^9.0.0",
"ncp": "^2.0.0",
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 {GoogleAuthOptions, AuthClient} 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
67 changes: 32 additions & 35 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} from 'google-auth-library';
import {OperationsClientBuilder} from './operationsClient';
import type {GrpcClientOptions, ClientStubOptions} from './grpc';
import {GaxCall, GRPCCall} from './apitypes';
Expand Down Expand Up @@ -85,15 +77,16 @@ export interface ServiceMethods {
[name: string]: protobuf.Method;
}

export type AuthClient =
| OAuth2Client
| Compute
| JWT
| UserRefreshClient
| BaseExternalAccountClient;
/**
* @deprecated use `GoogleAuth` here instead
*/
type deprecatedAuthClientAlias = AuthClient;

export class GrpcClient {
auth?: OAuth2Client | GoogleAuth;
auth?: GoogleAuth<AuthClient> | deprecatedAuthClientAlias;
/**
* @deprecated use {@link GrpcClient.auth} instead
*/
authClient?: AuthClient;
fallback: boolean;
grpcVersion: string;
Expand All @@ -114,33 +107,36 @@ 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
* to use in Node.js. Required for browser, optional for Node.js.
* @constructor
* @param options {@link GrpcClientOptions}
*/

constructor(
options: (GrpcClientOptions | {auth: OAuth2Client}) & {
options: (
| GrpcClientOptions
| {
/**
* @deprecated - use `authClient` for `AuthClient`s instead
Copy link
Contributor Author

Choose a reason for hiding this comment

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

GrpcClientOptions currently includes this parameter, and with the correct name; authClient.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should create a follow-up to clean this up in the next major to avoid confusion

*/
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 {
this.auth =
(options.auth as GoogleAuth) ||
new GoogleAuth(options as GoogleAuthOptions);
this.auth = new GoogleAuth({
authClient: options.auth,
...options,
});
}

this.fallback = options.fallback ? true : false;
this.grpcVersion = require('../../package.json').version;
this.httpRules = (options as GrpcClientOptions).httpRules;
Expand Down Expand Up @@ -266,7 +262,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 All @@ -284,7 +280,7 @@ export class GrpcClient {
) {
if (!this.authClient) {
if (this.auth && 'getClient' in this.auth) {
this.authClient = (await this.auth.getClient()) as AuthClient;
this.authClient = await this.auth.getClient();
} else if (this.auth && 'getRequestHeaders' in this.auth) {
this.authClient = this.auth;
}
Expand Down Expand Up @@ -342,7 +338,7 @@ export class GrpcClient {
protocol,
servicePath,
servicePort,
this.authClient,
this.auth || this.authClient,
encoder,
decoder,
this.numericEnums,
Expand Down Expand Up @@ -425,6 +421,7 @@ export function createApiCall(
};
}
if (descriptor && 'streaming' in descriptor && !isNodeJS()) {
// TODO: with `fetch` this is available in the browser...
return () => {
throw new Error(
'Server streaming over the REST transport is only supported in Node.js.'
Expand Down
11 changes: 7 additions & 4 deletions gax/src/fallbackServiceStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import nodeFetch from 'node-fetch';
import {Response as NodeFetchResponse, RequestInit} from 'node-fetch';
import {AbortController as NodeAbortController} from 'abort-controller';

import {AuthClient, GoogleAuth} from 'google-auth-library';

import {hasWindowFetch, hasAbortController, isNodeJS} from './featureDetection';
import {AuthClient} from './fallback';
import {StreamArrayParser} from './streamArrayParser';
import {pipeline, PipelineSource} from 'stream';
import type {Agent as HttpAgent} from 'http';
Expand All @@ -35,7 +36,9 @@ interface NodeFetchType {
// Node.js before v19 does not enable keepalive by default.
// We'll try to enable it very carefully to make sure we don't break possible non-Node use cases.
// TODO: remove this after Node 18 is EOL.
// More info: https://github.com/node-fetch/node-fetch#custom-agent
// More info:
// - https://github.com/node-fetch/node-fetch#custom-agent
// - https://github.com/googleapis/gax-nodejs/pull/1534
let agentOption:
| ((parsedUrl: {protocol: string}) => HttpAgent | HttpsAgent)
| null = null;
Expand Down Expand Up @@ -76,7 +79,7 @@ export function generateServiceStub(
protocol: string,
servicePath: string,
servicePort: number,
authClient: AuthClient,
auth: GoogleAuth<AuthClient> | AuthClient,
requestEncoder: (
rpc: protobuf.Method,
protocol: string,
Expand Down Expand Up @@ -150,7 +153,7 @@ export function generateServiceStub(
}
const streamArrayParser = new StreamArrayParser(rpc);

authClient
auth
.getRequestHeaders()
.then(authHeader => {
const fetchRequest: RequestInit = {
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 {AuthClient, GoogleAuth, GoogleAuthOptions} 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;
minifyJson?: boolean;
protoJson?: protobuf.Root;
Expand Down Expand Up @@ -114,7 +114,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
2 changes: 2 additions & 0 deletions gax/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import * as operationsClient from './operationsClient';
import * as routingHeader from './routingHeader';

export {GoogleAuth, GoogleAuthOptions} from 'google-auth-library';
export * as googleAuthLibrary from 'google-auth-library';

export {grpc};
export {CancellablePromise, OngoingCall} from './call';
export {createApiCall} from './createApiCall';
Expand Down
4 changes: 2 additions & 2 deletions gax/src/locationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {warn} from './warnings';
import type {GrpcClient} from './grpc';
import type {GrpcClient as FallbackGrpcClient} from './fallback';
import {createApiCall} from './createApiCall';
import type {GoogleAuth} from 'google-auth-library';
import type {AuthClient, GoogleAuth} from 'google-auth-library';
import * as routingHeader from './routingHeader';
import * as protos from '../protos/locations';
import type {
Expand Down Expand Up @@ -49,7 +49,7 @@ export class LocationsClient {
private _providedCustomServicePath: boolean;
private _protos: {};
private _defaults: {[method: string]: gax.CallSettings};
auth: GoogleAuth;
auth: GoogleAuth<AuthClient>;
descriptors: Descriptors = {
page: {},
stream: {},
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 type {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
21 changes: 7 additions & 14 deletions gax/test/test-application/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
grpc,
GoogleError,
GoogleAuth,
googleAuthLibrary
Status,
createBackoffSettings,
createMaxRetriesBackoffSettings,
Expand All @@ -45,30 +46,22 @@ async function testShowcase() {
gaxServerStreamingRetries: true,
};

const fakeGoogleAuth = {
getClient: async () => {
return {
getRequestHeaders: async () => {
return {
Authorization: 'Bearer zzzz',
};
},
};
},
} as unknown as GoogleAuth;

const restClientOpts = {
fallback: true,
protocol: 'http',
port: 7469,
auth: fakeGoogleAuth,
auth: new GoogleAuth({
authClient: new googleAuthLibrary.PassThroughClient(),
}),
};

const restClientOptsCompat = {
fallback: 'rest' as const,
protocol: 'http',
port: 7469,
auth: fakeGoogleAuth,
auth: new GoogleAuth({
authClient: new googleAuthLibrary.PassThroughClient(),
}),
};

const grpcClient = new EchoClient(grpcClientOpts);
Expand Down
Loading
Loading