There are a lot of 'rss' related NPM packages but I think I've found a combination that is great for parsing RSS feeds. Something that takes up the minimal node_modules
and works great. I think the killer combination is
got
for downloading, and,fast-xml-parser
for parsing them
The code impressively simple:
const got = require("got");
const parser = require("fast-xml-parser");
(async function main() {
const buffer = await got("https://hacks.mozilla.org/feed/", {
responseType: "buffer",
resolveBodyOnly: true,
timeout: 5000,
retry: 5,
});
var feed = parser.parse(buffer.toString());
for (const item of feed.rss.channel.item) {
console.log({ title: item.title, url: item.link });
break;
}
})();
// Outputs...
// {
// title: 'MDN localization update, February 2021',
// url: 'https://hacks.mozilla.org/2021/02/mdn-localization-update-february-2021/'
// }
I like about fast-xml-parser
is that it has no dependencies. And it's tiny:
▶ du -sh node_modules/fast-xml-parser 104K node_modules/fast-xml-parser
The got
package is quite a bit larger and has more dependencies. But I still love it. It's proven itself to be very reliable and very pleasant API. Both packages support TypeScript too.
A particular detail I like about fast-xml-parser
is that it doesn't try to do the downloading part too. This way, I can use my own preferred library and I could potentially write my own caching code if I want to protect against flaky network.
Comments