You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

appcache-node

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

appcache-node - npm Package Compare versions

Comparing version

to
0.2.0

epic.png

25

appcache.js

@@ -1,16 +0,11 @@

module.exports = function(options, app){
options = options || {};
var lines = options.files || [],
path = options.path || 'app.cache';
lines.unshift('CACHE MANIFEST', '', 'CACHE:');
lines.push('', '# ' + new Date());
app.cachefile = lines.join('\r\n');
app.all('/'+path, function(r, s){
s.writeHead(200, {
'Content-Type': 'text/cache-manifest'
});
s.end(app.cachefile);
});
module.exports = {
newCache: function(files){
files.unshift('CACHE MANIFEST', '', 'CACHE:');
files.push('', '# ' + new Date());
return files.join('\r\n');
},
toDataURI: function(fn){
return 'data:image/' + fn.match(/..\.(.*)$/)[1] + ';base64,' +
require('fs').readFileSync('./epic.png').toString('base64');
}
}

2

package.json

@@ -12,3 +12,3 @@ {

},
"version" : "0.1.0"
"version" : "0.2.0"
}

@@ -5,4 +5,11 @@ appcache-node

###Let their browsers do the work
####In any HTML page you wish to cache..
This module auto-generates your HTML5 Application Cache manifest, making it very easy to implement. In Node.JS, but for any web server. This makes for massive decrease on your server load.
###Installation
````
$ npm install appcache-node
````
Any HTML page with this will cache
````

@@ -12,51 +19,54 @@ <html manifest="app.cache">

####In the NodeJS/Express app.js..
In your app
````
require('appcache-node')({files: []}, app)
````
var c = require('appcache-node');
Server generates an app.cache file, so HTML5 Application Cache will force browsers to load their local copy.
This makes for massive decrease on your server load.
// generate a cache file
var cf = c.newCache();
Any html file served from your site declared with <html manifest="app.cache"> will be cached.
You can see the entire app cache in Chrome by going to chrome://appcache-internals
// in your request handler
if(r.url.match(/app\.cache$/)){
s.writeHead(200, {'Content-Type': 'text/cache-manifest'});
return s.end(cf);
}
````
You choose whatever JS and CSS your page uses, to cache as well. Use the files array..
While your HTML page caches automatically, you specify any JS, CSS, IMG, etc.. your page uses, so you have full control over which files are included in the cache.
````
require('appcache-node')({files: [
var c = require('appcache-node')([
'http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css'
, 'http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js'
]}, app)
]);
````
When you restart your app, the app.cache is rebuilt and so cache cleared.
When you restart your app, the app.cache is rebuilt and so cache is cleared.
or here's a trick to have the cache reset every hour, if you want browsers to have to reload periodically.
````
function docache(){
require('./appcache.js')({files: [
// no need to include html files that start with <html manifest="app.cache">
'http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css'
, 'http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js'
]}, app);
}
// generate a cache file
var cf = c.newCache([
'http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css'
, 'http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js'
]);
docache();
setInterval(docache, 1000*60*60);
// optional - invalidate and reload the cache every 1 hour
setInterval(function(){
cf = c.newCache([
'http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css'
, 'http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js'
])
}, 1000*60*60);
````
####Options
Use an alternate name for the cache file.. http://localhost:3000/myname.cache
###toDataURI(filename)
A helper function is included, to convert images to data-uris, thereby embedding them into the HTML page, although this is not required to use the appcache.
````
require('appcache-node')({path: 'myname.cache'}, app);
var c = require('./appcache.js');
var duri = '<img src="' + c.toDataURI('./epic.png') + '"/>';
==> "<img src='data:image/png;base64,iVBORw0KGgo...'/>"
````
####Tests
A test web server and example is included in this directory.
````
$ node tests-server
````
Go to http://localhost:3000
http://localhost:3000/app.cache to see raw cache file.
###Test
Run test.js to run an example web server.
More about the HTML5 Application Cache:
http://www.w3schools.com/html/html5_app_cache.asp
http://www.w3schools.com/html/html5_app_cache.asp