mafintosh / tar-stream

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

zero-sized files in tarballs do not call `end` handler #145

Closed mattgodbolt closed 1 year ago

mattgodbolt commented 1 year ago

If you are extracting an archive with zero-sized files in it, then the stream your entry handler is passed has already been end-ed. This means the on('end') event is not triggered.

Our code looks something like:

extract.on('entry', async (header, stream, next) => {
  //...
    const filestream = fs.createWriteStream(filepath);
    stream
        .on('error', error => {
            logger.error(`Error in stream handling: ${error}`);
            reject(error);
        })
        .on('end', () => {
            next();  /// <--- this handler is never called for zero-sized files, hanging the stream forever
        })
        .pipe(filestream);
    stream.resume();
    if (header.size === 0) next(); //// <--- adding this line "fixes" the issue
mattgodbolt commented 1 year ago

The stream for empty files is created here: https://github.com/mafintosh/tar-stream/blob/ca0e270f11dc61f507f7972e53071b8fa99e66bf/extract.js#L174

and the stream has already been ended here: https://github.com/mafintosh/tar-stream/blob/ca0e270f11dc61f507f7972e53071b8fa99e66bf/extract.js#L12

so there's no end() event that's emitted after we have had a change to register an on end handler (as best I can tell).

mafintosh commented 1 year ago

Fixed in 3.1.0