From fbf662a85972660ee63a21740a264fa528bef17a Mon Sep 17 00:00:00 2001 From: "Daniel St. Jules" Date: Wed, 10 Dec 2014 15:54:43 -0800 Subject: [PATCH] 0.5.0 --- bower.json | 2 +- dist/client-0.4.1.min.js | 11 ----------- dist/{client-0.4.1.js => client.js} | 12 +++++++++--- dist/client.min.js | 11 +++++++++++ dist/hub-0.4.1.min.js | 11 ----------- dist/{hub-0.4.1.js => hub.js} | 16 ++++++++++------ dist/hub.min.js | 11 +++++++++++ package.json | 2 +- 8 files changed, 43 insertions(+), 33 deletions(-) delete mode 100644 dist/client-0.4.1.min.js rename dist/{client-0.4.1.js => client.js} (97%) create mode 100644 dist/client.min.js delete mode 100644 dist/hub-0.4.1.min.js rename dist/{hub-0.4.1.js => hub.js} (95%) create mode 100644 dist/hub.min.js diff --git a/bower.json b/bower.json index 13269ed..80cf9f6 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "cross-storage", - "version": "0.4.0", + "version": "0.5.0", "description": "Cross domain local storage", "license": "Apache-2.0", "authors": [ diff --git a/dist/client-0.4.1.min.js b/dist/client-0.4.1.min.js deleted file mode 100644 index 7724e6a..0000000 --- a/dist/client-0.4.1.min.js +++ /dev/null @@ -1,11 +0,0 @@ -/** - * cross-storage - Cross domain local storage - * - * @version 0.4.1 - * @link https://github.com/zendesk/cross-storage - * @author Daniel St. Jules - * @copyright Zendesk - * @license Apache-2.0 - */ - -function CrossStorageClient(e,t){t=t||{},this._id=CrossStorageClient._generateUUID(),this._promise=t.promise||Promise,this._frameId=t.frameId||"CrossStorageClient-"+this._id,this._origin=CrossStorageClient._getOrigin(e),this._requests={},this._connected=!1,this._closed=!1,this._count=0,this._timeout=t.timeout||3e3,this._listener=null,this._installListener();var r;t.frameId&&(r=document.getElementById(t.frameId)),r&&this._poll(),r=r||this._createFrame(e),this._hub=r.contentWindow}CrossStorageClient.frameStyle={display:"none",position:"absolute",top:"-999px",left:"-999px"},CrossStorageClient._getOrigin=function(e){var t,r;return t=document.createElement("a"),t.href=e,r=t.protocol+"//"+t.host,r=r.replace(/:80$|:443$/,"")},CrossStorageClient._generateUUID=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0,r="x"==e?t:3&t|8;return r.toString(16)})},CrossStorageClient.prototype.onConnect=function(){var e=this;return this._connected?this._promise.resolve():this._closed?this._promise.reject(new Error("CrossStorageClient has closed")):(this._requests.connect||(this._requests.connect=[]),new this._promise(function(t,r){var o=setTimeout(function(){r(new Error("CrossStorageClient could not connect"))},e._timeout);e._requests.connect.push(function(e){return clearTimeout(o),e?r(e):void t()})}))},CrossStorageClient.prototype.set=function(e,t,r){return this._request("set",{key:e,value:t,ttl:r})},CrossStorageClient.prototype.get=function(){var e=Array.prototype.slice.call(arguments);return this._request("get",{keys:e})},CrossStorageClient.prototype.del=function(){var e=Array.prototype.slice.call(arguments);return this._request("del",{keys:e})},CrossStorageClient.prototype.clear=function(e){return this._request("clear",e)},CrossStorageClient.prototype.getKeys=function(){return this._request("getKeys")},CrossStorageClient.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},CrossStorageClient.prototype._installListener=function(){var e=this;this._listener=function(t){var r,o,n;if(!e._closed&&t.origin===e._origin)if("unavailable"!==t.data){if(!e._connected){if(e._connected=!0,!e._requests.connect)return;for(r=0;r * @copyright Zendesk @@ -207,8 +207,14 @@ CrossStorageClient.prototype.del = function() { return this._request('del', {keys: args}); }; -CrossStorageClient.prototype.clear = function (params) { - return this._request('clear', params); +/** + * Returns a promise that, when resolved, indicates that all localStorage + * data has been cleared. + * + * @returns {Promise} A promise that is settled on hub response or timeout + */ +CrossStorageClient.prototype.clear = function() { + return this._request('clear'); }; /** diff --git a/dist/client.min.js b/dist/client.min.js new file mode 100644 index 0000000..7e1e0fb --- /dev/null +++ b/dist/client.min.js @@ -0,0 +1,11 @@ +/** + * cross-storage - Cross domain local storage + * + * @version 0.5.0 + * @link https://github.com/zendesk/cross-storage + * @author Daniel St. Jules + * @copyright Zendesk + * @license Apache-2.0 + */ + +function CrossStorageClient(e,t){t=t||{},this._id=CrossStorageClient._generateUUID(),this._promise=t.promise||Promise,this._frameId=t.frameId||"CrossStorageClient-"+this._id,this._origin=CrossStorageClient._getOrigin(e),this._requests={},this._connected=!1,this._closed=!1,this._count=0,this._timeout=t.timeout||3e3,this._listener=null,this._installListener();var r;t.frameId&&(r=document.getElementById(t.frameId)),r&&this._poll(),r=r||this._createFrame(e),this._hub=r.contentWindow}CrossStorageClient.frameStyle={display:"none",position:"absolute",top:"-999px",left:"-999px"},CrossStorageClient._getOrigin=function(e){var t,r;return t=document.createElement("a"),t.href=e,r=t.protocol+"//"+t.host,r=r.replace(/:80$|:443$/,"")},CrossStorageClient._generateUUID=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0,r="x"==e?t:3&t|8;return r.toString(16)})},CrossStorageClient.prototype.onConnect=function(){var e=this;return this._connected?this._promise.resolve():this._closed?this._promise.reject(new Error("CrossStorageClient has closed")):(this._requests.connect||(this._requests.connect=[]),new this._promise(function(t,r){var o=setTimeout(function(){r(new Error("CrossStorageClient could not connect"))},e._timeout);e._requests.connect.push(function(e){return clearTimeout(o),e?r(e):(t(),void 0)})}))},CrossStorageClient.prototype.set=function(e,t,r){return this._request("set",{key:e,value:t,ttl:r})},CrossStorageClient.prototype.get=function(){var e=Array.prototype.slice.call(arguments);return this._request("get",{keys:e})},CrossStorageClient.prototype.del=function(){var e=Array.prototype.slice.call(arguments);return this._request("del",{keys:e})},CrossStorageClient.prototype.clear=function(){return this._request("clear")},CrossStorageClient.prototype.getKeys=function(){return this._request("getKeys")},CrossStorageClient.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},CrossStorageClient.prototype._installListener=function(){var e=this;this._listener=function(t){var r,o,n;if(!e._closed&&t.origin===e._origin)if("unavailable"!==t.data){if(!e._connected){if(e._connected=!0,!e._requests.connect)return;for(r=0;r - * @copyright Zendesk - * @license Apache-2.0 - */ - -var CrossStorageHub={};CrossStorageHub.init=function(e){var r=!0;try{window.localStorage||(r=!1)}catch(o){r=!1}return r?(CrossStorageHub._permissions=e||[],CrossStorageHub._installListener(),void window.parent.postMessage("ready","*")):window.parent.postMessage("unavailable","*")},CrossStorageHub._installListener=function(){var e=CrossStorageHub._listener;window.addEventListener?window.addEventListener("message",e,!1):window.attachEvent("onmessage",e)},CrossStorageHub._listener=function(e){var r,o,t,s;if("poll"===e.data)return window.parent.postMessage("ready",e.origin);if("ready"!==e.data){if(r=JSON.parse(e.data),CrossStorageHub._permitted(e.origin,r.method))try{t=CrossStorageHub["_"+r.method](r.params)}catch(n){o=n.message}else o="Invalid permissions for "+r.method;s=JSON.stringify({id:r.id,error:o,result:t}),window.parent.postMessage(s,e.origin)}},CrossStorageHub._permitted=function(e,r){var o,t,s,n;if(o=["get","set","del","clear","getKeys"],!CrossStorageHub._inArray(r,o))return!1;for(t=0;t1?o:o[0]},CrossStorageHub._del=function(e){for(var r=0;re;e++)o.push(window.localStorage.key(e));return o},CrossStorageHub._inArray=function(e,r){for(var o=0;o * @copyright Zendesk @@ -15,8 +15,8 @@ var CrossStorageHub = {}; * of origin is expected to be a RegExp, and allow, an array of strings. * The cross storage hub is then initialized to accept requests from any of * the matching origins, allowing access to the associated lists of methods. - * Methods may include any of: get, set, del, and getKeys. A 'ready' message - * is sent to the parent window once complete. + * Methods may include any of: get, set, del, getKeys and clear. A 'ready' + * message is sent to the parent window once complete. * * @example * // Subdomain can get, but only root domain can set and del @@ -39,7 +39,11 @@ CrossStorageHub.init = function(permissions) { } if (!available) { - return window.parent.postMessage('unavailable', '*'); + try { + return window.parent.postMessage('unavailable', '*'); + } catch (e) { + return; + } } CrossStorageHub._permissions = permissions || []; @@ -199,9 +203,9 @@ CrossStorageHub._del = function(params) { }; /** - * Clears the local storage + * Clears localStorage. */ -CrossStorageHub._clear = function () { +CrossStorageHub._clear = function() { window.localStorage.clear(); }; diff --git a/dist/hub.min.js b/dist/hub.min.js new file mode 100644 index 0000000..d53632e --- /dev/null +++ b/dist/hub.min.js @@ -0,0 +1,11 @@ +/** + * cross-storage - Cross domain local storage + * + * @version 0.5.0 + * @link https://github.com/zendesk/cross-storage + * @author Daniel St. Jules + * @copyright Zendesk + * @license Apache-2.0 + */ + +var CrossStorageHub={};CrossStorageHub.init=function(e){var r=!0;try{window.localStorage||(r=!1)}catch(t){r=!1}if(!r)try{return window.parent.postMessage("unavailable","*")}catch(t){return}CrossStorageHub._permissions=e||[],CrossStorageHub._installListener(),window.parent.postMessage("ready","*")},CrossStorageHub._installListener=function(){var e=CrossStorageHub._listener;window.addEventListener?window.addEventListener("message",e,!1):window.attachEvent("onmessage",e)},CrossStorageHub._listener=function(e){var r,t,o,s;if("poll"===e.data)return window.parent.postMessage("ready",e.origin);if("ready"!==e.data){if(r=JSON.parse(e.data),CrossStorageHub._permitted(e.origin,r.method))try{o=CrossStorageHub["_"+r.method](r.params)}catch(n){t=n.message}else t="Invalid permissions for "+r.method;s=JSON.stringify({id:r.id,error:t,result:o}),window.parent.postMessage(s,e.origin)}},CrossStorageHub._permitted=function(e,r){var t,o,s,n;if(t=["get","set","del","clear","getKeys"],!CrossStorageHub._inArray(r,t))return!1;for(o=0;o1?t:t[0]},CrossStorageHub._del=function(e){for(var r=0;re;e++)t.push(window.localStorage.key(e));return t},CrossStorageHub._inArray=function(e,r){for(var t=0;t