-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreq_file.js
166 lines (150 loc) · 4.9 KB
/
req_file.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var fs = require('fs');
var zlib = require('zlib');
var url = require('url');
var crypto = require('crypto');
var app_http = require('./app_http');
// This code returns "not found" when '..' appears in the url.
var extmap = {
'png' : { type: 'image/png', gzip: true },
'jpg' : { type: 'image/jpg', gzip: true },
'db' : { type: 'image/PNG', gzip: true },
'js' : { type: 'application/javascript', gzip: true },
'css' : { type: 'text/css', gzip: true },
'ico' : { type: 'image/x-icon', gzip: true },
'html' : { type: 'text/html', gzip: true },
'ogg' : { type: 'audio/ogg', gzip: false },
'mp3' : { type: 'audio/mp3', gzip: false }
};
// len = the number of characters to skip over
// to extract the filename from request url.
function FileRequestHandler(dir, len) {
this.publicDir = dir;
this.len = len;
this.files = [];
}
exports.create = function(publicDir, len) {
return new FileRequestHandler(publicDir, len);
}
// Insert file into files array.
// Throw exception if file already in array.
function insert(files, file) {
// Keep files ordered by name so we can use binary search when servicing requests.
files.push(null);
var i = files.length - 1;
for (; i > 0; --i) {
if (files[i - 1].name < file.name) break;
if (files[i - 1].name === file.name) throw new Error('duplicate insertion');
files[i] = files[i - 1];
}
files[i] = file;
}
// Return file or null. Uses binary search.
function find(files, filename) {
// Locate file using binary search.
var s = 0,
e = files.length - 1,
m;
while (s <= e) {
m = Math.floor((s + e) / 2);
if (files[m].name < filename) s = m + 1;
else if (files[m].name > filename) e = m - 1;
else return files[m];
}
return null;
}
FileRequestHandler.prototype.handle = function(req, res) {
var filename = url.parse(req.url).pathname;
filename = filename.substr(this.len);
var file = find(this.files, filename);
if (file === null) {
return app_http.replyNotFound(res);
}
if (req.headers['if-none-match'] === file.etag) {
return app_http.replyNotModified(res);
}
if (file.gzip !== undefined &&
req.headers['accept-encoding'] !== undefined &&
req.headers['accept-encoding'].indexOf('gzip') !== -1) {
return app_http.replyCached(res, file.gzip, file.type, file.etag, 'gzip');
} else {
return app_http.replyCached(res, file.data, file.type, file.etag);
}
};
// initialization
function getExt(filename) {
var i = filename.lastIndexOf('.');
if (i === -1) {
throw new Error(filename + ' has no extension');
}
var ext = filename.substr(i + 1);
if (extmap.hasOwnProperty(ext)) {
return extmap[ext];
} else {
throw new Error('extension unknown: ' + ext);
}
}
// Calculate and display memory consumption.
function displayStats(files) {
var uncompressed = 0, compressed = 0;
for (var i = 0; i < files.length; ++i) {
uncompressed += files[i].data.length;
if (files[i].gzip !== undefined) compressed += files[i].gzip.length;
}
console.log('memfile bytes, uncompressed: ' + Math.ceil(uncompressed / 1024 / 1024) + ' MB');
console.log('memfile bytes, compressed: ' + Math.ceil(compressed / 1024 / 1024) + ' MB');
}
FileRequestHandler.prototype.init = function(cb) {
var self = this;
readDir(this.publicDir, this.files, function() { displayStats(self.files); cb(); });
};
// Store contents of files in dir in the files array.
function readDir(dir, files, cb) {
fs.readdir(dir, function(err, filenames) {
if (err) throw err;
var n = filenames.length;
for (var i = 0; i < filenames.length; ++i) {
readFile(files, dir + '/' + filenames[i], function() { if (--n === 0) cb(); });
}
});
}
function endsWith(str, ending) {
if (str.length < ending) return false;
return str.substr(str.length - ending.length) == ending;
}
function ignore(filename) {
if (endsWith(filename, '.DS_Store')) return true;
if (endsWith(filename, '.swp')) return true;
return false;
}
function readFile(files, filename, cb) {
if (ignore(filename)) return cb();
fs.stat(filename, function(err, stats) {
if (err) throw err;
if (stats.isDirectory()) {
readDir(files, filename, cb);
} else if (stats.isFile()) {
readFile2(files, filename, cb);
} else {
throw new Error(filename + ' is not a file and not a directory.');
}
});
}
function readFile2(files, filename, cb) {
var ext = getExt(filename);
fs.readFile(filename, function (err, data) {
if (err) throw err;
var file = {
name: filename.substr(filename.indexOf('/') + 1),
type: ext.type,
data: data,
etag: app_http.etag(data)
};
insert(files, file);
if (ext.gzip === false) return cb();
zlib.gzip(file.data, function(err, result) {
if (err) throw err;
file.gzip = result;
cb();
});
});
}