Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow http(s).Agent ctor arg in lieu of instance #1165

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading