Skip to content

Commit

Permalink
feat!: remove .byteLength and .byteOffset properties from CID
Browse files Browse the repository at this point in the history
From #203

> I would like to also address byteOffset, byteLength fields. I don't think they were a bad idea, it's just I was not allowed to complete implementation of that idea.

Given that the feature is incomplete and untested, remove it for
now and if it's required it can be implemented fully in the future.

Fixes #208

BREAKING CHANGE: The CID class no longer has `.byteOffset` and `.byteLength` properties - these can be accessed via the `.bytes` property instead
  • Loading branch information
achingbrain committed Oct 15, 2022
1 parent 0ec751a commit 141e0f2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
10 changes: 4 additions & 6 deletions src/cid.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ export class CID {
/** @readonly */
this.bytes = bytes

// ArrayBufferView
/** @readonly */
this.byteOffset = bytes.byteOffset
/** @readonly */
this.byteLength = bytes.byteLength

// Circular reference
/** @readonly */
this.asCID = this
Expand Down Expand Up @@ -274,6 +268,10 @@ export class CID {
throw new Error('String codecs are no longer supported')
}

if (digest.bytes == null) {
throw new Error('Invalid digest')
}

switch (version) {
case 0: {
if (code !== DAG_PB_CODE) {
Expand Down
3 changes: 0 additions & 3 deletions src/link/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ export interface Link<
readonly version: V
readonly code: Format
readonly multihash: MultihashDigest<Alg>

readonly byteOffset: number
readonly byteLength: number
readonly bytes: ByteView<Link<Data, Format, Alg, V>>

equals: (other: unknown) => other is Link<Data, Format, Alg, Version>
Expand Down
28 changes: 28 additions & 0 deletions test/test-cid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,4 +708,32 @@ describe('CID', () => {
sender.close()
receiver.close()
})

describe('decode', () => {
const tests = {
v0: 'QmTFHZL5CkgNz19MdPnSuyLAi6AVq9fFp81zmPpaL2amED',
v1: 'bafybeif2pall7dybz7vecqka3zo24irdwabwdi4wc55jznaq75q7eaavvu'
}

Object.entries(tests).forEach(([version, cidString]) => {
it(`decode ${version} from bytes`, () => {
const cid1 = CID.parse(cidString)
const cid2 = CID.decode(cid1.bytes)

assert.deepStrictEqual(cid1, cid2)
})

it(`decode ${version} from subarray`, () => {
const cid1 = CID.parse(cidString)
// a byte array with an extra byte at the start and end
const bytes = new Uint8Array(cid1.bytes.length + 2)
bytes.set(cid1.bytes, 1)
// slice the cid bytes out of the middle to have a subarray with a non-zero .byteOffset
const subarray = bytes.subarray(1, cid1.bytes.length + 1)
const cid2 = CID.decode(subarray)

assert.deepStrictEqual(cid1, cid2)
})
})
})
})

0 comments on commit 141e0f2

Please sign in to comment.