iwoplaza / typed-binary

Describe binary structures with full TypeScript support. Encode and decode into pure JavaScript objects.
https://iwoplaza.github.io/typed-binary/
MIT License
106 stars 3 forks source link

Support all TypedArray primitives #8

Closed mkapal closed 2 months ago

mkapal commented 6 months ago

I'm happy to have found this library, it looks very promising!

One thing I'm missing is support for converting all primitives from the TypedArray group - that is all signed/unsigned integers, 16-bit integers and BigInts.

Do you plan to add those in the near future? Or I could try to do it myself and create a PR.

iwoplaza commented 4 months ago

Thank you for the suggestion! It does seem to be a rather general feature, enough to be included in the library. I will start work on it asap, apologies for the late response 💜

iwoplaza commented 4 months ago

I am currently wondering about one thing. Lets say we have the following code:

import { object, string, u8array } from 'typed-binary';

const Packet = object({
  label: string,
  data: u8Array(64),
});

const buffer = /* getting the data from somewhere */;
const reader = new BufferReader(buffer);

// type of packet inferred as { label: string, data: Uint8Array }
const packet = Packet.read(reader);

packet.data[0] = 'B'; // does this mutate `buffer`, or not?

Basically the question is, is packet.data a view into buffer (less copying, more efficient to read), or is it a copy (safer, more consistent behavior). I am leaning towards copying, just because if buffer changes, packet.label is not going to change, but packet.data IS going to change, and that's not consistent.

mkapal commented 3 months ago

I also think it should make a safe copy of the buffer so mutating it would not affect the original buffer.

iwoplaza commented 2 months ago

@mkapal The implementation and docs should be ready for a review, would love if you could take a look at it before merging 💚