Skip to content

Commit 8630fb9

Browse files
committed
added test for privacy Cache API block
1 parent b9e03c0 commit 8630fb9

File tree

6 files changed

+103
-47
lines changed

6 files changed

+103
-47
lines changed

LICENSE.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "cache-api-keyval-store",
3+
"version": "1.0.0",
4+
"description": "Cache API key/value store with expiration and JSON object storage.",
5+
"author": {
6+
"name": "info@optimization.team",
7+
"email": "info@optimization.team",
8+
"web": "optimization.team"
9+
},
10+
"engines": {
11+
"node": "~0.10"
12+
},
13+
"dependencies": {
14+
15+
},
16+
"devDependencies": {
17+
"grunt": "latest",
18+
"grunt-closure-compiler": "latest",
19+
"matchdep": "latest",
20+
"merge": "latest"
21+
}
22+
}

README.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Cache API Key/Value Store
44

5-
Fast and tiny key/value store with +50MB storage capacity in [most browsers](https://developer.mozilla.org/en-US/docs/Web/API/Cache#Browser_compatibility), expiration and JSON object data-type.
5+
Fast and tiny key/value store with +50MB storage capacity in [most browsers](https://developer.mozilla.org/en-US/docs/Web/API/Cache#Browser_compatibility), expiration and JSON object data-type support.
66

77
Cache API is currently available in Chrome >= 40, Firefox >=39 and Opera >= 27.
88

@@ -20,29 +20,33 @@ npm install --save cache-api-keyval
2020
<script src="/cache-api-keyval.js"></script>
2121
<script>
2222
23-
// load database
24-
var db = new CacheApiDB('my-store', { namespace: 'optional' });
23+
// wait for Cache API accessibility test to complete
24+
onCacheApiDB(function() {
2525
26-
if (db.supported) { // Cache API supported by browser
26+
// load database
27+
var db = new CacheApiDB('my-store', { namespace: 'optional' });
2728
28-
// set JSON object data
29-
db.set('key', { json: 'object' });
29+
if (db.supported) { // Cache API supported by browser
3030
31-
// set text data with expiration in 24 hours
32-
db.set('key2', 'string', 86400);
31+
// set JSON object data
32+
db.set('key', { json: 'object' });
3333
34-
// get data from cache
35-
db.get('key').then(function(json) {
36-
console.log('json object', json);
37-
});
34+
// set text data with expiration in 24 hours
35+
db.set('key2', 'string', 86400);
3836
39-
// delete key from database
40-
db.del('key2');
37+
// get data from cache
38+
db.get('key').then(function(json) {
39+
console.log('json object', json);
40+
});
4141
42-
// prune expired cache entries
43-
db.prune();
42+
// delete key from database
43+
db.del('key2');
4444
45-
}
45+
// prune expired cache entries
46+
db.prune();
47+
}
48+
49+
});
4650
</script>
4751
```
4852

cache-api-keyval.ext.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Closure Compiler externs
22
// @link https://developers.google.com/closure/compiler/
33

4-
window.CacheApiDB = global.CacheApiDB = this.CacheApiDB = self.CacheApiDB = function(store, options) {
4+
var global;
5+
window.CacheApiDB = global.CacheApiDB = this.CacheApiDB = self.CacheApiDB = window.onCacheApiDB = global.onCacheApiDB = this.onCacheApiDB = self.onCacheApiDB = function(store, options) {
56

67
options.namespace = '';
78

dist/cache-api-keyval.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cache-api-keyval",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Browser Cache API key/value store with +50MB capacity, expiration and JSON object data-type storage.",
55
"author": {
66
"name": "info@optimization.team",

src/cache-api-keyval.js

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Cache API key/value store - github.com/optimalisatie
2+
* Cache API key/value store - github.com/optimalisatie/Cache-API-Key-Value-Store
33
* Released under the terms of MIT license
44
*
55
* Copyright (C) 2018 github.com/optimalisatie
@@ -9,30 +9,70 @@
99

1010
var _root = ("undefined" != typeof window ? window : "undefined" != typeof global ? global : "undefined" != typeof self ? self : this);
1111

12-
// detect Cache API support
13-
if (!(caches instanceof CacheStorage)) {
14-
var error = 'Browser does not support Cache API';
12+
function print_error(msg) {
1513
if (console) {
16-
console.error(error);
14+
console.error(msg);
1715
}
18-
_root.CacheApiDB = (_root.CacheApiDBFallback) ? _root.CacheApiDBFallback() : function() {
16+
}
17+
18+
var queue = [];
19+
_root.onCacheApiDB = function(callback) {
20+
if (_root.CacheApiDB) {
21+
callback();
22+
} else {
23+
queue.push(callback);
24+
}
25+
}
26+
27+
// set cache api db controller
28+
function setCacheApiDB(factory) {
29+
30+
_root.CacheApiDB = factory;
31+
32+
// process callback queue
33+
var callback = queue.shift();
34+
while (callback) {
35+
callback();
36+
}
37+
}
38+
39+
if (!_root.CacheApiDBFallback) {
40+
_root.CacheApiDBFallback = function() {
1941
var that = this;
2042
that.supported = 0;
2143
['get', 'set', 'del', 'prune'].forEach(function(method) {
2244
that[method] = function() {
2345
return Promise.reject(error);
2446
}
2547
});
26-
};
48+
}
49+
}
50+
51+
// detect Cache API support
52+
if (!(caches instanceof CacheStorage)) {
53+
print_error('Browser does not support Cache API');
54+
_root.CacheApiDB = _root.CacheApiDBFallback;
2755
} else {
28-
_root.CacheApiDB = factory();
56+
57+
// test cache support
58+
// Cache API could be blocked by browser privacy settings
59+
caches.open('x').catch(function(e) {
60+
if (e.name == 'SecurityError') {
61+
print_error('Cache API is blocked by browser. Please check privacy settings (cookies, no-track option etc).');
62+
} else {
63+
print_error('Cache API error: ' + e.message);
64+
}
65+
setCacheApiDB(_root.CacheApiDBFallback);
66+
}).then(function() {
67+
setCacheApiDB(factory());
68+
})
69+
2970
}
3071
})(function() {
3172

3273
var DATE_HEADER = 'x-date';
3374
var EXPIRE_HEADER = 'x-expire';
3475
var CONTENT_TYPE_HEADER = 'Content-Type';
35-
var CONTENT_TYPE_JSON = 'application/json';
3676

3777
// return timestamp
3878
function NOW() {
@@ -62,10 +102,7 @@
62102
return false;
63103
}
64104

65-
// detect content type
66-
var json = cachedata.headers.get(CONTENT_TYPE_HEADER) === CONTENT_TYPE_JSON;
67-
68-
return (json) ? cachedata.json() : cachedata.text();
105+
return cachedata.json();
69106
});
70107

71108
});
@@ -82,17 +119,9 @@
82119
// cache date
83120
cache_headers[DATE_HEADER] = NOW();
84121

85-
// JSON content type
86-
if (IS_OBJECT(data)) {
87-
88-
// JSON
89-
cache_headers[CONTENT_TYPE_HEADER] = CONTENT_TYPE_JSON;
90-
data = JSON.stringify(data);
91-
} else {
92-
// string
93-
data = STRING(data);
94-
cache_headers[CONTENT_TYPE_HEADER] = 'text/plain'
95-
}
122+
// JSON
123+
cache_headers[CONTENT_TYPE_HEADER] = 'application/json';
124+
data = JSON.stringify(data);
96125

97126
// expire time
98127
if (expire) {

0 commit comments

Comments
 (0)