Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update stat behavior #889

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,28 @@ module.exports = function createMiddleware(_dir, _options) {
})
}

function statWithAccess (file, cb) {
fs.stat(file, (err, stat) => {
if (err) {
cb(err);
return;
}
fs.access(file, fs.constants.R_OK, (err) => {
stat.readable = !err;
cb(err, stat);
});
});
}


function statFile() {
try {
fs.stat(file, (err, stat) => {
if (err && (err.code === 'ENOENT' || err.code === 'ENOTDIR')) {
statWithAccess(file, (err, stat) => {
const effectively404 =
(err && (err.code === 'ENOENT' || err.code === 'ENOTDIR')) ||
(!stat || !stat.readable);

if (effectively404) {
if (req.statusCode === 404) {
// This means we're already trying ./404.html and can not find it.
// So send plain text response with 404 status code
Expand Down