Rewrite event emitter with Reactive programming(Semaphore example) - javascript

I'm use event emitters as synchronization primitives. For example I have one class which asks semaphore like structure in Redis. If semaphore is set it emits an event. The code is listed bellow:
var redis = require("redis"),
async = require('async'),
client = redis.createClient(),
assert = require('assert'),
EventEmitter = require('events').EventEmitter;
util = require('util');
var isMaster = false,
SEMAPHORE_ADDRESS = 'semaphore';
var SemaphoreAsker = function() {
var self = this;
var lifeCycle = function (next) {
client.set([SEMAPHORE_ADDRESS, true, 'NX', 'EX', 5], function(err, val) {
console.log('client');
if(err !== null) { throw err; }
if(val === 'OK') {
self.emit('crown');
} else {
console.log('still a minion');
}
});
};
async.forever(
function(next) {
setTimeout(
lifeCycle.bind(null, next),
1000
);
}
);
};
util.inherits(SemaphoreAsker, EventEmitter);
(new SemaphoreAsker()).on('crown', function() {
console.log('I`m master');
});
It works but looks a little bit heavy. Is it possible to rewrite the example with BaconJS(RxJS/whateverRPlibrary)?

The following should work in RXJS:
var callback = Rx.Observable.fromNodeCallback(client.set, client);
var source = Rx.Observable.interval(1000)
.selectMany(function() {
return callback([SEMAPHORE_ADDRESS, true, 'NX', 'EX', 5]);
})
.filter(function(x) { return x === 'OK'; })
.take(1);
source.subscribe(function(x) {
console.log("I am master");
});
If you are willing to additionally include the rx-node module you can also keep your event emitter structure by using
var emitter = RxNode.toEventEmitter(source, 'crown');
emitter.on('crown', function(){});
emitter.on('error', function(){});
emitter.on('end', function(){});

