0

To upload a file locally I'm using the following code:

import {pipeline} from "stream";
import {promisify} from "util";

const asyncPipeline = promisify(pipeline);

await asyncPipeline(
    fileData.fileStream,
    fs.createWriteStream(
        path.join(assetsPath, fileName),
        {

            flags: "wx"
        }
    )
);

My questions:

  1. It does work but I'm not sure if I need to do something extra, for instance, to close a stream manually, or the default true-value of autoClose does it for me automatically?

  2. How can I ensure that the WriteStream is closed?

Mike
  • 14,010
  • 29
  • 101
  • 161

1 Answers1

1

You're right that the pipeline option autoClose's default value of true will handle the closing of the stream. Whether the pipeline fails or succeeds, you can rest assured that the file descriptor will automatically be closed.

For more information on options and error handling, see the pipeline documentation.

JRagone
  • 60
  • 1
  • 11