From f2bddcce3ff53f05bdd7b2dcd9ebdb4030aa668f Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sat, 10 Aug 2024 03:41:48 +0200 Subject: [PATCH] tls: improve performance of getAllowUnauthorized --- benchmark/tls/get-allow-unauthorized.js | 34 +++++++++++++++++++++++++ lib/internal/options.js | 18 +++++++------ 2 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 benchmark/tls/get-allow-unauthorized.js 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; }