2

Newbie to Node here. I'm trying to download a .tar.xz file and extract it to a directory via the code shown below:

var request = require('request');
var tar = require('tar');
var xz = require('xz');

function downloadRaw(url, callback)
{
    return request({
        encoding: null,
        url: url
    }, callback);
}

function extractToDirectory(url, directory, callback)
{
    return downloadRaw(url)
        .pipe(new xz.Decompressor())
        .pipe(tar.Extract(directory))
        .on('finish', callback);
}

For some reason, the 'finish' event on the tar stream does not seem to be firing, even after it finishes extracting the contents of the archive. tar is a library that's maintained by npm themselves, so I assume that I'm making some kind of mistake here. Anyway, why is this happening and what can I do to fix it?

James Ko
  • 32,215
  • 30
  • 128
  • 239

2 Answers2

0

I see that downloadRaw takes a callback, but that you never pass it one, and so you pass an undefined value to the request function.

If you want to use the streaming api then don't pass the callback to either of those functions.

In fact why bother with that raw function at all. Why not just do:

return request(url)
.pipe(newxz.Decompressor())
.pipe(tar.Extract(directory))
 .on('finish', callback);
Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
0

Turns out the error event was firing, but not the finish. This fixed it:

function extractToDirectory(url, directory, callback)
{
    return downloadRaw(url)
        .pipe(new xz.Decompressor())
        .pipe(tar.Extract(directory))
        .on('error', callback)
        .on('finish', callback);
}
James Ko
  • 32,215
  • 30
  • 128
  • 239