Skip to content

Commit baa8c15

Browse files
committed
dns: allow NODE_DNS_VERBATIM to change default verbatim
Allow the NODE_DNS_VERBATIM environment variable to change the default value of `verbatim` in `dns.lookup()`.
1 parent 4f387c2 commit baa8c15

File tree

6 files changed

+78
-3
lines changed

6 files changed

+78
-3
lines changed

‎doc/api/cli.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,15 @@ added: v0.3.0
12951295

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

1298+
### `NODE_DNS_VERBATIM=[0, 1]`
1299+
<!-- YAML
1300+
added: REPLACEME
1301+
-->
1302+
1303+
Set the default value of `verbatim` in [`dns.lookup()`][]. The value may be:
1304+
* `0` to indicate `false`
1305+
* `1` to indicate `true`
1306+
12981307
### `NODE_EXTRA_CA_CERTS=file`
12991308
<!-- YAML
13001309
added: v7.3.0
@@ -1728,6 +1737,7 @@ $ node --max-old-space-size=1536 index.js
17281737
[`NODE_OPTIONS`]: #cli_node_options_options
17291738
[`NO_COLOR`]: https://no-color.org
17301739
[`SlowBuffer`]: buffer.md#buffer_class_slowbuffer
1740+
[`dns.lookup()`]: dns.html#dns_dns_lookup_hostname_options_callback
17311741
[`process.setUncaughtExceptionCaptureCallback()`]: process.md#process_process_setuncaughtexceptioncapturecallback_fn
17321742
[`tls.DEFAULT_MAX_VERSION`]: tls.md#tls_tls_default_max_version
17331743
[`tls.DEFAULT_MIN_VERSION`]: tls.md#tls_tls_default_min_version

‎lib/dns.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const {
4141
Resolver,
4242
validateHints,
4343
emitInvalidHostnameWarning,
44+
getDefaultVerbatim,
4445
} = require('internal/dns/utils');
4546
const {
4647
ERR_INVALID_ARG_TYPE,
@@ -96,7 +97,7 @@ function lookup(hostname, options, callback) {
9697
let hints = 0;
9798
let family = -1;
9899
let all = false;
99-
let verbatim = false;
100+
let verbatim = getDefaultVerbatim();
100101

101102
// Parse arguments
102103
if (hostname) {

‎lib/internal/dns/promises.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
32
const {
43
ArrayPrototypeMap,
54
ObjectCreate,
@@ -14,6 +13,7 @@ const {
1413
validateHints,
1514
validateTimeout,
1615
emitInvalidHostnameWarning,
16+
getDefaultVerbatim,
1717
} = require('internal/dns/utils');
1818
const { codes, dnsException } = require('internal/errors');
1919
const { toASCII } = require('internal/idna');
@@ -103,7 +103,7 @@ function lookup(hostname, options) {
103103
var hints = 0;
104104
var family = -1;
105105
var all = false;
106-
var verbatim = false;
106+
var verbatim = getDefaultVerbatim();
107107

108108
// Parse arguments
109109
if (hostname && typeof hostname !== 'string') {

‎lib/internal/dns/utils.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,17 @@ function emitInvalidHostnameWarning(hostname) {
184184
);
185185
}
186186

187+
function getDefaultVerbatim() {
188+
const { NODE_DNS_VERBATIM } = process.env;
189+
switch (NODE_DNS_VERBATIM) {
190+
case '1':
191+
return true;
192+
case '0':
193+
default:
194+
return false;
195+
}
196+
}
197+
187198
module.exports = {
188199
bindDefaultResolver,
189200
getDefaultResolver,
@@ -192,4 +203,5 @@ module.exports = {
192203
validateTimeout,
193204
Resolver,
194205
emitInvalidHostnameWarning,
206+
getDefaultVerbatim,
195207
};

‎lib/internal/main/print_help.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const envVars = new SafeMap(ArrayPrototypeConcat([
3838
'categories that should print debug output' }],
3939
['NODE_DISABLE_COLORS', { helpText: 'set to 1 to disable colors in ' +
4040
'the REPL' }],
41+
['NODE_DNS_VERBATIM', { helpText: 'set to 1 or 0 to enable or disable ' +
42+
'verbatim in dns.lookup by default' }],
4143
['NODE_EXTRA_CA_CERTS', { helpText: 'path to additional CA certificates ' +
4244
'file. Only read once during process startup.' }],
4345
['NODE_NO_WARNINGS', { helpText: 'set to 1 to silence process warnings' }],
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { internalBinding } = require('internal/test/binding');
6+
const cares = internalBinding('cares_wrap');
7+
const { promisify } = require('util')
8+
9+
// Test that NODE_DNS_VERBATIM works as expected.
10+
11+
const originalGetaddrinfo = cares.getaddrinfo;
12+
const calls = [];
13+
cares.getaddrinfo = (...args) => {
14+
calls.push(args);
15+
originalGetaddrinfo(...args);
16+
};
17+
18+
const dns = require('dns');
19+
const dnsPromises = dns.promises;
20+
21+
(async () => {
22+
{
23+
process.env.NODE_DNS_VERBATIM = '1';
24+
let verbatim;
25+
26+
await promisify(dns.lookup)('example.org');
27+
28+
assert.strictEqual(calls.length, 1);
29+
verbatim = calls[0][4];
30+
assert.strictEqual(verbatim, true);
31+
32+
await dnsPromises.lookup('example.org');
33+
assert.strictEqual(calls.length, 2);
34+
verbatim = calls[1][4];
35+
assert.strictEqual(verbatim, true);
36+
37+
process.env.NODE_DNS_VERBATIM = '0';
38+
39+
await promisify(dns.lookup)('example.org');
40+
41+
assert.strictEqual(calls.length, 3);
42+
verbatim = calls[2][4];
43+
assert.strictEqual(verbatim, false);
44+
45+
await dnsPromises.lookup('example.org');
46+
assert.strictEqual(calls.length, 4);
47+
verbatim = calls[3][4];
48+
assert.strictEqual(verbatim, false);
49+
}
50+
})();

0 commit comments

Comments
 (0)