kaitai_struct_javascript_runtime icon indicating copy to clipboard operation
kaitai_struct_javascript_runtime copied to clipboard

Open large binary file in kaitai struct for javascript?

Open Meigyoku-Thmn opened this issue 7 years ago • 4 comments

The current javascript implementation requires reading a whole file into memory (ArrayBuffer). This is not good if I want to read a large file, for example a very big zip file. Can you add an option to use file descriptor (returned by fs.openSync) instead of ArrayBuffer?

Meigyoku-Thmn avatar May 22 '18 10:05 Meigyoku-Thmn

That's actually a viable idea, which would hopefully require only adjustments to runtime. Any JavaScript volunteers?

GreyCat avatar May 22 '18 11:05 GreyCat

This is even possible in browsers using <input type="file">:

const file = input.files[0]
const slice = file.slice(offset, offset + size)

slice.arrayBuffer().then(
    arrayBuffer => {
        const data = new Uint8Array(arrayBuffer)
    }
)

cherue avatar Mar 15 '20 08:03 cherue

Wow, that's even better :)

I wonder what the overhead of using slice() + arrayBuffer() + new Uint8Array for every single read will be significant vs one ArrayBuffer + Uint8Array for whole file?

GreyCat avatar Mar 15 '20 10:03 GreyCat

Yeah that'll probably be an issue, especially for small files.

Maybe something like Python's from_bytes and from_file could work, if it all goes through the KaitaiStream constructor it wouldn't even need compiler changes.

  • ArrayBuffer would be the current implementation
  • Blob (File's ancestor) would be reading from a file in the browser
  • string (path) and number (file descriptor) would read from a file in Node

cherue avatar Mar 15 '20 12:03 cherue