Skip to content

Commit

Permalink
dns: allow NODE_DNS_VERBATIM to change default verbatim
Browse files Browse the repository at this point in the history
Allow the NODE_DNS_VERBATIM environment variable to change the default
value of `verbatim` in `dns.lookup()`.
  • Loading branch information
oyyd committed Apr 5, 2021
1 parent 4f387c2 commit baa8c15
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 3 deletions.
10 changes: 10 additions & 0 deletions doc/api/cli.md
Expand Up @@ -1295,6 +1295,15 @@ added: v0.3.0

When set, colors will not be used in the REPL.

### `NODE_DNS_VERBATIM=[0, 1]`
<!-- YAML
added: REPLACEME
-->

Set the default value of `verbatim` in [`dns.lookup()`][]. The value may be:
* `0` to indicate `false`
* `1` to indicate `true`

### `NODE_EXTRA_CA_CERTS=file`
<!-- YAML
added: v7.3.0
Expand Down Expand Up @@ -1728,6 +1737,7 @@ $ node --max-old-space-size=1536 index.js
[`NODE_OPTIONS`]: #cli_node_options_options
[`NO_COLOR`]: https://no-color.org
[`SlowBuffer`]: buffer.md#buffer_class_slowbuffer
[`dns.lookup()`]: dns.html#dns_dns_lookup_hostname_options_callback
[`process.setUncaughtExceptionCaptureCallback()`]: process.md#process_process_setuncaughtexceptioncapturecallback_fn
[`tls.DEFAULT_MAX_VERSION`]: tls.md#tls_tls_default_max_version
[`tls.DEFAULT_MIN_VERSION`]: tls.md#tls_tls_default_min_version
Expand Down
3 changes: 2 additions & 1 deletion lib/dns.js
Expand Up @@ -41,6 +41,7 @@ const {
Resolver,
validateHints,
emitInvalidHostnameWarning,
getDefaultVerbatim,
} = require('internal/dns/utils');
const {
ERR_INVALID_ARG_TYPE,
Expand Down Expand Up @@ -96,7 +97,7 @@ function lookup(hostname, options, callback) {
let hints = 0;
let family = -1;
let all = false;
let verbatim = false;
let verbatim = getDefaultVerbatim();

// Parse arguments
if (hostname) {
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/dns/promises.js
@@ -1,5 +1,4 @@
'use strict';

const {
ArrayPrototypeMap,
ObjectCreate,
Expand All @@ -14,6 +13,7 @@ const {
validateHints,
validateTimeout,
emitInvalidHostnameWarning,
getDefaultVerbatim,
} = require('internal/dns/utils');
const { codes, dnsException } = require('internal/errors');
const { toASCII } = require('internal/idna');
Expand Down Expand Up @@ -103,7 +103,7 @@ function lookup(hostname, options) {
var hints = 0;
var family = -1;
var all = false;
var verbatim = false;
var verbatim = getDefaultVerbatim();

// Parse arguments
if (hostname && typeof hostname !== 'string') {
Expand Down
12 changes: 12 additions & 0 deletions lib/internal/dns/utils.js
Expand Up @@ -184,6 +184,17 @@ function emitInvalidHostnameWarning(hostname) {
);
}

function getDefaultVerbatim() {
const { NODE_DNS_VERBATIM } = process.env;
switch (NODE_DNS_VERBATIM) {
case '1':
return true;
case '0':
default:
return false;
}
}

module.exports = {
bindDefaultResolver,
getDefaultResolver,
Expand All @@ -192,4 +203,5 @@ module.exports = {
validateTimeout,
Resolver,
emitInvalidHostnameWarning,
getDefaultVerbatim,
};
2 changes: 2 additions & 0 deletions lib/internal/main/print_help.js
Expand Up @@ -38,6 +38,8 @@ const envVars = new SafeMap(ArrayPrototypeConcat([
'categories that should print debug output' }],
['NODE_DISABLE_COLORS', { helpText: 'set to 1 to disable colors in ' +
'the REPL' }],
['NODE_DNS_VERBATIM', { helpText: 'set to 1 or 0 to enable or disable ' +
'verbatim in dns.lookup by default' }],
['NODE_EXTRA_CA_CERTS', { helpText: 'path to additional CA certificates ' +
'file. Only read once during process startup.' }],
['NODE_NO_WARNINGS', { helpText: 'set to 1 to silence process warnings' }],
Expand Down
50 changes: 50 additions & 0 deletions test/parallel/test-dns-default-verbatim.js
@@ -0,0 +1,50 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const { internalBinding } = require('internal/test/binding');
const cares = internalBinding('cares_wrap');
const { promisify } = require('util')

// Test that NODE_DNS_VERBATIM works as expected.

const originalGetaddrinfo = cares.getaddrinfo;
const calls = [];
cares.getaddrinfo = (...args) => {
calls.push(args);
originalGetaddrinfo(...args);
};

const dns = require('dns');
const dnsPromises = dns.promises;

(async () => {
{
process.env.NODE_DNS_VERBATIM = '1';
let verbatim;

await promisify(dns.lookup)('example.org');

assert.strictEqual(calls.length, 1);
verbatim = calls[0][4];
assert.strictEqual(verbatim, true);

await dnsPromises.lookup('example.org');
assert.strictEqual(calls.length, 2);
verbatim = calls[1][4];
assert.strictEqual(verbatim, true);

process.env.NODE_DNS_VERBATIM = '0';

await promisify(dns.lookup)('example.org');

assert.strictEqual(calls.length, 3);
verbatim = calls[2][4];
assert.strictEqual(verbatim, false);

await dnsPromises.lookup('example.org');
assert.strictEqual(calls.length, 4);
verbatim = calls[3][4];
assert.strictEqual(verbatim, false);
}
})();

0 comments on commit baa8c15

Please sign in to comment.