handles errors, increase limit

This commit is contained in:
Cyrus 2014-12-23 09:06:11 +08:00
parent 3927ddc0c0
commit 36d861fd79
1 changed files with 31 additions and 28 deletions

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,31 +91,34 @@ 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) {
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) {
length += data.length;
if (length > limit) {
req.response.req.abort();
}
next(null, data);
})).pipe(es.wait(function(err, data) {
var body;
try {
body = JSON.parse(data);
} catch(e) {
body = {};
}
data = {
text: data,
body: body,
type: req.response.headers['content-type'].split(';').shift()
};
if (!err) cb(data);
}));
var limit = 1024 * 10;
req
.on('response', function(res) {
if (!(/(text\/html|application\/json)/.test(res.headers['content-type']))) {
res.req.abort();
}
})
.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) {
if (err) return;
var body;
try {
body = JSON.parse(data);
} catch(e) {
body = {};
}
data = {
text: data,
body: body,
type: req.response.headers['content-type'].split(/ *; */).shift()
};
cb(data);
}));
}