New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

peero-server

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

peero-server - npm Package Compare versions

Comparing version

to
2.1.1

module.exports = {
"env": {
"browser": true,
"commonjs": true,
"es6": true
"node": true,
"es6": true,
"mocha": true
},

@@ -11,6 +12,2 @@ "extends": "eslint:recommended",

},
"globals": {
"process": false,
"__dirname": false
},
"rules": {

@@ -147,3 +144,3 @@ "accessor-pairs": "error",

"no-loop-func": "error",
"no-magic-numbers": "error",
"no-magic-numbers": 0,
"no-misleading-character-class": "error",

@@ -194,3 +191,3 @@ "no-mixed-operators": "error",

"no-undefined": "error",
"no-underscore-dangle": "error",
"no-underscore-dangle": 0,
"no-unmodified-loop-condition": "error",

@@ -197,0 +194,0 @@ "no-unneeded-ternary": "error",

const EventEmitter = require('events')
const uuid = require('uuid/v4')
const logger = require('./lib/logger')
const server = require('./lib/server')
class PeeroServer extends EventEmitter {
static get CONNECTING () {
return 0
}
static get CONNECTED () {
return 1
}
static get CLOSING () {
return 2
}
static get CLOSED () {
return 3
}
constructor (opts = {}) {

@@ -16,30 +31,6 @@ super()

this.options = Object.assign(defaults, opts)
this.initConstants()
this.initLogger()
this.initSocket()
this.logger.info(`Server running on port: ${this.options.port}`)
}
initLogger () {
this.logger = logger(this.options)
}
initSocket () {
this.sockets = {}
this.socket = server(this.options)
this.socket.on('connection', (socket) => {
this.onConnection(socket)
})
}
initConstants () {
this.CONNECTING = 0
this.CONNECTED = 1
this.CLOSING = 2
this.CLOSED = 3
}
onConnection (socket) {
_onConnection (socket) {
const peerId = uuid()

@@ -49,3 +40,3 @@

socket.on('message', (message) => {
return this.onMessage(socket, message)
return this._onMessage(socket, message)
})

@@ -57,3 +48,3 @@

onMessage (socket, message) {
_onMessage (socket, message) {
let data = null

@@ -64,11 +55,11 @@

} catch (err) {
return this.logger.error('Could not parse message to JSON.')
return null
}
if (data.type === 'register') {
this.registerClient(socket)
this.announcePeer(socket)
this.sendPeers(socket)
this._registerClient(socket)
this._announcePeer(socket)
this._sendPeers(socket)
} else if (data.type === 'signal') {
this.sendSignal(socket, data.data)
this._sendSignal(socket, data.data)
}

@@ -79,13 +70,13 @@

sendSignal (socket, data) {
_sendSignal (socket, data) {
if (data.signal.type === 'offer') {
this.sendOffer(socket, data.signal)
this._sendOffer(socket, data.signal)
}
if (data.signal.type === 'answer') {
this.sendAnswer(socket, data.peerId, data.signal)
this._sendAnswer(socket, data.peerId, data.signal)
}
}
sendOffer (socket, signal) {
_sendOffer (socket, signal) {
Object.keys(this.sockets).forEach((peerId) => {

@@ -104,3 +95,3 @@ if (socket.peerId === peerId) {

return this.sendMessage(peerId, message)
return this._sendMessage(peerId, message)
})

@@ -111,3 +102,3 @@

sendAnswer (socket, peerId, signal) {
_sendAnswer (socket, peerId, signal) {
const message = JSON.stringify({

@@ -121,7 +112,7 @@ type: 'answer',

this.sendMessage(peerId, message)
this._sendMessage(peerId, message)
this.emit('answer', socket, peerId, signal)
}
sendMessage (peerId, message) {
_sendMessage (peerId, message) {
const that = this

@@ -134,3 +125,3 @@ const socket = this.sockets[peerId]

if (socket.readyState !== this.CONNECTED) {
if (socket.readyState !== PeeroServer.CONNECTED) {
return false

@@ -144,16 +135,15 @@ }

return that.onSendError(socket, err)
return that._onSendError(socket, err)
})
}
onSendError (socket, err) {
if (socket.readyState > this.CONNECTED) {
this.removeSocket(socket.peerId)
_onSendError (socket, err) {
if (socket.readyState > PeeroServer.CONNECTED) {
this._removeSocket(socket.peerId)
}
this.logger.error('Could not send message.')
this.emit('error', err, socket)
}
registerClient (socket) {
_registerClient (socket) {
const message = JSON.stringify({

@@ -166,7 +156,7 @@ type: 'register',

this.sendMessage(socket.peerId, message)
this._sendMessage(socket.peerId, message)
this.emit('register', socket)
}
announcePeer (socket) {
_announcePeer (socket) {
Object.keys(this.sockets).forEach((peerId) => {

@@ -184,3 +174,3 @@ if (socket.peerId === peerId) {

return this.sendMessage(peerId, message)
return this._sendMessage(peerId, message)
})

@@ -191,3 +181,3 @@

sendPeers (socket) {
_sendPeers (socket) {
const peers = []

@@ -200,4 +190,4 @@

if (this.sockets[peerId].readyState > this.CONNECTED) {
return this.removeSocket(peerId)
if (this.sockets[peerId].readyState > PeeroServer.CONNECTED) {
return this._removeSocket(peerId)
}

@@ -215,12 +205,24 @@

this.sendMessage(socket.peerId, message)
this._sendMessage(socket.peerId, message)
this.emit('peers', socket, peers)
}
removeSocket (peerId) {
_removeSocket (peerId) {
Reflect.deleteProperty(this.sockets, peerId)
this.emit('remove', peerId)
}
run (cb) {
this.server = server(this.options)
this.server.on('connection', (socket) => {
this._onConnection(socket)
cb(this.options.port)
})
}
close (cb) {
this.server.close(cb)
}
}
module.exports = PeeroServer
{
"name": "peero-server",
"version": "2.0.2",
"version": "2.1.1",
"description": "Websocket server which facilitates discovery between Peero client peers.",
"main": "index.js",
"scripts": {},
"scripts": {
"test": "mocha"
},
"keywords": [

@@ -11,2 +13,6 @@ "peer-to-peer",

],
"repository": {
"type": "git",
"url": "https://github.com/riteable/peero-server"
},
"author": "Amer Beganovic",

@@ -17,8 +23,9 @@ "license": "ISC",

"uuid": "^3.3.2",
"winston": "^3.1.0",
"ws": "^6.0.0"
},
"devDependencies": {
"eslint": "^5.6.1"
"chai": "^4.2.0",
"eslint": "^5.6.1",
"mocha": "^5.2.0"
}
}

@@ -19,12 +19,19 @@ WebSocket server which facilitates discovery between [Peero client](https://github.com/riteable/peero-client) peers.

const server = new PeeroServer({
port: '80',
log: '/path/to/log/directory'
port: '80'
})
server.run((port) => {
console.log('Running on port: ' + port)
})
## Options
The constructor accepts the following options:
- **port**: The port of the websocket server. **Default:** 8080.
- **log**: The directory for file logs. When null, no logs are written to disk. When a directory is set, two log files are generated: `error.log` and `combined.log`. The former contains only errors when they're caught, the latter contains `error`, `warn` and `info` log levels. When `process.env.NODE_ENV !== 'production'`, logs are output to stdout. **Default:** null.
- **port**: The port of the WebSocket server. **Default:** 8080.
## Methods
- **run(callback)**: Initializes the server. The callback returns the port which has been set in the options.
- **close(callback)**: Closes the server and exits the process.
## Events

@@ -36,3 +43,3 @@ The instance emits the following events:

- **on('offer', (socket, signal))**: When an 'offer' signal is sent to all connected clients (except the signaling client).
- **on('answer', (socket, peerId, signal))**: When an 'answer' signal is sent to a client.
- **on('answer', (socket, peerId, signal))**: When an 'answer' signal is sent to a client. `peerId` is the ID of the answering client.
- **on('announce', (socket))**: When connected clients have been notified of a new connected client.

@@ -39,0 +46,0 @@ - **on('peers', (socket, peers))**: When the list of connected clients have been sent to a client.