Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secretz create hash stream scope issue #97

Open
nantas opened this issue Dec 16, 2014 · 1 comment
Open

Secretz create hash stream scope issue #97

nantas opened this issue Dec 16, 2014 · 1 comment

Comments

@nantas
Copy link

nantas commented Dec 16, 2014

My solution:

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

var cipher = process.argv[2], pass = process.argv[3];
var decipher = crypto.createDecipher(cipher, pass);
var tarParser = tar.Parse();
var hasher = crypto.createHash('md5', {encoding: 'hex'});
tarParser.on('entry', function(e) {
    if (e.type !== "File") return;
    //e.pipe(tr);

    e.pipe(hasher).pipe(through(null, end)).pipe(process.stdout);
    function end() {
        this.queue(' ' + e.path + '\n');
    }
});

process.stdin.pipe(decipher).pipe(zlib.createGunzip()).pipe(tarParser);

If I run it it gives the following error:

stream.js:94
    throw er; // Unhandled stream error in pipe.

Error: write after end
    at writeAfterEnd (_stream_writable.js:132:12)
    at Hash.Writable.write(_stream_writable.js:180:5)
    at Entry.ondata(stream.js:51:26)
    at Entry.emit(events.js:117:20)

The only difference from official solution is that I put the create hash stream definition in the global scope other than function scope. Can someone explain to me what's going on here? I'm a beginner for node.js and javascript in general.

Many thanks.

@marani
Copy link

marani commented Dec 23, 2014

For your solution, you are piping multiple entries into a single hash transform stream. Imagine two entries emitting data at the same time, your single hash stream will be confused.

For the official solution, each entry has its own hash stream so it will be ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants