mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-21 02:29:23 +00:00
feature(join) add from npm
This commit is contained in:
parent
822d444964
commit
776f8a5158
3 changed files with 3 additions and 154 deletions
|
|
@ -13,10 +13,11 @@
|
|||
minify = require(DIR_SERVER + 'minify'),
|
||||
rest = require(DIR_SERVER + 'rest'),
|
||||
route = require(DIR_SERVER + 'route'),
|
||||
join = require(DIR_SERVER + 'join'),
|
||||
ponse = require(DIR_SERVER + 'ponse'),
|
||||
tryRequire = require(DIR_SERVER + 'tryRequire'),
|
||||
|
||||
join = tryRequire('join-io', {log: true, exit: true}),
|
||||
|
||||
WIN = process.platform === 'WIN32',
|
||||
terminal = !WIN ? require(DIR_SERVER + 'terminal') : function() {},
|
||||
|
||||
|
|
|
|||
|
|
@ -1,153 +0,0 @@
|
|||
(function() {
|
||||
'use strict';
|
||||
|
||||
var DIR = '../',
|
||||
DIR_SERVER = DIR + 'server/',
|
||||
files = require(DIR_SERVER + 'files'),
|
||||
ponse = require(DIR_SERVER + 'ponse'),
|
||||
|
||||
Minify = require(DIR_SERVER + 'minify'),
|
||||
|
||||
Util = require(DIR + 'util'),
|
||||
|
||||
path = require('path'),
|
||||
zlib = require('zlib'),
|
||||
|
||||
FILE = __dirname + '/../join.js',
|
||||
|
||||
PREFIX = '/join';
|
||||
|
||||
module.exports = function(options) {
|
||||
return join.bind(null, options);
|
||||
};
|
||||
|
||||
function join(options, req, res, next) {
|
||||
var prefix, dir,
|
||||
names,
|
||||
isMinify,
|
||||
isFunc,
|
||||
exec = Util.exec,
|
||||
read = exec.with(readPipe, req, res),
|
||||
path = ponse.getPathName(req),
|
||||
regExp = new RegExp('^' + PREFIX + '(:|/)'),
|
||||
regExpFile = new RegExp('^' + PREFIX + '/join.js$'),
|
||||
isJoin = regExp.test(path),
|
||||
isJoinFile = regExpFile.test(path);
|
||||
|
||||
if (!options)
|
||||
options = {};
|
||||
|
||||
isFunc = Util.type.function(options.minify);
|
||||
|
||||
if (isFunc)
|
||||
isMinify = options.minify();
|
||||
else
|
||||
isMinify = options.minify;
|
||||
|
||||
if (!isJoin) {
|
||||
next();
|
||||
} else {
|
||||
prefix = options.prefix || PREFIX;
|
||||
dir = options.dir || __dirname + '/../../';
|
||||
|
||||
if (isJoinFile)
|
||||
names = [FILE];
|
||||
else
|
||||
names = parse(prefix, dir, path);
|
||||
|
||||
exec.if(!isMinify, function(error, namesObj) {
|
||||
var is = Util.type.object(namesObj);
|
||||
|
||||
if (is)
|
||||
names = names.map(function(key) {
|
||||
return namesObj[key] || key;
|
||||
});
|
||||
|
||||
read(names);
|
||||
}, function(callback) {
|
||||
uglify(names, callback);
|
||||
});
|
||||
}
|
||||
|
||||
return isJoin;
|
||||
}
|
||||
|
||||
function parse(prefix, dir, url) {
|
||||
var names,
|
||||
isStr = typeof url === 'string';
|
||||
|
||||
if (!isStr)
|
||||
throw(Error('url must be string!'));
|
||||
|
||||
names = url.replace(prefix + ':', '')
|
||||
.split(':')
|
||||
.map(function(name) {
|
||||
return path.join(dir, name);
|
||||
});
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
function readPipe(req, res, names) {
|
||||
var stream,
|
||||
path = ponse.getPathName(req),
|
||||
gzip = zlib.createGzip(),
|
||||
isGzip = ponse.isGZIP(req);
|
||||
|
||||
ponse.setHeader({
|
||||
name : names[0],
|
||||
cache : true,
|
||||
gzip : isGzip,
|
||||
request : req,
|
||||
response : res
|
||||
});
|
||||
|
||||
stream = isGzip ? gzip : res;
|
||||
|
||||
files.readPipe(names, stream, function(error) {
|
||||
var msg = '';
|
||||
|
||||
if (error) {
|
||||
Util.log(error);
|
||||
msg = error.message;
|
||||
|
||||
if (res.headersSent)
|
||||
stream.end(msg);
|
||||
else
|
||||
ponse.sendError(msg, {
|
||||
name : path,
|
||||
gzip : isGzip,
|
||||
request : req,
|
||||
response : res
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/*
|
||||
* pipe should be setted up after
|
||||
* readPipe called with stream param
|
||||
*/
|
||||
if (isGzip)
|
||||
gzip.pipe(res);
|
||||
}
|
||||
|
||||
function minify(name, callback) {
|
||||
Minify(name, 'name', function(error, name) {
|
||||
callback(null, name);
|
||||
});
|
||||
}
|
||||
|
||||
function retMinify(name) {
|
||||
return minify.bind(null, name);
|
||||
}
|
||||
|
||||
function uglify(names, callback) {
|
||||
var funcs = {};
|
||||
|
||||
names.forEach(function(name) {
|
||||
funcs[name] = retMinify(name);
|
||||
});
|
||||
|
||||
Util.exec.parallel(funcs, callback);
|
||||
}
|
||||
})();
|
||||
|
|
@ -31,6 +31,7 @@
|
|||
"flop": "~1.0.25",
|
||||
"fstream": "~1.0.2",
|
||||
"http-auth": "2.1.x",
|
||||
"join-io": "~1.2.0",
|
||||
"js-beautify": "~1.5.4",
|
||||
"marked": "~0.3.2",
|
||||
"mellow": "~0.9.7",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue