-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
73 lines (59 loc) · 1.9 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
'use strict';
var path = require('path');
var express = require('express');
var compile = require('./lib/compiler');
var settings = require('./lib/settings.js');
module.exports = less;
module.exports.settings = settings;
function less(filename, options) {
options = settings.normalize(options || {});
var app = express();
app.get('/bundle.css', function (req, res, next) {
compile(filename, options).done(function (result) {
res.setHeader('content-type', 'text/css');
// vary
if (!res.getHeader('Vary')) {
res.setHeader('Vary', 'Accept-Encoding');
} else if (!~res.getHeader('Vary').indexOf('Accept-Encoding')) {
res.setHeader('Vary', res.getHeader('Vary') + ', Accept-Encoding');
}
//check old etag
if (req.headers['if-none-match'] === result.md5) {
res.statusCode = 304;
res.end();
return;
}
//add new etag
res.setHeader('ETag', result.md5);
//add cache control
if (options.cache && options.cache !== 'dynamic') {
res.setHeader('Cache-Control', options.cache);
}
var buffer = result.raw;
//add gzip
if (options.gzip && supportsGzip(req)) {
res.setHeader('Content-Encoding', 'gzip');
buffer = result.gzipped;
}
//set content-length (buffer must always be a buffer)
res.setHeader('Content-Length', buffer.length);
if ('HEAD' === req.method) res.end();
else res.end(buffer);
}, next);
});
app.get('/files/:fileID/:filename', function (req, res, next) {
var filename;
if (filename = compile.file(req.params.fileID)) {
res.setHeader('X-Server-File-Name', filename);
res.sendfile(filename);
} else {
next();
}
});
return app;
}
function supportsGzip(req) {
return req.headers
&& req.headers['accept-encoding']
&& req.headers['accept-encoding'].indexOf('gzip') !== -1;
}