Repository navigation
FileHandle.stream #38350
Description
Activity
- addedfsIssues and PRs related to file-system APIs and the fs module.Issues and PRs related to file-system APIs and the fs module.feature requestIssues requesting new Node.js features.Issues requesting new Node.js features.good first issueIssues that are suitable for first-time contributors.Issues that are suitable for first-time contributors.
on Apr 22, 2021 Could also have a static alternative.
await fsp.stream(filePath, dst)
Another (less efficient) idea is to have a method that returns an async generator.
await pipeline(fsp.generator(filePath), dst)
Could I take this challenge? 😁
Reacted by Robert Nagy and Hiroyuki YagihashiWhy a
streammethod rather than adding Symbol.asyncIterator? Is the goal perf or ergonomics?Why a
streammethod rather than adding Symbol.asyncIterator? Is the goal perf or ergonomics?Performance.
Reacted by Benjamin GruenbaumJust to expand on this a bit to add to the discussion. I don't have a timeline yet on exactly when I'd be able to continue working on this, but as part of the effort around enabling
fetch(and a few other things), I've been looking at support for the web platform API standard Body mixin. My preference would be for whatever we do here to be aligned with that API.So, for instance, let's assume that a
FileHandleimplemented theBodymixin:const file = await fs.promises.open('file', 'rw'); file.body; // A WHATWG ReadableStream file.arrayBuffer(); // The content of the file as an ArrayBuffer file.blob(); // The content of the file a Blob file.formData(); // Doesn't really make sense here so probably good to omit file.json(); // The content of the file as JSON file.text(); // The content of the file as a string
Following this pattern, I'd suggest a
file.readable()that returns astream.Readableif thefileis readable, and afile.writable()that returns astream.Writableif thefileis writable.Reacted by Benjamin GruenbaumWhat about adding a
Symbol.readablesymbol likeSymbol.asyncIteratorthat enables getting a readable stream version of stuff (like FileHandles)?(Edit: obviously it'd be an
import { ReadableSymbol } from 'stream'rather than monkey patching the global)What about adding a
Symbol.readablesymbol likeSymbol.asyncIteratorthat enables getting a readable stream version of stuff (like FileHandles)?I would kind of like to move away from readables and towards async iterables. But maybe that’s a bigger discussion.
I would kind of like to move away from readables and towards async iterables. But maybe that’s a bigger discussion.
Deprecating streams in favour of async iterables as the contract would be ideal (one stream type, only pull, all modern promises with ease of debugging and syntax assist) eventually would be ideal - it would also make incorporating whatwg streams nicer - but IIRC performance really isn't there.
5 remaining items
Because there is an obvious answer to that: Because we're not using a consistent streams model on the native side.
Also because streams are slowish. Both solutions here live in JS land and one is faster than the other.
I'm fine with not doing this. Was just an idea on how get a little better perf with
FileHandle(I'm doing it like this in our perf sensitive code). In an ergonomic sense I would be fine with havingFileHandle.readable()and/orFileHandle[Symbol.AsyncIteartor]().little late to the party... but how about something like
const file = await fs.getFile('./readme.md') // file instanceof window.File file.stream() // new whatwg:stream ReadableStream() file.text() // Promise<string> file.arrayBuffer() // Promise<ArrayBuffer> await new Response(file).json() await new Response(file).formData()
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.
For more information on how the project manages feature requests, please consult the feature request management document.
- addedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on Apr 4, 2022 There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment.
For more information on how the project manages feature requests, please consult the feature request management document.
It might be possible to have a more efficient way to pipe a file to a stream by adding a
.streammethod toFileHandle.e.g.
vs
Less ergonomic but potentially better performance. This idea needs benchmarking.