|
|
|
@ -3,13 +3,33 @@ define([
|
|
|
|
|
'/bower_components/localforage/dist/localforage.min.js',
|
|
|
|
|
], function (Util, localForage) {
|
|
|
|
|
var S = {};
|
|
|
|
|
var onReady = Util.mkEvent(true);
|
|
|
|
|
|
|
|
|
|
// Check if indexedDB is allowed
|
|
|
|
|
var allowed = false;
|
|
|
|
|
try {
|
|
|
|
|
let request = indexedDB.open('mydatabase', 1);
|
|
|
|
|
request.onsuccess = function () {
|
|
|
|
|
allowed = true;
|
|
|
|
|
onReady.fire();
|
|
|
|
|
};
|
|
|
|
|
request.onerror = function () {
|
|
|
|
|
onReady.fire();
|
|
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
onReady.fire();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cache = localForage.createInstance({
|
|
|
|
|
driver: localForage.INDEXEDDB,
|
|
|
|
|
name: "cp_cache"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
S.getBlobCache = function (id, cb) {
|
|
|
|
|
cb = Util.once(Util.mkAsync(cb || function () {}));
|
|
|
|
|
|
|
|
|
|
onReady.reg(function () {
|
|
|
|
|
if (!allowed) { return void cb('NOCACHE'); }
|
|
|
|
|
cache.getItem(id, function (err, obj) {
|
|
|
|
|
if (err || !obj || !obj.c) {
|
|
|
|
|
return void cb(err || 'EINVAL');
|
|
|
|
@ -18,9 +38,13 @@ define([
|
|
|
|
|
obj.t = +new Date();
|
|
|
|
|
cache.setItem(id, obj);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
S.setBlobCache = function (id, u8, cb) {
|
|
|
|
|
cb = Util.once(Util.mkAsync(cb || function () {}));
|
|
|
|
|
|
|
|
|
|
onReady.reg(function () {
|
|
|
|
|
if (!allowed) { return void cb('NOCACHE'); }
|
|
|
|
|
if (!u8) { return void cb('EINVAL'); }
|
|
|
|
|
cache.setItem(id, {
|
|
|
|
|
c: u8,
|
|
|
|
@ -28,12 +52,16 @@ define([
|
|
|
|
|
}, function (err) {
|
|
|
|
|
cb(err);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// id: channel ID or blob ID
|
|
|
|
|
// returns array of messages
|
|
|
|
|
S.getChannelCache = function (id, cb) {
|
|
|
|
|
cb = Util.once(Util.mkAsync(cb || function () {}));
|
|
|
|
|
|
|
|
|
|
onReady.reg(function () {
|
|
|
|
|
if (!allowed) { return void cb('NOCACHE'); }
|
|
|
|
|
cache.getItem(id, function (err, obj) {
|
|
|
|
|
if (err || !obj || !Array.isArray(obj.c)) {
|
|
|
|
|
return void cb(err || 'EINVAL');
|
|
|
|
@ -42,6 +70,7 @@ define([
|
|
|
|
|
obj.t = +new Date();
|
|
|
|
|
cache.setItem(id, obj);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Keep the last two checkpoint + any checkpoint that may exist in the last 100 messages
|
|
|
|
@ -64,6 +93,9 @@ define([
|
|
|
|
|
|
|
|
|
|
S.storeCache = function (id, validateKey, val, cb) {
|
|
|
|
|
cb = Util.once(Util.mkAsync(cb || function () {}));
|
|
|
|
|
|
|
|
|
|
onReady.reg(function (allowed) {
|
|
|
|
|
if (!allowed) { return void cb('NOCACHE'); }
|
|
|
|
|
if (!Array.isArray(val) || !validateKey) { return void cb('EINVAL'); }
|
|
|
|
|
checkCheckpoints(val);
|
|
|
|
|
cache.setItem(id, {
|
|
|
|
@ -73,18 +105,27 @@ define([
|
|
|
|
|
}, function (err) {
|
|
|
|
|
cb(err);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
S.clearChannel = function (id, cb) {
|
|
|
|
|
cb = Util.once(Util.mkAsync(cb || function () {}));
|
|
|
|
|
|
|
|
|
|
onReady.reg(function () {
|
|
|
|
|
if (!allowed) { return void cb('NOCACHE'); }
|
|
|
|
|
cache.removeItem(id, function () {
|
|
|
|
|
cb();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
S.clear = function (cb) {
|
|
|
|
|
cb = Util.once(Util.mkAsync(cb || function () {}));
|
|
|
|
|
|
|
|
|
|
onReady.reg(function () {
|
|
|
|
|
if (!allowed) { return void cb('NOCACHE'); }
|
|
|
|
|
cache.clear(cb);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.CryptPad_clearIndexedDB = S.clear;
|
|
|
|
|