Created
October 2, 2013 14:07
-
-
Save danmactough/6794357 to your computer and use it in GitHub Desktop.
URL fetching wrapper for feedparser v.16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; |
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
Unpiping is just calling stream#unpipe() - http://nodejs.org/api/stream.html#stream_readable_unpipe_destination