Remove event-stream dependency in favor of plain Buffers

This commit is contained in:
Jérémie Astori 2017-09-21 03:20:24 -04:00
parent f85686bcb2
commit 75b927b4a3
No known key found for this signature in database
GPG Key ID: B9A4F245CD67BDE8
2 changed files with 12 additions and 18 deletions

View File

@ -44,7 +44,6 @@
"cheerio": "0.22.0", "cheerio": "0.22.0",
"colors": "1.1.2", "colors": "1.1.2",
"commander": "2.11.0", "commander": "2.11.0",
"event-stream": "3.3.4",
"express": "4.15.4", "express": "4.15.4",
"express-handlebars": "3.0.0", "express-handlebars": "3.0.0",
"fs-extra": "4.0.2", "fs-extra": "4.0.2",

View File

@ -5,7 +5,6 @@ const request = require("request");
const url = require("url"); const url = require("url");
const Helper = require("../../helper"); const Helper = require("../../helper");
const findLinks = require("../../../client/js/libs/handlebars/ircmessageparser/findLinks"); const findLinks = require("../../../client/js/libs/handlebars/ircmessageparser/findLinks");
const es = require("event-stream");
const storage = require("../storage"); const storage = require("../storage");
process.setMaxListeners(0); process.setMaxListeners(0);
@ -155,6 +154,7 @@ function fetch(uri, cb) {
} catch (e) { } catch (e) {
return cb(null); return cb(null);
} }
const buffers = [];
var length = 0; var length = 0;
var limit = Helper.config.prefetchMaxImageSize * 1024; var limit = Helper.config.prefetchMaxImageSize * 1024;
req req
@ -166,18 +166,15 @@ function fetch(uri, cb) {
} }
}) })
.on("error", function() {}) .on("error", function() {})
.pipe(es.map(function(data, next) { .on("data", (data) => {
length += data.length; length += data.length;
if (length > limit) { buffers.push(data);
req.response.req.abort();
}
next(null, data);
}))
.pipe(es.wait(function(err, data) {
if (err) {
return cb(null);
}
if (length > limit) {
req.abort();
}
})
.on("end", () => {
if (req.response.statusCode < 200 || req.response.statusCode > 299) { if (req.response.statusCode < 200 || req.response.statusCode > 299) {
return cb(null); return cb(null);
} }
@ -193,14 +190,12 @@ function fetch(uri, cb) {
type = req.response.headers["content-type"].split(/ *; */).shift(); type = req.response.headers["content-type"].split(/ *; */).shift();
} }
data = { cb({
data: data, data: Buffer.concat(buffers, length),
type: type, type: type,
size: size size: size
}; });
});
cb(data);
}));
} }
// https://github.com/request/request/issues/2120 // https://github.com/request/request/issues/2120