nodejs/fs: writing a tar to memory buffer

Viewed 1227

I need to be able to tar a directory, and send this to a remote endpoint via HTTP PUT.

I could of course create the tar, save it to disk, then read it again and send it.

But I'd rather like to create the tar, then pipe it to some buffer and send it immediately. I haven't been able to achieve this.

Code so far:

var tar = require('tar');
var fs = require("fs");

var path = "/home/me/uploaddir";

function getTar(path, cb) {
  var buf = new Buffer('');
  var wbuf = fs.createWriteStream(buf);
  wbuf.on("finish", function() {
    cb(buf);
  });
  tar.c({file:""},[path]).
    pipe(wbuf);
}

getTar(path, function(tar) {
   //send the tar over http
});

This code results in:

fs.js:575
  binding.open(pathModule._makeLong(path),
          ^

TypeError: path must be a string
    at TypeError (native)
    at Object.fs.open (fs.js:575:11)

I've also tried using an array as buffer, no joy.

1 Answers
Related