Skip to content

calling into_async_read panics whereas into_stream works well #19

@bmatthieu3

Description

@bmatthieu3

Hi,

I encounter some issues calling into_async_read on a ReadableStream:

Uncaught (in promise) Error: already locked to a reader, or not a readable byte stream

On the other hand, calling into_stream on a ReadableStream works perfectly and allows me to iterate over the chunk of data (as like in the example of the repo).

Here is my rust code:

        wasm_bindgen_futures::spawn_local(async move {
            use wasm_streams::ReadableStream;
            use web_sys::Response;
            use web_sys::window;
            use wasm_bindgen_futures::JsFuture;
            use futures::StreamExt;
            use wasm_bindgen::JsCast;
            use futures::TryStreamExt;
    
            let window = window().unwrap_throw();
            let resp_value = JsFuture::from(window.fetch_with_str(&url))
                .await
                .map_err(|_| "fetch failed").unwrap_throw();
            let resp: Response = resp_value.dyn_into().unwrap_throw();
        
            // Get the response's body as a JS ReadableStream
            let raw_body = resp.body().unwrap_throw();
            let body = ReadableStream::from_raw(raw_body.dyn_into().unwrap_throw());

            // Converting to an IntoAsyncRead panics
            let mut async_read = body.into_async_read();
            // While converting the JS ReadableStream to a Rust stream does work
            //let mut stream = body.into_stream();
            // Consume the stream, logging each individual chunk
            /*while let Some(Ok(chunk)) = stream.next().await {
                al_core::log(&format!("{:?}", chunk));
            }*/
        });

Thank you very much for your help,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions