wasm_bindgen_file_reader/
lib.rs

1use js_sys::Number;
2use js_sys::Uint8Array;
3use std::io::Read;
4use std::io::Seek;
5use std::io::SeekFrom;
6use web_sys::FileReaderSync;
7
8thread_local! {
9    static FILE_READER_SYNC: FileReaderSync = FileReaderSync::new().expect("Failed to create FileReaderSync. Help: make sure this is a web worker context.");
10}
11
12/// Wrapper around a `web_sys::File` that implements `Read` and `Seek`.
13pub struct WebSysFile {
14    file: web_sys::File,
15    pos: u64,
16}
17
18impl WebSysFile {
19    pub fn new(file: web_sys::File) -> Self {
20        Self { file, pos: 0 }
21    }
22
23    /// File size in bytes.
24    pub fn size(&self) -> u64 {
25        let size_f64 = self.file.size();
26
27        f64_to_u64_safe(size_f64).expect("file size is not a valid integer")
28    }
29}
30
31/// Convert `u64` to `f64` but only if it can be done without loss of precision (if the number does
32/// not exceed the `MAX_SAFE_INTEGER` constant).
33fn u64_to_f64_safe(x: u64) -> Option<f64> {
34    let x_float = x as f64;
35
36    if x_float <= Number::MAX_SAFE_INTEGER {
37        Some(x_float)
38    } else {
39        None
40    }
41}
42
43/// Convert `f64` to `u64` but only if it can be done without loss of precision (if the number is
44/// positive and it does not exceed the `MAX_SAFE_INTEGER` constant).
45fn f64_to_u64_safe(x: f64) -> Option<u64> {
46    if 0.0 <= x && x <= Number::MAX_SAFE_INTEGER {
47        Some(x as u64)
48    } else {
49        None
50    }
51}
52
53impl Read for WebSysFile {
54    fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
55        let buf_len = buf.len();
56        let old_offset = self.pos;
57        let offset_f64 = u64_to_f64_safe(old_offset).expect("offset too large");
58        let offset_end_f64 = u64_to_f64_safe(
59            old_offset.saturating_add(u64::try_from(buf_len).expect("buffer too large")),
60        )
61        .expect("offset + len too large");
62        let blob = self
63            .file
64            .slice_with_f64_and_f64(offset_f64, offset_end_f64)
65            .expect("failed to slice file");
66        let array_buffer = FILE_READER_SYNC.with(|file_reader_sync| {
67            file_reader_sync
68                .read_as_array_buffer(&blob)
69                .expect("failed to read as array buffer")
70        });
71        let array = Uint8Array::new(&array_buffer);
72        let actual_read_bytes = array.byte_length();
73        let actual_read_bytes_usize =
74            usize::try_from(actual_read_bytes).expect("read too many bytes at once");
75        // Copy to output buffer
76        array.copy_to(&mut buf[..actual_read_bytes_usize]);
77        // Update position
78        self.pos = old_offset
79            .checked_add(u64::from(actual_read_bytes))
80            .expect("new position too large");
81
82        Ok(actual_read_bytes_usize)
83    }
84}
85
86// Copied these functions from std because they are unstable
87fn overflowing_add_signed(lhs: u64, rhs: i64) -> (u64, bool) {
88    let (res, overflowed) = lhs.overflowing_add(rhs as u64);
89    (res, overflowed ^ (rhs < 0))
90}
91
92fn checked_add_signed(lhs: u64, rhs: i64) -> Option<u64> {
93    let (a, b) = overflowing_add_signed(lhs, rhs);
94    if b {
95        None
96    } else {
97        Some(a)
98    }
99}
100
101impl Seek for WebSysFile {
102    fn seek(&mut self, style: SeekFrom) -> Result<u64, std::io::Error> {
103        // Seek impl copied from std::io::Cursor
104        let (base_pos, offset) = match style {
105            SeekFrom::Start(n) => {
106                self.pos = n;
107                return Ok(n);
108            }
109            SeekFrom::End(n) => (self.size(), n),
110            SeekFrom::Current(n) => (self.pos, n),
111        };
112        match checked_add_signed(base_pos, offset) {
113            Some(n) => {
114                self.pos = n;
115                Ok(self.pos)
116            }
117            None => Err(std::io::Error::new(
118                std::io::ErrorKind::InvalidInput,
119                "invalid seek to a negative or overflowing position",
120            )),
121        }
122    }
123}