From ffc4e6fac0b9f755a141f5e7fea61950d195b4fa Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Tue, 20 Sep 2022 18:19:47 +1000 Subject: [PATCH] feat: remove deprecated CID properties & methods --- src/cid.js | 70 ------------------------------------- test/test-cid.spec.js | 78 ------------------------------------------ test/test-link.spec.js | 5 --- 3 files changed, 153 deletions(-) diff --git a/src/cid.js b/src/cid.js index be2b971c..cbb5fd80 100644 --- a/src/cid.js +++ b/src/cid.js @@ -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` @@ -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) -} -` diff --git a/test/test-cid.spec.js b/test/test-cid.spec.js index d819618f..5bdd3b1d 100644 --- a/test/test-cid.spec.js +++ b/test/test-cid.spec.js @@ -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) @@ -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)) @@ -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 { @@ -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') @@ -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' - ) - }) }) diff --git a/test/test-link.spec.js b/test/test-link.spec.js index 7dba3798..67f320fa 100644 --- a/test/test-link.spec.js +++ b/test/test-link.spec.js @@ -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' @@ -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', () => {