ipfs-pubsub-1on1
Advanced tools
Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "ipfs-pubsub-1on1", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "1-to-1 communication channel over IPFS Pubsub between two peers", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -8,3 +8,3 @@ 'use strict' | ||
const waitForPeers = require('./wait-for-peers') | ||
const { getPeerID } = require('./get-peer-id') | ||
const getPeerID = require('./get-peer-id') | ||
@@ -15,3 +15,3 @@ /** | ||
class DirectChannel extends EventEmitter { | ||
constructor (ipfs, receiverID, options = { open: true }) { | ||
constructor (ipfs, receiverID) { | ||
super() | ||
@@ -26,34 +26,8 @@ | ||
// Setup IDs | ||
this._senderID = getPeerID(this._ipfs) | ||
this._receiverID = receiverID | ||
if (!this._senderID) { | ||
throw new Error('Sender ID was undefined') | ||
} | ||
if (!this._receiverID) { | ||
throw new Error('Receiver ID was undefined') | ||
} | ||
// Channel's participants | ||
this._peers = Array.from([this._senderID, this._receiverID]).sort() | ||
// ID of the channel is "<peer1 id>/<peer 2 id>"" | ||
this._id = path.join('/', PROTOCOL, this._peers.join('/')) | ||
// Function to use to handle incoming messages | ||
this._messageHandler = message => { | ||
// Make sure the message is coming from the correct peer | ||
const isValid = message && message.from === this._receiverID | ||
// Filter out all messages that didn't come from the second peer | ||
if (isValid) { | ||
this.emit('message', message) | ||
} | ||
} | ||
// Start communicating | ||
if (options.open || !options) { | ||
this._openChannel() | ||
} | ||
// See _setup() for more state initialization | ||
} | ||
@@ -98,9 +72,29 @@ | ||
async _setup () { | ||
this._senderID = await getPeerID(this._ipfs) | ||
// Channel's participants | ||
this._peers = Array.from([this._senderID, this._receiverID]).sort() | ||
// ID of the channel is "<peer1 id>/<peer 2 id>"" | ||
this._id = path.join('/', PROTOCOL, this._peers.join('/')) | ||
// Function to use to handle incoming messages | ||
this._messageHandler = message => { | ||
// Make sure the message is coming from the correct peer | ||
const isValid = message && message.from === this._receiverID | ||
// Filter out all messages that didn't come from the second peer | ||
if (isValid) { | ||
this.emit('message', message) | ||
} | ||
} | ||
} | ||
async _openChannel () { | ||
await this._setup() | ||
await this._ipfs.pubsub.subscribe(this._id, this._messageHandler) | ||
} | ||
static async open (ipfs, receiverID, options) { | ||
const opts = Object.assign({}, options, { open: false }) | ||
const channel = new DirectChannel(ipfs, receiverID, opts) | ||
static async open (ipfs, receiverID) { | ||
const channel = new DirectChannel(ipfs, receiverID) | ||
await channel._openChannel() | ||
@@ -107,0 +101,0 @@ return channel |
'use strict' | ||
const getPeerID = (ipfs) => { | ||
return ipfs._peerInfo.id._idB58String || ipfs._peerInfo.id | ||
const getPeerID = async (ipfs) => { | ||
const peerInfo = await ipfs.id() | ||
return peerInfo.id | ||
} | ||
exports.getPeerID = getPeerID | ||
module.exports = getPeerID |
@@ -16,5 +16,9 @@ 'use strict' | ||
const interval = setInterval(async () => { | ||
if (await checkPeers()) { | ||
clearInterval(interval) | ||
resolve() | ||
try { | ||
if (await checkPeers()) { | ||
clearInterval(interval) | ||
resolve() | ||
} | ||
} catch (e) { | ||
reject(e) | ||
} | ||
@@ -21,0 +25,0 @@ }, 100) |
@@ -14,3 +14,4 @@ 'use strict' | ||
const Channel = require('../src/direct-channel.js') | ||
const Channel = require('../src/direct-channel') | ||
const getPeerID = require('../src/get-peer-id') | ||
const PROTOCOL = require('../src/protocol') | ||
@@ -40,5 +41,5 @@ | ||
ipfs3 = instances[2] | ||
id1 = ipfs1._peerInfo.id._idB58String | ||
id2 = ipfs2._peerInfo.id._idB58String | ||
id3 = ipfs3._peerInfo.id._idB58String | ||
id1 = await getPeerID(ipfs1) | ||
id2 = await getPeerID(ipfs2) | ||
id3 = await getPeerID(ipfs3) | ||
// Note, we only create channels between peer1 and peer2 in these test, | ||
@@ -56,3 +57,3 @@ // peer3 is used for "external actor" tests | ||
it('has two participants', async () => { | ||
const c = new Channel(ipfs1, ipfs2._peerInfo.id._idB58String) | ||
const c = await Channel.open(ipfs1, id2) | ||
assert.deepEqual(c.peers, expectedPeerIDs) | ||
@@ -64,3 +65,3 @@ c.close() | ||
const expectedID = path.join('/', PROTOCOL, expectedPeerIDs.join('/')) | ||
const c = new Channel(ipfs1, id2) | ||
const c = await Channel.open(ipfs1, id2) | ||
assert.deepEqual(c.id, expectedID) | ||
@@ -71,4 +72,4 @@ c.close() | ||
it('has two peers', async () => { | ||
const c1 = new Channel(ipfs1, id2) | ||
const c2 = new Channel(ipfs2, id1) | ||
const c1 = await Channel.open(ipfs1, id2) | ||
const c2 = await Channel.open(ipfs2, id1) | ||
assert.deepEqual(c1.peers, expectedPeerIDs) | ||
@@ -229,3 +230,3 @@ assert.deepEqual(c2.peers, expectedPeerIDs) | ||
try { | ||
c = new Channel({}, id2) | ||
c = await Channel.open({}, id2) | ||
} catch (e) { | ||
@@ -237,2 +238,13 @@ err = e | ||
}) | ||
it('throws an error if receiver ID was not given', async () => { | ||
let c, err | ||
try { | ||
c = await Channel.open(ipfs1) | ||
} catch (e) { | ||
err = e | ||
} | ||
assert.equal(err, 'Error: Receiver ID was undefined') | ||
}) | ||
}) | ||
@@ -239,0 +251,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14740
432