Skip to content

Commit e6db5cf

Browse files
achingbrainjacobheun
authored andcommitted
feat: refactor/async await (#199)
BREAKING CHANGE: ipfs-repo has been refactored to use async/await. All callback support has been dropped. See the README for the new api.
1 parent 25cecd7 commit e6db5cf

24 files changed

+1132
-1011
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ docs
22
yarn.lock
33
package-lock.json
44

5-
test/test-repo-for*
5+
test-repo-for*
66
.vscode
77
.eslintrc
88
# Logs
@@ -21,6 +21,7 @@ lib-cov
2121

2222
# Coverage directory used by tools like istanbul
2323
coverage
24+
.nyc_output
2425

2526
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
2627
.grunt
@@ -35,3 +36,4 @@ build
3536
node_modules
3637

3738
dist
39+

README.md

+42-61
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,9 @@ Example:
107107
const Repo = require('ipfs-repo')
108108
const repo = new Repo('/tmp/ipfs-repo')
109109

110-
repo.init({ cool: 'config' }, (err) => {
111-
if (err) {
112-
throw err
113-
}
114-
115-
repo.open((err) => {
116-
if (err) {
117-
throw err
118-
}
119-
120-
console.log('repo is ready')
121-
})
122-
})
110+
await repo.init({ cool: 'config' })
111+
await repo.open()
112+
console.log('repo is ready')
123113
```
124114

125115
This now has created the following structure, either on disk or as an in memory representation:
@@ -157,63 +147,61 @@ Arguments:
157147
const repo = new Repo('path/to/repo')
158148
```
159149

160-
#### `repo.init (callback)`
150+
#### `Promise repo.init ()`
161151

162152
Creates the necessary folder structure inside the repo.
163153

164-
#### `repo.open (callback)`
154+
#### `Promise repo.open ()`
165155

166156
[Locks](https://en.wikipedia.org/wiki/Record_locking) the repo to prevent conflicts arising from simultaneous access.
167157

168-
#### `repo.close (callback)`
158+
#### `Promise repo.close ()`
169159

170160
Unlocks the repo.
171161

172-
#### `repo.exists (callback)`
162+
#### `Promise<boolean> repo.exists ()`
173163

174-
Tells whether this repo exists or not. Calls back with `(err, bool)`.
164+
Tells whether this repo exists or not. Returned promise resolves to a `boolean`.
175165

176166
### Repos
177167

178168
Root repo:
179169

180-
#### `repo.put (key, value:Buffer, callback)`
170+
#### `Promise repo.put (key, value:Buffer)`
181171

182172
Put a value at the root of the repo.
183173

184174
* `key` can be a buffer, a string or a [Key](https://github.com/ipfs/interface-datastore#keys).
185175

186-
#### `repo.get (key, callback)`
176+
#### `Promise<Buffer> repo.get (key)`
187177

188178
Get a value at the root of the repo.
189179

190180
* `key` can be a buffer, a string or a [Key](https://github.com/ipfs/interface-datastore#keys).
191-
* `callback` is a callback function `function (err, result:Buffer)`
192181

193182
[Blocks](https://github.com/ipfs/js-ipfs-block#readme):
194183

195-
#### `repo.blocks.put (block:Block, callback)`
184+
#### `Promise repo.blocks.put (block:Block)`
196185

197186
* `block` should be of type [Block](https://github.com/ipfs/js-ipfs-block#readme).
198187

199-
#### `repo.blocks.putMany (blocks, callback)`
188+
#### `Promise repo.blocks.putMany (blocks)`
200189

201190
Put many blocks.
202191

203192
* `block` should be an array of type [Block](https://github.com/ipfs/js-ipfs-block#readme).
204193

205-
#### `repo.blocks.get (cid, callback)`
194+
#### `Promise<Buffer> repo.blocks.get (cid)`
206195

207196
Get block.
208197

209198
* `cid` is the content id of [type CID](https://github.com/ipld/js-cid#readme).
210-
* `callback` is a callback function `function (err, result:Buffer)`
211199

212200
Datastore:
213201

214202
#### `repo.datastore`
215203

216-
This is contains a full implementation of [the `interface-datastore` API](https://github.com/ipfs/interface-datastore#api).
204+
This contains a full implementation of [the `interface-datastore` API](https://github.com/ipfs/interface-datastore#api).
217205

218206

219207
### Utils
@@ -222,77 +210,70 @@ This is contains a full implementation of [the `interface-datastore` API](https:
222210

223211
Instead of using `repo.set('config')` this exposes an API that allows you to set and get a decoded config object, as well as, in a safe manner, change any of the config values individually.
224212

225-
##### `repo.config.set(key:string, value, callback)`
213+
##### `Promise repo.config.set(key:string, value)`
226214

227215
Set a config value. `value` can be any object that is serializable to JSON.
228216

229217
* `key` is a string specifying the object path. Example:
230218

231219
```js
232-
repo.config.set('a.b.c', 'c value', (err) => {
233-
if (err) { throw err }
234-
repo.config.get((err, config) => {
235-
if (err) { throw err }
236-
assert.equal(config.a.b.c, 'c value')
237-
})
238-
})
220+
await repo.config.set('a.b.c', 'c value')
221+
const config = await repo.config.get()
222+
assert.equal(config.a.b.c, 'c value')
239223
```
240224

241-
##### `repo.config.get(value, callback)`
225+
##### `Promise repo.config.set(value)`
242226

243227
Set the whole config value. `value` can be any object that is serializable to JSON.
244228

245-
##### `repo.config.get(key:string, callback)`
229+
##### `Promise<?> repo.config.get(key:string)`
246230

247-
Get a config value. `callback` is a function with the signature: `function (err, value)`, wehre the `
248-
value` is of the same type that was set before.
231+
Get a config value. Returned promise resolves to the same type that was set before.
249232

250233
* `key` is a string specifying the object path. Example:
251234

252235
```js
253-
repo.config.get('a.b.c', (err, value) => {
254-
if (err) { throw err }
255-
console.log('config.a.b.c = ', value)
256-
})
236+
const value = await repo.config.get('a.b.c')
237+
console.log('config.a.b.c = ', value)
257238
```
258239

259-
##### `repo.config.get(callback)`
240+
##### `Promise<Object> repo.config.get()`
260241

261-
Get the entire config value. `callback` is a function with the signature: `function (err, configValue:Object)`.
242+
Get the entire config value.
262243

263-
#### `repo.config.exists(callback)`
244+
#### `Promise<boolean> repo.config.exists()`
264245

265-
Whether the config sub-repo exists. Calls back with `(err, bool)`.
246+
Whether the config sub-repo exists.
266247

267248
#### `repo.version`
268249

269-
##### `repo.version.get (callback)`
250+
##### `Promise<Number> repo.version.get ()`
270251

271-
Gets the repo version.
252+
Gets the repo version (an integer).
272253

273-
##### `repo.version.set (version:number, callback)`
254+
##### `Promise repo.version.set (version:Number)`
274255

275256
Sets the repo version
276257

277258
#### `repo.apiAddr`
278259

279-
#### `repo.apiAddr.get (callback)`
260+
#### `Promise<String> repo.apiAddr.get ()`
280261

281262
Gets the API address.
282263

283-
#### `repo.apiAddr.set (value, callback)`
264+
#### `Promise repo.apiAddr.set (value)`
284265

285266
Sets the API address.
286267

287268
* `value` should be a [Multiaddr](https://github.com/multiformats/js-multiaddr) or a String representing a valid one.
288269

289-
### `repo.stat ([options], callback)`
270+
### `Promise<Object> repo.stat ([options])`
290271

291272
Gets the repo status.
292273

293274
`options` is an object which might contain the key `human`, which is a boolean indicating whether or not the `repoSize` should be displayed in MiB or not.
294275

295-
`callback` is a function with the signature `function (err, stats)`, where `stats` is an Object with the following keys:
276+
Returned promise resolves to an `Object` with the following keys:
296277

297278
- `numObjects`
298279
- `repoPath`
@@ -311,27 +292,27 @@ const memoryLock = require('ipfs-repo/src/lock-memory') // Default in browser
311292

312293
You can also provide your own custom Lock. It must be an object with the following interface:
313294

314-
#### `lock.lock (dir, callback)`
295+
#### `Promise lock.lock (dir)`
315296

316-
Sets the lock if one does not already exist. If a lock already exists, `callback` should be called with an error.
297+
Sets the lock if one does not already exist. If a lock already exists, should throw an error.
317298

318299
`dir` is a string to the directory the lock should be created at. The repo typically creates the lock at its root.
319300

320-
`callback` is a function with the signature `function (err, closer)`, where `closer` has a `close` method for removing the lock.
301+
Returns `closer`, where `closer` has a `close` method for removing the lock.
321302

322-
##### `closer.close (callback)`
303+
##### `Promise closer.close ()`
323304

324305
Closes the lock created by `lock.open`
325306

326-
`callback` is a function with the signature `function (err)`. If no error was returned, the lock was successfully removed.
307+
If no error was thrown, the lock was successfully removed.
327308

328-
#### `lock.locked (dir, callback)`
309+
#### `Promise<boolean> lock.locked (dir)`
329310

330311
Checks the existence of the lock.
331312

332-
`dir` is a string to the directory to check for the lock. The repo typically checks for the lock at its root.
313+
`dir` is the path to the directory to check for the lock. The repo typically checks for the lock at its root.
333314

334-
`callback` is a function with the signature `function (err, boolean)`, where `boolean` indicates the existence of the lock.
315+
Returned promise resolves to a `boolean` indicating the existence of the lock.
335316

336317
## Notes
337318

example.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
'use strict'
22

3-
const Repo = require('ipfs-repo')
4-
const repo = new Repo('/Users/awesome/.jsipfs')
3+
const Repo = require('ipfs-repo');
54

6-
repo.init({ my: 'config' }, (err) => {
7-
if (err) {
8-
throw err
9-
}
5+
(async () => {
6+
const repo = new Repo('/Users/awesome/.jsipfs')
107

11-
repo.open((err) => {
12-
if (err) {
13-
throw err
14-
}
15-
16-
console.log('repo is ready')
17-
})
18-
})
8+
await repo.init({ my: 'config' })
9+
await repo.open()
10+
console.log('repo is ready') // eslint-disable-line no-console
11+
})()

package.json

+19-19
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
},
1717
"scripts": {
1818
"test": "aegir test",
19-
"test:node": "aegir test --target node",
20-
"test:browser": "aegir test --target browser",
19+
"test:node": "aegir test -t node",
20+
"test:browser": "aegir test -t browser",
21+
"test:webworker": "aegir test -t webworker",
2122
"build": "aegir build",
2223
"lint": "aegir lint",
2324
"release": "aegir release --docs",
2425
"release-minor": "aegir release --type minor --docs",
2526
"release-major": "aegir release --type major --docs",
26-
"coverage": "aegir coverage",
27-
"coverage-publish": "aegir-coverage --provider coveralls",
27+
"coverage": "nyc -s npm run test:node && nyc report --reporter=html",
28+
"dep-check": "aegir dep-check",
2829
"docs": "aegir docs"
2930
},
3031
"repository": {
@@ -38,38 +39,37 @@
3839
],
3940
"homepage": "https://github.com/ipfs/js-ipfs-repo",
4041
"engines": {
41-
"node": ">=6.0.0",
42+
"node": ">=10.0.0",
4243
"npm": ">=3.0.0"
4344
},
4445
"devDependencies": {
45-
"aegir": "^18.2.1",
46+
"aegir": "^19.0.3",
4647
"chai": "^4.2.0",
4748
"dirty-chai": "^2.0.1",
4849
"lodash": "^4.17.11",
49-
"memdown": "^3.0.0",
50+
"memdown": "^4.0.0",
5051
"multihashes": "~0.4.14",
51-
"multihashing-async": "~0.5.2",
52+
"multihashing-async": "~0.7.0",
5253
"ncp": "^2.0.0",
5354
"rimraf": "^2.6.3"
5455
},
5556
"dependencies": {
56-
"async": "^2.6.2",
5757
"base32.js": "~0.1.0",
58-
"bignumber.js": "^8.1.1",
59-
"buffer": "^5.2.1",
58+
"bignumber.js": "^9.0.0",
6059
"cids": "~0.7.0",
61-
"datastore-core": "~0.6.0",
62-
"datastore-fs": "~0.8.0",
63-
"datastore-level": "~0.11.0",
60+
"datastore-core": "~0.7.0",
61+
"datastore-fs": "~0.9.0",
62+
"datastore-level": "~0.12.0",
6463
"debug": "^4.1.0",
65-
"dlv": "^1.1.2",
66-
"interface-datastore": "~0.6.0",
64+
"err-code": "^1.1.2",
65+
"interface-datastore": "~0.7.0",
6766
"ipfs-block": "~0.8.1",
67+
"just-safe-get": "^1.3.0",
6868
"just-safe-set": "^2.1.0",
69-
"multiaddr": "^6.0.6",
69+
"lodash.has": "^4.5.2",
70+
"p-queue": "^5.0.0",
7071
"proper-lockfile": "^4.0.0",
71-
"pull-stream": "^3.6.9",
72-
"sort-keys": "^2.0.0"
72+
"sort-keys": "^3.0.0"
7373
},
7474
"license": "MIT",
7575
"contributors": [

src/api-addr.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,28 @@ module.exports = (store) => {
99
/**
1010
* Get the current configuration from the repo.
1111
*
12-
* @param {function(Error, Object)} callback
13-
* @returns {void}
12+
* @returns {Promise<String>}
1413
*/
15-
get (callback) {
16-
store.get(apiFile, (err, value) => callback(err, value && value.toString()))
14+
async get () {
15+
const value = await store.get(apiFile)
16+
return value && value.toString()
1717
},
1818
/**
1919
* Set the current configuration for this repo.
2020
*
2121
* @param {Object} value - the api address to be written
22-
* @param {function(Error)} callback
23-
* @returns {void}
22+
* @returns {Promise<?>}
2423
*/
25-
set (value, callback) {
26-
store.put(apiFile, Buffer.from(value.toString()), callback)
24+
async set (value) { // eslint-disable-line require-await
25+
return store.put(apiFile, Buffer.from(value.toString()))
2726
},
2827
/**
2928
* Deletes api file
3029
*
31-
* @param {function(Error, bool)} callback
32-
* @returns {void}
30+
* @returns {Promise<void>}
3331
*/
34-
delete (callback) {
35-
store.delete(apiFile, callback)
32+
async delete () { // eslint-disable-line require-await
33+
return store.delete(apiFile)
3634
}
3735
}
3836
}

0 commit comments

Comments
 (0)