This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
/
dag.js
165 lines (132 loc) · 4.12 KB
/
dag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict'
const promisify = require('promisify-es6')
const CID = require('cids')
const pull = require('pull-stream')
const mapAsync = require('async/map')
const setImmediate = require('async/setImmediate')
const flattenDeep = require('lodash/flattenDeep')
const errCode = require('err-code')
module.exports = function dag (self) {
return {
put: promisify((dagNode, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
} else if (options && options.cid && (options.format || options.hashAlg)) {
return callback(new Error('Can\'t put dag node. Please provide either `cid` OR `format` and `hashAlg` options.'))
} else if (options && ((options.format && !options.hashAlg) || (!options.format && options.hashAlg))) {
return callback(new Error('Can\'t put dag node. Please provide `format` AND `hashAlg` options.'))
}
options = options || {}
const optionDefaults = {
format: 'dag-cbor',
hashAlg: 'sha2-256'
}
options = options.cid ? options : Object.assign({}, optionDefaults, options)
self._ipld.put(dagNode, options, (err, cid) => {
if (err) return callback(err)
if (options.preload !== false) {
self._preload(cid)
}
callback(null, cid)
})
}),
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('/')
try {
cid = new CID(split[0])
} catch (err) {
return setImmediate(() => callback(errCode(err, 'ERR_INVALID_CID')))
}
split.shift()
if (split.length > 0) {
path = split.join('/')
} else {
path = '/'
}
} else if (Buffer.isBuffer(cid)) {
try {
cid = new CID(cid)
} catch (err) {
return setImmediate(() => callback(errCode(err, 'ERR_INVALID_CID')))
}
}
if (options.preload !== false) {
self._preload(cid)
}
self._ipld.get(cid, path, options, callback)
}),
tree: promisify((cid, path, options, callback) => {
if (typeof path === 'object') {
callback = options
options = path
path = undefined
}
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('/')
try {
cid = new CID(split[0])
} catch (err) {
return setImmediate(() => callback(errCode(err, 'ERR_INVALID_CID')))
}
split.shift()
if (split.length > 0) {
path = split.join('/')
} else {
path = undefined
}
}
if (options.preload !== false) {
self._preload(cid)
}
pull(
self._ipld.treeStream(cid, path, options),
pull.collect(callback)
)
}),
// TODO - use IPLD selectors once they are implemented
_getRecursive: promisify((multihash, options, callback) => {
// gets flat array of all DAGNodes in tree given by multihash
if (typeof options === 'function') {
callback = options
options = {}
}
options = options || {}
let cid
try {
cid = new CID(multihash)
} catch (err) {
return setImmediate(() => callback(errCode(err, 'ERR_INVALID_CID')))
}
self.dag.get(cid, '', options, (err, res) => {
if (err) { return callback(err) }
mapAsync(res.value.links, (link, cb) => {
self.dag._getRecursive(link.multihash, cb)
}, (err, nodes) => {
// console.log('nodes:', nodes)
if (err) return callback(err)
callback(null, flattenDeep([res.value, nodes]))
})
})
})
}
}