Skip to content

Commit 9d047ae

Browse files
committed
feat: add Accept-Encoding header to requests
1 parent 3946707 commit 9d047ae

File tree

1 file changed

+27
-57
lines changed

1 file changed

+27
-57
lines changed

lib/feedsub.js

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const miniget = require('miniget');
55
const zlib = require('zlib');
66

77

8-
// These will be used for the skipdays tag.
8+
// Used for the skipdays tag.
99
const DAYS = ['sunday', 'monday', 'tuesday', 'wednesday',
1010
'thursday', 'friday', 'saturday'];
1111

@@ -17,10 +17,9 @@ module.exports = class FeedReader extends EventEmitter {
1717
* @param {!Object} options
1818
*/
1919
constructor(feed, options) {
20-
options = options || {};
2120
super();
2221
this.feed = feed;
23-
this.options = {
22+
this.options = Object.assign({
2423
interval : 10,
2524
forceInterval : false,
2625
autoStart : false,
@@ -32,8 +31,7 @@ module.exports = class FeedReader extends EventEmitter {
3231
skipHours : false,
3332
skipDays : false,
3433
readEveryItem : false
35-
};
36-
Object.assign(this.options, options);
34+
}, options);
3735

3836
// Create news emitter.
3937
this.news = new NewsEmitter({
@@ -62,37 +60,40 @@ module.exports = class FeedReader extends EventEmitter {
6260
}
6361
});
6462

65-
this.getOpts = Object.assign({ headers: {} }, this.options.requestOpts);
63+
this.getOpts = Object.assign({ }, this.options.requestOpts);
64+
this.getOpts.headers = Object.assign({
65+
'Accept-Encoding': 'gzip, deflate'
66+
}, this.getOpts.headers);
6667
if (this.options.autoStart) {
67-
this.start();
68+
this.start(true);
6869
}
6970
}
7071

7172

7273
/**
7374
* Start calling the read function on interval.
7475
*
75-
* @param {!Boolean} begin
76+
* @param {Boolean} readOnStart
7677
*/
77-
start(begin) {
78-
if (begin == null) begin = true;
79-
this.intervalid = this.readInterval(this.options.interval, null, begin);
78+
start(readOnStart) {
79+
this.stop();
80+
let ms = this.options.interval * 60000;
81+
this._intervalid = setInterval(this.read.bind(this), ms);
82+
if (readOnStart) {
83+
this.read();
84+
}
8085
}
8186

8287

8388
/**
8489
* Stop interval if any
8590
*/
8691
stop() {
87-
if (this.intervalid) {
88-
clearInterval(this.intervalid);
89-
delete this.intervalid;
90-
}
92+
clearInterval(this._intervalid);
9193
}
9294

9395

9496
/**
95-
*
9697
* Reads feed and determines if there are any new items
9798
* emits new items through event `item`
9899
* if there are new items, emits `items` event at end with all items
@@ -110,17 +111,17 @@ module.exports = class FeedReader extends EventEmitter {
110111
ended = true;
111112
this.newitems = [];
112113
this.first = false;
113-
if (abort && !aborted) {
114-
aborted = true;
115-
req.abort();
116-
}
117114
if (!aborted) {
118115
if (typeof callback === 'function') {
119116
callback(err);
120117
} else {
121118
this.emit('error', err);
122119
}
123120
}
121+
if (abort && !aborted) {
122+
aborted = true;
123+
req.abort();
124+
}
124125
};
125126

126127
const success = (results, abort) => {
@@ -180,37 +181,31 @@ module.exports = class FeedReader extends EventEmitter {
180181
// Only update if ttl is longer than requested interval.
181182
if (minutes > this.options.interval) {
182183
this.options.interval = minutes;
183-
if (this.intervalid) {
184+
if (this.options.autoStart) {
184185
this.start(false);
185186
}
186187
}
187188
});
188189
}
189190

190191
// Listen for skipHours if enabled.
191-
if (this.options.skipHours) {
192+
if (this.options.skipHours && !this.options.hoursToSkip) {
192193
parser.once('skiphours', (data) => {
193-
if (this.options.hoursToSkip) { return; }
194-
let hours = data.hour;
195-
196-
this.options.hoursToSkip = Array.isArray(hours) ?
197-
data.hour.map(h => parseInt(h, 10)) : [parseInt(hours, 10)];
194+
this.options.hoursToSkip =
195+
[].concat(data.hour).map(h => parseInt(h, 10));
198196
});
199197
}
200198

201199
// Listen for skipDays if enabled.
202-
if (this.options.skipDays !== false) {
200+
if (this.options.skipDays !== false && !this.options.daysToSkip) {
203201
parser.once('skipdays', (data) => {
204-
if (this.options.daysToSkip) { return; }
205-
let days = data.day;
206-
this.options.daysToSkip = Array.isArray(days) ? days : [days];
202+
this.options.daysToSkip = [].concat(data.day);
207203
});
208204
}
209205

210206

211207
// Compare date when first item is encountered.
212208
const firstitem = (item) => {
213-
214209
// If date is the same as last, abort.
215210
if (date && this.options.lastDate === date) {
216211
return success([], true);
@@ -274,7 +269,6 @@ module.exports = class FeedReader extends EventEmitter {
274269
output.pipe(parser);
275270

276271
output.on('end', () => {
277-
if (parser.close) { parser.close(); }
278272
if (!ended) {
279273
if (this.first && this.options.emitOnStart) {
280274
this.news.addHistory('item', this.newitems.map((item) => {
@@ -289,28 +283,4 @@ module.exports = class FeedReader extends EventEmitter {
289283

290284
req.on('error', error.bind(null, false));
291285
}
292-
293-
294-
/**
295-
* Starts calling the read fuction in an interval.
296-
* Passes callback to read
297-
* if begin is true, call read immediately.
298-
*
299-
* @param {!Function(!Error, Array.<Object>)} callback
300-
* @param {Number} interval
301-
* @param {Boolean} begin
302-
*/
303-
readInterval(callback, interval, begin) {
304-
this.stop();
305-
306-
// Allow callback argument to be optional.
307-
if (typeof callback === 'number') { interval = callback; }
308-
interval = parseInt(interval, 10);
309-
310-
if (interval > 0) {
311-
if (begin) { this.read(callback); }
312-
313-
return setInterval(() => this.read(callback), interval * 60000);
314-
}
315-
}
316286
};

0 commit comments

Comments
 (0)