Skip to content

Commit 3818322

Browse files
feat(createNodeHttpRequester): Add custom Agent support on createNodeHttpRequester (#1180)
you can now call e.g., `createNodeHttpRequester({ agent: new HttpsProxyAgent(process.env.http_proxy) })` Co-authored-by: Haroen Viaene <[email protected]>
1 parent 7101afe commit 3818322

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

packages/requester-node-http/src/createNodeHttpRequester.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1+
/* eslint functional/prefer-readonly-type: 0 */
12
/* eslint sonarjs/cognitive-complexity: 0 */ // -->
23

34
import { Destroyable, Request, Requester, Response } from '@algolia/requester-common';
45
import * as http from 'http';
56
import * as https from 'https';
67
import * as URL from 'url';
78

8-
export function createNodeHttpRequester(): Requester & Destroyable {
9+
export type NodeHttpRequesterOptions = {
10+
agent?: https.Agent | http.Agent;
11+
httpAgent?: http.Agent;
12+
httpsAgent?: https.Agent;
13+
};
14+
15+
export function createNodeHttpRequester({
16+
agent: userGlobalAgent,
17+
httpAgent: userHttpAgent,
18+
httpsAgent: userHttpsAgent,
19+
}: NodeHttpRequesterOptions = {}): Requester & Destroyable {
920
const agentOptions = { keepAlive: true };
10-
const httpAgent = new http.Agent(agentOptions);
11-
const httpsAgent = new https.Agent(agentOptions);
21+
const httpAgent = userHttpAgent || userGlobalAgent || new http.Agent(agentOptions);
22+
const httpsAgent = userHttpsAgent || userGlobalAgent || new https.Agent(agentOptions);
1223

1324
return {
1425
send(request: Request): Readonly<Promise<Response>> {
@@ -27,7 +38,7 @@ export function createNodeHttpRequester(): Requester & Destroyable {
2738
};
2839

2940
const req = (url.protocol === 'https:' ? https : http).request(options, response => {
30-
// eslint-disable-next-line functional/no-let, functional/prefer-readonly-type
41+
// eslint-disable-next-line functional/no-let
3142
let contentBuffers: Buffer[] = [];
3243

3344
response.on('data', chunk => {

0 commit comments

Comments
 (0)