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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add lookup method #1595

Merged
merged 1 commit into from
Jan 18, 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
16 changes: 16 additions & 0 deletions src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ function Request(method, url) {
this.qsRaw = this._query; // Unused, for backwards compatibility only
this._redirectList = [];
this._streamRequest = false;
this._lookup = null;
this.once('end', this.clearTimeout.bind(this));
}

Expand Down Expand Up @@ -300,6 +301,20 @@ Request.prototype.agent = function (agent) {
return this;
};

/**
* Gets/sets the `lookup` function to use custom DNS resolver.
*
* @param {Function} lookup
* @return {Function}
* @api public
*/

Request.prototype.lookup = function (lookup) {
if (arguments.length === 0) return this._lookup;
this._lookup = lookup;
return this;
};

/**
* Set _Content-Type_ response header passed through `mime.getType()`.
*
Expand Down Expand Up @@ -759,6 +774,7 @@ Request.prototype.request = function () {
options.cert = this._cert;
options.passphrase = this._passphrase;
options.agent = this._agent;
options.lookup = this._lookup;
options.rejectUnauthorized =
typeof this._disableTLSCerts === 'boolean'
? !this._disableTLSCerts
Expand Down
21 changes: 21 additions & 0 deletions test/node/lookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
const assert = require('assert');
const dns = require('dns');
const request = require('../support/client');
const setup = require('../support/setup');

const base = setup.uri;

function myLookup(hostname, options, callback) {
dns.lookup(hostname, options, callback);
}

describe('req.lookup()', () => {
it('should set a custom lookup', () => {
const r = request.get(`${base}/ok`).lookup(myLookup);
assert(r.lookup() === myLookup);
r.then((res) => {
res.text.should.equal('ok');
});
});
});