-
Notifications
You must be signed in to change notification settings - Fork 3
/
sockethub-client.min.js
47 lines (44 loc) · 11 KB
/
sockethub-client.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* sockethub-client 0.0.1-dev
*
* © 2013 Niklas E. Cathor (https://github.com/nilclass)
* © 2013 Nick Jennings (https://github.com/silverbucket)
*
* sockethub-client is dual-licensed under either the MIT License or GPLv3 (at your choice).
* See the files LICENSE-MIT and LICENSE-GPL for details.
*
* The latest version of sockethub-client can be found here:
* git://github.com/sockethub/sockethub-client.git
*
* For more information about sockethub visit http://sockethub.org/.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
/**
* almond 0.1.4 Copyright (c) 2011, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/almond for details
*/
/**
* This file is part of sockethub-client.
*
* © 2013 Niklas E. Cathor (https://github.com/nilclass)
* © 2013 Nick Jennings (https://github.com/silverbucket)
*
* sockethub-client is dual-licensed under either the MIT License or GPLv3 (at your choice).
* See the files LICENSE-MIT and LICENSE-GPL for details.
*
* The latest version of sockethub-client can be found here:
* git://github.com/sockethub/sockethub-client.git
*
* For more information about sockethub visit http://sockethub.org/.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
(function(e,t){typeof define=="function"&&define.amd?define(t):e.SockethubClient=t()})(this,function(){var e,t,n;return function(r){function c(e,t){var n,r,i,s,o,u,f,l,c,h,p=t&&t.split("/"),d=a.map,v=d&&d["*"]||{};if(e&&e.charAt(0)==="."&&t){p=p.slice(0,p.length-1),e=p.concat(e.split("/"));for(l=0;l<e.length;l+=1){h=e[l];if(h===".")e.splice(l,1),l-=1;else if(h===".."){if(l===1&&(e[2]===".."||e[0]===".."))break;l>0&&(e.splice(l-1,2),l-=2)}}e=e.join("/")}if((p||v)&&d){n=e.split("/");for(l=n.length;l>0;l-=1){r=n.slice(0,l).join("/");if(p)for(c=p.length;c>0;c-=1){i=d[p.slice(0,c).join("/")];if(i){i=i[r];if(i){s=i,o=l;break}}}if(s)break;!u&&v&&v[r]&&(u=v[r],f=l)}!s&&u&&(s=u,o=f),s&&(n.splice(0,o,s),e=n.join("/"))}return e}function h(e,t){return function(){return s.apply(r,l.call(arguments,0).concat([e,t]))}}function p(e){return function(t){return c(t,e)}}function d(e){return function(t){o[e]=t}}function v(e){if(u.hasOwnProperty(e)){var t=u[e];delete u[e],f[e]=!0,i.apply(r,t)}if(!o.hasOwnProperty(e))throw new Error("No "+e);return o[e]}function m(e,t){var n,r,i=e.indexOf("!");return i!==-1?(n=c(e.slice(0,i),t),e=e.slice(i+1),r=v(n),r&&r.normalize?e=r.normalize(e,p(t)):e=c(e,t)):e=c(e,t),{f:n?n+"!"+e:e,n:e,p:r}}function g(e){return function(){return a&&a.config&&a.config[e]||{}}}var i,s,o={},u={},a={},f={},l=[].slice;i=function(e,t,n,i){var s,a,l,c,p,y=[],b;i=i||e;if(typeof n=="function"){t=!t.length&&n.length?["require","exports","module"]:t;for(p=0;p<t.length;p+=1){c=m(t[p],i),a=c.f;if(a==="require")y[p]=h(e);else if(a==="exports")y[p]=o[e]={},b=!0;else if(a==="module")s=y[p]={id:e,uri:"",exports:o[e],config:g(e)};else if(o.hasOwnProperty(a)||u.hasOwnProperty(a))y[p]=v(a);else if(c.p)c.p.load(c.n,h(i,!0),d(a),{}),y[p]=o[a];else if(!f[a])throw new Error(e+" missing "+a)}l=n.apply(o[e],y);if(e)if(s&&s.exports!==r&&s.exports!==o[e])o[e]=s.exports;else if(l!==r||!b)o[e]=l}else e&&(o[e]=n)},e=t=s=function(e,t,n,o,u){return typeof e=="string"?v(m(e,t).f):(e.splice||(a=e,t.splice?(e=t,t=n,n=null):e=r),t=t||function(){},typeof n=="function"&&(n=o,o=u),o?i(r,e,t,n):setTimeout(function(){i(r,e,t,n)},15),s)},s.config=function(e){return a=e,s},n=function(e,t,n){t.splice||(n=t,t=[]),u[e]=[e,t,n]},n.amd={jQuery:!0}}(),n("vendor/almond",function(){}),n("sockethub/extend",[],function(){function e(t){var n=Array.prototype.slice.call(arguments,1);return n.forEach(function(n){for(var r in n)typeof n[r]=="object"&&typeof t[r]=="object"?e(t[r],n[r]):t[r]=n[r]}),t}return e}),function(){function e(t){function o(e){if(i){var t;if(e.fulfilled)try{t=[e.fulfilled.apply(null,s)]}catch(n){e.promise.reject(n);return}else t=s;t[0]&&typeof t[0].then=="function"?t[0].then(e.promise.fulfill,e.promise.reject):e.promise.fulfill.apply(null,t)}else if(e.rejected){var r;try{r=e.rejected.apply(null,s)}catch(n){e.promise.reject(n);return}r&&typeof r.then=="function"?r.then(e.promise.fulfill,e.promise.reject):e.promise.fulfill(r)}else e.promise.reject.apply(null,s)}function u(e,t){if(s){console.log("WARNING: Can't resolve promise, already resolved!");return}i=e,s=Array.prototype.slice.call(t),setTimeout(function(){var e=r.length;e===0&&!i;for(var t=0;t<e;t++)o(r[t]);r=undefined},0)}var n;typeof t=="function"&&setTimeout(function(){try{t(n)}catch(e){n.reject(e)}},0);var r=[],i,s;return n={then:function(t,n){var i={fulfilled:typeof t=="function"?t:undefined,rejected:typeof n=="function"?n:undefined,promise:e()};return s?setTimeout(function(){o(i)},0):r.push(i),i.promise},fulfill:function(){return u(!0,arguments),this},reject:function(){return u(!1,arguments),this}},n}typeof module!="undefined"?module.exports=e:typeof n=="function"?n("vendor/promising",[],function(){return e}):typeof window!="undefined"&&(window.promising=e)}(),n("sockethub/event_handling",[],function(){var e={on:function(e,t){this._validateEvent(e),this._handlers[e].push(t)},_emit:function(e){this._validateEvent(e);var t=Array.prototype.slice.call(arguments,1);this._handlers[e].forEach(function(e){e.apply(this,t)})},_validateEvent:function(e){if(!(e in this._handlers))throw"Unknown event: "+e},_delegateEvent:function(e,t){t.on(e,function(t){this._emit(e,t)}.bind(this))},_addEvent:function(e){this._handlers[e]=[]}};return function(t){var n=Array.prototype.slice.call(arguments,1);for(var r in e)t[r]=e[r];t._handlers={},n.forEach(function(e){t._addEvent(e)})}}),n("sockethub/client",["./extend","../vendor/promising","./event_handling"],function(e,t,n){var r=function(e,t){this.jsonClient=e,this.options=t,this._ridPromises={},n(this,"connected","disconnected","failed","message","unexpected-response"),e.on("message",this._processIncoming.bind(this)),this._delegateEvent("connected",e),this._delegateEvent("disconnected",e),this._delegateEvent("failed",e),this.__defineGetter__("connected",function(){return this.jsonClient.connected})};return r.prototype={declareVerb:function(t,n,r,i){this[t]=function(){var i=Array.prototype.slice.call(arguments),s=e({},r,{verb:t});n.forEach(function(e,t){var n=i[t],r=this._getDeepAttr(s,e);if(typeof r=="undefined"&&typeof n=="undefined")throw new Error("Expected a value for parameter "+e+", but got undefined!");this._setDeepAttr(s,e,n)}.bind(this));var o=i[n.length];return typeof o=="object"&&e(s,o),this.sendObject(s)},i&&(this[t]=i(this[t].bind(this)))},declareEvent:function(e){this._addEvent(e)},disconnect:function(){this.jsonClient.disconnect()},_ridCounter:0,sendObject:function(n){var r=t(),i=++this._ridCounter;return this._ridPromises[i]=r,n=e(n,{rid:i}),console.log("SEND",n),this.jsonClient.send(n),r},_getDeepAttr:function(e,t,n){var r=n||t.split("."),i=e[r.shift()];return r.length?this._getDeepAttr(i,undefined,r):i},_setDeepAttr:function(e,t,n,r){var i=r||t.split(".");i.length>1?this._setDeepAttr(e[i.shift()],undefined,n,i):e[i[0]]=n},_processIncoming:function(e){console.log(e.verb==="confirm"?"CONFIRM":"RECEIVE",e);var t=e.rid;if(typeof t!="undefined"){var n=this._ridPromises[t];if(n){if(e.verb==="confirm"){if(e.status)return;n.reject(e)}else"status"in e?n[e.status?"fulfill":"reject"](e):n.fulfill(e);delete this._ridPromises[t]}else this._emit("unexpected-response",e)}else this._emit("message",e)}},r}),n("sockethub/json_client",["./event_handling"],function(e){var t=function(t){this.socket=t,e(this,"message","connected","disconnected","failed"),this._listen()};return t.prototype={send:function(e){this.socket.send(JSON.stringify(e))},disconnect:function(){this.socket.close()},_listen:function(){this.socket.onmessage=this._processMessageEvent.bind(this),this.connected=!1,this.socket.onopen=function(){this.connected=!0,this._emit("connected")}.bind(this),this.socket.onclose=function(){this.connected?(this._emit("disconnected"),this.connected=!1):this._emit("failed")}.bind(this)},_processMessageEvent:function(e){this._emit("message",JSON.parse(e.data))}},t}),n("sockethub/connect",["./extend","./json_client","./client"],function(e,t,n){var r=10550,i="/sockethub",s="sockethub",o=function(o,u){var a;typeof u!="object"&&(u={}),typeof o=="string"&&!o.match(/wss?\:\/\//)&&(o={host:o});if(typeof o=="string")a=o;else{if(typeof o!="object")throw"SockethubClient.connect expects a URI, specified via a String or Object.";e(u,o);if(!u.host)throw"Required 'host' option not present";u.port||(u.port=r),u.path||(u.path=i),a=(u.ssl?"wss":"ws")+"://"+u.host+":"+u.port+u.path}return new n(new t(new WebSocket(a,s)),u)};return o}),n("sockethub/remoteStorage",[],function(){function t(t){t.defineModule("sockethub",e),t.access.claim("sockethub","rw");var n=t.getBearerToken();if(typeof n=="string"&&n.length>0){this.options.register||(this.options.register={});var r={};t.access.scopes.forEach(function(e){r[e]=t.access.get(e)});var i=t.getStorageInfo();i.type=String(i.type),this.options.register.remoteStorage={storageInfo:i,bearerToken:n,scope:r}}}var e=function(e,t){return e.declareType("config",{description:"sockethub config file",type:"object",properties:{host:{type:"string",description:"the hostname to connect to",format:"uri",required:!0},port:{type:"number",description:"the port number to connect to",required:!0},secret:{type:"string",description:"the secret to identify yourself with the sockethub server",required:!0}}}),{exports:{getConfig:function(){return e.getObject("config.json")},writeConfig:function(t){return e.storeObject("config","config.json",t)}}}};return t}),n("verbs/core",[],function(){var e=function(e){e.declareVerb("ping",[],{platform:"dispatcher"},function(e){return function(t){return typeof t!="object"&&(t={}),typeof t.timestamp!="number"&&(t.timestamp=(new Date).getTime()),e(t).then(function(e){return e.offset=(new Date).getTime()-t.timestamp,e})}}),e.declareVerb("register",["object"],{platform:"dispatcher"},function(t){return function(){return e.registered&&(console.log("WARNING: already registered!"),console.trace()),t.apply(this,arguments).then(function(t){e.registered=t.status;if(!t.status)throw setTimeout(function(){e._emit("registration-failed",t)},0),"Registration failed: "+t.message;return setTimeout(function(){e._emit("registered")},0),t})}}),e.registered=!1,e.declareEvent("registered"),e.declareEvent("registration-failed"),e.on("connected",function(){console.log("automatic registration!",e.options),e.options.register&&(console.log("automatic registration!"),e.register(e.options.register))}),e.on("disconnected",function(){delete e.registered}),e.declareVerb("set",["target.platform","object"],{platform:"dispatcher",target:{}})};return e}),n("sockethub-client",["sockethub/client","sockethub/connect","sockethub/remoteStorage","verbs/core"],function(e,t,n,r){return e.connect=function(){var e=t.apply(this,arguments);return r(e),e},e.prototype.connectStorage=n,e}),t("sockethub-client")});