From b4424884e89dc30b27a8466ba6cefac5e0612daa Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Thu, 2 Sep 2021 15:36:46 +0100 Subject: [PATCH 1/3] fix: convert cid before instanceof check (#9) The `CID` class can be loaded as an ESM or a CJS class which means `instanceof` breaks since they are loaded from different places. Run the passed `cid` through `CID.asCID` to convert it to whatever we think the `CID` class is before the test. --- .github/workflows/main.yml | 2 +- package.json | 5 ++-- src/index.js | 47 ++++++++++++++++++++++++++++---------- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b412471..92e8f09 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -27,7 +27,7 @@ jobs: strategy: matrix: os: [windows-latest, ubuntu-latest, macos-latest] - node: [14, 15] + node: [14, 16] fail-fast: true steps: - uses: actions/checkout@v2 diff --git a/package.json b/package.json index 212013e..cb7690f 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,9 @@ }, "homepage": "https://github.com/ipfs/js-blockstore-datastore-adapter#readme", "devDependencies": { - "aegir": "^33.1.0", - "interface-blockstore-tests": "^1.0.0" + "aegir": "^35.0.2", + "interface-blockstore-tests": "^1.0.0", + "util": "^0.12.4" }, "dependencies": { "err-code": "^3.0.1", diff --git a/src/index.js b/src/index.js index 42aca31..e90fd80 100644 --- a/src/index.js +++ b/src/index.js @@ -18,11 +18,13 @@ const { BlockstoreAdapter } = require('interface-blockstore') * @returns {Key} */ function cidToKey (cid) { - if (!(cid instanceof CID)) { + const c = CID.asCID(cid) + + if (!(c instanceof CID)) { throw errcode(new Error('Not a valid cid'), 'ERR_INVALID_CID') } - return new Key('/' + base32.encode(cid.multihash.bytes).slice(1).toUpperCase(), false) + return new Key('/' + base32.encode(c.multihash.bytes).slice(1).toUpperCase(), false) } /** @@ -50,32 +52,53 @@ function keyToCid (key) { * @returns {string} */ function convertPrefix (prefix) { - let bytes const firstChar = prefix.substring(0, 1) if (firstChar === '/') { return convertPrefix(prefix.substring(1)) } + /** @type {(input: string) => Uint8Array } */ + let decoder + if (firstChar.toLowerCase() === 'b') { // v1 cid prefix, remove version and codec bytes - bytes = base32.decode(prefix.toLowerCase()).subarray(2) + decoder = (input) => base32.decode(input.toLowerCase()).subarray(2) } else if (firstChar.toLowerCase() === 'c') { // v1 cid prefix, remove version and codec bytes - bytes = base32pad.decode(prefix.toLowerCase()).subarray(2) + decoder = (input) => base32pad.decode(input.toLowerCase()).subarray(2) } else if (firstChar === 'z') { // v1 cid - bytes = base58btc.decode(prefix).subarray(2) + decoder = (input) => base58btc.decode(input).subarray(2) } else if (firstChar === 'Q') { // v0 cid prefix - bytes = base58btc.decode('z' + prefix) + decoder = (input) => base58btc.decode('z' + input) } else { - bytes = base32.decode('b' + prefix.toLowerCase()).subarray(2) + decoder = (input) => base32.decode('b' + input.toLowerCase()).subarray(2) } - const str = base32.encode(bytes).substring(1).toUpperCase() + let bytes + + // find the longest prefix that we can safely decode + for (let i = 1; i < prefix.length; i++) { + try { + bytes = decoder(prefix.substring(0, i)) + } catch (err) { + if (err.message !== 'Unexpected end of data') { + throw err + } + } + } + + let str = '/C' + + if (bytes) { + // slice one character from the end of the string to ensure we don't end up + // with a padded value which could have a non-matching string at the end + str = `/${base32.encode(bytes).slice(1, -1).toUpperCase() || 'C'}` + } - return str || 'C' + return str } /** @@ -85,7 +108,7 @@ function convertPrefix (prefix) { function convertQuery (query) { return { ...query, - prefix: query.prefix ? `/${convertPrefix(query.prefix)}` : undefined, + prefix: query.prefix ? convertPrefix(query.prefix) : undefined, filters: query.filters ? query.filters.map( filter => (pair) => { @@ -110,7 +133,7 @@ function convertQuery (query) { function convertKeyQuery (query) { return { ...query, - prefix: query.prefix ? `/${convertPrefix(query.prefix)}` : undefined, + prefix: query.prefix ? convertPrefix(query.prefix) : undefined, filters: query.filters ? query.filters.map( filter => (key) => { From 3f8ef60253998e1e9cc8de9cd4f78a44d3bc538c Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 2 Sep 2021 15:39:17 +0100 Subject: [PATCH 2/3] chore: update contributors --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cb7690f..a293cfa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "blockstore-datastore-adapter", - "version": "1.0.0", + "version": "1.0.1", "description": "datastore interface", "leadMaintainer": "Alex Potsides ", "main": "src/index.js", From b3592d6ba5709e885eeff4a5ecd8ddc09df03d41 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 2 Sep 2021 15:39:18 +0100 Subject: [PATCH 3/3] chore: release version v1.0.1 --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b8f061..9c1382b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## [1.0.1](https://github.com/ipfs/js-blockstore-datastore-adapter/compare/v1.0.0...v1.0.1) (2021-09-02) + + +### Bug Fixes + +* convert cid before instanceof check ([#9](https://github.com/ipfs/js-blockstore-datastore-adapter/issues/9)) ([b442488](https://github.com/ipfs/js-blockstore-datastore-adapter/commit/b4424884e89dc30b27a8466ba6cefac5e0612daa)) + + + # [1.0.0](https://github.com/ipfs/js-blockstore-datastore-adapter/compare/v0.0.4...v1.0.0) (2021-07-06)