diff --git a/benchmark/tls/get-allow-unauthorized.js b/benchmark/tls/get-allow-unauthorized.js new file mode 100644 index 000000000000..194c317a30fd --- /dev/null +++ b/benchmark/tls/get-allow-unauthorized.js @@ -0,0 +1,34 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert'); + +const options = { + flags: ['--expose-internals'], +}; + +const bench = common.createBenchmark(main, { + n: [1024 * 1024 * 16], +}, options); + +function main({ n }) { + const { getAllowUnauthorized } = require('internal/options'); + + const length = 1024; + const array = new Array(length).fill(false); + + bench.start(); + + for (let i = 0; i < n; ++i) { + const index = i % length; + array[index] = getAllowUnauthorized(); + } + + bench.end(n); + + // Verify the entries to prevent dead code elimination from making + // the benchmark invalid. + for (let i = 0; i < length; ++i) { + assert.strictEqual(typeof array[i], 'boolean'); + } +} diff --git a/lib/internal/options.js b/lib/internal/options.js index 68acdb2a7b37..24681207ac47 100644 --- a/lib/internal/options.js +++ b/lib/internal/options.js @@ -7,6 +7,7 @@ const { } = internalBinding('options'); let warnOnAllowUnauthorized = true; +let allowUnauthorized = false; let optionsDict; let cliInfo; @@ -47,15 +48,16 @@ function getOptionValue(optionName) { } function getAllowUnauthorized() { - const allowUnauthorized = process.env.NODE_TLS_REJECT_UNAUTHORIZED === '0'; - - if (allowUnauthorized && warnOnAllowUnauthorized) { + if (warnOnAllowUnauthorized) { warnOnAllowUnauthorized = false; - process.emitWarning( - 'Setting the NODE_TLS_REJECT_UNAUTHORIZED ' + - 'environment variable to \'0\' makes TLS connections ' + - 'and HTTPS requests insecure by disabling ' + - 'certificate verification.'); + allowUnauthorized = process.env.NODE_TLS_REJECT_UNAUTHORIZED === '0'; + if (allowUnauthorized) { + process.emitWarning( + 'Setting the NODE_TLS_REJECT_UNAUTHORIZED ' + + 'environment variable to \'0\' makes TLS connections ' + + 'and HTTPS requests insecure by disabling ' + + 'certificate verification.'); + } } return allowUnauthorized; }