diff --git a/www/common/crypto.js b/www/common/crypto.js deleted file mode 100644 index dd6f826bd..000000000 --- a/www/common/crypto.js +++ /dev/null @@ -1,53 +0,0 @@ -define([ - '/bower_components/tweetnacl/nacl-fast.min.js', -], function () { - var Nacl = window.nacl; - var module = { exports: {} }; - - var encryptStr = function (str, key) { - var array = Nacl.util.decodeUTF8(str); - var nonce = Nacl.randomBytes(24); - var packed = Nacl.secretbox(array, nonce, key); - if (!packed) { throw new Error(); } - return Nacl.util.encodeBase64(nonce) + "|" + Nacl.util.encodeBase64(packed); - }; - - var decryptStr = function (str, key) { - var arr = str.split('|'); - if (arr.length !== 2) { throw new Error(); } - var nonce = Nacl.util.decodeBase64(arr[0]); - var packed = Nacl.util.decodeBase64(arr[1]); - var unpacked = Nacl.secretbox.open(packed, nonce, key); - if (!unpacked) { throw new Error(); } - return Nacl.util.encodeUTF8(unpacked); - }; - - var encrypt = module.exports.encrypt = function (msg, key) { - return encryptStr(msg, key); - }; - - var decrypt = module.exports.decrypt = function (msg, key) { - return decryptStr(msg, key); - }; - - var parseKey = module.exports.parseKey = function (str) { - var array = Nacl.util.decodeBase64(str); - var hash = Nacl.hash(array); - var lk = hash.subarray(32); - return { - lookupKey: lk, - cryptKey: hash.subarray(0,32), - channel: Nacl.util.encodeBase64(lk).substring(0,10) - }; - }; - - var rand64 = module.exports.rand64 = function (bytes) { - return Nacl.util.encodeBase64(Nacl.randomBytes(bytes)); - }; - - var genKey = module.exports.genKey = function () { - return rand64(18); - }; - - return module.exports; -}); diff --git a/www/common/es6-promise.min.js b/www/common/es6-promise.min.js deleted file mode 100644 index f26f3c8ce..000000000 --- a/www/common/es6-promise.min.js +++ /dev/null @@ -1,9 +0,0 @@ -/*! - * @overview es6-promise - a tiny implementation of Promises/A+. - * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald) - * @license Licensed under MIT license - * See https://raw.githubusercontent.com/jakearchibald/es6-promise/master/LICENSE - * @version 3.2.1 - */ - -(function(){"use strict";function t(t){return"function"==typeof t||"object"==typeof t&&null!==t}function e(t){return"function"==typeof t}function n(t){G=t}function r(t){Q=t}function o(){return function(){process.nextTick(a)}}function i(){return function(){B(a)}}function s(){var t=0,e=new X(a),n=document.createTextNode("");return e.observe(n,{characterData:!0}),function(){n.data=t=++t%2}}function u(){var t=new MessageChannel;return t.port1.onmessage=a,function(){t.port2.postMessage(0)}}function c(){return function(){setTimeout(a,1)}}function a(){for(var t=0;J>t;t+=2){var e=tt[t],n=tt[t+1];e(n),tt[t]=void 0,tt[t+1]=void 0}J=0}function f(){try{var t=require,e=t("vertx");return B=e.runOnLoop||e.runOnContext,i()}catch(n){return c()}}function l(t,e){var n=this,r=new this.constructor(p);void 0===r[rt]&&k(r);var o=n._state;if(o){var i=arguments[o-1];Q(function(){x(o,r,i,n._result)})}else E(n,r,t,e);return r}function h(t){var e=this;if(t&&"object"==typeof t&&t.constructor===e)return t;var n=new e(p);return g(n,t),n}function p(){}function _(){return new TypeError("You cannot resolve a promise with itself")}function d(){return new TypeError("A promises callback cannot return that same promise.")}function v(t){try{return t.then}catch(e){return ut.error=e,ut}}function y(t,e,n,r){try{t.call(e,n,r)}catch(o){return o}}function m(t,e,n){Q(function(t){var r=!1,o=y(n,e,function(n){r||(r=!0,e!==n?g(t,n):S(t,n))},function(e){r||(r=!0,j(t,e))},"Settle: "+(t._label||" unknown promise"));!r&&o&&(r=!0,j(t,o))},t)}function b(t,e){e._state===it?S(t,e._result):e._state===st?j(t,e._result):E(e,void 0,function(e){g(t,e)},function(e){j(t,e)})}function w(t,n,r){n.constructor===t.constructor&&r===et&&constructor.resolve===nt?b(t,n):r===ut?j(t,ut.error):void 0===r?S(t,n):e(r)?m(t,n,r):S(t,n)}function g(e,n){e===n?j(e,_()):t(n)?w(e,n,v(n)):S(e,n)}function A(t){t._onerror&&t._onerror(t._result),T(t)}function S(t,e){t._state===ot&&(t._result=e,t._state=it,0!==t._subscribers.length&&Q(T,t))}function j(t,e){t._state===ot&&(t._state=st,t._result=e,Q(A,t))}function E(t,e,n,r){var o=t._subscribers,i=o.length;t._onerror=null,o[i]=e,o[i+it]=n,o[i+st]=r,0===i&&t._state&&Q(T,t)}function T(t){var e=t._subscribers,n=t._state;if(0!==e.length){for(var r,o,i=t._result,s=0;si;i++)e.resolve(t[i]).then(n,r)}:function(t,e){e(new TypeError("You must pass an array to race."))})}function F(t){var e=this,n=new e(p);return j(n,t),n}function D(){throw new TypeError("You must pass a resolver function as the first argument to the promise constructor")}function K(){throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.")}function L(t){this[rt]=O(),this._result=this._state=void 0,this._subscribers=[],p!==t&&("function"!=typeof t&&D(),this instanceof L?C(this,t):K())}function N(t,e){this._instanceConstructor=t,this.promise=new t(p),this.promise[rt]||k(this.promise),Array.isArray(e)?(this._input=e,this.length=e.length,this._remaining=e.length,this._result=new Array(this.length),0===this.length?S(this.promise,this._result):(this.length=this.length||0,this._enumerate(),0===this._remaining&&S(this.promise,this._result))):j(this.promise,U())}function U(){return new Error("Array Methods must be provided an Array")}function W(){var t;if("undefined"!=typeof global)t=global;else if("undefined"!=typeof self)t=self;else try{t=Function("return this")()}catch(e){throw new Error("polyfill failed because global object is unavailable in this environment")}var n=t.Promise;(!n||"[object Promise]"!==Object.prototype.toString.call(n.resolve())||n.cast)&&(t.Promise=pt)}var z;z=Array.isArray?Array.isArray:function(t){return"[object Array]"===Object.prototype.toString.call(t)};var B,G,H,I=z,J=0,Q=function(t,e){tt[J]=t,tt[J+1]=e,J+=2,2===J&&(G?G(a):H())},R="undefined"!=typeof window?window:void 0,V=R||{},X=V.MutationObserver||V.WebKitMutationObserver,Z="undefined"==typeof self&&"undefined"!=typeof process&&"[object process]"==={}.toString.call(process),$="undefined"!=typeof Uint8ClampedArray&&"undefined"!=typeof importScripts&&"undefined"!=typeof MessageChannel,tt=new Array(1e3);H=Z?o():X?s():$?u():void 0===R&&"function"==typeof require?f():c();var et=l,nt=h,rt=Math.random().toString(36).substring(16),ot=void 0,it=1,st=2,ut=new M,ct=new M,at=0,ft=Y,lt=q,ht=F,pt=L;L.all=ft,L.race=lt,L.resolve=nt,L.reject=ht,L._setScheduler=n,L._setAsap=r,L._asap=Q,L.prototype={constructor:L,then:et,"catch":function(t){return this.then(null,t)}};var _t=N;N.prototype._enumerate=function(){for(var t=this.length,e=this._input,n=0;this._state===ot&&t>n;n++)this._eachEntry(e[n],n)},N.prototype._eachEntry=function(t,e){var n=this._instanceConstructor,r=n.resolve;if(r===nt){var o=v(t);if(o===et&&t._state!==ot)this._settledAt(t._state,e,t._result);else if("function"!=typeof o)this._remaining--,this._result[e]=t;else if(n===pt){var i=new n(p);w(i,t,o),this._willSettleAt(i,e)}else this._willSettleAt(new n(function(e){e(t)}),e)}else this._willSettleAt(r(t),e)},N.prototype._settledAt=function(t,e,n){var r=this.promise;r._state===ot&&(this._remaining--,t===st?j(r,n):this._result[e]=n),0===this._remaining&&S(r,this._result)},N.prototype._willSettleAt=function(t,e){var n=this;E(t,void 0,function(t){n._settledAt(it,e,t)},function(t){n._settledAt(st,e,t)})};var dt=W,vt={Promise:pt,polyfill:dt};"function"==typeof define&&define.amd?define(function(){return vt}):"undefined"!=typeof module&&module.exports?module.exports=vt:"undefined"!=typeof this&&(this.ES6Promise=vt),dt()}).call(this); \ No newline at end of file diff --git a/www/common/hyperjson.js b/www/common/hyperjson.js deleted file mode 100644 index b9dc87b9b..000000000 --- a/www/common/hyperjson.js +++ /dev/null @@ -1,98 +0,0 @@ -define([], function () { - // this makes recursing a lot simpler - var isArray = function (A) { - return Object.prototype.toString.call(A)==='[object Array]'; - }; - - var callOnHyperJSON = function (hj, cb) { - var children; - - if (hj && hj[2]) { - children = hj[2].map(function (child) { - if (isArray(child)) { - // if the child is an array, recurse - return callOnHyperJSON(child, cb); - } else if (typeof (child) === 'string') { - return child; - } else { - // the above branches should cover all methods - // if we hit this, there is a problem - throw new Error(); - } - }); - } else { - children = []; - } - // this should return the top level element of your new DOM - return cb(hj[0], hj[1], children); - }; - - var isTruthy = function (x) { - return x; - }; - - var DOM2HyperJSON = function(el, predicate, filter){ - if(!el.tagName && el.nodeType === Node.TEXT_NODE){ - return el.textContent; - } - if(!el.attributes){ - return; - } - if (predicate) { - if (!predicate(el)) { - // shortcircuit - return; - } - } - - var attributes = {}; - - var i = 0; - for(;i < el.attributes.length; i++){ - var attr = el.attributes[i]; - if(attr.name && attr.value){ - attributes[attr.name] = attr.value; - } - } - - // this should never be longer than three elements - var result = []; - - // get the element type, id, and classes of the element - // and push them to the result array - var sel = el.tagName; - - if(attributes.id){ - // we don't have to do much to validate IDs because the browser - // will only permit one id to exist - // unless we come across a strange browser in the wild - sel = sel +'#'+ attributes.id; - delete attributes.id; - } - result.push(sel); - - // second element of the array is the element attributes - result.push(attributes); - - // third element of the array is an array of child nodes - var children = []; - - // js hint complains if we use 'var' here - i = 0; - for(; i < el.childNodes.length; i++){ - children.push(DOM2HyperJSON(el.childNodes[i], predicate, filter)); - } - result.push(children.filter(isTruthy)); - - if (filter) { - return filter(result); - } else { - return result; - } - }; - - return { - fromDOM: DOM2HyperJSON, - callOn: callOnHyperJSON - }; -}); diff --git a/www/common/netflux-client.js b/www/common/netflux-client.js deleted file mode 100644 index b9165bf37..000000000 --- a/www/common/netflux-client.js +++ /dev/null @@ -1,291 +0,0 @@ -/*global: WebSocket */ -define(function () { - 'use strict'; - - var MAX_LAG_BEFORE_PING = 15000; - var MAX_LAG_BEFORE_DISCONNECT = 30000; - var PING_CYCLE = 5000; - var REQUEST_TIMEOUT = 30000; - - var now = function now() { - return new Date().getTime(); - }; - - var networkSendTo = function networkSendTo(ctx, peerId, content) { - var seq = ctx.seq++; - ctx.ws.send(JSON.stringify([seq, 'MSG', peerId, content])); - return new Promise(function (res, rej) { - ctx.requests[seq] = { reject: rej, resolve: res, time: now() }; - }); - }; - - var channelBcast = function channelBcast(ctx, chanId, content) { - var chan = ctx.channels[chanId]; - if (!chan) { - throw new Error("no such channel " + chanId); - } - var seq = ctx.seq++; - ctx.ws.send(JSON.stringify([seq, 'MSG', chanId, content])); - return new Promise(function (res, rej) { - ctx.requests[seq] = { reject: rej, resolve: res, time: now() }; - }); - }; - - var channelLeave = function channelLeave(ctx, chanId, reason) { - var chan = ctx.channels[chanId]; - if (!chan) { - throw new Error("no such channel " + chanId); - } - delete ctx.channels[chanId]; - ctx.ws.send(JSON.stringify([ctx.seq++, 'LEAVE', chanId, reason])); - }; - - var makeEventHandlers = function makeEventHandlers(ctx, mappings) { - return function (name, handler) { - var handlers = mappings[name]; - if (!handlers) { - throw new Error("no such event " + name); - } - handlers.push(handler); - }; - }; - - var mkChannel = function mkChannel(ctx, id) { - var internal = { - onMessage: [], - onJoin: [], - onLeave: [], - members: [], - jSeq: ctx.seq++ - }; - var chan = { - _: internal, - time: now(), - id: id, - members: internal.members, - bcast: function bcast(msg) { - return channelBcast(ctx, chan.id, msg); - }, - leave: function leave(reason) { - return channelLeave(ctx, chan.id, reason); - }, - on: makeEventHandlers(ctx, { message: internal.onMessage, join: internal.onJoin, leave: internal.onLeave }) - }; - ctx.requests[internal.jSeq] = chan; - ctx.ws.send(JSON.stringify([internal.jSeq, 'JOIN', id])); - - return new Promise(function (res, rej) { - chan._.resolve = res; - chan._.reject = rej; - }); - }; - - var mkNetwork = function mkNetwork(ctx) { - var network = { - webChannels: ctx.channels, - getLag: function getLag() { - return ctx.lag; - }, - sendto: function sendto(peerId, content) { - return networkSendTo(ctx, peerId, content); - }, - join: function join(chanId) { - return mkChannel(ctx, chanId); - }, - on: makeEventHandlers(ctx, { message: ctx.onMessage, disconnect: ctx.onDisconnect }) - }; - network.__defineGetter__("webChannels", function () { - return Object.keys(ctx.channels).map(function (k) { - return ctx.channels[k]; - }); - }); - return network; - }; - - var onMessage = function onMessage(ctx, evt) { - var msg = void 0; - try { - msg = JSON.parse(evt.data); - } catch (e) { - console.log(e.stack);return; - } - if (msg[0] !== 0) { - var req = ctx.requests[msg[0]]; - if (!req) { - console.log("error: " + JSON.stringify(msg)); - return; - } - delete ctx.requests[msg[0]]; - if (msg[1] === 'ACK') { - if (req.ping) { - // ACK of a PING - ctx.lag = now() - Number(req.ping); - return; - } - req.resolve(); - } else if (msg[1] === 'JACK') { - if (req._) { - // Channel join request... - if (!msg[2]) { - throw new Error("wrong type of ACK for channel join"); - } - req.id = msg[2]; - ctx.channels[req.id] = req; - return; - } - req.resolve(); - } else if (msg[1] === 'ERROR') { - req.reject({ type: msg[2], message: msg[3] }); - } else { - req.reject({ type: 'UNKNOWN', message: JSON.stringify(msg) }); - } - return; - } - - if (msg[2] === 'IDENT') { - ctx.uid = msg[3]; - - setInterval(function () { - if (now() - ctx.timeOfLastMessage < MAX_LAG_BEFORE_PING) { - return; - } - var seq = ctx.seq++; - var currentDate = now(); - ctx.requests[seq] = { time: now(), ping: currentDate }; - ctx.ws.send(JSON.stringify([seq, 'PING', currentDate])); - if (now() - ctx.timeOfLastMessage > MAX_LAG_BEFORE_DISCONNECT) { - ctx.ws.close(); - } - }, PING_CYCLE); - - return; - } else if (!ctx.uid) { - // extranious message, waiting for an ident. - return; - } - if (msg[2] === 'PING') { - msg[2] = 'PONG'; - ctx.ws.send(JSON.stringify(msg)); - return; - } - - if (msg[2] === 'MSG') { - var handlers = void 0; - if (msg[3] === ctx.uid) { - handlers = ctx.onMessage; - } else { - var chan = ctx.channels[msg[3]]; - if (!chan) { - console.log("message to non-existant chan " + JSON.stringify(msg)); - return; - } - handlers = chan._.onMessage; - } - handlers.forEach(function (h) { - try { - h(msg[4], msg[1]); - } catch (e) { - console.error(e); - } - }); - } - - if (msg[2] === 'LEAVE') { - var _chan = ctx.channels[msg[3]]; - if (!_chan) { - console.log("leaving non-existant chan " + JSON.stringify(msg)); - return; - } - _chan._.onLeave.forEach(function (h) { - try { - h(msg[1], msg[4]); - } catch (e) { - console.log(e.stack); - } - }); - } - - if (msg[2] === 'JOIN') { - var _chan2 = ctx.channels[msg[3]]; - if (!_chan2) { - console.log("ERROR: join to non-existant chan " + JSON.stringify(msg)); - return; - } - // have we yet fully joined the chan? - var synced = _chan2._.members.indexOf(ctx.uid) !== -1; - _chan2._.members.push(msg[1]); - if (!synced && msg[1] === ctx.uid) { - // sync the channel join event - _chan2.myID = ctx.uid; - _chan2._.resolve(_chan2); - } - if (synced) { - _chan2._.onJoin.forEach(function (h) { - try { - h(msg[1]); - } catch (e) { - console.log(e.stack); - } - }); - } - } - }; - - var connect = function connect(websocketURL) { - var ctx = { - ws: new WebSocket(websocketURL), - seq: 1, - lag: 0, - uid: null, - network: null, - channels: {}, - onMessage: [], - onDisconnect: [], - requests: {} - }; - setInterval(function () { - for (var id in ctx.requests) { - var req = ctx.requests[id]; - if (now() - req.time > REQUEST_TIMEOUT) { - delete ctx.requests[id]; - if (typeof req.reject === "function") { - req.reject({ type: 'TIMEOUT', message: 'waited ' + (now() - req.time) + 'ms' }); - } - } - } - }, 5000); - ctx.network = mkNetwork(ctx); - ctx.ws.onmessage = function (msg) { - return onMessage(ctx, msg); - }; - ctx.ws.onclose = function (evt) { - ctx.onDisconnect.forEach(function (h) { - try { - h(evt.reason); - } catch (e) { - console.log(e.stack); - } - }); - }; - return new Promise(function (resolve, reject) { - ctx.ws.onopen = function () { - var count = 0; - var interval = 100; - var checkIdent = function() { - if(ctx.uid !== null) { - return resolve(ctx.network); - } - else { - if(count * interval > REQUEST_TIMEOUT) { - return reject({ type: 'TIMEOUT', message: 'waited ' + (count * interval) + 'ms' }); - } - setTimeout(checkIdent, 100); - } - } - checkIdent(); - }; - }); - }; - - return { connect: connect }; -});