Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
test: add tests and docs
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
Alan Shaw authored and alanshaw committed Jul 17, 2019
1 parent 3a1b129 commit ecc5f6f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 33 deletions.
42 changes: 16 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,10 @@ To create an IPFS node programmatically:

```js
const IPFS = require('ipfs')
const node = new IPFS()
const node = await IPFS.create()

node.on('ready', () => {
// Ready to use!
// See https://github.com/ipfs/js-ipfs#core-api
})
// Ready to use!
// See https://github.com/ipfs/js-ipfs#core-api
```

### Through command line tool
Expand All @@ -124,8 +122,7 @@ The CLI is available by using the command `jsipfs` in your terminal. This is ali

Learn how to bundle with browserify and webpack in the [`examples`](https://github.com/ipfs/js-ipfs/tree/master/examples) folder.


You can also load it using a `<script>` using the [unpkg](https://unpkg.com) CDN **or** the [jsDelivr](https://www.jsdelivr.com/package/npm/ipfs) CDN. Inserting one of the following lines will make a `Ipfs` object available in the global namespace.
You can also load it using a `<script>` using the [unpkg](https://unpkg.com) CDN **or** the [jsDelivr](https://www.jsdelivr.com/package/npm/ipfs) CDN. Inserting one of the following lines will make an `Ipfs` object available in the global namespace.

```html
<!-- loading the minified version using unpkg -->
Expand All @@ -148,20 +145,20 @@ Inserting one of the above lines will make an `Ipfs` object available in the glo

```html
<script>
const node = new window.Ipfs()
node.on('ready', () => {
async function main () {
const node = await window.Ipfs.create()
// Ready to use!
// See https://github.com/ipfs/js-ipfs#core-api
})
}
main()
</script>
```

## Usage

### IPFS CLI

The `jsipfs` CLI, available when `js-ipfs` is installed globally, follows(should, it is a WIP) the same interface defined by `go-ipfs`, you can always use the `help` command for help menus.
The `jsipfs` CLI, available when `js-ipfs` is installed globally, follows (should, it is a WIP) the same interface defined by `go-ipfs`, you can always use the `help` command for help menus.

```sh
# Install js-ipfs globally
Expand Down Expand Up @@ -191,20 +188,14 @@ If you want a programmatic way to spawn a IPFS Daemon using JavaScript, check ou

### IPFS Module

Use the IPFS Module as a dependency of a project to __spawn in process instances of IPFS__. Create an instance by calling `new IPFS()` and waiting for its `ready` event:
Use the IPFS Module as a dependency of a project to __spawn in process instances of IPFS__. Create an instance by calling `await IPFS.create()`:

```js
// Create the IPFS node instance
const node = new IPFS()

node.on('ready', () => {
// Your node is now ready to use \o/

// stopping a node
node.stop(() => {
// node is now 'offline'
})
})
const node = await IPFS.create()
// Your node is now ready to use \o/
await node.stop()
// node is now 'offline'
```

### [Tutorials and Examples](/examples)
Expand All @@ -216,12 +207,11 @@ You can find some examples and tutorials in the [examples](/examples) folder, th
#### IPFS Constructor

```js
const node = new IPFS([options])
const node = await IPFS.create([options])
```

Creates and returns an instance of an IPFS node. Use the `options` argument to specify advanced configuration. It is an object with any of these properties:


##### `options.repo`

| Type | Default |
Expand All @@ -234,7 +224,7 @@ Example:

```js
// Store data outside your user directory
const node = new IPFS({ repo: '/var/ipfs/data' })
const node = await IPFS.create({ repo: '/var/ipfs/data' })
```

##### `options.init`
Expand Down
14 changes: 7 additions & 7 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,14 @@ class IPFS extends EventEmitter {
this.state = require('./state')(this)

boot(this)
this.once('ready', () => { this.__ready = true })
this.once('ready', () => { this._ready = true })
}

get ready () {
return new Promise((resolve, reject) => {
if (this.__ready) return resolve(this)
this.on('ready', () => resolve(this))
this.on('error', reject)
if (this._ready) return resolve(this)
this.once('ready', () => resolve(this))
this.once('error', reject)
})
}
}
Expand All @@ -163,7 +164,6 @@ module.exports.createNode = (options) => {
return new IPFS(options)
}

exports.create = (options) => {
let node = new IPFS(options)
return node.ready
module.exports.create = (options) => {
return new IPFS(options).ready
}
82 changes: 82 additions & 0 deletions test/core/create-node.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,88 @@ describe('create node', function () {
})
})

it('should resolve ready promise when initialized not started', async () => {
const ipfs = new IPFS({
init: true,
start: false,
repo: tempRepo,
config: { Addresses: { Swarm: [] } }
})

expect(ipfs.isOnline()).to.be.false()
await ipfs.ready
expect(ipfs.isOnline()).to.be.false()
})

it('should resolve ready promise when not initialized and not started', async () => {
const ipfs = new IPFS({
init: false,
start: false,
repo: tempRepo,
config: { Addresses: { Swarm: [] } }
})

expect(ipfs.isOnline()).to.be.false()
await ipfs.ready
expect(ipfs.isOnline()).to.be.false()
})

it('should resolve ready promise when initialized and started', async () => {
const ipfs = new IPFS({
init: true,
start: true,
repo: tempRepo,
config: { Addresses: { Swarm: [] } }
})

expect(ipfs.isOnline()).to.be.false()
await ipfs.ready
expect(ipfs.isOnline()).to.be.true()
await ipfs.stop()
})

it('should resolve ready promise when already ready', async () => {
const ipfs = new IPFS({
repo: tempRepo,
config: { Addresses: { Swarm: [] } }
})

expect(ipfs.isOnline()).to.be.false()
await ipfs.ready
expect(ipfs.isOnline()).to.be.true()
await ipfs.ready
expect(ipfs.isOnline()).to.be.true()
await ipfs.stop()
})

it('should reject ready promise on boot error', async () => {
const ipfs = new IPFS({
repo: tempRepo,
init: { bits: 1 }, // Too few bits will cause error on boot
config: { Addresses: { Swarm: [] } }
})

expect(ipfs.isOnline()).to.be.false()

try {
await ipfs.ready
} catch (err) {
return expect(ipfs.isOnline()).to.be.false()
}

throw new Error('ready promise did not reject')
})

it('should create a ready node with IPFS.create', async () => {
const ipfs = await IPFS.create({
repo: tempRepo,
config: { Addresses: { Swarm: [] } }
})

expect(ipfs.isOnline()).to.be.true()
await ipfs.stop()
})

it('init: { bits: 1024 }', function (done) {
this.timeout(80 * 1000)

Expand Down

0 comments on commit ecc5f6f

Please sign in to comment.