nodejs / nodejs/node

fs module should support writing from Blob and web ReadableStream sources

オープン
#61,684 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

feature request
主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず既存の fs.write、fs.promises.writeFile、fs.readableWebStream、filehandle.appendFile の動作を確認し、その後 issue の Blob および web ReadableStream のケースと比較してください。完了とは、サポート対象の範囲について合意され、要求された write API が記載された Blob および web ストリームのユースケースを処理できることを意味します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, nodejs
領域
operating-systems
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。