Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use slice for returrning an Array Buffer #34

Open
kion-dgl opened this issue Sep 5, 2023 · 0 comments
Open

Use slice for returrning an Array Buffer #34

kion-dgl opened this issue Sep 5, 2023 · 0 comments

Comments

@kion-dgl
Copy link

kion-dgl commented Sep 5, 2023

I ran into this issue when building this example: https://kion-dgl.github.io/three-cbor-loader/ because the model wasn't loading for the CBOR model, but it worked when emulated loading the CBOR model by loading the JSON and putting in ArrayBuffer where they were expected.

The issue turned out to be here: https://github.com/paroga/cbor-js/blob/master/cbor.js#L199

  function readArrayBuffer(length) {
    return commitRead(length, new Uint8Array(data, offset, length));
  }

The problem is that I wanted to work with an ArrayBuffer, not a Uint8Array. So I used cbor.myData.buffer and got the entire CBOR ArrayBuffer back and not the section sliced out that I wanted.

I recommend using ArrayBuffer.slice to only return the section of the CBOR data that corresponds to what the programmer is expecting for that attribute.

 function readArrayBuffer(length) {
   return commitRead(length, data.slice(offset, offset + length));
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant