DioxusLabs / DioxusLabs/dioxus
`byte_stream()` panics with ArrayBuffer cast error
- Dominant language
- Rust
- Stars
- 39.1k
- Forks
- 1.9k
- Avg merge
- 4d 10h
- Merged PRs (30d)
- 4
Description
**Problem**
I encountered a panic when using `FileData::byte_stream()` in my upload implementation. The method currently panics at line 123 in `files.rs`:
```
panicked at dioxus-web-0.7.0-rc.2\src\files.rs:123:69:
called `Result::unwrap()` on an `Err` value: JsValue(ArrayBuffer)
```
Looking at the code, `byte_stream()` tries to cast an `ArrayBuffer` directly to `Uint8Array`:
```rust
let array_buff = wasm_bindgen_futures::JsFuture::from(file.array_buffer())
.await
.unwrap();
let as_uint_array = array_buff.dyn_into::().unwrap(); // ❌ Panics here
```
However, `JsFuture` resolves to an `ArrayBuffer`, which cannot be directly cast to `Uint8Array` via `dyn_into()`.
## Possible Quick Fix
A simple fix would be to construct `Uint8Array` from the `ArrayBuffer`:
```rust
let array_buff = wasm_bindgen_futures::JsFuture::from(file.array_buffer())
.await
.unwrap();
let as_uint_array = Uint8Array::new(&array_buff); // ✅ Construct instead of cast
Ok(as_uint_array.to_vec().into())
```
**Steps To Reproduce**
- make somehow `FileData`
- call `file_data.byte_stream()`
**Expected behavior**
**Environment:**
- `dioxus-web`: 0.7.0-rc.2
- Target: `wasm32-unknown-unknown`
- Browser: Chrome
**Questionnaire**
I noticed the comment in the code mentions:
> We just read the entire file into memory and return it as a single chunk.
> This is not super great, especially given the wasm <-> js boundary duplication cost.
Is there a planned direction for how `byte_stream()` should be implemented more efficiently? Should it:
- Use the Blob's `stream()` method with ReadableStream?
- Implement chunked reading somehow?
- Or is the current "read entire file" approach acceptable for now, just needs the panic fixed?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.