Skip to content

Adding an option to modify the non-existent root directory warning (#524) #526

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ An array of directories can be provided to serve multiple static directories
under a single prefix. Files are served in a "first found, first served" manner,
so list directories in order of priority. Duplicate paths will raise an error.

#### `getPathNotFoundWarning`

A custom function that returns a warning message to log when a specified root directory is not found.
It receives a single argument: the path of the missing root directory.
The function should return a string describing the missing path.

#### `prefix`

Default: `'/'`
Expand Down
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ send.mime.default_type = 'application/octet-stream'

async function fastifyStatic (fastify, opts) {
opts.root = normalizeRoot(opts.root)
checkRootPathForErrors(fastify, opts.root)
checkRootPathForErrors(fastify, opts.root, opts.getPathNotFoundWarning)

const setHeaders = opts.setHeaders
if (setHeaders !== undefined && typeof setHeaders !== 'function') {
Expand Down Expand Up @@ -417,7 +417,7 @@ function normalizeRoot (root) {
return root
}

function checkRootPathForErrors (fastify, rootPath) {
function checkRootPathForErrors (fastify, rootPath, getPathNotFoundWarning) {
if (rootPath === undefined) {
throw new Error('"root" option is required')
}
Expand All @@ -434,18 +434,18 @@ function checkRootPathForErrors (fastify, rootPath) {
}

// check each path and fail at first invalid
rootPath.map((path) => checkPath(fastify, path))
rootPath.map((path) => checkPath(fastify, path, getPathNotFoundWarning))
return
}

if (typeof rootPath === 'string') {
return checkPath(fastify, rootPath)
return checkPath(fastify, rootPath, getPathNotFoundWarning)
}

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

function checkPath (fastify, rootPath) {
function checkPath (fastify, rootPath, getPathNotFoundWarning) {
if (typeof rootPath !== 'string') {
throw new TypeError('"root" option must be a string')
}
Expand All @@ -459,7 +459,11 @@ function checkPath (fastify, rootPath) {
pathStat = statSync(rootPath)
} catch (e) {
if (e.code === 'ENOENT') {
fastify.log.warn(`"root" path "${rootPath}" must exist`)
const warningMessage =
getPathNotFoundWarning
? getPathNotFoundWarning(rootPath)
: `"root" path "${rootPath}" must exist`
fastify.log.warn(warningMessage)
return
}

Expand Down
24 changes: 24 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'

Check failure on line 1 in test/static.test.js

View workflow job for this annotation

GitHub Actions / test / Test (22, macos-latest)

/Users/runner/work/fastify-static/fastify-static/test/static.test.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

/* eslint n/no-deprecated-api: "off" */

Expand Down Expand Up @@ -1123,6 +1123,30 @@
destination.end()
})

test('custom root not found warning', async (t) => {
t.plan(1)
const rootPath = path.join(__dirname, 'does-not-exist')
const pluginOptions = {
root: rootPath,
getPathNotFoundWarning: (path) => `CUSTOM "root" path "${path}" must exist`,
}
const destination = concat((data) => {
t.assert.deepStrictEqual(JSON.parse(data).msg, `CUSTOM "root" path "${rootPath}" must exist`)
})
const loggerInstance = pino(
{
level: 'warn'
},
destination
)
const fastify = Fastify({ loggerInstance })
fastify.register(fastifyStatic, pluginOptions)

await fastify.listen({ port: 0 })
fastify.server.unref()
destination.end()
})

test('send options', (t) => {
t.plan(12)
const pluginOptions = {
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ declare namespace fastifyStatic {

export interface FastifyStaticOptions extends SendOptions {
root: string | string[] | URL | URL[];
getPathNotFoundWarning?: (path: string) => string;
prefix?: string;
prefixAvoidTrailingSlash?: boolean;
serve?: boolean;
Expand Down
Loading