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

Commit

Permalink
chore: callbacks -> async / await
Browse files Browse the repository at this point in the history
BREAKING CHANGE: All places in the API that used callbacks are now replaced with async/await
  • Loading branch information
dirkmc authored and jacobheun committed Aug 15, 2019
1 parent 2301594 commit d5c0758
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 368 deletions.
49 changes: 22 additions & 27 deletions examples/full-s3-repo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,31 @@ let node = new IPFS({
console.log('Start the node')

// Test out the repo by sending and fetching some data
node.on('ready', () => {
node.on('ready', async () => {
console.log('Ready')
node.version()
.then((version) => {
console.log('Version:', version.version)
})

try {
const version = await node.version()
console.log('Version:', version.version)

// Once we have the version, let's add a file to IPFS
.then(() => {
return node.add({
path: 'data.txt',
content: Buffer.from(require('crypto').randomBytes(1024 * 25))
})
const filesAdded = await node.add({
path: 'data.txt',
content: Buffer.from(require('crypto').randomBytes(1024 * 25))
})
console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash)

// Log out the added files metadata and cat the file from IPFS
.then((filesAdded) => {
console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash)
return node.cat(filesAdded[0].hash)
})
const data = await node.cat(filesAdded[0].hash)

// Print out the files contents to console
.then((data) => {
console.log(`\nFetched file content containing ${data.byteLength} bytes`)
})
// Log out the error, if there is one
.catch((err) => {
console.log('File Processing Error:', err)
})
// After everything is done, shut the node down
// We don't need to worry about catching errors here
.then(() => {
console.log('\n\nStopping the node')
return node.stop()
})
console.log(`\nFetched file content containing ${data.byteLength} bytes`)
} catch (err) {
// Log out the error
console.log('File Processing Error:', err)
}
// After everything is done, shut the node down
// We don't need to worry about catching errors here
console.log('\n\nStopping the node')
return node.stop()
})
1 change: 0 additions & 1 deletion examples/full-s3-repo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"async": "^2.6.2",
"aws-sdk": "^2.402.0",
"datastore-s3": "../../",
"ipfs": "~0.34.4",
Expand Down
80 changes: 40 additions & 40 deletions examples/full-s3-repo/s3-lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,24 @@ class S3Lock {
* Creates the lock. This can be overriden to customize where the lock should be created
*
* @param {string} dir
* @param {function(Error, LockCloser)} callback
* @returns {void}
* @returns {Promise<LockCloser>}
*/
lock (dir, callback) {
async lock (dir) {
const lockPath = this.getLockfilePath(dir)

this.locked(dir, (err, alreadyLocked) => {
if (err || alreadyLocked) {
return callback(new Error('The repo is already locked'))
}

// There's no lock yet, create one
this.s3.put(lockPath, Buffer.from(''), (err, data) => {
if (err) {
return callback(err, null)
}
let alreadyLocked, err
try {
alreadyLocked = await this.locked(dir)
} catch (e) {
err = e
}
if (err || alreadyLocked) {
return callback(new Error('The repo is already locked'))
}

callback(null, this.getCloser(lockPath))
})
})
// There's no lock yet, create one
const data = await this.s3.put(lockPath, Buffer.from('')).promise()
return this.getCloser(lockPath)
}

/**
Expand All @@ -61,21 +59,20 @@ class S3Lock {
* Removes the lock. This can be overriden to customize how the lock is removed. This
* is important for removing any created locks.
*
* @param {function(Error)} callback
* @returns {void}
* @returns {Promise}
*/
close: (callback) => {
this.s3.delete(lockPath, (err) => {
if (err && err.statusCode !== 404) {
return callback(err)
async close: () => {
try {
await this.s3.delete(lockPath).promise()
} catch (err) {
if (err.statusCode !== 404) {
throw err
}

callback(null)
})
}
}
}

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

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

closer.close(() => {
console.log('Cleanup complete, exiting.')
process.exit()
})
try {
await closer.close()
} catch (e) {
console.log('Caught error cleaning up: %s', e.message)
}
console.log('Cleanup complete, exiting.')
process.exit()
}

// listen for graceful termination
Expand All @@ -103,19 +103,19 @@ class S3Lock {
* Calls back on whether or not a lock exists. Override this method to customize how the check is made.
*
* @param {string} dir
* @param {function(Error, boolean)} callback
* @returns {void}
* @returns {Promise<boolean>}
*/
locked (dir, callback) {
this.s3.get(this.getLockfilePath(dir), (err, data) => {
if (err && err.code === 'ERR_NOT_FOUND') {
return callback(null, false)
} else if (err) {
return callback(err)
async locked (dir) {
try {
await this.s3.get(this.getLockfilePath(dir)).promise()
} catch (err) {
if (err.code === 'ERR_NOT_FOUND') {
return false
}
throw err
}

callback(null, true)
})
return true
}
}

Expand Down
13 changes: 5 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@
},
"homepage": "https://github.com/ipfs/js-datastore-s3#readme",
"dependencies": {
"async": "^2.6.2",
"datastore-core": "~0.6.0",
"interface-datastore": "~0.6.0",
"once": "^1.4.0",
"pull-defer": "~0.2.3",
"pull-stream": "^3.6.9",
"datastore-core": "zcstarr/js-datastore-core",
"interface-datastore": "git://github.com/ipfs/interface-datastore.git#refactor/async-iterators",
"streaming-iterables": "^4.0.2",
"upath": "^1.1.0"
},
"devDependencies": {
Expand All @@ -56,8 +53,8 @@
"stand-in": "^4.2.0"
},
"peerDependencies": {
"ipfs-repo": "0.x",
"aws-sdk": "2.x"
"aws-sdk": "2.x",
"ipfs-repo": "0.x"
},
"contributors": [
"Jacob Heun <jacobheun@gmail.com>",
Expand Down
Loading

0 comments on commit d5c0758

Please sign in to comment.