-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathfs.js
97 lines (86 loc) · 2.54 KB
/
fs.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
/**
* Copyright 2015 Google Inc. All Rights Reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file or at
* https://github.com/firebase/superstatic/blob/master/LICENSE
*/
'use strict';
var crypto = require('crypto');
var fs = require('fs');
var pathjoin = require('join-path');
var RSVP = require('rsvp');
var _ = require('lodash');
var statPromise = RSVP.denodeify(fs.stat);
var multiStat = function(paths) {
var pathname = paths.shift();
return statPromise(pathname).then(function(stat) {
stat.path = pathname;
return stat;
}, function(err) {
if (paths.length) {
return multiStat(paths);
}
return RSVP.reject(err);
});
};
module.exports = function(options) {
var etagCache = {};
var cwd = options.cwd || process.cwd();
var publicPaths = options.public || ['.'];
if (!_.isArray(publicPaths)) {
publicPaths = [publicPaths];
}
function _fetchEtag(pathname, stat) {
return new RSVP.Promise(function(resolve, reject) {
var cached = etagCache[pathname];
if (cached && cached.timestamp === stat.mtime) {
return resolve(cached.value);
}
// the file you want to get the hash
var fd = fs.createReadStream(pathname);
var hash = crypto.createHash('md5');
hash.setEncoding('hex');
fd.on('error', function(err) {
reject(err);
});
fd.on('end', function() {
hash.end();
var etag = hash.read();
etagCache[pathname] = {
timestamp: stat.mtime,
value: etag
};
resolve(etag);
});
// read all file and pipe it (write it) to the hash object
return fd.pipe(hash);
});
}
return function(req, pathname) {
pathname = decodeURI(pathname);
// jumping to parent directories is not allowed
if (pathname.indexOf('../') >= 0) {
return RSVP.resolve(null);
}
var result = {};
var foundPath;
var fullPathnames = publicPaths.map(function(p) {
return pathjoin(cwd, p, pathname);
});
return multiStat(fullPathnames).then(function(stat) {
foundPath = stat.path;
result.modified = stat.mtime.getTime();
result.size = stat.size;
return _fetchEtag(stat.path, stat);
}).then(function(etag) {
result.etag = etag;
result.stream = fs.createReadStream(foundPath);
return result;
}).catch(function(err) {
if (err.code === 'ENOENT' || err.code === 'ENOTDIR' || err.code === 'EISDIR') {
return null;
}
return RSVP.reject(err);
});
};
};