diff options
author | Luca Casonato <hello@lcas.dev> | 2022-10-09 16:49:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-09 14:49:25 +0000 |
commit | 3b6b75bb46840a897a310dfd3fcbbd05618f3c5b (patch) | |
tree | 2d4d06cef295f9bcd5c1829f5aa41c11e8aa0a6e /ext/fetch/lib.rs | |
parent | a622c5df27e908bff152ce7374c47dabfdba0bba (diff) |
feat(core): improve resource read & write traits (#16115)
This commit introduces two new buffer wrapper types to `deno_core`. The
main benefit of these new wrappers is that they can wrap a number of
different underlying buffer types. This allows for a more flexible read
and write API on resources that will require less copying of data
between different buffer representations.
- `BufView` is a read-only view onto a buffer. It can be backed by
`ZeroCopyBuf`, `Vec<u8>`, and `bytes::Bytes`.
- `BufViewMut` is a read-write view onto a buffer. It can be cheaply
converted into a `BufView`. It can be backed by `ZeroCopyBuf` or
`Vec<u8>`.
Both new buffer views have a cursor. This means that the start point of
the view can be constrained to write / read from just a slice of the
view. Only the start point of the slice can be adjusted. The end point
is fixed. To adjust the end point, the underlying buffer needs to be
truncated.
Readable resources have been changed to better cater to resources that
do not support BYOB reads. The basic `read` method now returns a
`BufView` instead of taking a `ZeroCopyBuf` to fill. This allows the
operation to return buffers that the resource has already allocated,
instead of forcing the caller to allocate the buffer. BYOB reads are
still very useful for resources that support them, so a new `read_byob`
method has been added that takes a `BufViewMut` to fill. `op_read`
attempts to use `read_byob` if the resource supports it, which falls
back to `read` and performs an additional copy if it does not. For
Rust->JS reads this change should have no impact, but for Rust->Rust
reads, this allows the caller to avoid an additional copy in many
scenarios. This combined with the support for `BufView` to be backed by
`bytes::Bytes` allows us to avoid one data copy when piping from a
`fetch` response into an `ext/http` response.
Writable resources have been changed to take a `BufView` instead of a
`ZeroCopyBuf` as an argument. This allows for less copying of data in
certain scenarios, as described above. Additionally a new
`Resource::write_all` method has been added that takes a `BufView` and
continually attempts to write the resource until the entire buffer has
been written. Certain resources like files can override this method to
provide a more efficient `write_all` implementation.
Diffstat (limited to 'ext/fetch/lib.rs')
-rw-r--r-- | ext/fetch/lib.rs | 63 |
1 files changed, 42 insertions, 21 deletions
diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index 0adc32343..b8f784284 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -5,11 +5,14 @@ mod fs_fetch_handler; use data_url::DataUrl; use deno_core::error::type_error; use deno_core::error::AnyError; +use deno_core::futures::stream::Peekable; use deno_core::futures::Future; use deno_core::futures::Stream; use deno_core::futures::StreamExt; use deno_core::include_js_files; use deno_core::op; +use deno_core::BufView; +use deno_core::WriteOutcome; use deno_core::url::Url; use deno_core::AsyncRefCell; @@ -43,15 +46,14 @@ use serde::Deserialize; use serde::Serialize; use std::borrow::Cow; use std::cell::RefCell; +use std::cmp::min; use std::convert::From; use std::path::Path; use std::path::PathBuf; use std::pin::Pin; use std::rc::Rc; -use tokio::io::AsyncReadExt; use tokio::sync::mpsc; use tokio_stream::wrappers::ReceiverStream; -use tokio_util::io::StreamReader; // Re-export reqwest and data_url pub use data_url; @@ -252,7 +254,7 @@ where match data { None => { // If no body is passed, we return a writer for streaming the body. - let (tx, rx) = mpsc::channel::<std::io::Result<Vec<u8>>>(1); + let (tx, rx) = mpsc::channel::<std::io::Result<bytes::Bytes>>(1); // If the size of the body is known, we include a content-length // header explicitly. @@ -401,12 +403,11 @@ pub async fn op_fetch_send( let stream: BytesStream = Box::pin(res.bytes_stream().map(|r| { r.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err)) })); - let stream_reader = StreamReader::new(stream); let rid = state .borrow_mut() .resource_table .add(FetchResponseBodyResource { - reader: AsyncRefCell::new(stream_reader), + reader: AsyncRefCell::new(stream.peekable()), cancel: CancelHandle::default(), size: content_length, }); @@ -446,7 +447,7 @@ impl Resource for FetchCancelHandle { } pub struct FetchRequestBodyResource { - body: AsyncRefCell<mpsc::Sender<std::io::Result<Vec<u8>>>>, + body: AsyncRefCell<mpsc::Sender<std::io::Result<bytes::Bytes>>>, cancel: CancelHandle, } @@ -455,17 +456,16 @@ impl Resource for FetchRequestBodyResource { "fetchRequestBody".into() } - fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> { + fn write(self: Rc<Self>, buf: BufView) -> AsyncResult<WriteOutcome> { Box::pin(async move { - let data = buf.to_vec(); - let len = data.len(); + let bytes: bytes::Bytes = buf.into(); + let nwritten = bytes.len(); let body = RcRef::map(&self, |r| &r.body).borrow_mut().await; let cancel = RcRef::map(self, |r| &r.cancel); - body.send(Ok(data)).or_cancel(cancel).await?.map_err(|_| { + body.send(Ok(bytes)).or_cancel(cancel).await?.map_err(|_| { type_error("request body receiver not connected (request closed)") })?; - - Ok(len) + Ok(WriteOutcome::Full { nwritten }) }) } @@ -478,7 +478,7 @@ type BytesStream = Pin<Box<dyn Stream<Item = Result<bytes::Bytes, std::io::Error>> + Unpin>>; struct FetchResponseBodyResource { - reader: AsyncRefCell<StreamReader<BytesStream, bytes::Bytes>>, + reader: AsyncRefCell<Peekable<BytesStream>>, cancel: CancelHandle, size: Option<u64>, } @@ -488,15 +488,36 @@ impl Resource for FetchResponseBodyResource { "fetchResponseBody".into() } - fn read_return( - self: Rc<Self>, - mut buf: ZeroCopyBuf, - ) -> AsyncResult<(usize, ZeroCopyBuf)> { + fn read(self: Rc<Self>, limit: usize) -> AsyncResult<BufView> { Box::pin(async move { - let mut reader = RcRef::map(&self, |r| &r.reader).borrow_mut().await; - let cancel = RcRef::map(self, |r| &r.cancel); - let read = reader.read(&mut buf).try_or_cancel(cancel).await?; - Ok((read, buf)) + let reader = RcRef::map(&self, |r| &r.reader).borrow_mut().await; + + let fut = async move { + let mut reader = Pin::new(reader); + loop { + match reader.as_mut().peek_mut().await { + Some(Ok(chunk)) if !chunk.is_empty() => { + let len = min(limit, chunk.len()); + let chunk = chunk.split_to(len); + break Ok(chunk.into()); + } + // This unwrap is safe because `peek_mut()` returned `Some`, and thus + // currently has a peeked value that can be synchronously returned + // from `next()`. + // + // The future returned from `next()` is always ready, so we can + // safely call `await` on it without creating a race condition. + Some(_) => match reader.as_mut().next().await.unwrap() { + Ok(chunk) => assert!(chunk.is_empty()), + Err(err) => break Err(AnyError::from(err)), + }, + None => break Ok(BufView::empty()), + } + } + }; + + let cancel_handle = RcRef::map(self, |r| &r.cancel); + fut.try_or_cancel(cancel_handle).await }) } |