flex-combo
Advanced tools
Comparing version 0.3.8 to 0.3.9
@@ -16,3 +16,25 @@ var http = require('http') | ||
var debugInfo = require('debug')('flex-combo:info'); | ||
var green = '\u001b[32m'; | ||
var red = '\u001b[31m'; | ||
var blue = '\u001b[34m'; | ||
var gray = '\u001b[37m'; | ||
var yellow = '\u001b[33m'; | ||
var reset = '\u001b[0m'; | ||
function cosoleResp(type, c){ | ||
if(type == "Need"){ | ||
console.log('%s=>Need: %s%s%s %s',green, reset, blue, c, reset); | ||
return; | ||
} | ||
if(type == 'Not found'){ | ||
console.log(red+'<='+type+': ' + reset + gray + ' ' + c + ' ' + reset); | ||
return; | ||
} | ||
if(type == 'Actually'){ | ||
console.log(green+' -'+type+': ' + reset + gray + ' ' + c + ' ' + reset); | ||
return; | ||
} | ||
console.log(green+'<='+type+': ' + reset + gray + ' ' + c + ' ' + reset); | ||
return; | ||
} | ||
/** | ||
@@ -46,3 +68,3 @@ Yahoo Combo: | ||
fns:[], | ||
forwardPrefix:'' | ||
hosts:{'a.tbcdn.cn':'122.225.67.241', 'g.tbcdn.cn':'115.238.23.250'} | ||
}; | ||
@@ -74,2 +96,3 @@ | ||
} | ||
cosoleResp('Actually', filtered); | ||
return filtered; | ||
@@ -79,2 +102,3 @@ } | ||
function isBinFile(fileName){ | ||
fileName = fileName.split('?')[0]; | ||
return !/.js$|.css$|.less$/.test(fileName); | ||
@@ -87,2 +111,3 @@ } | ||
function longgestMatchedDir(fullPath) { | ||
fullPath = fullPath.split('?')[0]; | ||
var map = param.urls; | ||
@@ -103,5 +128,3 @@ var longestMatchNum = -1 , longestMatchPos = null; | ||
function readFromLocal (fullPath) { | ||
debug('local file:'+ fullPath); | ||
fullPath = filterUrl(fullPath); | ||
fullPath = fullPath.split('?')[0]; | ||
var longestMatchPos = longgestMatchedDir(fullPath); | ||
@@ -129,3 +152,3 @@ if(!longestMatchPos){ return null } | ||
if(isBinFile(absPath)){ | ||
debugInfo('Local bin: %s', absPath); | ||
cosoleResp('Local', absPath); | ||
return buff; | ||
@@ -140,3 +163,3 @@ } | ||
} | ||
debugInfo('Local text:%s', absPath); | ||
cosoleResp('Local', absPath); | ||
return adaptCharset(buff, outputCharset, charset); | ||
@@ -172,3 +195,3 @@ } | ||
var readFromCache = function(fullPath){ | ||
var readFromCache = function(url, fullPath){ | ||
var absPath = path.join(param.cacheDir, fullPath); | ||
@@ -178,3 +201,3 @@ if(fs.existsSync(absPath)){ | ||
if(isBinFile(absPath)){ | ||
debugInfo('Cached remote bin:%s',absPath); | ||
cosoleResp('Cache', absPath); | ||
return buff; | ||
@@ -186,4 +209,3 @@ } | ||
var outputCharset = param.charset; | ||
fullPath = filterUrl(fullPath); | ||
var longestMatchPos = longgestMatchedDir(fullPath); | ||
var longestMatchPos = longgestMatchedDir(url); | ||
if(longestMatchPos){ | ||
@@ -194,4 +216,3 @@ if(param.urlBasedCharset && param.urlBasedCharset[longestMatchPos]){ | ||
} | ||
debugInfo('Cached remote text:%s',absPath); | ||
cosoleResp('Cache', absPath); | ||
return adaptCharset(buff, outputCharset, charset); | ||
@@ -202,2 +223,23 @@ } | ||
function buildRequestOption(url, req) { | ||
var requestOption = {host: param.host, port: 80, path: url}; | ||
//处理IP加Request Header的情况 | ||
if (param.hostIp && param.headers) { | ||
requestOption.host = param.hostIp; | ||
requestOption.headers = param.headers; | ||
} | ||
if (param.hosts) { | ||
for (hostName in param.hosts) { | ||
if (req.headers.host == hostName) { | ||
requestOption.host = param.hosts[hostName]; | ||
if (!requestOption.headers) { | ||
requestOption.headers = {}; | ||
} | ||
requestOption.headers.host = hostName; | ||
break; | ||
} | ||
} | ||
} | ||
return requestOption; | ||
} | ||
exports = module.exports = function(prjDir, urls, options){ | ||
@@ -221,3 +263,2 @@ var userHome = process.env.HOME || process.env.HOMEPATH;//兼容windows | ||
} | ||
debug(util.inspect(param)); | ||
param.cacheDir = cacheDir; | ||
@@ -237,3 +278,2 @@ if(urls){ | ||
//远程请求的域名不能和访问域名一致,否则会陷入请求循环。 | ||
debug('laiel'); | ||
if(req.headers.host === param.host){ | ||
@@ -243,5 +283,2 @@ return; | ||
var url = req.url.replace(/http:\/\/.+?\//,'/');//兼容windows,windows平台下取得的req.url带http://部分 | ||
debugInfo('Request: %s', url); | ||
var prefix = url.indexOf(param.servlet + '?'); | ||
@@ -256,5 +293,4 @@ | ||
} | ||
cosoleResp('Need', url); | ||
var filteredUrl = filterUrl(url); | ||
debug('filteredUrl: %s, mime type: %s' ,filteredUrl, mime.lookup(filteredUrl.split('?')[0])); | ||
res.setHeader('Content-Type', mime.lookup(filteredUrl.split('?')[0])); | ||
@@ -268,3 +304,4 @@ var singleFileContent = readFromLocal(filteredUrl); | ||
var cachedFile = readFromCache(filteredUrl); | ||
var fileName = crypto.createHash('md5').update(req.headers.host+url).digest('hex'); | ||
var cachedFile = readFromCache(filteredUrl, fileName); | ||
if(cachedFile){ | ||
@@ -276,9 +313,9 @@ res.end(cachedFile); | ||
//本地没有,从服务器获取 | ||
url = param.forwardPrefix + url; | ||
debug('send http request:'+ param.host+ url); | ||
http.get({host: param.host, port: 80, path: url}, function(resp) { | ||
var requestOption = buildRequestOption(url, req); | ||
http.get(requestOption, function(resp) { | ||
var buffs = []; | ||
if(resp.statusCode !== 200){ | ||
debugInfo('Remote not found.'); | ||
res.end('File ' + url + ' not found.'); | ||
cosoleResp('Not found', requestOption.host + requestOption.path + ' (host:'+ reset + yellow + requestOption.headers.host + reset + ')'); | ||
res.end('File ' + requestOption.host + requestOption.path + ' not found.'); | ||
return; | ||
@@ -297,8 +334,9 @@ } | ||
if(isBinFile(filteredUrl)){ | ||
cacheFile(filteredUrl, buff); | ||
debugInfo('Remote bin file : %s',param.host+ url); | ||
var binfileName = crypto.createHash('md5').update(req.headers.host+requestOption.path).digest('hex'); | ||
cacheFile(binfileName, buff); | ||
cosoleResp('Remote', requestOption.host + requestOption.path); | ||
res.end(buff); | ||
return; | ||
} | ||
debugInfo('Remote text : %s',param.host+ url); | ||
cosoleResp('Remote', requestOption.host + requestOption.path); | ||
var charset = isUtf8(buff) ? 'utf8' : 'gbk'; | ||
@@ -316,3 +354,4 @@ var longestMatchPos = longgestMatchedDir(filteredUrl); | ||
var singleFileContent = adaptCharset(buff, outputCharset, charset); | ||
cacheFile(filteredUrl, buff, charset); | ||
var fileName = crypto.createHash('md5').update(req.headers.host+url).digest('hex'); | ||
cacheFile(fileName, buff, charset); | ||
res.end(singleFileContent ); | ||
@@ -329,8 +368,5 @@ return; | ||
} | ||
cosoleResp('Need', url); | ||
prefix = url.substring(0, prefix); | ||
debug(prefix+'|'+param.servlet); | ||
var files = url.substring(prefix.length + param.servlet.length + 1, url.length); | ||
debug(files); | ||
files = files.split(param.seperator, 1000); | ||
@@ -346,5 +382,4 @@ | ||
if(file === "") continue; | ||
var fullPath = filterUrl(prefix + files[i]); | ||
var fullPath = prefix + filterUrl(files[i]); | ||
if(i === 0 ){ | ||
debug('mime type:%s',mime.lookup(fullPath.split('?')[0])); | ||
res.setHeader('Content-Type', mime.lookup(fullPath.split('?')[0])); | ||
@@ -355,3 +390,2 @@ } | ||
if(!fileContent){ | ||
debug('file not in local"'+fullPath); | ||
if(prevNeedHttp){ | ||
@@ -375,3 +409,2 @@ needHttpGet += ',' + file; | ||
} | ||
debug('array size: '+reqArray.length); | ||
@@ -383,4 +416,4 @@ var reqPath = prefix + param.servlet + '?'; | ||
} | ||
var cacheName = crypto.createHash('md5').update(reqArray[i].file).digest('hex'); | ||
var cachedContent = readFromCache('/' + cacheName); | ||
var cacheName = crypto.createHash('md5').update(req.headers.host+reqArray[i].file).digest('hex'); | ||
var cachedContent = readFromCache(reqArray[i].file, '/' + cacheName); | ||
if(cachedContent){ | ||
@@ -393,6 +426,7 @@ reqArray[i].content = cachedContent; | ||
(function(id) { | ||
var requestPath = param.forwardPrefix + reqPath + reqArray[id].file; | ||
http.get({host: param.host, port: 80, path: requestPath}, function(resp) { | ||
var requestPath = reqPath + reqArray[id].file; | ||
var requestOption = buildRequestOption(requestPath, req); | ||
http.get(requestOption, function(resp) { | ||
if(resp.statusCode !== 200){ | ||
debugInfo('Remote not found : %s', 'define request: ', reqPath + reqArray[id].file); | ||
debugInfo('Remote not found: %s', reqPath + reqArray[id].file); | ||
reqArray[id].ready = true; | ||
@@ -405,3 +439,2 @@ reqArray[id].content = 'File '+ reqArray[id].file +' not found.'; | ||
var buffs = []; | ||
debug('request: ' + reqPath + reqArray[id].file); | ||
resp.on('data', function(chunk) { | ||
@@ -411,3 +444,3 @@ buffs.push(chunk); | ||
resp.on('end', function() { | ||
debug('response: ' + reqPath + reqArray[id].file); | ||
cosoleResp('Remote', requestOption.host + reqPath + reqArray[id].file); | ||
reqArray[id].ready = true; | ||
@@ -420,7 +453,6 @@ var buff = joinbuffers(buffs); | ||
} | ||
var fileName = crypto.createHash('md5').update(reqArray[id].file).digest('hex'); | ||
debugInfo('Remote text:%s', reqPath + reqArray[id].file); | ||
var fileName = crypto.createHash('md5').update(req.headers.host+reqArray[id].file).digest('hex'); | ||
var charset = isUtf8(buff) ? 'utf8' : 'gbk'; | ||
reqArray[id].content = adaptCharset(buff, param.charset, charset); | ||
cacheFile('/'+fileName, buff, charset); | ||
cacheFile('/' + fileName, buff, charset); | ||
sendData(); | ||
@@ -427,0 +459,0 @@ }); |
{ | ||
"name": "flex-combo", | ||
"version": "0.3.8", | ||
"version": "0.3.9", | ||
"description": "The Flex-combo is combo tool designed for web front-end developer. It support various kinds of combo format by modify configuration(eg. yahoo combo).", | ||
@@ -5,0 +5,0 @@ "main": "flex-combo.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69445
1542