jprichardson / node-fs-extra

Node.js: extra methods for the fs object like copy(), remove(), mkdirs()
MIT License
9.44k stars 776 forks source link

writeFile should accept a stream, like node:fs/promises now allows #977

Closed Arro closed 1 year ago

Arro commented 1 year ago

Attempting to write a stream to a file using fs-extra, my code looks like this:

import os from "os"
import path from "path"

import fs from "fs-extra"

const some_location = path.join(os.tmpdir(), `.some-file.json`)
await fs.writeFile(some_location, some_stream)

// result:
// TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of IncomingMessage

Nowadays, I can do it with node.js:

import os from "os"
import path from "path"

import { writeFile } from "node:fs/promises"

const some_location = path.join(os.tmpdir(), `.some-file.json`)
await writeFile(some_location, some_stream)

// result:
// [it writes the file]

Seeing as fs-extra is attempting to be "node.js' fs, but enhanced", should this be added to fs-extra?

RyanZim commented 1 year ago

fs-extra currently adds promise support to the existing callback API (we added this before fs/promises was a thing). Since fs-extra uses graceful-fs under the hood, we can't use fs/promises as graceful-fs doesn't yet support it (see https://github.com/isaacs/node-graceful-fs/issues/160). If you want this functionality, we suggest using fs/promises directly.