Merge branch 'staging' of github.com:xwiki-labs/cryptpad into staging

pull/1/head
yflory 7 years ago
commit de7806504b

@ -5,9 +5,11 @@ define([
'/drive/tests.js',
'/common/test.js',
'/common/common-hash.js',
'/common/common-util.js',
'/common/common-thumbnail.js',
'/common/wire.js',
'/common/flat-dom.js',
], function ($, Hyperjson, Sortify, Drive, Test, Hash, Thumb, Flat) {
], function ($, Hyperjson, Sortify, Drive, Test, Hash, Util, Thumb, Wire, Flat) {
window.Hyperjson = Hyperjson;
window.Sortify = Sortify;
@ -30,7 +32,7 @@ define([
ASSERTS.forEach(function (f, index) {
f(function (err) {
console.log("test " + index);
//console.log("test " + index);
done(err, index);
}, index);
});
@ -235,6 +237,54 @@ define([
return cb(true);
}, "version 2 hash failed to parse correctly");
assert(function (cb) {
Wire.create({
constructor: function (cb) {
var service = function (type, data, cb) {
switch (type) {
case "HEY_BUDDY":
return cb(void 0, "SALUT!");
default:
cb("ERROR");
}
};
var evt = Util.mkEvent();
var respond = function (e, out) {
evt.fire(e, out);
};
cb(void 0, {
send: function (raw /*, cb */) {
try {
var parsed = JSON.parse(raw);
var txid = parsed.txid;
var message = parsed.message;
setTimeout(function () {
service(message.command, message.content, function (e, result) {
respond(JSON.stringify({
txid: txid,
error: e,
content: result,
}));
});
});
} catch (e) { console.error("PEWPEW"); }
},
receive: function (f) {
evt.reg(f);
},
});
},
}, function (e, rpc) {
if (e) { return cb(false); }
rpc.send('HEY_BUDDY', null, function (e, out) {
if (e) { return void cb(false); }
if (out === 'SALUT!') { cb(true); }
});
});
}, "Test rpc factory");
/*
assert(function (cb) {
var getBlob = function (url, cb) {

@ -33,7 +33,7 @@ define([], function () {
data.map[id] = el.textContent;
return id;
}
if (!el || !el.attributes) { return void console.error(el); }
if (!el || !el.attributes) { return; }
id = uid();
data.map[id] = [
el.tagName,

@ -747,7 +747,7 @@ define([
var origin = privateData.origin;
var pathname = privateData.pathname;
var href = inDrive.test(pathname) ? origin+'/index.html' : origin+'/drive/';
var buttonTitle = inDrive ? Messages.header_homeTitle : Messages.header_logoTitle;
var buttonTitle = inDrive.test(pathname) ? Messages.header_homeTitle : Messages.header_logoTitle;
var $aTag = $('<a>', {
href: href,

@ -0,0 +1,94 @@
define([
], function () {
var Wire = {};
/* MISSION: write a generic RPC framework
Requirements
* some transmission methods can be interrupted
* handle disconnects and reconnects
* handle callbacks
* configurable timeout
* Service should expose 'addClient' method
* and handle broadcast
*
*/
var uid = function () {
return Number(Math.floor(Math.random () *
Number.MAX_SAFE_INTEGER)).toString(32);
};
/*
opt = {
send: function () {
},
receive: function () {
},
constructor: function (cb) {
cb(void 0 , {
send: function (content, cb) {
},
receive: function () {
}
});
},
};
*/
Wire.create = function (opt, cb) {
var ctx = {};
var pending = ctx.pending = {};
ctx.connected = false;
var rpc = {};
opt.constructor(function (e, service) {
if (e) { return setTimeout(function () { cb(e); }); }
rpc.send = function (type, data, cb) {
var txid = uid();
if (typeof(cb) !== 'function') {
throw new Error('expected callback');
}
ctx.pending[txid] = function (err, response) {
cb(err, response);
};
service.send(JSON.stringify({
txid: txid,
message: {
command: type,
content: data,
},
}));
};
service.receive(function (raw) {
try {
var data = JSON.parse(raw);
var txid = data.txid;
if (!txid) { throw new Error('NO_TXID'); }
var cb = pending[txid];
if (data.error) { return void cb(data.error); }
cb(void 0, data.content);
} catch (e) { console.error("UNHANDLED_MESSAGE", raw); }
});
cb(void 0, rpc);
});
};
return Wire;
});
Loading…
Cancel
Save