Skip to content

Instantly share code, notes, and snippets.

@diamondap
Created April 5, 2019 16:55
Show Gist options
  • Save diamondap/b6b36dd50e1e94e2bbe89a221124eb76 to your computer and use it in GitHub Desktop.
Save diamondap/b6b36dd50e1e94e2bbe89a221124eb76 to your computer and use it in GitHub Desktop.
// In the code below, extract is a tar-stream extract object.
// See https://github.com/mafintosh/tar-stream/blob/master/extract.js
//
// This 'entry' handler does not care about the contents of the entry,
// it just has to restructure the tar header data into a format that
// the caller understands. It then passes 'stream' to a PassThrough
// which discards the contents and allows tar-stream to move on to
// the next entry.
//
// The problem with the initial pipe to PassThrough on line 35 is that
// with multi-megabyte files, 'stream' pauses because PassThrough seems
// to pause.
//
// The workaround code on lines 41-43 fixes this in my local tests.
//
extract.on('entry', function(header, stream, next) {
var fileStat = tarReader._headerToFileStat(header);
var relPath = header.name;
tarReader.emit('entry', { relPath: relPath, fileStat: fileStat });
stream.on('end', function() {
if (header.type === "file") {
tarReader.fileCount += 1;
} else if (header.type === "directory") {
tarReader.dirCount += 1;
}
next();
});
// The line below was the original call to pipe the tar stream
// to a PassThrough reader. This stalled on multi-megabyte files.
// It looks like tar-stream's internal writer paused because the
// PassThrough stream paused.
//
// stream.pipe(new PassThrough());
// This works around https://github.com/mafintosh/tar-stream/issues/64
// by explicitly unpausing the PassThrough stream. Not sure why it
// only pauses once.
//
let pt = new PassThrough();
stream.pipe(pt);
pt.resume();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment