mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 19:53:36 +00:00
replace request with node-fetch
This commit is contained in:
commit
fa0f997928
@ -36,6 +36,7 @@ _This release is scheduled to be released on 2021-04-01._
|
||||
- Dont update the DOM when a module is not displayed.
|
||||
- Cleaned up jsdoc and tests.
|
||||
- Exposed logger as node module for easier access for 3rd party modules
|
||||
- Replaced deprecated `request` package with `node-fetch` and `digest-fetch`
|
||||
|
||||
### Removed
|
||||
|
||||
|
@ -6,7 +6,10 @@
|
||||
*/
|
||||
const Log = require("logger");
|
||||
const ical = require("node-ical");
|
||||
const request = require("request");
|
||||
const fetch = require("node-fetch");
|
||||
const digest = require("digest-fetch");
|
||||
const https = require("https");
|
||||
const base64 = require("base-64");
|
||||
|
||||
/**
|
||||
* Moment date
|
||||
@ -41,57 +44,53 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn
|
||||
* Initiates calendar fetch.
|
||||
*/
|
||||
const fetchCalendar = function () {
|
||||
function getFetcher(url, auth) {
|
||||
const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
|
||||
let headers = {
|
||||
"User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)"
|
||||
};
|
||||
let httpsAgent = null;
|
||||
|
||||
if (selfSignedCert) {
|
||||
httpsAgent = new https.Agent({
|
||||
rejectUnauthorized: false
|
||||
});
|
||||
}
|
||||
if (auth) {
|
||||
if (auth.method === "bearer") {
|
||||
headers.Authorization = "Bearer " + auth.pass;
|
||||
} else if (auth.method === "digest") {
|
||||
return new digest(auth.user, auth.pass).fetch(url, { headers: headers, httpsAgent: httpsAgent });
|
||||
} else {
|
||||
headers.Authorization = "Basic " + base64.encode(auth.user + ":" + auth.pass);
|
||||
}
|
||||
}
|
||||
return fetch(url, { headers: headers, httpsAgent: httpsAgent });
|
||||
}
|
||||
clearTimeout(reloadTimer);
|
||||
reloadTimer = 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
|
||||
};
|
||||
|
||||
if (selfSignedCert) {
|
||||
var agentOptions = {
|
||||
rejectUnauthorized: false
|
||||
};
|
||||
opts.agentOptions = agentOptions;
|
||||
}
|
||||
|
||||
if (auth) {
|
||||
if (auth.method === "bearer") {
|
||||
opts.auth = {
|
||||
bearer: auth.pass
|
||||
};
|
||||
} else {
|
||||
opts.auth = {
|
||||
user: auth.user,
|
||||
pass: auth.pass,
|
||||
sendImmediately: auth.method !== "digest"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
request(url, opts, function (err, r, requestData) {
|
||||
if (err) {
|
||||
fetchFailedCallback(self, err);
|
||||
getFetcher(url, auth)
|
||||
.catch((error) => {
|
||||
fetchFailedCallback(self, error);
|
||||
scheduleTimer();
|
||||
return;
|
||||
} else if (r.statusCode !== 200) {
|
||||
fetchFailedCallback(self, r.statusCode + ": " + r.statusMessage);
|
||||
})
|
||||
.then((response) => {
|
||||
if (response.status !== 200) {
|
||||
fetchFailedCallback(self, response.statusText);
|
||||
scheduleTimer();
|
||||
return;
|
||||
}
|
||||
|
||||
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));
|
||||
|
@ -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,20 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings
|
||||
});
|
||||
|
||||
const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
|
||||
const opts = {
|
||||
headers: {
|
||||
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"
|
||||
},
|
||||
encoding: null
|
||||
};
|
||||
|
||||
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);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
|
2127
package-lock.json
generated
2127
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -71,6 +71,7 @@
|
||||
"dependencies": {
|
||||
"colors": "^1.4.0",
|
||||
"console-stamp": "^3.0.0-rc4.2",
|
||||
"digest-fetch": "^1.1.6",
|
||||
"eslint": "^7.20.0",
|
||||
"express": "^4.17.1",
|
||||
"express-ipfilter": "^1.1.2",
|
||||
@ -80,8 +81,8 @@
|
||||
"iconv-lite": "^0.6.2",
|
||||
"module-alias": "^2.2.2",
|
||||
"moment": "^2.29.1",
|
||||
"node-fetch": "^2.6.1",
|
||||
"node-ical": "^0.12.8",
|
||||
"request": "^2.88.2",
|
||||
"rrule": "^2.6.8",
|
||||
"rrule-alt": "^2.2.8",
|
||||
"simple-git": "^2.35.2",
|
||||
|
@ -1,5 +1,5 @@
|
||||
const helpers = require("./global-setup");
|
||||
const request = require("request");
|
||||
const fetch = require("node-fetch");
|
||||
const expect = require("chai").expect;
|
||||
|
||||
const describe = global.describe;
|
||||
@ -46,15 +46,15 @@ describe("Electron app environment", function () {
|
||||
});
|
||||
|
||||
it("get request from http://localhost:8080 should return 200", function (done) {
|
||||
request.get("http://localhost:8080", function (err, res, body) {
|
||||
expect(res.statusCode).to.equal(200);
|
||||
fetch("http://localhost:8080").then((res) => {
|
||||
expect(res.status).to.equal(200);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it("get request from http://localhost:8080/nothing should return 404", function (done) {
|
||||
request.get("http://localhost:8080/nothing", function (err, res, body) {
|
||||
expect(res.statusCode).to.equal(404);
|
||||
fetch("http://localhost:8080/nothing").then((res) => {
|
||||
expect(res.status).to.equal(404);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
@ -1,5 +1,5 @@
|
||||
const helpers = require("./global-setup");
|
||||
const request = require("request");
|
||||
const fetch = require("node-fetch");
|
||||
const expect = require("chai").expect;
|
||||
const forEach = require("mocha-each");
|
||||
|
||||
@ -40,8 +40,8 @@ describe("All font files from roboto.css should be downloadable", function () {
|
||||
|
||||
forEach(fontFiles).it("should return 200 HTTP code for file '%s'", (fontFile, done) => {
|
||||
var fontUrl = "http://localhost:8080/fonts/" + fontFile;
|
||||
request.get(fontUrl, function (err, res, body) {
|
||||
expect(res.statusCode).to.equal(200);
|
||||
fetch(fontUrl).then((res) => {
|
||||
expect(res.status).to.equal(200);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
@ -1,5 +1,5 @@
|
||||
const helpers = require("./global-setup");
|
||||
const request = require("request");
|
||||
const fetch = require("node-fetch");
|
||||
const expect = require("chai").expect;
|
||||
|
||||
const describe = global.describe;
|
||||
@ -32,8 +32,8 @@ describe("ipWhitelist directive configuration", function () {
|
||||
process.env.MM_CONFIG_FILE = "tests/configs/noIpWhiteList.js";
|
||||
});
|
||||
it("should return 403", function (done) {
|
||||
request.get("http://localhost:8080", function (err, res, body) {
|
||||
expect(res.statusCode).to.equal(403);
|
||||
fetch("http://localhost:8080").then((res) => {
|
||||
expect(res.status).to.equal(403);
|
||||
done();
|
||||
});
|
||||
});
|
||||
@ -45,8 +45,8 @@ describe("ipWhitelist directive configuration", function () {
|
||||
process.env.MM_CONFIG_FILE = "tests/configs/empty_ipWhiteList.js";
|
||||
});
|
||||
it("should return 200", function (done) {
|
||||
request.get("http://localhost:8080", function (err, res, body) {
|
||||
expect(res.statusCode).to.equal(200);
|
||||
fetch("http://localhost:8080").then((res) => {
|
||||
expect(res.status).to.equal(200);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
@ -1,5 +1,5 @@
|
||||
const helpers = require("./global-setup");
|
||||
const request = require("request");
|
||||
const fetch = require("node-fetch");
|
||||
const expect = require("chai").expect;
|
||||
|
||||
const describe = global.describe;
|
||||
@ -33,8 +33,8 @@ describe("port directive configuration", function () {
|
||||
});
|
||||
|
||||
it("should return 200", function (done) {
|
||||
request.get("http://localhost:8090", function (err, res, body) {
|
||||
expect(res.statusCode).to.equal(200);
|
||||
fetch("http://localhost:8090").then((res) => {
|
||||
expect(res.status).to.equal(200);
|
||||
done();
|
||||
});
|
||||
});
|
||||
@ -52,8 +52,8 @@ describe("port directive configuration", function () {
|
||||
});
|
||||
|
||||
it("should return 200", function (done) {
|
||||
request.get("http://localhost:8100", function (err, res, body) {
|
||||
expect(res.statusCode).to.equal(200);
|
||||
fetch("http://localhost:8100").then((res) => {
|
||||
expect(res.status).to.equal(200);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
@ -1,5 +1,5 @@
|
||||
const helpers = require("./global-setup");
|
||||
const request = require("request");
|
||||
const fetch = require("node-fetch");
|
||||
const expect = require("chai").expect;
|
||||
|
||||
const describe = global.describe;
|
||||
@ -32,8 +32,8 @@ describe("Vendors", function () {
|
||||
Object.keys(vendors).forEach((vendor) => {
|
||||
it(`should return 200 HTTP code for vendor "${vendor}"`, function () {
|
||||
var urlVendor = "http://localhost:8080/vendor/" + vendors[vendor];
|
||||
request.get(urlVendor, function (err, res, body) {
|
||||
expect(res.statusCode).to.equal(200);
|
||||
fetch(urlVendor).then((res) => {
|
||||
expect(res.status).to.equal(200);
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -41,8 +41,8 @@ describe("Vendors", function () {
|
||||
Object.keys(vendors).forEach((vendor) => {
|
||||
it(`should return 404 HTTP code for vendor https://localhost/"${vendor}"`, function () {
|
||||
var urlVendor = "http://localhost:8080/" + vendors[vendor];
|
||||
request.get(urlVendor, function (err, res, body) {
|
||||
expect(res.statusCode).to.equal(404);
|
||||
fetch(urlVendor).then((res) => {
|
||||
expect(res.status).to.equal(404);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user