From 0023c64d59fc29958b5c0d632aab781161c6ae65 Mon Sep 17 00:00:00 2001 From: Karsten Hassel Date: Fri, 27 May 2022 19:46:28 +0200 Subject: [PATCH] use internal fetch function of node instead external `node-fetch` library if node version >= `v18` --- CHANGELOG.md | 1 + js/fetch.js | 20 ++++++++++++++++++++ js/server.js | 2 +- modules/default/calendar/calendarfetcher.js | 2 +- modules/default/newsfeed/newsfeedfetcher.js | 2 +- package.json | 3 ++- tests/e2e/env_spec.js | 6 +++--- tests/e2e/fonts.js | 6 +++--- tests/e2e/global-setup.js | 3 ++- tests/e2e/ipWhitelist_spec.js | 10 +++++----- tests/e2e/modules/alert_spec.js | 4 ++-- tests/e2e/modules/calendar_spec.js | 4 ++-- tests/e2e/modules/clock_es_spec.js | 4 ++-- tests/e2e/modules/clock_spec.js | 4 ++-- tests/e2e/modules/compliments_spec.js | 4 ++-- tests/e2e/modules/helloworld_spec.js | 4 ++-- tests/e2e/modules/newsfeed_spec.js | 4 ++-- tests/e2e/modules/weather_spec.js | 4 ++-- tests/e2e/modules_display_spec.js | 4 ++-- tests/e2e/modules_position_spec.js | 4 ++-- tests/e2e/port_config.js | 10 +++++----- tests/e2e/vendor_spec.js | 6 +++--- tests/e2e/without_modules.js | 4 ++-- 23 files changed, 69 insertions(+), 46 deletions(-) create mode 100644 js/fetch.js diff --git a/CHANGELOG.md b/CHANGELOG.md index d3ce0e91..db37766c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ _This release is scheduled to be released on 2022-07-01._ - Use latest node 18 when running tests on github actions - Update `electron` to v18 and other dependencies. +- Use internal fetch function of node instead external `node-fetch` library if node version >= `v18`. ### Fixed diff --git a/js/fetch.js b/js/fetch.js new file mode 100644 index 00000000..fb855f2a --- /dev/null +++ b/js/fetch.js @@ -0,0 +1,20 @@ +/** + * fetch + * + * @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); + } +} + +module.exports = fetch; diff --git a/js/server.js b/js/server.js index 3e73e99a..27d1caef 100644 --- a/js/server.js +++ b/js/server.js @@ -10,7 +10,7 @@ const path = require("path"); const ipfilter = require("express-ipfilter").IpFilter; const fs = require("fs"); const helmet = require("helmet"); -const fetch = require("node-fetch"); +const fetch = require("fetch"); const Log = require("logger"); const Utils = require("./utils.js"); diff --git a/modules/default/calendar/calendarfetcher.js b/modules/default/calendar/calendarfetcher.js index f27025d7..0798d178 100644 --- a/modules/default/calendar/calendarfetcher.js +++ b/modules/default/calendar/calendarfetcher.js @@ -8,7 +8,7 @@ const CalendarUtils = require("./calendarutils"); const Log = require("logger"); const NodeHelper = require("node_helper"); const ical = require("node-ical"); -const fetch = require("node-fetch"); +const fetch = require("fetch"); const digest = require("digest-fetch"); const https = require("https"); diff --git a/modules/default/newsfeed/newsfeedfetcher.js b/modules/default/newsfeed/newsfeedfetcher.js index dc69e4ce..5f3f8261 100644 --- a/modules/default/newsfeed/newsfeedfetcher.js +++ b/modules/default/newsfeed/newsfeedfetcher.js @@ -7,7 +7,7 @@ const Log = require("logger"); const FeedMe = require("feedme"); const NodeHelper = require("node_helper"); -const fetch = require("node-fetch"); +const fetch = require("fetch"); const iconv = require("iconv-lite"); /** diff --git a/package.json b/package.json index ecccce60..8ec9792b 100644 --- a/package.json +++ b/package.json @@ -89,7 +89,8 @@ }, "_moduleAliases": { "node_helper": "js/node_helper.js", - "logger": "js/logger.js" + "logger": "js/logger.js", + "fetch": "js/fetch.js" }, "engines": { "node": ">=14" diff --git a/tests/e2e/env_spec.js b/tests/e2e/env_spec.js index 1eb0fe1e..496086c5 100644 --- a/tests/e2e/env_spec.js +++ b/tests/e2e/env_spec.js @@ -1,4 +1,4 @@ -const fetch = require("node-fetch"); +const fetch = require("fetch"); const helpers = require("./global-setup"); describe("App environment", function () { @@ -6,8 +6,8 @@ describe("App environment", function () { helpers.startApplication("tests/configs/default.js"); helpers.getDocument(done); }); - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); it("get request from http://localhost:8080 should return 200", function (done) { diff --git a/tests/e2e/fonts.js b/tests/e2e/fonts.js index 54cd2d87..056b0306 100644 --- a/tests/e2e/fonts.js +++ b/tests/e2e/fonts.js @@ -1,4 +1,4 @@ -const fetch = require("node-fetch"); +const fetch = require("fetch"); const helpers = require("./global-setup"); describe("All font files from roboto.css should be downloadable", function () { @@ -17,8 +17,8 @@ describe("All font files from roboto.css should be downloadable", function () { beforeAll(function () { helpers.startApplication("tests/configs/without_modules.js"); }); - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); test.each(fontFiles)("should return 200 HTTP code for file '%s'", (fontFile, done) => { diff --git a/tests/e2e/global-setup.js b/tests/e2e/global-setup.js index 3cf8e908..37e3315a 100644 --- a/tests/e2e/global-setup.js +++ b/tests/e2e/global-setup.js @@ -16,10 +16,11 @@ exports.startApplication = function (configFilename, exec) { global.app.start(); }; -exports.stopApplication = function () { +exports.stopApplication = async function () { if (global.app) { global.app.stop(); } + await new Promise((resolve) => setTimeout(resolve, 100)); }; exports.getDocument = function (callback) { diff --git a/tests/e2e/ipWhitelist_spec.js b/tests/e2e/ipWhitelist_spec.js index b518cfff..3c3a06c3 100644 --- a/tests/e2e/ipWhitelist_spec.js +++ b/tests/e2e/ipWhitelist_spec.js @@ -1,4 +1,4 @@ -const fetch = require("node-fetch"); +const fetch = require("fetch"); const helpers = require("./global-setup"); describe("ipWhitelist directive configuration", function () { @@ -6,8 +6,8 @@ describe("ipWhitelist directive configuration", function () { beforeAll(function () { helpers.startApplication("tests/configs/noIpWhiteList.js"); }); - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); it("should return 403", function (done) { @@ -22,8 +22,8 @@ describe("ipWhitelist directive configuration", function () { beforeAll(function () { helpers.startApplication("tests/configs/empty_ipWhiteList.js"); }); - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); it("should return 200", function (done) { diff --git a/tests/e2e/modules/alert_spec.js b/tests/e2e/modules/alert_spec.js index 437c15b0..772a6247 100644 --- a/tests/e2e/modules/alert_spec.js +++ b/tests/e2e/modules/alert_spec.js @@ -5,8 +5,8 @@ describe("Alert module", function () { helpers.startApplication("tests/configs/modules/alert/default.js"); helpers.getDocument(done); }); - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); it("should show the welcome message", function () { diff --git a/tests/e2e/modules/calendar_spec.js b/tests/e2e/modules/calendar_spec.js index c3066060..e819d7be 100644 --- a/tests/e2e/modules/calendar_spec.js +++ b/tests/e2e/modules/calendar_spec.js @@ -25,8 +25,8 @@ describe("Calendar module", function () { }); }; - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); describe("Default configuration", function () { diff --git a/tests/e2e/modules/clock_es_spec.js b/tests/e2e/modules/clock_es_spec.js index c20c5322..c498efc7 100644 --- a/tests/e2e/modules/clock_es_spec.js +++ b/tests/e2e/modules/clock_es_spec.js @@ -1,8 +1,8 @@ const helpers = require("../global-setup"); describe("Clock set to spanish language module", function () { - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); const testMatch = function (element, regex) { diff --git a/tests/e2e/modules/clock_spec.js b/tests/e2e/modules/clock_spec.js index 00214670..baa5a1d0 100644 --- a/tests/e2e/modules/clock_spec.js +++ b/tests/e2e/modules/clock_spec.js @@ -2,8 +2,8 @@ const helpers = require("../global-setup"); const moment = require("moment"); describe("Clock module", function () { - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); const testMatch = function (element, regex) { diff --git a/tests/e2e/modules/compliments_spec.js b/tests/e2e/modules/compliments_spec.js index d9ea4f0c..bea87906 100644 --- a/tests/e2e/modules/compliments_spec.js +++ b/tests/e2e/modules/compliments_spec.js @@ -16,8 +16,8 @@ function doTest(complimentsArray) { } describe("Compliments module", function () { - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); describe("parts of days", function () { diff --git a/tests/e2e/modules/helloworld_spec.js b/tests/e2e/modules/helloworld_spec.js index f80e47ba..98d65663 100644 --- a/tests/e2e/modules/helloworld_spec.js +++ b/tests/e2e/modules/helloworld_spec.js @@ -1,8 +1,8 @@ const helpers = require("../global-setup"); describe("Test helloworld module", function () { - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); describe("helloworld set config text", function () { diff --git a/tests/e2e/modules/newsfeed_spec.js b/tests/e2e/modules/newsfeed_spec.js index af8d679b..0bc6ebdd 100644 --- a/tests/e2e/modules/newsfeed_spec.js +++ b/tests/e2e/modules/newsfeed_spec.js @@ -1,8 +1,8 @@ const helpers = require("../global-setup"); describe("Newsfeed module", function () { - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); describe("Default configuration", function () { diff --git a/tests/e2e/modules/weather_spec.js b/tests/e2e/modules/weather_spec.js index bb9489c7..008d1a81 100644 --- a/tests/e2e/modules/weather_spec.js +++ b/tests/e2e/modules/weather_spec.js @@ -40,8 +40,8 @@ describe("Weather module", function () { helpers.getDocument(callback); } - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); describe("Current weather", function () { diff --git a/tests/e2e/modules_display_spec.js b/tests/e2e/modules_display_spec.js index 432b1806..89b2babe 100644 --- a/tests/e2e/modules_display_spec.js +++ b/tests/e2e/modules_display_spec.js @@ -5,8 +5,8 @@ describe("Display of modules", function () { helpers.startApplication("tests/configs/modules/display.js"); helpers.getDocument(done); }); - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); it("should show the test header", function () { diff --git a/tests/e2e/modules_position_spec.js b/tests/e2e/modules_position_spec.js index c0cfa474..55ef2091 100644 --- a/tests/e2e/modules_position_spec.js +++ b/tests/e2e/modules_position_spec.js @@ -5,8 +5,8 @@ describe("Position of modules", function () { helpers.startApplication("tests/configs/modules/positions.js"); helpers.getDocument(done); }); - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); const positions = ["top_bar", "top_left", "top_center", "top_right", "upper_third", "middle_center", "lower_third", "bottom_left", "bottom_center", "bottom_right", "bottom_bar", "fullscreen_above", "fullscreen_below"]; diff --git a/tests/e2e/port_config.js b/tests/e2e/port_config.js index 329acab6..4a168d91 100644 --- a/tests/e2e/port_config.js +++ b/tests/e2e/port_config.js @@ -1,4 +1,4 @@ -const fetch = require("node-fetch"); +const fetch = require("fetch"); const helpers = require("./global-setup"); describe("port directive configuration", function () { @@ -6,8 +6,8 @@ describe("port directive configuration", function () { beforeAll(function () { helpers.startApplication("tests/configs/port_8090.js"); }); - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); it("should return 200", function (done) { @@ -22,8 +22,8 @@ describe("port directive configuration", function () { beforeAll(function () { helpers.startApplication("tests/configs/port_8090.js", (process.env.MM_PORT = 8100)); }); - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); it("should return 200", function (done) { diff --git a/tests/e2e/vendor_spec.js b/tests/e2e/vendor_spec.js index f6917c0d..3c6303fb 100644 --- a/tests/e2e/vendor_spec.js +++ b/tests/e2e/vendor_spec.js @@ -1,12 +1,12 @@ -const fetch = require("node-fetch"); +const fetch = require("fetch"); const helpers = require("./global-setup"); describe("Vendors", function () { beforeAll(function () { helpers.startApplication("tests/configs/default.js"); }); - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); describe("Get list vendors", function () { diff --git a/tests/e2e/without_modules.js b/tests/e2e/without_modules.js index e60e2312..cc8a6212 100644 --- a/tests/e2e/without_modules.js +++ b/tests/e2e/without_modules.js @@ -5,8 +5,8 @@ describe("Check configuration without modules", function () { helpers.startApplication("tests/configs/without_modules.js"); helpers.getDocument(done); }); - afterAll(function () { - helpers.stopApplication(); + afterAll(async function () { + await helpers.stopApplication(); }); it("Show the message MagicMirror² title", function () {