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

Commit 1232df3

Browse files
vmxachingbrain
authored andcommitted
feat: use async/await based ipfs-repo
This is WIP and IPFS doesn't even start properly.
1 parent 700eb3b commit 1232df3

33 files changed

+649
-757
lines changed

examples/traverse-ipld-graphs/get-path-accross-formats.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
'use strict'
22

33
const createNode = require('./create-node')
4-
const dagPB = require('ipld-dag-pb')
4+
const {
5+
DAGNode
6+
} = require('ipld-dag-pb')
57

68
async function main () {
79
const ipfs = await createNode()
810

911
console.log('\nStart of the example:')
1012

1113
const someData = Buffer.from('capoeira')
12-
const pbNode = dagPB.DAGNode.create(someData)
14+
const pbNode = new DAGNode(someData)
1315

1416
const pbNodeCid = await ipfs.dag.put(pbNode, {
1517
format: 'dag-pb',

examples/traverse-ipld-graphs/tree.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
'use strict'
22

33
const createNode = require('./create-node')
4-
const dagPB = require('ipld-dag-pb')
4+
const {
5+
DAGNode
6+
} = require('ipld-dag-pb')
57

68
async function main () {
79
const ipfs = await createNode()
810

911
console.log('\nStart of the example:')
1012

1113
const someData = Buffer.from('capoeira')
12-
const pbNode = dagPB.DAGNode.create(someData)
14+
const pbNode = new DAGNode(someData)
1315

1416
const pbNodeCid = await ipfs.dag.put(pbNode, {
1517
format: 'dag-pb',

package.json

+16-15
Original file line numberDiff line numberDiff line change
@@ -93,28 +93,28 @@
9393
"hapi-pino": "^6.1.0",
9494
"hashlru": "^2.3.0",
9595
"human-to-milliseconds": "^2.0.0",
96-
"interface-datastore": "~0.6.0",
96+
"interface-datastore": "~0.7.0",
9797
"ipfs-bitswap": "~0.25.1",
9898
"ipfs-block": "~0.8.1",
99-
"ipfs-block-service": "~0.15.2",
99+
"ipfs-block-service": "~0.16.0",
100100
"ipfs-http-client": "^37.0.2",
101101
"ipfs-http-response": "~0.3.1",
102-
"ipfs-mfs": "^0.12.2",
102+
"ipfs-mfs": "^0.13.0",
103103
"ipfs-multipart": "^0.2.0",
104104
"ipfs-repo": "~0.27.0",
105105
"ipfs-unixfs": "~0.1.16",
106-
"ipfs-unixfs-exporter": "~0.37.7",
107-
"ipfs-unixfs-importer": "~0.39.11",
106+
"ipfs-unixfs-exporter": "^0.38.0",
107+
"ipfs-unixfs-importer": "^0.40.0",
108108
"ipfs-utils": "~0.4.0",
109-
"ipld": "~0.24.1",
109+
"ipld": "~0.25.0",
110110
"ipld-bitcoin": "~0.3.0",
111111
"ipld-dag-cbor": "~0.15.0",
112-
"ipld-dag-pb": "~0.17.4",
112+
"ipld-dag-pb": "~0.18.0",
113113
"ipld-ethereum": "^4.0.0",
114114
"ipld-git": "~0.5.0",
115115
"ipld-raw": "^4.0.0",
116116
"ipld-zcash": "~0.3.0",
117-
"ipns": "~0.5.2",
117+
"ipns": "~0.6.0",
118118
"is-domain-name": "^1.0.1",
119119
"is-ipfs": "~0.6.1",
120120
"is-pull-stream": "~0.0.0",
@@ -127,10 +127,10 @@
127127
"ky": "~0.14.0",
128128
"ky-universal": "~0.3.0",
129129
"libp2p": "~0.26.1",
130-
"libp2p-bootstrap": "~0.9.3",
131-
"libp2p-crypto": "~0.16.0",
132-
"libp2p-delegated-content-routing": "^0.2.4",
133-
"libp2p-delegated-peer-routing": "^0.2.4",
130+
"libp2p-bootstrap": "~0.10.2",
131+
"libp2p-crypto": "~0.17.0",
132+
"libp2p-delegated-content-routing": "^0.3.1",
133+
"libp2p-delegated-peer-routing": "^0.3.1",
134134
"libp2p-floodsub": "^0.18.0",
135135
"libp2p-gossipsub": "~0.0.5",
136136
"libp2p-kad-dht": "~0.16.0",
@@ -153,11 +153,12 @@
153153
"multibase": "~0.6.0",
154154
"multicodec": "~0.5.5",
155155
"multihashes": "~0.4.14",
156-
"multihashing-async": "~0.6.0",
156+
"multihashing-async": "~0.7.0",
157+
"node-fetch": "^2.3.0",
157158
"p-queue": "^6.1.0",
158159
"peer-book": "~0.9.0",
159-
"peer-id": "~0.12.3",
160-
"peer-info": "~0.15.0",
160+
"peer-id": "~0.13.2",
161+
"peer-info": "~0.16.0",
161162
"progress": "^2.0.1",
162163
"promise-nodeify": "^3.0.1",
163164
"promisify-es6": "^1.0.3",

src/cli/daemon.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const TCP = require('libp2p-tcp')
99
const MulticastDNS = require('libp2p-mdns')
1010
const WS = require('libp2p-websockets')
1111
const Bootstrap = require('libp2p-bootstrap')
12-
const promisify = require('promisify-es6')
1312

1413
class Daemon {
1514
constructor (options) {
@@ -75,7 +74,7 @@ class Daemon {
7574

7675
// for the CLI to know the where abouts of the API
7776
if (this._httpApi._apiServers.length) {
78-
await promisify(ipfs._repo.apiAddr.set)(this._httpApi._apiServers[0].info.ma)
77+
await ipfs._repo.apiAddr.set(this._httpApi._apiServers[0].info.ma)
7978
}
8079

8180
this._log('started')

src/cli/utils.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ log.error = debug('cli:error')
1010
const Progress = require('progress')
1111
const byteman = require('byteman')
1212
const promisify = require('promisify-es6')
13+
const callbackify = require('callbackify')
1314

1415
exports.isDaemonOn = isDaemonOn
1516
function isDaemonOn () {
@@ -52,11 +53,9 @@ exports.getIPFS = (argv, callback) => {
5253
pass: argv.pass
5354
})
5455

55-
const cleanup = promisify((cb) => {
56+
const cleanup = callbackify(async () => {
5657
if (node && node._repo && !node._repo.closed) {
57-
node._repo.close((err) => cb(err))
58-
} else {
59-
cb()
58+
return node._repo.close()
6059
}
6160
})
6261

src/core/boot.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ module.exports = (self) => {
1515
// Checks if a repo exists, and if so opens it
1616
// Will return callback with a bool indicating the existence
1717
// of the repo
18-
(cb) => {
18+
// TODO vmx 2019-08-05: THIS WON'T WORK IN THE BROWSER due to transpiling, this needs a proper fix. This is just a hack to keep things moving
19+
async () => {
1920
// nothing to do
2021
if (!self._repo.closed) {
21-
return cb(null, true)
22+
return true
2223
}
2324

24-
self._repo.open((err, res) => {
25-
if (isRepoUninitializedError(err)) return cb(null, false)
26-
if (err) return cb(err)
27-
cb(null, true)
28-
})
25+
try {
26+
const res = await self._repo.open()
27+
} catch (err) {
28+
if (isRepoUninitializedError(err)) return false
29+
if (err) throw err
30+
}
31+
32+
return true
2933
},
3034
(repoOpened, cb) => {
3135
// Init with existing initialized, opened, repo

src/core/components/bootstrap.js

+30-59
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const defaultConfig = require('../runtime/config-nodejs.js')
44
const isMultiaddr = require('mafmt').IPFS.matches
5-
const promisify = require('promisify-es6')
5+
const callbackify = require('callbackify')
66

77
function isValidMultiaddr (ma) {
88
try {
@@ -18,76 +18,47 @@ function invalidMultiaddrError (ma) {
1818

1919
module.exports = function bootstrap (self) {
2020
return {
21-
list: promisify((callback) => {
22-
self._repo.config.get((err, config) => {
23-
if (err) {
24-
return callback(err)
25-
}
26-
callback(null, { Peers: config.Bootstrap })
27-
})
21+
list: callbackify(async () => {
22+
const config = await self._repo.config.get()
23+
return { Peers: config.Bootstrap }
2824
}),
29-
add: promisify((multiaddr, args, callback) => {
30-
if (typeof args === 'function') {
31-
callback = args
32-
args = { default: false }
33-
}
34-
25+
add: callbackify(async (multiaddr, args = { default: false }) => {
3526
if (multiaddr && !isValidMultiaddr(multiaddr)) {
36-
return setImmediate(() => callback(invalidMultiaddrError(multiaddr)))
27+
throw invalidMultiaddrError(multiaddr)
3728
}
3829

39-
self._repo.config.get((err, config) => {
40-
if (err) {
41-
return callback(err)
42-
}
43-
if (args.default) {
44-
config.Bootstrap = defaultConfig().Bootstrap
45-
} else if (multiaddr && config.Bootstrap.indexOf(multiaddr) === -1) {
46-
config.Bootstrap.push(multiaddr)
47-
}
48-
self._repo.config.set(config, (err) => {
49-
if (err) {
50-
return callback(err)
51-
}
30+
const config = self._repo.config.get()
31+
if (args.default) {
32+
config.Bootstrap = defaultConfig().Bootstrap
33+
} else if (multiaddr && config.Bootstrap.indexOf(multiaddr) === -1) {
34+
config.Bootstrap.push(multiaddr)
35+
}
36+
await self._repo.config.set(config)
5237

53-
callback(null, {
54-
Peers: args.default ? defaultConfig().Bootstrap : [multiaddr]
55-
})
56-
})
57-
})
58-
}),
59-
rm: promisify((multiaddr, args, callback) => {
60-
if (typeof args === 'function') {
61-
callback = args
62-
args = { all: false }
38+
return {
39+
Peers: args.default ? defaultConfig().Bootstrap : [multiaddr]
6340
}
41+
}),
42+
rm: callbackify(async (multiaddr, args = { all: false }) => {
6443
if (multiaddr && !isValidMultiaddr(multiaddr)) {
65-
return setImmediate(() => callback(invalidMultiaddrError(multiaddr)))
44+
throw invalidMultiaddrError(multiaddr)
6645
}
6746

68-
self._repo.config.get((err, config) => {
69-
if (err) {
70-
return callback(err)
71-
}
72-
if (args.all) {
73-
config.Bootstrap = []
74-
} else {
75-
config.Bootstrap = config.Bootstrap.filter((mh) => mh !== multiaddr)
76-
}
47+
const config = await self._repo.config.get()
48+
if (args.all) {
49+
config.Bootstrap = []
50+
} else {
51+
config.Bootstrap = config.Bootstrap.filter((mh) => mh !== multiaddr)
52+
}
7753

78-
self._repo.config.set(config, (err) => {
79-
if (err) {
80-
return callback(err)
81-
}
54+
await self._repo.config.set(config)
8255

83-
const res = []
84-
if (!args.all && multiaddr) {
85-
res.push(multiaddr)
86-
}
56+
const res = []
57+
if (!args.all && multiaddr) {
58+
res.push(multiaddr)
59+
}
8760

88-
callback(null, { Peers: res })
89-
})
90-
})
61+
return { Peers: res }
9162
})
9263
}
9364
}

src/core/components/config.js

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
3+
const callbackify = require('callbackify')
44

55
module.exports = function config (self) {
66
return {
7-
get: promisify((key, callback) => {
8-
if (typeof key === 'function') {
9-
callback = key
10-
key = undefined
11-
}
12-
13-
return self._repo.config.get(key, callback)
14-
}),
15-
set: promisify((key, value, callback) => {
16-
self._repo.config.set(key, value, callback)
17-
}),
18-
replace: promisify((config, callback) => {
19-
self._repo.config.set(config, callback)
20-
})
7+
get: callbackify(self._repo.config.get),
8+
set: callbackify(self._repo.config.set),
9+
replace: callbackify(self._repo.config.set)
2110
}
2211
}

src/core/components/dht.js

+19-22
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict'
22

33
const promisify = require('promisify-es6')
4-
const every = require('async/every')
54
const PeerId = require('peer-id')
65
const PeerInfo = require('peer-info')
76
const CID = require('cids')
87
const each = require('async/each')
98
const nextTick = require('async/nextTick')
9+
const { every, forEach } = require('p-iteration')
10+
const callbackify = require('callbackify')
1011

1112
const errcode = require('err-code')
1213

@@ -127,7 +128,7 @@ module.exports = (self) => {
127128
* @param {function(Error)} [callback]
128129
* @returns {Promise|void}
129130
*/
130-
provide: promisify((keys, options, callback) => {
131+
provide: callbackify(async (keys, options) => {
131132
if (!Array.isArray(keys)) {
132133
keys = [keys]
133134
}
@@ -139,29 +140,25 @@ module.exports = (self) => {
139140
options = options || {}
140141

141142
// ensure blocks are actually local
142-
every(keys, (key, cb) => {
143-
self._repo.blocks.has(key, cb)
144-
}, (err, has) => {
145-
if (err) {
146-
return callback(err)
147-
}
143+
const has = await every(keys, async (key) => {
144+
return self._repo.blocks.has(key)
145+
})
148146

149-
if (!has) {
150-
const errMsg = 'block(s) not found locally, cannot provide'
147+
if (!has) {
148+
const errMsg = 'block(s) not found locally, cannot provide'
151149

152-
log.error(errMsg)
153-
return callback(errcode(errMsg, 'ERR_BLOCK_NOT_FOUND'))
154-
}
150+
log.error(errMsg)
151+
throw errcode(errMsg, 'ERR_BLOCK_NOT_FOUND')
152+
}
155153

156-
if (options.recursive) {
157-
// TODO: Implement recursive providing
158-
return callback(errcode('not implemented yet', 'ERR_NOT_IMPLEMENTED_YET'))
159-
} else {
160-
each(keys, (cid, cb) => {
161-
self.libp2p.contentRouting.provide(cid, cb)
162-
}, callback)
163-
}
164-
})
154+
if (options.recursive) {
155+
// TODO: Implement recursive providing
156+
throw errcode('not implemented yet', 'ERR_NOT_IMPLEMENTED_YET')
157+
} else {
158+
forEach(keys, (cid) => {
159+
self.libp2p.contentRouting.provide(cid)
160+
})
161+
}
165162
}),
166163

167164
/**

0 commit comments

Comments
 (0)