I used the basic Bacon.fromBinder to create a custom stream for this. Without a working example this is a bit of guesswork, but hopefully this helps you.
var redis = require("redis"),
client = redis.createClient(),
assert = require('assert'),
Bacon = require('bacon');
var SEMAPHORE_ADDRESS = 'semaphore';
var SemaphoreAsker = function() {
return Bacon.fromBinder(function (sink) {
var intervalId = setInterval(pollRedis, 1000)
return function unsubscribe() {
clearInterval(intervalId)
}
function pollRedis() {
client.set([SEMAPHORE_ADDRESS, true, 'NX', 'EX', 5], function(err, val) {
if(err !== null) { sink(new Bacon.Error(err)) }
else if(val === 'OK') { sink(new Bacon.Next('crown'))
else { assert.fail(); }
}
}
})
}
SemaphoreAsker().take(1).onValue(function() {
console.log("I am master")
})

The #paulpdanies answer but rewritten with Bacon:
var source = Bacon.interval(1000).flatMap(function() {
return Bacon.fromNodeCallback(
client, 'set', [SEMAPHORE_ADDRESS, true, 'NX', 'EX', 1]
);
})
.filter(function(x) { return x === 'OK'; })
.take(1);
source.onValue(function(x) {
console.log(x);
});

Related

NodeJS unable to modify a class obj

this is my first post in this forum. So please forgive me the misstakes.
I want to write a NodeJS server which runs a WebSocket Server (npm ws module).
The NodeJS server contains also a Class Obj which i want to modify a funciton afterwards over the Websocket server.
My Problem is the modified functjion cant acces global variables.
Can someone help if there is a solution for this problem or why this happes because if you do this without the Websocket it works.
Here is the code:
Server code:
const WebSocket = require('ws');
// WebSocket Server
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
try {
message = JSON.parse(message);
if (message.type == "handler") {
handler.modify(message.data);
console.log("modifyed");
}
if (message.type == "func") {
handler.modify_func(message.data);
console.log("modifyed");
}
if (message.type == "run") {
eval(message.data);
}
}
catch (error) {
}
});
});
// Modifying class
class Handler {
constructor() {
this.functions = [];
}
modify(data) {
let temp_class = new Function('return ' + data)();
temp_class.functions.forEach(element => {
if (this.functions.indexOf(element) == -1) {
this.functions.push(element)
}
this[element] = temp_class[element];
});
}
modify_func(data) {
let temp_func = new Function('return ' + data)();
this[temp_func.name] = temp_func;
}
test_func_from_orginal() {
console.log("test_func_from_orginal says:");
console.log(test_val);
}
}
var test_val = "this is the global variable";
var handler = new Handler();
Client code:
const WebSocket = require('ws');
//WebSocket Client
var ws = new WebSocket('ws://localhost:8080');
ws.on('open', function open(event) {
// ws.send(JSON.stringify({ type: "handler", data: Handler.toString() }))
ws.send(JSON.stringify({ type: "func", data: test_func_from_func.toString() }))
console.log("open")
});
//Class Module
class Handler {
static get functions() {
return ["test"];
}
static test_func_from_class() {
console.log("test_func_from_class sayes:")
console.log(test_val);
}
}
function test_func_from_func() {
console.log("test_func_from_func sayes:")
console.log(test_val);
}
setTimeout(function () { ws.send(JSON.stringify({ type: "run", data: 'handler.test_func_from_orginal()' })) }, 1000);
// setTimeout(function () { ws.send(JSON.stringify({ type: "run", data: 'handler.test_func_from_class()' })) }, 1000);
setTimeout(function () { ws.send(JSON.stringify({ type: "run", data: 'handler.test_func_from_func()' })) }, 1000);
Ok, so this is what it's all about - a simple mistake. If you cut out all the websocket stuff (which is not really relevant here, as strings, and not contexts, got passed from and back anyway), you'll get this:
class ServerHandler {
constructor() {
this.functions = [];
}
modify(data) {
let temp_class = new Function('return ' + data)();
// not sure why not just `eval(data)` btw
temp_class.functions.forEach(funcName => {
if (this.functions.indexOf(funcName) == -1) {
this.functions.push(funcName)
}
this[funcName] = temp_class[funcName];
});
}
}
class ClientHandler {
static get functions() {
return ["test_func_from_class"];
// not "test" as in your example
// you actually don't even need this registry:
// all the static methods can be collected in runtime
}
static test_func_from_class() {
console.log("test_func_from_class sayes:")
console.log(test_val);
}
}
var test_val = 42;
var handler = new ServerHandler();
handler.modify(ClientHandler.toString());
eval(`handler.test_func_from_class()`); // 42
This all works fine, as there's no longer a mismatch between a name of method stored in static get functions ("test") and actual name of that method ("test_func_from_class"). The trick is that all the static functions created along with that temporary class are scoped the same way any other entity created in ServerHandler; that's how they 'see' that test_val.
But 'works' here is about mere possibility of this approach from technical perspective, and not about feasibility. Both new Function and eval with arbitrary input are very dangerous security holes - and they're left wide open here.
I found now a solution for my problem.
Server Code
const WebSocket = require('ws');
// WebSocket Server
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
message = JSON.parse(message);
try {
if (message.type == "run") {
eval(message.data);
}
if (message.type == "obj_handler") {
handler.modify(JSON.parse(message.data));
}
}
catch (error) {
console.log(error);
}
// console.log('received: %s', message);
});
ws.send('something');
});
class ServerHandler {
constructor() {
this.data = "hi";
}
modify(data) {
for (const func in data) {
this[func] = eval(data[func]);
}
}
}
var test_val = 42;
var handler = new ServerHandler();
Client Code:
const WebSocket = require('ws');
//WebSocket Client
try {
var ws = new WebSocket('ws://localhost:8080');
ws.on('open', function open(event) {
ws.send(JSON.stringify({ type: "obj_handler", data: update.convert() }))
});
}
catch (error) {
}
// Needed Update with 2 new Functions
update = {
func_test_global: () => {
console.log(test_val);
},
func_test_this: _ => {
console.log(this.data);
},
convert: function () {
let new_update = {};
for (const func in this) {
if (func != "convert")
new_update[func] = "" + this[func];
}
return JSON.stringify(new_update)
}
}
// setTimeout(function () { ws.send(JSON.stringify({ type: "run", data: 'handler.func_test_global()' })) }, 1000);
// setTimeout(function () { ws.send(JSON.stringify({ type: "run", data: 'handler.func_test_this()' })) }, 1000);

How to separate clients in an event based websocket implementation?

I'm writing my own event-based websocket wrapper, based on the ws package for node and having trouble separating clients on the server side.
I set up a test scenario where my client emits a message to the server and the server sends a "ping" back to that same client.
What happens is that my test scenario works perfectly with the first connected client, but as soon as another client connects, emit messages from the first client will ping to the other client (messages from second client will correctly ping to the second client).
It feels like the WebSocket is somehow shared between the two clients but I can't seem to find where this is happening.
//SERVER CODE
let events = {};
function WebSockServer(port) {
//Init server
const wss = new WebSocket.Server({
port,
clientTracking: true
}, () => {
if (events['open']) {
events['open'].callback();
}
});
wss.on('connection', ws => {
ws.id = uuidv4();
let sock = new WebSock(ws, wss);
if (events['connection']) {
events['connection'].callback(sock);
}
ws.on('message', json => {
console.log('received message from ' + ws.id);
const message = JSON.parse(json);
//Check flags
for (let key in message.flags) {
if (key === 'binary' && message.flags[key].length > 0) {
message.flags[key].forEach(binary => {
const parsed = Buffer.from(message.data[binary], 'utf-16');
message.data[binary] = parsed;
});
}
}
//Check if event handler exists & check if event is only to be triggered once
if (events[message.event] && !events[message.event].once) {
events[message.event].callback(message.data);
}
else if (events[message.event] && events[message.event].once) {
events[message.event].callback(message.data);
delete events[message.event];
}
else {
return;
}
});
ws.on('close', () => {
if (events['close']) {
console.log('client disconnected');
events['close'].callback(ws.id);
}
wss.clients.delete(ws);
});
});
//Create an event handler
this.on = function on(event, callback) {
events[event] = {callback, once: false};
return this;
}
wss.on('error', err => {
console.log('Server shut down.');
});
}
function WebSock(ws, wss) {
this.ws = ws;
this.id = ws.id;
//Create an event handler
this.on = function on(event, callback) {
events[event] = {callback, once: false};
return this;
}
//Create an event handler for one time use
this.once = function once(event, callback) {
events[event] = {callback, once: true};
return this;
}
//Emit message to socket
this.emit = function emit(event, data) {
let flags = {
binary: []
};
//Check for binary data
for (let key in data) {
if (Buffer.isBuffer(data[key])) {
const stringified = data[key].toJSON();
data[key] = stringified;
flags.binary.push(key);
}
}
const payload = JSON.stringify({event, data, flags});
this.ws.send(payload);
return this;
}
//Emit message to specific socket
this.emitTo = function emitTo(id, event, data) {
let destination;
let flags = {
binary: []
};
//Find socket
for (let socket of wss.clients) {
if (socket.id === id) {
destination = socket;
break;
}
}
//Check for binary data
for (let key in data) {
if (Buffer.isBuffer(data[key])) {
const stringified = data[key].toJSON();
data[key] = stringified;
flags.binary.push(key);
}
}
//Send message to socket
if (destination) {
//Check type
if (typeof event != 'string' || typeof data != 'object') {
throw new TypeError('event must be a string, data must be an object');
}
const payload = JSON.stringify({event, data, flags});
destination.send(payload);
return this;
}
}
}
module.exports = WebSockServer;
//CLIENT CODE
function webSock(url) {
const ws = new WebSocket(url);
let events = {};
//Create an event handler
this.on = function on(event, callback) {
events[event] = {callback, once: false};
return this;
}
//Create an event handler for one time use
this.once = function once(event, callback) {
events[event] = {callback, once: true};
return this;
}
//Emit message to server
this.emit = function emit(event, data) {
let flags = {
binary: []
};
//Check for binary data
for (let key in data) {
if (Buffer.isBuffer(data[key])) {
const stringified = data[key].toJSON();
data[key] = stringified;
flags.binary.push(key);
}
}
const payload = JSON.stringify({event, data, flags});
ws.send(payload);
return this;
}
ws.on('open', () => {
if (events['open']) {
events['open'].callback(ws);
}
});
ws.on('close', () => {
if (events['close']) {
events['close'].callback();
}
})
ws.on('message', json => {
const message = JSON.parse(json);
//Check flags
for (let key in message.flags) {
if (key === 'binary' && message.flags[key].length > 0) {
message.flags[key].forEach(buffer => {
const parsed = Buffer.from(message.data[buffer]);
message.data[buffer] = parsed;
});
}
}
//Check if event handler exists & check if event is only to be triggered once
if (events[message.event] && !events[message.event].once) {
events[message.event].callback(message.data);
}
else if (events[message.event] && events[message.event].once) {
events[message.event].callback(message.data);
delete events[message.event];
}
else {
return;
}
});
}
module.exports = webSock;
Any help would be much appreciated, I'm sure I'm missing something obvious.

Comparing 2 attributes using the function "some"

var e = require("./myApp.js");
var myServer = e.CreateServer(1337);
myServer.Register("/", "GET", function (req, res) { res.end("J") });
myServer.Register("/", "GET", function (req, res) { res.end("Ja") });
myServer.Start();
This is my "Wrapper":
module.exports = (function () {
function _createServer(port) {
var routingTable = [];
var port = port;
var server = require('http').createServer();
function _start() {
server.listen(port);
console.log("Server was started");
};
function RegisterRecord(url, method, fnc) {
this.url = url;
this.method = method;
this.fnc = fnc;
};
function _register(newUrl, newMethod, newFnc) {
if (_checkInput(newUrl, newMethod))
console.log("Register failed! Record with same URL and Method already exist");
else {
routingTable.push(new RegisterRecord(newUrl, newMethod, newFnc));
console.log("Register success!");
}
};
function _checkInput(newUrl, newMethod) {
return routingTable.some(function fnc(record) { record.url == newUrl && record.method == newMethod });
};
return {
Start: _start,
Register: _register,
ShutDown: _shutDown
};
};
return { CreateServer: _createServer };
})();
So the most important functions are "_register" and "checkInput".
My aim is that the same URL and Method are only allowed on time in the array routingTable. So when I execute the programm, the Command Promp prints two times Register success. But "/" and "GET" should only be allowed one time.
How can I compare the URL and method so that they can be unique?
PS: The "Wrapper" is in the JS File "./MyApp.js"
You need filter:
function _checkInput(newUrl, newMethod) {
return routingTable
.filter( function(el) {
return el.url === newUrl && el.method === newMethod;
})
.length > 0;
};
Upd. Of course, you can use the some - you just forgot to return a value from it:
function _checkInput(newUrl, newMethod) {
return routingTable
.some( function(el) {
// Need return
return el.url === newUrl && el.method === newMethod;
})
};

CRON Job not working on meteor

In main.js in server folder,
I have this code,
var DDP = require('ddp');
var DDPlogin = require('ddp-login');
var Job = require('meteor-job');
var ddp = new DDP({
host: "127.0.0.1",
port: 3000,
use_ejson: true
});
Meteor.startup(() => {
var myJobs = JobCollection('myJobQueue');
Job.setDDP(ddp);
ddp.connect(function (err) {
if(err) throw err;
DDPlogin(ddp, function (err, token) {
if (err) throw err;
});
});
myJobs.allow({
admin: function (userId, method, params) {
return true;
}
});
Meteor.publish('allJobs', function () {
return myJobs.find({});
});
myJobs.startJobServer();
var workers = Job.processJobs('myJobQueue', 'sendEmail',
function (job, cb) {
console.log(job.data.text);
job.done();
cb(null);
}
);
And in my main.js in client folder,
I have this code,
var myJobs = JobCollection('myJobQueue');
var jobSub = null;
class App extends Component {
componentDidMount(){
if(jobSub !== null)
jobSub.stop();
jobSub = Meteor.subscribe('allJobs');
var job = new Job(myJobs, 'sendEmail',
{
text: 'bozo#clowns.com'
}
);
job.priority('normal')
.retry({ retries: 5,
wait: 60*1000 }) // 1 minute between attempts
.delay(0) // start immediately
.save();
}
...
render(){
console.log(myJobs.find().fetch());
...
}
}
I am using the vsivsi:meteor-job-collection package.
The problem is that console.log() is not executed.
What is wrong in my step by step installation and usage?
I need to console.log() every minute.

How to stub a dynamic object method using Sinon.js?

I have following module.
var Sendcloud = require('sendcloud');
var sc = new Sendcloud("key1","key2","key3");
var service = {};
service.restorePassword = function (params, cb) {
if (!params.to || !params.name || !params.token) {
throw "Miss params"
}
var defaultTemplate = adminBaseUrl + "reset/token/" + params.token;
var subject = params.subject || "Letter";
var template = params.template || defaultTemplate;
// Send email
sc.send(params.to, subject, template).then(function (info) {
console.log(info)
if (info.message === "success") {
return cb(null, "success");
} else {
return cb("failure", null);
}
});
};
module.exports = service;
I experience problems stubbing sc.send method. How to correctly cover this point using sinon.js? Or maybe I need to replace sendcloud module?
You need to use proxyquire module or rewire module.
Here an example of using proxyquire
var proxyquire = require('proxyquire');
var sinon = require('sinon');
var Sendcloud = require('sendcloud');
require('sinon-as-promised');
describe('service', function() {
var service;
var sc;
beforeEach(function() {
delete require.cache['sendcloud'];
sc = sinon.createStubInstance(Sendcloud);
service = proxyquire('./service', {
'sendcloud': sinon.stub().returns(sc)
});
});
it('#restorePassword', function(done) {
sc.send.resolves({});
var obj = {
to: 'to',
name: 'name',
token: 'token'
};
service.restorePassword(obj, function() {
console.log(sc.send.args[0]);
done();
});
});
});

Categories

Resources