-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Blob and FileReader #164
Comments
While |
Libuv has a sendfile() abstraction that papers over the platform differences. It round-trips through the thread pool so it's not necessarily faster than plain read+write but it's a good starting point. That said, sendfile() is a one-trick pony. In libuv, we would want a more generic API - possibly one loosely modeled after UNIX pipes - so we can use splice() or other operating system-specific primitives where appropriate. |
It's a one trick pony but it's a very common use case IMO. @manvalls I'm not sure I understand why you want a |
@benjamingr |
This has been discussed in nodejs/node-v0.x-archive#1802... |
Blob does not specify whether the information it represents is in memory or on disk. At some point, to send the data, something (the kernel, userland code, or JS) will have to load at least part of the information into memory, regardless of whether it was marshalled using In other words, the Blob, File, and FileReader APIs are not the cornerstone of this proposal: the goal is to be able to transparently reduce unnecessary marshalling and unmarshalling from kernel to heap, and from heap to JS during reads and writes to resources. While a small win could be attained by adding a Work towards enabling this sort of communication between streams is underway on the whatwg/streams spec to make off-main-thread piping possible. This is delicate, intricate work; and rather than implement this functionality ourselves at present, it would be best to contribute to that work and see what lessons can be learned from its outcomes. Happily, this fits in with the larger desire to gradually move towards whatwg specs where feasible. In summary: the |
one use case that is hard to do in node that blobs excel at in the browser is blob urls, the ability to put random data in a blob and treat it like a url, the inability to do this in node (especially for files) can be annoying when trying to use poorly written libraries that will only take a file path (e.g. compositing in image magic since you need to pass it 2 files and it can only take one from stdin). While there are some hacks using named pipes that sorta do the trick, god I miss blob urls in node. |
In the browser we've got the
Blob
object, which represents some binary data that typically is stored in the hard drive. If we want to read said data, we use theFileReader
object. Sometimes, we just don't need to read that data, instead we want to send it, and the actual contents of it don't mind us at all.In the browser this is straightforward, we just
obj.send(blob);
, but if we want to do the same thing using node'sBuffer
API, we've got to read that data from the hard drive first, and thus consuming a considerable amount of memory, and then send it. The closest thing to sending Blobs I can think of in the Node world isstream.pipe(otherStream);
but this is still implemented on plain javascript, loading hd data in memory bit by bit. Multiply those bits of binary data by thousands of connections and you may find a bottleneck.Such operations should be done on a lower level, allowing things like the
sendfile
system call. Imo, implementing theBlob
object, at first as some sugar aroundfs
and later as a full lower level implementation, would allow us to save some memory and, at the same time, reduce the gap between Node and the browser's worlds.The text was updated successfully, but these errors were encountered: