From 6c31b976ebca5fbfe863fbd9e0aa70b412ee36c9 Mon Sep 17 00:00:00 2001 From: veeck Date: Sat, 29 Oct 2022 22:41:44 +0200 Subject: [PATCH 1/4] Switch back to third party fetch lib for all node versions --- CHANGELOG.md | 2 ++ js/fetch.js | 27 +++++++++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd7fafe13b..5e606bfcdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ Special thanks to: @rejas, @sdetweil ### Removed +- Removed usage of internal fetch function of node until it is more stable. + ### Updated - Cleaned up test directory (#2937) and jest config (#2959) diff --git a/js/fetch.js b/js/fetch.js index 8009dc16f3..35cb6c58b9 100644 --- a/js/fetch.js +++ b/js/fetch.js @@ -1,20 +1,27 @@ /** - * fetch + * Helper class to provide either third party fetch library or (if node >= 18) + * return internal node fetch implementation. + * + * Attention: After some discussion we always return the third party + * implementation until the node implementation is stable and more tested + * @see https://github.com/MichMich/MagicMirror/pull/2952 * * @param {string} url to be fetched * @param {object} options object e.g. for headers * @class */ async function fetch(url, options = {}) { - const nodeVersion = process.version.match(/^v(\d+)\.*/)[1]; - if (nodeVersion >= 18) { - // node version >= 18 - return global.fetch(url, options); - } else { - // node version < 18 - const nodefetch = require("node-fetch"); - return nodefetch(url, options); - } + // const nodeVersion = process.version.match(/^v(\d+)\.*/)[1]; + // if (nodeVersion >= 18) { + // // node version >= 18 + // return global.fetch(url, options); + // } else { + // // node version < 18 + // const nodefetch = require("node-fetch"); + // return nodefetch(url, options); + // } + const nodefetch = require("node-fetch"); + return nodefetch(url, options); } module.exports = fetch; From 26342c865c56692683467f117a5ead6d935c72f2 Mon Sep 17 00:00:00 2001 From: veeck Date: Sat, 29 Oct 2022 22:59:32 +0200 Subject: [PATCH 2/4] Fix jsdoc --- js/fetch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/fetch.js b/js/fetch.js index 35cb6c58b9..7055315f82 100644 --- a/js/fetch.js +++ b/js/fetch.js @@ -4,8 +4,8 @@ * * Attention: After some discussion we always return the third party * implementation until the node implementation is stable and more tested - * @see https://github.com/MichMich/MagicMirror/pull/2952 * + * @see https://github.com/MichMich/MagicMirror/pull/2952 * @param {string} url to be fetched * @param {object} options object e.g. for headers * @class From 7eb1cd6ff2d313785b8e1d546ace4a246f6f3317 Mon Sep 17 00:00:00 2001 From: veeck Date: Sat, 29 Oct 2022 23:04:14 +0200 Subject: [PATCH 3/4] Revert fetch change in tests --- tests/e2e/helpers/global-setup.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/e2e/helpers/global-setup.js b/tests/e2e/helpers/global-setup.js index 7f60b40e2c..ee8fb83ef2 100644 --- a/tests/e2e/helpers/global-setup.js +++ b/tests/e2e/helpers/global-setup.js @@ -1,4 +1,5 @@ const jsdom = require("jsdom"); +const corefetch = require("fetch"); exports.startApplication = async (configFilename, exec) => { jest.resetModules(); @@ -82,8 +83,13 @@ exports.waitForAllElements = (selector) => { }); }; -// When native fetch is used keep-alive is set which causes issues with tests that should not share the connection, fall back to use the older one for now... -exports.fetch = require("node-fetch"); +exports.fetch = (url) => { + return new Promise((resolve) => { + corefetch(url).then((res) => { + resolve(res); + }); + }); +}; exports.testMatch = async (element, regex) => { const elem = await this.waitForElement(element); From 99bbe0c1bbd9aea9a722ba46b80c31d4269590f5 Mon Sep 17 00:00:00 2001 From: veeck Date: Sat, 29 Oct 2022 23:04:52 +0200 Subject: [PATCH 4/4] Update jsdoc --- js/fetch.js | 1 + 1 file changed, 1 insertion(+) diff --git a/js/fetch.js b/js/fetch.js index 7055315f82..3ae874c813 100644 --- a/js/fetch.js +++ b/js/fetch.js @@ -6,6 +6,7 @@ * implementation until the node implementation is stable and more tested * * @see https://github.com/MichMich/MagicMirror/pull/2952 + * @see https://github.com/MichMich/MagicMirror/issues/2649 * @param {string} url to be fetched * @param {object} options object e.g. for headers * @class