Skip to content

Commit

Permalink
feat: remove deprecated CID properties & methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Oct 12, 2022
1 parent 85a9296 commit ffc4e6f
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 153 deletions.
70 changes: 0 additions & 70 deletions src/cid.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,41 +219,6 @@ export class CID {
return `CID(${this.toString()})`
}

// Deprecated

/**
* @param {any} value
* @returns {value is CID}
*/
static isCID (value) {
deprecate(/^0\.0/, IS_CID_DEPRECATION)
return Boolean(value && (value[cidSymbol] || value.asCID === value))
}

get toBaseEncodedString () {
throw new Error('Deprecated, use .toString()')
}

get codec () {
throw new Error(
'"codec" property is deprecated, use integer "code" property instead'
)
}

get buffer () {
throw new Error(
'Deprecated .buffer property, use .bytes to get Uint8Array instead'
)
}

get multibaseName () {
throw new Error('"multibaseName" property is deprecated')
}

get prefix () {
throw new Error('"prefix" property is deprecated')
}

/**
* Takes any input `value` and returns a `CID` instance if it was
* a `CID` otherwise returns `null`. If `value` is instanceof `CID`
Expand Down Expand Up @@ -605,38 +570,3 @@ const encodeCID = (version, code, multihash) => {
const cidSymbol = Symbol.for('@ipld/js-cid/CID')
const readonly = { writable: false, configurable: false, enumerable: true }
const hidden = { writable: false, enumerable: false, configurable: false }

// ESM does not support importing package.json where this version info
// should come from. To workaround it version is copied here.
const version = '0.0.0-dev'
// Start throwing exceptions on major version bump
/**
*
* @param {RegExp} range
* @param {string} message
*/
const deprecate = (range, message) => {
/* eslint-disable no-console */
if (range.test(version)) {
console.warn(message)
/* c8 ignore next 3 */
} else {
throw new Error(message)
}
}

const IS_CID_DEPRECATION = `CID.isCID(v) is deprecated and will be removed in the next major release.
Following code pattern:
if (CID.isCID(value)) {
doSomethingWithCID(value)
}
Is replaced with:
const cid = CID.asCID(value)
if (cid) {
// Make sure to use cid instead of value
doSomethingWithCID(cid)
}
`
78 changes: 0 additions & 78 deletions test/test-cid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,6 @@ describe('CID', () => {
assert.throws(() => CID.create(0, 113, hash), msg)
})

// This was failing for quite some time, test just missed await so it went
// unnoticed. Not sure we still care about checking fourth argument.
// it('throws on trying to pass specific base encoding [deprecated]', async () => {
// const hash = await sha256.digest(textEncoder.encode('abc'))
// const msg = 'No longer supported, cannot specify base encoding in instantiation'
// assert.throws(() => CID.create(0, 112, hash, 'base32'), msg)
// })

it('throws on trying to base encode CIDv0 in other base than base58btc', async () => {
const mhStr = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
const cid = CID.parse(mhStr)
Expand Down Expand Up @@ -286,18 +278,6 @@ describe('CID', () => {
)
})

it('.isCid', () => {
assert.ok(CID.isCID(CID.parse(h1)))

assert.ok(!CID.isCID(false))

assert.ok(!CID.isCID(textEncoder.encode('hello world')))

assert.ok(CID.isCID(CID.parse(h1).toV0()))

assert.ok(CID.isCID(CID.parse(h1).toV1()))
})

it('works with deepEquals', () => {
const ch1 = CID.parse(h1)
assert.deepStrictEqual(ch1, CID.parse(h1))
Expand Down Expand Up @@ -515,12 +495,6 @@ describe('CID', () => {
assert.ok(equals(json.hash, hash.bytes))
})

it('isCID', async () => {
const hash = await sha256.digest(textEncoder.encode('abc'))
const cid = CID.create(1, 112, hash)
assert.strictEqual(OLDCID.isCID(cid), false)
})

it('asCID', async () => {
const hash = await sha256.digest(textEncoder.encode('abc'))
class IncompatibleCID {
Expand All @@ -545,7 +519,6 @@ describe('CID', () => {
const code = 112

const incompatibleCID = new IncompatibleCID(version, code, hash)
assert.ok(CID.isCID(incompatibleCID))
assert.strictEqual(incompatibleCID.toString(), '[object Object]')
// @ts-expect-error - no such method
assert.strictEqual(typeof incompatibleCID.toV0, 'undefined')
Expand Down Expand Up @@ -719,59 +692,8 @@ describe('CID', () => {
)
})

describe('deprecations', async () => {
it('codec', async () => {
const hash = await sha256.digest(textEncoder.encode('abc'))
const cid = CID.create(1, 112, hash)

assert.throws(
() => cid.codec,
'"codec" property is deprecated, use integer "code" property instead'
)
assert.throws(
// @ts-expect-error - 'string' is not assignable to parameter of type 'number'
() => CID.create(1, 'dag-pb', hash),
'String codecs are no longer supported'
)
})

it('multibaseName', async () => {
const hash = await sha256.digest(textEncoder.encode('abc'))
const cid = CID.create(1, 112, hash)
assert.throws(
() => cid.multibaseName,
'"multibaseName" property is deprecated'
)
})

it('prefix', async () => {
const hash = await sha256.digest(textEncoder.encode('abc'))
const cid = CID.create(1, 112, hash)
assert.throws(() => cid.prefix, '"prefix" property is deprecated')
})

it('toBaseEncodedString()', async () => {
const hash = await sha256.digest(textEncoder.encode('abc'))
const cid = CID.create(1, 112, hash)
assert.throws(
// @ts-expect-error - deprecated
() => cid.toBaseEncodedString(),
'Deprecated, use .toString()'
)
})
})

it('invalid CID version', async () => {
const encoded = varint.encodeTo(2, new Uint8Array(32))
assert.throws(() => CID.decode(encoded), 'Invalid CID version 2')
})

it('buffer', async () => {
const hash = await sha256.digest(textEncoder.encode('abc'))
const cid = CID.create(1, 112, hash)
assert.throws(
() => cid.buffer,
'Deprecated .buffer property, use .bytes to get Uint8Array instead'
)
})
})
5 changes: 0 additions & 5 deletions test/test-link.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* globals describe, it */

import * as Link from '../src/link.js'
import { CID } from '../src/cid.js'
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import { sha256 } from '../src/hashes/sha2.js'
Expand All @@ -24,10 +23,6 @@ describe('Link', () => {
it('isLink', () => {
assert.equal(Link.isLink(0), false)
assert.equal(Link.isLink(false), false)
assert.equal(CID.isCID(CID.parse(h1)), true)
assert.equal(CID.isCID(CID.parse(h1).toV0()), true)

assert.equal(CID.isCID(CID.parse(h1).toV1()), true)
})

describe('create', () => {
Expand Down

0 comments on commit ffc4e6f

Please sign in to comment.