str4d / wage

A WASM package and web app for encrypting and decrypting age-encrypted files, powered by rage.
https://rage-encry.pt
Apache License 2.0
74 stars 14 forks source link

Expose a Rust stream to JavaScript as a user-downloadable file #1

Closed str4d closed 4 years ago

str4d commented 4 years ago

The wage::decryptor_for_file API takes a JavaScript File and returns a handle to an age::Decryptor that wraps File.stream(). It would be great if there were a way to similarly expose age::stream::StreamReader as a JavaScript ReadableStream that could be passed back out to the browser, giving it the option to stream-process the decrypted file instead of having the entire decrypted file read into memory.

One issue here is that the Rust handle stores a lifetime, and wasm-bindgen doesn't currently support returning objects with lifetimes (https://github.com/rustwasm/wasm-bindgen/issues/1187). So we may need to hack around that with unsafe, or alternatively have an API that takes a JavaScript TransformStream and writes the decrypted file to it (which would make for a somewhat wonky JavaScript API, but should still enable building JS pipelines).

str4d commented 4 years ago

It turns out that wasm-streams can convert a Box<dyn Stream<Item = Result<JsValue, JsValue>>> into a ReadableStream (source).