-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcombo.js
More file actions
138 lines (134 loc) · 4.19 KB
/
combo.js
File metadata and controls
138 lines (134 loc) · 4.19 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* nodejs-combo 0.2.0
* kavenyan@gmail.com
* thanks to devglass
*/
var sys = require('sys'),
url = require('url'),
fs = require('fs'),
path = require('path'),
http = require('http'),
filesCache = {};
http.createServer(function(req,res){
var COMBO_SERVER = 'nodejs-combo',
COMBO_FLAG = '/combo=',
COMBO_SEPARATOR = '&',
NEW_LINE = '\n',
pathname = req.url,
files = [],
filenames,
filesCount = 0, //set filesCount is number so that filesCount ++ is right
cacheFile,
getPath = function(pathname){
if(pathname.slice(0,1) !== '/'){
pathname = '/' + pathname;
}
return path.join(__dirname,pathname);
},
setCache = function(pathname,data){
if(pathname.slice(0,1) === '/'){
pathname = pathname.slice(1);
}
filesCache[pathname] = data;
console.log('Setting cache pathname is ' + pathname);
},
getCache = function(pathname){
if(pathname.slice(0,1) === '/'){
pathname = pathname.slice(1);
}
console.log('Getting cache pathname is ' + pathname);
return filesCache[pathname];
},
throwErr = function(err,pathname){
console.log(err.message);
console.log(getPath(pathname));
var body = err.message.replace(getPath(pathname),pathname);
console.log('err = ' + err);
res.writeHead(500,{
'Content-Type':'text/plain',
'Content-Length':body.length,
'Date': new Date(),
'Connection':'close',
'Server':COMBO_SERVER
});
res.write(body);
res.end();
},
sendReq = function(data){
console.log('start to send request! data.length = ' + data.length);
res.writeHead(200, {
//'Content-Type': 'application/x-javascript',
'Content-Type': 'text/plain',
'Content-Length': data.length,
'Cache-Control': 'max-age=315360000',
'Vary': 'Accept-Encoding',
'Date': new Date(),
'Expires': new Date((new Date()).getTime() + (60 * 60 * 1000 * 365 * 10)),
'Age': '300',
'Connection': 'close',
'Accept-Ranges': 'bytes',
'Server':COMBO_SERVER
});
res.write(data);
res.end();
},
sendComboReq = function(){
if(filesCount === filenames.length){
fileData = files.join(NEW_LINE);
sendReq(fileData);
setCache(pathname,fileData);
}
};
if(pathname === '/favicon.ico'){
return;
}
console.log('Serving:' + pathname);
cacheFile = getCache(pathname);
if(cacheFile){
console.log(pathname + ' comes from nodejs-combo cache!');
sendReq(cacheFile);
}else{
if(pathname.slice(0,COMBO_FLAG.length) === COMBO_FLAG){
console.log('combo is here');
filenames = pathname.slice(7).split(COMBO_SEPARATOR);
for(var i=0,n=filenames.length;i<n;i++){
cacheFile = getCache(filenames[i]);
if(cacheFile){
filesCount ++;
files[i] = cacheFile;
sendComboReq()
}else{
fs.readFile(getPath(filenames[i]),(function(i){
return function(){
var err = arguments[0],
data = arguments[1],
fileData;
//console.log('i = ' + i);
if(err){
throwErr(err,filenames[i]);
}else{
files[i] = data;
setCache(filenames[i],data);
}
filesCount ++;
//use filesCount rather than files.length,
//because fs.readFile() is async, file[n] maybe is finished first,
//so that files.length === filesnames.length cause an error.
sendComboReq();
}
})(i));//pass i to fs.readFile's callback, see more at Y.rbind of YUI3
}
}
}else{
console.log('return the file directly');
fs.readFile(getPath(pathname),function(err,data){
if(err){
throwErr(err,pathname);
}else{
sendReq(data);
setCache(pathname,data);
}
});
}
}
}).listen(8037);
console.log('nodejs-combo is running!');