Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: object API internals updated to use CID
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Oct 29, 2016
1 parent 3674b8e commit 5cb10cc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
39 changes: 25 additions & 14 deletions src/core/components/object.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict'

const mDAG = require('ipfs-merkle-dag')
const waterfall = require('async/waterfall')
const promisify = require('promisify-es6')
const bs58 = require('bs58')
const DAGNode = mDAG.DAGNode
const DAGLink = mDAG.DAGLink
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const DAGLink = dagPB.DAGLink
const CID = require('cids')

function normalizeMultihash (multihash, enc) {
if (typeof multihash === 'string') {
Expand Down Expand Up @@ -49,9 +50,7 @@ function parseJSONBuffer (buf) {
}

function parseProtoBuffer (buf) {
const node = new DAGNode()
node.unMarshal(buf)
return node
return dagPB.util.deserialize(buf)
}

module.exports = function object (self) {
Expand All @@ -63,9 +62,16 @@ module.exports = function object (self) {
}

waterfall([
(cb) => self.object.get(multihash, options, cb),
(cb) => {
self.object.get(multihash, options, cb)
},
(node, cb) => {
self._dagService.put(edit(node), (err) => {
node = edit(node)

self._ipldResolver.put({
node: node,
cid: new CID(node.multihash())
}, (err) => {
cb(err, node)
})
}
Expand All @@ -77,15 +83,17 @@ module.exports = function object (self) {
new: promisify((cb) => {
const node = new DAGNode()

self._dagService.put(node, function (err) {
self._ipldResolver.put({
node: node,
cid: new CID(node.multihash())
}, function (err) {
if (err) {
return cb(err)
}

cb(null, node)
})
}),

put: promisify((obj, options, cb) => {
if (typeof options === 'function') {
cb = options
Expand Down Expand Up @@ -114,7 +122,10 @@ module.exports = function object (self) {
return cb(new Error('obj not recognized'))
}

self._dagService.put(node, (err, block) => {
self._ipldResolver.put({
node: node,
cid: new CID(node.multihash())
}, (err, block) => {
if (err) {
return cb(err)
}
Expand All @@ -136,8 +147,8 @@ module.exports = function object (self) {
} catch (err) {
return cb(err)
}

self._dagService.get(mh, cb)
const cid = new CID(mh)
self._ipldResolver.get(cid, cb)
}),

data: promisify((multihash, options, cb) => {
Expand Down Expand Up @@ -180,7 +191,7 @@ module.exports = function object (self) {
return cb(err)
}

const blockSize = node.marshal().length
const blockSize = dagPB.util.serialize(node).length
const linkLength = node.links.reduce((a, l) => a + l.size, 0)

cb(null, {
Expand Down
5 changes: 2 additions & 3 deletions src/core/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

const BlockService = require('ipfs-block-service')
const mDAG = require('ipfs-merkle-dag')
const DAGService = mDAG.DAGService
const IPLDResolver = require('ipld-resolver')
const PeerBook = require('peer-book')

const defaultRepo = require('./default-repo')
Expand Down Expand Up @@ -44,7 +43,7 @@ function IPFS (repoInstance) {
this._libp2pNode = null
this._bitswap = null
this._blockService = new BlockService(this._repo)
this._dagService = new DAGService(this._blockService)
this._ipldResolver = new IPLDResolver(this._blockService)

// IPFS Core exposed components

Expand Down
1 change: 0 additions & 1 deletion test/core/both/test-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ const common = {
}
}

console.log('->')
test.block(common)

0 comments on commit 5cb10cc

Please sign in to comment.