implement cryptput

pull/1/head
ansuz 8 years ago
parent 84d5e4b35d
commit 29d406b7a9

@ -1,59 +1,87 @@
define([ define([
'/customize/messages.js?app=read', '/customize/messages.js?app=cryptget',
'/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-crypto/crypto.js',
'/bower_components/chainpad-netflux/chainpad-netflux.js', '/bower_components/chainpad-netflux/chainpad-netflux.js',
'/common/cryptpad-common.js', '/common/cryptpad-common.js',
], function (Messages, Crypto, Realtime, Cryptpad) { '/bower_components/textpatcher/TextPatcher.js',
var Crypt = {}; '/bower_components/jquery/dist/jquery.min.js',
], function (Messages, Crypto, Realtime, Cryptpad, TextPatcher) {
var noop = function () {};
var finish = function (S, err, doc) {
if (S.done) { return; }
S.cb(err, doc);
S.done = true;
var finish = function (Session, err, doc) { var abort = Cryptpad.find(S, ['realtime', 'realtime', 'abort']);
if (Session.done) { return; } if (typeof(abort) === 'function') {
Session.cb(err, doc); S.realtime.realtime.sync();
Session.done = true; abort();
var abort = Cryptpad.find(Session,
['realtime', 'realtime', 'abort']);
if (typeof(abort) === 'function') { abort(); }
};
var get = Crypt.get = function (hash, cb, opt) {
if (typeof(cb) !== 'function') {
throw new Error('Crypt.get expects a callback');
} }
};
var Session = { var makeConfig = function (hash) {
cb: cb, var secret = Cryptpad.getSecrets(hash);
}; if (!secret.keys) { secret.keys = secret.key; } // support old hashses
opt = opt || {};
var secret = Session.secret = Cryptpad.getSecrets(hash);
var config = { var config = {
websocketURL: Cryptpad.getWebsocketURL(), websocketURL: Cryptpad.getWebsocketURL(),
channel: secret.channel, channel: secret.channel,
validateKey: secret.keys.validateKey || undefined,
crypto: Crypto.createEncryptor(secret.keys), crypto: Crypto.createEncryptor(secret.keys),
logLevel: 0, logLevel: 0,
}; };
return config;
};
var onError = config.onError = function () { var isObject = function (o) {
finish(Session, Messages.websocketError); return typeof(o) === 'object';
}; };
var onAbort = config.onAbort = function () {
finish(Session, Messages.disconnectAlert); 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);
var onReady = config.onReady = function (info) { var onReady = config.onReady = function (info) {
var realtime = Session.realtime = info.realtime; var rt = Session.session = info.realtime;
finish(Session, void 0, realtime.getUserDoc()); finish(Session, void 0, rt.getUserDoc());
}; };
overwrite(config, opt);
var realtime = Session.realtime = Realtime.start(config);
};
var onConnectionChange = config.onConnectionChange = function (info) { var put = function (hash, doc, cb, opt) {
if (info.state) { return; } if (typeof(cb) !== 'function') {
finish(Session, Messages.disconnectAlert); 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;
TextPatcher.create({
realtime: realtime,
})(doc);
realtime.sync();
realtime.abort();
finish(Session, void 0);
}; };
Object.keys(opt).forEach(function (k) { config[k] = opt[k]; }); overwrite(config, opt);
return (Session.realtime = Realtime.start(config)); var realtime = Session.session = Realtime.start(config);
}; };
return Crypt; return {
get: get,
put: put,
};
}); });

Loading…
Cancel
Save