-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
st.js
486 lines (419 loc) · 12.3 KB
/
st.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
module.exports = st
st.Mount = Mount
var mime = require('mime')
var path = require('path')
var LRU = require('lru-cache')
var fs
try {
fs = require('graceful-fs')
} catch (e) {
fs = require('fs')
}
var url = require('url')
var zlib = require('zlib')
var Neg = require('negotiator')
var http = require('http')
var AC = require('async-cache')
var util = require('util')
// default caching options
var defaultCacheOptions = {
fd: {
max: 1000,
maxAge: 1000 * 60 * 60,
dispose: shouldClose
},
stat: {
max: 5000,
maxAge: 1000 * 60
},
content: {
max: 1024 * 1024 * 64,
length: function (n) {
return n.length
},
maxAge: 1000 * 60 * 10
},
index: {
max: 1024 * 8,
length: function (n) {
return n.length
},
maxAge: 1000 * 60 * 10
},
readdir: {
max: 1000,
length: function (n) {
return n.length
},
maxAge: 1000 * 60 * 10
}
}
var streaming = Object.create(null)
var pendingClose = Object.create(null)
function shouldClose (path, fd) {
// means that this fd is no longer in the fd lru.
// however, if any requests are still in process, then
// don't close it yet, or that'll cause problems.
if (!streaming[path]) {
delete pendingClose[path]
return fs.close(fd, function () {})
}
pendingClose[path] = fd
}
function st (opt) {
var p, u
if (typeof opt === 'string') {
p = opt
opt = arguments[1]
if (typeof opt === 'string') {
u = opt
opt = arguments[2]
}
}
if (!opt) opt = {}
else opt = util._extend({}, opt)
if (!p) p = opt.path
if (typeof p !== 'string') throw new Error('no path specified')
p = path.resolve(p)
if (!u) u = opt.url
if (!u) u = p.substr(process.cwd().length).replace(/\\/g, '/')
if (u.charAt(0) !== '/') u = '/' + u
opt.url = u
opt.path = p
var m = new Mount(opt)
var fn = m.serve.bind(m)
fn._this = m
return fn
}
function Mount (opt) {
if (!opt) throw new Error('no options provided')
if (typeof opt !== 'object') throw new Error('invalid options')
if (!(this instanceof Mount)) return new Mount(opt)
this.opt = opt
this.url = opt.url
this.path = opt.path
this._index = opt.index === false ? false
: typeof opt.index === 'string' ? opt.index
: true
// cache basically everything
var c = this.getCacheOptions(opt)
this.cache = {
fd: AC(c.fd),
stat: AC(c.stat),
index: AC(c.index),
readdir: AC(c.readdir),
content: AC(c.content)
};
}
Mount.prototype.getCacheOptions = function (opt) {
var o = opt.cache || {}
var d = defaultCacheOptions
// should really only ever set max and maxAge here.
// load and fd disposal is important to control.
var c = {
fd: util._extend(util._extend({}, d.fd), o.fd),
stat: util._extend(util._extend({}, d.stat), o.stat),
index: util._extend(util._extend({}, d.index), o.index),
readdir: util._extend(util._extend({}, d.readdir), o.readdir),
content: util._extend(util._extend({}, d.content), o.content)
}
c.fd.dispose = shouldClose
c.fd.load = this._loadFd.bind(this)
c.stat.load = this._loadStat.bind(this)
c.index.load = this._loadIndex.bind(this)
c.readdir.load = this._loadReaddir.bind(this)
c.content.load = this._loadContent.bind(this)
return c
}
// get a path from a url
Mount.prototype.getPath = function (u) {
u = path.join('/', url.parse(u).pathname).replace(/\\/g, '/')
if (u.indexOf(this.url) !== 0) return false;
// /a/b/c mounted on /path/to/z/d/x
// /a/b/c/d --> /path/to/z/d/x/d
u = u.substr(this.url.length)
if (u.charAt(0) !== '/') u = '/' + u
var p = path.join(this.path, u)
return p
}
// get a url from a path
Mount.prototype.getUrl = function (p) {
p = path.resolve(p)
if (p.indexOf(this.path) !== 0) return false;
p = path.join('/', p.substr(this.path.length))
var u = path.join(this.url, p).replace(/\\/g, '/')
return u
}
Mount.prototype.serve = function (req, res, next) {
if (req.method !== 'HEAD' && req.method !== 'GET') {
if (typeof next === 'function') next()
return false
}
// querystrings are of no concern to us
req.url = url.parse(req.url).pathname
var p = this.getPath(req.url)
if (!p) {
if (typeof next === 'function') next()
return false;
}
// don't allow dot-urls by default, unless explicitly allowed.
if (!this.opt.dot && p.match(/(^|\/)\./)) {
res.statusCode = 403
res.end('Forbidden')
return true
}
// now we have a path. check for the fd.
this.cache.fd.get(p, function (er, fd) {
// inability to open is some kind of error, probably 404
if (er) return this.error(er, res)
this.cache.stat.get(fd+':'+p, function (er, stat) {
if (er) return this.error(er, res);
var ims = req.headers['if-modified-since']
if (ims) ims = new Date(ims).getTime();
if (ims && ims >= stat.mtime.getTime()) {
res.statusCode = 304
res.end()
return
}
var etag = getEtag(stat)
if (req.headers['if-none-match'] === etag) {
res.statusCode = 304
res.end()
return
}
res.setHeader('cache-control', 'public')
res.setHeader('last-modified', stat.mtime.toUTCString())
res.setHeader('etag', etag)
if (stat.isDirectory()) {
return this.index(p, req, res)
}
return this.file(p, fd, stat, etag, req, res)
}.bind(this));
}.bind(this));
return true;
}
Mount.prototype.error = function (er, res) {
res.statusCode = typeof er === 'number' ? er
: er.code === 'ENOENT' || er.code === 'EISDIR' ? 404
: er.code === 'EPERM' || er.code === 'EACCES' ? 403
: 500;
if (typeof res.error === 'function') {
// pattern of express and ErrorPage
return res.error(res.statusCode, er)
}
res.setHeader('content-type', 'text/plain')
res.end(http.STATUS_CODES[res.statusCode] + '\n')
}
Mount.prototype.index = function (p, req, res) {
if (this._index === true) {
return this.autoindex(p, req, res)
}
if (typeof this._index === 'string') {
if (!/\/$/.test(req.url)) req.url += '/'
req.url += this._index
return this.serve(req, res)
}
return this.error(404, res)
}
Mount.prototype.autoindex = function (p, req, res) {
if (!/\/$/.exec(req.url)) {
res.statusCode = 301
res.setHeader('location', req.url + '/')
res.end('Moved: ' + req.url + '/')
return
}
this.cache.index.get(p, function (er, html) {
if (er) return this.error(er, res)
res.statusCode = 200
res.setHeader('content-type', 'text/html')
res.setHeader('content-length', html.length)
res.end(html)
}.bind(this));
}
Mount.prototype.file = function (p, fd, stat, etag, req, res) {
var key = fd + ':' + stat.size + ':' + etag
var mt = mime.lookup(path.extname(p))
if (mt !== 'application/octet-stream') {
res.setHeader('content-type', mt)
}
// only use the content cache if it will actually fit there.
if (this.cache.content.has(key)) {
this.cachedFile(p, fd, stat, etag, req, res)
} else {
this.streamFile(p, fd, stat, etag, req, res)
}
}
Mount.prototype.cachedFile = function (p, fd, stat, etag, req, res) {
var key = fd + ':' + stat.size + ':' + etag
var gz = getGz(p, req)
this.cache.content.get(key, function (er, content) {
if (er) return this.error(er, res)
res.statusCode = 200
if (gz && content.gz) {
res.setHeader('content-encoding', 'gzip')
res.setHeader('content-length', content.gz.length)
res.end(content.gz)
} else {
res.setHeader('content-length', content.length)
res.end(content)
}
}.bind(this));
}
Mount.prototype.streamFile = function (p, fd, stat, etag, req, res) {
var streamOpt = { fd: fd, start: 0, end: stat.size }
var stream = fs.createReadStream(p, streamOpt)
stream.destroy = function () {}
// make sure it knows that we're using this right now.
streaming[p] = fd
// too late to effectively handle any errors.
// just kill the connection if that happens.
stream.on('error', function(e) {
console.error('Error serving %s\n%s', p, e.stack || e.message)
res.socket.destroy()
})
if (res.filter) {
stream = stream.pipe(res.filter)
}
var gzstr = zlib.Gzip()
var gz = getGz(p, req)
res.statusCode = 200
stream.pipe(gzstr)
if (gz) {
// we don't know how long it'll be, since it will be compressed.
res.setHeader('content-encoding', 'gzip')
gzstr.pipe(res)
} else {
if (!res.filter) res.setHeader('content-length', stat.size)
stream.pipe(res)
}
stream.on('end', function () {
delete streaming[p]
if (!pendingClose[p])
return
fs.close(fd, function () {})
delete pendingClose[p]
})
if (this.cache.content._cache.max > stat.size) {
// collect it, and put it in the cache
var key = fd + ':' + stat.size + ':' + etag
var bufs = []
var gzbufs = []
stream.on('data', function (c) {
bufs.push(c)
})
gzstr.on('data', function (c) {
gzbufs.push(c)
})
gzstr.on('end', function () {
var content = Buffer.concat(bufs)
content.gz = Buffer.concat(gzbufs)
this.cache.content.set(key, content)
}.bind(this))
}
}
// cache-fillers
Mount.prototype._loadIndex = function (p, cb) {
// truncate off the first bits
var url = p.substr(this.path.length).replace(/\\/g, '/')
var str =
'<!doctype html>' +
'<html>' +
'<head><title>Index of ' + url + '</title></head>' +
'<body>' +
'<h1>Index of ' + url + '</h1>' +
'<hr><pre><a href="../">../</a>\n'
this.cache.readdir.get(p, function (er, data) {
if (er) return cb(er)
var nameLen = 0
var sizeLen = 0
Object.keys(data).map(function (f) {
var d = data[f]
var name = f.replace(/"/g, '"')
if (d.size === '-') name += '/'
var showName = name.replace(/^(.{40}).{3,}$/, '$1..>')
nameLen = Math.max(nameLen, showName.length)
sizeLen = Math.max(sizeLen, ('' + d.size).length)
return [ '<a href="' + name + '">' + showName + '</a>',
d.mtime, d.size, showName ];
}).sort(function (a, b) {
return a[2] === '-' && b[2] !== '-' ? -1 // dirs first
: a[2] !== '-' && b[2] === '-' ? 1
: a[0].toLowerCase() < b[0].toLowerCase() ? -1 // then alpha
: a[0].toLowerCase() > b[0].toLowerCase() ? 1
: 0
}).forEach(function (line) {
var namePad = new Array(8 + nameLen - line[3].length).join(' ')
var sizePad = new Array(8 + sizeLen - ('' + line[2]).length).join(' ')
str += line[0] + namePad +
line[1].toISOString() +
sizePad + line[2] + '\n'
})
str += '</pre><hr></body></html>'
cb(null, new Buffer(str))
});
}
Mount.prototype._loadReaddir = function (p, cb) {
var len
var data
fs.readdir(p, function (er, files) {
if (er) return cb(er)
files = files.filter(function (f) {
if (!this.opt.dot) return !/^\./.test(f)
else return f !== '.' && f !== '..'
}.bind(this))
len = files.length
data = {}
files.forEach(function (file) {
var pf = path.join(p, file)
this.cache.stat.get(pf, function (er, stat) {
if (er) return cb(er)
if (stat.isDirectory()) stat.size = '-'
data[file] = stat
next()
}.bind(this))
}.bind(this))
}.bind(this))
function next () {
if (--len === 0) cb(null, data)
}
}
Mount.prototype._loadStat = function (key, cb) {
// key is either fd:path or just a path
var fdp = key.match(/^(\d+):(.*)/)
if (fdp) {
var fd = +fdp[1]
var p = fdp[2]
fs.fstat(fd, function (er, stat) {
if (er) return cb(er)
this.cache.stat.set(p, stat)
cb(null, stat)
}.bind(this))
} else {
fs.stat(key, cb)
}
}
Mount.prototype._loadFd = function (path, cb) {
// if we were about to close it, just don't.
fs.open(path, 'r', function (er, fd) {
if (!er)
streaming[path] = fd
cb(er, fd)
})
}
Mount.prototype._loadContent = function (key, cb) {
// this function should never be called.
// we check if the thing is in the cache, and if not, stream it in
// manually. this.cache.content.get() should not ever happen.
throw new Error('This should not ever happen')
}
function getEtag (s) {
return '"' + s.dev + '-' + s.ino + '-' + s.mtime.getTime() + '"'
}
function getGz (p,req) {
var gz = false
if (!/\.t?gz$/.exec(p)) {
var neg = req.negotiator || new Neg(req)
gz = neg.preferredEncoding(['gzip', 'identity']) === 'gzip'
}
return gz
}