Files

106 lines
4.2 KiB
Diff

diff --git a/build.js b/build.js
index 0884d9ffaa24d0ae3ec020a6faff12b3125b2071..0e2a690abb3bd121f6ba7c0855163b816f5a7a70 100644
--- a/build.js
+++ b/build.js
@@ -1,5 +1,5 @@
const fs = require('fs');
-const { join, normalize, resolve } = require('path');
+const { join, normalize, resolve, sep } = require('path');
const { totalist } = require('totalist/sync');
const { parse } = require('@polka/url');
const { lookup } = require('mrmime');
@@ -35,7 +35,7 @@ function viaCache(cache, uri, extns) {
}
}
-function viaLocal(dir, isEtag, uri, extns) {
+function viaLocal(dir, isEtag, uri, extns, shouldServe) {
let i=0, arr=toAssume(uri, extns);
let abs, stats, name, headers;
for (; i < arr.length; i++) {
@@ -43,6 +43,7 @@ function viaLocal(dir, isEtag, uri, extns) {
if (abs.startsWith(dir) && fs.existsSync(abs)) {
stats = fs.statSync(abs);
if (stats.isDirectory()) continue;
+ if (shouldServe && !shouldServe(abs)) continue;
headers = toHeaders(name, stats, isEtag);
headers['Cache-Control'] = isEtag ? 'no-cache' : 'no-store';
return { abs, stats, headers };
@@ -161,7 +162,7 @@ module.exports = function (dir, opts={}) {
});
}
- let lookup = opts.dev ? viaLocal.bind(0, dir, isEtag) : viaCache.bind(0, FILES);
+ let lookup = opts.dev ? viaLocal.bind(0, dir.endsWith(sep) ? dir : dir + sep, isEtag) : viaCache.bind(0, FILES);
return function (req, res, next) {
let extns = [''];
@@ -176,7 +177,7 @@ module.exports = function (dir, opts={}) {
catch (err) { /* malform uri */ }
}
- let data = lookup(pathname, extns) || isSPA && !isMatch(pathname, ignores) && lookup(fallback, extns);
+ let data = lookup(pathname, extns, opts.shouldServe) || isSPA && !isMatch(pathname, ignores) && lookup(fallback, extns, opts.shouldServe);
if (!data) return next ? next() : isNotFound(req, res);
if (isEtag && req.headers['if-none-match'] === data.headers['ETag']) {
diff --git a/build.mjs b/build.mjs
index c93bbe6bdfb7ad13ee20f0c44d80d6aacdd64087..9127789c792ffb23022524fd2a6f997286afe0cf 100644
--- a/build.mjs
+++ b/build.mjs
@@ -1,5 +1,5 @@
import * as fs from 'fs';
-import { join, normalize, resolve } from 'path';
+import { join, normalize, resolve, sep } from 'path';
import { totalist } from 'totalist/sync';
import { parse } from '@polka/url';
import { lookup } from 'mrmime';
@@ -35,7 +35,7 @@ function viaCache(cache, uri, extns) {
}
}
-function viaLocal(dir, isEtag, uri, extns) {
+function viaLocal(dir, isEtag, uri, extns, shouldServe) {
let i=0, arr=toAssume(uri, extns);
let abs, stats, name, headers;
for (; i < arr.length; i++) {
@@ -43,6 +43,7 @@ function viaLocal(dir, isEtag, uri, extns) {
if (abs.startsWith(dir) && fs.existsSync(abs)) {
stats = fs.statSync(abs);
if (stats.isDirectory()) continue;
+ if (shouldServe && !shouldServe(abs)) continue;
headers = toHeaders(name, stats, isEtag);
headers['Cache-Control'] = isEtag ? 'no-cache' : 'no-store';
return { abs, stats, headers };
@@ -161,7 +162,7 @@ export default function (dir, opts={}) {
});
}
- let lookup = opts.dev ? viaLocal.bind(0, dir, isEtag) : viaCache.bind(0, FILES);
+ let lookup = opts.dev ? viaLocal.bind(0, dir.endsWith(sep) ? dir : dir + sep, isEtag) : viaCache.bind(0, FILES);
return function (req, res, next) {
let extns = [''];
@@ -176,7 +177,7 @@ export default function (dir, opts={}) {
catch (err) { /* malform uri */ }
}
- let data = lookup(pathname, extns) || isSPA && !isMatch(pathname, ignores) && lookup(fallback, extns);
+ let data = lookup(pathname, extns, opts.shouldServe) || isSPA && !isMatch(pathname, ignores) && lookup(fallback, extns, opts.shouldServe);
if (!data) return next ? next() : isNotFound(req, res);
if (isEtag && req.headers['if-none-match'] === data.headers['ETag']) {
diff --git a/sirv.d.ts b/sirv.d.ts
index c05040fc6ec504a1828a7badd39f669981acd0ee..e9597e8b5bf24613a09565f0e13024ae3ca8fa5e 100644
--- a/sirv.d.ts
+++ b/sirv.d.ts
@@ -19,6 +19,8 @@ declare module 'sirv' {
gzip?: boolean;
onNoMatch?: (req: IncomingMessage, res: ServerResponse) => void;
setHeaders?: (res: ServerResponse, pathname: string, stats: Stats) => void;
+ /** patched */
+ shouldServe?: (absoluteFilePath: string) => void;
}
export default function(dir?: string, opts?: Options): RequestHandler;