From b46f74cd78aa2f5265a0ede76ec104c4162d4ca9 Mon Sep 17 00:00:00 2001 From: ansuz Date: Wed, 14 Sep 2016 11:47:54 +0200 Subject: [PATCH] remove alternative storage adaptors from core --- storage/amnesia.js | 56 ----------------------------------- storage/kad.js | 51 -------------------------------- storage/lvl.js | 73 ---------------------------------------------- storage/mongo.js | 61 -------------------------------------- storage/sql.js | 58 ------------------------------------ 5 files changed, 299 deletions(-) delete mode 100644 storage/amnesia.js delete mode 100644 storage/kad.js delete mode 100644 storage/lvl.js delete mode 100644 storage/mongo.js delete mode 100644 storage/sql.js diff --git a/storage/amnesia.js b/storage/amnesia.js deleted file mode 100644 index b7c1702e6..000000000 --- a/storage/amnesia.js +++ /dev/null @@ -1,56 +0,0 @@ -/* - As the log statement says, this module does nothing to persist your data - across sessions. If your process crashes for any reason, all pads will die. - - This might be useful if you want to debug other parts of the codebase, if - you want to test out cryptpad without installing mongodb locally, or if - you don't want to rely on a remote db like the one at mongolab.com. - - Maybe you just like the idea of a forgetful pad? To use this module, edit - config.js to include a directive `storage: './storage/amnesia' - - Enjoy! -*/ - -module.exports.create = function(conf, cb){ - console.log("Loading amnesiadb. This is a horrible idea in production,"+ - " as data *will not* persist\n"); - - var db=[], - index=0; - - if (conf.removeChannels) { - console.log("Server is set to remove channels %sms after the last remaining client leaves.", conf.channelRemovalTimeout); - } - - cb({ - message: function(channelName, content, cb){ - var val = { - id:index++, - chan: channelName, - msg: content, - time: new Date().getTime(), - }; - db.push(val); - if (cb) { cb(); } - }, - getMessages: function(channelName, handler, cb){ - db.sort(function(a,b){ - return a.id - b.id; - }); - db.filter(function(val){ - return val.chan === channelName; - }).forEach(function(doc){ - handler(doc.msg); - }); - if (cb) { cb(); } - }, - removeChannel: function (channelName, cb) { - var err = false; - db = db.filter(function (msg) { - return msg.chan !== channelName; - }); - cb(err); - }, - }); -}; diff --git a/storage/kad.js b/storage/kad.js deleted file mode 100644 index 84287c70e..000000000 --- a/storage/kad.js +++ /dev/null @@ -1,51 +0,0 @@ -var kad=require("kad"); -var levelup=require("levelup"); - -/* - THiS FILE IS NOT PRODUCTION READY - DON'T USE IT! -*/ - -module.exports.create=function(conf,cb){ - var dht= kad({ - address:conf.kadAddress, - port:conf.kadPort, - storage:levelup(conf.kadStore), - seeds:conf.kadSeeds, - transport: kad.transports.UDP, - }); - - var getIndex=function(cName,f){ - dht.get(cName+'=>index',function(e,out){ - e && console.error(e) || f(Number(out)); - }); - }; - - cb({ - message:function(cName, content, cb){ - getIndex(cName, function(index){ - index+=1; - dht.put(cName+'=>index', ''+index,function(e){ - e && console.error("ERROR updating index (%s): %s",index,e) || - console.log("PUT SUCCESS: %s", cName+'=>index') - }); - dht.put(cName+'=>'+index, content, function(e){ - e && console.error("ERROR updating value at %s: %s",cName+'=>'+index,e)|| - console.log("PUT SUCCESS: %s", cName+'=>index') - cb(); - }); - }); - }, - getMessages: function(cName, cb){ - getIndex(cName, function(index){ - for(var i=index;i>=0;i--){ - dht.get(cName+'=>'+i,function(e,out){ - if(e) return console.error("DHT GET ERROR: %s",e); - console.log("GET SUCCESS: %s", cName+'=>index') - cb(out); - }); - } - }); - }, - }); -}; diff --git a/storage/lvl.js b/storage/lvl.js deleted file mode 100644 index ff6713a0c..000000000 --- a/storage/lvl.js +++ /dev/null @@ -1,73 +0,0 @@ -var Level = require("level"); -var nThen = require('nthen'); - -var getIndex = function(db, cName, cb) { - db.get(cName+'=>index', function(e, out){ - if (e) { - if (e.notFound) { - cb(-1); - } else { - throw e; - } - return; - } - cb(parseInt(out)); - }); -}; - -var insert = function (db, channelName, content, cb) { - var index; - var doIt = function () { - db.locked = true; - nThen(function (waitFor) { - getIndex(db, channelName, waitFor(function (i) { index = i+1; })); - }).nThen(function (waitFor) { - db.put(channelName+'=>'+index, content, waitFor(function (e) { if (e) { throw e; } })); - }).nThen(function (waitFor) { - db.put(channelName+'=>index', ''+index, waitFor(function (e) { if (e) { throw e; } })); - }).nThen(function (waitFor) { - db.locked = false; - if (!db.queue.length) { return; } - db.queue.shift()(); - }).nThen(cb); - }; - if (db.locked) { - db.queue.push(doIt); - } else { - doIt(); - } -}; - -var getMessages = function (db, channelName, msgHandler, cb) { - var index; - nThen(function (waitFor) { - getIndex(db, channelName, waitFor(function (i) { - index = i; - })); - }).nThen(function (waitFor) { - var again = function (i) { - db.get(channelName + '=>' + i, waitFor(function (e, out) { - if (e) { throw e; } - msgHandler(out); - if (i < index) { again(i+1); } - else if (cb) { cb(); } - })); - }; - if (index > -1) { again(0); } - else if (cb) { cb(); } - }); -}; - -module.exports.create = function (conf, cb) { - var db = Level(conf.levelPath || './test.level.db'); - db.locked = false; - db.queue = []; - cb({ - message: function (channelName, content, cb) { - insert(db, channelName, content, cb); - }, - getMessages: function (channelName, msgHandler, cb) { - getMessages(db, channelName, msgHandler, cb); - } - }); -}; diff --git a/storage/mongo.js b/storage/mongo.js deleted file mode 100644 index 67f662a09..000000000 --- a/storage/mongo.js +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2014 XWiki SAS - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -var MongoClient = require('mongodb').MongoClient; - -var MONGO_URI = "mongodb://demo_user:demo_password@ds027769.mongolab.com:27769/demo_database"; -var COLLECTION_NAME = 'cryptpad'; - -var insert = function (coll, channelName, content, cb) { - var val = {chan: channelName, msg:content, time: (new Date()).getTime()}; - coll.insertOne(val, {}, function (err, r) { - console.log(r); - if (err || (r.insertedCount !== 1)) { - console.log('failed to insert ' + err); - return; - } - cb(); - }); -}; - -var getMessages = function (coll, channelName, cb) { - // find entries with a matching channelname - coll.find({chan:channelName}) - // sort by _id, ascending - .sort( { _id: 1 } ) - // iterate over entries - .forEach(function (doc) { - cb(doc.msg); - }, function (err) { - if (!err) { return; } - console.log('error ' + err); - }); -}; - -module.exports.create = function (conf, cb) { - MongoClient.connect(conf.mongoUri, function(err, db) { - var coll = db.collection(conf.mongoCollectionName); - if (err) { throw err; } - cb({ - message: function (channelName, content, cb) { - insert(coll, channelName, content, cb); - }, - getMessages: function (channelName, msgHandler) { - getMessages(coll, channelName, msgHandler); - } - }); - }); -}; diff --git a/storage/sql.js b/storage/sql.js deleted file mode 100644 index 9bb07519d..000000000 --- a/storage/sql.js +++ /dev/null @@ -1,58 +0,0 @@ -var Knex = require("knex"); - -var getMessages = function (knex, channel, msgHandler, cb) { - return knex('messages') - .where({ - channel: channel, - }) - .select('*') - .then(function (rows) { - rows.forEach(function (row) { - msgHandler(row.content); - }); - cb(); - }) - .catch(function (e) { - console.error(e); - cb(); - }); -}; - -var insert = function (knex, channel, content, cb) { - knex.table('messages').insert({ - channel: channel, - content: content, - }) - .then(function () { - cb(); - }); -}; - -module.exports.create = function (conf, cb) { - var knex = Knex({ - dialect: 'sqlite3', - connection: conf.dbConnection, - useNullAsDefault: true, - }); - - knex.schema.hasTable('messages').then(function (exists) { - if (exists) { return; } - - return knex.schema.createTable('messages', function (table) { - table.increments('id'); - table.string('content'); - table.string('channel'); - table.timestamps(); - }); - }) - .then(function () { - cb({ - message: function (channelName, content, cb) { - insert(knex, channelName, content, cb); - }, - getMessages: function (channelName, msgHandler, cb) { - getMessages(knex, channelName, msgHandler, cb); - }, - }); - }); -};