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

clusterhub

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

clusterhub - npm Package Compare versions

Comparing version 0.2.11 to 0.3.0

65

lib/fork.js

@@ -1,13 +0,12 @@

var cluster = require('cluster');
var has = require('./util').has;
var commands = require('./globals').commands;
var hubs = require('./globals').hubs;
var workers = require('./globals').workers;
var queue = require('./globals').queue;
var queuemsg = require('./globals').queuemsg;
var isReady = require('./globals').isReady;
var broadcast = require('./globals').broadcast;
var emit = require('./globals').emit;
var ready = require('./globals').ready;
var Hub = require('./hub');
const cluster = require('cluster');
const commands = require('./globals').commands;
const hubs = require('./globals').hubs;
const workers = require('./globals').workers;
const queue = require('./globals').queue;
const queuemsg = require('./globals').queuemsg;
const isReady = require('./globals').isReady;
const broadcast = require('./globals').broadcast;
const emit = require('./globals').emit;
const ready = require('./globals').ready;
const Hub = require('./hub');

@@ -21,12 +20,12 @@

*/
var origFork = cluster.fork.bind(cluster);
cluster.fork = function(env) {
const origFork = cluster.fork.bind(cluster);
cluster.fork = (env) => {
var worker = origFork(env);
var events = {};
var events = new Map();
var obj = {
worker: worker,
events: events,
worker,
events,
ononline,
onmessage,
ready: false,
ononline: ononline,
onmessage: onmessage,
};

@@ -43,6 +42,6 @@ workers.push(obj);

var msg;
while (msg = queue.shift()) msg.fn(msg.msg);
while ((msg = queue.shift())) msg.fn(msg.msg);
// Tell all workers hub is ready.
workers.forEach(function(child) {
workers.forEach((child) => {
child.worker.send({ cmd: commands.READY });

@@ -57,3 +56,3 @@ });

if (msg.dir !== __dirname) return;
if (!has(hubs, msg.hub)) {
if (!hubs.has(msg.hub)) {
new Hub(msg.hub);

@@ -96,6 +95,6 @@ }

function onon(msg) {
if (has(events, msg.event)) {
events[msg.event]++;
if (events.has(msg.event)) {
events.set(events.get, events.get(msg.event) + 1);
} else {
events[msg.event] = 1;
events.set(msg.event, 1);
}

@@ -105,4 +104,8 @@ }

function onoff(msg) {
if (has(events, msg.event) && --events[msg.event] === 0) {
delete events[msg.event];
if (events.has(msg.event)) {
var n = events.get(msg.event) - 1;
events.set(msg.event, n);
if (n === 0) {
events.delete(msg.event);
}
}

@@ -113,7 +116,5 @@ }

if (msg.event) {
if (has(events, msg.event)) {
delete events[msg.event];
}
events.delete(msg.event);
} else {
obj.events = events = {};
events.clear();
}

@@ -123,3 +124,3 @@ }

function oncmd(msg) {
var db = hubs[msg.hub]._db;
var db = hubs.get(msg.hub)._db;
var result = db[msg.cmd].apply(db, msg.args);

@@ -126,0 +127,0 @@

@@ -1,5 +0,2 @@

var has = require('./util').has;
// Command constants
// Command constants.
exports.commands = {

@@ -15,7 +12,7 @@ EVENT : 0,

/**
* Keep track of hubs and workers
* Keep track of hubs and workers.
*/
var hubs = exports.hubs = {};
var workers = exports.workers = [];
var queue = exports.queue = [];
const hubs = exports.hubs = new Map();
const workers = exports.workers = [];
const queue = exports.queue = [];

@@ -26,3 +23,3 @@

*/
var isReady = exports.isReady = function isReady() {
const isReady = exports.isReady = function isReady() {
var online = workers.filter(function(obj) { return obj.ready; });

@@ -36,3 +33,3 @@ return online.length === workers.length;

*/
exports.queuemsg = function queuemsg(fn, msg) {
exports.queuemsg = (fn, msg) => {
if (isReady()) {

@@ -54,6 +51,6 @@ fn(msg);

*/
exports.broadcast = function broadcast(id, event, args, origin) {
workers.forEach(function(child) {
exports.broadcast = (id, event, args, origin) => {
workers.forEach((child) => {
if (origin && child.worker === origin) return;
if (!has(child.events, event)) return;
if (!child.events.has(event)) return;

@@ -63,4 +60,4 @@ child.worker.send({

hub : id,
event : event,
args : args,
event,
args,
});

@@ -79,9 +76,9 @@

*/
exports.emit = function emit(id, event, args) {
var hub = hubs[id];
exports.emit = (id, event, args) => {
var hub = hubs.get(id);
// Check if there are listeners for this event.
if (!has(hub._listeners, event)) return;
if (!hub._listeners.has(event)) return;
hub._listeners[event].forEach(function(listener) {
hub._listeners.get(event).forEach((listener) => {
listener.apply({ local: false, remote: true }, args);

@@ -94,12 +91,10 @@ });

*/
exports.ready = function() {
for (var key in hubs) {
if (has(hubs, key)) {
var hub = hubs[key];
if (hub.isready) continue;
hub.isready = true;
var listener;
while (listener = hub._onready.shift()) listener();
}
exports.ready = () => {
for (var entry of hubs.entries()) {
var hub = entry[1];
if (hub.isready) continue;
hub.isready = true;
var listener;
while ((listener = hub._onready.shift()) != null) listener();
}
};

@@ -1,10 +0,9 @@

var cluster = require('cluster');
var EventVat = require('eventvat');
var has = require('./util').has;
var commands = require('./globals').commands;
var hubs = require('./globals').hubs;
var broadcast = require('./globals').broadcast;
var emit = require('./globals').emit;
var isMaster = cluster.isMaster;
var isWorker = cluster.isWorker;
const cluster = require('cluster');
const EventVat = require('eventvat');
const commands = require('./globals').commands;
const hubs = require('./globals').hubs;
const broadcast = require('./globals').broadcast;
const emit = require('./globals').emit;
const isMaster = cluster.isMaster;
const isWorker = cluster.isWorker;

@@ -18,6 +17,6 @@

this._id = id || '';
if (has(hubs, this._id)) return hubs[this._id];
hubs[this._id] = this;
if (hubs.has(this._id)) return hubs.get(this._id);
hubs.set(this._id, this);
this._listeners = {};
this._listeners = new Map();
this._onready = [];

@@ -32,4 +31,4 @@

if (has(self._listeners, this.event)) {
self._listeners[this.event].forEach(function(listener) {
if (self._listeners.has(this.event)) {
self._listeners.get(this.event).forEach((listener) => {
listener.apply(null, args);

@@ -42,3 +41,3 @@ });

} else {
this._cb = {};
this._cb = new Map();
}

@@ -54,5 +53,4 @@ };

*/
Object.keys(EventVat.prototype).forEach(function(cmd) {
Object.keys(EventVat.prototype).forEach((cmd) => {
Hub.prototype[cmd] = function() {
var self = this;
var args = Array.prototype.slice.call(arguments);

@@ -66,4 +64,4 @@ var cb;

if (isMaster) {
var rs = self._db[cmd].apply(self._db, args);
if (cb) process.nextTick(function() { cb(rs); });
var rs = this._db[cmd].apply(this._db, args);
if (cb) process.nextTick(() => { cb(rs); });
return rs;

@@ -76,12 +74,7 @@

if (cb) {
while (has(self._cb, (key = Math.floor(Math.random() * 20000))));
self._cb[key] = cb;
while (this._cb.has((key = Math.floor(Math.random() * 20000))));
this._cb.set(key, cb);
}
self._send({
cmd : cmd,
args : args,
key : key,
});
this._send({ cmd, args, key });
}

@@ -151,7 +144,3 @@ };

if (isWorker) {
this._send({
cmd : commands.EVENT,
event : event,
args : args,
});
this._send({ cmd: commands.EVENT, event, args });
} else {

@@ -177,7 +166,7 @@ broadcast(this._id, event, args);

Hub.prototype.on = function(event, listener) {
if (!has(this._listeners, event)) this._listeners[event] = [];
this._listeners[event].push(listener);
if (!this._listeners.has(event)) this._listeners.set(event, []);
this._listeners.get(event).push(listener);
if (isWorker) {
this._send({ cmd: commands.ON, event: event });
this._send({ cmd: commands.ON, event });
}

@@ -201,6 +190,6 @@ };

Hub.prototype.off = function(event, listener) {
if (!has(this._listeners, event)) return;
if (!this._listeners.has(event)) return;
// Remove local listener.
var listeners = this._listeners[event];
var listeners = this._listeners.get(event);
var found = false;

@@ -218,3 +207,3 @@ for (var i = 0, l = listeners.length; i < l; i++) {

if (found && isWorker) {
this._send({ cmd: commands.OFF, event: event });
this._send({ cmd: commands.OFF, event });
}

@@ -269,11 +258,9 @@ };

if (event) {
if (has(this._listeners, event)) {
delete this._listeners[event];
}
this._listeners.delete(event);
} else {
this._listeners = {};
this._listeners.clear();
}
if (isWorker) {
this._send({ cmd: commands.OFFALL, event: event });
this._send({ cmd: commands.OFFALL, event });
}

@@ -300,3 +287,3 @@ };

this._db.die();
delete hubs[this._id];
hubs.delete(this._id);
};

@@ -1,3 +0,3 @@

var cluster = require('cluster');
var Hub = require('./hub');
const cluster = require('cluster');
const Hub = require('./hub');

@@ -11,3 +11,3 @@

} else {
require('./listener.js');
require('./listener');
}

@@ -24,5 +24,3 @@

globalHub.Hub = Hub;
globalHub.createHub = function(id) {
return new Hub(id);
};
globalHub.createHub = (id) => new Hub(id);

@@ -29,0 +27,0 @@ module.exports = globalHub;

@@ -1,6 +0,5 @@

var has = require('./util').has;
var commands = require('./globals').commands;
var hubs = require('./globals').hubs;
var emit = require('./globals').emit;
var ready = require('./globals').ready;
const commands = require('./globals').commands;
const hubs = require('./globals').hubs;
const emit = require('./globals').emit;
const ready = require('./globals').ready;

@@ -11,3 +10,3 @@

*/
process.on('message', function(msg) {
process.on('message', (msg) => {
if (msg.cmd === commands.READY) {

@@ -19,4 +18,4 @@ return ready();

if (msg.dir !== __dirname) return;
if (msg.hub === undefined || !has(hubs, msg.hub)) return;
var hub = hubs[msg.hub];
if (msg.hub === undefined || !hubs.has(msg.hub)) return;
var hub = hubs.get(msg.hub);

@@ -28,5 +27,5 @@ if (msg.event) {

// it can be a response to another command too
hub._cb[msg.key].apply(null, msg.args);
delete hub._cb[msg.key];
hub._cb.get(msg.key).apply(null, msg.args);
hub._cb.delete(msg.key);
}
});

@@ -11,3 +11,3 @@ {

],
"version": "0.2.11",
"version": "0.3.0",
"repository": {

@@ -29,3 +29,3 @@ "type": "git",

"engines": {
"node": ">=0.12"
"node": ">=4"
},

@@ -36,6 +36,6 @@ "dependencies": {

"devDependencies": {
"istanbul": "*",
"mocha": "*"
"istanbul": "^0.4.5",
"mocha": "^4.0.1"
},
"license": "MIT"
}

@@ -13,5 +13,5 @@ # clusterhub

```js
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var hub = require('clusterhub');
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
const hub = require('clusterhub');

@@ -25,3 +25,3 @@ if (cluster.isMaster) {

} else {
hub.on('event', function(data) {
hub.on('event', (data) => {
// do something with `data`

@@ -81,5 +81,6 @@ });

#### worker process
```
hub.set('foo', 'bar', function() {
hub.get('foo', function(val) {
```js
hub.set('foo', 'bar', () => {
hub.get('foo', (val) => {
console.log(val === 'bar'); // true

@@ -91,4 +92,4 @@ });

#### master process
```
var returnedVal = hub.incr('foo', function(val) {
```js
var returnedVal = hub.incr('foo', (val) => {
// can be given a callback for consistency

@@ -95,0 +96,0 @@ console.log(val === 1); // true

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