mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
refactor(join) add readPipe
This commit is contained in:
parent
0b3b0df132
commit
8b6747ed4b
1 changed files with 55 additions and 56 deletions
|
|
@ -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);
|
||||
}
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue