📜 ⬆️ ⬇️

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

image

As we know, the localStorage API has some limitations that you may have to bypass when writing large applications. The new cross-storage * library allows for cross-storage support of localStorage using privileges. This library also includes the Promise API for the ES6 standard.

Cross-storage uses two components: hubs (hubs) and clients (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 such types of access as read, write, and delete ( get, set, del ).

 CrossStorageHub.init([ { 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 onConnect method returns a promise that is executed when the connection to the hub has been established. You can also call storage.close at the end of the connection, which is implemented using an iframe .

Daniel recommends using es6 -promise for older browsers.

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

Marks

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

Source: https://habr.com/ru/post/236223/


All Articles