From 8b6747ed4b52265e7bdd1c3967a2f660345c463a Mon Sep 17 00:00:00 2001 From: coderaiser Date: Fri, 14 Mar 2014 07:16:35 -0400 Subject: [PATCH] refactor(join) add readPipe --- lib/server/join.js | 111 ++++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 56 deletions(-) diff --git a/lib/server/join.js b/lib/server/join.js index 0cad6056..bf0f0a06 100644 --- a/lib/server/join.js +++ b/lib/server/join.js @@ -9,70 +9,69 @@ 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); - }; + function join(before, dir, req, res, callback) { + var names, + readFunc = Util.bind(readPipe, req, res, dir), + path = main.getPathName(req), + isJoin = CloudFunc.isJoinURL(path); if (!isJoin) Util.exec(callback); else { names = CloudFunc.getJoinArray(path); - if (!before) - readPipe(); - else - before(names, readPipe); + readFunc = Util.bind(readFunc, names); + + Util.ifExec(!before, readFunc, function(callback) { + before(names, callback); + }); } return isJoin; } + + function readPipe(req, res, dir, names) { + var stream, + gzip = zlib.createGzip(), + isGzip = main.isGZIP(req); + + main.mainSetHeader({ + name : names[0], + cache : true, + gzip : isGzip, + request : req, + response : res + }); + + stream = isGzip ? gzip : response; + + files.readPipe({ + names : names, + dir : dir, + write : stream, + callback : function(error) { + var errorStr; + + if (error) + if (!response.headersSent) + main.sendError({ + request : req, + response : res, + 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(res); + } })();