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

Commit

Permalink
feat: use new IPLD API
Browse files Browse the repository at this point in the history
This is part of the Awesome Endeavour: Async Iterators:
#1670
  • Loading branch information
vmx committed Mar 27, 2019
1 parent 66d6c42 commit ca42740
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 163 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"dependencies": {
"@nodeutils/defaults-deep": "^1.1.0",
"async": "^2.6.1",
"async-iterator-to-pull-stream": "^1.1.0",
"bignumber.js": "^8.0.2",
"binary-querystring": "~0.1.2",
"bl": "^3.0.0",
Expand Down Expand Up @@ -114,7 +115,7 @@
"ipfs-unixfs": "~0.1.16",
"ipfs-unixfs-exporter": "~0.36.1",
"ipfs-unixfs-importer": "~0.38.5",
"ipld": "~0.21.1",
"ipld": "~0.22.0",
"ipld-bitcoin": "~0.1.8",
"ipld-dag-pb": "~0.15.3",
"ipld-ethereum": "^2.0.1",
Expand Down
68 changes: 55 additions & 13 deletions src/core/components/dag.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
const promisify = require('promisify-es6')
const CID = require('cids')
const pull = require('pull-stream')
const iterToPull = require('async-iterator-to-pull-stream')
const mapAsync = require('async/map')
const setImmediate = require('async/setImmediate')
const flattenDeep = require('lodash/flattenDeep')
const errCode = require('err-code')
const multicodec = require('multicodec')

module.exports = function dag (self) {
return {
Expand All @@ -25,21 +27,42 @@ module.exports = function dag (self) {
}

const optionDefaults = {
format: 'dag-cbor',
hashAlg: 'sha2-256'
format: multicodec.DAG_CBOR,
hashAlg: multicodec.SHA2_256
}

options = options.cid ? options : Object.assign({}, optionDefaults, options)
// The IPLD expects the format and hashAlg as constants
if (options.format && typeof options.format === 'string') {
const constantName = options.format.toUpperCase().replace(/-/g, '_')
options.format = multicodec[constantName]
}
if (options.hashAlg && typeof options.hashAlg === 'string') {
const constantName = options.hashAlg.toUpperCase().replace(/-/g, '_')
options.hashAlg = multicodec[constantName]
}

self._ipld.put(dagNode, options, (err, cid) => {
if (err) return callback(err)
options = options.cid ? options : Object.assign({}, optionDefaults, options)

if (options.preload !== false) {
self._preload(cid)
}
// js-ipld defaults to verion 1 CIDs. Hence set version 0 explicitly for
// dag-pb nodes
if (options.format === multicodec.DAG_PB &&
options.hashAlg === multicodec.SHA2_256 &&
options.version === undefined) {
options.version = 0
}

callback(null, cid)
})
self._ipld.put(dagNode, options.format, {
hashAlg: options.hashAlg,
cidVersion: options.version
}).then(
(cid) => {
if (options.preload !== false) {
self._preload(cid)
}
return callback(null, cid)
},
(error) => callback(error)
)
}),

get: promisify((cid, path, options, callback) => {
Expand All @@ -54,7 +77,7 @@ module.exports = function dag (self) {
// Allow options in path position
if (typeof path !== 'string') {
options = path
path = null
path = undefined
} else {
options = {}
}
Expand Down Expand Up @@ -90,7 +113,26 @@ module.exports = function dag (self) {
self._preload(cid)
}

self._ipld.get(cid, path, options, callback)
if (path === undefined || path === '/') {
self._ipld.get(cid).then(
(value) => {
callback(null, {
value,
remainderPath: ''
})
},
(error) => callback(error)
)
} else {
const result = self._ipld.resolve(cid, path)
const promisedValue = options.localResolve ?
result.first() :
result.last()
promisedValue.then(
(value) => callback(null, value),
(error) => callback(error)
)
}
}),

tree: promisify((cid, path, options, callback) => {
Expand Down Expand Up @@ -135,7 +177,7 @@ module.exports = function dag (self) {
}

pull(
self._ipld.treeStream(cid, path, options),
iterToPull(self._ipld.tree(cid, path, options)),
pull.collect(callback)
)
}),
Expand Down
10 changes: 7 additions & 3 deletions src/core/components/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
DAGNode
} = require('ipld-dag-pb')
const UnixFs = require('ipfs-unixfs')
const multicodec = require('multicodec')

const IPNS = require('../ipns')
const OfflineDatastore = require('../ipns/routing/offline-datastore')
Expand Down Expand Up @@ -132,9 +133,12 @@ module.exports = function init (self) {
(cb) => DAGNode.create(new UnixFs('directory').marshal(), cb),
(node, cb) => self.dag.put(node, {
version: 0,
format: 'dag-pb',
hashAlg: 'sha2-256'
}, cb),
format: multicodec.DAG_PB,
hashAlg: multicodec.SHA2_256
}).then(
(cid) => cb(null, cid),
(error) => cb(error)
),
(cid, cb) => self._ipns.initializeKeyspace(privateKey, cid.toBaseEncodedString(), cb)
], cb)
}
Expand Down
90 changes: 42 additions & 48 deletions src/core/components/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const DAGNode = dagPB.DAGNode
const DAGLink = dagPB.DAGLink
const CID = require('cids')
const mh = require('multihashes')
const multicodec = require('multicodec')
const Unixfs = require('ipfs-unixfs')
const errCode = require('err-code')

