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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(javascript): allow providing options for node requester #872

Merged
merged 1 commit into from
Jul 27, 2022
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type http from 'http';
import http from 'http';
import https from 'https';
import { Readable } from 'stream';

import type { EndRequest } from '@algolia/client-common';
Expand All @@ -17,6 +18,72 @@ import {

const requester = createHttpRequester();

const httpsBaseRequest = https.request;
const httpBaseRequest = http.request;

describe('api', () => {
const mockedRequestResponse = {
destroy: jest.fn(),
on: jest.fn(),
once: jest.fn(),
write: jest.fn(),
end: jest.fn(),
};

beforeAll(() => {
// @ts-expect-error we don't care about the response for those tests
https.request = jest.fn(() => mockedRequestResponse);
});

afterAll(() => {
https.request = httpsBaseRequest;
http.request = httpBaseRequest;
jest.resetAllMocks();
jest.clearAllMocks();
});

it('allow init without parameters', () => {
expect(() => createHttpRequester()).not.toThrow();
});

it('allow providing custom agent', async () => {
const agent = new http.Agent();
// @ts-expect-error we don't care about the response for those tests
http.request = jest.fn(() => mockedRequestResponse);
const tmpRequester = createHttpRequester({
agent,
});

await tmpRequester.send({
...requestStub,
url: 'http://algolia-dns.net/foo?x-algolia-header=bar',
});

expect(http.request).toHaveBeenCalled();
});

it('allow overriding default options', async () => {
const tmpRequester = createHttpRequester({
requesterOptions: {
headers: {
'my-extra-header': 'algolia',
},
},
});

await tmpRequester.send(requestStub);

expect(https.request).toHaveBeenCalledWith(
expect.objectContaining({
headers: expect.objectContaining({
'my-extra-header': 'algolia',
}),
}),
expect.any(Function)
);
});
});

describe('status code handling', () => {
it('sends requests', async () => {
const body = getStringifiedBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,30 @@ import { URL } from 'url';

import type { EndRequest, Requester, Response } from '@algolia/client-common';

export type CreateHttpRequesterOptions = Partial<{
agent: http.Agent | https.Agent;
httpAgent: http.Agent;
httpsAgent: https.Agent;
/**
* RequestOptions to be merged with the end request, it will override default options if provided.
*/
requesterOptions: https.RequestOptions;
}>;

// Global agents allow us to reuse the TCP protocol with multiple clients
const agentOptions = { keepAlive: true };
const httpAgent = new http.Agent(agentOptions);
const httpsAgent = new https.Agent(agentOptions);
const defaultHttpAgent = new http.Agent(agentOptions);
const defaultHttpsAgent = new https.Agent(agentOptions);

export function createHttpRequester({
agent: userGlobalAgent,
httpAgent: userHttpAgent,
httpsAgent: userHttpsAgent,
requesterOptions = {},
}: CreateHttpRequesterOptions = {}): Requester {
const httpAgent = userHttpAgent || userGlobalAgent || defaultHttpAgent;
const httpsAgent = userHttpsAgent || userGlobalAgent || defaultHttpsAgent;

export function createHttpRequester(): Requester {
function send(request: EndRequest): Promise<Response> {
return new Promise((resolve) => {
let responseTimeout: NodeJS.Timeout | undefined;
Expand All @@ -23,10 +41,17 @@ export function createHttpRequester(): Requester {
hostname: url.hostname,
path,
method: request.method,
headers: request.headers,
...(url.port !== undefined ? { port: url.port || '' } : {}),
...requesterOptions,
headers: {
...request.headers,
...requesterOptions.headers,
},
};

if (url.port && !requesterOptions.port) {
options.port = url.port;
}

const req = (url.protocol === 'https:' ? https : http).request(
options,
(response) => {
Expand Down