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

Array buffer view #29

Merged
merged 4 commits into from
Aug 4, 2020
Merged

Array buffer view #29

merged 4 commits into from
Aug 4, 2020

Conversation

Gozala
Copy link
Contributor

@Gozala Gozala commented Aug 3, 2020

This pull request changes CID representation to a glorified ArrayBuffer view (as per multiformats/js-cid#111 (comment) and discussions in other higher bandwidth channels). Primary goal here is to illustrate and discuss the concept, which is why it end up mixing bunch of things (we can spin smaller pull request from this as necessary).

Overview

  • Mail goal is to make CID just be a glorified view over the binary CID (just like all the other typed arrays). That is to say it complies with ArrayBufferView interface as defined below:

     interface ArrayBufferView {
      byteOffset: number,
      byteLength: number,
      buffer: ArrayBuffer
    }
    • This also implies that CID can represent view into the larger buffer. This provides nice benefits when using binary protocol to move data across threads or processes.
  • This is not strictly necessary, but it simplifies CID constructor so it's in the spirit of other ArrayBufferViews.

    • It introduces CID.from(v) (again as other views) to make CID from string and all other supported inputs.
    • It introduces CID.create(version, code, bytes) for creating CID out of given parameters.
    • It makes constructor of following type
      interface CID {
         constructor(buffer:ArrayBuffer, byteOffset=0, byteLength:buffer.byteLength)
         constructor(ArrayBufferView)
      }
  • Renames previously existing buffer field to bytes, because buffer needs to be an ArrayBuffer. It is also aligned with recent change in old CIDs as per fix: replace node buffers with uint8arrays js-cid#117

@mikeal
Copy link
Contributor

mikeal commented Aug 4, 2020

The constructor changes are really nice. It’s more code but it’s a lot clearer what is going on, so +1 on all of that.

I still don’t like JSDoc but I’m willing to accept it here since you took the time to add them and other comments, so +1 there.

By “glorified ArrayBufferView” you just mean that it exposes the same properties and methods right? I’m not seeing anything in here that would imply it’s subclassing an ArrayBufferView or anything like that.

@Gozala
Copy link
Contributor Author

Gozala commented Aug 4, 2020

By “glorified ArrayBufferView” you just mean that it exposes the same properties and methods right? I’m not seeing anything in here that would imply it’s subclassing an ArrayBufferView or anything like that.

ArrayBuffer is defined in specs as an interface that typed arrays implement not as something they subclass. So I mean two things:

  1. Implementation complies with ArrayBufferInterface (defined in TS syntax in the description).
  2. Under the hood CID class is just a view of the range in the ArrayBuffer.

Second point is bit subtle. Currently CID is more of thing that you create rather than a view into a larger buffer (although technically it is still possible by creating UInt8Array view and then CID with it).

@Gozala
Copy link
Contributor Author

Gozala commented Aug 4, 2020

BTW I have no idea why polendina is timing out here. Locally it seems to do just fine.

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

Successfully merging this pull request may close these issues.

2 participants