Expand Down Expand Up @@ -87,19 +88,19 @@ module.exports = function object (self) {
return cb(err)
}

self._ipld.put(node, {
version: 0,
hashAlg: 'sha2-256',
format: 'dag-pb'
}, (err, cid) => {
if (err) return cb(err)

if (options.preload !== false) {
self._preload(cid)
}

cb(null, cid)
})
self._ipld.put(node, multicodec.DAG_PB, {
cidVersion: 0,
hashAlg: multicodec.SHA2_256,
}).then(
(cid) => {
if (options.preload !== false) {
self._preload(cid)
}

cb(null, cid)
},
(error) => cb(error)
)
})
}
], callback)
Expand Down Expand Up @@ -137,21 +138,19 @@ module.exports = function object (self) {
return callback(err)
}

self._ipld.put(node, {
version: 0,
hashAlg: 'sha2-256',
format: 'dag-pb'
}, (err, cid) => {
if (err) {
return callback(err)
}

if (options.preload !== false) {
self._preload(cid)
}
self._ipld.put(node, multicodec.DAG_PB, {
cidVersion: 0,
hashAlg: multicodec.SHA2_256,
}).then(
(cid) => {
if (options.preload !== false) {
self._preload(cid)
}

callback(null, cid)
})
callback(null, cid)
},
(error) => callback(error)
)
})
}),
put: promisify((obj, options, callback) => {
Expand Down Expand Up @@ -200,21 +199,19 @@ module.exports = function object (self) {
}

function next () {
self._ipld.put(node, {
version: 0,
hashAlg: 'sha2-256',
format: 'dag-pb'
}, (err, cid) => {
if (err) {
return callback(err)
}

if (options.preload !== false) {
self._preload(cid)
}
self._ipld.put(node, multicodec.DAG_PB, {
cidVersion: 0,
hashAlg: multicodec.SHA2_256,
}).then(
(cid) => {
if (options.preload !== false) {
self._preload(cid)
}

callback(null, cid)
})
callback(null, cid)
},
(error) => callback(error)
)
}
}),

Expand Down Expand Up @@ -248,13 +245,10 @@ module.exports = function object (self) {
self._preload(cid)
}

self._ipld.get(cid, (err, result) => {
if (err) {
return callback(err)
}

callback(null, result.value)
})
self._ipld.get(cid).then(
(node) => callback(null, node),
(error) => callback(error)
)
}),

data: promisify((multihash, options, callback) => {
Expand Down
9 changes: 5 additions & 4 deletions src/core/components/pin-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const protobuf = require('protons')
const fnv1a = require('fnv1a')
const varint = require('varint')
const { DAGNode, DAGLink } = require('ipld-dag-pb')
const multicodec = require('multicodec')
const someSeries = require('async/someSeries')
const eachOfSeries = require('async/eachOfSeries')

Expand Down Expand Up @@ -110,8 +111,8 @@ exports = module.exports = function (dag) {

dag.put(rootNode, {
version: 0,
format: 'dag-pb',
hashAlg: 'sha2-256',
format: multicodec.DAG_PB,
hashAlg: multicodec.SHA2_256,
preload: false
}, (err, cid) => {
if (err) { return callback(err, cid) }
Expand Down Expand Up @@ -194,8 +195,8 @@ exports = module.exports = function (dag) {

const opts = {
version: 0,
hashAlg: 'sha2-256',
format: 'dag-pb',
format: multicodec.DAG_PB,
hashAlg: multicodec.SHA2_256,
preload: false
}

Expand Down
9 changes: 5 additions & 4 deletions src/core/components/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const setImmediate = require('async/setImmediate')
const { Key } = require('interface-datastore')
const errCode = require('err-code')
const multibase = require('multibase')
const multicodec = require('multicodec')

const createPinSet = require('./pin-set')
const { resolvePath } = require('../utils')
Expand Down Expand Up @@ -104,8 +105,8 @@ module.exports = (self) => {
if (err) { return cb(err) }
dag.put(empty, {
version: 0,
hashAlg: 'sha2-256',
format: 'dag-pb',
format: multicodec.DAG_PB,
hashAlg: multicodec.SHA2_256,
preload: false
}, cb)
}),
Expand All @@ -116,8 +117,8 @@ module.exports = (self) => {
root = node
dag.put(root, {
version: 0,
hashAlg: 'sha2-256',
format: 'dag-pb',
format: multicodec.DAG_PB,
hashAlg: multicodec.SHA2_256,
preload: false
}, (err, cid) => {
if (!err) {
Expand Down
Loading

0 comments on commit ca42740

Please sign in to comment.