tbennett
tbennett

Reputation: 424

node.js tar module, 'entry' readable stream

how should I use 'entry' readable stream from tar module to pipe - npmsj.org their content without get a stream error in pipe?

this is to get hint for stream-adventure - github last exercise.

I'm not looking for a answer. but a hint or advice.

Here is my code:

var zlib = require('zlib');
var tar = require('tar');
var crypto = require('crypto');
var through = require('through');

var unzip = zlib.createGunzip();
var parser = tar.Parse();
var stream = process.stdin.pipe(crypto.createDecipher(process.argv[2], process.argv[3])).pipe(unzip);
var md5 = crypto.createHash('md5', { encoding: 'hex' });

parser.on('entry', function(entry) {
  if (entry.type === 'File') {
    entry.pipe(md5).pipe(process.stdout);
    console.log(entry.path);
  }
});

unzip.pipe(parser);

here is the output:

$> stream-adventure run app
97911dcc607865d621029f6f927c7851
stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^
Error: write after end
    at writeAfterEnd (_stream_writable.js:130:12)
    at Hash.Writable.write (_stream_writable.js:178:5)
    at Entry.ondata (stream.js:51:26)
    at Entry.EventEmitter.emit (events.js:117:20)
    at Entry._read (/home/n0t/stream-adventure/secretz/node_modules/tar/lib/entry.js:111:10)
    at Entry.write (/home/n0t/stream-adventure/secretz/node_modules/tar/lib/entry.js:68:8)
    at Parse._process (/home/n0t/stream-adventure/secretz/node_modules/tar/lib/parse.js:104:11)
    at BlockStream.<anonymous> (/home/n0t/stream-adventure/secretz/node_modules/tar/lib/parse.js:46:8)
    at BlockStream.EventEmitter.emit (events.js:95:17)
    at BlockStream._emitChunk (/home/n0t/stream-adventure/secretz/node_modules/tar/node_modules/block-stream/block-stream.js:145:10)

and with the verify:

$> stream-adventure verify app

ACTUAL:   "97911dcc607865d621029f6f927c7851"
EXPECTED: "97911dcc607865d621029f6f927c7851 secretz/METADATA.TXT"

ACTUAL:   null
EXPECTED: "2cdcfa9f8bbefb82fb7a894964b5c199 secretz/SPYING.TXT"

ACTUAL:   null
EXPECTED: ""

# FAIL

Upvotes: 1

Views: 2737

Answers (1)

Paul Mougel
Paul Mougel

Reputation: 17038

You get this error because entry writes into the md5 stream after is has been closed. Once a stream is closed, you can't write into it again: for md5 this is easy to understand because you have to reset the internal buffers, otherwise the hash will be skewed.

In your example, on each file in the tar module, you pipe the file stream into the same md5 stream. You just have to pipe the file stream into a new MD5 stream; here is how you can do it properly:

parser.on('entry', function(entry) {
  if (entry.type === 'File') {
    var md5 = crypto.createHash('md5', { encoding: 'hex' });
    entry.pipe(md5).pipe(process.stdout);
    console.log(entry.path);
  }
});

Upvotes: 2

Related Questions