From 36d861fd79745d612f460070a5804bafec97ff6c Mon Sep 17 00:00:00 2001 From: Cyrus Date: Tue, 23 Dec 2014 09:06:11 +0800 Subject: [PATCH] handles errors, increase limit --- src/plugins/irc-events/link.js | 59 ++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/plugins/irc-events/link.js b/src/plugins/irc-events/link.js index f334047..c3b2244 100644 --- a/src/plugins/irc-events/link.js +++ b/src/plugins/irc-events/link.js @@ -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); + })); }