Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
feat/dag: rebase, use waterfall for put
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Aug 13, 2017
1 parent 9a1a399 commit 8e6d61b
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 127 deletions.
127 changes: 0 additions & 127 deletions src/dag/dag.js

This file was deleted.

61 changes: 61 additions & 0 deletions src/dag/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict'

const dagPB = require('ipld-dag-pb')
const dagCBOR = require('ipld-dag-cbor')
const promisify = require('promisify-es6')
const CID = require('cids')
const waterfall = require('async/waterfall')
const block = require('../block')

module.exports = (send) => {
return promisify((cid, path, options, callback) => {
if (typeof path === 'function') {
callback = path
path = undefined
}

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

options = options || {}

if (CID.isCID(cid)) {
cid = cid.toBaseEncodedString()
}

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

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

waterfall([
cb => {
send({
path: 'dag/resolve',
args: cid + '/' + path,
qs: options
}, cb)
},
(resolved, cb) => {
block(send).get(new CID(resolved['Cid']['/']), (err, blk) => cb(err, blk, resolved['RemPath']))
},
(blk, path, cb) => {
if (blk.cid.codec === 'dag-cbor') {
dagCBOR.resolver.resolve(blk, path, cb)
}
if (blk.cid.codec === 'dag-pb') {
dagPB.resolver.resolve(blk, path, cb)
}
}
], callback)
})
}
12 changes: 12 additions & 0 deletions src/dag/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const moduleConfig = require('../utils/module-config')

module.exports = (arg) => {
const send = moduleConfig(arg)

return {
get: require('./get')(send),
put: require('./put')(send)
}
}
68 changes: 68 additions & 0 deletions src/dag/put.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict'

const dagPB = require('ipld-dag-pb')
const dagCBOR = require('ipld-dag-cbor')
const promisify = require('promisify-es6')
const CID = require('cids')
const multihash = require('multihashes')

function noop () {}

module.exports = (send) => {
return promisify((dagNode, options, callback) => {
if (typeof options === 'function') {
return setImmediate(() => callback(new Error('no options were passed')))
}

callback = callback || noop

let hashAlg = options.hash || 'sha2-256'
let format
let inputEnc

if (options.cid && CID.isCID(options.cid)) {
format = options.cid.codec
hashAlg = multihash.decode(options.cid.multihash).name
prepare()
} else if (options.format) {
format = options.format
prepare()
} else {
callback(new Error('Invalid arguments'))
}

function prepare () {
inputEnc = 'raw'

if (format === 'dag-cbor') {
dagCBOR.util.serialize(dagNode, finalize)
}
if (format === 'dag-pb') {
dagPB.util.serialize(dagNode, finalize)
}
}

function finalize (err, serialized) {
if (err) { return callback(err) }

send({
path: 'dag/put',
qs: {
hash: hashAlg,
format: format,
'input-enc': inputEnc
},
files: serialized
}, (err, result) => {
if (err) {
return callback(err)
}
if (result.Cid) {
return callback(null, new CID(result.Cid['/']))
} else {
return callback(result)
}
})
}
})
}
1 change: 1 addition & 0 deletions src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function requireCommands () {
bootstrap: require('../bootstrap'),
commands: require('../commands'),
config: require('../config'),
dag: require('../dag'),
dht: require('../dht'),
diag: require('../diag'),
id: require('../id'),
Expand Down

0 comments on commit 8e6d61b

Please sign in to comment.