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

Commit ad9eab8

Browse files
magik6kdaviddias
authored andcommitted
feat(dag): rebase, use waterfall for put
1 parent 7ba0343 commit ad9eab8

File tree

5 files changed

+142
-127
lines changed

5 files changed

+142
-127
lines changed

src/dag/dag.js

-127
This file was deleted.

src/dag/get.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict'
2+
3+
const dagPB = require('ipld-dag-pb')
4+
const dagCBOR = require('ipld-dag-cbor')
5+
const promisify = require('promisify-es6')
6+
const CID = require('cids')
7+
const waterfall = require('async/waterfall')
8+
const block = require('../block')
9+
10+
module.exports = (send) => {
11+
return promisify((cid, path, options, callback) => {
12+
if (typeof path === 'function') {
13+
callback = path
14+
path = undefined
15+
}
16+
17+
if (typeof options === 'function') {
18+
callback = options
19+
options = {}
20+
}
21+
22+
options = options || {}
23+
24+
if (CID.isCID(cid)) {
25+
cid = cid.toBaseEncodedString()
26+
}
27+
28+
if (typeof cid === 'string') {
29+
const split = cid.split('/')
30+
cid = split[0]
31+
split.shift()
32+
33+
if (split.length > 0) {
34+
path = split.join('/')
35+
} else {
36+
path = '/'
37+
}
38+
}
39+
40+
waterfall([
41+
cb => {
42+
send({
43+
path: 'dag/resolve',
44+
args: cid + '/' + path,
45+
qs: options
46+
}, cb)
47+
},
48+
(resolved, cb) => {
49+
block(send).get(new CID(resolved['Cid']['/']), (err, blk) => cb(err, blk, resolved['RemPath']))
50+
},
51+
(blk, path, cb) => {
52+
if (blk.cid.codec === 'dag-cbor') {
53+
dagCBOR.resolver.resolve(blk, path, cb)
54+
}
55+
if (blk.cid.codec === 'dag-pb') {
56+
dagPB.resolver.resolve(blk, path, cb)
57+
}
58+
}
59+
], callback)
60+
})
61+
}

src/dag/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
const moduleConfig = require('../utils/module-config')
4+
5+
module.exports = (arg) => {
6+
const send = moduleConfig(arg)
7+
8+
return {
9+
get: require('./get')(send),
10+
put: require('./put')(send)
11+
}
12+
}

src/dag/put.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict'
2+
3+
const dagPB = require('ipld-dag-pb')
4+
const dagCBOR = require('ipld-dag-cbor')
5+
const promisify = require('promisify-es6')
6+
const CID = require('cids')
7+
const multihash = require('multihashes')
8+
9+
function noop () {}
10+
11+
module.exports = (send) => {
12+
return promisify((dagNode, options, callback) => {
13+
if (typeof options === 'function') {
14+
return setImmediate(() => callback(new Error('no options were passed')))
15+
}
16+
17+
callback = callback || noop
18+
19+
let hashAlg = options.hash || 'sha2-256'
20+
let format
21+
let inputEnc
22+
23+
if (options.cid && CID.isCID(options.cid)) {
24+
format = options.cid.codec
25+
hashAlg = multihash.decode(options.cid.multihash).name
26+
prepare()
27+
} else if (options.format) {
28+
format = options.format
29+
prepare()
30+
} else {
31+
callback(new Error('Invalid arguments'))
32+
}
33+
34+
function prepare () {
35+
inputEnc = 'raw'
36+
37+
if (format === 'dag-cbor') {
38+
dagCBOR.util.serialize(dagNode, finalize)
39+
}
40+
if (format === 'dag-pb') {
41+
dagPB.util.serialize(dagNode, finalize)
42+
}
43+
}
44+
45+
function finalize (err, serialized) {
46+
if (err) { return callback(err) }
47+
48+
send({
49+
path: 'dag/put',
50+
qs: {
51+
hash: hashAlg,
52+
format: format,
53+
'input-enc': inputEnc
54+
},
55+
files: serialized
56+
}, (err, result) => {
57+
if (err) {
58+
return callback(err)
59+
}
60+
if (result['Cid']) {
61+
return callback(null, new CID(result['Cid']['/']))
62+
} else {
63+
return callback(result)
64+
}
65+
})
66+
}
67+
})
68+
}

src/utils/load-commands.js

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function requireCommands () {
2121
bootstrap: require('../bootstrap'),
2222
commands: require('../commands'),
2323
config: require('../config'),
24+
dag: require('../dag'),
2425
dht: require('../dht'),
2526
diag: require('../diag'),
2627
id: require('../id'),

0 commit comments

Comments
 (0)