cryptpad/www/common/cryptget.js

99 lines
3 KiB
JavaScript
Raw Normal View History

define([
'jquery',
'/bower_components/chainpad-crypto/crypto.js',
'/bower_components/chainpad-netflux/chainpad-netflux.js',
'/common/cryptpad-common.js',
'/bower_components/textpatcher/TextPatcher.js'
], function ($, Crypto, Realtime, Cryptpad, TextPatcher) {
2017-05-04 14:16:09 +00:00
//var Messages = Cryptpad.Messages;
//var noop = function () {};
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;
var disconnect = Cryptpad.find(S, ['network', 'disconnect']);
if (typeof(disconnect) === 'function') { disconnect(); }
2016-12-22 15:00:13 +00:00
var abort = Cryptpad.find(S, ['realtime', 'realtime', 'abort']);
if (typeof(abort) === 'function') {
S.realtime.realtime.sync();
abort();
}
2016-12-22 15:00:13 +00:00
};
2016-12-22 15:00:13 +00:00
var makeConfig = function (hash) {
// We can't use cryptget with a file or a user so we can use 'pad' as hash type
var secret = Cryptpad.getSecrets('pad', hash);
2016-12-22 15:00:13 +00:00
if (!secret.keys) { secret.keys = secret.key; } // support old hashses
var config = {
websocketURL: Cryptpad.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');
}
var Session = { cb: cb, };
var config = makeConfig(hash);
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;
2016-12-22 15:00:13 +00:00
finish(Session, void 0, rt.getUserDoc());
};
2016-12-22 15:00:13 +00:00
overwrite(config, opt);
2017-05-04 14:16:09 +00:00
Session.realtime = Realtime.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');
}
var config = makeConfig(hash);
var Session = { cb: cb, };
config.onReady = function (info) {
var realtime = Session.session = info.realtime;
Session.network = info.network;
2016-12-22 15:00:13 +00:00
TextPatcher.create({
realtime: realtime,
})(doc);
var to = window.setTimeout(function () {
cb(new Error("Timeout"));
}, 5000);
Cryptpad.whenRealtimeSyncs(realtime, function () {
window.clearTimeout(to);
realtime.abort();
finish(Session, void 0);
});
};
2016-12-22 15:00:13 +00:00
overwrite(config, opt);
2017-05-04 14:16:09 +00:00
Session.session = Realtime.start(config);
};
2016-12-22 15:00:13 +00:00
return {
get: get,
put: put,
};
});