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