mafintosh / tar-stream

tar-stream is a streaming tar parser and generator.
MIT License
400 stars 93 forks source link

unable to use stream.pipeline() #139

Closed aol-nnov closed 1 year ago

aol-nnov commented 2 years ago

minimal example:

const pipeline = require('stream').pipeline;
const fs = require('fs');
const tar = require('tar-stream');

const extractor = tar.extract();
const repacker = tar.pack();

extractor.on('entry', (header, s, next) => {
    header.name = header.name.replace('rootfs', '.');
    s.pipe(repacker.entry(header, next))
});

extractor.on('finish', function () {
    repacker.finalize();
});

pipeline(fs.createReadStream('./input.tar'),
    extractor,
    repacker,
    fs.createWriteStream('./res.tar'), console.log
)

Actual output:

$ node ./tar-stream-example.js 
internal/streams/pipeline.js:54
  return from.pipe(to);
              ^

TypeError: Cannot read property 'pipe' of undefined
    at pipe (internal/streams/pipeline.js:54:15)
    at Array.reduce (<anonymous>)
    at pipeline (internal/streams/pipeline.js:94:18)
    at Object.<anonymous> (/Users/aol/develop/lotes-local/bootstrap.js/tar-stream-example.js:17:1)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47
fs.createReadStream('./input.tar').pipe(extractor)
.pipe(repacker).pipe(fs.createWriteStream('./res.tar'));

Does not work either. How come? Any workarounds? Why am I forced to create two separate pipes (in -> extract and repack -> out)? Please, help!

Andrey

mafintosh commented 1 year ago

The extracter is not a readable stream, you have to make two pipelines