Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit 3505512

Browse files
committed
chore: callbacks -> async / await
BREAKING CHANGE: All places in the API that used callbacks are now replaced with async/await
1 parent 866e4e8 commit 3505512

File tree

8 files changed

+264
-368
lines changed

8 files changed

+264
-368
lines changed

examples/full-s3-repo/index.js

+22-27
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,31 @@ let node = new IPFS({
2020
console.log('Start the node')
2121

2222
// Test out the repo by sending and fetching some data
23-
node.on('ready', () => {
23+
node.on('ready', async () => {
2424
console.log('Ready')
25-
node.version()
26-
.then((version) => {
27-
console.log('Version:', version.version)
28-
})
25+
26+
try {
27+
const version = await node.version()
28+
console.log('Version:', version.version)
29+
2930
// Once we have the version, let's add a file to IPFS
30-
.then(() => {
31-
return node.add({
32-
path: 'data.txt',
33-
content: Buffer.from(require('crypto').randomBytes(1024 * 25))
34-
})
31+
const filesAdded = await node.add({
32+
path: 'data.txt',
33+
content: Buffer.from(require('crypto').randomBytes(1024 * 25))
3534
})
35+
console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash)
36+
3637
// Log out the added files metadata and cat the file from IPFS
37-
.then((filesAdded) => {
38-
console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash)
39-
return node.cat(filesAdded[0].hash)
40-
})
38+
const data = await node.cat(filesAdded[0].hash)
39+
4140
// Print out the files contents to console
42-
.then((data) => {
43-
console.log(`\nFetched file content containing ${data.byteLength} bytes`)
44-
})
45-
// Log out the error, if there is one
46-
.catch((err) => {
47-
console.log('File Processing Error:', err)
48-
})
49-
// After everything is done, shut the node down
50-
// We don't need to worry about catching errors here
51-
.then(() => {
52-
console.log('\n\nStopping the node')
53-
return node.stop()
54-
})
41+
console.log(`\nFetched file content containing ${data.byteLength} bytes`)
42+
} catch (err) {
43+
// Log out the error
44+
console.log('File Processing Error:', err)
45+
}
46+
// After everything is done, shut the node down
47+
// We don't need to worry about catching errors here
48+
console.log('\n\nStopping the node')
49+
return node.stop()
5550
})

examples/full-s3-repo/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"author": "",
1111
"license": "ISC",
1212
"dependencies": {
13-
"async": "^2.6.2",
1413
"aws-sdk": "^2.402.0",
1514
"datastore-s3": "../../",
1615
"ipfs": "~0.34.4",

examples/full-s3-repo/s3-lock.js

+40-40
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,24 @@ class S3Lock {
2727
* Creates the lock. This can be overriden to customize where the lock should be created
2828
*
2929
* @param {string} dir
30-
* @param {function(Error, LockCloser)} callback
31-
* @returns {void}
30+
* @returns {Promise<LockCloser>}
3231
*/
33-
lock (dir, callback) {
32+
async lock (dir) {
3433
const lockPath = this.getLockfilePath(dir)
3534

36-
this.locked(dir, (err, alreadyLocked) => {
37-
if (err || alreadyLocked) {
38-
return callback(new Error('The repo is already locked'))
39-
}
40-
41-
// There's no lock yet, create one
42-
this.s3.put(lockPath, Buffer.from(''), (err, data) => {
43-
if (err) {
44-
return callback(err, null)
45-
}
35+
let alreadyLocked, err
36+
try {
37+
alreadyLocked = await this.locked(dir)
38+
} catch (e) {
39+
err = e
40+
}
41+
if (err || alreadyLocked) {
42+
return callback(new Error('The repo is already locked'))
43+
}
4644

47-
callback(null, this.getCloser(lockPath))
48-
})
49-
})
45+
// There's no lock yet, create one
46+
const data = await this.s3.put(lockPath, Buffer.from('')).promise()
47+
return this.getCloser(lockPath)
5048
}
5149

5250
/**
@@ -61,21 +59,20 @@ class S3Lock {
6159
* Removes the lock. This can be overriden to customize how the lock is removed. This
6260
* is important for removing any created locks.
6361
*
64-
* @param {function(Error)} callback
65-
* @returns {void}
62+
* @returns {Promise}
6663
*/
67-
close: (callback) => {
68-
this.s3.delete(lockPath, (err) => {
69-
if (err && err.statusCode !== 404) {
70-
return callback(err)
64+
async close: () => {
65+
try {
66+
await this.s3.delete(lockPath).promise()
67+
} catch (err) {
68+
if (err.statusCode !== 404) {
69+
throw err
7170
}
72-
73-
callback(null)
74-
})
71+
}
7572
}
7673
}
7774

78-
const cleanup = (err) => {
75+
const cleanup = async (err) => {
7976
if (err instanceof Error) {
8077
console.log('\nAn Uncaught Exception Occurred:\n', err)
8178
} else if (err) {
@@ -84,10 +81,13 @@ class S3Lock {
8481

8582
console.log('\nAttempting to cleanup gracefully...')
8683

87-
closer.close(() => {
88-
console.log('Cleanup complete, exiting.')
89-
process.exit()
90-
})
84+
try {
85+
await closer.close()
86+
} catch (e) {
87+
console.log('Caught error cleaning up: %s', e.message)
88+
}
89+
console.log('Cleanup complete, exiting.')
90+
process.exit()
9191
}
9292

9393
// listen for graceful termination
@@ -103,19 +103,19 @@ class S3Lock {
103103
* Calls back on whether or not a lock exists. Override this method to customize how the check is made.
104104
*
105105
* @param {string} dir
106-
* @param {function(Error, boolean)} callback
107-
* @returns {void}
106+
* @returns {Promise<boolean>}
108107
*/
109-
locked (dir, callback) {
110-
this.s3.get(this.getLockfilePath(dir), (err, data) => {
111-
if (err && err.code === 'ERR_NOT_FOUND') {
112-
return callback(null, false)
113-
} else if (err) {
114-
return callback(err)
108+
async locked (dir) {
109+
try {
110+
await this.s3.get(this.getLockfilePath(dir)).promise()
111+
} catch (err) {
112+
if (err.code === 'ERR_NOT_FOUND') {
113+
return false
115114
}
115+
throw err
116+
}
116117

117-
callback(null, true)
118-
})
118+
return true
119119
}
120120
}
121121

package.json

+5-8
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,9 @@
3737
},
3838
"homepage": "https://github.com/ipfs/js-datastore-s3#readme",
3939
"dependencies": {
40-
"async": "^2.6.2",
41-
"datastore-core": "~0.6.0",
42-
"interface-datastore": "~0.6.0",
43-
"once": "^1.4.0",
44-
"pull-defer": "~0.2.3",
45-
"pull-stream": "^3.6.9",
40+
"datastore-core": "zcstarr/js-datastore-core",
41+
"interface-datastore": "git://github.com/ipfs/interface-datastore.git#refactor/async-iterators",
42+
"streaming-iterables": "^4.0.2",
4643
"upath": "^1.1.0"
4744
},
4845
"devDependencies": {
@@ -56,8 +53,8 @@
5653
"stand-in": "^4.2.0"
5754
},
5855
"peerDependencies": {
59-
"ipfs-repo": "0.x",
60-
"aws-sdk": "2.x"
56+
"aws-sdk": "2.x",
57+
"ipfs-repo": "0.x"
6158
},
6259
"contributors": [
6360
"Jacob Heun <[email protected]>",

0 commit comments

Comments
 (0)