Skip to content

Commit

Permalink
feat(javascript): allow providing options for node requester (#872)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed Jul 27, 2022
1 parent 7b96d39 commit 0d988f9
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 6 deletions.
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

0 comments on commit 0d988f9

Please sign in to comment.