Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

peer-relay

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

peer-relay - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

2

index.js
module.exports = require('./lib/client')
module.exports.WGram = require('./lib/wgram')
module.exports.Socket = require('./lib/wgram')
module.exports.debug = require('debug')

@@ -38,6 +38,6 @@ var KBucket = require('k-bucket')

self.wrtcConnector = new WrtcConnector(self.id, self.router)
self.wrtcConnector = new WrtcConnector(self.id, self.router, opts.wrtc)
self.wrtcConnector.on('connection', onConnection)
self._debug('Client(%s)', JSON.stringify(opts))
self._debug('Client(%s)', JSON.stringify(opts, ['port', 'bootstrap']))

@@ -181,3 +181,3 @@ for (var uri of (opts.bootstrap || [])) {

ws: self.wsConnector.url,
wrtc: true
wrtc: self.wrtcConnector.supported
}

@@ -193,3 +193,3 @@ })

if (msg.data.wrtc) self.wrtcConnector.connect(from)
if (msg.data.wrtc && self.wrtcConnector.supported) self.wrtcConnector.connect(from)
else if (msg.data.ws) self.wsConnector.connect(msg.data.ws)

@@ -196,0 +196,0 @@ }

@@ -22,8 +22,8 @@ 'use strict'

self._bindState = BIND_STATE_UNBOUND
self._client = new Client(opts)
self._client.on('message', onMessage)
self._client.on('close', onClose)
self._client.on('error', onError)
self.peer = new Client(opts)
self.peer.on('message', onMessage)
self.peer.on('close', onClose)
self.peer.on('error', onError)
PORT_MAPPINGS[self._port] = self._client.id.toString('hex')
PORT_MAPPINGS[self._port] = self.peer.id.toString('hex')

