11import * as https from "https" ;
22import * as http from "http" ;
33import { buildQueryString } from "@aws-sdk/querystring-builder" ;
4- import { HttpOptions , NodeHttpOptions } from "@aws-sdk/types" ;
4+ import { HttpHandlerOptions } from "@aws-sdk/types" ;
55import { HttpHandler , HttpRequest , HttpResponse } from "@aws-sdk/protocol-http" ;
66import { setConnectionTimeout } from "./set-connection-timeout" ;
77import { setSocketTimeout } from "./set-socket-timeout" ;
88import { writeRequestBody } from "./write-request-body" ;
99import { getTransformedHeaders } from "./get-transformed-headers" ;
1010
11+ /**
12+ * Represents the http options that can be passed to a node http client.
13+ */
14+ export interface NodeHttpOptions {
15+ /**
16+ * The maximum time in milliseconds that the connection phase of a request
17+ * may take before the connection attempt is abandoned.
18+ */
19+ connectionTimeout ?: number ;
20+
21+ /**
22+ * The maximum time in milliseconds that a socket may remain idle before it
23+ * is closed.
24+ */
25+ socketTimeout ?: number ;
26+
27+ httpAgent ?: http . Agent ;
28+ httpsAgent ?: https . Agent ;
29+ }
30+
1131export class NodeHttpHandler implements HttpHandler {
1232 private readonly httpAgent : http . Agent ;
1333 private readonly httpsAgent : https . Agent ;
34+ private readonly connectionTimeout ?: number ;
35+ private readonly socketTimeout ?: number ;
1436
15- constructor ( private readonly httpOptions : NodeHttpOptions = { } ) {
16- const { keepAlive = true } = httpOptions ;
17- this . httpAgent = new http . Agent ( { keepAlive } ) ;
18- this . httpsAgent = new https . Agent ( { keepAlive } ) ;
37+ constructor ( {
38+ connectionTimeout,
39+ socketTimeout,
40+ httpAgent,
41+ httpsAgent
42+ } : NodeHttpOptions = { } ) {
43+ this . connectionTimeout = connectionTimeout ;
44+ this . socketTimeout = socketTimeout ;
45+ const keepAlive = true ;
46+ this . httpAgent = httpAgent || new http . Agent ( { keepAlive } ) ;
47+ this . httpsAgent = httpsAgent || new https . Agent ( { keepAlive } ) ;
1948 }
2049
2150 destroy ( ) : void {
@@ -25,7 +54,7 @@ export class NodeHttpHandler implements HttpHandler {
2554
2655 handle (
2756 request : HttpRequest ,
28- { abortSignal } : HttpOptions
57+ { abortSignal } : HttpHandlerOptions
2958 ) : Promise < { response : HttpResponse } > {
3059 return new Promise ( ( resolve , reject ) => {
3160 // if the request was already aborted, prevent doing extra work
@@ -61,8 +90,8 @@ export class NodeHttpHandler implements HttpHandler {
6190 req . on ( "error" , reject ) ;
6291
6392 // wire-up any timeout logic
64- setConnectionTimeout ( req , reject , this . httpOptions . connectionTimeout ) ;
65- setSocketTimeout ( req , reject , this . httpOptions . socketTimeout ) ;
93+ setConnectionTimeout ( req , reject , this . connectionTimeout ) ;
94+ setSocketTimeout ( req , reject , this . socketTimeout ) ;
6695
6796 // wire-up abort logic
6897 if ( abortSignal ) {
0 commit comments