UNPKG

evrythng-hub

Version:

Evrythng.js Hub plugin for transparent local Thng-Hub requests

2 lines 7.2 kB
!function(a,b){"use strict";if("function"==typeof define&&define.amd)define("optional",[],{load:function(a,b,c){var d=function(a){c(a)},e=function(a){var c=a.requireModules&&a.requireModules[0];requirejs.undef(c),define(c,[],function(){}),b([c],d)};b([a],d,e)}}),define(["optional!evrythng-ws","optional!jsrsasign","optional!node-jose-browserified"],function(a,c,d){return b(void 0,a,c,d)});else if("object"==typeof exports){var c,d,e,f;f=function(a){var b;try{b=require(a)}catch(a){}finally{return b}},c=f("evrythng-mqtt"),d=f("jsrsasign"),e=f("node-jose"),module.exports=b(c,void 0,d,e)}else a.EVT.Hub=a.Evrythng.Hub=b(void 0,a.EVT.WS,a.KJUR,a.nodeJose)}(this,function(a,b,c,d){"use strict";function e(a){return"[object Object]"===Object.prototype.toString.call(a)}function f(a,b,c){var d=a+"://"+b+":"+c;return"http"!==a&&(d+="/mqtt"),d}function g(){return d.util.base64url.encode(d.util.randomBytes(32))}function h(a){return d.JWK.asKey(a)}function i(a,b,c){return e(a)&&(a=JSON.stringify(a)),d.JWE.createEncrypt(c||{},b).update(a).final()}function j(a,b){return d.JWE.createDecrypt(b).decrypt(a).then(function(a){return a.plaintext.toString()})}function k(a,b){var d={alg:"HS256",typ:"JWT"},e={iss:a.iss,aud:a.aud,sub:a.sub,jti:a.jti||g(),data:a.data};return c.jws.JWS.sign("HS256",JSON.stringify(d),JSON.stringify(e),b)}function l(a,b,d){return d=d||{},d.alg=["HS256"],c.jws.JWS.verifyJWT(a,b,d)}function m(a){return a=a.split("."),{header:JSON.parse(q(a[0])),payload:JSON.parse(q(a[1]))}}function n(a,b){return i(a,b,{format:"compact"})}function o(a,b,c){var d=k({iss:c.iss,aud:c.aud,sub:c.sub,data:a},b.kid);return i(d,b)}function p(a,b,c){return j(a,b).then(function(a){var d=l(a,b.kid,{iss:[c.iss],aud:[c.aud],sub:[c.sub]});if(d)return m(a).payload.data;throw new Error("Unable to verify jsonwebtoken.")})}function q(a){return d.util.base64url.decode(a,"utf8")}function r(a,b){b=b||"get";for(var c=/^(.*?)(\?|$)/.exec(a)[1],d=0;d<u.length;d++)if(u[d].path.test(c)&&u[d].method.indexOf(b)!==-1)return!0;return!1}var s="2.0.0",t={timeout:1e3,remote:!1,targetHub:null},u=[{path:/^\/thngs$/,method:["get"]},{path:/^\/thngs\/[^\/]+$/,method:["get"]},{path:/^\/thngs\/[^\/]+\/properties$/,method:["post","get","put"]},{path:/^\/thngs\/[^\/]+\/properties\/[^\/]+$/,method:["get","put"]},{path:/^\/thngs\/[^\/]+\/actions\/[^\/]+$/,method:["post","get"]},{path:/^\/thngs\/[^\/]+\/actions\/[^\/]+\/[^\/]+$/,method:["get"]},{path:/^\/collections$/,method:["get"]},{path:/^\/collections\/[^\/]+$/,method:["get"]},{path:/^\/collections\/[^\/]+\/thngs$/,method:["get"]},{path:/^\/collections\/[^\/]+\/actions\/[^\/]+$/,method:["post","get"]},{path:/^\/collections\/[^\/]+\/actions\/[^\/]+\/[^\/]+$/,method:["get"]},{path:/^\/actions$/,method:["get"]}],v=a||b,w={},x={version:s,settings:t,setup:function(a){if(!e(a))throw new TypeError("Setup should be called with an options object.");for(var b in a)a.hasOwnProperty(b)&&(this.settings[b]=a[b]);return a.targetHub&&a.targetHub.customFields.key&&(this.settings.targetHub.customFields.secretKey=h(a.targetHub.customFields.key)),this.settings},install:function(b,c,d,e,g){function h(a){var c=arguments,d=(a.interceptors||[]).concat(b.settings.interceptors||[]),e=[{request:k},{request:l("http")},{request:m},{request:function(a){a.remote||(a.timeout=A.settings.timeout)}}];return a.interceptors=d.concat(e),B.api.apply(b,c).catch(function(e){if(!(v&&e&&e.cancelled)){if(e&&0===e.status)return g.info("Local hub is unavailable. Switching to remote..."),delete a.timeout,a.interceptors=d,B.api.apply(b,c);throw e}})}function i(b){return function(){var c=Array.prototype.slice.call(arguments),d=this,f=v===a?"mqtt":"ws",h={apiUrl:v.settings.apiUrl,url:d.path,authorization:d.scope.apiKey,targetHub:A.settings.targetHub},i={},j=c[0],n=[{request:k},{request:l(f)},{request:m},{request:function(a,b){e.extend(i,a,!0),b()}}];return B.api({authorization:d.scope.apiKey,url:d.path,interceptors:n}).catch(function(){if(!i.remote){var a={authorization:i.headers.authorization,apiUrl:i.apiUrl};e.isFunction(c[1])?c.splice(1,0,a):c[1]=e.extend(c[1],a),"subscribe"===b&&A.settings.targetHub.customFields.security.response&&(c[0]=z(c[0],h))}return B[b].apply(d,c).catch(function(){return g.info("Local hub is unavailable. Switching to remote..."),c[0]=j,c[1]={},B[b].apply(d,c)})})}}function j(){return Promise.resolve().then(s(this.apiKey)).then(t(this.apiKey))}function k(a){return a.remote=q(a)||!r(a.url,a.method),a}function l(a){return function(b){if(b.remote)return b;if(!A.settings.targetHub)throw new Error('There is no "targetHub" property in the settings.');return b.apiUrl=f(a,A.settings.targetHub.customFields.ip.v4,A.settings.targetHub.customFields.ports[a]),b}}function m(a){if(a.remote||!A.settings.targetHub.customFields.security.request&&!A.settings.targetHub.customFields.security.response)return a;if(!A.settings.targetHub.customFields.secretKey)throw new Error("THNGHUB=["+A.settings.targetHub.id+"] requires encryption, but there is no encryption key.");var b={authorization:a.authorization,url:a.url,targetHub:A.settings.targetHub};return Promise.all([u(a.authorization,a),x(a.data,a)]).then(function(c){var d=c[0],e=c[1];return a.headers.authorization=d,e&&(a.data=e),A.settings.targetHub.customFields.security.response&&(a.interceptors=[{response:function(a){return y(a,b)}}]),a})}function q(a){return void 0!==a.remote?a.remote:A.settings.remote}function s(a){return function(){return B.api({url:"/collections",params:{filter:"tags=HubDistribution"},authorization:a}).then(function(a){if(!a.length)throw new Error("There is no distributed collection in this project.");return a[0].id})}}function t(a){return function(b){return B.api({url:"/collections/"+b+"/thngs",authorization:a}).then(function(a){if(!a.length)throw new Error("There are no THNGHUBS in the distributed collection.");return a.filter(function(a){return a.properties["~connected"]})})}}function u(a,b){return w[a]&&w[a][A.settings.targetHub.id]?Promise.resolve(w[a][A.settings.targetHub.id]):A.settings.targetHub.customFields.secretKey.then(function(b){return n(a,b)}).then(function(c){return w[a]=w[b.authorization]||{},w[a][A.settings.targetHub.id]=c,c})}function x(a,b){if(a)return A.settings.targetHub.customFields.secretKey.then(function(c){return o(a,c,{iss:b.authorization,aud:A.settings.targetHub.id,sub:b.url})})}function y(a,b){if(a)return A.settings.targetHub.customFields.secretKey.then(function(c){return p(a,c,{aud:b.authorization,iss:b.targetHub.id,sub:b.url})})}function z(a,b){return function(c){var d=c.resource;return A.settings.targetHub.customFields.secretKey.then(function(a){return p(c,a,{aud:b.authorization,iss:b.targetHub.id,sub:b.url})}).then(function(b){return a(d.parse(b))})}}var A=this,B={api:b.api,subscribe:d.prototype.subscribe,publish:d.prototype.publish};b.api=h,c.prototype.getAvailableHubs=j,v&&(d.prototype.subscribe=i("subscribe"),d.prototype.publish=i("publish"))}};return c&&d&&(x.utils={generateId:g,initSecretKey:h,encrypt:i,decrypt:j,createJWT:k,verifyJWT:l,decodeJWT:m,encryptAuthorization:n,encryptPayload:o,decryptPayload:p}),x.$inject=["core","scope/scope","resource","utils","logger"],x}); //# sourceMappingURL=evrythng-hub.min.js.map