diff --git a/ChangeLog b/ChangeLog index 4139eddd..69dc6f9b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -40,6 +40,8 @@ running * Added module is-file-changed. +* Added suport of AppCache. + 2012.08.24, Version 0.1.6 diff --git a/index.html b/index.html index 96a56660..6bc49df2 100644 --- a/index.html +++ b/index.html @@ -1,5 +1,5 @@ - + diff --git a/lib/server/appcache.js b/lib/server/appcache.js index a04babc3..bdcf40f0 100644 --- a/lib/server/appcache.js +++ b/lib/server/appcache.js @@ -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; + } } \ No newline at end of file diff --git a/server.js b/server.js index 44ab485d..965d5fef 100644 --- a/server.js +++ b/server.js @@ -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;