mafintosh / tar-stream

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

error handling issue #146

Closed splmatto closed 1 year ago

splmatto commented 1 year ago
node -v
v18.12.1

I'm using tar-stream's extract functionality.

I recently upgraded from tar-stream@2.2.0 to tar-stream@3.0.0, and the error handling in my code is no longer triggered when I pass an error to the next function passed to my entry callback.

Here's an attempt at a minimal repo:

#!/usr/bin/env node
const
  { pipeline, finished } = require('node:stream/promises'),
  { extract } = require('tar-stream'),
  fs = require('fs');

const extractor = extract();

let filesProcessed = 0;

extractor.on('entry', async (header, stream, next) => {
  console.log(`extracting ${header.name}`);
  for await (const chunk of stream) {
    console.log(`consuming ${chunk.length} bytes`);
  }

  let err;
  filesProcessed += 1;
  if (filesProcessed > 1) {
    err = new Error('something wicked');
  }

  next(err);
});

extractor.on('error', (e) => {
  console.log(`error ${e.message}`);
});

extractor.on('finish', () => {
  console.log('fin');
});

extractor.on('destroy', () => {
  console.log('destroy');
});

const reader = fs.createReadStream('foo.tar');

(async () => {
  pipeline(
    reader,
    extractor
  ).then(() => console.log('done'))
    .catch((err) => console.log(err));

  await finished(reader, {
  }).catch((e) => {
    console.log(`ERR: ${e.message}`);
  });
  console.log('DONE');
})();

Here's the output when integrating tar-stream@2.2.0:

./extract_err.js
extracting foo.txt
consuming 4 bytes
extracting bar.txt
consuming 4 bytes
error something wicked
Error: something wicked
    at Extract.<anonymous> (/Users/matto/junk/node/extract_err.js:20:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
ERR: something wicked
DONE

And here's the output when integrating tar-stream@3.0.0:

./extract_err.js
extracting foo.txt
consuming 4 bytes
extracting bar.txt
consuming 4 bytes
DONE

Here's the contents of foo.tar:

tar tf foo.tar
foo.txt
bar.txt
goo.txt
mafintosh commented 1 year ago

Thanks, we’ll take a look

air2 commented 1 year ago

I run into the same problem, and can verify with no code change downgrading to 2.2.0 made the errors appear. Also when awaiting the pipeline in 3.0 it never finishes, in 2.2.0 it rejects. So I guess I have to change the code to wait for reader to finish in 3.0.0 and handle the errors another way instead of waiting for the pipeline to finish as I normally would, or stay at 2.2.0 for now.

mafintosh commented 1 year ago

Think this is fixed in 3.1.0