Skip to content

Commit

Permalink
support hint and family, update tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Musinux authored and Marsup committed Apr 10, 2024
1 parent 387ae24 commit b4b323e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
9 changes: 7 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ Initiate an HTTP request.

- `timeout` - number of milliseconds to wait without receiving a response before aborting the request. Defaults to `0` (no limit).

- `lookup` - DNS lookup function. see [http.request(url[,options][,callback])](https://nodejs.org/api/http.html#httprequestoptions-callback). Defaults to [dns.lookup()](https://nodejs.org/api/dns.html#dnslookuphostname-options-callback).

- `family` - IP address family to use when resolving `host` or `hostname`. Valid values are `4` or `6`. When unspecified, both IP v4 and v6 will be used.

- `hints` - Optional `dns.lookup()` hints.


Returns a promise that resolves into a node response object. The promise has a `req` property which is the instance of the node.js [ClientRequest](http://nodejs.org/api/http.html#http_class_http_clientrequest) object.

### `read(response, options)`
Expand All @@ -148,8 +155,6 @@ Returns a promise that resolves into a node response object. The promise has a `
- `'strict'` - as `true`, except returns an error for non-JSON content-type.
- `'force'` - try `JSON.parse` regardless of the content-type header.

- `lookup` - DNS lookup function. see [http.request(url[,options][,callback])](https://nodejs.org/api/http.html#httprequestoptions-callback). Defaults to [dns.lookup()](https://nodejs.org/api/dns.html#dnslookuphostname-options-callback).

- `maxBytes` - the maximum allowed response payload size. Defaults to `0` (no limit).

- `timeout` - the number of milliseconds to wait while reading data before aborting handling of the response. Defaults to `0`.
Expand Down
10 changes: 10 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ declare namespace Client {
*/
readonly lookup?: LookupFunction;

/**
* IP address family to use when resolving host or hostname. Valid values are 4 or 6. When unspecified, both IP v4 and v6 will be used.
*/
readonly family?: number;

/**
* Optional dns.lookup() hints.
*/
readonly hints?: number;

/**
* Fully qualified URL string used as the base URL.
*/
Expand Down
18 changes: 6 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const Tap = require('./tap');

const internals = {
jsonRegex: /^application\/([a-z0-9.]*[+-]json|json)$/,
shallowOptions: ['agent', 'agents', 'beforeRedirect', 'payload', 'redirected', 'lookup']
shallowOptions: ['agent', 'agents', 'beforeRedirect', 'payload', 'redirected'],
httpOptions: ['secureProtocol', 'ciphers', 'lookup', 'family', 'hints']
};


Expand Down Expand Up @@ -61,7 +62,6 @@ internals.Client = class {
Hoek.assert(internals.isNullOrUndefined(options.beforeRedirect) || typeof options.beforeRedirect === 'function', 'options.beforeRedirect must be a function');
Hoek.assert(internals.isNullOrUndefined(options.redirected) || typeof options.redirected === 'function', 'options.redirected must be a function');
Hoek.assert(options.gunzip === undefined || typeof options.gunzip === 'boolean' || options.gunzip === 'force', 'options.gunzip must be a boolean or "force"');
Hoek.assert(options.lookup === undefined || typeof options.lookup === 'function', 'options.lookup must be a function');
}
catch (err) {
return Promise.reject(err);
Expand Down Expand Up @@ -160,8 +160,10 @@ internals.Client = class {

const client = uri.protocol === 'https:' ? Https : Http;

if (options.lookup !== undefined) {
uri.lookup = options.lookup;
for (const option of internals.httpOptions) {
if (options[option] !== undefined) {
uri[option] = options[option];
}
}

if (options.rejectUnauthorized !== undefined &&
Expand All @@ -178,14 +180,6 @@ internals.Client = class {
uri.agent = uri.protocol === 'https:' ? this.agents.https : this.agents.http;
}

if (options.secureProtocol !== undefined) {
uri.secureProtocol = options.secureProtocol;
}

if (options.ciphers !== undefined) {
uri.ciphers = options.ciphers;
}

this._emit('preRequest', uri, options);

const start = Date.now();
Expand Down
44 changes: 43 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,7 @@ describe('options.lookup', () => {
let dnsLookupCalled = false;

const server = await internals.server('ok');
flags.onCleanup = () => server.close();
await Wreck.request('get', `http://localhost:${server.address().port}/`, {
lookup: (hostname, options, callback) => {

Expand All @@ -1212,7 +1213,6 @@ describe('options.lookup', () => {
}
});
expect(dnsLookupCalled).to.equal(true);
flags.onCleanup = () => server.close();
});

it('uses the lookup function and fails if the lookup function rejects the domain', async (flags) => {
Expand All @@ -1226,6 +1226,48 @@ describe('options.lookup', () => {
});
});

describe('options.hints', () => {

it('passes the hint parameter to the lookup function to resolve the server ip address', async (flags) => {

const expectedHints = Dns.ADDRCONFIG;
let actualHints;

const server = await internals.server('ok');
flags.onCleanup = () => server.close();
await Wreck.request('get', `http://localhost:${server.address().port}/`, {
lookup: (hostname, options, callback) => {

actualHints = options.hints;
return Dns.lookup(hostname, options, callback);
},
hints: expectedHints
});
expect(actualHints).to.equal(expectedHints);
});
});

describe('options.family', () => {

it('passes the family parameter to the lookup function to resolve the server ip address', async (flags) => {

const expectedFamily = 4;
let actualFamily;

const server = await internals.server('ok');
flags.onCleanup = () => server.close();
await Wreck.request('get', `http://localhost:${server.address().port}/`, {
lookup: (hostname, options, callback) => {

actualFamily = options.family;
return Dns.lookup(hostname, options, callback);
},
family: expectedFamily // IPv4
});
expect(actualFamily).to.equal(expectedFamily);
});
});

describe('options.baseUrl', () => {

it('uses path when path is a full URL', async (flags) => {
Expand Down

0 comments on commit b4b323e

Please sign in to comment.