improved reading file with streams

This commit is contained in:
coderaiser 2012-11-22 07:27:45 -05:00
parent c3cfbce39f
commit e24042e672
5 changed files with 82 additions and 53 deletions

View file

@ -112,6 +112,8 @@ set enveronment varibles "oauth_client_id" and
* Changed funcyBox version to 2.1.3.
* Improved reading file with streams.
2012.10.01, Version 0.1.7

View file

@ -2,7 +2,7 @@
"cache" : {"allowed" : false},
"appcache" : false,
"minification" : {
"js" : true,
"js" : false,
"css" : true,
"html" : true,
"img" : true

View file

@ -6,6 +6,7 @@
SRVDIR,
Util,
OK = 200,
Extensions = {
'.css' : 'text/css',
'.js' : 'text/javascript',
@ -28,6 +29,7 @@
/* Functions */
exports.generateHeaders = generateHeaders,
exports.sendFile = sendFile,
exports.require = mrequire,
exports.librequire = librequire,
exports.srvrequire = srvrequire,
@ -75,7 +77,7 @@
* second initializing after all modules load, so global var is
* totally filled of all information that should know all modules
*/
global.cloudcmd.main = exports;
var main = global.cloudcmd.main = exports;
/**
* function do safe require of needed module
@ -152,5 +154,33 @@
return lRet;
}
/**
* send file to client thru pipe
* and gzip it if client support
*
* @param pName - имя файла
* @param pGzip - данные сжаты gzip'ом
*/
function sendFile(pParams){
var lName = pParams.name,
lReq = pParams.request,
lRes = pParams.response,
lEnc = lReq.headers['accept-encoding'] || '',
lGzip = lEnc.match(/\bgzip\b/),
lReadStream = main.fs.createReadStream(lName, {
'bufferSize': 4 * 1024
});
lRes.writeHead(OK, generateHeaders(lName, true) );
if (lGzip)
lReadStream = lReadStream.pipe( main.zlib.createGzip() );
lReadStream.pipe(lRes);
}
})();

View file

@ -31,6 +31,7 @@
command : lCommand,
method : lMethod,
body : pBody,
request : lReq,
response : lRes
});
@ -57,7 +58,7 @@
/**
* getting data on method and command
*
* @param pParams {command, method, body, response}
* @param pParams {command, method, body, requrest, response}
*/
function getData(pParams){
var lResult,
@ -85,7 +86,7 @@
/**
* process data on GET request
*
* @param pParams {command, method, body, response}
* @param pParams {command, method, body, requrest, response}
*/
function onGET(pParams){
var lResult = {error: 'command not found'},
@ -114,7 +115,7 @@
/**
* process data on PUT request
*
* @param pParams {command, method, body, response}
* @param pParams {command, method, body, requrest, response}
*/
function onPUT(pParams){
var lResult = {error: 'command not found'},
@ -128,23 +129,16 @@
send(lRes, pTocken);
});
lResult = false;
break;
/* Example:
* read=[lib/dom.js, lib/cloudfunc.js, client.js]
*/
case 'read':
console.log(lBody);
var lFiles = lBody;
if( Util.isString(lFiles) ){
lRes.writeHead(OK, main.generateHeaders(lFiles, true) );
fs.createReadStream(lFiles, {
'bufferSize': 4 * 1024
})
.pipe( zlib.createGzip() )
.pipe(lRes);
pParams.name = lFiles;
main.sendFile(pParams);
lResult = null;
}

View file

@ -246,12 +246,13 @@
!Util.isContainStr(pathname, lNoJS_s) &&
!Util.strCmp(pathname, '/') &&
!Util.strCmp(lQuery, 'json') ) {
/* если имена файлов проекта - загружаем их*/
/* убираем слеш и читаем файл с текущец директории*/
/* если имена файлов проекта - загружаем их *
* убираем слеш и читаем файл с текущец директории */
/* добавляем текующий каталог к пути */
var lName = '.' + pathname;
console.log('reading '+lName);
console.log('reading ' + lName);
/* watching is file changed */
if(lConfig.appcache)
@ -283,20 +284,21 @@
* and in Minifys cache is files, so save it to
* CloudServer cache
*/
if(!lFileData &&
lMinify._allowed){
console.log('trying to read data from Minify.Cache');
lFromCache_o.cache = false;
lFileData = CloudServer.Minify.Cache[
Path.basename(lName)];
if(!lFileData && lMinify._allowed){
console.log('trying to read data from Minify.Cache');
lFromCache_o.cache = false;
lFileData = CloudServer.Minify.Cache[
Path.basename(lName)];
}
var lReadFileFunc_f = CloudServer.getReadFileFunc(lName),
/* если там что-то есть передаём данные в функцию readFile */
lResult = true;
lResult = lFileData;
if(lFileData){
/* if file readed not from cache -
* he readed from minified cache
if(lResult){
/* if file readed not from cache -
* he readed from minified cache
*/
lFromCache_o.minify = !lFromCache_o.cache;
@ -311,33 +313,34 @@
/* if file not in one of caches
* and minimizing setted then minimize it
*/
else if(lName.indexOf('min') < 0 &&
CloudServer.Minify){
var lMin_o = lConfig.minification,
else if(lName.indexOf('min') < 0 && CloudServer.Minify){
var lMin_o = lConfig.minification,
lCheck_f = function(pExt){
return Util.checkExtension(lName,pExt);
},
isAllowd_b = (lCheck_f('js') && lMin_o.js) ||
(lCheck_f('css') && lMin_o.css) ||
(lCheck_f('html') && lMin_o.html);
if(isAllowd_b){
lResult = CloudServer.Minify.optimize(lName, {
cache: true,
callback: function(pFileData){
lReadFileFunc_f(undefined, pFileData, false);
}
});
}
else
lResult = false;
} else
lResult = false;
lCheck_f = function(pExt){
return Util.checkExtension(lName,pExt);
};
lResult = (lCheck_f('js') && lMin_o.js) ||
(lCheck_f('css') && lMin_o.css) ||
(lCheck_f('html') && lMin_o.html);
if(lResult){
lResult = CloudServer.Minify.optimize(lName, {
cache: true,
callback: function(pFileData){
lReadFileFunc_f(undefined, pFileData, false);
}
});
}
}
if(!lResult)
Fs.readFile(lName, lReadFileFunc_f);
main.sendFile({
name: lName,
request: pReq,
response: pRes
});
}else{/* если мы имеем дело с файловой системой*/
/* если путь не начинаеться с no-js - значит
* js включен