Skip to content
Merged
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
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const send = require('send')
const fp = require('fastify-plugin')

function fastifyStatic (fastify, opts, next) {
const error = checkRootPathForErrors(opts.root)
const error = checkRootPathForErrors(fastify, opts.root)
if (error !== undefined) return next(error)

const setHeaders = opts.setHeaders
Expand Down Expand Up @@ -163,7 +163,7 @@ function fastifyStatic (fastify, opts, next) {
next()
}

function checkRootPathForErrors (rootPath) {
function checkRootPathForErrors (fastify, rootPath) {
if (rootPath === undefined) {
return new Error('"root" option is required')
}
Expand All @@ -180,7 +180,8 @@ function checkRootPathForErrors (rootPath) {
pathStat = statSync(rootPath)
} catch (e) {
if (e.code === 'ENOENT') {
return new Error(`"root" path "${rootPath}" must exist`)
fastify.log.warn(`"root" path "${rootPath}" must exist`)
return
}

return e
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"devDependencies": {
"@types/node": "^10.12.27",
"concat-stream": "^2.0.0",
"coveralls": "^3.0.3",
"eslint-plugin-typescript": "^0.14.0",
"fastify": "^2.0.0",
Expand Down
30 changes: 15 additions & 15 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const t = require('tap')
const simple = require('simple-get')
const Fastify = require('fastify')
const compress = require('fastify-compress')
const concat = require('concat-stream')
const pino = require('pino')

const fastifyStatic = require('../')

Expand Down Expand Up @@ -592,14 +594,22 @@ t.test('prefix default', t => {
t.doesNotThrow(() => fastify.register(fastifyStatic, pluginOptions))
})

t.test('root not found error', t => {
t.plan(1)
t.test('root not found warning', t => {
t.plan(2)
const rootPath = path.join(__dirname, 'does-not-exist')
const pluginOptions = { root: rootPath }
const fastify = Fastify({ logger: false })
const destination = concat(data => {
t.equal(JSON.parse(data).msg, `"root" path "${rootPath}" must exist`)
})
const logger = pino({
level: 'warn'
}, destination)
const fastify = Fastify({ logger: logger })
fastify.register(fastifyStatic, pluginOptions)
fastify.listen(0, err => {
t.equal(err.message, `"root" path "${rootPath}" must exist`)
t.error(err)
fastify.server.unref()
destination.end()
})
})

Expand Down Expand Up @@ -673,7 +683,7 @@ t.test('setHeaders option', t => {
})

t.test('errors', t => {
t.plan(6)
t.plan(5)

t.test('no root', t => {
t.plan(1)
Expand Down Expand Up @@ -705,16 +715,6 @@ t.test('errors', t => {
})
})

t.test('root doesn\'t exist', t => {
t.plan(1)
const pluginOptions = { root: path.join(__dirname, 'foo', 'bar') }
const fastify = Fastify({ logger: false })
fastify.register(fastifyStatic, pluginOptions)
.ready(err => {
t.equal(err.constructor, Error)
})
})

t.test('root is not a directory', t => {
t.plan(1)
const pluginOptions = { root: __filename }
Expand Down