kaitai_struct_javascript_runtime
kaitai_struct_javascript_runtime copied to clipboard
Port JavaScript runtime to TypeScript
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.jsfile and added it to the.gitignore - Updated
.npmignore
Some caveats from that PR still apply:
- This uses
Object.setPrototypeOfto work around potential Error inheritence issues - This uses
rollup.jsas 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.lengthandarr[0]I've typed asArrayLike<number>instead of something more specific likeUint8Array) - 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.
Looking forward to this PR. 👀