Skip to content
Open
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
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "stable"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ipfs-friends

[![Build Status](https://travis-ci.org/tableflip/ipfs-friends.svg?branch=master)](https://travis-ci.org/tableflip/ipfs-friends) [![dependencies Status](https://david-dm.org/tableflip/ipfs-friends/status.svg)](https://david-dm.org/tableflip/ipfs-friends) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

> get by with a little help from your friends (to share files)

Turn IPFS peers in to file sharing friends.
Expand Down
47 changes: 35 additions & 12 deletions lib/friends.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,32 @@ class FriendDaemon extends EventEmitter {
this._index = null
this.onMessage = this.onMessage.bind(this)
this._options = options || {}
this._state = 'stopped'
}

async start () {
const ipfs = this._ipfs
const { id } = await ipfs.id()
const index = await readFriends(ipfs, { dir: this._options.dataDir })
this.peerId = id
this._index = index
const peerIds = Object.keys(index)
await Promise.all(peerIds.map(peerId => {
return ipfs.pubsub.subscribe(peerId, this.onMessage)
}))
this._publish({ action: 'online' })
.catch(err => log('failed to publish online message', err))
if (this._state !== 'stopped') throw new Error(`unable to start from ${this._state}`)
this._state = 'starting'

try {
const ipfs = this._ipfs
const { id } = await ipfs.id()
const index = await readFriends(ipfs, { dir: this._options.dataDir })
this.peerId = id
this._index = index
const peerIds = Object.keys(index)
await Promise.all(peerIds.map(peerId => {
return ipfs.pubsub.subscribe(peerId, this.onMessage)
}))
this._publish({ action: 'online' })
.catch(err => log('failed to publish online message', err))
this._state = 'started'
} catch (err) {
this._state = 'stopped'
throw err
}

return this
}

async onMessage (msg) {
Expand Down Expand Up @@ -83,6 +95,7 @@ class FriendDaemon extends EventEmitter {
}

async add (peerId, name) {
if (this._state !== 'started') throw new Error('not started')
if (!peerId) throw new Error('missing friend peer ID')
if (!name) throw new Error('missing friend name')
validateCid(peerId)
Expand All @@ -97,6 +110,7 @@ class FriendDaemon extends EventEmitter {
}

async share (cid, name) {
if (this._state !== 'started') throw new Error('not started')
if (!cid) throw new Error('missing share CID')
if (!name) throw new Error('missing share name')
validateCid(cid)
Expand All @@ -112,6 +126,7 @@ class FriendDaemon extends EventEmitter {
}

async dump (peerIdOrName) {
if (this._state !== 'started') throw new Error('not started')
if (!peerIdOrName) throw new Error('missing friend peer ID or name')

const index = this._index
Expand All @@ -134,6 +149,9 @@ class FriendDaemon extends EventEmitter {
}

async stop () {
if (this._state !== 'started') throw new Error('not started')
this._state = 'stopping'

const peerIds = Object.keys(this._index)
// Unsubscribe from all our friends
try {
Expand All @@ -153,13 +171,18 @@ class FriendDaemon extends EventEmitter {
}

this.peerId = null
this._ipfs = null
this._state = 'stopped'
return this
}

_publish (msg) {
const { peerId, _ipfs: ipfs } = this
return ipfs.pubsub.publish(peerId, Buffer.from(JSON.stringify(msg)))
}

state () {
return this._state
}
}

module.exports = FriendDaemon
Expand Down
Loading