Skip to content

Commit 14a25ac

Browse files
committed
Adding an option to surpress or modify the non-existent root directory warning (#524)
1 parent e76497f commit 14a25ac

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ An array of directories can be provided to serve multiple static directories
148148
under a single prefix. Files are served in a "first found, first served" manner,
149149
so list directories in order of priority. Duplicate paths will raise an error.
150150
151+
#### `getPathNotFoundWarning`
152+
153+
A custom function that returns a warning message to log when a specified root directory is not found.
154+
It receives a single argument: the path of the missing root directory.
155+
The function should return a string describing the missing path.
156+
151157
#### `prefix`
152158
153159
Default: `'/'`

index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ send.mime.default_type = 'application/octet-stream'
1919

2020
async function fastifyStatic (fastify, opts) {
2121
opts.root = normalizeRoot(opts.root)
22-
checkRootPathForErrors(fastify, opts.root)
22+
checkRootPathForErrors(fastify, opts.root, opts.getPathNotFoundWarning);
2323

2424
const setHeaders = opts.setHeaders
2525
if (setHeaders !== undefined && typeof setHeaders !== 'function') {
@@ -417,7 +417,7 @@ function normalizeRoot (root) {
417417
return root
418418
}
419419

420-
function checkRootPathForErrors (fastify, rootPath) {
420+
function checkRootPathForErrors (fastify, rootPath, getPathNotFoundWarning) {
421421
if (rootPath === undefined) {
422422
throw new Error('"root" option is required')
423423
}
@@ -434,18 +434,18 @@ function checkRootPathForErrors (fastify, rootPath) {
434434
}
435435

436436
// check each path and fail at first invalid
437-
rootPath.map((path) => checkPath(fastify, path))
437+
rootPath.map((path) => checkPath(fastify, path, getPathNotFoundWarning))
438438
return
439439
}
440440

441441
if (typeof rootPath === 'string') {
442-
return checkPath(fastify, rootPath)
442+
return checkPath(fastify, rootPath, getPathNotFoundWarning)
443443
}
444444

445445
throw new Error('"root" option must be a string or array of strings')
446446
}
447447

448-
function checkPath (fastify, rootPath) {
448+
function checkPath (fastify, rootPath, getPathNotFoundWarning) {
449449
if (typeof rootPath !== 'string') {
450450
throw new TypeError('"root" option must be a string')
451451
}
@@ -459,7 +459,11 @@ function checkPath (fastify, rootPath) {
459459
pathStat = statSync(rootPath)
460460
} catch (e) {
461461
if (e.code === 'ENOENT') {
462-
fastify.log.warn(`"root" path "${rootPath}" must exist`)
462+
const warningMessage =
463+
getPathNotFoundWarning
464+
? getPathNotFoundWarning(rootPath)
465+
: `"root" path "${rootPath}" must exist`
466+
fastify.log.warn(warningMessage)
463467
return
464468
}
465469

test/static.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,30 @@ test('root not found warning', async (t) => {
11231123
destination.end()
11241124
})
11251125

1126+
test('custom root not found warning', async (t) => {
1127+
t.plan(1)
1128+
const rootPath = path.join(__dirname, 'does-not-exist')
1129+
const pluginOptions = {
1130+
root: rootPath,
1131+
getPathNotFoundWarning: (path) => `CUSTOM "root" path "${path}" must exist`,
1132+
}
1133+
const destination = concat((data) => {
1134+
t.assert.deepStrictEqual(JSON.parse(data).msg, `CUSTOM "root" path "${rootPath}" must exist`)
1135+
})
1136+
const loggerInstance = pino(
1137+
{
1138+
level: 'warn'
1139+
},
1140+
destination
1141+
)
1142+
const fastify = Fastify({ loggerInstance })
1143+
fastify.register(fastifyStatic, pluginOptions)
1144+
1145+
await fastify.listen({ port: 0 })
1146+
fastify.server.unref()
1147+
destination.end()
1148+
})
1149+
11261150
test('send options', (t) => {
11271151
t.plan(12)
11281152
const pluginOptions = {

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ declare namespace fastifyStatic {
8686

8787
export interface FastifyStaticOptions extends SendOptions {
8888
root: string | string[] | URL | URL[];
89+
getPathNotFoundWarning?: (path: string) => string;
8990
prefix?: string;
9091
prefixAvoidTrailingSlash?: boolean;
9192
serve?: boolean;

0 commit comments

Comments
 (0)