Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/async await #199

Merged
merged 21 commits into from
Jun 4, 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ docs
yarn.lock
package-lock.json

test/test-repo-for*
test-repo-for*
.vscode
.eslintrc
# Logs
Expand All @@ -21,6 +21,7 @@ lib-cov

# Coverage directory used by tools like istanbul
coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
Expand All @@ -35,3 +36,4 @@ build
node_modules

dist

103 changes: 42 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,9 @@ Example:
const Repo = require('ipfs-repo')
const repo = new Repo('/tmp/ipfs-repo')

repo.init({ cool: 'config' }, (err) => {
if (err) {
throw err
}

repo.open((err) => {
if (err) {
throw err
}

console.log('repo is ready')
})
})
await repo.init({ cool: 'config' })
await repo.open()
console.log('repo is ready')
```

This now has created the following structure, either on disk or as an in memory representation:
Expand Down Expand Up @@ -157,63 +147,61 @@ Arguments:
const repo = new Repo('path/to/repo')
```

#### `repo.init (callback)`
#### `Promise repo.init ()`

Creates the necessary folder structure inside the repo.

#### `repo.open (callback)`
#### `Promise repo.open ()`

[Locks](https://en.wikipedia.org/wiki/Record_locking) the repo to prevent conflicts arising from simultaneous access.

#### `repo.close (callback)`
#### `Promise repo.close ()`

Unlocks the repo.

#### `repo.exists (callback)`
#### `Promise<boolean> repo.exists ()`

Tells whether this repo exists or not. Calls back with `(err, bool)`.
Tells whether this repo exists or not. Returned promise resolves to a `boolean`.

### Repos

Root repo:

#### `repo.put (key, value:Buffer, callback)`
#### `Promise repo.put (key, value:Buffer)`

Put a value at the root of the repo.

* `key` can be a buffer, a string or a [Key](https://github.com/ipfs/interface-datastore#keys).

#### `repo.get (key, callback)`
#### `Promise<Buffer> repo.get (key)`

Get a value at the root of the repo.

* `key` can be a buffer, a string or a [Key](https://github.com/ipfs/interface-datastore#keys).
* `callback` is a callback function `function (err, result:Buffer)`

[Blocks](https://github.com/ipfs/js-ipfs-block#readme):

#### `repo.blocks.put (block:Block, callback)`
#### `Promise repo.blocks.put (block:Block)`

* `block` should be of type [Block](https://github.com/ipfs/js-ipfs-block#readme).

#### `repo.blocks.putMany (blocks, callback)`
#### `Promise repo.blocks.putMany (blocks)`

Put many blocks.

* `block` should be an array of type [Block](https://github.com/ipfs/js-ipfs-block#readme).

#### `repo.blocks.get (cid, callback)`
#### `Promise<Buffer> repo.blocks.get (cid)`

Get block.

* `cid` is the content id of [type CID](https://github.com/ipld/js-cid#readme).
* `callback` is a callback function `function (err, result:Buffer)`

Datastore:

#### `repo.datastore`

This is contains a full implementation of [the `interface-datastore` API](https://github.com/ipfs/interface-datastore#api).
This contains a full implementation of [the `interface-datastore` API](https://github.com/ipfs/interface-datastore#api).


### Utils
Expand All @@ -222,77 +210,70 @@ This is contains a full implementation of [the `interface-datastore` API](https:

Instead of using `repo.set('config')` this exposes an API that allows you to set and get a decoded config object, as well as, in a safe manner, change any of the config values individually.

##### `repo.config.set(key:string, value, callback)`
##### `Promise repo.config.set(key:string, value)`

Set a config value. `value` can be any object that is serializable to JSON.

* `key` is a string specifying the object path. Example:

```js
repo.config.set('a.b.c', 'c value', (err) => {
if (err) { throw err }
repo.config.get((err, config) => {
if (err) { throw err }
assert.equal(config.a.b.c, 'c value')
})
})
await repo.config.set('a.b.c', 'c value')
const config = await repo.config.get()
assert.equal(config.a.b.c, 'c value')
```

##### `repo.config.get(value, callback)`
##### `Promise repo.config.set(value)`

Set the whole config value. `value` can be any object that is serializable to JSON.

##### `repo.config.get(key:string, callback)`
##### `Promise<?> repo.config.get(key:string)`

Get a config value. `callback` is a function with the signature: `function (err, value)`, wehre the `
value` is of the same type that was set before.
Get a config value. Returned promise resolves to the same type that was set before.

* `key` is a string specifying the object path. Example:

```js
repo.config.get('a.b.c', (err, value) => {
if (err) { throw err }
console.log('config.a.b.c = ', value)
})
const value = await repo.config.get('a.b.c')
console.log('config.a.b.c = ', value)
```

##### `repo.config.get(callback)`
##### `Promise<Object> repo.config.get()`

Get the entire config value. `callback` is a function with the signature: `function (err, configValue:Object)`.
Get the entire config value.

#### `repo.config.exists(callback)`
#### `Promise<boolean> repo.config.exists()`

Whether the config sub-repo exists. Calls back with `(err, bool)`.
Whether the config sub-repo exists.

#### `repo.version`

##### `repo.version.get (callback)`
##### `Promise<Number> repo.version.get ()`

Gets the repo version.
Gets the repo version (an integer).

##### `repo.version.set (version:number, callback)`
##### `Promise repo.version.set (version:Number)`

Sets the repo version

#### `repo.apiAddr`

#### `repo.apiAddr.get (callback)`
#### `Promise<String> repo.apiAddr.get ()`

Gets the API address.

#### `repo.apiAddr.set (value, callback)`
#### `Promise repo.apiAddr.set (value)`

Sets the API address.

* `value` should be a [Multiaddr](https://github.com/multiformats/js-multiaddr) or a String representing a valid one.

### `repo.stat ([options], callback)`
### `Promise<Object> repo.stat ([options])`

Gets the repo status.

`options` is an object which might contain the key `human`, which is a boolean indicating whether or not the `repoSize` should be displayed in MiB or not.

`callback` is a function with the signature `function (err, stats)`, where `stats` is an Object with the following keys:
Returned promise resolves to an `Object` with the following keys:

- `numObjects`
- `repoPath`
Expand All @@ -311,27 +292,27 @@ const memoryLock = require('ipfs-repo/src/lock-memory') // Default in browser

You can also provide your own custom Lock. It must be an object with the following interface:

#### `lock.lock (dir, callback)`
#### `Promise lock.lock (dir)`

Sets the lock if one does not already exist. If a lock already exists, `callback` should be called with an error.
Sets the lock if one does not already exist. If a lock already exists, should throw an error.

`dir` is a string to the directory the lock should be created at. The repo typically creates the lock at its root.

`callback` is a function with the signature `function (err, closer)`, where `closer` has a `close` method for removing the lock.
Returns `closer`, where `closer` has a `close` method for removing the lock.

##### `closer.close (callback)`
##### `Promise closer.close ()`

Closes the lock created by `lock.open`

`callback` is a function with the signature `function (err)`. If no error was returned, the lock was successfully removed.
If no error was thrown, the lock was successfully removed.

#### `lock.locked (dir, callback)`
#### `Promise<boolean> lock.locked (dir)`

Checks the existence of the lock.

`dir` is a string to the directory to check for the lock. The repo typically checks for the lock at its root.
`dir` is the path to the directory to check for the lock. The repo typically checks for the lock at its root.

`callback` is a function with the signature `function (err, boolean)`, where `boolean` indicates the existence of the lock.
Returned promise resolves to a `boolean` indicating the existence of the lock.

## Notes

Expand Down
21 changes: 7 additions & 14 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
'use strict'

const Repo = require('ipfs-repo')
const repo = new Repo('/Users/awesome/.jsipfs')
const Repo = require('ipfs-repo');

repo.init({ my: 'config' }, (err) => {
if (err) {
throw err
}
(async () => {
const repo = new Repo('/Users/awesome/.jsipfs')

repo.open((err) => {
if (err) {
throw err
}

console.log('repo is ready')
})
})
await repo.init({ my: 'config' })
await repo.open()
console.log('repo is ready') // eslint-disable-line no-console
})()
38 changes: 19 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
},
"scripts": {
"test": "aegir test",
"test:node": "aegir test --target node",
"test:browser": "aegir test --target browser",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser",
"test:webworker": "aegir test -t webworker",
"build": "aegir build",
"lint": "aegir lint",
"release": "aegir release --docs",
"release-minor": "aegir release --type minor --docs",
"release-major": "aegir release --type major --docs",
"coverage": "aegir coverage",
"coverage-publish": "aegir-coverage --provider coveralls",
"coverage": "nyc -s npm run test:node && nyc report --reporter=html",
"dep-check": "aegir dep-check",
"docs": "aegir docs"
},
"repository": {
Expand All @@ -38,38 +39,37 @@
],
"homepage": "https://github.com/ipfs/js-ipfs-repo",
"engines": {
"node": ">=6.0.0",
"node": ">=10.0.0",
"npm": ">=3.0.0"
},
"devDependencies": {
"aegir": "^18.2.1",
"aegir": "^19.0.3",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"lodash": "^4.17.11",
"memdown": "^3.0.0",
"memdown": "^4.0.0",
"multihashes": "~0.4.14",
"multihashing-async": "~0.5.2",
"multihashing-async": "~0.7.0",
"ncp": "^2.0.0",
"rimraf": "^2.6.3"
},
"dependencies": {
"async": "^2.6.2",
"base32.js": "~0.1.0",
"bignumber.js": "^8.1.1",
"buffer": "^5.2.1",
"bignumber.js": "^9.0.0",
"cids": "~0.7.0",
"datastore-core": "~0.6.0",
"datastore-fs": "~0.8.0",
"datastore-level": "~0.11.0",
"datastore-core": "~0.7.0",
"datastore-fs": "~0.9.0",
"datastore-level": "~0.12.0",
"debug": "^4.1.0",
"dlv": "^1.1.2",
"interface-datastore": "~0.6.0",
"err-code": "^1.1.2",
"interface-datastore": "~0.7.0",
"ipfs-block": "~0.8.1",
"just-safe-get": "^1.3.0",
"just-safe-set": "^2.1.0",
"multiaddr": "^6.0.6",
"lodash.has": "^4.5.2",
"p-queue": "^5.0.0",
"proper-lockfile": "^4.0.0",
"pull-stream": "^3.6.9",
"sort-keys": "^2.0.0"
"sort-keys": "^3.0.0"
},
"license": "MIT",
"contributors": [
Expand Down
22 changes: 10 additions & 12 deletions src/api-addr.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,28 @@ module.exports = (store) => {
/**
* Get the current configuration from the repo.
*
* @param {function(Error, Object)} callback
* @returns {void}
* @returns {Promise<String>}
*/
get (callback) {
store.get(apiFile, (err, value) => callback(err, value && value.toString()))
async get () {
const value = await store.get(apiFile)
return value && value.toString()
},
/**
* Set the current configuration for this repo.
*
* @param {Object} value - the api address to be written
* @param {function(Error)} callback
* @returns {void}
* @returns {Promise<?>}
*/
set (value, callback) {
store.put(apiFile, Buffer.from(value.toString()), callback)
async set (value) { // eslint-disable-line require-await
return store.put(apiFile, Buffer.from(value.toString()))
},
/**
* Deletes api file
*
* @param {function(Error, bool)} callback
* @returns {void}
* @returns {Promise<void>}
*/
delete (callback) {
store.delete(apiFile, callback)
async delete () { // eslint-disable-line require-await
return store.delete(apiFile)
}
}
}
Loading