Skip to content
This repository has been archived by the owner on Sep 30, 2023. It is now read-only.

Commit

Permalink
Improve backwards compatibility in regards to previous commit
Browse files Browse the repository at this point in the history
- Restore fromEntryHash
- Make fromMultihash/toMultihash simply call fromCID/toCID
  • Loading branch information
satazor committed Jan 10, 2019
1 parent 040ff46 commit f7c35ba
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 136 deletions.
10 changes: 8 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"mocha-headless-chrome": "~2.0.1",
"orbit-db-keystore": "github:orbitdb/orbit-db-keystore",
"rimraf": "~2.6.1",
"spy": "~1.0.0",
"standard": "~12.0.1",
"webpack": "~4.28.0",
"webpack-cli": "~3.1.2"
Expand Down
60 changes: 34 additions & 26 deletions src/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { isDefined, dagNode } = require('./utils')
const IPLD_LINKS = ['next']
const IpfsNotDefinedError = () => new Error('Ipfs instance not defined')
const getCidProp = (entry) => entry.v === 0 ? 'hash' : 'cid'
const getCodec = (entry) => entry.v === 0 ? 'dag-pb' : 'dag-cbor'

class Entry {
/**
Expand Down Expand Up @@ -48,7 +49,8 @@ class Entry {
entry.identity = identity.toJSON()
entry.sig = signature
entry.cid = await Entry.toCID(ipfs, entry)
return entry

return Entry.ensureInterop(entry)
}

/**
Expand Down Expand Up @@ -101,7 +103,7 @@ class Entry {

// Ensure `entry` follows the correct format
const e = {
cid: null,
[getCidProp(entry)]: null,
id: entry.id,
payload: entry.payload,
next: entry.next,
Expand All @@ -113,7 +115,7 @@ class Entry {
if (entry.identity) Object.assign(e, { identity: entry.identity })
if (entry.sig) Object.assign(e, { sig: entry.sig })

return dagNode.write(ipfs, 'dag-cbor', e, IPLD_LINKS)
return dagNode.write(ipfs, getCodec(entry), e, IPLD_LINKS)
}

/**
Expand All @@ -128,24 +130,7 @@ class Entry {
* @deprecated
*/
static async toMultihash (ipfs, entry) {
if (!ipfs) throw IpfsNotDefinedError()
if (!Entry.isEntry(entry)) throw new Error('Invalid object format, cannot generate entry multihash')

// Ensure `entry` follows the correct format
const e = {
hash: null,
id: entry.id,
payload: entry.payload,
next: entry.next,
v: 0,
clock: entry.clock
}

if (entry.key) Object.assign(e, { key: entry.key })
if (entry.identity) Object.assign(e, { identity: entry.identity })
if (entry.sig) Object.assign(e, { sig: entry.sig })

return dagNode.write(ipfs, 'dag-pb', e, IPLD_LINKS)
return Entry.toCID(ipfs, entry)
}

/**
Expand All @@ -165,7 +150,7 @@ class Entry {
const e = await dagNode.read(ipfs, cid, IPLD_LINKS)

let entry = {
cid,
[getCidProp(e)]: cid,
id: e.id,
payload: e.payload,
next: e.next,
Expand All @@ -177,7 +162,7 @@ class Entry {
if (e.identity) Object.assign(entry, { identity: e.identity })
if (e.sig) Object.assign(entry, { sig: e.sig })

return entry
return Entry.ensureInterop(entry)
}

/**
Expand All @@ -192,9 +177,6 @@ class Entry {
* @deprecated
*/
static async fromMultihash (ipfs, multihash) {
if (!ipfs) throw IpfsNotDefinedError()
if (!multihash) throw new Error(`Invalid multihash: ${multihash}`)

return Entry.fromCID(ipfs, multihash)
}

Expand All @@ -212,6 +194,32 @@ class Entry {
obj.clock !== undefined
}

/**
* Ensures that this entry is interoperable between earlier versions
* and the most recent one (and vice-versa).
* @param {Entry} entry The entry to ensure interoperability
* @return {Entry} entry The same entry but with backwards and forward interoperability
*/
static ensureInterop (entry) {
if (entry.cid && entry.hash) {
return entry
}

const prop = getCidProp(entry)
const accessorProp = prop === 'hash' ? 'cid' : 'hash'

Object.defineProperty(entry, accessorProp, {
get () {
return this[prop]
},
set (value) {
this[prop] = value
}
})

return entry
}

/**
* Compares two entries.
* @param {Entry} a
Expand Down
20 changes: 11 additions & 9 deletions src/log-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ class LogIO {
}

/**
* Create a log from a multihash.
* @param {IPFS} ipfs An IPFS instance
* @param {string} multihash Multihash (as a Base58 encoded string) to create the Log from
* @param {number} [length=-1] How many items to include in the log
* @param {Array<Entry>} [exclude] Entries to not fetch (cached)
* @param {function(cid, entry, parent, depth)} onProgressCallback
* @returns {Promise<Log>}
* @deprecated
*/
* Create a log from a multihash.
* @param {IPFS} ipfs An IPFS instance
* @param {string} multihash Multihash (as a Base58 encoded string) to create the Log from
* @param {number} [length=-1] How many items to include in the log
* @param {Array<Entry>} [exclude] Entries to not fetch (cached)
* @param {function(cid, entry, parent, depth)} onProgressCallback
* @returns {Promise<Log>}
* @deprecated
*/
static async fromMultihash (ipfs, multihash, length = -1, exclude, onProgressCallback) {
if (!isDefined(ipfs)) throw LogError.IPFSNotDefinedError()
if (!isDefined(multihash)) throw new Error(`Invalid multihash: ${multihash}`)
Expand Down Expand Up @@ -113,6 +113,7 @@ class LogIO {

static async fromJSON (ipfs, json, length = -1, timeout, onProgressCallback) {
if (!isDefined(ipfs)) throw LogError.IPFSNotDefinedError()
json.heads.forEach(Entry.ensureInterop)
const headCids = json.heads.map(e => e.cid)
const entries = await EntryIO.fetchParallel(ipfs, headCids, length, [], 16, timeout, onProgressCallback)
const finalEntries = entries.slice().sort(Entry.compare)
Expand Down Expand Up @@ -144,6 +145,7 @@ class LogIO {
if (!Array.isArray(sourceEntries)) {
sourceEntries = [sourceEntries]
}
sourceEntries.forEach(Entry.ensureInterop)

// Fetch given length, return size at least the given input entries
length = length > -1 ? Math.max(length, sourceEntries.length) : length
Expand Down
18 changes: 17 additions & 1 deletion src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,30 @@ class Log extends GSet {
* @param {string} [logId] The ID of the log
* @param {number} [length=-1] How many entries to include in the log
* @param {function(cid, entry, parent, depth)} onProgressCallback
* @return {Promise<Log>} New Log
* @return {Promise<Log>} New Log
*/
static async fromEntryCid (ipfs, access, identity, cid, logId, length = -1, exclude, onProgressCallback) {
// TODO: need to verify the entries with 'key'
const data = await LogIO.fromEntryCid(ipfs, cid, length, exclude, onProgressCallback)
return new Log(ipfs, access, identity, logId, data.values)
}

/**
* Create a log from a single entry's multihash.
* @param {IPFS} ipfs An IPFS instance
* @param {AccessController} access The access controller instance
* @param {Identity} identity The identity instance
* @param {string} multihash The entry's multihash
* @param {string} [logId] The ID of the log
* @param {number} [length=-1] How many entries to include in the log
* @param {function(cid, entry, parent, depth)} onProgressCallback
* @return {Promise<Log>} New Log
* @deprecated
*/
static async fromEntryHash (ipfs, access, identity, multihash, logId, length = -1, exclude, onProgressCallback) {
return Log.fromEntryCid(ipfs, access, identity, multihash, logId, length, exclude, onProgressCallback)
}

/**
* Create a log from a Log Snapshot JSON.
* @param {IPFS} ipfs An IPFS instance
Expand Down
Loading

0 comments on commit f7c35ba

Please sign in to comment.