Skip to content

Commit 38ba617

Browse files
committedApr 15, 2019
chore: walk directory manually;
- no need for tglob bcuz all files (no pattern)
1 parent cab8a3a commit 38ba617

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed
 

‎packages/sirv/index.js

+28-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const fs = require('fs');
22
const { join, resolve } = require('path');
3-
const tglob = require('tiny-glob/sync');
43
const parser = require('@polka/url');
54
const mime = require('mime/lite');
65

@@ -9,7 +8,9 @@ const noop = () => {};
98

109
function toAssume(uri, extns) {
1110
let i=0, x, len=uri.length - 1;
12-
if (uri.charCodeAt(len) === 47) uri=uri.substring(0, len);
11+
if (uri.charCodeAt(len) === 47) {
12+
uri = uri.substring(0, len);
13+
}
1314

1415
let arr=[], tmp=`${uri}/index`;
1516
for (; i < extns.length; i++) {
@@ -30,14 +31,22 @@ function find(uri, extns) {
3031
return data;
3132
}
3233

33-
function toEtag(obj) {
34-
return `W/"${obj.size.toString(16)}-${obj.mtime.getTime().toString(16)}"`;
35-
}
36-
3734
function is404(res) {
3835
return (res.statusCode=404,res.end());
3936
}
4037

38+
function list(dir, fn, pre='') {
39+
let i=0, abs, stats;
40+
let arr = fs.readdirSync(dir);
41+
for (; i < arr.length; i++) {
42+
abs = join(dir, arr[i]);
43+
stats = fs.statSync(abs);
44+
stats.isDirectory()
45+
? list(abs, fn, join(pre, arr[i]))
46+
: fn(join(pre, arr[i]), abs, stats);
47+
}
48+
}
49+
4150
module.exports = function (dir, opts={}) {
4251
dir = resolve(dir || '.');
4352

@@ -59,20 +68,21 @@ module.exports = function (dir, opts={}) {
5968
let cc = opts.maxAge != null && `public,max-age=${opts.maxAge}`;
6069
if (cc && opts.immutable) cc += ',immutable';
6170

62-
opts.cwd = dir;
63-
let abs, stats, headers;
64-
opts.dot = !!opts.dotfiles;
65-
tglob('**/*.*', opts).forEach(str => {
66-
abs = join(dir, str);
67-
stats = fs.statSync(abs);
68-
headers = {
71+
list(dir, (name, abs, stats) => {
72+
if (!opts.dotfiles && name.charAt(0) === '.') {
73+
return;
74+
}
75+
76+
let headers = {
6977
'Content-Length': stats.size,
70-
'Content-Type': mime.getType(str),
71-
'Last-Modified': stats.mtime.toUTCString()
78+
'Content-Type': mime.getType(name),
79+
'Last-Modified': stats.mtime.toUTCString(),
7280
};
81+
7382
if (cc) headers['Cache-Control'] = cc;
74-
if (opts.etag) headers['ETag'] = toEtag(stats);
75-
FILES['/' + str.replace(/\\+/g, '/')] = { abs, stats, headers };
83+
if (opts.etag) headers['ETag'] = `W/"${stats.size}-${stats.mtime.getTime()}"`;
84+
85+
FILES['/' + name.replace(/\\+/g, '/')] = { abs, stats, headers };
7686
});
7787

7888
return function (req, res, next) {
@@ -84,5 +94,5 @@ module.exports = function (dir, opts={}) {
8494
res.writeHead(200, data.headers);
8595

8696
fs.createReadStream(data.abs).pipe(res);
87-
}
97+
};
8898
}

‎packages/sirv/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
},
1818
"dependencies": {
1919
"@polka/url": "^0.5.0",
20-
"mime": "^2.3.1",
21-
"tiny-glob": "^0.2.0"
20+
"mime": "^2.3.1"
2221
}
2322
}

0 commit comments

Comments
 (0)
Please sign in to comment.