Skip to content

Commit

Permalink
.silent.js version (extra small)
Browse files Browse the repository at this point in the history
  • Loading branch information
optimalisatie committed Nov 6, 2018
1 parent 9ad667c commit 4553945
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 57 deletions.
59 changes: 57 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,52 @@ module.exports = function(grunt) {
banner: '/*! Cache API Key/Value Store v<%= pkg.version %>\n * (c) https://github.com/optimalisatie */\n'
},

// minify
uglify: {

"cache-api-keyval": {
options: {
compress: {
global_defs: {
"SILENT": false
}
},
mangle: {},
dead_code: true,
banner: ''
},
files: {
'min/cache-api-keyval.js': [
'src/cache-api-keyval.js'
]
}
},

"cache-api-keyval-silent": {
options: {
compress: {
global_defs: {
"SILENT": true
}
},
mangle: {},
dead_code: true,
banner: ''
},
files: {
'min/cache-api-keyval.silent.js': [
'src/cache-api-keyval.js'
]
}
}

},

// closure compiler
"closure-compiler": {
"cache-api-keyval": {
closurePath: 'closure-compiler',
js: 'src/cache-api-keyval.js',
js: 'min/cache-api-keyval.js',
jsOutputFile: 'dist/cache-api-keyval.js',
maxBuffer: 10000,
options: {
Expand All @@ -21,6 +62,19 @@ module.exports = function(grunt) {
externs: ['cache-api-keyval.ext.js'],
define: ['DEBUG=false']
}
},

"cache-api-keyval-silent": {
closurePath: 'closure-compiler',
js: 'min/cache-api-keyval.silent.js',
jsOutputFile: 'dist/cache-api-keyval.silent.js',
maxBuffer: 10000,
options: {
compilation_level: 'ADVANCED_OPTIMIZATIONS',
language_in: 'ECMASCRIPT5_STRICT',
externs: ['cache-api-keyval.ext.js'],
define: ['DEBUG=false']
}
}
}
});
Expand All @@ -29,7 +83,8 @@ module.exports = function(grunt) {
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

grunt.registerTask('build', [
'closure-compiler:cache-api-keyval'
'uglify',
'closure-compiler'
]);
grunt.registerTask('default', ['']);
};
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,22 @@ window.CacheApiDBFallback = function(store, options) {
this.del = function(key) { /* delete key from store */ }
this.prune = function() { /* cleanup database */ }
};
```
```

## Tinier

`cache-api-keyval.silent.js` is a stripped version without error reporting and fallback mechanism. It can be used with manual Cache API verification.


```html
<script src="/cache-api-keyval.silent.js"></script>
<script>
// check if Cache API is available
if ("caches" in window) {
// load database
var db = new CacheApiDB('my-store', { namespace: 'optional' });
// ...
}
10 changes: 5 additions & 5 deletions dist/cache-api-keyval.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions dist/cache-api-keyval.silent.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
{
"name": "cache-api-keyval",
"version": "1.0.9",
"version": "1.0.10",
"description": "Browser Cache API key/value store with +50MB capacity, expiration and JSON object data-type storage.",
"author": {
"name": "[email protected]",
"email": "[email protected]",
"web": "optimization.team"
},
"keywords": ["Cache", "IndexedDB", "Cache API", "CacheStorage", "keyvalue", "keyval", "key/value", "localstorage"],
"keywords": [
"Cache",
"IndexedDB",
"Cache API",
"CacheStorage",
"keyvalue",
"keyval",
"key/value",
"localstorage"
],
"homepage": "https://github.com/optimalisatie/",
"copyright": "Copyright (C) 2018 Optimization.Team, Utrecht, NLD",
"main": "cache-api-keyval.js",
Expand All @@ -22,6 +31,7 @@
"devDependencies": {
"grunt": "latest",
"grunt-closure-compiler": "latest",
"grunt-contrib-uglify": "latest",
"matchdep": "latest",
"merge": "latest"
}
Expand Down
115 changes: 68 additions & 47 deletions src/cache-api-keyval.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,75 +9,90 @@

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

function print_error(msg) {
if (console) {
console.error(msg);
// filtered out by Google Closure Compiler to create .silent.js version
if (!SILENT) {
function print_error(msg) {
if (console) {
console.error(msg);
}
}
}

var queue = [];
_root.onCacheApiDB = function(callback) {
if (_root.CacheApiDB) {
callback();
} else {
queue.push(callback);

var queue = [];
_root.onCacheApiDB = function(callback) {
if (_root.CacheApiDB) {
callback();
} else {
queue.push(callback);
}
}
}

// set cache api db controller
function setCacheApiDB(fallbackFactory) {
// set cache api db controller
function setCacheApiDB(fallbackFactory) {

// set fallback
if (fallbackFactory) {
_root.CacheApiDB = fallbackFactory;
}
// set fallback
if (fallbackFactory) {
_root.CacheApiDB = fallbackFactory;
}

// process callback queue
var callback = queue.shift();
while (callback) {
callback();
// process callback queue
var callback = queue.shift();
while (callback) {
callback();
}
}
}

var nosupport_error = 'No Cache API';
var nosupport_error = 'No Cache API';

if (!_root.CacheApiDBFallback) {
_root.CacheApiDBFallback = function() {
var that = this;
that.no = 1;
['get', 'set', 'del', 'prune'].forEach(function(method) {
that[method] = function() {
return Promise.reject(nosupport_error);
}
});
if (!_root.CacheApiDBFallback) {
_root.CacheApiDBFallback = function() {
var that = this;
that.no = 1;
['get', 'set', 'del', 'prune'].forEach(function(method) {
that[method] = function() {
return Promise.reject(nosupport_error);
}
});
}
}
}

// detect Cache API support
if (!("caches" in _root) || !(caches instanceof CacheStorage)) {
print_error(nosupport_error);
_root.CacheApiDB = _root.CacheApiDBFallback;

// filtered out by Google Closure Compiler to create .silent.js version
if (SILENT) {
return;
} else {
print_error(nosupport_error);
_root.CacheApiDB = _root.CacheApiDBFallback;
}
} else {

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

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

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

// fallback
setCacheApiDB(_root.CacheApiDBFallback);
// report errors unrelated to privacy settings
if (e.name != 'SecurityError') {
print_error('Cache API: ' + e.message);
}

}).then(function() {
// fallback
setCacheApiDB(_root.CacheApiDBFallback);

// continue with Cache API
setCacheApiDB();
})
}).then(function() {

// continue with Cache API
setCacheApiDB();
});

}

}
})(function() {
Expand Down Expand Up @@ -270,7 +285,13 @@

// output error
function ERROR(msg) {
throw new Error(msg || 'input');

// filtered out by Google Closure Compiler to create .silent.js version
if (SILENT) {
throw Error();
} else {
throw Error(msg || 'input');
}
}

// public get method
Expand Down

0 comments on commit 4553945

Please sign in to comment.