Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add storage opt in hub. Can now configure localStorage or sessionStorage #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Features an API using ES6 promises.
* [Overview](#overview)
* [Installation](#installation)
* [API](#api)
* [CrossStorageHub.init(permissions)](#crossstoragehubinitpermissions)
* [CrossStorageHub.init(opts)](#crossstoragehubinitopts)
* [new CrossStorageClient(url, \[opts\])](#new-crossstorageclienturl-opts)
* [CrossStorageClient.prototype.onConnect()](#crossstorageclientprototypeonconnect)
* [CrossStorageClient.prototype.set(key, value)](#crossstorageclientprototypesetkey-value)
Expand All @@ -30,13 +30,13 @@ The library is a convenient alternative to sharing a root domain cookie.
Unlike cookies, your client-side data isn't limited to a few kilobytes - you
get up to 2.49M chars. For a client-heavy application, you can potentially
shave a few KB off your request headers by avoiding cookies. This is all thanks
to LocalStorage, which is available in IE 8+, FF 3.5+, Chrome 4+, as well as a
to LocalStorage / SessionStorage API, which is available in IE 8+, FF 3.5+, Chrome 4+, as well as a
majority of mobile browsers. For a list of compatible browsers, refer to
[caniuse](http://caniuse.com/#feat=namevalue-storage).

How does it work? The library is divided into two types of components: hubs
and clients. The hubs reside on a host of choice and interact directly with
the LocalStorage API. The clients then load said hub over an embedded iframe
the LocalStorage / SessionStorage API. The clients then load said hub over an embedded iframe
and post messages, requesting data to be stored, retrieved, and deleted. This
allows multiple clients to access and share the data located in a single store.

Expand Down Expand Up @@ -119,10 +119,13 @@ init code via another resource.

## API

#### CrossStorageHub.init(permissions)
#### CrossStorageHub.init(opts)

Accepts an array of objects with two keys: origin and allow. The value
Accepts an option object, which may include a permissions and a storage
The permissions is an array of objects with two keys: origin and allow. The value
of origin is expected to be a RegExp, and allow, an array of strings.
The storage option can be one of `window.localStorage`
or `window.sessionStorage`. Default is window.localStorage (if supported).
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, getKeys and clear. A 'ready'
Expand Down Expand Up @@ -222,7 +225,7 @@ storage.onConnect().then(function() {

#### CrossStorageClient.prototype.clear()

Returns a promise that, when resolved, indicates that all localStorage
Returns a promise that, when resolved, indicates that all storage
data has been cleared.

``` javascript
Expand Down Expand Up @@ -283,7 +286,7 @@ cookies for those user agents, or requesting the data from a server-side store.

## Compression

Most localStorage-compatible browsers offer at least ~5Mb of storage. But keys
Most localStorage/sessionStorage-compatible browsers offer at least ~5Mb of storage. But keys
and values are defined as DOMStrings, which are UTF-8 encoded using single
16-bit sequences. That means a string of ~2.5 million ASCII characters will use
up ~5Mb, since they're 2 bytes per char.
Expand Down
6 changes: 3 additions & 3 deletions dist/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
};

/**
* Returns a promise that, when resolved, indicates that all localStorage
* Returns a promise that, when resolved, indicates that all storage
* data has been cleared.
*
* @returns {Promise} A promise that is settled on hub response or timeout
Expand Down Expand Up @@ -284,12 +284,12 @@
// Ignore messages not from the correct origin
if (origin !== client._origin) return;

// LocalStorage isn't available in the hub
// Underlying storage isn't available in the hub
if (message.data === 'cross-storage:unavailable') {
if (!client._closed) client.close();
if (!client._requests.connect) return;

error = new Error('Closing client. Could not access localStorage in hub.');
error = new Error('Closing client. Could not access underlying storage in hub.');
for (i = 0; i < client._requests.connect.length; i++) {
client._requests.connect[i](error);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/client.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 20 additions & 13 deletions dist/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@
* {origin: /:(www\.)?example.com$/, allow: ['get', 'set', 'del']}
* ]);
*
* @param {array} permissions An array of objects with origin and allow
* @param {object} [opts] An optional object containing additional options,
* including timeout, frameId, and promise
*
* @param {array} _permissions An array of objects with origin and allow
*/
CrossStorageHub.init = function(permissions) {
CrossStorageHub.init = function(opts) {
opts = opts || {};

CrossStorageHub._storage = opts.storage || window.localStorage;

var available = true;

// Return if localStorage is unavailable, or third party
// Return if storage is unavailable, or third party
// access is disabled
try {
if (!window.localStorage) available = false;
if (!CrossStorageHub._storage) available = false;
} catch (e) {
available = false;
}
Expand All @@ -47,7 +54,7 @@
}
}

CrossStorageHub._permissions = permissions || [];
CrossStorageHub._permissions = opts.permissions || [];
CrossStorageHub._installListener();
window.parent.postMessage('cross-storage:ready', '*');
};
Expand Down Expand Up @@ -165,7 +172,7 @@
* @param {object} params An object with key and value
*/
CrossStorageHub._set = function(params) {
window.localStorage.setItem(params.key, params.value);
CrossStorageHub._storage.setItem(params.key, params.value);
};

/**
Expand All @@ -179,7 +186,7 @@
CrossStorageHub._get = function(params) {
var storage, result, i, value;

storage = window.localStorage;
storage = CrossStorageHub._storage;
result = [];

for (i = 0; i < params.keys.length; i++) {
Expand All @@ -202,30 +209,30 @@
*/
CrossStorageHub._del = function(params) {
for (var i = 0; i < params.keys.length; i++) {
window.localStorage.removeItem(params.keys[i]);
CrossStorageHub._storage.removeItem(params.keys[i]);
}
};

/**
* Clears localStorage.
* Clears storage.
*/
CrossStorageHub._clear = function() {
window.localStorage.clear();
CrossStorageHub._storage.clear();
};

/**
* Returns an array of all keys stored in localStorage.
* Returns an array of all keys stored in storage.
*
* @returns {string[]} The array of keys
*/
CrossStorageHub._getKeys = function(params) {
var i, length, keys;

keys = [];
length = window.localStorage.length;
length = CrossStorageHub._storage.length;

for (i = 0; i < length; i++) {
keys.push(window.localStorage.key(i));
keys.push(CrossStorageHub._storage.key(i));
}

return keys;
Expand Down
2 changes: 1 addition & 1 deletion dist/hub.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading