|
| 1 | +diff --git a/build.js b/build.js |
| 2 | +index 0c6ed8e2c0949c93978dd1a244baa9bf2448e9b8..08a9347cfdca06e6a97077ea4582c5b0922ecb2d 100644 |
| 3 | +--- a/build.js |
| 4 | ++++ b/build.js |
| 5 | +@@ -35,7 +35,7 @@ function viaCache(cache, uri, extns) { |
| 6 | + } |
| 7 | + } |
| 8 | + |
| 9 | +-function viaLocal(dir, isEtag, uri, extns) { |
| 10 | ++function viaLocal(dir, isEtag, uri, extns, shouldServe) { |
| 11 | + let i=0, arr=toAssume(uri, extns); |
| 12 | + let abs, stats, name, headers; |
| 13 | + for (; i < arr.length; i++) { |
| 14 | +@@ -43,6 +43,7 @@ function viaLocal(dir, isEtag, uri, extns) { |
| 15 | + if (abs.startsWith(dir) && fs.existsSync(abs)) { |
| 16 | + stats = fs.statSync(abs); |
| 17 | + if (stats.isDirectory()) continue; |
| 18 | ++ if (shouldServe && !shouldServe(abs)) continue; |
| 19 | + headers = toHeaders(name, stats, isEtag); |
| 20 | + headers['Cache-Control'] = isEtag ? 'no-cache' : 'no-store'; |
| 21 | + return { abs, stats, headers }; |
| 22 | +@@ -176,7 +177,7 @@ module.exports = function (dir, opts={}) { |
| 23 | + catch (err) { /* malform uri */ } |
| 24 | + } |
| 25 | + |
| 26 | +- let data = lookup(pathname, extns) || isSPA && !isMatch(pathname, ignores) && lookup(fallback, extns); |
| 27 | ++ let data = lookup(pathname, extns, opts.shouldServe) || isSPA && !isMatch(pathname, ignores) && lookup(fallback, extns, opts.shouldServe); |
| 28 | + if (!data) return next ? next() : isNotFound(req, res); |
| 29 | + |
| 30 | + if (isEtag && req.headers['if-none-match'] === data.headers['ETag']) { |
| 31 | +diff --git a/build.mjs b/build.mjs |
| 32 | +index 3ad14d45630a8627b93842a04a96465120d3f223..8451277c015b56a7d2cb99aaee3a318d9c0893dd 100644 |
| 33 | +--- a/build.mjs |
| 34 | ++++ b/build.mjs |
| 35 | +@@ -35,7 +35,7 @@ function viaCache(cache, uri, extns) { |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | +-function viaLocal(dir, isEtag, uri, extns) { |
| 40 | ++function viaLocal(dir, isEtag, uri, extns, shouldServe) { |
| 41 | + let i=0, arr=toAssume(uri, extns); |
| 42 | + let abs, stats, name, headers; |
| 43 | + for (; i < arr.length; i++) { |
| 44 | +@@ -43,6 +43,7 @@ function viaLocal(dir, isEtag, uri, extns) { |
| 45 | + if (abs.startsWith(dir) && fs.existsSync(abs)) { |
| 46 | + stats = fs.statSync(abs); |
| 47 | + if (stats.isDirectory()) continue; |
| 48 | ++ if (shouldServe && !shouldServe(abs)) continue; |
| 49 | + headers = toHeaders(name, stats, isEtag); |
| 50 | + headers['Cache-Control'] = isEtag ? 'no-cache' : 'no-store'; |
| 51 | + return { abs, stats, headers }; |
| 52 | +@@ -176,7 +177,7 @@ export default function (dir, opts={}) { |
| 53 | + catch (err) { /* malform uri */ } |
| 54 | + } |
| 55 | + |
| 56 | +- let data = lookup(pathname, extns) || isSPA && !isMatch(pathname, ignores) && lookup(fallback, extns); |
| 57 | ++ let data = lookup(pathname, extns, opts.shouldServe) || isSPA && !isMatch(pathname, ignores) && lookup(fallback, extns, opts.shouldServe); |
| 58 | + if (!data) return next ? next() : isNotFound(req, res); |
| 59 | + |
| 60 | + if (isEtag && req.headers['if-none-match'] === data.headers['ETag']) { |
| 61 | +diff --git a/index.d.mts b/index.d.mts |
| 62 | +index 8bfe364f1db2d1382c56a9b75a014579083cfa70..a8dfa1c473ff15c979bbfbc28c3630a12e222c3a 100644 |
| 63 | +--- a/index.d.mts |
| 64 | ++++ b/index.d.mts |
| 65 | +@@ -24,6 +24,8 @@ export interface Options { |
| 66 | + gzip?: boolean; |
| 67 | + onNoMatch?: (req: IncomingMessage, res: ServerResponse) => void; |
| 68 | + setHeaders?: (res: ServerResponse, pathname: string, stats: Stats) => void; |
| 69 | ++ /** patched */ |
| 70 | ++ shouldServe?: (absoluteFilePath: string) => void; |
| 71 | + } |
| 72 | + |
| 73 | + export default function (dir?: string, opts?: Options): RequestHandler; |
| 74 | +diff --git a/index.d.ts b/index.d.ts |
| 75 | +index 96cc63e7ee1ccdd7266e9193252b799068ef2e3c..9e298d627002cd0b073073aa13528b7492541b5b 100644 |
| 76 | +--- a/index.d.ts |
| 77 | ++++ b/index.d.ts |
| 78 | +@@ -25,6 +25,8 @@ declare namespace sirv { |
| 79 | + gzip?: boolean; |
| 80 | + onNoMatch?: (req: IncomingMessage, res: ServerResponse) => void; |
| 81 | + setHeaders?: (res: ServerResponse, pathname: string, stats: Stats) => void; |
| 82 | ++ /** patched */ |
| 83 | ++ shouldServe?: (absoluteFilePath: string) => void; |
| 84 | + } |
| 85 | + } |
| 86 | + |
0 commit comments