kaitai_struct_javascript_runtime icon indicating copy to clipboard operation
kaitai_struct_javascript_runtime copied to clipboard

Port JavaScript runtime to TypeScript

Open Tschrock opened this issue 4 years ago • 1 comments

This is a continuation of #18, resolving some of the comments there and adding additional types and fixes.

Notable changes from #18:

  • Merged in new changes from master
  • Added types for all members and methods
  • Changed error types to pass the message in the super() call
  • Added jsdoc comments for all method arguments/returns
  • Removed the generated KaitaiStream.js file and added it to the .gitignore
  • Updated .npmignore

Some caveats from that PR still apply:

  • This uses Object.setPrototypeOf to work around potential Error inheritence issues
  • This uses rollup.js as the bundler to get a global fallback for umd modules
  • CI/Tests may need additional changes to build the runtime before running

Along with some additional notes:

  • Not all of the types for method arguments were obvious if the function wasn't directly used or didn't use the argument in a way that narrowed it down. I didn't want to make too many assumptions about them, so in those places I've used looser types based on how the function uses it (for example, an argument that's only used with arr.length and arr[0] I've typed as ArrayLike<number> instead of something more specific like Uint8Array)
  • I've made the assumption that methods/members starting with an underscore (like _trimAlloc()) are intended to be private and left the rest public. It's possible there's other methods that were originally intended to be private but weren't marked.
  • I've left many of the exception arguments typed as any, since it was hard to find examples of them being used and theoretically anything could be passed in.

I've managed to run the kaitai tests with this in a node environment, and the results match the current JS version.

I don't feel 100% confident with this yet - but that's mostly because it wasn't clear what all the types should be. The functionality itself hasn't changed much and should still be solid.

Tschrock avatar Dec 03 '21 15:12 Tschrock

Looking forward to this PR. 👀

benzsuankularb avatar Mar 03 '22 16:03 benzsuankularb