-
Notifications
You must be signed in to change notification settings - Fork 51
feat: make blockstore identity-hash compatible #297
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6a105c7
feat(blockstore): make identity-hash compatible
hannahhoward d2d3f3f
fix(deps): add missing multihashes dep
hannahhoward d0ad9a4
fix(idstore): response to PR comments
hannahhoward dd812a6
fix(blockstore): use simpler block interface in type
hannahhoward File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
'use strict' | ||
|
||
const Block = require('ipld-block') | ||
const filter = require('it-filter') | ||
const mh = require('multihashes') | ||
const pushable = require('it-pushable') | ||
const drain = require('it-drain') | ||
const CID = require('cids') | ||
const errcode = require('err-code') | ||
|
||
/** | ||
* @typedef {import("interface-datastore").Query} Query | ||
* @typedef {import("interface-datastore").Datastore} Datastore | ||
* @typedef {import("interface-datastore").Options} DatastoreOptions | ||
* @typedef {import('./types').Blockstore} Blockstore | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {Blockstore} blockstore | ||
*/ | ||
module.exports = (blockstore) => { | ||
return createIdStore(blockstore) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just export the |
||
} | ||
|
||
/** | ||
* @param {Blockstore} store | ||
* @returns {Blockstore} | ||
*/ | ||
function createIdStore (store) { | ||
return { | ||
open () { | ||
return store.open() | ||
}, | ||
query (query, options) { | ||
return store.query(query, options) | ||
}, | ||
|
||
async get (cid, options) { | ||
const extracted = extractContents(cid) | ||
if (extracted.isIdentity) { | ||
return Promise.resolve(new Block(extracted.digest, cid)) | ||
} | ||
return store.get(cid, options) | ||
}, | ||
|
||
async * getMany (cids, options) { | ||
for await (const cid of cids) { | ||
yield this.get(cid, options) | ||
} | ||
}, | ||
|
||
async put (block, options) { | ||
const { isIdentity } = extractContents(block.cid) | ||
if (isIdentity) { | ||
return Promise.resolve(block) | ||
} | ||
return store.put(block, options) | ||
}, | ||
|
||
async * putMany (blocks, options) { | ||
// in order to return all blocks. we're going to assemble a seperate iterable | ||
// return rather than return the resolves of store.putMany using the same | ||
// process used by blockstore.putMany | ||
const output = pushable() | ||
|
||
// process.nextTick runs on the microtask queue, setImmediate runs on the next | ||
// event loop iteration so is slower. Use process.nextTick if it is available. | ||
const runner = process && process.nextTick ? process.nextTick : setImmediate | ||
|
||
runner(async () => { | ||
try { | ||
await drain(store.putMany(async function * () { | ||
for await (const block of blocks) { | ||
if (!extractContents(block.cid).isIdentity) { | ||
yield block | ||
} | ||
// if non identity blocks successfully write, blocks are included in output | ||
output.push(block) | ||
} | ||
}())) | ||
|
||
output.end() | ||
} catch (err) { | ||
output.end(err) | ||
} | ||
}) | ||
|
||
yield * output | ||
}, | ||
|
||
has (cid, options) { | ||
const { isIdentity } = extractContents(cid) | ||
if (isIdentity) { | ||
return Promise.resolve(true) | ||
} | ||
return store.has(cid, options) | ||
}, | ||
|
||
delete (cid, options) { | ||
const { isIdentity } = extractContents(cid) | ||
if (isIdentity) { | ||
return Promise.resolve() | ||
} | ||
return store.delete(cid, options) | ||
}, | ||
|
||
deleteMany (cids, options) { | ||
return store.deleteMany(filter(cids, (cid) => !extractContents(cid).isIdentity), options) | ||
}, | ||
|
||
close () { | ||
return store.close() | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param {CID} k | ||
* @returns {{ isIdentity: false } | { isIdentity: true, digest: Uint8Array}} | ||
*/ | ||
function extractContents (k) { | ||
if (!CID.isCID(k)) { | ||
throw errcode(new Error('Not a valid cid'), 'ERR_INVALID_CID') | ||
} | ||
|
||
// Pre-check by calling Prefix(), this much faster than extracting the hash. | ||
const decoded = mh.decode(k.multihash) | ||
|
||
if (decoded.name !== 'identity') { | ||
return { | ||
isIdentity: false | ||
} | ||
} | ||
|
||
return { | ||
isIdentity: true, | ||
digest: decoded.digest | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What was the error that meant this needed to change from
^
to~
?