-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeedapi.js
39 lines (36 loc) · 1.35 KB
/
feedapi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const crypto = require('crypto');
const needle = require('needle');
const feedparser = require('feedparser');
const JSONStream = require('JSONStream');
const es = require('event-stream');
const xss = require('xss');
const parser = () => es.map((entry, callback) => {
callback(null, {
author: entry.author || entry.meta.author || entry.meta.title,
title: entry.title,
link: entry.link,
date: Date.parse(entry.date || entry.pubDate),
id: crypto.createHash('sha256').update(entry.link).digest('base64').substring(0, 6),
content: xss(entry.description || entry.summary, {stripIgnoreTag: true, stripIgnoreTagBody: ['script', 'style']})
});
});
const feedapi = function (req, res) {
const handleError = function (error) {
res.status(500).send(error);
this.unpipe();
};
const checkStatus = function (response) {
if (response.statusCode >= 400)
this.emit('error', {message: response.statusMessage});
};
needle.get(req.params.url, {parse: false, follow: 4}).on('error', handleError).on('response', checkStatus)
.pipe(feedparser({}).on('error', handleError))
.pipe(parser().on('error', handleError))
.pipe(JSONStream.stringify().on('error', handleError))
.pipe(res.set({
'Content-Type': 'application/json; charset=utf-8',
'Cache-Control': 'public, max-age=60'
})
);
};
module.exports = feedapi;