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

More dag.api goodness #783

Merged
merged 9 commits into from
Mar 13, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/core/components/dag.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,77 @@
'use strict'

const promisify = require('promisify-es6')
const CID = require('cids')
const pull = require('pull-stream')

module.exports = function dag (self) {
return {
put: promisify((dagNode, options, callback) => {
self._ipldResolver.put(dagNode, options, callback)
}),

get: promisify((cid, path, options, callback) => {
if (typeof path === 'function') {
callback = path
path = undefined
}

if (typeof options === 'function') {
callback = options
options = {}
}

options = options || {}

if (typeof cid === 'string') {
const split = cid.split('/')
cid = new CID(split[0])
split.shift()

if (split.length > 0) {
path = split.join('/')
} else {
path = '/'
}
}

self._ipldResolver.get(cid, path, options, callback)
}),

tree: promisify((cid, path, options, callback) => {
if (typeof path === 'function') {
callback = path
path = undefined
}

if (typeof options === 'function') {
callback = options
options = {}
}

options = options || {}

if (typeof cid === 'string') {
const split = cid.split('/')
cid = new CID(split[0])
split.shift()

if (split.length > 0) {
path = split.join('/')
} else {
path = '/'
}
}

pull(
self._ipldResolver.treeStream(cid, path, options),
pull.collect((err, results) => {
if (err) {
return callback(err)
}
callback(null, results)
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can just run pull.collect(callback) instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! (you know when you open a callback for testing? :P)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never :P

)
})
}
}