Skip to content

Commit 17978e7

Browse files
authored
fix(gax): widen port to number | string in GrpcClientOptions (#9542)
Widens `port` from `number` to `number | string` on `GrpcClientOptions` and `ClientStubOptions` in `google-gax` to restore type compatibility with downstream client libraries (such as `@google-cloud/pubsub`, whose `ClientConfig` extends `gax.GrpcClientOptions` with `port?: string | number`) #9496 added `port?: number` to `GrpcClientOptions` (released in `google-gax@6.11.0`). Because `@google-cloud/pubsub`'s `ClientConfig` interface extends `gax.GrpcClientOptions` and declares `port?: string | number`, TypeScript compilation with `skipLibCheck: false` (such as in `@google-cloud/pubsub`'s `system-test/install.ts`) fails
1 parent 3b3f600 commit 17978e7

4 files changed

Lines changed: 19 additions & 11 deletions

File tree

‎core/packages/gax/src/fallback.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ export class GrpcClient {
161161
this._servicePath =
162162
(options as GrpcClientOptions).servicePath ||
163163
(options as GrpcClientOptions).apiEndpoint;
164-
this._port = (options as GrpcClientOptions).port;
164+
const rawPort = (options as GrpcClientOptions).port;
165+
this._port = typeof rawPort === 'string' ? parseInt(rawPort, 10) : rawPort;
165166
}
166167

167168
/**
@@ -371,7 +372,8 @@ export class GrpcClient {
371372
servicePort = parseInt(match[2]);
372373
}
373374
if (opts.port) {
374-
servicePort = opts.port;
375+
servicePort =
376+
typeof opts.port === 'string' ? parseInt(opts.port, 10) : opts.port;
375377
} else if (!servicePort) {
376378
servicePort = 443;
377379
}

‎core/packages/gax/src/grpc.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export interface GrpcClientOptions extends GoogleAuthOptions {
5555
universeDomain?: string;
5656
servicePath?: string;
5757
apiEndpoint?: string;
58-
port?: number;
58+
port?: number | string;
5959
}
6060

6161
export interface MetadataValue {
@@ -104,7 +104,7 @@ export type GrpcModule = typeof grpc;
104104
export interface ClientStubOptions {
105105
protocol?: string;
106106
servicePath?: string;
107-
port?: number;
107+
port?: number | string;
108108
sslCreds?: grpc.ChannelCredentials;
109109
[index: string]: string | number | undefined | {};
110110
// For mtls:
@@ -180,7 +180,10 @@ export class GrpcClient {
180180
this.auth = options.auth || new GoogleAuth(options);
181181
this.fallback = false;
182182
this._servicePath = options.servicePath || options.apiEndpoint;
183-
this._port = options.port;
183+
this._port =
184+
typeof options.port === 'string'
185+
? parseInt(options.port, 10)
186+
: options.port;
184187

185188
const minimumVersion = 10;
186189
const major = Number(process.version.match(/^v(\d+)/)?.[1]);

‎core/packages/gax/src/observability/metadataResolver.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ export function extractFromSettings(
135135
| {
136136
servicePath?: string;
137137
apiEndpoint?: string;
138-
port?: number;
139-
servicePort?: number;
138+
port?: number | string;
139+
servicePort?: number | string;
140140
}
141141
| undefined;
142142
const endpoint =
@@ -155,11 +155,14 @@ export function extractFromSettings(
155155
}
156156

157157
// Resolve server port if not already parsed from endpoint.
158-
const port =
159-
(settings as {port?: number; servicePort?: number}).port ||
160-
(settings as {port?: number; servicePort?: number}).servicePort ||
158+
const rawPort =
159+
(settings as {port?: number | string; servicePort?: number | string})
160+
.port ||
161+
(settings as {port?: number | string; servicePort?: number | string})
162+
.servicePort ||
161163
otherArgs?.port ||
162164
otherArgs?.servicePort;
165+
const port = typeof rawPort === 'string' ? parseInt(rawPort, 10) : rawPort;
163166
if (port && typeof port === 'number' && !result.serverPort) {
164167
result.serverPort = port;
165168
}

‎core/packages/gax/src/resumableUpload.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export interface ResumableUploadContext {
200200
/** The hostname of the API service endpoint. */
201201
servicePath: string;
202202
/** The port of the API service endpoint. */
203-
servicePort: number;
203+
servicePort: number | string;
204204
/** The protocol (usually `https`). */
205205
protocol: string;
206206
/** The protobuf method descriptor for the resumable upload RPC. */

0 commit comments

Comments
 (0)