Skip to content

Commit 7b5ad30

Browse files
committed
fix
If serve is set explicitly to false then root is required If root is set, it must be validated
1 parent c1a7904 commit 7b5ad30

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ send.mime.default_type = 'application/octet-stream'
1919

2020
/** @type {import("fastify").FastifyPluginAsync<import("./types").FastifyStaticOptions>} */
2121
async function fastifyStatic (fastify, opts) {
22-
if (opts.serve !== false) {
22+
if (opts.serve !== false || opts.root !== undefined) {
2323
opts.root = normalizeRoot(opts.root)
2424
checkRootPathForErrors(fastify, opts.root)
2525
}

test/static.test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,7 @@ test('maxAge option', async (t) => {
12601260
})
12611261

12621262
test('errors', async (t) => {
1263-
t.plan(11)
1263+
t.plan(12)
12641264

12651265
await t.test('no root', async (t) => {
12661266
t.plan(1)
@@ -1325,6 +1325,16 @@ test('errors', async (t) => {
13251325
await t.assert.rejects(async () => await fastify.register(fastifyStatic, pluginOptions))
13261326
})
13271327

1328+
await t.test('no root and serve: false', async (t) => {
1329+
t.plan(1)
1330+
const pluginOptions = {
1331+
serve: false,
1332+
root: []
1333+
}
1334+
const fastify = Fastify({ logger: false })
1335+
await t.assert.rejects(async () => await fastify.register(fastifyStatic, pluginOptions))
1336+
})
1337+
13281338
await t.test('duplicate root paths are not allowed', async (t) => {
13291339
t.plan(1)
13301340
const pluginOptions = {

0 commit comments

Comments
 (0)