handles errors, increase limit

This commit is contained in:
Cyrus 2014-12-23 09:06:11 +08:00
parent 3927ddc0c0
commit 36d861fd79

View File

@ -1,7 +1,7 @@
var _ = require("lodash");
var cheerio = require("cheerio");
var Msg = require("../../models/msg");
var request = require("hyperquest");
var request = require("request");
var Helper = require("../../helper");
var es = require('event-stream');
@ -91,20 +91,23 @@ function parse(msg, url, res, client) {
function fetch(url, cb) {
var req = request.get(url);
var length = 0;
var limit = 1024;
req.on('response', function(res) {
var limit = 1024 * 10;
req
.on('response', function(res) {
if (!(/(text\/html|application\/json)/.test(res.headers['content-type']))) {
// stop wasting precious bandwidth <3
res.req.abort();
}
});
req.pipe(es.map(function(data, next) {
})
.on('error', function() {})
.pipe(es.map(function(data, next) {
length += data.length;
if (length > limit) {
req.response.req.abort();
}
next(null, data);
})).pipe(es.wait(function(err, data) {
}))
.pipe(es.wait(function(err, data) {
if (err) return;
var body;
try {
body = JSON.parse(data);
@ -114,8 +117,8 @@ function fetch(url, cb) {
data = {
text: data,
body: body,
type: req.response.headers['content-type'].split(';').shift()
type: req.response.headers['content-type'].split(/ *; */).shift()
};
if (!err) cb(data);
cb(data);
}));
}