masq-client
Advanced tools
Comparing version 1.0.0 to 1.1.0
132
dist/masq.js
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.MasqClient = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
;(function (root) { | ||
/** | ||
* Forked from https://github.com/zendesk/cross-storage | ||
* Forked from https://gitstore.com/zendesk/cross-storage | ||
* | ||
* Constructs a new cross storage client given the url to a hub. By default, | ||
* Constructs a new cross storage client given the url to a store. By default, | ||
* an iframe is created within the document body that points to the url. It | ||
@@ -16,6 +16,6 @@ * also accepts an options object, which may include a timeout, frameId, and | ||
* @example | ||
* var storage = new MasqClient('https://store.example.com/hub.html'); | ||
* var storage = new MasqClient('https://store.example.com/store.html'); | ||
* | ||
* @example | ||
* var storage = new MasqClient('https://store.example.com/hub.html', { | ||
* var storage = new MasqClient('https://store.example.com/store.html', { | ||
* timeout: 5000, | ||
@@ -27,10 +27,11 @@ * frameId: 'storageFrame' | ||
* | ||
* @param {string} url The url to a cross storage hub | ||
* @param {string} url The url to a cross storage store | ||
* @param {object} [opts] An optional object containing additional options, | ||
* including timeout, frameId, and promise | ||
* | ||
* @property {string} _endpoint Default endpoint URL for the store | ||
* @property {string} _id A UUID v4 id | ||
* @property {function} _promise The Promise object to use | ||
* @property {string} _frameId The id of the iFrame pointing to the hub url | ||
* @property {string} _origin The hub's origin | ||
* @property {string} _frameId The id of the iFrame pointing to the store url | ||
* @property {string} _origin The store's origin | ||
* @property {object} _requests Mapping of request ids to callbacks | ||
@@ -41,6 +42,9 @@ * @property {bool} _connected Whether or not it has connected | ||
* @property {function} _listener The listener added to the window | ||
* @property {Window} _hub The hub window | ||
* @property {Window} _store The store window | ||
* @property {Window} _regwindow The app registration window | ||
*/ | ||
function MasqClient (url, opts) { | ||
url = url || 'https://qwantresearch.github.io/masq-store/' | ||
this._endpoint = 'https://qwantresearch.gitstore.io/masq-store/' | ||
url = url || this._endpoint | ||
opts = opts || {} | ||
@@ -58,2 +62,3 @@ | ||
this._listener = null | ||
this._regwindow = null | ||
@@ -67,3 +72,3 @@ this._installListener() | ||
// If using a passed iframe, poll the hub for a ready message | ||
// If using a passed iframe, poll the store for a ready message | ||
if (frame) { | ||
@@ -75,3 +80,3 @@ this._poll() | ||
frame = frame || this._createFrame(url) | ||
this._hub = frame.contentWindow | ||
this._store = frame.contentWindow | ||
} | ||
@@ -100,3 +105,3 @@ | ||
* | ||
* @param {string} url The url to a cross storage hub | ||
* @param {string} url The url to a cross storage store | ||
* @returns {string} The origin of the url | ||
@@ -142,3 +147,3 @@ */ | ||
* Returns a promise that is fulfilled when a connection has been established | ||
* with the cross storage hub. Its use is required to avoid sending any | ||
* with the cross storage store. Its use is required to avoid sending any | ||
* requests prior to initialization being complete. | ||
@@ -177,2 +182,59 @@ * | ||
/** | ||
* Registers an app with the store. | ||
* | ||
* @param {object} params Parameters that describe the app | ||
* @returns {Promise} A promise that is settled on app registration status | ||
*/ | ||
MasqClient.prototype.registerApp = function (params) { | ||
return new Promise(function (resolve, reject) { | ||
if (this._regwindow === undefined || this._regwindow.closed) { | ||
var w = 400 | ||
var h = 600 | ||
params.endpoint = params.endpoint || this._endpoint | ||
if (!params.url) { | ||
reject(new Error('No app URL provided to registerApp()')) | ||
} | ||
var url = params.endpoint + '?add=1&appUrl=' + encodeURIComponent(params.url) | ||
if (params.title) { | ||
url += '&title=' + encodeURIComponent(params.title) | ||
} | ||
if (params.desc) { | ||
url += '&desc=' + encodeURIComponent(params.desc) | ||
} | ||
if (params.icon) { | ||
url += '&icon=' + encodeURIComponent(params.icon) | ||
} | ||
var dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : window.screen.left | ||
var dualScreenTop = window.screenTop !== undefined ? window.screenTop : window.screen.top | ||
var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : window.screen.width | ||
var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : window.screen.height | ||
var left = ((width / 2) - (w / 2)) + dualScreenLeft | ||
var top = ((height / 2) - (h / 2)) + dualScreenTop | ||
this._regwindow = window.open(url, '', 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left) | ||
// Puts focus on the newWindow | ||
if (window.focus) { | ||
this._regwindow.focus() | ||
} | ||
// wrap onunload in a load event to avoid it being triggered too early | ||
window.addEventListener('message', function (e) { | ||
console.log('closing child', e) | ||
if (e.data === 'REGISTRATIONFINISHED') { | ||
resolve(e) | ||
} | ||
}, false) | ||
// this._regwindow.addEventListener('unload', function (e) { | ||
// e.preventDefault() | ||
// resolve(e) | ||
// }, false) | ||
} | ||
}) | ||
} | ||
/** | ||
* Sets a key to the specified value. Returns a promise that is fulfilled on | ||
@@ -184,3 +246,3 @@ * success, or rejected if any errors setting the key occurred, or the request | ||
* @param {*} value The value to assign | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -196,3 +258,3 @@ MasqClient.prototype.set = function (key, value) { | ||
* Accepts one or more keys for which to retrieve their values. Returns a | ||
* promise that is settled on hub response or timeout. On success, it is | ||
* promise that is settled on store response or timeout. On success, it is | ||
* fulfilled with the value of the key if only passed a single argument. | ||
@@ -203,3 +265,3 @@ * Otherwise it's resolved with an array of values. On failure, it is rejected | ||
* @param {...string} key The key to retrieve | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -214,6 +276,6 @@ MasqClient.prototype.get = function (key) { | ||
* Accepts one or more keys for deletion. Returns a promise that is settled on | ||
* hub response or timeout. | ||
* store response or timeout. | ||
* | ||
* @param {...string} key The key to delete | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -232,3 +294,3 @@ MasqClient.prototype.del = function () { | ||
* | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -245,3 +307,3 @@ MasqClient.prototype.clear = function () { | ||
* | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -259,3 +321,3 @@ MasqClient.prototype.getAll = function () { | ||
* @param {object} data The data object to set | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -272,3 +334,3 @@ MasqClient.prototype.setAll = function (data) { | ||
* | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -327,3 +389,3 @@ MasqClient.prototype.user = function () { | ||
// LocalStorage isn't available in the hub | ||
// LocalStorage isn't available in the store | ||
if (message.data['cross-storage'] === 'unavailable') { | ||
@@ -333,3 +395,3 @@ if (!client._closed) client.close() | ||
error = new Error('Closing client. Could not access localStorage in hub.') | ||
error = new Error('Closing client. Could not access localStorage in store.') | ||
for (i = 0; i < client._requests.connect.length; i++) { | ||
@@ -389,3 +451,3 @@ client._requests.connect[i](error) | ||
* Invoked when a frame id was passed to the client, rather than allowing | ||
* the client to create its own iframe. Polls the hub for a ready event to | ||
* the client to create its own iframe. Polls the store for a ready event to | ||
* establish a connected state. | ||
@@ -403,5 +465,5 @@ */ | ||
if (client._connected) return clearInterval(interval) | ||
if (!client._hub) return | ||
if (!client._store) return | ||
client._hub.postMessage({'cross-storage': 'init'}, targetOrigin) | ||
client._store.postMessage({'cross-storage': 'init'}, targetOrigin) | ||
}, 100) | ||
@@ -412,3 +474,3 @@ } | ||
* Invoked when a frame id was passed to the client, rather than allowing | ||
* the client to create its own iframe. Polls the hub for a ready event to | ||
* the client to create its own iframe. Polls the store for a ready event to | ||
* establish a connected state. | ||
@@ -426,5 +488,5 @@ */ | ||
if (client._connected) return clearInterval(interval) | ||
if (!client._hub) return | ||
if (!client._store) return | ||
client._hub.postMessage({'cross-storage': 'poll'}, targetOrigin) | ||
client._store.postMessage({'cross-storage': 'poll'}, targetOrigin) | ||
}, 100) | ||
@@ -434,3 +496,3 @@ } | ||
/** | ||
* Creates a new iFrame containing the hub. Applies the necessary styles to | ||
* Creates a new iFrame containing the store. Applies the necessary styles to | ||
* hide the element from view, prior to adding it to the document body. | ||
@@ -441,3 +503,3 @@ * Returns the created element. | ||
* | ||
* @param {string} url The url to the hub | ||
* @param {string} url The url to the store | ||
* returns {HTMLIFrameElement} The iFrame element itself | ||
@@ -465,3 +527,3 @@ */ | ||
/** | ||
* Sends a message containing the given method and params to the hub. Stores | ||
* Sends a message containing the given method and params to the store. Stores | ||
* a callback in the _requests object for later invocation on message, or | ||
@@ -474,3 +536,3 @@ * deletion on timeout. Returns a promise that is settled in either instance. | ||
* @param {*} params The arguments to pass | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -523,3 +585,3 @@ MasqClient.prototype._request = function (method, params) { | ||
// Send message | ||
client._hub.postMessage(req, targetOrigin) | ||
client._store.postMessage(req, targetOrigin) | ||
@@ -526,0 +588,0 @@ // Restore original toJSON |
@@ -1,1 +0,1 @@ | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).MasqClient=e()}}(function(){return function e(t,n,r){function o(s,c){if(!n[s]){if(!t[s]){var u="function"==typeof require&&require;if(!c&&u)return u(s,!0);if(i)return i(s,!0);var a=new Error("Cannot find module '"+s+"'");throw a.code="MODULE_NOT_FOUND",a}var l=n[s]={exports:{}};t[s][0].call(l.exports,function(e){var n=t[s][1][e];return o(n||e)},l,l.exports,e,t,n,r)}return n[s].exports}for(var i="function"==typeof require&&require,s=0;s<r.length;s++)o(r[s]);return o}({1:[function(e,t,n){!function(e){function r(e,t){e=e||"https://qwantresearch.github.io/masq-store/",t=t||{},this._id=r._generateUUID(),this._promise=t.promise||Promise,this._frameId=t.frameId||"MasqClient-"+this._id,this._origin=r._getOrigin(e),this._requests={},this._connected=!1,this._closed=!1,this._count=0,this._timeout=t.timeout||5e3,this._listener=null,this._installListener();var n;t.frameId&&(n=document.getElementById(t.frameId)),n&&this._poll(),n=n||this._createFrame(e),this._hub=n.contentWindow}r.frameStyle={display:"none",position:"absolute",top:"-999px",left:"-999px"},r._getOrigin=function(e){var t,n,r;return t=document.createElement("a"),t.href=e,t.host||(t=window.location),n=t.protocol&&":"!==t.protocol?t.protocol:window.location.protocol,r=n+"//"+t.host,r=r.replace(/:80$|:443$/,"")},r._generateUUID=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0;return("x"==e?t:3&t|8).toString(16)})},r.prototype.onConnect=function(){var e=this;return this._connected?this._promise.resolve():this._closed?this._promise.reject(new Error("MasqClient has closed")):(this._requests.connect||(this._requests.connect=[]),new this._promise(function(t,n){var r=setTimeout(function(){n(new Error("MasqClient could not connect"))},e._timeout);e._requests.connect.push(function(e){if(clearTimeout(r),e)return n(e);t()})}))},r.prototype.set=function(e,t){return this._request("set",{key:e,value:t})},r.prototype.get=function(e){var t=Array.prototype.slice.call(arguments);return this._request("get",{keys:t})},r.prototype.del=function(){var e=Array.prototype.slice.call(arguments);return this._request("del",{keys:e})},r.prototype.clear=function(){return this._request("clear")},r.prototype.getAll=function(){return this._request("getAll")},r.prototype.setAll=function(e){return this._request("setAll",e)},r.prototype.user=function(){return this._request("user")},r.prototype.close=function(){var e=document.getElementById(this._frameId);e&&e.parentNode.removeChild(e),window.removeEventListener?window.removeEventListener("message",this._listener,!1):window.detachEvent("onmessage",this._listener),this._connected=!1,this._closed=!0},r.prototype._installListener=function(){var e=this;this._listener=function(t){var n,r,o;if(!e._closed&&t.data&&("null"===t.origin?"file://":t.origin)===e._origin)if("unavailable"!==t.data["cross-storage"]){if(t.data["cross-storage"]&&!e._connected){if("listening"===t.data["cross-storage"])return void e._init();if(e._connected=!0,!e._requests.connect)return;for(n=0;n<e._requests.connect.length;n++)e._requests.connect[n](r);delete e._requests.connect}if("ready"!==t.data["cross-storage"]){try{o=t.data}catch(e){return}if(o.client){if(t.data.sync){var i=new CustomEvent("Sync");document.dispatchEvent(i)}e._requests[o.client]&&e._requests[o.client](o.error,o.result)}}}else{if(e._closed||e.close(),!e._requests.connect)return;for(r=new Error("Closing client. Could not access localStorage in hub."),n=0;n<e._requests.connect.length;n++)e._requests.connect[n](r)}},window.addEventListener?window.addEventListener("message",this._listener,!1):window.attachEvent("onmessage",this._listener)},r.prototype._init=function(){var e,t,n;n="file://"===(e=this)._origin?"*":e._origin,t=setInterval(function(){if(e._connected)return clearInterval(t);e._hub&&e._hub.postMessage({"cross-storage":"init"},n)},100)},r.prototype._poll=function(){var e,t,n;n="file://"===(e=this)._origin?"*":e._origin,t=setInterval(function(){if(e._connected)return clearInterval(t);e._hub&&e._hub.postMessage({"cross-storage":"poll"},n)},100)},r.prototype._createFrame=function(e){var t,n;(t=window.document.createElement("iframe")).id=this._frameId;for(n in r.frameStyle)r.frameStyle.hasOwnProperty(n)&&(t.style[n]=r.frameStyle[n]);return window.document.body.appendChild(t),t.src=e,t},r.prototype._request=function(e,t){var n,r;return this._closed?this._promise.reject(new Error("MasqClient has closed")):(r=this,r._count++,n={client:this._id+":"+r._count,method:e,params:t},new this._promise(function(e,t){var o,i,s;o=setTimeout(function(){r._requests[n.client]&&(delete r._requests[n.client],t(new Error("Timeout: could not perform "+n.method)))},r._timeout),r._requests[n.client]=function(i,s){if(clearTimeout(o),delete r._requests[n.client],i)return t(new Error(i));e(s)},Array.prototype.toJSON&&(i=Array.prototype.toJSON,Array.prototype.toJSON=null),s="file://"===r._origin?"*":r._origin,r._hub.postMessage(n,s),i&&(Array.prototype.toJSON=i)}))},void 0!==t&&t.exports?t.exports=r:void 0!==n?n.MasqClient=r:e.MasqClient=r}(this)},{}]},{},[1])(1)}); | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).MasqClient=e()}}(function(){return function e(t,n,o){function r(s,c){if(!n[s]){if(!t[s]){var l="function"==typeof require&&require;if(!c&&l)return l(s,!0);if(i)return i(s,!0);var d=new Error("Cannot find module '"+s+"'");throw d.code="MODULE_NOT_FOUND",d}var u=n[s]={exports:{}};t[s][0].call(u.exports,function(e){var n=t[s][1][e];return r(n||e)},u,u.exports,e,t,n,o)}return n[s].exports}for(var i="function"==typeof require&&require,s=0;s<o.length;s++)r(o[s]);return r}({1:[function(e,t,n){!function(e){function o(e,t){this._endpoint="https://qwantresearch.gitstore.io/masq-store/",e=e||this._endpoint,t=t||{},this._id=o._generateUUID(),this._promise=t.promise||Promise,this._frameId=t.frameId||"MasqClient-"+this._id,this._origin=o._getOrigin(e),this._requests={},this._connected=!1,this._closed=!1,this._count=0,this._timeout=t.timeout||5e3,this._listener=null,this._regwindow=null,this._installListener();var n;t.frameId&&(n=document.getElementById(t.frameId)),n&&this._poll(),n=n||this._createFrame(e),this._store=n.contentWindow}o.frameStyle={display:"none",position:"absolute",top:"-999px",left:"-999px"},o._getOrigin=function(e){var t,n,o;return t=document.createElement("a"),t.href=e,t.host||(t=window.location),n=t.protocol&&":"!==t.protocol?t.protocol:window.location.protocol,o=n+"//"+t.host,o=o.replace(/:80$|:443$/,"")},o._generateUUID=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0;return("x"==e?t:3&t|8).toString(16)})},o.prototype.onConnect=function(){var e=this;return this._connected?this._promise.resolve():this._closed?this._promise.reject(new Error("MasqClient has closed")):(this._requests.connect||(this._requests.connect=[]),new this._promise(function(t,n){var o=setTimeout(function(){n(new Error("MasqClient could not connect"))},e._timeout);e._requests.connect.push(function(e){if(clearTimeout(o),e)return n(e);t()})}))},o.prototype.registerApp=function(e){return new Promise(function(t,n){if(void 0===this._regwindow||this._regwindow.closed){e.endpoint=e.endpoint||this._endpoint,e.url||n(new Error("No app URL provided to registerApp()"));var o=e.endpoint+"?add=1&appUrl="+encodeURIComponent(e.url);e.title&&(o+="&title="+encodeURIComponent(e.title)),e.desc&&(o+="&desc="+encodeURIComponent(e.desc)),e.icon&&(o+="&icon="+encodeURIComponent(e.icon));var r=void 0!==window.screenLeft?window.screenLeft:window.screen.left,i=void 0!==window.screenTop?window.screenTop:window.screen.top,s=(window.innerWidth?window.innerWidth:document.documentElement.clientWidth?document.documentElement.clientWidth:window.screen.width)/2-200+r,c=(window.innerHeight?window.innerHeight:document.documentElement.clientHeight?document.documentElement.clientHeight:window.screen.height)/2-300+i;this._regwindow=window.open(o,"","scrollbars=yes, width=400, height=600, top="+c+", left="+s),window.focus&&this._regwindow.focus(),window.addEventListener("message",function(e){console.log("closing child",e),"REGISTRATIONFINISHED"===e.data&&t(e)},!1)}})},o.prototype.set=function(e,t){return this._request("set",{key:e,value:t})},o.prototype.get=function(e){var t=Array.prototype.slice.call(arguments);return this._request("get",{keys:t})},o.prototype.del=function(){var e=Array.prototype.slice.call(arguments);return this._request("del",{keys:e})},o.prototype.clear=function(){return this._request("clear")},o.prototype.getAll=function(){return this._request("getAll")},o.prototype.setAll=function(e){return this._request("setAll",e)},o.prototype.user=function(){return this._request("user")},o.prototype.close=function(){var e=document.getElementById(this._frameId);e&&e.parentNode.removeChild(e),window.removeEventListener?window.removeEventListener("message",this._listener,!1):window.detachEvent("onmessage",this._listener),this._connected=!1,this._closed=!0},o.prototype._installListener=function(){var e=this;this._listener=function(t){var n,o,r;if(!e._closed&&t.data&&("null"===t.origin?"file://":t.origin)===e._origin)if("unavailable"!==t.data["cross-storage"]){if(t.data["cross-storage"]&&!e._connected){if("listening"===t.data["cross-storage"])return void e._init();if(e._connected=!0,!e._requests.connect)return;for(n=0;n<e._requests.connect.length;n++)e._requests.connect[n](o);delete e._requests.connect}if("ready"!==t.data["cross-storage"]){try{r=t.data}catch(e){return}if(r.client){if(t.data.sync){var i=new CustomEvent("Sync");document.dispatchEvent(i)}e._requests[r.client]&&e._requests[r.client](r.error,r.result)}}}else{if(e._closed||e.close(),!e._requests.connect)return;for(o=new Error("Closing client. Could not access localStorage in store."),n=0;n<e._requests.connect.length;n++)e._requests.connect[n](o)}},window.addEventListener?window.addEventListener("message",this._listener,!1):window.attachEvent("onmessage",this._listener)},o.prototype._init=function(){var e,t,n;n="file://"===(e=this)._origin?"*":e._origin,t=setInterval(function(){if(e._connected)return clearInterval(t);e._store&&e._store.postMessage({"cross-storage":"init"},n)},100)},o.prototype._poll=function(){var e,t,n;n="file://"===(e=this)._origin?"*":e._origin,t=setInterval(function(){if(e._connected)return clearInterval(t);e._store&&e._store.postMessage({"cross-storage":"poll"},n)},100)},o.prototype._createFrame=function(e){var t,n;(t=window.document.createElement("iframe")).id=this._frameId;for(n in o.frameStyle)o.frameStyle.hasOwnProperty(n)&&(t.style[n]=o.frameStyle[n]);return window.document.body.appendChild(t),t.src=e,t},o.prototype._request=function(e,t){var n,o;return this._closed?this._promise.reject(new Error("MasqClient has closed")):(o=this,o._count++,n={client:this._id+":"+o._count,method:e,params:t},new this._promise(function(e,t){var r,i,s;r=setTimeout(function(){o._requests[n.client]&&(delete o._requests[n.client],t(new Error("Timeout: could not perform "+n.method)))},o._timeout),o._requests[n.client]=function(i,s){if(clearTimeout(r),delete o._requests[n.client],i)return t(new Error(i));e(s)},Array.prototype.toJSON&&(i=Array.prototype.toJSON,Array.prototype.toJSON=null),s="file://"===o._origin?"*":o._origin,o._store.postMessage(n,s),i&&(Array.prototype.toJSON=i)}))},void 0!==t&&t.exports?t.exports=o:void 0!==n?n.MasqClient=o:e.MasqClient=o}(this)},{}]},{},[1])(1)}); |
{ | ||
"name": "masq-client", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Client library for Masq", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
127
src/index.js
;(function (root) { | ||
/** | ||
* Forked from https://github.com/zendesk/cross-storage | ||
* Forked from https://gitstore.com/zendesk/cross-storage | ||
* | ||
* Constructs a new cross storage client given the url to a hub. By default, | ||
* Constructs a new cross storage client given the url to a store. By default, | ||
* an iframe is created within the document body that points to the url. It | ||
@@ -15,6 +15,6 @@ * also accepts an options object, which may include a timeout, frameId, and | ||
* @example | ||
* var storage = new MasqClient('https://store.example.com/hub.html'); | ||
* var storage = new MasqClient('https://store.example.com/store.html'); | ||
* | ||
* @example | ||
* var storage = new MasqClient('https://store.example.com/hub.html', { | ||
* var storage = new MasqClient('https://store.example.com/store.html', { | ||
* timeout: 5000, | ||
@@ -26,10 +26,11 @@ * frameId: 'storageFrame' | ||
* | ||
* @param {string} url The url to a cross storage hub | ||
* @param {string} url The url to a cross storage store | ||
* @param {object} [opts] An optional object containing additional options, | ||
* including timeout, frameId, and promise | ||
* | ||
* @property {string} _endpoint Default endpoint URL for the store | ||
* @property {string} _id A UUID v4 id | ||
* @property {function} _promise The Promise object to use | ||
* @property {string} _frameId The id of the iFrame pointing to the hub url | ||
* @property {string} _origin The hub's origin | ||
* @property {string} _frameId The id of the iFrame pointing to the store url | ||
* @property {string} _origin The store's origin | ||
* @property {object} _requests Mapping of request ids to callbacks | ||
@@ -40,6 +41,9 @@ * @property {bool} _connected Whether or not it has connected | ||
* @property {function} _listener The listener added to the window | ||
* @property {Window} _hub The hub window | ||
* @property {Window} _store The store window | ||
* @property {Window} _regwindow The app registration window | ||
*/ | ||
function MasqClient (url, opts) { | ||
url = url || 'https://qwantresearch.github.io/masq-store/' | ||
this._endpoint = 'https://qwantresearch.gitstore.io/masq-store/' | ||
url = url || this._endpoint | ||
opts = opts || {} | ||
@@ -57,2 +61,3 @@ | ||
this._listener = null | ||
this._regwindow = null | ||
@@ -66,3 +71,3 @@ this._installListener() | ||
// If using a passed iframe, poll the hub for a ready message | ||
// If using a passed iframe, poll the store for a ready message | ||
if (frame) { | ||
@@ -74,3 +79,3 @@ this._poll() | ||
frame = frame || this._createFrame(url) | ||
this._hub = frame.contentWindow | ||
this._store = frame.contentWindow | ||
} | ||
@@ -99,3 +104,3 @@ | ||
* | ||
* @param {string} url The url to a cross storage hub | ||
* @param {string} url The url to a cross storage store | ||
* @returns {string} The origin of the url | ||
@@ -141,3 +146,3 @@ */ | ||
* Returns a promise that is fulfilled when a connection has been established | ||
* with the cross storage hub. Its use is required to avoid sending any | ||
* with the cross storage store. Its use is required to avoid sending any | ||
* requests prior to initialization being complete. | ||
@@ -176,2 +181,54 @@ * | ||
/** | ||
* Registers an app with the store. | ||
* | ||
* @param {object} params Parameters that describe the app | ||
* @returns {Promise} A promise that is settled on app registration status | ||
*/ | ||
MasqClient.prototype.registerApp = function (params) { | ||
return new Promise(function (resolve, reject) { | ||
if (this._regwindow === undefined || this._regwindow.closed) { | ||
var w = 400 | ||
var h = 600 | ||
params.endpoint = params.endpoint || this._endpoint | ||
if (!params.url) { | ||
reject(new Error('No app URL provided to registerApp()')) | ||
} | ||
var url = params.endpoint + '?add=1&appUrl=' + encodeURIComponent(params.url) | ||
if (params.title) { | ||
url += '&title=' + encodeURIComponent(params.title) | ||
} | ||
if (params.desc) { | ||
url += '&desc=' + encodeURIComponent(params.desc) | ||
} | ||
if (params.icon) { | ||
url += '&icon=' + encodeURIComponent(params.icon) | ||
} | ||
var dualScreenLeft = window.screenLeft !== undefined ? window.screenLeft : window.screen.left | ||
var dualScreenTop = window.screenTop !== undefined ? window.screenTop : window.screen.top | ||
var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : window.screen.width | ||
var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : window.screen.height | ||
var left = ((width / 2) - (w / 2)) + dualScreenLeft | ||
var top = ((height / 2) - (h / 2)) + dualScreenTop | ||
this._regwindow = window.open(url, '', 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left) | ||
// Puts focus on the newWindow | ||
if (window.focus) { | ||
this._regwindow.focus() | ||
} | ||
// wrap onunload in a load event to avoid it being triggered too early | ||
window.addEventListener('message', function (e) { | ||
if (e.data === 'REGISTRATIONFINISHED') { | ||
resolve(e) | ||
} | ||
}, false) | ||
} | ||
}) | ||
} | ||
/** | ||
* Sets a key to the specified value. Returns a promise that is fulfilled on | ||
@@ -183,3 +240,3 @@ * success, or rejected if any errors setting the key occurred, or the request | ||
* @param {*} value The value to assign | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -195,3 +252,3 @@ MasqClient.prototype.set = function (key, value) { | ||
* Accepts one or more keys for which to retrieve their values. Returns a | ||
* promise that is settled on hub response or timeout. On success, it is | ||
* promise that is settled on store response or timeout. On success, it is | ||
* fulfilled with the value of the key if only passed a single argument. | ||
@@ -202,3 +259,3 @@ * Otherwise it's resolved with an array of values. On failure, it is rejected | ||
* @param {...string} key The key to retrieve | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -213,6 +270,6 @@ MasqClient.prototype.get = function (key) { | ||
* Accepts one or more keys for deletion. Returns a promise that is settled on | ||
* hub response or timeout. | ||
* store response or timeout. | ||
* | ||
* @param {...string} key The key to delete | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -231,3 +288,3 @@ MasqClient.prototype.del = function () { | ||
* | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -244,3 +301,3 @@ MasqClient.prototype.clear = function () { | ||
* | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -258,3 +315,3 @@ MasqClient.prototype.getAll = function () { | ||
* @param {object} data The data object to set | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -271,3 +328,3 @@ MasqClient.prototype.setAll = function (data) { | ||
* | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -326,3 +383,3 @@ MasqClient.prototype.user = function () { | ||
// LocalStorage isn't available in the hub | ||
// LocalStorage isn't available in the store | ||
if (message.data['cross-storage'] === 'unavailable') { | ||
@@ -332,3 +389,3 @@ if (!client._closed) client.close() | ||
error = new Error('Closing client. Could not access localStorage in hub.') | ||
error = new Error('Closing client. Could not access localStorage in store.') | ||
for (i = 0; i < client._requests.connect.length; i++) { | ||
@@ -388,3 +445,3 @@ client._requests.connect[i](error) | ||
* Invoked when a frame id was passed to the client, rather than allowing | ||
* the client to create its own iframe. Polls the hub for a ready event to | ||
* the client to create its own iframe. Polls the store for a ready event to | ||
* establish a connected state. | ||
@@ -402,5 +459,5 @@ */ | ||
if (client._connected) return clearInterval(interval) | ||
if (!client._hub) return | ||
if (!client._store) return | ||
client._hub.postMessage({'cross-storage': 'init'}, targetOrigin) | ||
client._store.postMessage({'cross-storage': 'init'}, targetOrigin) | ||
}, 100) | ||
@@ -411,3 +468,3 @@ } | ||
* Invoked when a frame id was passed to the client, rather than allowing | ||
* the client to create its own iframe. Polls the hub for a ready event to | ||
* the client to create its own iframe. Polls the store for a ready event to | ||
* establish a connected state. | ||
@@ -425,5 +482,5 @@ */ | ||
if (client._connected) return clearInterval(interval) | ||
if (!client._hub) return | ||
if (!client._store) return | ||
client._hub.postMessage({'cross-storage': 'poll'}, targetOrigin) | ||
client._store.postMessage({'cross-storage': 'poll'}, targetOrigin) | ||
}, 100) | ||
@@ -433,3 +490,3 @@ } | ||
/** | ||
* Creates a new iFrame containing the hub. Applies the necessary styles to | ||
* Creates a new iFrame containing the store. Applies the necessary styles to | ||
* hide the element from view, prior to adding it to the document body. | ||
@@ -440,3 +497,3 @@ * Returns the created element. | ||
* | ||
* @param {string} url The url to the hub | ||
* @param {string} url The url to the store | ||
* returns {HTMLIFrameElement} The iFrame element itself | ||
@@ -464,3 +521,3 @@ */ | ||
/** | ||
* Sends a message containing the given method and params to the hub. Stores | ||
* Sends a message containing the given method and params to the store. Stores | ||
* a callback in the _requests object for later invocation on message, or | ||
@@ -473,3 +530,3 @@ * deletion on timeout. Returns a promise that is settled in either instance. | ||
* @param {*} params The arguments to pass | ||
* @returns {Promise} A promise that is settled on hub response or timeout | ||
* @returns {Promise} A promise that is settled on store response or timeout | ||
*/ | ||
@@ -522,3 +579,3 @@ MasqClient.prototype._request = function (method, params) { | ||
// Send message | ||
client._hub.postMessage(req, targetOrigin) | ||
client._store.postMessage(req, targetOrigin) | ||
@@ -525,0 +582,0 @@ // Restore original toJSON |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59669
992
6