-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (75 loc) · 2.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var debug = require('debug')('mat-tms');
// var TMS = require("midway-tms");
var thunkify = require("thunkify");
var toString = Object.prototype.toString;
var named = require('named-regexp').named;
var TMS = require('./libs/tms');
var Stream = require('stream')
function cobody(stream) {
return function(cb){
var buffers = []
stream.on('data', function(chunk){
buffers.push(chunk)
})
stream.on('end', function(){
cb(null, Buffer.concat(buffers).toString('utf-8'))
})
}
}
module.exports = function (tmsOption){
tmsOption = tmsOption || {
// 配置缓存失效(5分钟)
timeout: 5 * 60 * 1000,
// 配置服务器本地目录
filePath : '/home/tms/',
// // <!--TMS:rgn/tb-fp/2014/mm/atb/index/floor-nav-v2.php,gbk,1:TMS-->
// tmsRegex : /<!--TMS:(:<prefix>rgn)?\/?(:<url>[^,]*),(:<encoding>[^,]*),\d:TMS-->/ig
// {{tms("atb/h5_db12dumiao.html","alp")}}
tmsRegex : /\{\{tms\("(:<url>[^"]*)","(:<prefix>\w*)"\)}}/ig
}
var tmsRegex = named(tmsOption.tmsRegex);
// var tms_cache = new TMS(tmsOption);
// var load = function(path, option, cb){
// debug("tms uri:" + path)
// tms_cache.load(path, option, function(err, succ){
// if(err){
// debug("tms err:" + err)
// }
// cb(null, err ? "" : succ);
// });
// }
// load = thunkify(load);
return function *tms(next){
yield next;
var body = this.body
if ( body && Buffer.isBuffer(body)){
body = body.toString()
}
else if (body && body instanceof Stream ) {
body = yield cobody(this.body)
} else if (!body) {
return
}
var pathLoad = [];
var matched
// 查找 整个的 tms
while (matched = tmsRegex.exec(body)) {
var option = {}
// 查找tms上的属性
for(cap in matched.captures){
option[cap] = matched.captures[cap] && matched.captures[cap][0]
}
if(option.url){
option.url = "1/" + (option.prefix || "rgn") + "/" + option.url
console.log(option.url)
pathLoad.push(TMS.getTMS(option.url, option.encoding || "utf8"))
}else{
pathLoad.push("");
}
}
pathLoad = yield pathLoad;
this.body = tmsRegex.replace(body, function(){
return pathLoad.shift();
})
}
}