Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade repo to new multiformats module #313

Merged
merged 25 commits into from
Jul 9, 2021

Conversation

achingbrain
Copy link
Member

@achingbrain achingbrain commented May 14, 2021

Replaces cids, multibase, multihashes etc with new multiformats module.

Depends on:

BREAKING CHANGE: The blockstore now takes instances of the new CID class and returns Uint8Arrays

Replaces `cids`, `multibase`, `multihashes` etc with new `multiformats`
module.

BREAKING CHANGE: The blockstore now takes instances of the new CID class
@achingbrain achingbrain requested review from rvagg and vasco-santos May 14, 2021 15:27
@@ -1,18 +1,17 @@
'use strict'

const Block = require('ipld-block')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a Block in multiformats, it's not quite as featureful as ipld-block IIRC or what we tried with https://github.com/ipld/js-block, but it's a really nice container cid:bytes:value that can be quite useful with some extra sugar that might be close to what the user previously expected. We could return that to the user and they get to consume it as they want.

See https://github.com/multiformats/js-multiformats/blob/master/src/block.js (I don't think it got documented yet because we were working on getting it right and figuring out the tradeoffs between that and what we might do in js-block if we did that above it, but it should be good to use as is).

b = new Block(bData, new CID(hash))
const identityHash = await multihashing(identityData, 'identity')
identityCID = new CID(1, 'dag-cbor', identityHash)
const digest1 = await sha256.digest(bData)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use require('multiformats/block').Block here, e.g.:

const block = await Block.encode({ value: bData, codec: identity, hasher: sha256 })
// then you have:
// block.cid
// block.bytes
// block.value

const welcomeHash = mh.fromHexString(
'1220120f6af601d46e10b2d2e11ed71c55d25f3042c22501e41d1246e7a1e9d3d8ec'
const welcomeHash = CID.decode(
uint8ArrayFromString('1220120f6af601d46e10b2d2e11ed71c55d25f3042c22501e41d1246e7a1e9d3d8ec', 'base16')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there might be potential to trim dependencies a bit more, multiformats exports a bytes that has some basic utilities including fromHex() and toHex() (just available on the root export though):

const { CID, bytes } = require('multiformats')
// ...

CID.decode(bytes.fromHex('1220120f...'))

const val = await repo.blocks.get(new CID(welcomeHash))
expect(uint8ArrayToString(val.data)).to.match(/Hello and Welcome to IPFS/)
const val = await repo.blocks.get(welcomeHash)
expect(uint8ArrayToString(val)).to.match(/Hello and Welcome to IPFS/)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bytes also has a fromString() and toString() that does utf-8 with TextEncoder and TextDecoder similar to this btw

@@ -151,8 +172,14 @@ function createBaseStore (store) {
return out
},

close () {
return store.close()
batch () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we support batch? If not, perhaps throw an error instead?

@rvagg
Copy link
Member

rvagg commented May 17, 2021

Cool, nice bundle min reduction too

As mentioned in the inline comments, it might be worth exploring require('multiformats/block').Block as a replacement for ipld-block. It's lightweight and can be just used for cid, bytes and value properties but has some extras that might make it a decent replacement.

/cc @Gozala who might like to see the code being consumed here and also have thoughts on usage of Block

@rvagg
Copy link
Member

rvagg commented May 17, 2021

followed @vasco-santos's lead with suggestions for easy merge if that helps (not sure it does) for the easy ones

@achingbrain
Copy link
Member Author

it might be worth exploring require('multiformats/block').Block

I've got my eye on it but so far it hasn't been necessary - CIDs in, Uint8Arrays out seems to cover all of our use cases.

@Gozala
Copy link

Gozala commented Jun 29, 2021

/cc @Gozala who might like to see the code being consumed here and also have thoughts on usage of Block

I always felt that custom Blockstore API that was slightly incompatible with Datastore interface was awkward & I am glad to see it go. I think been at layer below Block here is a right decision.

@achingbrain achingbrain merged commit 4144a93 into master Jul 9, 2021
@achingbrain achingbrain deleted the feat/update-to-new-multiformats branch July 9, 2021 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants