nodejs / nodejs/node

fs module should support writing from Blob and web ReadableStream sources

未关闭
#61,684 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

feature request
主要语言
JavaScript
星标
122k
派生
37.3k
平均合并
4 天 2 小时
30 天内合并 PR
283

描述

What is the problem this feature will solve?

Currently, all Node.js fs module methods for writing data (such as fs.write, fs.createWriteStream) only accept Buffers or strings. There is no built-in support for writing data directly from Blob or web ReadableStream sources. This limits interoperability with web platform APIs and makes working with large files or streaming data @less seamless, especially when trying to move data efficiently from web contexts into the filesystem. Furthermore, Node.js provides fs.readableWebStream, but no corresponding fs.createWritableStream that would allow writing from web streams or Blobs directly.

What is the feature you are proposing to solve the problem?

Extend the Node.js fs module with the following capabilities:

  • Allow fs.write and related write methods to accept Blob objects as a valid input, making it possible to write the entirety of a Blob's contents to a file without manual conversion to Buffer.
  • Introduce fs.createWritableStream (web-compatible), which would be a direct counterpart to fs.readableWebStream and support writing from web ReadableStream sources to files.

This would improve ease-of-use when working with large binary/web platform data and make fs more aligned with modern web standards.

What alternatives have you considered?

Current workarounds involve manually converting Blobs and web streams into Buffers or converting web streams into node:streams

The only solution we have today is to use the fs.promise.writeFile(path, webStream) which luckily supports async iterable

with that in mind, the NodeJS document for...

... filehandle.appendFile(data[, options]) says it support:

data: <string> | <Buffer> | <TypedArray> | <DataView> | <AsyncIterable> | <Iterable> | <Stream>

it should also say

data: <string> | <Buffer> | <TypedArray> | <DataView> | <AsyncIterable> | <Iterable> | <Stream> | <ReadableStream>

or just

data: <string> | <Buffer> | <TypedArray> | <DataView> | <AsyncIterable> | <Iterable>

as stream's are async iterable

and i also do not thing it should say Buffer anymore... buffer is a typed array already... so it can just say:

data: <string> | <TypedArray> | <DataView> | <AsyncIterable> | <Iterable>

but this is just only off-topic


able to write Blob's would enable efficient native copy operations (like using openAsBlob(path) and writing directly to another file) without data passing through the JS runtime.

able to write blobs avoids extra conversions/allocations in JS, reduces memory footprint and GC pressure


the solution i'm looking for is something like:

blob = new Blob(['hello world'])
await filehandle.write(blob)

await blob.stream().pipeTo(fs.createWritableStream(dest))

// using fetch is becoming more popular, so a web writable stream is highly requested
await new Response('...').body.pipeTo(fs.createWritableStream(dest))

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先阅读现有的 fs.write、fs.promises.writeFile、fs.readableWebStream 和 filehandle.appendFile 行为,然后将其与 issue 中的 Blob 和 web ReadableStream 情况进行比较。完成的标准是,已就支持范围达成一致,并且所请求的写入 API 能够处理所述的 Blob 和 web-stream 用例。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, nodejs
领域
operating-systems
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
冷清
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。