Skip to content

Commit

Permalink
Merge pull request #50 from str4d/writablestream_into_async_write
Browse files Browse the repository at this point in the history
Use `WritableStream::into_async_write`
  • Loading branch information
str4d authored Nov 21, 2022
2 parents 5de7b79 + f36d8a2 commit 1a64717
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 68 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ impl Encryptor {
) -> Result<wasm_streams::writable::sys::WritableStream, JsValue> {
// Convert from the opaque web_sys::WritableStream Rust type to the fully-functional
// wasm_streams::writable::WritableStream.
let sink = WritableStream::from_raw(output).into_sink();
let stream = WritableStream::from_raw(output);

let writer = self
.0
.wrap_async_output(shim::SinkWriter::new(sink, CHUNK_SIZE))
.wrap_async_output(stream.into_async_write())
.await
.map_err(|e| JsValue::from(format!("{}", e)))?;

Expand Down
66 changes: 0 additions & 66 deletions src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,75 +7,9 @@ use futures::{
};
use js_sys::Uint8Array;
use pin_project::pin_project;
use std::io;
use std::pin::Pin;
use wasm_bindgen::prelude::*;

#[pin_project(project = SinkWriterProj)]
pub(crate) struct SinkWriter<S: Sink<JsValue, Error = JsValue> + Unpin> {
#[pin]
sink: S,
default_chunk_size: usize,
chunk: Vec<u8>,
}

impl<S: Sink<JsValue, Error = JsValue> + Unpin> SinkWriter<S> {
pub(crate) fn new(sink: S, default_chunk_size: usize) -> Self {
SinkWriter {
sink,
default_chunk_size,
chunk: Vec::with_capacity(default_chunk_size),
}
}

fn poll_sink_chunk(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
let SinkWriterProj {
mut sink, chunk, ..
} = self.project();

if !chunk.is_empty() {
ready!(sink.as_mut().poll_ready(cx))
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("JS error: {:?}", e)))?;
sink.as_mut()
.start_send(Uint8Array::from(&chunk[..]).into())
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("JS error: {:?}", e)))?;
chunk.clear();
}

Poll::Ready(Ok(()))
}
}

impl<S: Sink<JsValue, Error = JsValue> + Unpin> AsyncWrite for SinkWriter<S> {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
if self.chunk.len() >= self.default_chunk_size {
ready!(self.as_mut().poll_sink_chunk(cx))?;
}
self.chunk.extend_from_slice(buf);
Poll::Ready(Ok(buf.len()))
}

fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
ready!(self.as_mut().poll_sink_chunk(cx))?;
self.project()
.sink
.poll_flush(cx)
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("JS error: {:?}", e)))
}

fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
ready!(self.as_mut().poll_sink_chunk(cx))?;
self.project()
.sink
.poll_close(cx)
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("JS error: {:?}", e)))
}
}

struct Chunk {
bytes: SecretVec<u8>,
offset: usize,
Expand Down

1 comment on commit 1a64717

@SpencerLobree
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@str4d hi ,i tried to contact you via twitter, but i'm not sure if you saw my message...

I'm looking for a dev with your stack/advisor/consultant to work on our platform , Metify[.]tech, as a freelancer or full-time . some projects start soon and others begin in 6-12 months ,so if you're busy now ,we can discuss it in advance... are you up ? contact me on telegram @SpencerLobree or via email at [email protected]

Please sign in to comment.