Replies: 2 comments
-
|
The solution above works, but is hacky. I also haven't put much effort into thinking how it works when reading a very large json project file. We would presumably need to stream on the read side as well, and I haven't explored all of the nuances of that. In any case it's clumsy solution, and far from ideal. It doesn't give us "real" disk access. For example we can't read and write temporary files, and we certainly can't perform seek operations on an open file descriptor. For those cases, we could use something like bro-fs or browserfs ([docs here])(http://jvilk.com/browserfs/1.4.1/), or use LocalStorage/IndexedDB directly. However, eventually we would want to write the contents to disk, and we're back to the JSON too large problem. We also can't traverse a directory in order to read multiple files, along with their pathnames (although browserfs lets us use Dropbox as a backend, so maybe that would be a less than ideal solution). Given that, I think we should eschew the above solution, or any like it, and make the definitive decision to move towards File System Access API, abandoning Firefox support as a consequence. We can also submit a request to the Firefox team in the dim hopes that they see reason. |
Beta Was this translation helpful? Give feedback.
-
|
Hi @gnodar01, I'm the maintainer of BrowserFS. I'd like to let you know that BrowserFS has been deprecated. You can read more about on the BrowserFS readme (https://github.com/jvilk/BrowserFS). ZenFS is an updated and active fork that I created. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
There are several motivating reasons to move to File System Access API as discussed in #276, under "Project File Storage and Retrieval". With respect to issue #270, there is an alternative solution. We can create a streaming download solution, without having to use the File System Access API.
big-json is a package meant for node. Many node-oriented packages can be run in the browser via polyfills. Webpack <= 4 used to provide those polyfills by default, but Webpack > 4 does not. You can mess with the webpack config file to override this default, but since we’re using Create-React-App, that usually means ejecting, which isn’t the end of the world, but I’d rather avoid it. Alternatively we could use react-app-rewired to tweak webpack config without ejecting. With that, we can install the necessary node polyfills for big-json, which are stream-browserify and assert, and process. In project root, we would create
config-overrides.jswith something like the following, telling webpack how to resolve the missing dependencies:Then in
package.json, we’d need to replace all instances ofreact-scriptswithreact-app-rewired.This will achieve streaming JSON stringification and parsing. With that in place, we can no longer use file-saver, because blobs have a size limitation as well (500 mb - 2 GB), similar to size limits for JSON.stringify, and for more generally variables holding strings. We’d have to replace it with StreamSaver.
StreamSaver uses a MITM in order to install a service worker, which uses
respondWithto act as a psuedo-server. The data stream is transfered to the service worker usingpostMessage, and then the worker creates a download link that we open, allowing us to stream to disk.big-json.createStringifyStreamreturns aJsonStreamStringifyobject (json-stream-stringify package), which is a NodeJS read stream. StreamSaver however uses the web stream API, and provides a web writable stream oncreateWriteStream. We can't just callpipeon the node-oriented read stream to the web-oriented write stream. To convert the node read stream to a web read stream, we can use readable-stream-node-to-web . Then we can callpipeToon the newly converted web read stream to the web write stream.An alternative is to eschew piping altogether, and use the
dataevent handler on the node-oriented read stream in order to build an array of stringified JSON chunks. Once that array is built, we can construct aBlobout of it, and stream that to the web write stream provided byStreamSaver.Beta Was this translation helpful? Give feedback.
All reactions