Skip to content

Commit 6f77a6a

Browse files
committed
feat: follow redirects with miniget
fixes #30
1 parent 4be0be7 commit 6f77a6a

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
lines changed

lib/feedsub.js

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
const EventEmitter = require('events').EventEmitter;
22
const FeedMe = require('feedme');
33
const NewsEmitter = require('newsemitter');
4-
const http = require('http');
5-
const https = require('https');
6-
const url = require('url');
4+
const miniget = require('miniget');
75
const zlib = require('zlib');
86

97

@@ -21,6 +19,7 @@ module.exports = class FeedReader extends EventEmitter {
2119
constructor(feed, options) {
2220
options = options || {};
2321
super();
22+
this.feed = feed;
2423
this.options = {
2524
interval : 10,
2625
forceInterval : false,
@@ -53,7 +52,6 @@ module.exports = class FeedReader extends EventEmitter {
5352

5453
this.news.addHistory('item', this.options.history);
5554

56-
5755
this.newitems = [];
5856
this.first = this.news.history.item.length === 0;
5957

@@ -64,9 +62,7 @@ module.exports = class FeedReader extends EventEmitter {
6462
}
6563
});
6664

67-
this.getOpts = url.parse(feed);
68-
this.getOpts.headers = {};
69-
Object.assign(this.getOpts, this.options.requestOpts);
65+
this.getOpts = Object.assign({ headers: {} }, this.options.requestOpts);
7066
if (this.options.autoStart) {
7167
this.start();
7268
}
@@ -144,24 +140,14 @@ module.exports = class FeedReader extends EventEmitter {
144140
}
145141
};
146142

147-
const httpLib = {
148-
'http:': http,
149-
'https:': https,
150-
}[this.getOpts.protocol];
151-
const req = httpLib.get(this.getOpts, (res) => {
152-
// Check the response is successful.
153-
if (res.statusCode !== 200) {
154-
155-
// Check if not modified code is sent back
156-
// in this case, the body will be empty.
157-
if (res.statusCode === 304) {
158-
return success([]);
159-
}
160-
161-
return error(true, new Error('Status Code: ' + res.statusCode));
143+
const req = miniget(this.feed, this.getOpts);
144+
req.on('response', (res) => {
145+
// Check if not modified code is sent back
146+
// in this case, the body will be empty.
147+
if (res.statusCode === 304) {
148+
return success([]);
162149
}
163150

164-
165151
// Check headers for conditional get.
166152
if (res.headers['last-modified']) {
167153
this.getOpts.headers['If-Modified-Since'] = res.headers['last-modified'];
@@ -274,20 +260,18 @@ module.exports = class FeedReader extends EventEmitter {
274260
switch(res.headers['content-encoding']) {
275261
case 'gzip':
276262
output = zlib.createGunzip();
277-
res.pipe(output);
263+
req.pipe(output);
278264
break;
279265
case 'deflate':
280266
output = zlib.createInflate();
281-
res.pipe(output);
267+
req.pipe(output);
282268
break;
283269
default:
284-
output = res;
270+
output = req;
285271
}
286272

287273
// Pipe data from gunzipper/inflater to parser.
288-
output.on('data', (chunk) => {
289-
parser.write(chunk);
290-
});
274+
output.pipe(parser);
291275

292276
output.on('end', () => {
293277
if (parser.close) { parser.close(); }

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"dependencies": {
2525
"feedme": "^1.0.0",
26+
"miniget": "^1.3.0",
2627
"newsemitter": "^0.3.0"
2728
},
2829
"devDependencies": {

0 commit comments

Comments
 (0)