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

Commit bcaf12f

Browse files
committed
feat: use new IPLD API
This is part of the Awesome Endeavour: Async Iterators: #1670
1 parent 66d6c42 commit bcaf12f

File tree

9 files changed

+169
-163
lines changed

9 files changed

+169
-163
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"dependencies": {
8181
"@nodeutils/defaults-deep": "^1.1.0",
8282
"async": "^2.6.1",
83+
"async-iterator-to-pull-stream": "^1.1.0",
8384
"bignumber.js": "^8.0.2",
8485
"binary-querystring": "~0.1.2",
8586
"bl": "^3.0.0",
@@ -114,7 +115,7 @@
114115
"ipfs-unixfs": "~0.1.16",
115116
"ipfs-unixfs-exporter": "~0.36.1",
116117
"ipfs-unixfs-importer": "~0.38.5",
117-
"ipld": "~0.21.1",
118+
"ipld": "~0.22.0",
118119
"ipld-bitcoin": "~0.1.8",
119120
"ipld-dag-pb": "~0.15.3",
120121
"ipld-ethereum": "^2.0.1",

src/core/components/dag.js

+55-13
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,42 @@ 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.format, {
55+
hashAlg: options.hashAlg,
56+
cidVersion: options.version
57+
}).then(
58+
(cid) => {
59+
if (options.preload !== false) {
60+
self._preload(cid)
61+
}
62+
return callback(null, cid)
63+
},
64+
(error) => callback(error)
65+
)
4366
}),
4467

4568
get: promisify((cid, path, options, callback) => {
@@ -54,7 +77,7 @@ module.exports = function dag (self) {
5477
// Allow options in path position
5578
if (typeof path !== 'string') {
5679
options = path
57-
path = null
80+
path = undefined
5881
} else {
5982
options = {}
6083
}
@@ -90,7 +113,26 @@ module.exports = function dag (self) {
90113
self._preload(cid)
91114
}
92115

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

96138
tree: promisify((cid, path, options, callback) => {
@@ -135,7 +177,7 @@ module.exports = function dag (self) {
135177
}
136178

137179
pull(
138-
self._ipld.treeStream(cid, path, options),
180+
iterToPull(self._ipld.tree(cid, path, options)),
139181
pull.collect(callback)
140182
)
141183
}),

src/core/components/init.js

+7-3
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')
@@ -132,9 +133,12 @@ module.exports = function init (self) {
132133
(cb) => DAGNode.create(new UnixFs('directory').marshal(), cb),
133134
(node, cb) => self.dag.put(node, {
134135
version: 0,
135-
format: 'dag-pb',
136-
hashAlg: 'sha2-256'
137-
}, cb),
136+
format: multicodec.DAG_PB,
137+
hashAlg: multicodec.SHA2_256
138+
}).then(
139+
(cid) => cb(null, cid),
140+
(error) => cb(error)
141+
),
138142
(cid, cb) => self._ipns.initializeKeyspace(privateKey, cid.toBaseEncodedString(), cb)
139143
], cb)
140144
}

src/core/components/object.js

+42-48
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,19 @@ module.exports = function object (self) {
8788
return cb(err)
8889
}
8990

90-
self._ipld.put(node, {
91-
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-
})
91+
self._ipld.put(node, multicodec.DAG_PB, {
92+
cidVersion: 0,
93+
hashAlg: multicodec.SHA2_256,
94+
}).then(
95+
(cid) => {
96+
if (options.preload !== false) {
97+
self._preload(cid)
98+
}
99+
100+
cb(null, cid)
101+
},
102+
(error) => cb(error)
103+
)
103104
})
104105
}
105106
], callback)
@@ -137,21 +138,19 @@ module.exports = function object (self) {
137138
return callback(err)
138139
}
139140

140-
self._ipld.put(node, {
141-
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-
}
141+
self._ipld.put(node, multicodec.DAG_PB, {
142+
cidVersion: 0,
143+
hashAlg: multicodec.SHA2_256,
144+
}).then(
145+
(cid) => {
146+
if (options.preload !== false) {
147+
self._preload(cid)
148+
}
152149

153-
callback(null, cid)
154-
})
150+
callback(null, cid)
151+
},
152+
(error) => callback(error)
153+
)
155154
})
156155
}),
157156
put: promisify((obj, options, callback) => {
@@ -200,21 +199,19 @@ module.exports = function object (self) {
200199
}
201200

202201
function next () {
203-
self._ipld.put(node, {
204-
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-
}
202+
self._ipld.put(node, multicodec.DAG_PB, {
203+
cidVersion: 0,
204+
hashAlg: multicodec.SHA2_256,
205+
}).then(
206+
(cid) => {
207+
if (options.preload !== false) {
208+
self._preload(cid)
209+
}
215210

216-
callback(null, cid)
217-
})
211+
callback(null, cid)
212+
},
213+
(error) => callback(error)
214+
)
218215
}
219216
}),
220217

@@ -248,13 +245,10 @@ module.exports = function object (self) {
248245
self._preload(cid)
249246
}
250247

251-
self._ipld.get(cid, (err, result) => {
252-
if (err) {
253-
return callback(err)
254-
}
255-
256-
callback(null, result.value)
257-
})
248+
self._ipld.get(cid).then(
249+
(node) => callback(null, node),
250+
(error) => callback(error)
251+
)
258252
}),
259253

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

src/core/components/pin-set.js

+5-4
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 someSeries = require('async/someSeries')
1011
const eachOfSeries = require('async/eachOfSeries')
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

+5-4
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)