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

Commit a72e3a8

Browse files
committed
feat: use async/await based ipfs-repo
This is WIP and IPFS doesn't even start properly.
1 parent 06ee4ce commit a72e3a8

29 files changed

+332
-372
lines changed

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,23 @@
9191
"hapi-pino": "^6.0.2",
9292
"hashlru": "^2.3.0",
9393
"human-to-milliseconds": "^2.0.0",
94-
"interface-datastore": "~0.6.0",
94+
"interface-datastore": "~0.7.0",
9595
"ipfs-bitswap": "~0.25.1",
9696
"ipfs-block": "~0.8.1",
97-
"ipfs-block-service": "~0.15.2",
97+
"ipfs-block-service": "~0.16.0",
9898
"ipfs-http-client": "^33.1.0",
9999
"ipfs-http-response": "~0.3.1",
100-
"ipfs-mfs": "~0.12.0",
100+
"ipfs-mfs": "^0.13.0",
101101
"ipfs-multipart": "~0.1.1",
102102
"ipfs-repo": "~0.27.0",
103103
"ipfs-unixfs": "~0.1.16",
104-
"ipfs-unixfs-exporter": "~0.37.7",
105-
"ipfs-unixfs-importer": "~0.39.11",
104+
"ipfs-unixfs-exporter": "^0.38.0",
105+
"ipfs-unixfs-importer": "^0.40.0",
106106
"ipfs-utils": "~0.0.4",
107-
"ipld": "~0.24.1",
107+
"ipld": "~0.25.0",
108108
"ipld-bitcoin": "~0.3.0",
109109
"ipld-dag-cbor": "~0.15.0",
110-
"ipld-dag-pb": "~0.17.4",
110+
"ipld-dag-pb": "~0.18.0",
111111
"ipld-ethereum": "^4.0.0",
112112
"ipld-git": "~0.5.0",
113113
"ipld-raw": "^4.0.0",
@@ -120,7 +120,7 @@
120120
"iso-url": "~0.4.6",
121121
"just-safe-set": "^2.1.0",
122122
"kind-of": "^6.0.2",
123-
"libp2p": "~0.25.4",
123+
"libp2p": "git+https://github.com/libp2p/js-libp2p.git",
124124
"libp2p-bootstrap": "~0.9.3",
125125
"libp2p-crypto": "~0.16.0",
126126
"libp2p-delegated-content-routing": "^0.2.4",
@@ -146,6 +146,7 @@
146146
"multihashes": "~0.4.14",
147147
"multihashing-async": "~0.6.0",
148148
"node-fetch": "^2.3.0",
149+
"p-iteration": "^1.1.8",
149150
"peer-book": "~0.9.0",
150151
"peer-id": "~0.12.3",
151152
"peer-info": "~0.15.0",

src/cli/daemon.js

Lines changed: 1 addition & 2 deletions
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 11 additions & 7 deletions
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

Lines changed: 30 additions & 59 deletions
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

Lines changed: 4 additions & 15 deletions
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

Lines changed: 19 additions & 22 deletions
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
/**

src/core/components/files-regular/refs-local-pull-stream.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ module.exports = function (self) {
99
return () => {
1010
const deferred = pullDefer.source()
1111

12-
self._repo.blocks.query({ keysOnly: true }, (err, blocks) => {
13-
if (err) {
14-
return deferred.resolve(pull.error(err))
12+
self._repo.blocks.query({ keysOnly: true }).then(
13+
(blocks) => {
14+
const refs = blocks.map(b => dsKeyToRef(b.key))
15+
deferred.resolve(pull.values(refs))
16+
}, (err) => {
17+
deferred.resolve(pull.error(err))
1518
}
16-
17-
const refs = blocks.map(b => dsKeyToRef(b.key))
18-
deferred.resolve(pull.values(refs))
19-
})
19+
)
2020

2121
return deferred
2222
}

0 commit comments

Comments
 (0)