From 96de046d3ec7b926e0506810079d65cecc1b8dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20D=2E=20Rodas?= Date: Mon, 16 Dec 2013 14:30:03 -0300 Subject: [PATCH 1/2] follow redirect --- lib/wget.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/wget.js b/lib/wget.js index 1b55d3e..e7cf370 100644 --- a/lib/wget.js +++ b/lib/wget.js @@ -8,8 +8,8 @@ var util = require('util'); var fs = require('fs'); var EventEmitter = require('events').EventEmitter; -function download(src, output, options) { - var downloader = new EventEmitter(), +function download(src, output, options, __parentevent) { + var downloader = __parentevent || new EventEmitter(), srcUrl, tunnelAgent, req; @@ -53,6 +53,14 @@ function download(src, output, options) { downloader.emit('end', output); }); } else { + if (res.header.location) { + try { + downloader(res.header.location, output, options, downloader) + return; + } catch (e) { + // invalid url, we don't care + } + } downloader.emit('error', 'Server respond ' + res.statusCode); } }); @@ -144,4 +152,4 @@ function cleanProtocol(str) { } exports.download = download; -exports.request = request; \ No newline at end of file +exports.request = request; From 01b5eaa77ef51dd6c8fcb5982022053ad400672b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20D=2E=20Rodas?= Date: Tue, 17 Dec 2013 01:29:54 -0300 Subject: [PATCH 2/2] fix issue with EMFILE and memleaks on failure --- lib/wget.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/wget.js b/lib/wget.js index e7cf370..a304dc9 100644 --- a/lib/wget.js +++ b/lib/wget.js @@ -53,6 +53,7 @@ function download(src, output, options, __parentevent) { downloader.emit('end', output); }); } else { + req.abort(); // release resources to avoid EMFILE if (res.header.location) { try { downloader(res.header.location, output, options, downloader)