-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.js
More file actions
170 lines (147 loc) · 5.22 KB
/
Copy pathmiddleware.js
File metadata and controls
170 lines (147 loc) · 5.22 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var connect = require("connect"),
error = require("./error"),
exec = require("child_process").exec,
fs = require("fs"),
parse = require("url").parse,
path = require("path"),
utils = require("connect/lib/connect/utils");
exports.staticProvider = function (root, mount) {
var staticGzip = exports.staticGzip({
root : path.normalize(root),
compress : [
"application/javascript",
"application/xml",
"text/css",
"text/html"
]
});
var staticProvider = connect.staticProvider(path.normalize(root));
return function (request, response, next) {
var url = request.url;
var pathname = require("url").parse(url).pathname;
if (pathname.indexOf(mount) === 0) {
request.url = url.replace(mount, "") || "/";
staticGzip(request, response, function (err) {
if (err) {
request.url = url;
return next(err);
}
staticProvider(request, response, function (err) {
request.url = url;
next(err);
});
});
} else
next();
};
};
exports.errorHandler = function() {
return function(err, req, res, next) {
if (!(err instanceof Error)) {
err = new error.InternalServerError(err.message || err.toString());
}
else if (!(err instanceof error.HttpError)) {
err.code = 500;
err.defaultMessage = "Internal Server Error";
}
var isXHR = req.headers["x-requested-with"] && req.headers["x-requested-with"].toLowerCase() == "xmlhttprequest";
if (!isXHR) {
fs.readFile(__dirname + "/view/error.tmpl.html", "utf8", function(e, html) {
if (e)
return next(e);
html = html
.toString('utf8')
.replace(/\<%errormsg%\>/g, err.toString());
res.writeHead(err.code || 500, {"Content-Type": "text/html"});
return res.end(html);
});
}
else {
res.writeHead(err.code || 500, {"Content-Type": "text/plain"});
res.end(err.message ? err.message.toString() : "");
}
if (err.stack)
console.log("Exception found" + err.message + "\n" + err.stack);
};
};
exports.bin = "gzip";
exports.flags = "--best";
exports.staticGzip = function(options){
var options = options || {},
root = options.root,
compress = options.compress,
flags = options.flags || exports.flags,
bin = options.bin || exports.bin;
if (!root)
throw new Error("staticGzip root must be set");
if (!compress)
throw new Error("staticGzip compress array must be passed");
return function(req, res, next){
if (req.method !== "GET")
return next();
var acceptEncoding = req.headers["accept-encoding"] || "";
// Ignore when Accept-Encoding does not allow gzip
if (acceptEncoding && !~acceptEncoding.indexOf("gzip"))
return next();
// Parse the url
var url = parse(req.url),
filename = path.join(root, url.pathname),
mime = utils.mime.type(filename).split(';')[0];
// MIME type not white-listed
if (!~compress.indexOf(mime))
return next();
// Check if gzipped static is available
gzipped(filename, function(err, path, ext){
if (err && err.errno === process.ENOENT) {
next();
// We were looking for a gzipped static,
// so lets gzip it!
if (err.path.indexOf(".gz") === err.path.length - 3)
gzip(filename, path, flags, bin);
}
else if (err) {
next(err);
}
else {
// Re-write the url to serve the gzipped static
req.url = (url.pathname + ext).replace(/[^/]+$/, ".$&");
var writeHead = res.writeHead;
res.writeHead = function(status, headers){
headers = headers || {};
res.writeHead = writeHead;
headers["Content-Type"] = mime;
headers["Content-Encoding"] = "gzip";
res.writeHead(status, headers);
};
next();
}
});
}
};
function gzipped(path, fn) {
fs.stat(path, function(err, stat){
if (err) return fn(err);
var ext = "." + Number(stat.mtime) + ".gz";
path += ext;
path = path.replace(/[^/]+$/, ".$&");
fs.stat(path, function(err){
fn(err, path, ext);
});
});
};
/**
* Escapes a command for its usage in CLI
*/
var escapeShell = function(cmd) {
return cmd.replace(/([\\"'`$\s])/g, "\\$1");
}
function gzip(src, dest, flags, bin) {
var cmd = escapeShell(bin) + " " + flags + " -c "
+ escapeShell(src) + " > " + escapeShell(dest);
exec(cmd, function(err, stdout, stderr){
if (err) {
console.error("\n" + err.stack);
fs.unlink(dest);
}
});
};