flex-combo
Advanced tools
Comparing version
39
api.js
@@ -51,2 +51,6 @@ var urlLib = require("url"); | ||
if (confJSON.filter) { | ||
this.param.filter = confJSON.filter; | ||
} | ||
if (this.param.cache) { | ||
@@ -62,2 +66,6 @@ this.cacheDir = pathLib.join(confDir, "../.cache"); | ||
} | ||
if (!this.param.urls['/']) { | ||
this.param.urls['/'] = this.param.rootdir || "src"; | ||
} | ||
}; | ||
@@ -169,3 +177,3 @@ FlexCombo.prototype = { | ||
}, | ||
getCacheFilePath: function(_url) { | ||
getCacheFilePath: function (_url) { | ||
if (this.cacheDir) { | ||
@@ -218,3 +226,3 @@ return pathLib.join(this.cacheDir, Helper.MD5(pathLib.join(this.HOST, _url))); | ||
engineHandler: function (_url, next) { | ||
var absPath = Helper.getRealPath(_url, this.param.filter, this.param.urls, this.param.debug); | ||
var absPath = Helper.getRealPath(_url, this.param.filter, this.param.urls, this.param.traceRule); | ||
@@ -236,3 +244,5 @@ var matchedIndex = -1; | ||
self.result[_url] = self.convert(result, _url); | ||
self.param.debug && Helper.Log.engine(_url, realPath || absPath); | ||
if ((_url + (realPath || absPath)).match(Helper.RegExp(self.param.traceRule))) { | ||
Helper.Log.engine(_url, realPath || absPath); | ||
} | ||
} | ||
@@ -257,3 +267,5 @@ next(); | ||
this.result[_url] = buff; | ||
this.param.debug && Helper.Log.local(_url, absPath); | ||
if ((_url + absPath).match(Helper.RegExp(this.param.traceRule))) { | ||
Helper.Log.local(_url, absPath); | ||
} | ||
} | ||
@@ -268,3 +280,5 @@ | ||
this.result[_url] = fsLib.readFileSync(absPath); | ||
this.param.debug && Helper.Log.cache(_url, absPath); | ||
if ((_url + absPath).match(Helper.RegExp(this.param.traceRule))) { | ||
Helper.Log.cache(_url, absPath); | ||
} | ||
} | ||
@@ -295,3 +309,5 @@ | ||
self.result[_url] = buff; | ||
Helper.Log.remote(_url, requestOption); | ||
if (_url.match(Helper.RegExp(self.param.traceRule))) { | ||
Helper.Log.remote(_url, requestOption); | ||
} | ||
next(); | ||
@@ -312,3 +328,3 @@ }); | ||
else { | ||
this.result[_url] = new Buffer("/* " + _url + " Loop! */"); | ||
this.result[_url] = new Buffer("/* " + _url + " is NOT FOUND in Local, and flex-combo doesn't know the URL where the online assets exist! */"); | ||
Helper.Log.error(_url); | ||
@@ -332,3 +348,5 @@ } | ||
Helper.Log.request(this.HOST, files); | ||
if ((this.HOST + files.join(' ')).match(Helper.RegExp(this.param.traceRule))) { | ||
Helper.Log.request(this.HOST, files); | ||
} | ||
@@ -366,3 +384,6 @@ for (var i = 0; i < FLen; i++) { | ||
} | ||
Helper.Log.response(self.HOST + req.url); | ||
var resurl = self.HOST + req.url; | ||
if (resurl.match(Helper.RegExp(self.param.traceRule))) { | ||
Helper.Log.response(resurl); | ||
} | ||
res.end(); | ||
@@ -369,0 +390,0 @@ }); |
module.exports = { | ||
rootdir: "src", | ||
urls: {}, | ||
charset: "utf-8", | ||
urlBasedCharset: {}, | ||
hosts: { | ||
@@ -11,5 +15,2 @@ "a.tbcdn.cn": "115.238.23.240", | ||
seperator: ',', | ||
charset: "utf-8", | ||
urlBasedCharset: {}, | ||
urls: {}, | ||
engine: {}, | ||
@@ -22,3 +23,3 @@ filter: { | ||
anonymous: false, | ||
debug: false | ||
traceRule: '' | ||
}; |
@@ -19,3 +19,3 @@ var fsLib = require("fs"); | ||
/* 获取应用filter规则后的url */ | ||
exports.filteredUrl = function (_url, filter, debug) { | ||
exports.filteredUrl = function (_url, filter, traceRule) { | ||
filter = filter || {}; | ||
@@ -29,3 +29,5 @@ var regx; | ||
_url = _url.replace(regx, filter[fk]); | ||
debug && utilLib.logue("%s %s " + ori_url + " => %s", "[Filter]", regx, _url); | ||
if ((ori_url + _url).match(exports.RegExp(traceRule))) { | ||
utilLib.logue("%s %s " + ori_url + " => %s", "[Filter]", regx, _url); | ||
} | ||
} | ||
@@ -37,4 +39,4 @@ } | ||
/* 获取应用filter规则后的本地地址 */ | ||
exports.getRealPath = function (_url, filter, map, debug) { | ||
_url = exports.filteredUrl(_url, filter, debug); | ||
exports.getRealPath = function (_url, filter, map, traceRule) { | ||
_url = exports.filteredUrl(_url, filter, traceRule); | ||
_url = (_url.match(/^\//) ? '' : '/') + _url; | ||
@@ -72,2 +74,9 @@ map = map || {}; | ||
exports.RegExp = function (str) { | ||
str = str.toString(); | ||
return new RegExp(str.replace(/[\*\.\?\+\$\^\[\]\(\)\{\}\|\\\/]/g, function (all) { | ||
return "\\" + all; | ||
})); | ||
}; | ||
exports.merge = utilLib.merge; | ||
@@ -74,0 +83,0 @@ exports.MD5 = utilLib.MD5; |
{ | ||
"name": "flex-combo", | ||
"version": "0.8.2", | ||
"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).", | ||
"version": "0.8.3", | ||
"description": "A combo tool designed for web front-end developer, which support various kinds of combo format by modify configuration(eg. yahoo combo).", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -15,9 +15,14 @@ # Flex Combo 介绍 | ||
## 安装 | ||
安装为命令 | ||
npm install -g flex-combo | ||
全局安装 | ||
``` | ||
npm install -g flex-combo | ||
``` | ||
或者安装到某个项目 | ||
npm install flex-combo | ||
``` | ||
npm install flex-combo | ||
``` | ||
@@ -28,3 +33,3 @@ ##快速上手 | ||
```` | ||
``` | ||
127.0.0.1 g.cdn.cn | ||
@@ -38,4 +43,5 @@ ``` | ||
``` | ||
之后所有向`g.cdn.cn`发起的静态资源请求都将通过`Flex Combo`代理 | ||
之后所有向`g.cdn.cn`发起的资源文件请求都将通过`Flex Combo`代理 | ||
## 特性 | ||
@@ -97,12 +103,14 @@ | ||
Options 如下: | ||
Options 如下: | ||
-d, --dir [string] 本地目录,默认为执行命令的当前目录 | ||
-u, --url [string] 本地目录映射URL,例如:传入/apps/et本地目录被映射到/apps/et下,这意味着只有当一个请求以/apps/et开头时,才会本地目录中寻找文件,本地目录由dir参数所指定的 | ||
-H, --host [string] 服务器域名,如果文件不在本地,将到此域名处请求文件。 | ||
-s, --servlet [string] Combo的servlet,对于Tengine而言是"?",对yahoo而言是"combo",默认是"?" | ||
-e, --seperator [string] 文件分隔符,默认为"," | ||
-c, --charset [string] http响应数据的编码方式,默认为utf-8 | ||
-p, --http_port [int] 启动HTTP服务的端口,默认为80 | ||
-P, --https_port [int] 启动HTTPS服务的端口,默认为443 | ||
``` | ||
-d, --dir [string] 本地目录,默认为执行命令的当前目录 | ||
-u, --url [string] 本地目录映射URL,例如:传入/apps/et本地目录被映射到/apps/et下,这意味着只有当一个请求以/apps/et开头时,才会本地目录中寻找文件,本地目录由dir参数所指定的 | ||
-H, --host [string] 服务器域名,如果文件不在本地,将到此域名处请求文件。 | ||
-s, --servlet [string] Combo的servlet,对于Tengine而言是"?",对yahoo而言是"combo",默认是"?" | ||
-e, --seperator [string] 文件分隔符,默认为"," | ||
-c, --charset [string] http响应数据的编码方式,默认为utf-8 | ||
-p, --http_port [int] 启动HTTP服务的端口,默认为80 | ||
-P, --https_port [int] 启动HTTPS服务的端口,默认为443 | ||
``` | ||
@@ -125,3 +133,8 @@ 在项目目录下执行`flex-combo`而不带任何参数时,将以项目目录为根目录建立Combo服务器。 | ||
{ | ||
"rootdir": "src", | ||
"urls": { | ||
"/xxx": "/Users/david/xxxproject" | ||
}, | ||
"charset": "utf-8", | ||
"urlBasedCharset": {}, | ||
"hosts": { | ||
@@ -136,7 +149,2 @@ "a.cdn.cn":"122.225.67.241", | ||
"seperator": ",", | ||
"charset": "utf-8", | ||
"urlBasedCharset": {}, | ||
"urls": { | ||
"/xxx": "/Users/david/xxxproject" | ||
}, | ||
"engine": { | ||
@@ -150,6 +158,9 @@ "^/mock/.+\\.json$":"mock/index.js" | ||
}, | ||
"debug": true | ||
"traceRule": "" | ||
} | ||
``` | ||
#### rootdir | ||
`Flex Combo`所代理资源文件的本地映射根目录 | ||
#### urls | ||
@@ -183,2 +194,16 @@ | ||
#### 编码参数 | ||
`charset` 设置flex-combo返回数据的编码集。只能设置为`gbk`或者`utf-8`。该设置与源文件编码集无关。`Flex Combo`假设源文件只有`gbk`和`utf-8`两种编码方式。会自动探测源文件是否`utf-8`。因此你可以在一个combo链接中同时引入`utf-8`和`gbk`编码的文件而不会出错。 | ||
`urlBasedCharset` 可针对某一个url设置响应字符集。例如: | ||
``` | ||
"charset" : "utf-8", | ||
"urlBasedCharset" : {"/apps/aaa.js":"gbk"} | ||
``` | ||
允许在大多数情况下返回字符集为utf-8字符集的资源。但在访问/apps/aaa.js的情况下,以gbk的方式编码。 | ||
这个特性多被用来引入编码几不同的第三方脚本。 | ||
#### host相关参数 | ||
@@ -213,16 +238,2 @@ | ||
#### 编码参数 | ||
`charset` 设置flex-combo返回数据的编码集。只能设置为`gbk`或者`utf-8`。该设置与源文件编码集无关。`Flex Combo`假设源文件只有`gbk`和`utf-8`两种编码方式。会自动探测源文件是否`utf-8`。因此你可以在一个combo链接中同时引入`utf-8`和`gbk`编码的文件而不会出错。 | ||
`urlBasedCharset` 可针对某一个url设置响应字符集。例如: | ||
``` | ||
"charset" : "utf-8", | ||
"urlBasedCharset" : {"/apps/aaa.js":"gbk"} | ||
``` | ||
允许在大多数情况下返回字符集为utf-8字符集的资源。但在访问/apps/aaa.js的情况下,以gbk的方式编码。 | ||
这个特性多被用来引入编码几不同的第三方脚本。 | ||
#### 自定义引擎 | ||
@@ -236,5 +247,5 @@ | ||
#### 调试模式 | ||
#### 调试信息输出规则 | ||
`debug` 为true时,终端窗口将显示更多信息 | ||
`traceRule` 为正则表达式字符串,确定终端窗口显示信息的规则 | ||
@@ -241,0 +252,0 @@ ## lib开发模式 |
Sorry, the diff of this file is not supported yet
48489
2.26%706
4.13%293
3.9%