multiformats / multiformats/js-multiformats

Make block size limit part of the BlockEncoder API

Open
#223 5 comments 0 reactions 1 assignee View on GitHub

@Gozala is already working on this.

Since Dec 6, 2022.

Dominant language
TypeScript
Stars
268
Forks
57
PR merge metrics
No merged PRs in 30d

Description

Currently BlockEncoder interface just encodes passed input into bytes

https://github.com/multiformats/js-multiformats/blob/58117f2c1a90934903651b456ec3a09552d942e7/src/codecs/interface.ts#L6-L10

Problem is:

  1. If you go beyond block size limit, your block won't be bit-swappable.
  2. You may not even know you've gone past block size limit.
  3. Most people is not even aware of the block size limit.

Proposed solution:

I would like to propose amending our BlockEncoder interface as follows:

export interface BlockEncoder<Code extends number, T> {
  name: string
  code: Code
  /**
   * Encodes given data. If `buffer` is provided data is written into it and
   * a `Uint8Array` view of the written bytes is returned.  If `buffer` is not
   * passed new buffer is allocated with a `byteLength` corresponding to
   * default block size limit in IPFS. If encoded data does not fit the `buffer`
   * `RangeError` exception is thrown.
   */
  encode: (data: T, buffer?: Uint8Array) => ByteView<T>
}

Idea here is that:

  1. User should be able to optionally pass in the buffer to write data into.
  2. If data does not fit the block size limit encode fails.
  3. User is still able to encode blocks larger than a block size limit by passing in larger buffer.

This will be non-breaking change at the API level, but it would be breaking in the sense that errors will occur if block is larger than a block size limit. Never the less it seems like a better default than silently letting things slip.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.