Skip to content

Commit

Permalink
fix(node-http-handler): undeprecate connectionTimeout (#4669)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Apr 26, 2023
1 parent 33501ba commit 79c46f3
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions packages/node-http-handler/src/node-http-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,31 @@ import { Agent as hsAgent, request as hsRequest, RequestOptions } from "https";

import { NODEJS_TIMEOUT_ERROR_CODES } from "./constants";
import { getTransformedHeaders } from "./get-transformed-headers";
import { setConnectionTimeout } from "./set-connection-timeout";
import { setSocketTimeout } from "./set-socket-timeout";
import { writeRequestBody } from "./write-request-body";

/**
* Represents the http options that can be passed to a node http client.
*/
export interface NodeHttpHandlerOptions {
/**
* @deprecated Use {@link requestTimeout}
*
* Note:{@link NodeHttpHandler} will resolve request timeout via nullish coalescing the following fields:
* {@link requestTimeout} ?? {@link connectionTimeout} ?? {@link socketTimeout} ?? {@link DEFAULT_REQUEST_TIMEOUT}
*
* The maximum time in milliseconds that the connection phase of a request
* may take before the connection attempt is abandoned.
*
* Defaults to 0, which disables the timeout.
*/
connectionTimeout?: number;

/**
* The maximum time in milliseconds that the connection phase of a request
* may take before the connection attempt is abandoned.
* The number of milliseconds a request can take before automatically being terminated.
* Defaults to 0, which disables the timeout.
*/
requestTimeout?: number;

/**
* @deprecated Use {@link requestTimeout}
*
* Note:{@link NodeHttpHandler} will resolve request timeout via nullish coalescing the following fields:
* {@link requestTimeout} ?? {@link connectionTimeout} ?? {@link socketTimeout} ?? {@link DEFAULT_REQUEST_TIMEOUT}
*
* The maximum time in milliseconds that a socket may remain idle before it
* is closed.
*/
Expand All @@ -45,9 +41,8 @@ export interface NodeHttpHandlerOptions {
}

interface ResolvedNodeHttpHandlerConfig {
requestTimeout: number;
requestTimeout?: number;
connectionTimeout?: number;
socketTimeout?: number;
httpAgent: hAgent;
httpsAgent: hsAgent;
}
Expand Down Expand Up @@ -82,8 +77,7 @@ export class NodeHttpHandler implements HttpHandler {

return {
connectionTimeout,
socketTimeout,
requestTimeout: requestTimeout ?? connectionTimeout ?? socketTimeout ?? DEFAULT_REQUEST_TIMEOUT,
requestTimeout: requestTimeout ?? socketTimeout,
httpAgent: httpAgent || new hAgent({ keepAlive, maxSockets }),
httpsAgent: httpsAgent || new hsAgent({ keepAlive, maxSockets }),
};
Expand Down Expand Up @@ -142,11 +136,9 @@ export class NodeHttpHandler implements HttpHandler {
}
});

const timeout: number = this.config?.requestTimeout ?? DEFAULT_REQUEST_TIMEOUT;
req.setTimeout(timeout, () => {
req.destroy();
reject(Object.assign(new Error(`Connection timed out after ${timeout} ms`), { name: "TimeoutError" }));
});
// wire-up any timeout logic
setConnectionTimeout(req, reject, this.config.connectionTimeout);
setSocketTimeout(req, reject, this.config.requestTimeout);

// wire-up abort logic
if (abortSignal) {
Expand Down

0 comments on commit 79c46f3

Please sign in to comment.