Skip to content

Commit 4553945

Browse files
committed
.silent.js version (extra small)
1 parent 9ad667c commit 4553945

6 files changed

Lines changed: 165 additions & 57 deletions

File tree

Gruntfile.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,52 @@ module.exports = function(grunt) {
88
banner: '/*! Cache API Key/Value Store v<%= pkg.version %>\n * (c) https://github.com/optimalisatie */\n'
99
},
1010

11+
// minify
12+
uglify: {
13+
14+
"cache-api-keyval": {
15+
options: {
16+
compress: {
17+
global_defs: {
18+
"SILENT": false
19+
}
20+
},
21+
mangle: {},
22+
dead_code: true,
23+
banner: ''
24+
},
25+
files: {
26+
'min/cache-api-keyval.js': [
27+
'src/cache-api-keyval.js'
28+
]
29+
}
30+
},
31+
32+
"cache-api-keyval-silent": {
33+
options: {
34+
compress: {
35+
global_defs: {
36+
"SILENT": true
37+
}
38+
},
39+
mangle: {},
40+
dead_code: true,
41+
banner: ''
42+
},
43+
files: {
44+
'min/cache-api-keyval.silent.js': [
45+
'src/cache-api-keyval.js'
46+
]
47+
}
48+
}
49+
50+
},
51+
1152
// closure compiler
1253
"closure-compiler": {
1354
"cache-api-keyval": {
1455
closurePath: 'closure-compiler',
15-
js: 'src/cache-api-keyval.js',
56+
js: 'min/cache-api-keyval.js',
1657
jsOutputFile: 'dist/cache-api-keyval.js',
1758
maxBuffer: 10000,
1859
options: {
@@ -21,6 +62,19 @@ module.exports = function(grunt) {
2162
externs: ['cache-api-keyval.ext.js'],
2263
define: ['DEBUG=false']
2364
}
65+
},
66+
67+
"cache-api-keyval-silent": {
68+
closurePath: 'closure-compiler',
69+
js: 'min/cache-api-keyval.silent.js',
70+
jsOutputFile: 'dist/cache-api-keyval.silent.js',
71+
maxBuffer: 10000,
72+
options: {
73+
compilation_level: 'ADVANCED_OPTIMIZATIONS',
74+
language_in: 'ECMASCRIPT5_STRICT',
75+
externs: ['cache-api-keyval.ext.js'],
76+
define: ['DEBUG=false']
77+
}
2478
}
2579
}
2680
});
@@ -29,7 +83,8 @@ module.exports = function(grunt) {
2983
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
3084

3185
grunt.registerTask('build', [
32-
'closure-compiler:cache-api-keyval'
86+
'uglify',
87+
'closure-compiler'
3388
]);
3489
grunt.registerTask('default', ['']);
3590
};

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,22 @@ window.CacheApiDBFallback = function(store, options) {
6767
this.del = function(key) { /* delete key from store */ }
6868
this.prune = function() { /* cleanup database */ }
6969
};
70-
```
70+
```
71+
72+
## Tinier
73+
74+
`cache-api-keyval.silent.js` is a stripped version without error reporting and fallback mechanism. It can be used with manual Cache API verification.
75+
76+
77+
```html
78+
<script src="/cache-api-keyval.silent.js"></script>
79+
<script>
80+
81+
// check if Cache API is available
82+
if ("caches" in window) {
83+
84+
// load database
85+
var db = new CacheApiDB('my-store', { namespace: 'optional' });
86+
87+
// ...
88+
}

dist/cache-api-keyval.js

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

dist/cache-api-keyval.silent.js

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

package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
{
22
"name": "cache-api-keyval",
3-
"version": "1.0.9",
3+
"version": "1.0.10",
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",
77
"email": "info@optimization.team",
88
"web": "optimization.team"
99
},
10-
"keywords": ["Cache", "IndexedDB", "Cache API", "CacheStorage", "keyvalue", "keyval", "key/value", "localstorage"],
10+
"keywords": [
11+
"Cache",
12+
"IndexedDB",
13+
"Cache API",
14+
"CacheStorage",
15+
"keyvalue",
16+
"keyval",
17+
"key/value",
18+
"localstorage"
19+
],
1120
"homepage": "https://github.com/optimalisatie/",
1221
"copyright": "Copyright (C) 2018 Optimization.Team, Utrecht, NLD",
1322
"main": "cache-api-keyval.js",
@@ -22,6 +31,7 @@
2231
"devDependencies": {
2332
"grunt": "latest",
2433
"grunt-closure-compiler": "latest",
34+
"grunt-contrib-uglify": "latest",
2535
"matchdep": "latest",
2636
"merge": "latest"
2737
}

src/cache-api-keyval.js

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,75 +9,90 @@
99

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

12-
function print_error(msg) {
13-
if (console) {
14-
console.error(msg);
12+
// filtered out by Google Closure Compiler to create .silent.js version
13+
if (!SILENT) {
14+
function print_error(msg) {
15+
if (console) {
16+
console.error(msg);
17+
}
1518
}
16-
}
1719

18-
var queue = [];
19-
_root.onCacheApiDB = function(callback) {
20-
if (_root.CacheApiDB) {
21-
callback();
22-
} else {
23-
queue.push(callback);
20+
21+
var queue = [];
22+
_root.onCacheApiDB = function(callback) {
23+
if (_root.CacheApiDB) {
24+
callback();
25+
} else {
26+
queue.push(callback);
27+
}
2428
}
25-
}
2629

27-
// set cache api db controller
28-
function setCacheApiDB(fallbackFactory) {
30+
// set cache api db controller
31+
function setCacheApiDB(fallbackFactory) {
2932

30-
// set fallback
31-
if (fallbackFactory) {
32-
_root.CacheApiDB = fallbackFactory;
33-
}
33+
// set fallback
34+
if (fallbackFactory) {
35+
_root.CacheApiDB = fallbackFactory;
36+
}
3437

35-
// process callback queue
36-
var callback = queue.shift();
37-
while (callback) {
38-
callback();
38+
// process callback queue
39+
var callback = queue.shift();
40+
while (callback) {
41+
callback();
42+
}
3943
}
40-
}
4144

42-
var nosupport_error = 'No Cache API';
45+
var nosupport_error = 'No Cache API';
4346

44-
if (!_root.CacheApiDBFallback) {
45-
_root.CacheApiDBFallback = function() {
46-
var that = this;
47-
that.no = 1;
48-
['get', 'set', 'del', 'prune'].forEach(function(method) {
49-
that[method] = function() {
50-
return Promise.reject(nosupport_error);
51-
}
52-
});
47+
if (!_root.CacheApiDBFallback) {
48+
_root.CacheApiDBFallback = function() {
49+
var that = this;
50+
that.no = 1;
51+
['get', 'set', 'del', 'prune'].forEach(function(method) {
52+
that[method] = function() {
53+
return Promise.reject(nosupport_error);
54+
}
55+
});
56+
}
5357
}
5458
}
5559

5660
// detect Cache API support
5761
if (!("caches" in _root) || !(caches instanceof CacheStorage)) {
58-
print_error(nosupport_error);
59-
_root.CacheApiDB = _root.CacheApiDBFallback;
62+
63+
// filtered out by Google Closure Compiler to create .silent.js version
64+
if (SILENT) {
65+
return;
66+
} else {
67+
print_error(nosupport_error);
68+
_root.CacheApiDB = _root.CacheApiDBFallback;
69+
}
6070
} else {
6171

6272
// enable instant usage of Cache API store
6373
_root.CacheApiDB = factory();
6474

65-
// test if Cache API is blocked by browser privacy settings
66-
caches.open('x').catch(function(e) {
75+
// filtered out by Google Closure Compiler to create .silent.js version
76+
if (!SILENT) {
6777

68-
// report errors unrelated to privacy settings
69-
if (e.name != 'SecurityError') {
70-
print_error('Cache API: ' + e.message);
71-
}
78+
// test if Cache API is blocked by browser privacy settings
79+
caches.open('x').catch(function(e) {
7280

73-
// fallback
74-
setCacheApiDB(_root.CacheApiDBFallback);
81+
// report errors unrelated to privacy settings
82+
if (e.name != 'SecurityError') {
83+
print_error('Cache API: ' + e.message);
84+
}
7585

76-
}).then(function() {
86+
// fallback
87+
setCacheApiDB(_root.CacheApiDBFallback);
7788

78-
// continue with Cache API
79-
setCacheApiDB();
80-
})
89+
}).then(function() {
90+
91+
// continue with Cache API
92+
setCacheApiDB();
93+
});
94+
95+
}
8196

8297
}
8398
})(function() {
@@ -270,7 +285,13 @@
270285

271286
// output error
272287
function ERROR(msg) {
273-
throw new Error(msg || 'input');
288+
289+
// filtered out by Google Closure Compiler to create .silent.js version
290+
if (SILENT) {
291+
throw Error();
292+
} else {
293+
throw Error(msg || 'input');
294+
}
274295
}
275296

276297
// public get method

0 commit comments

Comments
 (0)