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

Commit 689df2c

Browse files
committed
feat: use new IPLD API
This is part of the Awesome Endeavour: Async Iterators: #1670
1 parent 2b3bdc3 commit 689df2c

File tree

8 files changed

+145
-133
lines changed

8 files changed

+145
-133
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"dependencies": {
8383
"@nodeutils/defaults-deep": "^1.1.0",
8484
"async": "^2.6.1",
85+
"async-iterator-to-pull-stream": "^1.1.0",
8586
"bignumber.js": "^8.0.2",
8687
"binary-querystring": "~0.1.2",
8788
"bl": "^2.1.2",
@@ -115,7 +116,7 @@
115116
"ipfs-repo": "~0.26.1",
116117
"ipfs-unixfs": "~0.1.16",
117118
"ipfs-unixfs-engine": "~0.35.3",
118-
"ipld": "~0.20.1",
119+
"ipld": "git+https://github.com/ipld/js-ipld.git#new-api-impl",
119120
"ipld-bitcoin": "~0.1.8",
120121
"ipld-dag-pb": "~0.15.0",
121122
"ipld-ethereum": "^2.0.1",
@@ -148,6 +149,7 @@
148149
"multiaddr": "^6.0.0",
149150
"multiaddr-to-uri": "^4.0.0",
150151
"multibase": "~0.6.0",
152+
"multicodec": "~0.5.0",
151153
"multihashes": "~0.4.14",
152154
"multihashing-async": "~0.5.1",
153155
"node-fetch": "^2.3.0",

src/core/components/dag.js

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
const promisify = require('promisify-es6')
44
const CID = require('cids')
55
const pull = require('pull-stream')
6+
const iterToPull = require('async-iterator-to-pull-stream')
67
const mapAsync = require('async/map')
78
const setImmediate = require('async/setImmediate')
89
const flattenDeep = require('lodash/flattenDeep')
910
const errCode = require('err-code')
11+
const multicodec = require('multicodec')
1012

1113
module.exports = function dag (self) {
1214
return {
@@ -25,21 +27,39 @@ module.exports = function dag (self) {
2527
}
2628

2729
const optionDefaults = {
28-
format: 'dag-cbor',
29-
hashAlg: 'sha2-256'
30+
format: multicodec.DAG_CBOR,
31+
hashAlg: multicodec.SHA2_256
3032
}
3133

32-
options = options.cid ? options : Object.assign({}, optionDefaults, options)
34+
// The IPLD expects the format and hashAlg as constants
35+
if (options.format && typeof options.format === 'string') {
36+
const constantName = options.format.toUpperCase().replace(/-/g, '_')
37+
options.format = multicodec[constantName]
38+
}
39+
if (options.hashAlg && typeof options.hashAlg === 'string') {
40+
const constantName = options.hashAlg.toUpperCase().replace(/-/g, '_')
41+
options.hashAlg = multicodec[constantName]
42+
}
3343

34-
self._ipld.put(dagNode, options, (err, cid) => {
35-
if (err) return callback(err)
44+
options = options.cid ? options : Object.assign({}, optionDefaults, options)
3645

37-
if (options.preload !== false) {
38-
self._preload(cid)
39-
}
46+
// js-ipld defaults to verion 1 CIDs. Hence set version 0 explicitly for
47+
// dag-pb nodes
48+
if (options.format === multicodec.DAG_PB &&
49+
options.hashAlg === multicodec.SHA2_256 &&
50+
options.version === undefined) {
51+
options.version = 0
52+
}
4053

41-
callback(null, cid)
42-
})
54+
self._ipld.put([dagNode], options).first().then(
55+
(cid) => {
56+
if (options.preload !== false) {
57+
self._preload(cid)
58+
}
59+
return callback(null, cid)
60+
},
61+
(error) => callback(error)
62+
)
4363
}),
4464

4565
get: promisify((cid, path, options, callback) => {
@@ -54,7 +74,7 @@ module.exports = function dag (self) {
5474
// Allow options in path position
5575
if (typeof path !== 'string') {
5676
options = path
57-
path = null
77+
path = undefined
5878
} else {
5979
options = {}
6080
}
@@ -90,7 +110,27 @@ module.exports = function dag (self) {
90110
self._preload(cid)
91111
}
92112

93-
self._ipld.get(cid, path, options, callback)
113+
if (path === undefined || path === '/') {
114+
const result = self._ipld.get([cid])
115+
result.first().then(
116+
(value) => {
117+
callback(null, {
118+
value,
119+
remainderPath: ''
120+
})
121+
},
122+
(error) => callback(error)
123+
)
124+
} else {
125+
const result = self._ipld.resolve(cid, path)
126+
const promisedValue = options.localResolve ?
127+
result.first() :
128+
result.last()
129+
promisedValue.then(
130+
(value) => callback(null, value),
131+
(error) => callback(error)
132+
)
133+
}
94134
}),
95135

96136
tree: promisify((cid, path, options, callback) => {
@@ -135,7 +175,7 @@ module.exports = function dag (self) {
135175
}
136176

137177
pull(
138-
self._ipld.treeStream(cid, path, options),
178+
iterToPull(self._ipld.tree(cid, path, options)),
139179
pull.collect(callback)
140180
)
141181
}),

src/core/components/init.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
DAGNode
1212
} = require('ipld-dag-pb')
1313
const UnixFs = require('ipfs-unixfs')
14+
const multicodec = require('multicodec')
1415

1516
const IPNS = require('../ipns')
1617
const OfflineDatastore = require('../ipns/routing/offline-datastore')
@@ -130,11 +131,13 @@ module.exports = function init (self) {
130131
(cb) => {
131132
waterfall([
132133
(cb) => DAGNode.create(new UnixFs('directory').marshal(), cb),
133-
(node, cb) => self.dag.put(node, {
134+
async (node) => {
135+
return self.dag.put(node, {
134136
version: 0,
135-
format: 'dag-pb',
136-
hashAlg: 'sha2-256'
137-
}, cb),
137+
format: multicodec.DAG_PB,
138+
hashAlg: multicodec.SHA2_256
139+
})
140+
},
138141
(cid, cb) => self._ipns.initializeKeyspace(privateKey, cid.toBaseEncodedString(), cb)
139142
], cb)
140143
}

src/core/components/object.js

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const DAGNode = dagPB.DAGNode
99
const DAGLink = dagPB.DAGLink
1010
const CID = require('cids')
1111
const mh = require('multihashes')
12+
const multicodec = require('multicodec')
1213
const Unixfs = require('ipfs-unixfs')
1314
const errCode = require('err-code')
1415

@@ -87,19 +88,20 @@ module.exports = function object (self) {
8788
return cb(err)
8889
}
8990

90-
self._ipld.put(node, {
91+
self._ipld.put([node], {
9192
version: 0,
92-
hashAlg: 'sha2-256',
93-
format: 'dag-pb'
94-
}, (err, cid) => {
95-
if (err) return cb(err)
96-
97-
if (options.preload !== false) {
98-
self._preload(cid)
99-
}
100-
101-
cb(null, cid)
102-
})
93+
hashAlg: multicodec.SHA2_256,
94+
format: multicodec.DAG_PB
95+
}).first().then(
96+
(cid) => {
97+
if (options.preload !== false) {
98+
self._preload(cid)
99+
}
100+
101+
cb(null, cid)
102+
},
103+
(error) => cb(error)
104+
)
103105
})
104106
}
105107
], callback)
@@ -137,21 +139,20 @@ module.exports = function object (self) {
137139
return callback(err)
138140
}
139141

