sqlite3-cluster.js/server.js

147 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-07-22 00:58:34 +00:00
'use strict';
2015-07-23 02:58:18 +00:00
/*global Promise*/
2015-07-22 00:58:34 +00:00
2015-07-31 21:06:29 +00:00
var PromiseA = Promise;
try {
PromiseA = require('bluebird').Promise;
} catch(e) {
console.warn("For better Promise support please use bluebird");
}
2015-07-23 02:58:18 +00:00
var wsses = {};
function createApp(server, options) {
if (wsses[options.filename]) {
2015-07-31 21:06:29 +00:00
return PromiseA.resolve(wsses[options.filename]);
2015-07-23 02:58:18 +00:00
}
return require('./wrapper').create(options).then(function (db) {
2015-07-24 22:11:10 +00:00
2015-07-23 02:58:18 +00:00
var url = require('url');
2015-07-24 22:11:10 +00:00
//var express = require('express');
//var app = express();
2015-07-23 02:58:18 +00:00
var wss = server.wss;
2015-07-22 00:58:34 +00:00
2015-07-24 22:11:10 +00:00
function app(req, res) {
res.end('NOT IMPLEMENTED');
}
2015-07-23 02:58:18 +00:00
wss.on('connection', function (ws) {
2015-07-24 22:11:10 +00:00
2015-07-23 02:58:18 +00:00
var location = url.parse(ws.upgradeReq.url, true);
// you might use location.query.access_token to authenticate or share sessions
// or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312
2015-07-22 00:58:34 +00:00
2015-07-23 02:58:18 +00:00
ws.__session_id = location.query.session_id || Math.random();
2015-07-22 00:58:34 +00:00
2015-07-23 02:58:18 +00:00
ws.on('message', function (buffer) {
var cmd;
2015-07-22 00:58:34 +00:00
2015-07-23 02:58:18 +00:00
try {
cmd = JSON.parse(buffer.toString('utf8'));
} catch(e) {
2015-07-24 08:57:17 +00:00
console.error('[ERROR] parse json');
console.error(e);
console.error(buffer);
console.error();
2015-07-23 02:58:18 +00:00
ws.send(JSON.stringify({ type: 'error', value: { message: e.message, code: "E_PARSE_JSON" } }));
2015-07-24 08:57:17 +00:00
return;
2015-07-23 02:58:18 +00:00
}
2015-07-22 00:58:34 +00:00
2015-07-23 02:58:18 +00:00
switch(cmd.type) {
case 'init':
db[cmd.func].apply(db, cmd.args).then(function () {
var args = Array.prototype.slice.call(arguments);
var myself;
if (args[0] === db) {
args = [];
myself = true;
}
ws.send(JSON.stringify({
id: cmd.id
, self: myself
, args: args
//, this: this
}));
});
2015-07-23 02:58:18 +00:00
break;
2015-07-22 00:58:34 +00:00
2015-07-23 02:58:18 +00:00
case 'rpc':
if (!db._initialized) {
ws.send(JSON.stringify({
type: 'error'
, id: cmd.id
, args: [{ message: 'database has not been initialized' }]
, error: { message: 'database has not been initialized' }
}));
return;
}
2015-07-24 08:57:17 +00:00
cmd.args.push(function () {
var args = Array.prototype.slice.call(arguments);
var myself;
if (args[0] === db) {
args = [];
myself = true;
}
2015-07-24 08:57:17 +00:00
ws.send(JSON.stringify({
this: this
, args: args
, self: myself
2015-07-24 08:57:17 +00:00
, id: cmd.id
}));
});
db[cmd.func].apply(db, cmd.args);
2015-07-23 02:58:18 +00:00
break;
2015-07-22 00:58:34 +00:00
2015-07-23 02:58:18 +00:00
default:
2015-07-24 22:11:10 +00:00
throw new Error('UNKNOWN TYPE');
//break;
2015-07-23 02:58:18 +00:00
}
2015-07-22 00:58:34 +00:00
2015-07-23 02:58:18 +00:00
});
ws.send(JSON.stringify({ type: 'session', value: ws.__session_id }));
2015-07-22 00:58:34 +00:00
});
2015-07-23 02:58:18 +00:00
app.masterClient = db;
wsses[options.filename] = app;
return app;
2015-07-22 00:58:34 +00:00
});
2015-07-23 02:58:18 +00:00
}
2015-07-22 00:58:34 +00:00
2015-07-23 02:58:18 +00:00
function create(options) {
2015-07-24 22:11:10 +00:00
var server = require('http').createServer();
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ server: server });
//var port = process.env.PORT || process.argv[0] || 4080;
2015-07-22 00:58:34 +00:00
2015-07-24 22:11:10 +00:00
var fs = require('fs');
var ps = [];
2015-07-23 02:58:18 +00:00
2015-07-31 21:06:29 +00:00
ps.push(new PromiseA(function (resolve) {
2015-07-23 02:58:18 +00:00
fs.unlink(options.sock, function () {
// ignore error when socket doesn't exist
2015-07-24 22:11:10 +00:00
server.listen(options.sock, resolve);
2015-07-23 02:58:18 +00:00
});
2015-07-24 22:11:10 +00:00
}));
2015-07-23 02:58:18 +00:00
2015-07-24 22:11:10 +00:00
ps.push(createApp({ server: server, wss: wss }, options).then(function (app) {
server.on('request', app);
return { masterClient: app.masterClient };
}));
2015-07-31 21:06:29 +00:00
return PromiseA.all(ps).then(function (results) {
2015-07-24 22:11:10 +00:00
return results[1];
2015-07-22 00:58:34 +00:00
});
}
module.exports.create = create;