cryptpad/www/common/cryptget.js

114 lines
3.4 KiB
JavaScript
Raw Normal View History

define([
'/bower_components/chainpad-crypto/crypto.js',
'/bower_components/chainpad-netflux/chainpad-netflux.js',
2017-11-13 15:32:40 +00:00
'/common/common-util.js',
'/common/common-hash.js',
'/common/common-realtime.js',
2017-11-23 11:28:49 +00:00
'/common/outer/network-config.js',
'/bower_components/chainpad/chainpad.dist.js',
], function (Crypto, CPNetflux, Util, Hash, Realtime, NetConfig) {
2016-12-22 15:00:13 +00:00
var finish = function (S, err, doc) {
if (S.done) { return; }
S.cb(err, doc);
S.done = true;
2018-10-18 16:50:38 +00:00
if (!S.hasNetwork) {
var disconnect = Util.find(S, ['network', 'disconnect']);
if (typeof(disconnect) === 'function') { disconnect(); }
}
if (S.leave) {
try {
S.leave();
} catch (e) { console.log(e); }
}
var abort = Util.find(S, ['session', 'realtime', 'abort']);
2016-12-22 15:00:13 +00:00
if (typeof(abort) === 'function') {
2018-10-18 16:50:38 +00:00
S.session.realtime.sync();
2016-12-22 15:00:13 +00:00
abort();
}
2016-12-22 15:00:13 +00:00
};
var makeConfig = function (hash, password) {
// We can't use cryptget with a file or a user so we can use 'pad' as hash type
var secret = Hash.getSecrets('pad', hash, password);
2016-12-22 15:00:13 +00:00
if (!secret.keys) { secret.keys = secret.key; } // support old hashses
var config = {
2017-11-23 11:28:49 +00:00
websocketURL: NetConfig.getWebsocketURL(),
channel: secret.channel,
2016-12-22 15:00:13 +00:00
validateKey: secret.keys.validateKey || undefined,
crypto: Crypto.createEncryptor(secret.keys),
logLevel: 0,
};
2016-12-22 15:00:13 +00:00
return config;
};
2016-12-22 15:00:13 +00:00
var isObject = function (o) {
return typeof(o) === 'object';
};
var overwrite = function (a, b) {
if (!(isObject(a) && isObject(b))) { return; }
Object.keys(b).forEach(function (k) { a[k] = b[k]; });
};
var get = function (hash, cb, opt) {
if (typeof(cb) !== 'function') {
throw new Error('Cryptget expects a callback');
}
opt = opt || {};
var config = makeConfig(hash, opt.password);
2018-10-18 16:50:38 +00:00
var Session = { cb: cb, hasNetwork: Boolean(opt.network) };
2017-05-04 14:16:09 +00:00
config.onReady = function (info) {
2016-12-22 15:00:13 +00:00
var rt = Session.session = info.realtime;
Session.network = info.network;
2018-10-18 16:50:38 +00:00
Session.leave = info.leave;
2016-12-22 15:00:13 +00:00
finish(Session, void 0, rt.getUserDoc());
};
config.onChannelError = function (info) {
finish(Session, info.error);
};
2016-12-22 15:00:13 +00:00
overwrite(config, opt);
2017-11-13 15:32:40 +00:00
Session.realtime = CPNetflux.start(config);
2016-12-22 15:00:13 +00:00
};
2016-12-22 15:00:13 +00:00
var put = function (hash, doc, cb, opt) {
if (typeof(cb) !== 'function') {
throw new Error('Cryptput expects a callback');
}
opt = opt || {};
2016-12-22 15:00:13 +00:00
var config = makeConfig(hash, opt.password);
2016-12-22 15:00:13 +00:00
var Session = { cb: cb, };
2016-12-22 15:00:13 +00:00
config.onReady = function (info) {
var realtime = Session.session = info.realtime;
Session.network = info.network;
2016-12-22 15:00:13 +00:00
realtime.contentUpdate(doc);
2016-12-22 15:00:13 +00:00
2017-11-30 14:01:17 +00:00
var to = setTimeout(function () {
cb(new Error("Timeout"));
}, 5000);
2017-11-13 15:32:40 +00:00
Realtime.whenRealtimeSyncs(realtime, function () {
2017-11-30 14:01:17 +00:00
clearTimeout(to);
realtime.abort();
finish(Session, void 0);
});
};
2016-12-22 15:00:13 +00:00
overwrite(config, opt);
2017-11-13 15:32:40 +00:00
Session.session = CPNetflux.start(config);
};
2016-12-22 15:00:13 +00:00
return {
get: get,
put: put,
};
});