Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs/fs: writing a tar to memory buffer

Tags:

node.js

fs

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.

like image 633
transient_loop Avatar asked Jan 21 '26 03:01

transient_loop


1 Answers

The following solution

creates the tar, then pipes it to some buffer and sends it immediately

and with great speed thanks to the tar-fs library:

First install the libraries request for simplified requests and tar-fs, which provides filesystem bindings for tar-stream: npm i -S tar-fs request

var tar = require('tar-fs')
var request = require('request')
var fs = require('fs')

// pack specific files in the directory
function packTar (folderName, pathsArr) {
  return tar.pack(folderName, {
    entries: pathsArr 
  })
}

// return put stream
function makePutReq (url) {
  return request.put(url)
}

packTar('./testFolder', ['test.txt', 'test1.txt'])
  .pipe(makePutReq('https://www.example.com/put'))

I have renamed the function names to be super verbose.

like image 196
Tom Avatar answered Jan 23 '26 19:01

Tom