-
Notifications
You must be signed in to change notification settings - Fork 50
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
Conversation
Replaces `cids`, `multibase`, `multihashes` etc with new `multiformats` module. BREAKING CHANGE: The blockstore now takes instances of the new CID class
@@ -1,18 +1,17 @@ | |||
'use strict' | |||
|
|||
const Block = require('ipld-block') |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
test/interop-test.js
Outdated
const welcomeHash = mh.fromHexString( | ||
'1220120f6af601d46e10b2d2e11ed71c55d25f3042c22501e41d1246e7a1e9d3d8ec' | ||
const welcomeHash = CID.decode( | ||
uint8ArrayFromString('1220120f6af601d46e10b2d2e11ed71c55d25f3042c22501e41d1246e7a1e9d3d8ec', 'base16') |
There was a problem hiding this comment.
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...'))
test/interop-test.js
Outdated
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/) |
There was a problem hiding this comment.
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
src/blockstore.js
Outdated
@@ -151,8 +172,14 @@ function createBaseStore (store) { | |||
return out | |||
}, | |||
|
|||
close () { | |||
return store.close() | |||
batch () { |
There was a problem hiding this comment.
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?
Cool, nice bundle min reduction too As mentioned in the inline comments, it might be worth exploring /cc @Gozala who might like to see the code being consumed here and also have thoughts on usage of |
followed @vasco-santos's lead with suggestions for easy merge if that helps (not sure it does) for the easy ones |
Co-authored-by: Vasco Santos <vasco.santos@moxy.studio> Co-authored-by: Rod Vagg <rod@vagg.org>
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. |
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 |
Replaces
cids
,multibase
,multihashes
etc with newmultiformats
module.Depends on:
BREAKING CHANGE: The blockstore now takes instances of the new
CID
class and returnsUint8Arrays