diff --git a/modules/default/calendar/calendarfetcher.js b/modules/default/calendar/calendarfetcher.js index 73f92b1e..b0849da7 100644 --- a/modules/default/calendar/calendarfetcher.js +++ b/modules/default/calendar/calendarfetcher.js @@ -6,7 +6,8 @@ */ const Log = require("logger"); const ical = require("node-ical"); -const request = require("request"); +const fetch = require("node-fetch"); +const https = require('https'); /** * Moment date @@ -43,22 +44,22 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn const fetchCalendar = function () { clearTimeout(reloadTimer); reloadTimer = null; + httpsAgent = null; const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]); - const opts = { - headers: { - "User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)" - }, - gzip: true + const headers = { + "User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)" }; if (selfSignedCert) { - var agentOptions = { - rejectUnauthorized: false - }; - opts.agentOptions = agentOptions; + httpsAgent = new https.Agent({ + rejectUnauthorized: false, + }); } +// todo: auth, +// https://github.com/devfans/digest-fetch +// https://hackersandslackers.com/making-api-requests-with-nodejs/ if (auth) { if (auth.method === "bearer") { opts.auth = { @@ -73,25 +74,27 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn } } - request(url, opts, function (err, r, requestData) { - if (err) { - fetchFailedCallback(self, err); - scheduleTimer(); - return; - } else if (r.statusCode !== 200) { - fetchFailedCallback(self, r.statusCode + ": " + r.statusMessage); - scheduleTimer(); - return; + fetch(url, { headers: headers, httpsAgent: httpsAgent }) + .catch(error => { + fetchFailedCallback(self, error); + scheduleTimer(); + }) + .then(response => { + if (response.status !== 200) { + throw new Error(response.status + ": " + response.statusText); } + return response; + }) + .then(response => response.text()) + .then(responseData => { let data = []; try { - data = ical.parseICS(requestData); + data = ical.parseICS(responseData); } catch (error) { fetchFailedCallback(self, error.message); scheduleTimer(); - return; } Log.debug(" parsed data=" + JSON.stringify(data)); diff --git a/modules/default/newsfeed/newsfeedfetcher.js b/modules/default/newsfeed/newsfeedfetcher.js index 68ac75ac..48c4cbbd 100644 --- a/modules/default/newsfeed/newsfeedfetcher.js +++ b/modules/default/newsfeed/newsfeedfetcher.js @@ -6,7 +6,7 @@ */ const Log = require("logger"); const FeedMe = require("feedme"); -const request = require("request"); +const fetch = require("node-fetch"); const iconv = require("iconv-lite"); /** @@ -79,22 +79,21 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings }); const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]); - const opts = { - headers: { - "User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)", - "Cache-Control": "max-age=0, no-cache, no-store, must-revalidate", - Pragma: "no-cache" - }, - encoding: null + const headers = { + "User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)", + "Cache-Control": "max-age=0, no-cache, no-store, must-revalidate", + Pragma: "no-cache" }; - request(url, opts) - .on("error", function (error) { + fetch(url, { headers: headers }) + .catch(error => { fetchFailedCallback(self, error); scheduleTimer(); }) - .pipe(iconv.decodeStream(encoding)) - .pipe(parser); + .then(res => { + res.body.pipe(iconv.decodeStream(encoding)).pipe(parser); + }); + }; /** diff --git a/package.json b/package.json index 8ca6cd30..97e52710 100644 --- a/package.json +++ b/package.json @@ -80,8 +80,8 @@ "iconv-lite": "^0.6.2", "module-alias": "^2.2.2", "moment": "^2.29.1", + "node-fetch": "^2.6.1", "node-ical": "^0.12.7", - "request": "^2.88.2", "rrule": "^2.6.6", "rrule-alt": "^2.2.8", "simple-git": "^2.31.0",