ipfs-pubsub-1on1
Advanced tools
Comparing version 0.0.8 to 0.1.1-4e113e9.0
{ | ||
"name": "ipfs-pubsub-1on1", | ||
"version": "0.0.8", | ||
"version": "0.1.1-4e113e9.0", | ||
"description": "1-to-1 communication channel over IPFS Pubsub between two peers", | ||
"license": "MIT", | ||
"author": "Haad", | ||
"type": "module", | ||
"main": "src/direct-channel.js", | ||
@@ -13,10 +14,18 @@ "dependencies": { | ||
"mocha": "^8.1.3", | ||
"orbit-db-test-utils": "^0.11.1", | ||
"orbit-db-test-utils": "^3.0.0", | ||
"p-map-series": "^2.1.0", | ||
"rimraf": "^3.0.2" | ||
"rimraf": "^3.0.2", | ||
"standard": "^17.0.0" | ||
}, | ||
"scripts": { | ||
"test": "TEST=all mocha", | ||
"test:coverage": "istanbul cover _mocha" | ||
"test:coverage": "istanbul cover _mocha", | ||
"lint": "standard", | ||
"lint:fix": "standard --fix" | ||
}, | ||
"standard": { | ||
"env": [ | ||
"mocha" | ||
] | ||
} | ||
} |
@@ -13,3 +13,3 @@ # ipfs-pubsub-1on1 | ||
// Include as lib | ||
const Channel = require('ipfs-pubsub-1on1') | ||
import Channel from 'ipfs-pubsub-1on1' | ||
// Create IPFS instance somehow | ||
@@ -16,0 +16,0 @@ const ipfs = new IPFS() |
@@ -1,14 +0,11 @@ | ||
'use strict' | ||
import EventEmitter from 'events' | ||
import PROTOCOL from './protocol.js' | ||
import encode from './encoding.js' | ||
import waitForPeers from './wait-for-peers.js' | ||
import getPeerID from './get-peer-id.js' | ||
const path = require('path') | ||
const EventEmitter = require('events') | ||
const PROTOCOL = require('./protocol') | ||
const encode = require('./encoding') | ||
const waitForPeers = require('./wait-for-peers') | ||
const getPeerID = require('./get-peer-id') | ||
/** | ||
* Communication channel over Pubsub between two IPFS nodes | ||
*/ | ||
class DirectChannel extends EventEmitter { | ||
export default class DirectChannel extends EventEmitter { | ||
constructor (ipfs, receiverID) { | ||
@@ -60,3 +57,3 @@ super() | ||
if (this._closed) return | ||
let m = encode(message) | ||
const m = encode(message) | ||
await this._ipfs.pubsub.publish(this._id, m) | ||
@@ -86,3 +83,3 @@ } | ||
// Make sure the message is coming from the correct peer | ||
const isValid = message && message.from === this._receiverID | ||
const isValid = message && String(message.from) === String(this._receiverID) | ||
// Filter out all messages that didn't come from the second peer | ||
@@ -107,3 +104,1 @@ if (isValid) { | ||
} | ||
module.exports = DirectChannel |
@@ -1,6 +0,4 @@ | ||
'use strict' | ||
import { Buffer } from 'safe-buffer' | ||
const Buffer = require('safe-buffer').Buffer | ||
module.exports = (_message) => { | ||
export default (_message) => { | ||
let message = _message | ||
@@ -7,0 +5,0 @@ if (!Buffer.isBuffer(message)) { |
@@ -1,8 +0,4 @@ | ||
'use strict' | ||
const getPeerID = async (ipfs) => { | ||
export default async (ipfs) => { | ||
const peerInfo = await ipfs.id() | ||
return peerInfo.id | ||
} | ||
module.exports = getPeerID |
@@ -1,3 +0,1 @@ | ||
'use strict' | ||
module.exports = 'ipfs-pubsub-direct-channel/v1' | ||
export default 'ipfs-pubsub-direct-channel/v1' |
@@ -1,7 +0,7 @@ | ||
'use strict' | ||
const waitForPeers = async (ipfs, peersToWait, topic, isClosed) => { | ||
export default async (ipfs, peersToWait, topic, isClosed) => { | ||
const checkPeers = async () => { | ||
const peers = await ipfs.pubsub.peers(topic) | ||
const hasAllPeers = peersToWait.map((e) => peers.includes(e)).filter((e) => e === false).length === 0 | ||
const idPeersToWait = peersToWait.map(e => String(e)) | ||
const idPeers = peers.map(e => String(e)) | ||
const hasAllPeers = idPeersToWait.map((e) => idPeers.includes(e)).filter((e) => e === false).length === 0 | ||
return hasAllPeers | ||
@@ -14,3 +14,3 @@ } | ||
return new Promise(async (resolve, reject) => { | ||
return new Promise((resolve, reject) => { | ||
const interval = setInterval(async () => { | ||
@@ -30,3 +30,1 @@ try { | ||
} | ||
module.exports = waitForPeers |
@@ -1,8 +0,5 @@ | ||
'use strict' | ||
const path = require('path') | ||
const rmrf = require('rimraf') | ||
const assert = require('assert') | ||
const pMapSeries = require('p-map-series') | ||
const { | ||
import path from 'path' | ||
import rmrf from 'rimraf' | ||
import assert from 'assert' | ||
import { | ||
connectPeers, | ||
@@ -13,21 +10,12 @@ startIpfs, | ||
testAPIs, | ||
waitForPeers, | ||
} = require('orbit-db-test-utils') | ||
waitForPeers | ||
} from 'orbit-db-test-utils' | ||
const Channel = require('../src/direct-channel') | ||
const getPeerID = require('../src/get-peer-id') | ||
const PROTOCOL = require('../src/protocol') | ||
import Channel from '../src/direct-channel.js' | ||
import PROTOCOL from '../src/protocol.js' | ||
// IPFS instances used in these tests | ||
const ipfsPaths = [ | ||
'./tmp/peer1/ipfs', | ||
'./tmp/peer2/ipfs', | ||
'./tmp/peer3/ipfs', | ||
] | ||
Object.keys(testAPIs).forEach(API => { | ||
describe(`DirectChannel ${API}`, function() { | ||
describe(`DirectChannel ${API}`, function () { | ||
this.timeout(5000) | ||
let instances = [] | ||
let ipfsd1, ipfsd2, ipfsd3, ipfs1, ipfs2, ipfs3 | ||
@@ -64,3 +52,3 @@ | ||
describe('create a channel', function() { | ||
describe('create a channel', function () { | ||
it('has two participants', async () => { | ||
@@ -99,3 +87,3 @@ const c = await Channel.open(ipfs1, id2) | ||
describe('properties', function() { | ||
describe('properties', function () { | ||
let c | ||
@@ -133,3 +121,3 @@ | ||
describe('messaging', function() { | ||
describe('messaging', function () { | ||
it('sends and receives messages', async () => { | ||
@@ -142,3 +130,3 @@ const c1 = await Channel.open(ipfs1, id2) | ||
return new Promise(async (resolve, reject) => { | ||
return new Promise((resolve, reject) => { | ||
c1.on('error', reject) | ||
@@ -149,7 +137,6 @@ c2.on('error', reject) | ||
assert.notEqual(m, null) | ||
assert.equal(m.from, id1) | ||
assert.equal(m.from, id1.toString()) | ||
assert.equal(Buffer.from(m.data).toString(), Buffer.from('hello1')) | ||
assert.equal(m.topicIDs.length, 1) | ||
assert.equal(m.topicIDs[0], c1.id) | ||
assert.equal(m.topicIDs[0], c2.id) | ||
assert.equal(m.topic, c1.id) | ||
assert.equal(m.topic, c2.id) | ||
await c2.send(Buffer.from('hello2')) | ||
@@ -159,7 +146,6 @@ }) | ||
c1.on('message', (m) => { | ||
assert.equal(m.from, id2) | ||
assert.equal(m.from, id2.toString()) | ||
assert.equal(Buffer.from(m.data).toString(), Buffer.from('hello2')) | ||
assert.equal(m.topicIDs.length, 1) | ||
assert.equal(m.topicIDs[0], c1.id) | ||
assert.equal(m.topicIDs[0], c2.id) | ||
assert.equal(m.topic, c1.id) | ||
assert.equal(m.topic, c2.id) | ||
c1.close() | ||
@@ -170,3 +156,3 @@ c2.close() | ||
await c1.send('hello1') | ||
c1.send('hello1') | ||
}) | ||
@@ -176,9 +162,7 @@ }) | ||
describe('connect', function() { | ||
describe('connect', function () { | ||
it('connects the peers', async () => { | ||
let c1, c2 | ||
const c1 = await Channel.open(ipfs1, id2) | ||
const c2 = await Channel.open(ipfs2, id1) | ||
c1 = await Channel.open(ipfs1, id2) | ||
c2 = await Channel.open(ipfs2, id1) | ||
let peers = await ipfs1.pubsub.peers(c1.id) | ||
@@ -190,3 +174,3 @@ // assert.deepEqual(peers, []) | ||
peers = await ipfs1.pubsub.peers(c1.id) | ||
assert.deepEqual(peers, [id2]) | ||
assert.deepEqual(peers.map(e => String(e)), [id2.toString()]) | ||
@@ -198,3 +182,3 @@ c1.close() | ||
describe('disconnecting', function() { | ||
describe('disconnecting', function () { | ||
it('closes a channel', async () => { | ||
@@ -207,3 +191,3 @@ const c1 = await Channel.open(ipfs1, id2) | ||
return new Promise(async (resolve, reject) => { | ||
return async () => { | ||
assert.equal(c1._closed, false) | ||
@@ -220,3 +204,2 @@ assert.equal(c1._isClosed(), false) | ||
c2.close() | ||
const topics2 = await ipfs2.pubsub.ls() | ||
assert.deepEqual(topics1, []) | ||
@@ -231,5 +214,4 @@ assert.equal(c2._closed, true) | ||
assert.deepEqual(peers2, []) | ||
resolve() | ||
}, 200) | ||
}) | ||
} | ||
}) | ||
@@ -253,7 +235,7 @@ | ||
describe('errors', function() { | ||
describe('errors', function () { | ||
it('throws an error if pubsub is not supported by given IPFS instance', async () => { | ||
let c, err | ||
let err | ||
try { | ||
c = await Channel.open({}, id2) | ||
await Channel.open({}, id2) | ||
} catch (e) { | ||
@@ -267,5 +249,5 @@ err = e | ||
it('throws an error if receiver ID was not given', async () => { | ||
let c, err | ||
let err | ||
try { | ||
c = await Channel.open(ipfs1) | ||
await Channel.open(ipfs1) | ||
} catch (e) { | ||
@@ -279,3 +261,3 @@ err = e | ||
describe('non-participant peers can\'t send messages', function() { | ||
describe('non-participant peers can\'t send messages', function () { | ||
it('doesn\'t receive unwanted messages', async () => { | ||
@@ -289,7 +271,6 @@ const c1 = await Channel.open(ipfs1, id2) | ||
c1.on('message', (m) => { | ||
assert.equal(m.from, id2) | ||
assert.equal(m.from, id2.toString()) | ||
assert.equal(m.data.toString(), 'hello1') | ||
assert.equal(m.topicIDs.length, 1) | ||
assert.equal(m.topicIDs[0], c1.id) | ||
assert.equal(m.topicIDs[0], c2.id) | ||
assert.equal(m.topic, c1.id) | ||
assert.equal(m.topic, c2.id) | ||
}) | ||
@@ -296,0 +277,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
14842
12
Yes
5
358