[Translation] Cross-storage: Make local data available between domains

Original author: Alex Young
  • Transfer

As we know the localStorage API has some limitations that may have to be circumvented when writing large applications. The new cross-storage * library provides cross- domain support for localStorage using permissions. This library also includes the Promise API for the ES6 standard .

Cross-storage uses two components: hubs and clients. Hubs can set permissions depending on the domain and this is nothing more than the forced use of the principle of the same source . The specified library includes access types such as read, write, and delete ( get, set, del).

  { origin: /\.example.com$/, allow: ['get'] },
  { origin: /:(www\.)?example.com$/, allow: ['get', 'set', 'del'] }

The client, in turn, will be able to access the hub as follows:

var storage = new CrossStorageClient('https://store.example.com/hub.html');
storage.onConnect().then(function() {
  // Set a key with a TTL of 90 seconds
  return storage.set('newKey', 'foobar', 90000);
}).then(function() {
  return storage.get('existingKey', 'newKey');
}).then(function(res) {
  console.log(res.length); // 2
}).catch(function(err) {
  // Handle error

Note that the method onConnectreturns a promise that is executed when the connection to the hub has been established. You can also call storage.closeat the end of a connection that is implemented using iframe.

Daniel recommends using the es6 -promise polyfile for older browsers.

The project uses Gulp to build client code and also comes bundled with zuul tests .


* Cross-storage library, license: Apache 2.0, npm: cross-storage , bower:, cross-storageauthor: Daniel St. Jules

Also popular now: