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

Commit 629dba7

Browse files
dirkmcjacobheun
authored andcommitted
refactor: callbacks -> async / await (#17)
* chore: callbacks -> async / await BREAKING CHANGE: All places in the API that used callbacks are now replaced with async/await
1 parent 2301594 commit 629dba7

File tree

10 files changed

+275
-383
lines changed

10 files changed

+275
-383
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ stages:
77

88
node_js:
99
- '10'
10+
- '12'
1011

1112
os:
1213
- linux
@@ -20,7 +21,6 @@ jobs:
2021
include:
2122
- stage: check
2223
script:
23-
- npx aegir commitlint --travis
2424
- npx aegir dep-check
2525
- npm run lint
2626

README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai/)
44
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
55
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
6-
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
76
[![Build Status](https://travis-ci.com/ipfs/js-datastore-s3.svg)](https://travis-ci.com/ipfs/js-datastore-s3) [![codecov](https://codecov.io/gh/ipfs/js-datastore-s3/branch/master/graph/badge.svg)](https://codecov.io/gh/ipfs/js-datastore-s3)
8-
[![Coverage Status](https://coveralls.io/repos/github/ipfs/js-datastore-s3/badge.svg?branch=master)](https://coveralls.io/github/ipfs/js-datastore-s3?branch=master) [![Dependency Status](https://david-dm.org/diasdavid/js-peer-id.svg?style=flat-square)](https://david-dm.org/ipfs/js-datastore-s3)
7+
[![Dependency Status](https://david-dm.org/diasdavid/js-peer-id.svg?style=flat-square)](https://david-dm.org/ipfs/js-datastore-s3)
98
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
10-
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
11-
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square)
129

1310
> Datastore implementation backed by s3.
1411

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

+10-14
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
"release": "aegir release --target node --docs",
1717
"release-minor": "aegir release --type minor --target node --docs",
1818
"release-major": "aegir release --type major --target node --docs",
19-
"coverage": "aegir coverage --timeout 10000",
20-
"coverage-publish": "aegir coverage --provider codecov --timeout 10000",
19+
"coverage": "nyc --reporter=text --reporter=lcov npm run test:node",
2120
"docs": "aegir docs"
2221
},
2322
"repository": {
@@ -37,27 +36,24 @@
3736
},
3837
"homepage": "https://github.com/ipfs/js-datastore-s3#readme",
3938
"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",
39+
"datastore-core": "^0.7.0",
40+
"interface-datastore": "^0.7.0",
41+
"streaming-iterables": "^4.1.0",
4642
"upath": "^1.1.0"
4743
},
4844
"devDependencies": {
49-
"aegir": "^18.1.0",
50-
"aws-sdk": "^2.402.0",
45+
"aegir": "^20.0.0",
46+
"aws-sdk": "^2.510.0",
5147
"chai": "^4.2.0",
5248
"dirty-chai": "^2.0.1",
53-
"flow-bin": "~0.93.0",
49+
"flow-bin": "^0.93.0",
5450
"flow-typed": "^2.5.1",
55-
"ipfs-repo": "~0.26.1",
51+
"ipfs-repo": "^0.27.0",
5652
"stand-in": "^4.2.0"
5753
},
5854
"peerDependencies": {
59-
"ipfs-repo": "0.x",
60-
"aws-sdk": "2.x"
55+
"aws-sdk": "2.x",
56+
"ipfs-repo": "^0.27.0"
6157
},
6258
"contributors": [
6359
"Jacob Heun <[email protected]>",

0 commit comments

Comments
 (0)