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