Skip to content

Commit 5790a50

Browse files
committed
dns, src: add --preserve-dns-order flag with NODE_OPTIONS whitelist
Added flag with NODE_OPTIONS whitelist to allow preserving lookup order as retrieved from resolver. Still allows individual lookup requests with verbatim flag assigned explicitly to override the config flag. Open for suggestions on how to craft a test for this. Refs: nodejs#14731 (comment)
1 parent d6b1b84 commit 5790a50

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

lib/dns.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
const util = require('util');
2525

2626
const cares = process.binding('cares_wrap');
27+
const { preserveDnsOrder } = process.binding('config');
2728
const { isLegalPort } = require('internal/net');
2829
const { customPromisifyArgs } = require('internal/util');
2930
const errors = require('internal/errors');
@@ -128,7 +129,7 @@ function lookup(hostname, options, callback) {
128129
var hints = 0;
129130
var family = -1;
130131
var all = false;
131-
var verbatim = false;
132+
var verbatim = !!preserveDnsOrder;
132133

133134
// Parse arguments
134135
if (hostname && typeof hostname !== 'string') {
@@ -143,7 +144,10 @@ function lookup(hostname, options, callback) {
143144
hints = options.hints >>> 0;
144145
family = options.family >>> 0;
145146
all = options.all === true;
146-
verbatim = options.verbatim === true;
147+
148+
if (Object.prototype.hasOwnProperty.call(options, 'verbatim')) {
149+
verbatim = options.verbatim === true;
150+
}
147151

148152
if (hints !== 0 &&
149153
hints !== cares.AI_ADDRCONFIG &&

src/node.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ std::string config_warning_file; // NOLINT(runtime/string)
258258
// that is used by lib/internal/bootstrap_node.js
259259
bool config_expose_internals = false;
260260

261+
// Set in node.cc by ParseArgs when --preserve-dns-order is used.
262+
// Used in node_config.cc to set a constant on process.binding('config')
263+
// that is used by lib/dns.js
264+
bool config_preserve_dns_order = false;
265+
261266
bool v8_initialized = false;
262267

263268
bool linux_at_secure = false;
@@ -3626,6 +3631,7 @@ static void CheckIfAllowedInEnv(const char* exe, bool is_env,
36263631
"--pending-deprecation",
36273632
"--no-warnings",
36283633
"--napi-modules",
3634+
"--preserve-dns-order",
36293635
"--expose-http2", // keep as a non-op through v9.x
36303636
"--experimental-modules",
36313637
"--loader",
@@ -3848,6 +3854,9 @@ static void ParseArgs(int* argc,
38483854
} else if (strcmp(arg, "--expose-http2") == 0 ||
38493855
strcmp(arg, "--expose_http2") == 0) {
38503856
// Keep as a non-op through v9.x
3857+
} else if (strcmp(arg, "--preserve-dns-order") == 0 ||
3858+
strcmp(arg, "--preserve_dns_order") == 0) {
3859+
config_preserve_dns_order = true;
38513860
} else if (strcmp(arg, "-") == 0) {
38523861
break;
38533862
} else if (strcmp(arg, "--") == 0) {

src/node_config.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ static void InitConfig(Local<Object> target,
8888
if (config_expose_internals)
8989
READONLY_BOOLEAN_PROPERTY("exposeInternals");
9090

91+
if (config_preserve_dns_order)
92+
READONLY_BOOLEAN_PROPERTY("preserveDnsOrder");
93+
9194
READONLY_PROPERTY(target,
9295
"bits",
9396
Number::New(env->isolate(), 8 * sizeof(intptr_t)));

src/node_internals.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ extern bool config_preserve_symlinks;
171171
// that is used by lib/module.js
172172
extern bool config_experimental_modules;
173173

174+
// Set in node.cc by ParseArgs when --preserve-dns-order is used.
175+
// Used in node_config.cc to set a constant on process.binding('config')
176+
// that is used by lib/dns.js
177+
extern bool config_preserve_dns_order;
178+
174179
// Set in node.cc by ParseArgs when --loader is used.
175180
// Used in node_config.cc to set a constant on process.binding('config')
176181
// that is used by lib/internal/bootstrap_node.js

0 commit comments

Comments
 (0)