optionally use bluebird
This commit is contained in:
parent
55a9a65da7
commit
ee68b21d91
10
client.js
10
client.js
|
@ -2,6 +2,12 @@
|
||||||
|
|
||||||
/*global Promise*/
|
/*global Promise*/
|
||||||
|
|
||||||
|
var PromiseA = Promise;
|
||||||
|
try {
|
||||||
|
PromiseA = require('bluebird').Promise;
|
||||||
|
} catch(e) {
|
||||||
|
console.warn("For better Promise support please use bluebird");
|
||||||
|
}
|
||||||
// TODO iterate over the prototype
|
// TODO iterate over the prototype
|
||||||
// translate request / response
|
// translate request / response
|
||||||
var sqlite3real = require('sqlite3');
|
var sqlite3real = require('sqlite3');
|
||||||
|
@ -25,7 +31,7 @@ function startServer(opts) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getConnection(opts) {
|
function getConnection(opts) {
|
||||||
return new Promise(function (resolve) {
|
return new PromiseA(function (resolve) {
|
||||||
//setTimeout(function () {
|
//setTimeout(function () {
|
||||||
var WebSocket = require('ws');
|
var WebSocket = require('ws');
|
||||||
var ws = new WebSocket('ws+unix:' + opts.sock);
|
var ws = new WebSocket('ws+unix:' + opts.sock);
|
||||||
|
@ -105,7 +111,7 @@ function create(opts) {
|
||||||
var messages = [];
|
var messages = [];
|
||||||
|
|
||||||
function init(opts) {
|
function init(opts) {
|
||||||
return new Promise(function (resolve) {
|
return new PromiseA(function (resolve, reject) {
|
||||||
var id = Math.random();
|
var id = Math.random();
|
||||||
|
|
||||||
ws.send(JSON.stringify({
|
ws.send(JSON.stringify({
|
||||||
|
|
12
server.js
12
server.js
|
@ -1,12 +1,18 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
/*global Promise*/
|
/*global Promise*/
|
||||||
|
|
||||||
|
var PromiseA = Promise;
|
||||||
|
try {
|
||||||
|
PromiseA = require('bluebird').Promise;
|
||||||
|
} catch(e) {
|
||||||
|
console.warn("For better Promise support please use bluebird");
|
||||||
|
}
|
||||||
var wsses = {};
|
var wsses = {};
|
||||||
|
|
||||||
function createApp(server, options) {
|
function createApp(server, options) {
|
||||||
|
|
||||||
if (wsses[options.filename]) {
|
if (wsses[options.filename]) {
|
||||||
return Promise.resolve(wsses[options.filename]);
|
return PromiseA.resolve(wsses[options.filename]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return require('./wrapper').create(options).then(function (db) {
|
return require('./wrapper').create(options).then(function (db) {
|
||||||
|
@ -119,7 +125,7 @@ function create(options) {
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var ps = [];
|
var ps = [];
|
||||||
|
|
||||||
ps.push(new Promise(function (resolve) {
|
ps.push(new PromiseA(function (resolve) {
|
||||||
fs.unlink(options.sock, function () {
|
fs.unlink(options.sock, function () {
|
||||||
// ignore error when socket doesn't exist
|
// ignore error when socket doesn't exist
|
||||||
|
|
||||||
|
@ -132,7 +138,7 @@ function create(options) {
|
||||||
return { masterClient: app.masterClient };
|
return { masterClient: app.masterClient };
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return Promise.all(ps).then(function (results) {
|
return PromiseA.all(ps).then(function (results) {
|
||||||
return results[1];
|
return results[1];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
14
wrapper.js
14
wrapper.js
|
@ -1,6 +1,12 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/*global Promise*/
|
/*global Promise*/
|
||||||
|
var PromiseA = Promise;
|
||||||
|
try {
|
||||||
|
PromiseA = require('bluebird').Promise;
|
||||||
|
} catch(e) {
|
||||||
|
console.warn("For better Promise support please use bluebird");
|
||||||
|
}
|
||||||
var sqlite3 = require('sqlite3');
|
var sqlite3 = require('sqlite3');
|
||||||
var dbs = {};
|
var dbs = {};
|
||||||
|
|
||||||
|
@ -35,7 +41,7 @@ function create(opts) {
|
||||||
var key = newOpts.key || opts.key;
|
var key = newOpts.key || opts.key;
|
||||||
var bits = newOpts.bits || opts.bits;
|
var bits = newOpts.bits || opts.bits;
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
return new PromiseA(function (resolve, reject) {
|
||||||
console.log('OPTS', opts);
|
console.log('OPTS', opts);
|
||||||
console.log('BITS', bits);
|
console.log('BITS', bits);
|
||||||
if (db._initialized) {
|
if (db._initialized) {
|
||||||
|
@ -62,13 +68,13 @@ function create(opts) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO db.run(sql, function () { resolve() });
|
// TODO db.run(sql, function () { resolve() });
|
||||||
setup.push(new Promise(function (resolve, reject) {
|
setup.push(new PromiseA(function (resolve, reject) {
|
||||||
db.run("PRAGMA KEY = \"x'" + sanitize(key) + "'\"", [], function (err) {
|
db.run("PRAGMA KEY = \"x'" + sanitize(key) + "'\"", [], function (err) {
|
||||||
if (err) { reject(err); return; }
|
if (err) { reject(err); return; }
|
||||||
resolve(this);
|
resolve(this);
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
setup.push(new Promise(function (resolve, reject) {
|
setup.push(new PromiseA(function (resolve, reject) {
|
||||||
//process.nextTick(function () {
|
//process.nextTick(function () {
|
||||||
db.run("PRAGMA CIPHER = 'aes-" + sanitize(bits) + "-cbc'", [], function (err) {
|
db.run("PRAGMA CIPHER = 'aes-" + sanitize(bits) + "-cbc'", [], function (err) {
|
||||||
if (err) { reject(err); return; }
|
if (err) { reject(err); return; }
|
||||||
|
@ -77,7 +83,7 @@ function create(opts) {
|
||||||
//});
|
//});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
Promise.all(setup).then(function () {
|
PromiseA.all(setup).then(function () {
|
||||||
// restore original functions
|
// restore original functions
|
||||||
resolve(db);
|
resolve(db);
|
||||||
}, reject);
|
}, reject);
|
||||||
|
|
Loading…
Reference in New Issue