Skip to content

Commit d70a00a

Browse files
authored
feat: allow http(s).Agent ctor arg in lieu of instance (#1165)
* feat: allow http(s).Agent ctor arg in lieu of instance * duck type check
1 parent 1e23f96 commit d70a00a

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

.changeset/fifty-ghosts-live.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@smithy/node-http-handler": minor
3+
"@smithy/types": minor
4+
---
5+
6+
allow ctor args in lieu of Agent instances in node-http-handler ctor

packages/node-http-handler/src/node-http-handler.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,22 @@ describe("NodeHttpHandler", () => {
3737
hRequestSpy.mockRestore();
3838
hsRequestSpy.mockRestore();
3939
});
40+
4041
describe("constructor", () => {
42+
it("allows https.Agent and http.Agent ctor args in place of actual instances", async () => {
43+
const nodeHttpHandler = new NodeHttpHandler({
44+
httpAgent: { maxSockets: 37 },
45+
httpsAgent: { maxSockets: 39, keepAlive: false },
46+
});
47+
48+
await nodeHttpHandler.handle({} as any);
49+
expect(hRequestSpy.mock.calls[0][0]?.agent.maxSockets).toEqual(37);
50+
expect(hRequestSpy.mock.calls[0][0]?.agent.keepAlive).toEqual(true);
51+
52+
expect((nodeHttpHandler as any).config.httpsAgent.maxSockets).toEqual(39);
53+
expect((nodeHttpHandler as any).config.httpsAgent.keepAlive).toEqual(false);
54+
});
55+
4156
it.each([
4257
["empty", undefined],
4358
["a provider", async () => {}],

packages/node-http-handler/src/node-http-handler.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ import { writeRequestBody } from "./write-request-body";
1414

1515
export { NodeHttpHandlerOptions };
1616

17-
interface ResolvedNodeHttpHandlerConfig {
18-
requestTimeout?: number;
19-
connectionTimeout?: number;
20-
socketAcquisitionWarningTimeout?: number;
17+
interface ResolvedNodeHttpHandlerConfig extends Omit<NodeHttpHandlerOptions, "httpAgent" | "httpsAgent"> {
2118
httpAgent: hAgent;
2219
httpsAgent: hsAgent;
2320
}
@@ -118,8 +115,18 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
118115
return {
119116
connectionTimeout,
120117
requestTimeout: requestTimeout ?? socketTimeout,
121-
httpAgent: httpAgent || new hAgent({ keepAlive, maxSockets }),
122-
httpsAgent: httpsAgent || new hsAgent({ keepAlive, maxSockets }),
118+
httpAgent: (() => {
119+
if (httpAgent instanceof hAgent || typeof (httpAgent as hAgent)?.destroy === "function") {
120+
return httpAgent as hAgent;
121+
}
122+
return new hAgent({ keepAlive, maxSockets, ...httpAgent });
123+
})(),
124+
httpsAgent: (() => {
125+
if (httpsAgent instanceof hsAgent || typeof (httpsAgent as hsAgent)?.destroy === "function") {
126+
return httpsAgent as hsAgent;
127+
}
128+
return new hsAgent({ keepAlive, maxSockets, ...httpsAgent });
129+
})(),
123130
};
124131
}
125132

packages/types/src/http/httpHandlerInitialization.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { Agent as hAgent } from "http";
2-
import type { Agent as hsAgent } from "https";
1+
import type { Agent as hAgent, AgentOptions as hAgentOptions } from "http";
2+
import type { Agent as hsAgent, AgentOptions as hsAgentOptions } from "https";
33

44
/**
55
*
@@ -51,8 +51,15 @@ export interface NodeHttpHandlerOptions {
5151
*/
5252
socketTimeout?: number;
5353

54-
httpAgent?: hAgent;
55-
httpsAgent?: hsAgent;
54+
/**
55+
* You can pass http.Agent or its constructor options.
56+
*/
57+
httpAgent?: hAgent | hAgentOptions;
58+
59+
/**
60+
* You can pass https.Agent or its constructor options.
61+
*/
62+
httpsAgent?: hsAgent | hsAgentOptions;
5663
}
5764

5865
/**

0 commit comments

Comments
 (0)