use internal fetch function of node instead external node-fetch library if node version >= v18

This commit is contained in:
Karsten Hassel 2022-05-27 19:46:28 +02:00
parent cbda20f67e
commit 0023c64d59
23 changed files with 69 additions and 46 deletions

View File

@ -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

20
js/fetch.js Normal file
View File

@ -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;

View File

@ -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");

View File

@ -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");

View File

@ -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");
/**

View File

@ -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"

View File

@ -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) {

View File

@ -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) => {

View File

@ -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) {

View File

@ -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) {

View File

@ -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 () {

View File

@ -25,8 +25,8 @@ describe("Calendar module", function () {
});
};
afterAll(function () {
helpers.stopApplication();
afterAll(async function () {
await helpers.stopApplication();
});
describe("Default configuration", function () {

View File

@ -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) {

View File

@ -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) {

View File

@ -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 () {

View File

@ -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 () {

View File

@ -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 () {

View File

@ -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 () {

View File

@ -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 () {

View File

@ -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"];

View File

@ -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) {

View File

@ -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 () {

View File

@ -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 () {