From 8d52b860d19ff6de3f341e91a746cee5a6f5fda2 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Tue, 10 Sep 2024 18:49:00 -0400 Subject: [PATCH] fix: update stat behavior --- lib/core/index.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/core/index.js b/lib/core/index.js index 920e55b9..23f273c7 100644 --- a/lib/core/index.js +++ b/lib/core/index.js @@ -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