refactor(join) add readPipe

This commit is contained in:
coderaiser 2014-03-14 07:16:35 -04:00
parent 0b3b0df132
commit 8b6747ed4b

View file

@ -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);
}
})();