added suport of app cache

This commit is contained in:
coderaiser 2012-09-13 05:26:43 -04:00
parent a1d0f5ff04
commit 78fea25494
4 changed files with 48 additions and 30 deletions

View file

@ -40,6 +40,8 @@ running
* Added module is-file-changed.
* Added suport of AppCache.
2012.08.24, Version 0.1.6

View file

@ -1,5 +1,5 @@
<!DOCTYPE html>
<html> <!-- manifest=cloudcmd.appcache -->
<html manifest=cloudcmd.appcache>
<head>
<meta charset="utf-8">
<!-- mobile first design -->

View file

@ -1,10 +1,10 @@
var fs = require('fs');
var crypto = require('crypto');
/* varible contain all watched file names
* {name: true}
*/
var FileNames = {};
var FileNames = {},
NamesList_s = '';
/* function thet use for crossplatform
* access to fs.watch or fs.watchFile function
@ -16,6 +16,30 @@ var Manifest = '';
setWatachFunctions();
exports.createManifest = function(){
var lAllNames = cloudRequire('hashes.json');
if(lAllNames)
for(var lName in lAllNames)
exports.watch(lName);
processManifest();
};
exports.watch = function(pFileName){
console.log(pFileName + ' is watched');
if(!FileNames[pFileName]){
fs_watch(pFileName, on_fs_watch(pFileName));
NamesList_s += pFileName + '\n';
FileNames[pFileName] = true;
}
else if(firstFileRead_b){
processManifest();
firstFileRead_b = false;
}
};
function setWatachFunctions(){
if(process.platform === 'win32'){
/* good on windows */
@ -30,25 +54,6 @@ function setWatachFunctions(){
}
exports.watch = function(pFileName){
console.log(pFileName + ' is watched');
if(!FileNames[pFileName]){
try{
fs_watch(pFileName, on_fs_watch(pFileName));
}
catch(pError){
console.log(pError);
}
Manifest += pFileName + '\n';
FileNames[pFileName] = true;
}
else if(firstFileRead_b){
processManifest();
firstFileRead_b = false;
}
};
function onWatch (pFileName){
return function(pEvent, pFileName){
console.log('file ' + pFileName + 'is changed');
@ -58,7 +63,7 @@ function onWatch (pFileName){
function onWatchFile(pFileName){
return function(pCurr, pPrev){
console.log(pCurr);
if(pCurr.mtime !== pCurr.pPrev.mtime)
if(pCurr.mtime !== pPrev.mtime)
console.log('file ' + pFileName + 'is changed');
};
}
@ -67,7 +72,7 @@ function processManifest(){
Manifest = 'CACHE MANIFEST\n' +
'#' + new Date() + '\n' +
'CACHE:\n' +
Manifest +
NamesList_s +
'NETWORK:\n' +
'*';
@ -75,4 +80,14 @@ function processManifest(){
fs.writeFile('cloudcmd.appcache', Manifest, function(){
console.log('cloudcmd.appcache refreshed');
});
}
/* function do safe require of needed module */
function cloudRequire(pModule){
try{
return require(pModule);
}
catch(pError){
return false;
}
}

View file

@ -99,6 +99,8 @@ if(!Zlib)
/* добавляем модуль с функциями */
var CloudFunc = cloudRequire(CloudServer.LIBDIR +
'/cloudfunc');
CloudServer.AppCache = cloudRequire(CloudServer.LIBDIRSERVER +
'/appcache');
CloudServer.Obj = cloudRequire(CloudServer.LIBDIRSERVER +
'/object');
if(CloudServer.Obj){
@ -160,11 +162,10 @@ CloudServer.init = (function(){
/* Если нужно минимизируем скрипты */
this.Minify.doit();
/* Для скриптов, которые будут считываться и сжиматься по-ходу
* по-обращению устанавливам флаг "не обращаться внимание
* на изминение файла". Мы будем его контролировать сами.
*/
//this.Minify.force = true;
/* створюємо файл app cache */
var lAppCache = CloudServer.AppCache;
if(lAppCache)
lAppCache.createManifest();
});
@ -338,7 +339,7 @@ CloudServer._controller = function(pReq, pRes)
console.log('reading '+lName);
/* watching is file changed */
//CloudServer.AppCache.watch(lName);
CloudServer.AppCache.watch(lName);
/* сохраняем указатель на response и имя */
CloudServer.Responses[lName] = pRes;