flex-combo
Advanced tools
Comparing version 0.11.1 to 0.11.2
198
api.js
@@ -54,4 +54,5 @@ var urlLib = require("url"); | ||
if (this.param.cache && !fsLib.existsSync(this.cacheDir)) { | ||
mkdirp.sync(this.cacheDir); | ||
fsLib.chmod(this.cacheDir, 0777); | ||
mkdirp(this.cacheDir, function(e, dir) { | ||
fsLib.chmod(dir, 0777); | ||
}); | ||
} | ||
@@ -84,5 +85,5 @@ | ||
}, | ||
addEngine: function (rule, func, p, inner) { | ||
addEngine: function (rule, func, p, realtime) { | ||
if (rule && typeof func == "function") { | ||
(inner ? this.engines : ENGINES).push({ | ||
(realtime ? this.engines : ENGINES).push({ | ||
rule: rule, | ||
@@ -94,2 +95,41 @@ func: func, | ||
}, | ||
buildRequestOption: function (url) { | ||
url = encodeURI(url); | ||
if (!this.req) { | ||
return {path: url}; | ||
} | ||
else if (this.req.headers["x-broker"] == "flex-combo") { | ||
return false; | ||
} | ||
var protocol = (this.req.protocol || "http") + ':'; | ||
var H = this.req.headers.host.split(':'); | ||
var reqPort = H[1] || (protocol == "https:" ? 443 : 80); | ||
var reqHostName = H[0]; | ||
var reqHostIP = reqHostName; | ||
if (this.param.hostIp) { | ||
reqHostIP = this.param.hostIp; | ||
} | ||
else if (this.param.hosts && this.param.hosts[reqHostName]) { | ||
reqHostIP = this.param.hosts[reqHostName]; | ||
} | ||
var requestOption = { | ||
protocol: protocol, | ||
host: reqHostIP, | ||
port: reqPort, | ||
path: url, | ||
method: this.req.method || "GET", | ||
rejectUnauthorized: false, | ||
headers: { | ||
"x-broker": "flex-combo", | ||
host: reqHostName, | ||
cookie: this.req.headers.cookie | ||
} | ||
}; | ||
requestOption.headers = merge.recursive(true, this.param.headers || {}, requestOption.headers); | ||
return requestOption; | ||
}, | ||
init: function (req, res) { | ||
@@ -139,69 +179,2 @@ this.req = req; | ||
}, | ||
convert: function (buff, _url) { | ||
if (!Buffer.isBuffer(buff)) { | ||
buff = new Buffer(buff); | ||
} | ||
var selfCharset = isUtf8(buff) ? "utf-8" : "gbk"; | ||
var outputCharset = (this.param.charset || "utf-8").toLowerCase(); | ||
if (this.param.urlBasedCharset && _url && this.param.urlBasedCharset[_url]) { | ||
outputCharset = this.param.urlBasedCharset[_url]; | ||
} | ||
if (selfCharset == outputCharset) { | ||
return buff; | ||
} | ||
else { | ||
return iconv.encode(iconv.decode(buff, selfCharset), outputCharset); | ||
} | ||
}, | ||
getCacheFilePath: function (_url) { | ||
if (this.cacheDir) { | ||
return pathLib.join(this.cacheDir, Helper.MD5(pathLib.join(this.HOST, _url))); | ||
} | ||
else { | ||
return false; | ||
} | ||
}, | ||
cacheFile: function (_url, buff) { | ||
var absPath = this.getCacheFilePath(_url); | ||
if (absPath && !/[<>\*\?]+/g.test(absPath)) { | ||
fsLib.writeFile(absPath, buff); | ||
} | ||
}, | ||
buildRequestOption: function (url) { | ||
if (this.req.headers["x-broker"] == "flex-combo") { | ||
return false; | ||
} | ||
var protocol = (this.req.protocol || "http") + ':'; | ||
var H = this.req.headers.host.split(':'); | ||
var reqPort = H[1] || (protocol == "https:" ? 443 : 80); | ||
var reqHostName = H[0]; | ||
var reqHostIP = reqHostName; | ||
if (this.param.hostIp) { | ||
reqHostIP = this.param.hostIp; | ||
} | ||
else if (this.param.hosts && this.param.hosts[reqHostName]) { | ||
reqHostIP = this.param.hosts[reqHostName]; | ||
} | ||
var requestOption = { | ||
protocol: protocol, | ||
host: reqHostIP, | ||
port: reqPort, | ||
path: url, | ||
method: this.req.method || "GET", | ||
rejectUnauthorized: false, | ||
headers: { | ||
"x-broker": "flex-combo", | ||
host: reqHostName, | ||
cookie: this.req.headers.cookie | ||
} | ||
}; | ||
requestOption.headers = merge.recursive(true, this.param.headers || {}, requestOption.headers); | ||
return requestOption; | ||
}, | ||
engineHandler: function (_url, next) { | ||
@@ -258,4 +231,4 @@ var filteredURL = Helper.filteredUrl(_url, this.param.filter, this.param.traceRule); | ||
else { | ||
if (/favicon\.ico$/.test(_url)) { | ||
this.result[_url] = fsLib.readFileSync(pathLib.join(__dirname, "bin/favicon.ico")); | ||
if (/^\/favicon\.ico$/.test(_url)) { | ||
this.result[_url] = fsLib.readFileSync(pathLib.join(__dirname, "assets/favicon.ico")); | ||
} | ||
@@ -287,15 +260,31 @@ else { | ||
if (requestOption) { | ||
fetch.request(requestOption, function (e, buff) { | ||
fetch.request(requestOption, function (e, buff, nsres) { | ||
var remoteURL = self.HOST + _url; | ||
var tips; | ||
if (e) { | ||
self.result[_url] = new Buffer("/* " + _url + " " + e.code + "! */"); | ||
Helper.Log.error(_url); | ||
next(); | ||
tips = remoteURL + " Fetch Error!"; | ||
self.result[_url] = new Buffer("/* " + tips + " */"); | ||
Helper.Log.error(tips); | ||
next(500); | ||
} | ||
else { | ||
self.cacheFile(_url, buff); | ||
self.result[_url] = buff; | ||
if (self.param.traceRule && self.param.traceRule.test("Remote " + _url)) { | ||
Helper.Log.remote(_url, requestOption); | ||
if (nsres.statusCode == 404) { | ||
tips = remoteURL + ' ' + nsres.statusMessage + '!'; | ||
if (/^image\//.test(self.MIME)) { | ||
self.result[_url] = fsLib.readFileSync(pathLib.join(__dirname, "assets/404.jpg")); | ||
} | ||
else { | ||
self.result[_url] = new Buffer("/* " + tips + " */"); | ||
} | ||
Helper.Log.error(tips); | ||
next(404); | ||
} | ||
next(); | ||
else { | ||
self.cacheFile(_url, buff); | ||
self.result[_url] = buff; | ||
if (self.param.traceRule && self.param.traceRule.test("Remote " + _url)) { | ||
Helper.Log.remote(_url, requestOption); | ||
} | ||
next(); | ||
} | ||
} | ||
@@ -306,4 +295,4 @@ }); | ||
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); | ||
next(); | ||
Helper.Log.error(_url + " Not Found!"); | ||
next(404); | ||
} | ||
@@ -353,4 +342,4 @@ } | ||
async.series(Q, function () { | ||
this.res.writeHead(200, { | ||
async.series(Q, function (statusCode) { | ||
this.res.writeHead(statusCode || 200, { | ||
"Access-Control-Allow-Origin": '*', | ||
@@ -376,2 +365,39 @@ "Content-Type": this.MIME + (Helper.isBinFile(this.URL) ? '' : ";charset=" + this.param.charset), | ||
} | ||
}, | ||
convert: function (buff, _url) { | ||
if (!Buffer.isBuffer(buff)) { | ||
buff = new Buffer(buff); | ||
} | ||
var selfCharset = isUtf8(buff) ? "utf-8" : "gbk"; | ||
var outputCharset = (this.param.charset || "utf-8").toLowerCase(); | ||
if (this.param.urlBasedCharset && _url && this.param.urlBasedCharset[_url]) { | ||
outputCharset = this.param.urlBasedCharset[_url]; | ||
} | ||
if (selfCharset == outputCharset) { | ||
return buff; | ||
} | ||
else { | ||
return iconv.encode(iconv.decode(buff, selfCharset), outputCharset); | ||
} | ||
}, | ||
getCacheFilePath: function (_url) { | ||
if (this.cacheDir) { | ||
return pathLib.join(this.cacheDir, Helper.MD5(pathLib.join(this.HOST, _url))); | ||
} | ||
else { | ||
return false; | ||
} | ||
}, | ||
cacheFile: function (_url, buff) { | ||
var absPath = this.getCacheFilePath(_url); | ||
if (absPath && !/[<>\*\?]+/g.test(absPath)) { | ||
fsLib.writeFile(absPath, buff, function(e) { | ||
if (!e) { | ||
fsLib.chmod(absPath, 0777); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -378,0 +404,0 @@ }; |
{ | ||
"name": "flex-combo", | ||
"version": "0.11.1", | ||
"version": "0.11.2", | ||
"description": "A combo tool designed for web front-end developer, which support various kinds of combo format by modify configuration(eg. yahoo combo).", | ||
@@ -14,3 +14,3 @@ "main": "index.js", | ||
"dac": "~0.5.1", | ||
"fetch-agent": "~0.2.1", | ||
"fetch-agent": "~0.2.2", | ||
"mace": "~2.0.0", | ||
@@ -17,0 +17,0 @@ "merge": "~1.2.0", |
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
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
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
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
75796
14
709
Updatedfetch-agent@~0.2.2