nodejs / nodejs/node

fs module should support writing from Blob and web ReadableStream sources

Aperta
#61,684 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature request
Lingua principale
JavaScript
Stelle
122k
Fork
37.3k
Merge medio
4g 2h
PR unite (30g)
283

Descrizione

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))

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Leggi prima il comportamento esistente di fs.write, fs.promises.writeFile, fs.readableWebStream e filehandle.appendFile, quindi confrontalo con i casi Blob e web ReadableStream dell’issue. Il lavoro è completo quando l’ambito supportato è stato concordato e le API di scrittura richieste gestiscono i casi d’uso Blob e web-stream indicati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, nodejs
Ambito
operating-systems
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.