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
|
|
|
// TODO iterate over the prototype
|
|
|
|
// translate request / response
|
|
|
|
var sqlite3real = require('sqlite3');
|
|
|
|
|
|
|
|
/*
|
|
|
|
function createConnection(opts) {
|
|
|
|
var server = ;
|
|
|
|
|
|
|
|
return server.create(opts).then(function () {
|
|
|
|
// created and listening
|
|
|
|
});
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
function startServer(opts) {
|
|
|
|
return require('./server').create(opts).then(function (server) {
|
|
|
|
// this process doesn't need to connect to itself
|
|
|
|
// through a socket
|
|
|
|
return server.masterClient;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getConnection(opts) {
|
2015-07-31 21:06:29 +00:00
|
|
|
return new PromiseA(function (resolve) {
|
2015-07-24 22:11:10 +00:00
|
|
|
//setTimeout(function () {
|
2015-07-23 02:58:18 +00:00
|
|
|
var WebSocket = require('ws');
|
|
|
|
var ws = new WebSocket('ws+unix:' + opts.sock);
|
|
|
|
|
|
|
|
ws.on('error', function (err) {
|
|
|
|
console.error('[ERROR] ws connection failed, retrying');
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
function retry() {
|
|
|
|
setTimeout(function () {
|
2015-07-24 22:11:10 +00:00
|
|
|
getConnection(opts).then(resolve, retry);
|
2015-07-23 02:58:18 +00:00
|
|
|
}, 100 + (Math.random() * 250));
|
|
|
|
}
|
|
|
|
|
2015-07-24 19:58:29 +00:00
|
|
|
if (!opts.connect && ('ENOENT' === err.code || 'ECONNREFUSED' === err.code)) {
|
|
|
|
console.log('[NO SERVER] attempting to create a server #######################');
|
2015-07-23 02:58:18 +00:00
|
|
|
return startServer(opts).then(function (client) {
|
|
|
|
// ws.masterClient = client;
|
|
|
|
resolve({ masterClient: client });
|
2015-07-29 00:04:54 +00:00
|
|
|
}, function (err) {
|
|
|
|
console.error('[ERROR] failed to connect to sqlite3-cluster service. retrying...');
|
|
|
|
console.error(err);
|
2015-07-23 02:58:18 +00:00
|
|
|
retry();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
retry();
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.on('open', function () {
|
|
|
|
resolve(ws);
|
|
|
|
});
|
2015-07-24 22:11:10 +00:00
|
|
|
//}, 100 + (Math.random() * 250));
|
|
|
|
});
|
2015-07-23 02:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function create(opts) {
|
2015-07-24 19:58:29 +00:00
|
|
|
if (!opts.sock) {
|
|
|
|
opts.sock = opts.filename + '.sock';
|
|
|
|
}
|
|
|
|
|
|
|
|
var promise;
|
|
|
|
var numcpus = require('os').cpus().length;
|
|
|
|
if (opts.standalone || (1 === numcpus && !opts.serve && !opts.connect)) {
|
|
|
|
return require('./wrapper').create(opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
function retryServe() {
|
|
|
|
return startServer(opts).then(function (client) {
|
|
|
|
// ws.masterClient = client;
|
|
|
|
return { masterClient: client };
|
2015-07-24 22:11:10 +00:00
|
|
|
}, function (err) {
|
|
|
|
console.error('[ERROR] retryServe()');
|
|
|
|
console.error(err);
|
2015-07-24 19:58:29 +00:00
|
|
|
retryServe();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.serve) {
|
|
|
|
promise = retryServe();
|
|
|
|
} else {
|
|
|
|
promise = getConnection(opts);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
if (opts.connect) {
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2015-07-23 02:58:18 +00:00
|
|
|
// TODO maybe use HTTP POST instead?
|
2015-07-24 19:58:29 +00:00
|
|
|
return promise.then(function (ws) {
|
2015-07-23 02:58:18 +00:00
|
|
|
if (ws.masterClient) {
|
|
|
|
return ws.masterClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
var db = {};
|
|
|
|
var proto = sqlite3real.Database.prototype;
|
2015-07-24 08:57:17 +00:00
|
|
|
var messages = [];
|
2015-07-23 02:58:18 +00:00
|
|
|
|
2015-07-29 00:04:54 +00:00
|
|
|
function init(opts) {
|
2015-07-31 21:06:29 +00:00
|
|
|
return new PromiseA(function (resolve, reject) {
|
2015-07-29 00:04:54 +00:00
|
|
|
var id = Math.random();
|
|
|
|
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
type: 'init'
|
|
|
|
, args: [opts]
|
|
|
|
, func: 'init'
|
|
|
|
, filename: opts.filename
|
|
|
|
, id: id
|
|
|
|
}));
|
|
|
|
|
|
|
|
function onMessage(data) {
|
|
|
|
var cmd;
|
|
|
|
|
|
|
|
try {
|
|
|
|
cmd = JSON.parse(data.toString('utf8'));
|
|
|
|
} catch(e) {
|
|
|
|
console.error('[ERROR] in client, from sql server parse json');
|
|
|
|
console.error(e);
|
|
|
|
console.error(data);
|
|
|
|
console.error();
|
|
|
|
|
|
|
|
//ws.send(JSON.stringify({ type: 'error', value: { message: e.message, code: "E_PARSE_JSON" } }));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.id !== id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.self) {
|
|
|
|
cmd.args = [db];
|
|
|
|
}
|
|
|
|
|
|
|
|
messages.splice(messages.indexOf(onMessage), 1);
|
|
|
|
|
|
|
|
if ('error' === cmd.type) {
|
|
|
|
reject(cmd.args[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(cmd.args[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
messages.push(onMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function rpcThunk(fname, args) {
|
2015-07-23 02:58:18 +00:00
|
|
|
var id;
|
|
|
|
var cb;
|
|
|
|
|
|
|
|
if ('function' === typeof args[args.length - 1]) {
|
|
|
|
id = Math.random();
|
|
|
|
cb = args.pop();
|
|
|
|
}
|
|
|
|
|
2015-07-24 08:57:17 +00:00
|
|
|
ws.send(JSON.stringify({
|
2015-07-23 02:58:18 +00:00
|
|
|
type: 'rpc'
|
|
|
|
, func: fname
|
|
|
|
, args: args
|
|
|
|
, filename: opts.filename
|
|
|
|
, id: id
|
2015-07-24 08:57:17 +00:00
|
|
|
}));
|
2015-07-23 02:58:18 +00:00
|
|
|
|
|
|
|
if (!cb) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onMessage(data) {
|
2015-07-24 08:57:17 +00:00
|
|
|
var cmd;
|
|
|
|
|
|
|
|
try {
|
|
|
|
cmd = JSON.parse(data.toString('utf8'));
|
|
|
|
} catch(e) {
|
|
|
|
console.error('[ERROR] in client, from sql server parse json');
|
|
|
|
console.error(e);
|
|
|
|
console.error(data);
|
|
|
|
console.error();
|
|
|
|
|
|
|
|
//ws.send(JSON.stringify({ type: 'error', value: { message: e.message, code: "E_PARSE_JSON" } }));
|
2015-07-23 02:58:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-24 08:57:17 +00:00
|
|
|
if (cmd.id !== id) {
|
2015-07-23 02:58:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-29 00:04:54 +00:00
|
|
|
if (cmd.self) {
|
|
|
|
cmd.args = [db];
|
|
|
|
}
|
2015-07-24 08:57:17 +00:00
|
|
|
cb.apply(cmd.this, cmd.args);
|
|
|
|
|
|
|
|
if ('on' !== fname) {
|
|
|
|
var index = messages.indexOf(onMessage);
|
|
|
|
messages.splice(index, 1);
|
|
|
|
}
|
2015-07-23 02:58:18 +00:00
|
|
|
}
|
|
|
|
|
2015-07-24 08:57:17 +00:00
|
|
|
messages.push(onMessage);
|
2015-07-23 02:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db.sanitize = require('./wrapper').sanitize;
|
2015-07-24 20:47:11 +00:00
|
|
|
db.escape = require('./wrapper').escape;
|
2015-07-23 02:58:18 +00:00
|
|
|
|
2015-07-29 00:04:54 +00:00
|
|
|
// TODO get methods from server (cluster-store does this)
|
|
|
|
// instead of using the prototype
|
2015-07-23 02:58:18 +00:00
|
|
|
Object.keys(sqlite3real.Database.prototype).forEach(function (key) {
|
|
|
|
|
|
|
|
if ('function' === typeof proto[key]) {
|
|
|
|
db[key] = function () {
|
2015-07-29 00:04:54 +00:00
|
|
|
rpcThunk(key, Array.prototype.slice.call(arguments));
|
2015-07-23 02:58:18 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-07-29 00:04:54 +00:00
|
|
|
db.init = init;
|
|
|
|
|
2015-07-24 08:57:17 +00:00
|
|
|
ws.on('message', function (data) {
|
|
|
|
messages.forEach(function (fn) {
|
|
|
|
try {
|
|
|
|
fn(data);
|
|
|
|
} catch(e) {
|
2015-07-24 22:11:10 +00:00
|
|
|
console.error("[ERROR] ws.on('message', fn) (multi-callback)");
|
|
|
|
console.error(e);
|
2015-07-24 08:57:17 +00:00
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-07-23 02:58:18 +00:00
|
|
|
|
|
|
|
// serialize
|
|
|
|
// parallel
|
|
|
|
db.serialize = db.parallel = function () {
|
|
|
|
throw new Error('NOT IMPLEMENTED in SQLITE3-remote');
|
|
|
|
};
|
|
|
|
|
|
|
|
return db;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.sanitize = require('./wrapper').sanitize;
|
2015-07-24 20:47:11 +00:00
|
|
|
module.exports.escape = require('./wrapper').escape;
|
2015-07-23 02:58:18 +00:00
|
|
|
module.exports.create = create;
|