From 36c4a2da4493dfe7c4d10332570b961be6e7667c Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Sat, 23 Mar 2019 18:11:34 -0400 Subject: [PATCH] Log warning if root directory does not exist instead of throwing error. It is still an error of the root path exists but is not a directory. Fixes #101 --- index.js | 7 ++++--- package.json | 1 + test/static.test.js | 30 +++++++++++++++--------------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 84bca23c..6a54898d 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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') } @@ -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 diff --git a/package.json b/package.json index 2367a83b..f25afcc9 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/static.test.js b/test/static.test.js index 63ecabf1..9160d122 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -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('../') @@ -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() }) }) @@ -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) @@ -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 }