-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
108 lines (101 loc) · 2.8 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
102
103
104
105
106
107
108
var common = require('common');
var mime = require('mime');
var fs = require('fs');
var path = require('path');
var rex = require('rex');
var jade = require('jade');
var stylus = require('stylus');
var onsecure = function(fn) {
return function(request, response) {
if (/(^|\/)\.\.(\/|$)/.test(path.normalize(request.url))) {
response.writeHead(403);
response.end();
return;
}
fn(request, response);
};
};
exports.stylus = function(location) {
return onsecure(function(request, response) {
common.step([
function(next) {
fs.readFile(common.format(location, request.params), 'utf-8', next);
},
function(src, next) {
stylus.render(src, next);
},
function(str) {
response.writeHead(200, {
'content-type': 'text/css; charset=utf-8',
'content-length': Buffer.byteLength(str)
});
response.end(str);
}
], function(err) {
response.writeHead(500);
response.end(err.stack);
});
});
};
exports.rex = function(location) {
return onsecure(function(request, response) {
common.step([
function(next) {
rex.parse(common.format(location, request.params), next);
},
function(src) {
response.writeHead(200, {
'content-type': 'text/javascript; charset=utf-8',
'content-length': Buffer.byteLength(src)
});
response.end(src);
}
], function(err) {
response.writeHead(500);
response.end(err.stack);
});
});
};
exports.jade = function(location, locals) {
return onsecure(function(request, response) {
common.step([
function(next) {
fs.readFile(common.format(location, request.params), 'utf-8', next);
},
function(src) {
var str = jade.compile(src, {self:true})(locals);
response.writeHead(200, {
'content-type': 'text/html; charset=utf-8',
'content-length': Buffer.byteLength(str)
});
response.end(str);
}
], function(err) {
response.writeHead(500);
response.end(err.stack);
});
});
};
exports.file = function(location, options) {
options = options || {};
options.status = options.status || 200;
var base = options.cacheMaxAge && {'cache-control':'public, max-age='+options.cacheMaxAge};
return onsecure(function(request, response) {
var filename = common.format(location, request.params);
var onnotfound = function() {
response.writeHead(404);
response.end();
};
fs.stat(filename, common.fork(onnotfound, function(stat) {
var type = mime.lookup(filename);
var headers = {
'content-type':type+((/^text\//.test(type) || type === 'application/javascript') ? '; charset=utf-8' : ''),
'content-length':stat.size,
'date':new Date().toUTCString(),
'last-modified':stat.mtime.toUTCString()
};
response.writeHead(options.status, base ? common.join(headers, base) : headers);
fs.createReadStream(filename).pipe(response);
}));
});
};