From 3202b79990888cf88463067d3f97888cf2315abd Mon Sep 17 00:00:00 2001 From: Emily Strickland Date: Fri, 8 Apr 2022 15:42:31 -0700 Subject: [PATCH 1/5] Set `prefetchTimeout` setting default to 5000 ms This change adds a `prefetchTimeout` setting to the default configuration and sets it to 5000 milliseconds. Its description indicates the advantages and disadvantages of changing its default value. --- defaults/config.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/defaults/config.js b/defaults/config.js index 89212c39..eba57777 100644 --- a/defaults/config.js +++ b/defaults/config.js @@ -159,6 +159,18 @@ module.exports = { // // This value is set to `50` kilobytes by default. prefetchMaxSearchSize: 50, + + // ### `prefetchTimeout` + // + // When `prefetch` is enabled, this value sets the number of milliseconds + // before The Lounge gives up attempting to fetch a link. This can be useful + // if you've increased the `prefetchMaxImageSize`. + // + // Take caution, however, that an inordinately large value may lead to + // performance issues or even a denial of service, since The Lounge will not + // be able to clean up outgoing connections as quickly. Usually the default + // value is appropriate, so only change it if necessary. + prefetchTimeout: 5000, // ### `fileUpload` // From 379648521711cea80396e34f02db030faf823a13 Mon Sep 17 00:00:00 2001 From: Emily Strickland Date: Fri, 8 Apr 2022 15:45:37 -0700 Subject: [PATCH 2/5] Configure link fetch to use `prefetchTimeout` This change modifies the `fetch` function in `link.js` to use the new `prefetchTimeout` config setting introduced in the previous commit. This allows configuring the length of the timeout. I've added a comment here to indicate milliseconds are the unit in use, since otherwise that would no longer be obvious from the code without looking at the default value (which could change). --- src/plugins/irc-events/link.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/irc-events/link.js b/src/plugins/irc-events/link.js index 00236def..e76fa54b 100644 --- a/src/plugins/irc-events/link.js +++ b/src/plugins/irc-events/link.js @@ -390,7 +390,7 @@ function fetch(uri, headers) { try { const gotStream = got.stream(uri, { retry: 0, - timeout: 5000, + timeout: Helper.config.prefetchTimeout, // milliseconds headers: getRequestHeaders(headers), https: { rejectUnauthorized: false, From b2a363f0999cd46daf46dae1d30ff313ec275144 Mon Sep 17 00:00:00 2001 From: Emily Strickland Date: Sat, 9 Apr 2022 00:17:57 +0000 Subject: [PATCH 3/5] Document default value for prefetchTimeout --- defaults/config.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/defaults/config.js b/defaults/config.js index eba57777..42004794 100644 --- a/defaults/config.js +++ b/defaults/config.js @@ -170,6 +170,8 @@ module.exports = { // performance issues or even a denial of service, since The Lounge will not // be able to clean up outgoing connections as quickly. Usually the default // value is appropriate, so only change it if necessary. + // + // This value is set to `5000` milliseconds by default. prefetchTimeout: 5000, // ### `fileUpload` From ff886846a88c7bf4383ba070e76ef62f7a981760 Mon Sep 17 00:00:00 2001 From: Emily Strickland Date: Sat, 9 Apr 2022 00:19:08 +0000 Subject: [PATCH 4/5] Warn about unset prefetchTimeout, default to 5000 ms --- src/plugins/irc-events/link.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/plugins/irc-events/link.js b/src/plugins/irc-events/link.js index e76fa54b..ae2ed01b 100644 --- a/src/plugins/irc-events/link.js +++ b/src/plugins/irc-events/link.js @@ -10,6 +10,8 @@ const storage = require("../storage"); const currentFetchPromises = new Map(); const imageTypeRegex = /^image\/.+/; const mediaTypeRegex = /^(audio|video)\/.+/; +const log = require("../../log"); + module.exports = function (client, chan, msg, cleanText) { if (!Helper.config.prefetch) { @@ -381,6 +383,12 @@ function fetch(uri, headers) { return promise; } + const prefetchTimeout = Helper.config.prefetchTimeout; + + if (!prefetchTimeout) { + log.warn("prefetchTimeout is missing from your The Lounge configuration, defaulting to 5000 ms"); + } + promise = new Promise((resolve, reject) => { let buffer = Buffer.from(""); let contentLength = 0; @@ -390,7 +398,7 @@ function fetch(uri, headers) { try { const gotStream = got.stream(uri, { retry: 0, - timeout: Helper.config.prefetchTimeout, // milliseconds + timeout: prefetchTimeout || 5000, // milliseconds headers: getRequestHeaders(headers), https: { rejectUnauthorized: false, From 3a842903144620af1b0a5be781ec13be77b9ba54 Mon Sep 17 00:00:00 2001 From: Emily Strickland Date: Sat, 9 Apr 2022 19:40:38 +0000 Subject: [PATCH 5/5] Apply fixes suggested by Prettier to fix CI --- defaults/config.js | 2 +- src/plugins/irc-events/link.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/defaults/config.js b/defaults/config.js index 42004794..25afdd2c 100644 --- a/defaults/config.js +++ b/defaults/config.js @@ -159,7 +159,7 @@ module.exports = { // // This value is set to `50` kilobytes by default. prefetchMaxSearchSize: 50, - + // ### `prefetchTimeout` // // When `prefetch` is enabled, this value sets the number of milliseconds diff --git a/src/plugins/irc-events/link.js b/src/plugins/irc-events/link.js index ae2ed01b..0d7a5061 100644 --- a/src/plugins/irc-events/link.js +++ b/src/plugins/irc-events/link.js @@ -12,7 +12,6 @@ const imageTypeRegex = /^image\/.+/; const mediaTypeRegex = /^(audio|video)\/.+/; const log = require("../../log"); - module.exports = function (client, chan, msg, cleanText) { if (!Helper.config.prefetch) { return; @@ -386,7 +385,9 @@ function fetch(uri, headers) { const prefetchTimeout = Helper.config.prefetchTimeout; if (!prefetchTimeout) { - log.warn("prefetchTimeout is missing from your The Lounge configuration, defaulting to 5000 ms"); + log.warn( + "prefetchTimeout is missing from your The Lounge configuration, defaulting to 5000 ms" + ); } promise = new Promise((resolve, reject) => {