140-
self._ipld.put(node, {
142+
self._ipld.put([node], {
141143
version: 0,
142-
hashAlg: 'sha2-256',
143-
format: 'dag-pb'
144-
}, (err, cid) => {
145-
if (err) {
146-
return callback(err)
147-
}
148-
149-
if (options.preload !== false) {
150-
self._preload(cid)
151-
}
144+
hashAlg: multicodec.SHA2_256,
145+
format: multicodec.DAG_PB
146+
}).first().then(
147+
(cid) => {
148+
if (options.preload !== false) {
149+
self._preload(cid)
150+
}
152151

153-
callback(null, cid)
154-
})
152+
callback(null, cid)
153+
},
154+
(error) => callback(error)
155+
)
155156
})
156157
}),
157158
put: promisify((obj, options, callback) => {
@@ -200,21 +201,20 @@ module.exports = function object (self) {
200201
}
201202

202203
function next () {
203-
self._ipld.put(node, {
204+
self._ipld.put([node], {
204205
version: 0,
205-
hashAlg: 'sha2-256',
206-
format: 'dag-pb'
207-
}, (err, cid) => {
208-
if (err) {
209-
return callback(err)
210-
}
211-
212-
if (options.preload !== false) {
213-
self._preload(cid)
214-
}
206+
hashAlg: multicodec.SHA2_256,
207+
format: multicodec.DAG_PB
208+
}).first().then(
209+
(cid) => {
210+
if (options.preload !== false) {
211+
self._preload(cid)
212+
}
215213

216-
callback(null, cid)
217-
})
214+
callback(null, cid)
215+
},
216+
(error) => callback(error)
217+
)
218218
}
219219
}),
220220

