diff --git a/lib/server.js b/lib/server.js index b32dbcdf..3160a997 100644 --- a/lib/server.js +++ b/lib/server.js @@ -25,6 +25,7 @@ Socket = main.socket, Console = main.console, Terminal = main.terminal, + join = main.srvrequire('join'), zlib = main.zlib, http = main.http, @@ -35,6 +36,8 @@ files = main.files, Server, Rest, Route; + + join = join && Util.bind(join, beforeJoin, DIR) || Util.exec.bind(Util); /* базовая инициализация */ function init(pAppCachProcessing) { @@ -238,70 +241,15 @@ }); } - function join(request, response, callback) { - var names, i, n, name, minName, stream, check, - funcs = [], - config = main.config, - dir = DIR, - gzip = zlib.createGzip(), - isGzip = main.isGZIP(request), - path = main.getPathName(request), - - isJoin = CloudFunc.isJoinURL(path), - readPipe = function() { - main.mainSetHeader({ - name : names[0], - cache : config.cache, - gzip : isGzip, - request : request, - response : response - }); - - if (!isGzip) - stream = response; - else - stream = gzip; - - files.readPipe({ - names : names, - dir : dir, - write : stream, - callback : function(error) { - var errorStr; - - if (error) - if (!response.headersSent) - main.sendError({ - request : request, - response : response, - name : path - }, error); - else { - Util.log(error); - errorStr = error.toString(); - stream.end(errorStr); - } - } - }); - - /* - * pipe should be setted up after - * readPipe called with stream param - */ - if (isGzip) - gzip.pipe(response); - }; - - if (!isJoin) - Util.exec(callback); - else { - names = CloudFunc.getJoinArray(path); + function beforeJoin(names, callback) { + var i, name, check, minName, dir, funcs, + config = main.config, n = names.length; - if (!config.minify) - readPipe(); - else { - for (i = 0; i < n; i++) { + if (!config.minify) + Util.exec(callback); + else { + for (i = 0; i < n; i++) { name = Path.join(DIR, names[i]); check = checkExtension(name); @@ -314,14 +262,11 @@ } } - funcs.push(minify.bind(null, name)); + funcs.push(Util.bind(minify, name)); } - Util.asyncCall(funcs, readPipe); - } + Util.asyncCall(funcs, callback); } - - return isJoin; } function checkExtension(name) { diff --git a/lib/server/join.js b/lib/server/join.js new file mode 100644 index 00000000..0cad6056 --- /dev/null +++ b/lib/server/join.js @@ -0,0 +1,78 @@ +(function() { + + var main = global.cloudcmd.main, + files = main.files, + Util = main.util, + CloudFunc = main.cloudfunc, + minify = main.minify, + zlib = require('zlib'); + + module.exports = join; + + function join(before, dir, request, response, callback) { + var names, i, n, name, minName, stream, check, + funcs = [], + config = main.config, + gzip = zlib.createGzip(), + isGzip = main.isGZIP(request), + path = main.getPathName(request), + + isJoin = CloudFunc.isJoinURL(path), + readPipe = function() { + main.mainSetHeader({ + name : names[0], + cache : config.cache, + gzip : isGzip, + request : request, + response : response + }); + + if (!isGzip) + stream = response; + else + stream = gzip; + + files.readPipe({ + names : names, + dir : dir, + write : stream, + callback : function(error) { + var errorStr; + + if (error) + if (!response.headersSent) + main.sendError({ + request : request, + response : response, + name : path + }, error); + else { + Util.log(error); + errorStr = error.toString(); + stream.end(errorStr); + } + } + }); + + /* + * pipe should be setted up after + * readPipe called with stream param + */ + if (isGzip) + gzip.pipe(response); + }; + + if (!isJoin) + Util.exec(callback); + else { + names = CloudFunc.getJoinArray(path); + + if (!before) + readPipe(); + else + before(names, readPipe); + } + + return isJoin; + } +})();