forked from andrewrk/connect-static
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (140 loc) · 4.87 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
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
var zlib = require('zlib');
var fs = require('fs');
var stream = require('stream');
var util = require('util');
var path = require('path');
var Pend = require('pend');
var findit = require('findit2');
var mime = require('mime');
var url = require('url');
var StreamSink = require('streamsink');
var crypto = require('crypto');
module.exports = createGzipStaticMiddleware;
function createGzipStaticMiddleware(options, cb) {
options = options || {};
var dir = options.dir || "public";
var ignoreFile = options.ignoreFile || defaultIgnoreFile;
var aliases = options.aliases || [['/', '/index.html']];
var followSymlinks = (options.followSymlinks == null) ? true : !!options.followSymlinks;
var cacheControlHeader = options.cacheControlHeader || 'max-age=0, must-revalidate';
var cache = {};
var pend = new Pend();
var walker = findit(dir, {followSymlinks: followSymlinks});
walker.on('error', function(err) {
walker.stop();
cb(err);
});
walker.on('file', function(file, stat, linkPath) {
var usePath = linkPath || file;
if (ignoreFile(usePath)) return;
var relName = '/' + path.relative(dir, usePath).replace(/\\/g, '/');
var compressedSink = new StreamSink();
var uncompressedSink = new StreamSink();
var hashSink = new StreamSink();
var inStream = fs.createReadStream(file);
var cacheObj;
var mimeType = mime.lookup(relName);
cache[relName] = cacheObj = {
sink: null,
mime: mimeType,
charset: mime.charsets.lookup(mimeType),
mtime: stat.mtime,
hash: null,
compressed: null,
};
var fileDone = pend.hold();
var thisPend = new Pend();
var gzipPendCb = thisPend.hold();
var hashPendCb = thisPend.hold();
var uncompressedPendCb = thisPend.hold();
inStream.on('error', function(err) {
if (err.code === 'EISDIR') {
delete cache[relName];
gzipPendCb();
uncompressedPendCb();
hashPendCb();
} else {
walker.stop();
gzipPendCb(err);
uncompressedPendCb(err);
hashPendCb(err);
}
});
inStream.pipe(zlib.createGzip()).pipe(compressedSink);
compressedSink.on('finish', gzipPendCb);
inStream.pipe(uncompressedSink);
uncompressedSink.on('finish', uncompressedPendCb);
inStream.pipe(crypto.createHash('sha1')).pipe(hashSink);
hashSink.on('finish', function() {
cacheObj.hash = '"' + hashSink.toString('base64') + '"';
hashPendCb();
});
thisPend.wait(function(err) {
if (err) return fileDone(err);
var compressionRatio = compressedSink.length / uncompressedSink.length;
if (compressionRatio >= 0.95) {
// 95% of original size or worse. discard compressed sink
cacheObj.sink = uncompressedSink;
cacheObj.compressed = false;
} else {
// better than 95% of original size. discard uncompressed sink
cacheObj.sink = compressedSink;
cacheObj.compressed = true;
}
fileDone();
});
});
walker.on('end', function() {
pend.wait(function(err) {
if (err) return cb(err);
aliases.forEach(function(alias) {
cache[alias[0]] = cache[alias[1]];
});
cb(null, middleware);
});
function middleware(req, resp, next) {
var parsedUrl = url.parse(req.url);
var c = cache[parsedUrl.pathname];
if (!c) return next();
if (req.headers['if-none-match'] === c.hash) {
resp.statusCode = 304;
resp.end();
return;
}
var ifModifiedSince = new Date(req.headers['if-modified-since']);
if (!isNaN(ifModifiedSince) && c.mtime <= ifModifiedSince) {
resp.statusCode = 304;
resp.end();
return;
}
var sink = c.sink;
resp.setHeader('Content-Type', c.mime + (c.charset ? '; charset=' + c.charset : ''));
resp.setHeader('Cache-Control', cacheControlHeader);
resp.setHeader('ETag', c.hash);
if (req.headers['accept-encoding'] == null) {
if (c.compressed) {
// Stored as compressed, but will pipe through gunzip - this is the
// only case where the client does not get Content-Length.
// (Instead, chunked transfer is used.)
sink.createReadStream().pipe(zlib.createGunzip()).pipe(resp);
} else {
// Uncompressed response for client that requests uncompressed:
// The length matches the expectations.
resp.setHeader('Content-Length', sink.length);
sink.createReadStream().pipe(resp);
}
} else {
if (c.compressed) {
resp.setHeader('Content-Encoding', 'gzip');
}
// This time we don't have to decompress, so the length is OK:
resp.setHeader('Content-Length', sink.length);
sink.createReadStream().pipe(resp);
}
}
});
}
function defaultIgnoreFile(file) {
var basename = path.basename(file);
return /^\./.test(basename) || /~$/.test(basename);
}