From bee5d94886c91f7d5ca3e1cab9a4d2cdfac8d268 Mon Sep 17 00:00:00 2001 From: Robo Date: Tue, 15 Aug 2023 19:19:45 +0900 Subject: [PATCH] feat: support dns-result-order Node.js cli flag (#39376) * feat: support dns-result-order Node.js cli flag * chore: update docs Co-authored-by: Erick Zhao * chore: remove patch --------- Co-authored-by: Erick Zhao --- docs/api/command-line-switches.md | 9 +++++++ shell/common/node_bindings.cc | 10 +++++--- spec/api-utility-process-spec.ts | 24 +++++++++++++++++++ .../api/utility-process/dns-result-order.js | 3 +++ 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 spec/fixtures/api/utility-process/dns-result-order.js diff --git a/docs/api/command-line-switches.md b/docs/api/command-line-switches.md index ee5e746420c2..fd63da4f1cbe 100644 --- a/docs/api/command-line-switches.md +++ b/docs/api/command-line-switches.md @@ -291,6 +291,15 @@ Print stack traces for deprecations. Print stack traces for process warnings (including deprecations). +### `--dns-result-order=order` + +Set the default value of the `verbatim` parameter in the Node.js [`dns.lookup()`](https://nodejs.org/api/dns.html#dnslookuphostname-options-callback) and [`dnsPromises.lookup()`](https://nodejs.org/api/dns.html#dnspromiseslookuphostname-options) functions. The value could be: + +* `ipv4first`: sets default `verbatim` `false`. +* `verbatim`: sets default `verbatim` `true`. + +The default is `verbatim` and `dns.setDefaultResultOrder()` have higher priority than `--dns-result-order`. + [app]: app.md [append-switch]: command-line.md#commandlineappendswitchswitch-value [debugging-main-process]: ../tutorial/debugging-main-process.md diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index 63a99e30e997..766d04305999 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -244,9 +244,13 @@ bool IsAllowedOption(base::StringPiece option) { }); // This should be aligned with what's possible to set via the process object. - static constexpr auto options = base::MakeFixedFlatSet( - {"--trace-warnings", "--trace-deprecation", "--throw-deprecation", - "--no-deprecation"}); + static constexpr auto options = base::MakeFixedFlatSet({ + "--trace-warnings", + "--trace-deprecation", + "--throw-deprecation", + "--no-deprecation", + "--dns-result-order", + }); if (debug_options.contains(option)) return electron::fuses::IsNodeCliInspectEnabled(); diff --git a/spec/api-utility-process-spec.ts b/spec/api-utility-process-spec.ts index 2afaeca61e77..278fbfba62ef 100644 --- a/spec/api-utility-process-spec.ts +++ b/spec/api-utility-process-spec.ts @@ -257,6 +257,30 @@ describe('utilityProcess module', () => { child.stdout!.on('data', listener); }); + it('supports changing dns verbatim with --dns-result-order', (done) => { + const child = utilityProcess.fork(path.join(fixturesPath, 'dns-result-order.js'), [], { + stdio: 'pipe', + execArgv: ['--dns-result-order=ipv4first'] + }); + + let output = ''; + const cleanup = () => { + child.stderr!.removeListener('data', listener); + child.stdout!.removeListener('data', listener); + child.once('exit', () => { done(); }); + child.kill(); + }; + + const listener = (data: Buffer) => { + output += data; + expect(output.trim()).to.contain('ipv4first', 'default verbatim should be ipv4first'); + cleanup(); + }; + + child.stderr!.on('data', listener); + child.stdout!.on('data', listener); + }); + ifit(process.platform !== 'win32')('supports redirecting stdout to parent process', async () => { const result = 'Output from utility process'; const appProcess = childProcess.spawn(process.execPath, [path.join(fixturesPath, 'inherit-stdout'), `--payload=${result}`]); diff --git a/spec/fixtures/api/utility-process/dns-result-order.js b/spec/fixtures/api/utility-process/dns-result-order.js new file mode 100644 index 000000000000..74d1219003c6 --- /dev/null +++ b/spec/fixtures/api/utility-process/dns-result-order.js @@ -0,0 +1,3 @@ +const dns = require('node:dns'); +console.log(dns.getDefaultResultOrder()); +process.exit(0);