@@ -248,13 +248,10 @@ module.exports = function object (self) {
248248
self._preload(cid)
249249
}
250250

251-
self._ipld.get(cid, (err, result) => {
252-
if (err) {
253-
return callback(err)
254-
}
255-
256-
callback(null, result.value)
257-
})
251+
self._ipld.get([cid]).first().then(
252+
(node) => callback(null, node),
253+
(error) => callback(error)
254+
)
258255
}),
259256

260257
data: promisify((multihash, options, callback) => {

src/core/components/pin-set.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const protobuf = require('protons')
66
const fnv1a = require('fnv1a')
77
const varint = require('varint')
88
const { DAGNode, DAGLink } = require('ipld-dag-pb')
9+
const multicodec = require('multicodec')
910
const some = require('async/some')
1011
const eachOf = require('async/eachOf')
1112

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

111112
dag.put(rootNode, {
112113
version: 0,
113-
format: 'dag-pb',
114-
hashAlg: 'sha2-256',
114+
format: multicodec.DAG_PB,
115+
hashAlg: multicodec.SHA2_256,
115116
preload: false
116117
}, (err, cid) => {
117118
if (err) { return callback(err, cid) }
@@ -194,8 +195,8 @@ exports = module.exports = function (dag) {
194195

195196
const opts = {
196197
version: 0,
197-
hashAlg: 'sha2-256',
198-
format: 'dag-pb',
198+
format: multicodec.DAG_PB,
199+
hashAlg: multicodec.SHA2_256,
199200
preload: false
200201
}
201202

src/core/components/pin.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const setImmediate = require('async/setImmediate')
1515
const { Key } = require('interface-datastore')
1616
const errCode = require('err-code')
1717
const multibase = require('multibase')
18+
const multicodec = require('multicodec')
1819

1920
const createPinSet = require('./pin-set')
2021
const { resolvePath } = require('../utils')
@@ -104,8 +105,8 @@ module.exports = (self) => {
104105
if (err) { return cb(err) }
105106
dag.put(empty, {
106107
version: 0,
107-
hashAlg: 'sha2-256',
108-
format: 'dag-pb',
108+
format: multicodec.DAG_PB,
109+
hashAlg: multicodec.SHA2_256,
109110
preload: false
110111
}, cb)
111112
}),
@@ -116,8 +117,8 @@ module.exports = (self) => {
116117
root = node
117118
dag.put(root, {
118119
version: 0,
119-
hashAlg: 'sha2-256',
120-
format: 'dag-pb',
120+
format: multicodec.DAG_PB,
121+
hashAlg: multicodec.SHA2_256,
121122
preload: false
122123
}, (err, cid) => {
123124
if (!err) {

0 commit comments

Comments
 (0)