New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

democracy

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

democracy - npm Package Compare versions

Comparing version
2.1.1
to
3.0.0
+65
-6
lib/democracy.js

@@ -7,3 +7,3 @@ /**

const uuid = require('uuid');
const shortid = require('shortid');
const dgram = require('dgram');

@@ -28,2 +28,3 @@ const {EventEmitter} = require('events');

this._nodes = {};
this._chunks = {};

@@ -34,6 +35,7 @@ // Merge the passed options with the defaults.

timeout: options.timeout || 3000,
maxPacketSize: options.maxPacketSize || 508,
source: options.source || '0.0.0.0:12345',
peers: options.peers || [],
weight: options.weight || Math.random() * Date.now(),
id: options.id || uuid.v4(),
id: options.id || shortid.generate(),
channels: options.channels || [],

@@ -143,9 +145,34 @@ };

// Adjust the max size by the max size of the chunk wrapper data.
const maxSize = this.options.maxPacketSize;
const chunkSize = maxSize - 52;
// Check if the packet needs to be chunked.
const str = JSON.stringify(data);
let chunks = [];
if (str.length > maxSize) {
const count = Math.ceil(str.length / chunkSize);
const packetId = shortid.generate();
for (let i = 0; i < count; i += 1) {
chunks.push(JSON.stringify({
chunk: str.substr(i * chunkSize, chunkSize),
id: packetId,
c: count,
i,
}));
}
} else {
chunks.push(str);
}
// Data must be sent as a Buffer over the UDP socket.
const msg = Buffer.from(JSON.stringify(data));
chunks = chunks.map(chunk => Buffer.from(chunk));
// Loop through each connect node and send the packet over.
for (let i = 0; i < this.options.peers.length; i += 1) {
if (!id || this._nodes[id].source === `${this.options.peers[i][0]}:${this.options.peers[i][1]}`) {
this.socket.send(msg, 0, msg.length, this.options.peers[i][1], this.options.peers[i][0]);
for (let x = 0; x < chunks.length; x += 1) {
for (let i = 0; i < this.options.peers.length; i += 1) {
if (!id || this._nodes[id].source === `${this.options.peers[i][0]}:${this.options.peers[i][1]}`) {
this.socket.send(chunks[x], 0, chunks[x].length, this.options.peers[i][1], this.options.peers[i][0]);
}
}

@@ -250,2 +277,34 @@ }

// Check if this is a chunk and put in the store.
if (data && data.chunk && data.id) {
// Add the chunk to the buffer.
this._chunks[data.id] = this._chunks[data.id] || [];
this._chunks[data.id].push(data);
// If the buffer is full, combine and process.
if (this._chunks[data.id].length === data.c) {
// Sort the chunks by index.
this._chunks[data.id].sort((a, b) => {
if (a.i < b.i) {
return -1;
}
if (a.i > b.i) {
return 1;
}
return 0;
});
// Merge the data into a single string.
const newData = this._chunks[data.id].reduce((acc, val) => acc + val.chunk, '');
delete this._chunks[data.id];
// Process the data as a buffer.
this.processEvent(Buffer.from(newData));
}
return this;
}
// Validate the data.
if (!data || data.id === this._id) {

@@ -252,0 +311,0 @@ return this;

+5
-5
{
"name": "democracy",
"version": "2.1.1",
"version": "3.0.0",
"description": "Node.js unicast discovery, master-slave elections and pub/sub.",

@@ -28,8 +28,8 @@ "homepage": "https://github.com/goldfire/democracy.js",

"dependencies": {
"uuid": "3.2.*"
"shortid": "2.2.14"
},
"devDependencies": {
"eslint": "4.19.*",
"eslint-config-airbnb-base": "12.1.*",
"eslint-plugin-import": "2.10.*"
"eslint": "5.9.*",
"eslint-config-airbnb-base": "13.1.*",
"eslint-plugin-import": "2.14.*"
},

@@ -36,0 +36,0 @@ "engines": [

@@ -55,2 +55,3 @@ ## Description

timeout: 3000, // How long a peer must go without sending a `hello` to be considered down.
maxPacketSize: 508, // Maximum size per packet. If the data exceeds this, it will be chunked.
source: '0.0.0.0:12345', // The IP and port to listen to (usually the local IP).

@@ -57,0 +58,0 @@ peers: [], // The other servers/ports you want to communicate with (can be on the same or different server).