Skip to content

Commit

Permalink
Bring back error handling for ReadableStream.getReader()
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiasBuelens committed Sep 30, 2023
1 parent 7c6d4b7 commit d89a21e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/readable/byob_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ impl<'stream> ReadableStreamBYOBReader<'stream> {
Ok(Self {
raw: stream
.as_raw()
.get_reader_with_options(
.unchecked_ref::<sys::ReadableStreamExt>()
.try_get_reader_with_options(
sys::ReadableStreamGetReaderOptions::new()
.mode(sys::ReadableStreamReaderMode::Byob),
)
)?
.unchecked_into(),
_stream: PhantomData,
})
Expand Down
9 changes: 5 additions & 4 deletions src/readable/default_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ pub struct ReadableStreamDefaultReader<'stream> {

impl<'stream> ReadableStreamDefaultReader<'stream> {
pub(crate) fn new(stream: &mut ReadableStream) -> Result<Self, js_sys::Error> {
if stream.is_locked() {
return Err(js_sys::Error::new("Already locked"));
}
Ok(Self {
raw: stream.as_raw().get_reader().unchecked_into(),
raw: stream
.as_raw()
.unchecked_ref::<sys::ReadableStreamExt>()
.try_get_reader()?
.unchecked_into(),
_stream: PhantomData,
})
}
Expand Down
17 changes: 16 additions & 1 deletion src/readable/sys.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Raw bindings to JavaScript objects used
//! by a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
//! These are re-exported from [web-sys](https://docs.rs/web-sys/0.3.64/web_sys/struct.ReadableStream.html).
use js_sys::Uint8Array;
use js_sys::{Error, Object, Uint8Array};
use wasm_bindgen::prelude::*;
pub use web_sys::ReadableByteStreamController;
// Re-export from web-sys
Expand All @@ -14,6 +14,21 @@ pub use web_sys::ReadableStreamGetReaderOptions;
pub use web_sys::ReadableStreamReaderMode;
pub use web_sys::StreamPipeOptions as PipeOptions;

#[wasm_bindgen]
extern "C" {
/// Additional methods for [`ReadableStream`](web_sys::ReadableStream).
pub(crate) type ReadableStreamExt;

#[wasm_bindgen(method, catch, js_name = getReader)]
pub(crate) fn try_get_reader(this: &ReadableStreamExt) -> Result<Object, Error>;

#[wasm_bindgen(method, catch, js_name = getReader)]
pub(crate) fn try_get_reader_with_options(
this: &ReadableStreamExt,
options: &ReadableStreamGetReaderOptions,
) -> Result<Object, Error>;
}

#[wasm_bindgen]
extern "C" {
/// A result returned by [`ReadableStreamDefaultReader.read`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/read).
Expand Down

0 comments on commit d89a21e

Please sign in to comment.