Skip to content

feat(createNodeHttpRequester): Add proxy support to another Agent on createNodeHttpRequester #1180

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

Merged
merged 8 commits into from
Aug 5, 2020
19 changes: 15 additions & 4 deletions packages/requester-node-http/src/createNodeHttpRequester.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
/* eslint functional/prefer-readonly-type: 0 */
/* eslint sonarjs/cognitive-complexity: 0 */ // -->

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

export function createNodeHttpRequester(): Requester & Destroyable {
export type NodeHttpRequesterOptions = {
agent?: https.Agent | http.Agent;
httpAgent?: http.Agent;
httpsAgent?: https.Agent;
};

export function createNodeHttpRequester({
agent: userGlobalAgent,
httpAgent: userHttpAgent,
httpsAgent: userHttpsAgent,
}: NodeHttpRequesterOptions = {}): Requester & Destroyable {
const agentOptions = { keepAlive: true };
const httpAgent = new http.Agent(agentOptions);
const httpsAgent = new https.Agent(agentOptions);
const httpAgent = userHttpAgent || userGlobalAgent || new http.Agent(agentOptions);
const httpsAgent = userHttpsAgent || userGlobalAgent || new https.Agent(agentOptions);

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

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

response.on('data', chunk => {
Expand Down