@ohos-ports/cross-storage 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +176 -0
- package/README.md +325 -0
- package/dist/client.js +465 -0
- package/dist/client.min.js +11 -0
- package/dist/hub.js +279 -0
- package/dist/hub.min.js +11 -0
- package/lib/client.js +629 -0
- package/lib/hub.js +299 -0
- package/lib/index.js +4 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
Cross domain local storage, with permissions. Enables multiple browser
|
|
4
|
+
windows/tabs, across a variety of domains, to share a single localStorage.
|
|
5
|
+
Features an API using ES6 promises.
|
|
6
|
+
|
|
7
|
+
[](https://travis-ci.org/zendesk/cross-storage)
|
|
8
|
+
|
|
9
|
+
* [Overview](#overview)
|
|
10
|
+
* [Installation](#installation)
|
|
11
|
+
* [API](#api)
|
|
12
|
+
* [CrossStorageHub.init(permissions)](#crossstoragehubinitpermissions)
|
|
13
|
+
* [new CrossStorageClient(url, \[opts\])](#new-crossstorageclienturl-opts)
|
|
14
|
+
* [CrossStorageClient.prototype.onConnect()](#crossstorageclientprototypeonconnect)
|
|
15
|
+
* [CrossStorageClient.prototype.set(key, value)](#crossstorageclientprototypesetkey-value)
|
|
16
|
+
* [CrossStorageClient.prototype.get(key1, \[key2\], \[...\])](#crossstorageclientprototypegetkey1-key2-)
|
|
17
|
+
* [CrossStorageClient.prototype.del(key1, \[key2\], \[...\])](#crossstorageclientprototypedelkey1-key2-)
|
|
18
|
+
* [CrossStorageClient.prototype.getKeys()](#crossstorageclientprototypegetkeys)
|
|
19
|
+
* [CrossStorageClient.prototype.clear()](#crossstorageclientprototypeclear)
|
|
20
|
+
* [CrossStorageClient.prototype.close()](#crossstorageclientprototypeclose)
|
|
21
|
+
* [Compatibility](#compatibility)
|
|
22
|
+
* [Compression](#compression)
|
|
23
|
+
* [Building](#building)
|
|
24
|
+
* [Tests](#tests)
|
|
25
|
+
* [Copyright and license](#copyright-and-license)
|
|
26
|
+
|
|
27
|
+
## Overview
|
|
28
|
+
|
|
29
|
+
The library is a convenient alternative to sharing a root domain cookie.
|
|
30
|
+
Unlike cookies, your client-side data isn't limited to a few kilobytes - you
|
|
31
|
+
get up to 2.49M chars. For a client-heavy application, you can potentially
|
|
32
|
+
shave a few KB off your request headers by avoiding cookies. This is all thanks
|
|
33
|
+
to LocalStorage, which is available in IE 8+, FF 3.5+, Chrome 4+, as well as a
|
|
34
|
+
majority of mobile browsers. For a list of compatible browsers, refer to
|
|
35
|
+
[caniuse](http://caniuse.com/#feat=namevalue-storage).
|
|
36
|
+
|
|
37
|
+
How does it work? The library is divided into two types of components: hubs
|
|
38
|
+
and clients. The hubs reside on a host of choice and interact directly with
|
|
39
|
+
the LocalStorage API. The clients then load said hub over an embedded iframe
|
|
40
|
+
and post messages, requesting data to be stored, retrieved, and deleted. This
|
|
41
|
+
allows multiple clients to access and share the data located in a single store.
|
|
42
|
+
|
|
43
|
+
Care should be made to limit the origins of the bidirectional communication.
|
|
44
|
+
As such, when initializing the hub, an array of permissions objects is passed.
|
|
45
|
+
Any messages from clients whose origin does not match the pattern are ignored,
|
|
46
|
+
as well as those not within the allowed set of methods. The set of permissions
|
|
47
|
+
are enforced thanks to the same-origin policy. However, keep in mind that any
|
|
48
|
+
user has full control of their local storage data - it's still client data.
|
|
49
|
+
This only restricts access on a per-domain or web app level.
|
|
50
|
+
|
|
51
|
+
**Hub**
|
|
52
|
+
|
|
53
|
+
``` javascript
|
|
54
|
+
// Config s.t. subdomains can get, but only the root domain can set and del
|
|
55
|
+
CrossStorageHub.init([
|
|
56
|
+
{origin: /\.example.com$/, allow: ['get']},
|
|
57
|
+
{origin: /:\/\/(www\.)?example.com$/, allow: ['get', 'set', 'del']}
|
|
58
|
+
]);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Note the $ for matching the end of the string. The RegExps in the above example
|
|
62
|
+
will match origins such as valid.example.com, but not
|
|
63
|
+
invalid.example.com.malicious.com.
|
|
64
|
+
|
|
65
|
+
**Client**
|
|
66
|
+
|
|
67
|
+
``` javascript
|
|
68
|
+
var storage = new CrossStorageClient('https://store.example.com/hub.html');
|
|
69
|
+
|
|
70
|
+
storage.onConnect().then(function() {
|
|
71
|
+
return storage.set('newKey', 'foobar');
|
|
72
|
+
}).then(function() {
|
|
73
|
+
return storage.get('existingKey', 'newKey');
|
|
74
|
+
}).then(function(res) {
|
|
75
|
+
console.log(res.length); // 2
|
|
76
|
+
}).catch(function(err) {
|
|
77
|
+
// Handle error
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Installation
|
|
82
|
+
|
|
83
|
+
The library can be installed via bower:
|
|
84
|
+
|
|
85
|
+
``` bash
|
|
86
|
+
bower install cross-storage
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Or using npm:
|
|
90
|
+
|
|
91
|
+
``` bash
|
|
92
|
+
npm install cross-storage
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
along with browserify:
|
|
96
|
+
|
|
97
|
+
``` javascript
|
|
98
|
+
var CrossStorageClient = require('cross-storage').CrossStorageClient;
|
|
99
|
+
var CrossStorageHub = require('cross-storage').CrossStorageHub;
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
When serving the hub, you may want to set the CORS and CSP headers for your
|
|
103
|
+
server depending on client/hub location. For example:
|
|
104
|
+
|
|
105
|
+
``` javascript
|
|
106
|
+
{
|
|
107
|
+
'Access-Control-Allow-Origin': '*',
|
|
108
|
+
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
|
|
109
|
+
'Access-Control-Allow-Headers': 'X-Requested-With',
|
|
110
|
+
'Content-Security-Policy': "default-src 'unsafe-inline' *",
|
|
111
|
+
'X-Content-Security-Policy': "default-src 'unsafe-inline' *",
|
|
112
|
+
'X-WebKit-CSP': "default-src 'unsafe-inline' *",
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
If using inline JS to create the hub, you'll need to specify `unsafe-inline`
|
|
117
|
+
for the CSP headers. Otherwise, it can be left out if simply including the
|
|
118
|
+
init code via another resource.
|
|
119
|
+
|
|
120
|
+
## API
|
|
121
|
+
|
|
122
|
+
#### CrossStorageHub.init(permissions)
|
|
123
|
+
|
|
124
|
+
Accepts an array of objects with two keys: origin and allow. The value
|
|
125
|
+
of origin is expected to be a RegExp, and allow, an array of strings.
|
|
126
|
+
The cross storage hub is then initialized to accept requests from any of
|
|
127
|
+
the matching origins, allowing access to the associated lists of methods.
|
|
128
|
+
Methods may include any of: get, set, del, getKeys and clear. A 'ready'
|
|
129
|
+
message is sent to the parent window once complete.
|
|
130
|
+
|
|
131
|
+
``` javascript
|
|
132
|
+
CrossStorageHub.init([
|
|
133
|
+
{origin: /localhost:3000$/, allow: ['get', 'set', 'del', 'getKeys', 'clear']}
|
|
134
|
+
]);
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### new CrossStorageClient(url, [opts])
|
|
138
|
+
|
|
139
|
+
Constructs a new cross storage client given the url to a hub. By default,
|
|
140
|
+
an iframe is created within the document body that points to the url. It
|
|
141
|
+
also accepts an options object, which may include a timeout, frameId, and
|
|
142
|
+
promise. The timeout, in milliseconds, is applied to each request and
|
|
143
|
+
defaults to 5000ms. The options object may also include a frameId,
|
|
144
|
+
identifying an existing frame on which to install its listeners. If the
|
|
145
|
+
promise key is supplied the constructor for a Promise, that Promise library
|
|
146
|
+
will be used instead of the default window.Promise.
|
|
147
|
+
|
|
148
|
+
``` javascript
|
|
149
|
+
var storage = new CrossStorageClient('http://localhost:3000/hub.html');
|
|
150
|
+
|
|
151
|
+
var storage = new CrossStorageClient('http://localhost:3000/hub.html', {
|
|
152
|
+
timeout: 5000,
|
|
153
|
+
frameId: 'storageFrame'
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
#### CrossStorageClient.prototype.onConnect()
|
|
158
|
+
|
|
159
|
+
Returns a promise that is fulfilled when a connection has been established
|
|
160
|
+
with the cross storage hub. Its use is required to avoid sending any
|
|
161
|
+
requests prior to initialization being complete.
|
|
162
|
+
|
|
163
|
+
``` javascript
|
|
164
|
+
storage.onConnect().then(function() {
|
|
165
|
+
// ready!
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
#### CrossStorageClient.prototype.set(key, value)
|
|
170
|
+
|
|
171
|
+
Sets a key to the specified value. Returns a promise that is fulfilled on
|
|
172
|
+
success, or rejected if any errors setting the key occurred, or the request
|
|
173
|
+
timed out.
|
|
174
|
+
|
|
175
|
+
``` javascript
|
|
176
|
+
storage.onConnect().then(function() {
|
|
177
|
+
return storage.set('key', JSON.stringify({foo: 'bar'}));
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
#### CrossStorageClient.prototype.get(key1, [key2], [...])
|
|
182
|
+
|
|
183
|
+
Accepts one or more keys for which to retrieve their values. Returns a
|
|
184
|
+
promise that is settled on hub response or timeout. On success, it is
|
|
185
|
+
fulfilled with the value of the key if only passed a single argument.
|
|
186
|
+
Otherwise it's resolved with an array of values. On failure, it is rejected
|
|
187
|
+
with the corresponding error message.
|
|
188
|
+
|
|
189
|
+
``` javascript
|
|
190
|
+
storage.onConnect().then(function() {
|
|
191
|
+
return storage.get('key1');
|
|
192
|
+
}).then(function(res) {
|
|
193
|
+
return storage.get('key1', 'key2', 'key3');
|
|
194
|
+
}).then(function(res) {
|
|
195
|
+
// ...
|
|
196
|
+
});
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
#### CrossStorageClient.prototype.del(key1, [key2], [...])
|
|
200
|
+
|
|
201
|
+
Accepts one or more keys for deletion. Returns a promise that is settled on
|
|
202
|
+
hub response or timeout.
|
|
203
|
+
|
|
204
|
+
``` javascript
|
|
205
|
+
storage.onConnect().then(function() {
|
|
206
|
+
return storage.del('key1', 'key2');
|
|
207
|
+
});
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
#### CrossStorageClient.prototype.getKeys()
|
|
211
|
+
|
|
212
|
+
Returns a promise that, when resolved, passes an array of keys currently
|
|
213
|
+
in storage.
|
|
214
|
+
|
|
215
|
+
``` javascript
|
|
216
|
+
storage.onConnect().then(function() {
|
|
217
|
+
return storage.getKeys();
|
|
218
|
+
}).then(function(keys) {
|
|
219
|
+
// ['key1', 'key2', ...]
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
#### CrossStorageClient.prototype.clear()
|
|
224
|
+
|
|
225
|
+
Returns a promise that, when resolved, indicates that all localStorage
|
|
226
|
+
data has been cleared.
|
|
227
|
+
|
|
228
|
+
``` javascript
|
|
229
|
+
storage.onConnect().then(function() {
|
|
230
|
+
return storage.clear();
|
|
231
|
+
});
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
#### CrossStorageClient.prototype.close()
|
|
235
|
+
|
|
236
|
+
Deletes the iframe and sets the connected state to false. The client can
|
|
237
|
+
no longer be used after being invoked.
|
|
238
|
+
|
|
239
|
+
``` javascript
|
|
240
|
+
storage.onConnect().then(function() {
|
|
241
|
+
return storage.set('key1', 'key2');
|
|
242
|
+
}).catch(function(err) {
|
|
243
|
+
// Handle error
|
|
244
|
+
}).then(function() {
|
|
245
|
+
storage.close();
|
|
246
|
+
});
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
## Compatibility
|
|
250
|
+
|
|
251
|
+
For compatibility with older browsers, simply load a Promise polyfill such as
|
|
252
|
+
[es6-promise](https://github.com/jakearchibald/es6-promise).
|
|
253
|
+
|
|
254
|
+
``` html
|
|
255
|
+
<script src="https://s3.amazonaws.com/es6-promises/promise-1.0.0.min.js"></script>
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
You can also use RSVP or any other ES6 compliant promise library. Supports IE8
|
|
259
|
+
and up using the above polyfill. A JSON polyfill is also required
|
|
260
|
+
for IE8 in Compatibility View. Also note that `catch` is a reserved word in IE8,
|
|
261
|
+
and so error handling with promises can be done as:
|
|
262
|
+
|
|
263
|
+
``` javascript
|
|
264
|
+
storage.onConnect().then(function() {
|
|
265
|
+
return storage.get('key1');
|
|
266
|
+
}).then(function(res) {
|
|
267
|
+
// ... on success
|
|
268
|
+
})['catch'](function(err) {
|
|
269
|
+
// ... on error
|
|
270
|
+
});
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
**Breaking Changes**
|
|
274
|
+
|
|
275
|
+
API breaking changes were introduced in both 0.6 and 1.0. Refer to
|
|
276
|
+
[releases](https://github.com/zendesk/cross-storage/releases) for details.
|
|
277
|
+
|
|
278
|
+
**Notes on Safari 7+ (OSX, iOS)**
|
|
279
|
+
|
|
280
|
+
All cross-domain local storage access is disabled by default with Safari 7+.
|
|
281
|
+
This is a result of the "Block cookies and other website data" privacy setting
|
|
282
|
+
being set to "From third parties and advertisers". Any cross-storage client
|
|
283
|
+
code will not crash, however, it will only have access to a sandboxed, isolated
|
|
284
|
+
local storage instance. As such, none of the data previously set by other
|
|
285
|
+
origins will be accessible. If an option, one could fall back to using root
|
|
286
|
+
cookies for those user agents, or requesting the data from a server-side store.
|
|
287
|
+
|
|
288
|
+
## Compression
|
|
289
|
+
|
|
290
|
+
Most localStorage-compatible browsers offer at least ~5Mb of storage. But keys
|
|
291
|
+
and values are defined as DOMStrings, which are UTF-8 encoded using single
|
|
292
|
+
16-bit sequences. That means a string of ~2.5 million ASCII characters will use
|
|
293
|
+
up ~5Mb, since they're 2 bytes per char.
|
|
294
|
+
|
|
295
|
+
If you need to maximize your storage space, consider using
|
|
296
|
+
[lz-string](https://github.com/pieroxy/lz-string/). For smaller strings, it's
|
|
297
|
+
not uncommon to see a 50% reduction in size when compressed, which will bring
|
|
298
|
+
you a lot closer to 5 million characters. At that point, you're only limited by
|
|
299
|
+
the average compression rate of your strings.
|
|
300
|
+
|
|
301
|
+
## Building
|
|
302
|
+
|
|
303
|
+
The minified, production JavaScript can be generated with gulp by running
|
|
304
|
+
`gulp dist`. If not already on your system, gulp can be installed using
|
|
305
|
+
`npm install -g gulp`
|
|
306
|
+
|
|
307
|
+
## Tests
|
|
308
|
+
|
|
309
|
+
Tests can be ran locally using `npm test`. Tests are ran using Zuul, and
|
|
310
|
+
the Travis CI build uses Sauce Labs for multi-browser testing as well.
|
|
311
|
+
|
|
312
|
+
## Copyright and license
|
|
313
|
+
|
|
314
|
+
Copyright 2016 Zendesk
|
|
315
|
+
|
|
316
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
317
|
+
this file except in compliance with the License.
|
|
318
|
+
You may obtain a copy of the License at
|
|
319
|
+
|
|
320
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
321
|
+
|
|
322
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
323
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
324
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
325
|
+
specific language governing permissions and limitations under the License.
|