Comparing version 0.2.6 to 0.2.7
@@ -26,3 +26,2 @@ /** | ||
var fs = require('fs'), | ||
@@ -33,3 +32,5 @@ Ftp = require('ftp'), | ||
tmp = require('tmp'), | ||
spawn = require('child_process').spawn; | ||
spawn = require('child_process').spawn, | ||
util = require('util'), | ||
md5 = require('MD5'); | ||
@@ -122,2 +123,53 @@ var isWin = !!os.platform().match(/^win/), | ||
/* | ||
* There is a obscure bug concerning ftp.get and stream.pipe, leading to | ||
* occasional gobbled chunks at the end of the download | ||
* | ||
* https://github.com/mscdex/node-ftp/issues/70 | ||
* | ||
* Thus, we handle the download manually and compute MD5 and total size to | ||
* get meaningful debug output. | ||
*/ | ||
function consumeDownload(instream, outstream, callback) { | ||
var chunks = [], | ||
instreamTerminated = false, | ||
outstreamTerminated = false, | ||
buffering = false, | ||
chunkIdx = 0, | ||
totalSize = 0; | ||
function write() { | ||
return outstream.write(chunks[chunkIdx++]); | ||
} | ||
function terminateOutstream() { | ||
if (!outstreamTerminated) outstream.end(); | ||
outstreamTerminated = true; | ||
} | ||
instream.on('data', function(chunk) { | ||
chunks.push(chunk); | ||
totalSize += chunk.length; | ||
if (!buffering) buffering = !write(); | ||
}); | ||
instream.on('end', function() { | ||
instreamTerminated = true; | ||
if (!buffering) terminateOutstream(); | ||
}); | ||
outstream.on('drain', function() { | ||
// Write chunks until the queue is empty or the the writer get | ||
// overwhelmed. | ||
while ((buffering = chunkIdx < chunks.length) && write()); | ||
// Stop buffering if all chunks have been flushed. | ||
if (!buffering && instreamTerminated) terminateOutstream(); | ||
}); | ||
outstream.on('close', function() { | ||
callback(undefined, totalSize, md5(Buffer.concat(chunks), totalSize)); | ||
}); | ||
} | ||
function download(name) { | ||
@@ -134,10 +186,4 @@ console.log('Downloading ' + name + '...'); | ||
writer.on('error', onError); | ||
writer.on('close', function() { | ||
ftpClient.end(); | ||
console.log('Download complete!'); | ||
callback(outfile); | ||
}); | ||
writer.on('open', function() { | ||
function onOpen() { | ||
ftpClient.get(name, function(error, stream) { | ||
@@ -147,5 +193,15 @@ if (error) throw error; | ||
stream.on('error', onError); | ||
stream.pipe(writer); | ||
consumeDownload(stream, writer, function(err, bytes, hash) { | ||
if (err) throw err; | ||
ftpClient.end(); | ||
console.log(util.format('Download complete - %s bytes, MD5: %s', | ||
bytes, hash)); | ||
callback(outfile); | ||
}); | ||
}); | ||
}); | ||
} | ||
writer.on('open', onOpen); | ||
}); | ||
@@ -198,2 +254,3 @@ } | ||
if (fs.existsSync(libxlDir)) { | ||
console.log('Libxl already downloaded, nothing to do'); | ||
process.exit(0); | ||
@@ -200,0 +257,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.2.6", | ||
"version": "0.2.7", | ||
"description": "Node bindings for the libxl library for reading and writing excel (XLS and XLSX) spreadsheets.", | ||
@@ -32,4 +32,5 @@ "keywords": [ | ||
"ftp": "~0.3.0", | ||
"tmp": "~0.0.24" | ||
"tmp": "~0.0.24", | ||
"MD5": "~1.2.1" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
970375
1753
4
+ AddedMD5@~1.2.1
+ AddedMD5@1.2.2(transitive)
+ Addedcharenc@0.0.2(transitive)
+ Addedcrypt@0.0.2(transitive)