Skip to content

Commit

Permalink
feat: allow http(s).Agent ctor arg in lieu of instance (#1165)
Browse files Browse the repository at this point in the history
* feat: allow http(s).Agent ctor arg in lieu of instance

* duck type check
  • Loading branch information
kuhe authored Feb 21, 2024
1 parent 1e23f96 commit d70a00a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/fifty-ghosts-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@smithy/node-http-handler": minor
"@smithy/types": minor
---

allow ctor args in lieu of Agent instances in node-http-handler ctor
15 changes: 15 additions & 0 deletions packages/node-http-handler/src/node-http-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,22 @@ describe("NodeHttpHandler", () => {
hRequestSpy.mockRestore();
hsRequestSpy.mockRestore();
});

describe("constructor", () => {
it("allows https.Agent and http.Agent ctor args in place of actual instances", async () => {
const nodeHttpHandler = new NodeHttpHandler({
httpAgent: { maxSockets: 37 },
httpsAgent: { maxSockets: 39, keepAlive: false },
});

await nodeHttpHandler.handle({} as any);
expect(hRequestSpy.mock.calls[0][0]?.agent.maxSockets).toEqual(37);
expect(hRequestSpy.mock.calls[0][0]?.agent.keepAlive).toEqual(true);

expect((nodeHttpHandler as any).config.httpsAgent.maxSockets).toEqual(39);
expect((nodeHttpHandler as any).config.httpsAgent.keepAlive).toEqual(false);
});

it.each([
["empty", undefined],
["a provider", async () => {}],
Expand Down
19 changes: 13 additions & 6 deletions packages/node-http-handler/src/node-http-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import { writeRequestBody } from "./write-request-body";

export { NodeHttpHandlerOptions };

interface ResolvedNodeHttpHandlerConfig {
requestTimeout?: number;
connectionTimeout?: number;
socketAcquisitionWarningTimeout?: number;
interface ResolvedNodeHttpHandlerConfig extends Omit<NodeHttpHandlerOptions, "httpAgent" | "httpsAgent"> {
httpAgent: hAgent;
httpsAgent: hsAgent;
}
Expand Down Expand Up @@ -118,8 +115,18 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
return {
connectionTimeout,
requestTimeout: requestTimeout ?? socketTimeout,
httpAgent: httpAgent || new hAgent({ keepAlive, maxSockets }),
httpsAgent: httpsAgent || new hsAgent({ keepAlive, maxSockets }),
httpAgent: (() => {
if (httpAgent instanceof hAgent || typeof (httpAgent as hAgent)?.destroy === "function") {
return httpAgent as hAgent;
}
return new hAgent({ keepAlive, maxSockets, ...httpAgent });
})(),
httpsAgent: (() => {
if (httpsAgent instanceof hsAgent || typeof (httpsAgent as hsAgent)?.destroy === "function") {
return httpsAgent as hsAgent;
}
return new hsAgent({ keepAlive, maxSockets, ...httpsAgent });
})(),
};
}

Expand Down
15 changes: 11 additions & 4 deletions packages/types/src/http/httpHandlerInitialization.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Agent as hAgent } from "http";
import type { Agent as hsAgent } from "https";
import type { Agent as hAgent, AgentOptions as hAgentOptions } from "http";
import type { Agent as hsAgent, AgentOptions as hsAgentOptions } from "https";

/**
*
Expand Down Expand Up @@ -51,8 +51,15 @@ export interface NodeHttpHandlerOptions {
*/
socketTimeout?: number;

httpAgent?: hAgent;
httpsAgent?: hsAgent;
/**
* You can pass http.Agent or its constructor options.
*/
httpAgent?: hAgent | hAgentOptions;

/**
* You can pass https.Agent or its constructor options.
*/
httpsAgent?: hsAgent | hsAgentOptions;
}

/**
Expand Down

0 comments on commit d70a00a

Please sign in to comment.