From b577c1b847c72714b1bd77d3b372befb461f9595 Mon Sep 17 00:00:00 2001 From: Nathan Byrd Date: Sun, 12 Feb 2023 13:38:52 -0600 Subject: [PATCH] Added default connection timeout and new config option outbound.connectionTimeoutMilliseconds --- core/http_util.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/core/http_util.js b/core/http_util.js index d7bf57a8..b7b01437 100644 --- a/core/http_util.js +++ b/core/http_util.js @@ -1,10 +1,14 @@ const { Errors } = require('./enig_error.js'); // deps -const { isString, isObject, truncate } = require('lodash'); +const { isString, isObject, truncate, get, has } = require('lodash'); const https = require('https'); const httpSignature = require('http-signature'); const crypto = require('crypto'); +const Config = require('./config.js').get; + +const TimeoutConfigPath = 'outbound.connectionTimeoutMilliseconds'; +const DefaultTimeoutMilliseconds = 5000; exports.getJson = getJson; exports.postJson = postJson; @@ -56,6 +60,15 @@ function postJson(url, json, options, cb) { } function _makeRequest(url, options, cb) { + let defaultTimeout = DefaultTimeoutMilliseconds; + // Only set to config value if it has one, this allows us to set it + // to zero, but still have a default if none is set + if (has(Config(), TimeoutConfigPath)) { + defaultTimeout = get(Config(), TimeoutConfigPath); + } + + options = Object.assign({}, options, { timeout: defaultTimeout }); // Let options override default timeout if needed + if (options.body) { options.headers['Content-Length'] = Buffer.from(options.body).length;