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

callbacks -> async / await #17

Merged
merged 3 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ stages:

node_js:
- '10'
- '12'

os:
- linux
Expand All @@ -20,7 +21,6 @@ jobs:
include:
- stage: check
script:
- npx aegir commitlint --travis
- npx aegir dep-check
- npm run lint

Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai/)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
[![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)
[![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)
[![Dependency Status](https://david-dm.org/diasdavid/js-peer-id.svg?style=flat-square)](https://david-dm.org/ipfs/js-datastore-s3)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square)

> Datastore implementation backed by s3.

Expand Down
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
24 changes: 10 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"release": "aegir release --target node --docs",
"release-minor": "aegir release --type minor --target node --docs",
"release-major": "aegir release --type major --target node --docs",
"coverage": "aegir coverage --timeout 10000",
"coverage-publish": "aegir coverage --provider codecov --timeout 10000",
"coverage": "nyc --reporter=text --reporter=lcov npm run test:node",
"docs": "aegir docs"
},
"repository": {
Expand All @@ -37,27 +36,24 @@
},
"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": "^0.7.0",
"interface-datastore": "^0.7.0",
"streaming-iterables": "^4.1.0",
"upath": "^1.1.0"
},
"devDependencies": {
"aegir": "^18.1.0",
"aws-sdk": "^2.402.0",
"aegir": "^20.0.0",
"aws-sdk": "^2.510.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"flow-bin": "~0.93.0",
"flow-bin": "^0.93.0",
"flow-typed": "^2.5.1",
"ipfs-repo": "~0.26.1",
"ipfs-repo": "^0.27.0",
"stand-in": "^4.2.0"
},
"peerDependencies": {
"ipfs-repo": "0.x",
"aws-sdk": "2.x"
"aws-sdk": "2.x",
"ipfs-repo": "^0.27.0"
},
"contributors": [
"Jacob Heun <jacobheun@gmail.com>",
Expand Down
Loading