Skip to content

Instantly share code, notes, and snippets.

@danmactough
Created October 2, 2013 14:07
Show Gist options
  • Save danmactough/6794357 to your computer and use it in GitHub Desktop.
Save danmactough/6794357 to your computer and use it in GitHub Desktop.
URL fetching wrapper for feedparser v.16
var FeedParser = require('feedparser')
, request = require('request')
, STATUS_CODES = require('http').STATUS_CODES;
module.exports = function (feed, cb) {
var parser = new FeedParser();
var req = request(feed);
var items = [];
var _called = false;
req.on('error', function (err) {
if (!_called) {
_called = true;
cb(err);
}
});
req.on('response', function (response) {
if (200 !== response.statusCode) {
var err = new Error(STATUS_CODES[response.statusCode]);
err.code = response.statusCode;
parser.end();
if (!_called) {
_called = true;
cb(err);
}
}
});
parser.on('error', function (err) {
if (!_called) {
_called = true;
cb(err);
}
});
parser.on('readable', function () {
var stream = this, item;
while (item = stream.read()) {
// items.push(item);
}
});
parser.on('end', function () {
if (!_called) {
_called = true;
cb(null, items);
}
});
return req.pipe(parser);
};
@rdbcci
Copy link

rdbcci commented Oct 2, 2013

is the _called latching pattern an example of unpiping or is that something else?

@danmactough
Copy link
Author

@rdbcci
Copy link

rdbcci commented Oct 5, 2013

Thanks for the unpiping link. It seems all of my superfluous emitting problems are solved with this latching technique. Do you think it is also prudent to unpipe at the first error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment