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

stream.pipeline [ERR_STREAM_PREMATURE_CLOSE]: Premature close #228

Closed
boggddan opened this issue Dec 30, 2020 · 14 comments
Closed

stream.pipeline [ERR_STREAM_PREMATURE_CLOSE]: Premature close #228

boggddan opened this issue Dec 30, 2020 · 14 comments

Comments

@boggddan
Copy link

Hello. Could help me. I am using I am using stream.pipeline but I have an error [ERR_STREAM_PREMATURE_CLOSE]: Premature close. The file is unpacked, apparently the error occurs due to the fact that it happens with method destroy() instead of end().

stackoverflow

How do I fix this error? Thank you.

Ububtu 20.10 x64
Node.js 15.4.0

import fs from 'fs';
import { Transform } from 'stream';
import { pipeline } from 'stream/promises';
import path from 'path';
import unzipper from 'unzipper';

const fileNameZip = '17-ufop.zip';

(async () => {
  await pipeline(
    fs.createReadStream(fileNameZip),
    unzipper.Parse({ forceStream: true }),
    async (source) => {
      for await (const chunk of source) {
        const fileName = path.basename(chunk.path);
        console.log(fileName)
        if (/17\.1-EX_XML_EDR_UO/.test(fileName)) {
          await pipeline(chunk, fs.createWriteStream(fileName));
        } else {
          chunk.autodrain();
        }
      }
    }
  );

  console.log('Done');
})();

Error

node:internal/process/promises:227
          triggerUncaughtException(err, true /* fromPromise */);
          ^

Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
    at new NodeError (node:internal/errors:278:15)
    at Parse.onclose (node:internal/streams/end-of-stream:117:38)
    at Parse.emit (node:events:388:22)
    at Parse.<anonymous> (/home/user/Project/ufopParser/node_modules/unzipper/lib/parse.js:29:10)
    at Parse.emit (node:events:388:22)
    at finish (node:internal/streams/writable:734:10)
    at processTicksAndRejections (node:internal/process/task_queues:80:21) {
  code: 'ERR_STREAM_PREMATURE_CLOSE'
}
@eberlitz
Copy link

eberlitz commented Sep 1, 2021

Any solutions to this? I'm facing the same error here:

await pipeline(
      createReadStream('./sample.zip'),
      Parse({ forceStream: true }),
      async function processZip(entries: AsyncIterable<Entry>) {
        for await (const entry of entries) {
          const { path, type } = entry;
          console.log(path, type);
          await entry.autodrain();
        }
        return true;
      }
    );

Gives me Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close.

Node version v14.17.0
unzipper@0.10.11

@zachsa
Copy link

zachsa commented Jan 17, 2022

I get this error as well, having upgraded from Node 16 to Node 17

@theguriev
Copy link

Same error on our side after update :(

@tinesoft
Copy link

tinesoft commented Mar 3, 2022

any update on this @ZJONSSON ?

@tinesoft
Copy link

tinesoft commented Mar 16, 2022

This error looks like a misuse of the stream API in the first place, that is now more severely reported by Node since v17+.

Maybe @mcollina and @ronag (nodejs core members) can give a hand here?

I'd gladly provide my assistance with a repro project, but unfortunately I don't know enough about streams to provide a full PR... :-(

@wolfymaster
Copy link

wolfymaster commented Oct 28, 2022

I am also now facing this issue. This was not an issue on Node v12.. but we recently upgraded to Node v18 and I'm receiving this error. I'm not able to find a workaround.

EDIT: Found a workaround:

stream
    .pipe(unzipper.Parse())
    .on('entry', function (entry) {
      const fileName: string = entry.path;
      const type = entry.type; // 'Directory' or 'File'
      if (type === 'File')) {
       // do some stuff
      } else {
        entry.autodrain();
      }
    })
    .promise();

Seems like maybe it's just an issue with the async for...of ?

@BrunoCambianica
Copy link

BrunoCambianica commented Jan 23, 2023

👋
I had the same issue as well when using the async for of on node 18.
I did switch to Transform from stream and it works correctly:

stream.pipe(
    new Transform({
      objectMode: true,
      transform(entry, _, callback) {
        if (entry.type === 'File') {
          // process data
          callback()
        } else {
          entry.autodrain();
          callback()
        }
      },
    })
)

@ronag
Copy link

ronag commented Jan 23, 2023

Is this a problem in Node 19 as well?

@eamon0989
Copy link

I'm also facing this issue on Node 18, as mentioned above, it seems to be an issue with async for...of. @wolfymaster's solution above worked as a (hopefully temporary) fix.

@some-dev-1
Copy link

Can confirm that @wolfymaster's solution works for me too.

@bassmanitram
Copy link

bassmanitram commented May 9, 2023

Unfortunately I use an async/await-based batching algorithm right in the middle of that do some stuff, so the listener function has to be async, which of course is the root of the issue. I don't have the time or budget to completely refactor the code to make that work. #269 provides the real fix, and my comment there explains what I'm doing to get the fix, pending the PR merge.

@ZJONSSON
Copy link
Owner

ZJONSSON commented May 9, 2023

Will merge later today

@ZJONSSON
Copy link
Owner

This PR has been published as a patch version #274

@bassmanitram
Copy link

THANK you - and I apologize for my doubts!

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