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

Commit 65827db

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

35 files changed

+659
-770
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

+19-18
Original file line numberDiff line numberDiff line change
@@ -92,40 +92,40 @@
9292
"hapi-pino": "^6.0.2",
9393
"hashlru": "^2.3.0",
9494
"human-to-milliseconds": "^2.0.0",
95-
"interface-datastore": "~0.6.0",
95+
"interface-datastore": "~0.7.0",
9696
"ipfs-bitswap": "~0.25.1",
9797
"ipfs-block": "~0.8.1",
98-
"ipfs-block-service": "~0.15.2",
99-
"ipfs-http-client": "^33.1.1",
98+
"ipfs-block-service": "~0.16.0",
99+
"ipfs-http-client": "^33.1.0",
100100
"ipfs-http-response": "~0.3.1",
101-
"ipfs-mfs": "~0.12.0",
101+
"ipfs-mfs": "^0.13.0",
102102
"ipfs-multipart": "~0.1.1",
103103
"ipfs-repo": "~0.27.0",
104104
"ipfs-unixfs": "~0.1.16",
105-
"ipfs-unixfs-exporter": "~0.37.7",
106-
"ipfs-unixfs-importer": "~0.39.11",
105+
"ipfs-unixfs-exporter": "^0.38.0",
106+
"ipfs-unixfs-importer": "^0.40.0",
107107
"ipfs-utils": "~0.0.4",
108-
"ipld": "~0.24.1",
108+
"ipld": "~0.25.0",
109109
"ipld-bitcoin": "~0.3.0",
110110
"ipld-dag-cbor": "~0.15.0",
111-
"ipld-dag-pb": "~0.17.4",
111+
"ipld-dag-pb": "~0.18.0",
112112
"ipld-ethereum": "^4.0.0",
113113
"ipld-git": "~0.5.0",
114114
"ipld-raw": "^4.0.0",
115115
"ipld-zcash": "~0.3.0",
116-
"ipns": "~0.5.2",
116+
"ipns": "~0.6.0",
117117
"is-domain-name": "^1.0.1",
118118
"is-ipfs": "~0.6.1",
119119
"is-pull-stream": "~0.0.0",
120120
"is-stream": "^2.0.0",
121121
"iso-url": "~0.4.6",
122122
"just-safe-set": "^2.1.0",
123123
"kind-of": "^6.0.2",
124-
"libp2p": "~0.25.4",
125-
"libp2p-bootstrap": "~0.9.3",
126-
"libp2p-crypto": "~0.16.0",
127-
"libp2p-delegated-content-routing": "^0.2.4",
128-
"libp2p-delegated-peer-routing": "^0.2.4",
124+
"libp2p": "^0.26.1",
125+
"libp2p-bootstrap": "~0.10.2",
126+
"libp2p-crypto": "~0.17.0",
127+
"libp2p-delegated-content-routing": "^0.3.1",
128+
"libp2p-delegated-peer-routing": "^0.3.1",
129129
"libp2p-kad-dht": "~0.16.0",
130130
"libp2p-keychain": "~0.5.0",
131131
"libp2p-mdns": "~0.12.0",
@@ -146,11 +146,12 @@
146146
"multibase": "~0.6.0",
147147
"multicodec": "~0.5.5",
148148
"multihashes": "~0.4.14",
149-
"multihashing-async": "~0.6.0",
149+
"multihashing-async": "~0.7.0",
150150
"node-fetch": "^2.3.0",
151+
"p-iteration": "^1.1.8",
151152
"peer-book": "~0.9.0",
152-
"peer-id": "~0.12.3",
153-
"peer-info": "~0.15.0",
153+
"peer-id": "~0.13.2",
154+
"peer-info": "~0.16.0",
154155
"progress": "^2.0.1",
155156
"promisify-es6": "^1.0.3",
156157
"protons": "^1.0.1",
@@ -191,7 +192,7 @@
191192
"execa": "^2.0.4",
192193
"form-data": "^2.5.1",
193194
"hat": "0.0.3",
194-
"interface-ipfs-core": "^0.110.0",
195+
"interface-ipfs-core": "^0.111.0",
195196
"ipfsd-ctl": "^0.44.1",
196197
"libp2p-websocket-star": "~0.10.2",
197198
"ncp": "^2.0.0",

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 () {
@@ -55,11 +56,9 @@ exports.getIPFS = (argv, callback) => {
5556
}
5657
})
5758

58-
const cleanup = promisify((cb) => {
59+
const cleanup = callbackify(async () => {
5960
if (node && node._repo && !node._repo.closed) {
60-
node._repo.close((err) => cb(err))
61-
} else {
62-
cb()
61+
return node._repo.close()
6362
}
6463
})
6564

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)