@@ -50,3 +50,3 @@ function onMessage (msg) {

var self = this
if (!self._client) throw new Error('Not running')
if (!self.peer) throw new Error('Not running')
if (self._bindState !== BIND_STATE_UNBOUND) throw new Error('Socket is already bound')

@@ -59,10 +59,10 @@

if (self._client.peers.count() > 0) {
if (self.peer.peers.count() > 0) {
onBind()
} else {
self._client.once('peer', onBind)
self.peer.once('peer', onBind)
}
function onBind () {
PORT_MAPPINGS[self._port] = self._client.id.toString('hex')
PORT_MAPPINGS[self._port] = self.peer.id.toString('hex')
self._bindState = BIND_STATE_BOUND

@@ -82,3 +82,3 @@ self.emit('listening')

var self = this
if (!self._client) throw new Error('Not running')
if (!self.peer) throw new Error('Not running')

@@ -91,3 +91,3 @@ var isIP = address.indexOf('.') !== -1

buffer: buffer.slice(offset, length),
address: isLocal ? address : self._client.id.toString('hex'),
address: isLocal ? address : self.peer.id.toString('hex'),
port: self._port

@@ -98,3 +98,3 @@ }

self._client.send(new Buffer(id, 'hex'), msg)
self.peer.send(new Buffer(id, 'hex'), msg)
if (cb) cb()

@@ -105,6 +105,6 @@ }

var self = this
if (!self._client) throw new Error('Not running')
if (!self.peer) throw new Error('Not running')
self._client.destroy()
self._client = null
self.peer.destroy()
self.peer = null

@@ -119,6 +119,6 @@ delete PORT_MAPPINGS[self._port]

var self = this
if (!self._client) throw new Error('Not running')
if (!self.peer) throw new Error('Not running')
return {
address: self._client.id.toString('hex'),
address: self.peer.id.toString('hex'),
port: self._port,

@@ -125,0 +125,0 @@ family: 'peer-relay'

var inherits = require('util').inherits
var EventEmitter = require('events').EventEmitter
var SimplePeer = require('simple-peer')
var debug = require('debug')('peer-relay:wrtc')
var SimplePeer = require('simple-peer')
var ElectronWebrtc = require('electron-webrtc')
var wrtc = getWebrtc()

@@ -11,3 +9,3 @@ module.exports = WrtcConnector

inherits(WrtcConnector, EventEmitter)
function WrtcConnector (id, router) {
function WrtcConnector (id, router, wrtc) {
var self = this

@@ -17,2 +15,4 @@

self.destroyed = false
self.supported = wrtc != null || SimplePeer.WEBRTC_SUPPORT
self._wrtc = wrtc
self._pending = {}

@@ -49,3 +49,3 @@ self._router = router

trickle: true,
wrtc: wrtc
wrtc: self._wrtc
})

@@ -151,8 +151,1 @@

}
function getWebrtc () {
if (SimplePeer.WEBRTC_SUPPORT) return undefined
var wrtc = ElectronWebrtc()
wrtc.on('error', function (err) { debug(err) })
return wrtc
}
{
"name": "peer-relay",
"version": "0.0.1",
"version": "0.0.2",
"description": "relay messages through peers in a network",

@@ -9,7 +9,7 @@ "main": "index.js",

"test": "standard && mocha test.js --use-strict --timeout 10000",
"bundle": "browserify index.js -s PeerRelay -d -o bundle.js -i process -i ws -i electron-webrtc",
"bundle": "browserify index.js -s PeerRelay -d -o bundle.js -i ws",
"prepublish": "npm run -s bundle",
"standard": "standard",
"browserify": "browserify",
"start": "server.js"
"start": "./server.js"
},

@@ -16,0 +16,0 @@ "standard": {

@@ -17,2 +17,3 @@ # peer-relay

* bootstrap - an array of web socket urls to peers already connected to the network
* wrtc - custom webrtc implementation

@@ -31,3 +32,3 @@ `port` can only be specified if the peer is running nodejs since start a WebSocket server is not possible in a browser. Every peer should specify at least on bootstrap peer (unless that peer is the first/only peer in the network)

Disconnect the a currently connected peer with `id`.
Disconnect the a currently connected peer with `id`.

@@ -42,6 +43,8 @@ ### `peer.send(id, data)`

### `var socket = new PeerRelay.WGram([opts])`
### `var socket = new PeerRelay.Socket([opts])`
Creates a new [dgram](https://nodejs.org/api/dgram.html) like socket that uses peer-relay to send messages between peers. This allows for peer-relay to be used by programs that expect the dgram interface. `opts` is the same as the `opts` for `new PeerRelay([opts])`. The returned object is tries to match the interface provided by dgram's [Socket](https://nodejs.org/api/dgram.html#dgram_class_dgram_socket)
Creates a new [dgram](https://nodejs.org/api/dgram.html) like socket that uses peer-relay to send messages between peers. This allows for peer-relay to be used by programs that expect the dgram interface. This method accepts the same arguments as the PeerRelay constructor. The returned object tries to match the interface provided by dgram's [Socket](https://nodejs.org/api/dgram.html#dgram_class_dgram_socket).
`socket.peer` references the underlying PeerRelay instance.
## Events

@@ -48,0 +51,0 @@

@@ -6,11 +6,26 @@ #!/usr/bin/env node

var opts = {
port: parseInt(process.argv[2]),
bootstrap: process.argv.length === 4 ? [ process.argv[3] ] : []
var needsHelp = process.argv.indexOf('--help') !== -1 ||
process.argv.indexOf('-h') !== -1 ||
process.argv.length < 3
if (!needsHelp) {
var opts = {
port: parseInt(process.argv[2]),
bootstrap: process.argv.length === 4 ? [ process.argv[3] ] : []
}
var c = new Client(opts)
c.on('peer', function (id) {
console.error('PEER', id.toString('hex', 0, 2))
})
} else {
console.error(`\
${process.argv[1]} port [bootstrap_urls...]
Starts a PeerRelay node and listens for WebSocket connectionso on 'port'
An optional list of bootstrap urls can be provided as positional arguments.
`)
}
var c = new Client(opts)
c.on('peer', function (id) {
console.error('PEER', id.toString('hex', 0, 2))
})
var assert = require('assert')
var Client = require('./lib/client')
var wrtc = require('electron-webrtc')()
wrtc.on('error', function (err) { console.error(err, err.stack) })
describe('End to End', function () {

@@ -154,2 +157,20 @@ var clients = []

it('webrtc connect and send message', function (done) {
// c1 <-> c2 <-> c3
var c2 = startClient({ port: 8002, bootstrap: [] })
var c1 = startClient({ wrtc: wrtc, bootstrap: ['ws://localhost:8002'] })
var c3 = startClient({ wrtc: wrtc, bootstrap: ['ws://localhost:8002'] })
c1.on('peer', function (id) {
assert.ok(id.equals(c2.id) || id.equals(c3.id))
if (id.equals(c3.id)) c1.send(c3.id, 'TEST')
})
c3.on('message', function (msg, id) {
assert.ok(id.equals(c1.id))
assert.equal(msg, 'TEST')
done()
})
})
// it('relay chain', function (done) {

@@ -156,0 +177,0 @@ // var peers = []

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc