Skip to content

Commit

Permalink
Update socketcluster
Browse files Browse the repository at this point in the history
Fix #64
  • Loading branch information
zalmoxisus committed Dec 5, 2018
1 parent 0be73be commit d68a756
Show file tree
Hide file tree
Showing 5 changed files with 2,745 additions and 103 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var getPort = require('getport');
var SocketCluster = require('socketcluster');
var getOptions = require('./lib/options');

var LOG_LEVEL_NONE = 0;
Expand All @@ -7,7 +8,6 @@ var LOG_LEVEL_WARN = 2;
var LOG_LEVEL_INFO = 3;

module.exports = function(argv) {
var SocketCluster = require('socketcluster').SocketCluster;
var options = Object.assign(getOptions(argv), {
workerController: __dirname + '/lib/worker.js',
allowClientPublish: false
Expand Down
2 changes: 1 addition & 1 deletion lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ module.exports = function getOptions(argv) {
maxRequestBody: argv.passphrase || '16mb',
logHTTPRequests: argv.logHTTPRequests,
logLevel: argv.logLevel || 3,
wsEngine: argv.wsEngine || process.env.npm_package_remotedev_wsengine || 'uws'
wsEngine: argv.wsEngine || process.env.npm_package_remotedev_wsengine || 'ws'
};
}
205 changes: 107 additions & 98 deletions lib/worker.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var SCWorker = require("socketcluster/scworker");
var path = require('path');
var app = require('express')();
var bodyParser = require('body-parser');
Expand All @@ -7,116 +8,124 @@ var graphiqlMiddleware = require('./middleware/graphiql');
var graphqlMiddleware = require('./middleware/graphql');
var createStore = require('./store');

module.exports.run = function(worker) {
var httpServer = worker.httpServer;
var scServer = worker.scServer;
var store = createStore(worker.options);
var limit = worker.options.maxRequestBody;
var logHTTPRequests = worker.options.logHTTPRequests;
class Worker extends SCWorker {
run() {
var httpServer = this.httpServer;
var scServer = this.scServer;
var options = this.options;
var store = createStore(options);
var limit = options.maxRequestBody;
var logHTTPRequests = options.logHTTPRequests;

httpServer.on('request', app);
httpServer.on('request', app);

app.set('view engine', 'ejs');
app.set('views', path.resolve(__dirname, '..', 'views'));

if (logHTTPRequests) {
if (typeof logHTTPRequests === 'object') app.use(morgan('combined', logHTTPRequests));
else app.use(morgan('combined'));
}
app.set('view engine', 'ejs');
app.set('views', path.resolve(__dirname, '..', 'views'));

app.use('/graphiql', graphiqlMiddleware);
if (logHTTPRequests) {
if (typeof logHTTPRequests === 'object') app.use(morgan('combined', logHTTPRequests));
else app.use(morgan('combined'));
}

app.get('*', function(req, res) {
res.render('index', { port: worker.options.port });
});
app.use('/graphiql', graphiqlMiddleware);

app.use(cors({ methods: 'POST' }));
app.use(bodyParser.json({ limit: limit }));
app.use(bodyParser.urlencoded({ limit: limit, extended: false }));
app.get('*', function (req, res) {
res.render('index', {port: options.port});
});

app.use('/graphql', graphqlMiddleware(store));
app.use(cors({methods: 'POST'}));
app.use(bodyParser.json({limit: limit}));
app.use(bodyParser.urlencoded({limit: limit, extended: false}));

app.post('/', function(req, res) {
if (!req.body) return res.status(404).end();
switch(req.body.op) {
case 'get':
store.get(req.body.id).then(function(r) {
res.send(r || {});
}).catch(function(error) {
console.error(error);
res.sendStatus(500)
});
break;
case 'list':
store.list(req.body.query, req.body.fields).then(function(r) {
res.send(r);
}).catch(function(error) {
console.error(error);
res.sendStatus(500)
});
break;
default:
store.add(req.body).then(function(r) {
res.send({ id: r.id, error: r.error });
scServer.exchange.publish('report', {
type: 'add', data: r
app.use('/graphql', graphqlMiddleware(store));

app.post('/', function (req, res) {
if (!req.body) return res.status(404).end();
switch (req.body.op) {
case 'get':
store.get(req.body.id).then(function (r) {
res.send(r || {});
}).catch(function (error) {
console.error(error);
res.sendStatus(500)
});
break;
case 'list':
store.list(req.body.query, req.body.fields).then(function (r) {
res.send(r);
}).catch(function (error) {
console.error(error);
res.sendStatus(500)
});
}).catch(function(error) {
break;
default:
store.add(req.body).then(function (r) {
res.send({id: r.id, error: r.error});
scServer.exchange.publish('report', {
type: 'add', data: r
});
}).catch(function (error) {
console.error(error);
res.status(500).send({})
});
}
});

scServer.addMiddleware(scServer.MIDDLEWARE_EMIT, function (req, next) {
var channel = req.event;
var data = req.data;
if (channel.substr(0, 3) === 'sc-' || channel === 'respond' || channel === 'log') {
scServer.exchange.publish(channel, data);
} else if (channel === 'log-noid') {
scServer.exchange.publish('log', {id: req.socket.id, data: data});
}
next();
});

scServer.addMiddleware(scServer.MIDDLEWARE_SUBSCRIBE, function (req, next) {
next();
if (req.channel === 'report') {
store.list().then(function (data) {
req.socket.emit(req.channel, {type: 'list', data: data});
}).catch(function (error) {
console.error(error);
res.status(500).send({})
});
}
});

scServer.addMiddleware(scServer.MIDDLEWARE_EMIT, function (req, next) {
var channel = req.event;
var data = req.data;
if (channel.substr(0, 3) === 'sc-' || channel === 'respond' || channel === 'log') {
scServer.exchange.publish(channel, data);
} else if (channel === 'log-noid') {
scServer.exchange.publish('log', { id: req.socket.id, data: data });
}
next();
});
}
});

scServer.addMiddleware(scServer.MIDDLEWARE_SUBSCRIBE, function (req, next) {
next();
if (req.channel === 'report') {
store.list().then(function(data) {
req.socket.emit(req.channel, { type: 'list', data: data });
}).catch(function(error) {
console.error(error);
scServer.on('connection', function (socket) {
var channelToWatch, channelToEmit;
socket.on('login', function (credentials, respond) {
if (credentials === 'master') {
channelToWatch = 'respond';
channelToEmit = 'log';
} else {
channelToWatch = 'log';
channelToEmit = 'respond';
}
this.exchange.subscribe('sc-' + socket.id).watch(function (msg) {
socket.emit(channelToWatch, msg);
});
respond(null, channelToWatch);
});
}
});

scServer.on('connection', function(socket) {
var channelToWatch, channelToEmit;
socket.on('login', function (credentials, respond) {
if (credentials === 'master') {
channelToWatch = 'respond'; channelToEmit = 'log';
} else {
channelToWatch = 'log'; channelToEmit = 'respond';
}
worker.exchange.subscribe('sc-' + socket.id).watch(function(msg) {
socket.emit(channelToWatch, msg);
socket.on('getReport', function (id, respond) {
store.get(id).then(function (data) {
respond(null, data);
}).catch(function (error) {
console.error(error);
});
});
respond(null, channelToWatch);
});
socket.on('getReport', function (id, respond) {
store.get(id).then(function(data) {
respond(null, data);
}).catch(function(error) {
console.error(error);
socket.on('disconnect', function () {
var channel = this.exchange.channel('sc-' + socket.id);
channel.unsubscribe();
channel.destroy();
scServer.exchange.publish(
channelToEmit,
{id: socket.id, type: 'DISCONNECTED'}
);
});
});
socket.on('disconnect', function() {
var channel = worker.exchange.channel('sc-' + socket.id);
channel.unsubscribe(); channel.destroy();
scServer.exchange.publish(
channelToEmit,
{ id: socket.id, type: 'DISCONNECTED' }
);
});
});
};
};
}

new Worker();
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"remotedev"
],
"engines": {
"node": ">=4.0.0"
"node": ">=6.0.0"
},
"author": "Mihail Diordiev <zalmoxisus@gmail.com> (https://github.com/zalmoxisus)",
"license": "MIT",
Expand All @@ -51,14 +51,14 @@
"minimist": "^1.2.0",
"morgan": "^1.7.0",
"semver": "^5.3.0",
"socketcluster": "^8.0.0",
"socketcluster": "^14.3.3",
"sqlite3": "^3.1.8",
"uuid": "^3.0.1"
},
"devDependencies": {
"expect": "^1.20.2",
"mocha": "^3.2.0",
"socketcluster-client": "^5.1.1",
"socketcluster-client": "^14.0.0",
"supertest": "^3.0.0"
}
}
Loading

0 comments on commit d68a756

Please sign in to comment.