Skip to content

Commit ee94423

Browse files
author
Zane Starr
committed
Refactor js-repo away from callbacks leaning on async/await
1 parent 01a4737 commit ee94423

18 files changed

+518
-919
lines changed

example.js

+3-13
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33
const Repo = require('ipfs-repo')
44
const repo = new Repo('/Users/awesome/.jsipfs')
55

6-
repo.init({ my: 'config' }, (err) => {
7-
if (err) {
8-
throw err
9-
}
10-
11-
repo.open((err) => {
12-
if (err) {
13-
throw err
14-
}
15-
16-
console.log('repo is ready')
17-
})
18-
})
6+
repo.init({ my: 'config' })
7+
.then(repo.open)
8+
.then(() => console.log('repo is ready'))

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,14 @@
4949
"rimraf": "^2.6.2"
5050
},
5151
"dependencies": {
52-
"async": "^2.6.1",
5352
"base32.js": "~0.1.0",
5453
"bignumber.js": "^8.0.2",
5554
"cids": "~0.5.7",
5655
"datastore-core": "~0.6.0",
5756
"datastore-fs": "~0.7.0",
58-
"datastore-level": "~0.10.0",
57+
"datastore-level": "git://github.com/ipfs/js-datastore-level.git#refactor/async-iterators",
5958
"debug": "^4.1.0",
60-
"interface-datastore": "~0.6.0",
59+
"interface-datastore": "git://github.com/ipfs/interface-datastore.git#refactor/async-iterators",
6160
"ipfs-block": "~0.8.0",
6261
"lodash.get": "^4.4.2",
6362
"lodash.has": "^4.5.2",

src/api-addr.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,28 @@ module.exports = (store) => {
1010
/**
1111
* Get the current configuration from the repo.
1212
*
13-
* @param {function(Error, Object)} callback
14-
* @returns {void}
13+
* @returns {Promise<Object>}
1514
*/
16-
get (callback) {
17-
store.get(apiFile, (err, value) => callback(err, value && value.toString()))
15+
async get () {
16+
const value = await store.get(apiFile)
17+
return value && value.toString()
1818
},
1919
/**
2020
* Set the current configuration for this repo.
2121
*
2222
* @param {Object} value - the api address to be written
23-
* @param {function(Error)} callback
24-
* @returns {void}
23+
* @returns {Promise<void>}
2524
*/
26-
set (value, callback) {
27-
store.put(apiFile, Buffer.from(value.toString()), callback)
25+
set (value) {
26+
return store.put(apiFile, Buffer.from(value.toString()))
2827
},
2928
/**
3029
* Deletes api file
3130
*
32-
* @param {function(Error, bool)} callback
33-
* @returns {void}
31+
* @returns {Promise<void>}
3432
*/
35-
delete (callback) {
36-
store.delete(apiFile, callback)
33+
delete () {
34+
return store.delete(apiFile)
3735
}
3836
}
3937
}

src/blockstore.js

+66-102
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ const ShardingStore = core.ShardingDatastore
55
const Key = require('interface-datastore').Key
66
const base32 = require('base32.js')
77
const Block = require('ipfs-block')
8-
const setImmediate = require('async/setImmediate')
9-
const reject = require('async/reject')
108
const CID = require('cids')
11-
const pull = require('pull-stream')
129

1310
/**
1411
* Transform a raw buffer to a base32 encoded key.
@@ -31,21 +28,17 @@ const cidToDsKey = (cid) => {
3128
return keyFromBuffer(cid.buffer)
3229
}
3330

34-
module.exports = (filestore, options, callback) => {
35-
maybeWithSharding(filestore, options, (err, store) => {
36-
if (err) { return callback(err) }
37-
38-
callback(null, createBaseStore(store))
39-
})
31+
module.exports = async (filestore, options) => {
32+
const store = await maybeWithSharding(filestore, options)
33+
return createBaseStore(store)
4034
}
4135

42-
function maybeWithSharding (filestore, options, callback) {
36+
function maybeWithSharding (filestore, options) {
4337
if (options.sharding) {
4438
const shard = new core.shard.NextToLast(2)
45-
ShardingStore.createOrOpen(filestore, shard, callback)
46-
} else {
47-
setImmediate(() => callback(null, filestore))
39+
return ShardingStore.createOrOpen(filestore, shard)
4840
}
41+
return filestore
4942
}
5043

5144
function createBaseStore (store) {
@@ -54,142 +47,113 @@ function createBaseStore (store) {
5447
* Query the store.
5548
*
5649
* @param {object} query
57-
* @param {function(Error, Array)} callback
58-
* @return {void}
50+
* @return {Iterable}
5951
*/
60-
query (query, callback) {
61-
pull(
62-
store.query(query),
63-
pull.collect(callback)
64-
)
52+
query (query) {
53+
return store.query(query)
6554
},
6655
/**
6756
* Get a single block by CID.
6857
*
6958
* @param {CID} cid
70-
* @param {function(Error, Block)} callback
71-
* @returns {void}
59+
* @returns {Promise<Block>}
7260
*/
73-
get (cid, callback) {
61+
async get (cid) {
7462
if (!CID.isCID(cid)) {
75-
return setImmediate(() => {
76-
callback(new Error('Not a valid cid'))
77-
})
63+
throw new Error('Not a valid cid')
7864
}
79-
8065
const key = cidToDsKey(cid)
81-
store.get(key, (err, blockData) => {
82-
if (err) {
83-
// If not found, we try with the other CID version.
84-
// If exists, then store that block under the CID that was requested.
85-
// Some duplication occurs.
86-
if (err.code === 'ERR_NOT_FOUND') {
87-
const otherCid = cidToOtherVersion(cid)
88-
if (!otherCid) return callback(err)
89-
90-
const otherKey = cidToDsKey(otherCid)
91-
return store.get(otherKey, (err, blockData) => {
92-
if (err) return callback(err)
93-
94-
store.put(key, blockData, (err) => {
95-
if (err) return callback(err)
96-
callback(null, new Block(blockData, cid))
97-
})
98-
})
99-
}
100-
101-
return callback(err)
66+
let blockData
67+
try {
68+
blockData = await store.get(key)
69+
return new Block(blockData, cid)
70+
} catch (err) {
71+
if (err.code === 'ERR_NOT_FOUND') {
72+
const otherCid = cidToOtherVersion(cid)
73+
if (!otherCid) throw err
74+
75+
const otherKey = cidToDsKey(otherCid)
76+
const blockData = await store.get(otherKey)
77+
await store.put(key, blockData)
78+
return new Block(blockData, cid)
10279
}
103-
104-
callback(null, new Block(blockData, cid))
105-
})
80+
}
10681
},
107-
put (block, callback) {
82+
/**
83+
* Write a single block to the store.
84+
*
85+
* @param {Block} block
86+
* @returns {Promise<void>}
87+
*/
88+
put (block) {
10889
if (!Block.isBlock(block)) {
109-
return setImmediate(() => {
110-
callback(new Error('invalid block'))
111-
})
90+
throw new Error('invalid block')
11291
}
11392

11493
const k = cidToDsKey(block.cid)
115-
116-
store.has(k, (err, exists) => {
117-
if (err) { return callback(err) }
118-
if (exists) { return callback() }
119-
120-
store.put(k, block.data, callback)
94+
return store.has(k).then((exists) => {
95+
if (exists) { return }
96+
return store.put(k, block.data)
12197
})
12298
},
99+
123100
/**
124101
* Like put, but for more.
125102
*
126103
* @param {Array<Block>} blocks
127-
* @param {function(Error)} callback
128-
* @returns {void}
104+
* @returns {Promise<void>}
129105
*/
130-
putMany (blocks, callback) {
106+
async putMany (blocks) {
131107
const keys = blocks.map((b) => ({
132108
key: cidToDsKey(b.cid),
133109
block: b
134110
}))
135111

136-
const batch = store.batch()
137-
reject(keys, (k, cb) => store.has(k.key, cb), (err, newKeys) => {
138-
if (err) {
139-
return callback(err)
140-
}
141-
142-
newKeys.forEach((k) => {
143-
batch.put(k.key, k.block.data)
144-
})
145-
146-
batch.commit(callback)
112+
const batch = await store.batch()
113+
const newKeys = await Promise.all(keys.filter((k) => { store.has(k.key) }))
114+
newKeys.forEach((k) => {
115+
batch.put(k.key, k.block.data)
147116
})
117+
return batch.commit()
148118
},
149119
/**
150120
* Does the store contain block with this cid?
151121
*
152122
* @param {CID} cid
153-
* @param {function(Error, bool)} callback
154-
* @returns {void}
123+
* @returns {Promise<bool>}
155124
*/
156-
has (cid, callback) {
125+
has (cid) {
157126
if (!CID.isCID(cid)) {
158-
return setImmediate(() => {
159-
callback(new Error('Not a valid cid'))
160-
})
127+
throw new Error('Not a valid cid')
161128
}
162129

163-
store.has(cidToDsKey(cid), (err, exists) => {
164-
if (err) return callback(err)
165-
if (exists) return callback(null, true)
166-
167-
// If not found, we try with the other CID version.
168-
const otherCid = cidToOtherVersion(cid)
169-
if (!otherCid) return callback(null, false)
170-
171-
store.has(cidToDsKey(otherCid), callback)
172-
})
130+
return store.has(cidToDsKey(cid))
131+
.then((exists) => {
132+
if (exists) return exists
133+
const otherCid = cidToOtherVersion(cid)
134+
if (!otherCid) return false
135+
return store.has(cidToDsKey(otherCid))
136+
})
173137
},
174138
/**
175139
* Delete a block from the store
176140
*
177141
* @param {CID} cid
178-
* @param {function(Error)} callback
179-
* @returns {void}
142+
* @returns {Promise<void>}
180143
*/
181-
delete (cid, callback) {
144+
delete (cid) {
182145
if (!CID.isCID(cid)) {
183-
return setImmediate(() => {
184-
callback(new Error('Not a valid cid'))
185-
})
146+
throw new Error('Not a valid cid')
186147
}
187-
188-
store.delete(cidToDsKey(cid), callback)
148+
return store.delete(cidToDsKey(cid))
189149
},
190-
191-
close (callback) {
192-
store.close(callback)
150+
/**
151+
* Close the store
152+
*
153+
* @returns {Promise<void>}
154+
*/
155+
close () {
156+
return store.close()
193157
}
194158
}
195159
}

0 commit comments

Comments
 (0)