Skip to content

Commit c9fdbbe

Browse files
committed
Fixes #21: Remove TTL, and store raw values rather than JSON
1 parent 31c4dc9 commit c9fdbbe

File tree

6 files changed

+27
-94
lines changed

6 files changed

+27
-94
lines changed

README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Features an API using ES6 promises.
1212
* [CrossStorageHub.init(permissions)](#crossstoragehubinitpermissions)
1313
* [new CrossStorageClient(url, \[opts\])](#new-crossstorageclienturl-opts)
1414
* [CrossStorageClient.prototype.onConnect()](#crossstorageclientprototypeonconnect)
15-
* [CrossStorageClient.prototype.set(key, value, \[ttl\])](#crossstorageclientprototypesetkey-value-ttl)
15+
* [CrossStorageClient.prototype.set(key, value)](#crossstorageclientprototypesetkey-value)
1616
* [CrossStorageClient.prototype.get(key1, \[key2\], \[...\])](#crossstorageclientprototypegetkey1-key2-)
1717
* [CrossStorageClient.prototype.del(key1, \[key2\], \[...\])](#crossstorageclientprototypedelkey1-key2-)
1818
* [CrossStorageClient.prototype.getKeys()](#crossstorageclientprototypegetkeys)
@@ -68,8 +68,7 @@ invalid.example.com.malicious.com.
6868
var storage = new CrossStorageClient('https://store.example.com/hub.html');
6969

7070
storage.onConnect().then(function() {
71-
// Set a key with a TTL of 90 seconds
72-
return storage.set('newKey', 'foobar', 90000);
71+
return storage.set('newKey', 'foobar');
7372
}).then(function() {
7473
return storage.get('existingKey', 'newKey');
7574
}).then(function(res) {
@@ -167,18 +166,15 @@ storage.onConnect().then(function() {
167166
});
168167
```
169168

170-
#### CrossStorageClient.prototype.set(key, value, [ttl])
169+
#### CrossStorageClient.prototype.set(key, value)
171170

172-
Sets a key to the specified value, optionally accepting a ttl to passively
173-
expire the key after a number of milliseconds. Returns a promise that is
174-
fulfilled on success, or rejected if any errors setting the key occurred,
175-
or the request timed out.
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.
176174

177175
``` javascript
178176
storage.onConnect().then(function() {
179-
return storage.set('key', {foo: 'bar'});
180-
}).then(function() {
181-
return storage.set('expiringKey', 'foobar', 10000);
177+
return storage.set('key', JSON.stringify({foo: 'bar'}));
182178
});
183179
```
184180

@@ -295,8 +291,7 @@ If you need to maximize your storage space, consider using
295291
[lz-string](https://github.com/pieroxy/lz-string/). For smaller strings, it's
296292
not uncommon to see a 50% reduction in size when compressed, which will bring
297293
you a lot closer to 5 million characters. At that point, you're only limited by
298-
the average compression rate of your strings, as well as the minor overhead
299-
produced by the serialization format used by the library: JSON.
294+
the average compression rate of your strings.
300295

301296
## Building
302297

@@ -311,7 +306,7 @@ the Travis CI build uses Sauce Labs for multi-browser testing as well.
311306

312307
## Copyright and license
313308

314-
Copyright 2014 Zendesk
309+
Copyright 2016 Zendesk
315310

316311
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
317312
this file except in compliance with the License.

lib/client.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,18 @@
162162
};
163163

164164
/**
165-
* Sets a key to the specified value, optionally accepting a ttl to passively
166-
* expire the key after a number of milliseconds. Returns a promise that is
167-
* fulfilled on success, or rejected if any errors setting the key occurred,
168-
* or the request timed out.
165+
* Sets a key to the specified value. Returns a promise that is fulfilled on
166+
* success, or rejected if any errors setting the key occurred, or the request
167+
* timed out.
169168
*
170169
* @param {string} key The key to set
171170
* @param {*} value The value to assign
172-
* @param {int} ttl Time to live in milliseconds
173171
* @returns {Promise} A promise that is settled on hub response or timeout
174172
*/
175-
CrossStorageClient.prototype.set = function(key, value, ttl) {
173+
CrossStorageClient.prototype.set = function(key, value) {
176174
return this._request('set', {
177175
key: key,
178-
value: value,
179-
ttl: ttl
176+
value: value
180177
});
181178
};
182179

lib/hub.js

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -150,59 +150,36 @@
150150
};
151151

152152
/**
153-
* Sets a key to the specified value. If a ttl is provided, an expiration
154-
* timestamp is added to the object to be stored, prior to serialization.
153+
* Sets a key to the specified value.
155154
*
156-
* @param {object} params An object with key, value and optional ttl
155+
* @param {object} params An object with key and value
157156
*/
158157
CrossStorageHub._set = function(params) {
159-
var ttl, item;
160-
161-
ttl = params.ttl;
162-
if (ttl && parseInt(ttl, 10) !== ttl) {
163-
throw new Error('ttl must be a number');
164-
}
165-
166-
item = {value: params.value};
167-
if (ttl) {
168-
item.expire = CrossStorageHub._now() + ttl;
169-
}
170-
171-
window.localStorage.setItem(params.key, JSON.stringify(item));
158+
window.localStorage.setItem(params.key, params.value);
172159
};
173160

174161
/**
175162
* Accepts an object with an array of keys for which to retrieve their values.
176163
* Returns a single value if only one key was supplied, otherwise it returns
177-
* an array. Any keys not set, or expired, result in a null element in the
178-
* resulting array.
164+
* an array. Any keys not set result in a null element in the resulting array.
179165
*
180166
* @param {object} params An object with an array of keys
181167
* @returns {*|*[]} Either a single value, or an array
182168
*/
183169
CrossStorageHub._get = function(params) {
184-
var storage, result, i, item, key;
170+
var storage, result, i, value;
185171

186172
storage = window.localStorage;
187173
result = [];
188174

189175
for (i = 0; i < params.keys.length; i++) {
190-
key = params.keys[i];
191-
192176
try {
193-
item = JSON.parse(storage.getItem(key));
177+
value = storage.getItem(params.keys[i]);
194178
} catch (e) {
195-
item = null;
179+
value = null;
196180
}
197181

198-
if (item === null) {
199-
result.push(null);
200-
} else if (item.expire && item.expire < CrossStorageHub._now()) {
201-
storage.removeItem(key);
202-
result.push(null);
203-
} else {
204-
result.push(item.value);
205-
}
182+
result.push(value);
206183
}
207184

208185
return (result.length > 1) ? result : result[0];

test/getOnlyHub.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
{origin: /.*/, allow: ['get']}
1010
]);
1111

12-
window.localStorage.setItem('key1', JSON.stringify({value: 'original'}));
12+
window.localStorage.setItem('key1', 'original');
1313
</script>
1414
</html>

test/invalidOriginHub.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
{origin: /^someOtherOrigin$/, allow: ['get', 'set', 'del']}
1010
]);
1111

12-
window.localStorage.setItem('key1', JSON.stringify({value: 'original'}));
12+
window.localStorage.setItem('key1', 'original');
1313
</script>
1414
</html>

test/test.js

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ describe('CrossStorageClient', function() {
4545
}
4646
});
4747

48-
var setGet = function(key, value, ttl) {
48+
var setGet = function(key, value) {
4949
return function() {
50-
return storage.set(key, value, ttl).then(function() {
50+
return storage.set(key, value).then(function() {
5151
return storage.get(key);
5252
});
5353
};
@@ -205,7 +205,6 @@ describe('CrossStorageClient', function() {
205205

206206
storage.onConnect().then(function() {
207207
storage.close();
208-
209208
return storage.set('key1', 'new');
210209
})['catch'](function(err) {
211210
expect(err.message).to.be('CrossStorageClient has closed');
@@ -219,8 +218,7 @@ describe('CrossStorageClient', function() {
219218
});
220219

221220
it('returns null when calling get on a non-existent key', function(done) {
222-
storage.onConnect()
223-
.then(function() {
221+
storage.onConnect().then(function() {
224222
return storage.get('key1');
225223
}).then(function(res) {
226224
expect(res).to.be(null);
@@ -240,18 +238,6 @@ describe('CrossStorageClient', function() {
240238
})['catch'](done);
241239
});
242240

243-
it('can set objects as the value', function(done) {
244-
var key = 'key1';
245-
var object = {foo: 'bar'};
246-
247-
storage.onConnect()
248-
.then(setGet(key, object))
249-
.then(function(res) {
250-
expect(res).to.eql(object);
251-
done();
252-
})['catch'](done);
253-
});
254-
255241
it('can overwrite existing values', function(done) {
256242
var key = 'key1';
257243
var value = 'new';
@@ -266,28 +252,6 @@ describe('CrossStorageClient', function() {
266252
})['catch'](done);
267253
});
268254

269-
it('can set a ttl on the key', function(done) {
270-
var key = 'key1';
271-
var value = 'foobar';
272-
273-
var delay = function() {
274-
// Delay by 100ms
275-
return new Promise(function(resolve, reject) {
276-
setTimeout(resolve, 100);
277-
});
278-
};
279-
280-
storage.onConnect()
281-
.then(setGet(key, value, 50))
282-
.then(delay)
283-
.then(function() {
284-
return storage.get(key);
285-
}).then(function(res) {
286-
expect(res).to.be(null);
287-
done();
288-
})['catch'](done);
289-
});
290-
291255
it('returns an array of values if get is passed multiple keys', function(done) {
292256
var keys = ['key1', 'key2'];
293257
var values = ['foo', 'bar'];

0 commit comments

Comments
 (0)