Skip to content

Commit

Permalink
Accept ArrayBufferLike in deserialize
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Nov 28, 2024
1 parent 589e582 commit b0275a5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 11 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ export function serialize(instance: unknown): Uint8Array {
/**
* Deserializes a struct from a Uint8Array
*/
export function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBufferView) {
export function deserialize(instance: unknown, _buffer: ArrayBufferLike | ArrayBufferView) {
checkInstance(instance);
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][Symbol.struct_metadata];

const buffer = _buffer instanceof Uint8Array ? _buffer : new Uint8Array('buffer' in _buffer ? _buffer.buffer : _buffer);

const view = new DataView(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));
const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteOffset + buffer.byteLength);

for (const [name, { type, offset, length }] of members) {
for (let i = 0; i < (length || 1); i++) {
Expand Down

0 comments on commit b0275a5

Please sign in to comment.