cryptpad-amnesia-store
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -10,3 +10,4 @@ /* | ||
Maybe you just like the idea of a forgetful pad? To use this module, edit | ||
config.js to include a directive `storage: './storage/amnesia' | ||
config.js to include a directive `storage: 'cryptpad-amnesia-store', after | ||
having installed it via npm with `npm install cryptpad-amnesia-store` | ||
@@ -20,5 +21,2 @@ Enjoy! | ||
var db=[], | ||
index=0; | ||
if (conf.removeChannels) { | ||
@@ -28,32 +26,70 @@ console.log("Server is set to remove channels %sms after the last remaining client leaves.", conf.channelRemovalTimeout); | ||
var db = {}; | ||
var getChannel = function (channelName) { | ||
return (channelName in db)? db[channelName]: (db[channelName] = []); | ||
}; | ||
// destructive! This function actually modifies the supplied array. | ||
var trimHistory = function (A) { | ||
var i = A.length -1; | ||
console.log(i); | ||
if (i < 0) { return; } | ||
// checkpoints found | ||
var msg; | ||
var cpf = 0; | ||
for (;i >= 0;i--) { | ||
try { | ||
msg = JSON.parse(A[i].msg); | ||
if (msg[4].indexOf('cp|') === 0) { | ||
if (++cpf === 2) { | ||
return A.slice(i); | ||
console.log(msg[4]); | ||
A.splice(0, i -1); | ||
return; | ||
} | ||
} | ||
} | ||
catch (err) { | ||
console.error(err); | ||
continue; | ||
} | ||
} | ||
return; | ||
}; | ||
cb({ | ||
message: function(channelName, content, cb){ | ||
var val = { | ||
id:index++, | ||
chan: channelName, | ||
msg: content, | ||
time: new Date().getTime(), | ||
time: +new Date() | ||
}; | ||
db.push(val); | ||
var channel = getChannel(channelName); | ||
channel.push(val); | ||
// clean up old longer histories even if people don't reload | ||
if (!conf.preserveHistory && channel.length > 250) { | ||
var tmp = trimHistory(channel); | ||
if (tmp) { channel = db[channelName] = tmp; } | ||
} | ||
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); | ||
}); | ||
var channel = getChannel(channelName); | ||
// when you fetch message history, you might as well try to trim it | ||
if (!conf.preserveHistory && channel.length > 100) { | ||
var tmp = trimHistory(channel); | ||
if (tmp) { channel = db[channelName] = tmp; } | ||
} | ||
channel.forEach(function (val) { handler(val.msg); }); | ||
if (cb) { cb(); } | ||
}, | ||
removeChannel: function (channelName, cb) { | ||
var err = false; | ||
db = db.filter(function (msg) { | ||
return msg.chan !== channelName; | ||
}); | ||
cb(err); | ||
if (channelName in db) { delete db[channelName]; } | ||
cb(); | ||
}, | ||
}); | ||
}; |
{ | ||
"name": "cryptpad-amnesia-store", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "storage api for cryptpad implemented as an in-memory store", | ||
@@ -5,0 +5,0 @@ "main": "amnesia.js", |
@@ -7,3 +7,3 @@ # cryptpad-amnesia-store | ||
Unsupported. | ||
Experimental. | ||
@@ -10,0 +10,0 @@ ## Why? |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38860
79