fastify-static
Advanced tools
Comparing version 0.2.1 to 0.3.0
46
index.js
@@ -15,4 +15,9 @@ 'use strict' | ||
function fastifyStatic (fastify, opts, next) { | ||
const error = checkOptions(opts) | ||
if (error instanceof Error) return next(error) | ||
const error = checkPathsForErrors({ | ||
root: opts.root, | ||
page500Path: opts.page500Path, | ||
page403Path: opts.page403Path, | ||
page404Path: opts.page404Path | ||
}) | ||
if (error !== undefined) return next(error) | ||
@@ -40,3 +45,3 @@ const root = opts.root | ||
function pumpSendToReply (req, reply, pathname) { | ||
const sendStream = send(req, pathname, {root}) | ||
const sendStream = send(req, pathname, { root }) | ||
@@ -52,4 +57,5 @@ sendStream.on('error', function (err) { | ||
if (!opts.prefix) opts.prefix = '/' | ||
if (opts.prefix === undefined) opts.prefix = '/' | ||
const prefix = opts.prefix[opts.prefix.length - 1] === '/' ? opts.prefix : (opts.prefix + '/') | ||
fastify.get(prefix + '*', function (req, reply) { | ||
@@ -70,17 +76,27 @@ pumpSendToReply(req.req, reply, '/' + req.params['*']) | ||
function checkOptions (opts) { | ||
if (typeof opts.root !== 'string') { | ||
return new Error('"root" option is required') | ||
} | ||
if (!path.isAbsolute(opts.root)) { | ||
return new Error('"root" option must be an absolute path') | ||
} | ||
let rootStat | ||
function checkPathsForErrors (paths) { | ||
if (paths.root === undefined) return new Error('"root" option is required') | ||
var err = checkPath(paths.root, 'root', 'isDirectory') | ||
if (!err && paths.page500Path !== undefined) err = checkPath(paths.page500Path, 'page500Path', 'isFile') | ||
if (!err && paths.page403Path !== undefined) err = checkPath(paths.page403Path, 'page403Path', 'isFile') | ||
if (!err && paths.page404Path !== undefined) err = checkPath(paths.page404Path, 'page404Path', 'isFile') | ||
return err | ||
} | ||
function checkPath (p, pathName, statMethod) { | ||
if (typeof p !== 'string') return new Error(`"${pathName}" option must be a string`) | ||
if (path.isAbsolute(p) === false) return new Error(`"${pathName}" option must be an absolute path`) | ||
let pathStat | ||
try { | ||
rootStat = statSync(opts.root) | ||
pathStat = statSync(p) | ||
} catch (e) { | ||
return e | ||
} | ||
if (!rootStat.isDirectory()) { | ||
return new Error('"root" option must be an absolute path') | ||
if (pathStat[statMethod]() === false) { | ||
return new Error(`${pathName} option must point to a ${statMethod.slice(2).toLowerCase()}`) | ||
} | ||
@@ -87,0 +103,0 @@ } |
{ | ||
"name": "fastify-static", | ||
"version": "0.2.1", | ||
"description": "Plugin for serving static file as fast as possible Edit", | ||
"version": "0.3.0", | ||
"description": "Plugin for serving static files as fast as possible.", | ||
"main": "index.js", | ||
@@ -32,4 +32,4 @@ "scripts": { | ||
"devDependencies": { | ||
"fastify": "^0.29.0", | ||
"coveralls": "^3.0.0", | ||
"fastify": "^0.30.2", | ||
"pre-commit": "^1.2.2", | ||
@@ -36,0 +36,0 @@ "request": "^2.81.0", |
# fastify-static [![Build Status](https://travis-ci.org/fastify/fastify-static.svg?branch=master)](https://travis-ci.org/fastify/fastify-static) [![Greenkeeper badge](https://badges.greenkeeper.io/fastify/fastify-static.svg)](https://greenkeeper.io/) [![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-static/badge.svg)](https://snyk.io/test/github/fastify/fastify-static) | ||
Plugin for serving static file | ||
Plugin for serving static files as fast as possible. | ||
@@ -5,0 +5,0 @@ ## Install |
@@ -308,1 +308,104 @@ 'use strict' | ||
}) | ||
t.test('errors', t => { | ||
t.plan(11) | ||
t.test('no root', t => { | ||
t.plan(1) | ||
const pluginOptions = {} | ||
const fastify = require('fastify')({logger: false}) | ||
fastify.register(fastifyStatic, pluginOptions, err => { | ||
t.equal(err.constructor, Error) | ||
}) | ||
}) | ||
t.test('root is not a string', t => { | ||
t.plan(1) | ||
const pluginOptions = { root: 42 } | ||
const fastify = require('fastify')({logger: false}) | ||
fastify.register(fastifyStatic, pluginOptions, err => { | ||
t.equal(err.constructor, Error) | ||
}) | ||
}) | ||
t.test('root is not an absolute path', t => { | ||
t.plan(1) | ||
const pluginOptions = { root: './my/path' } | ||
const fastify = require('fastify')({logger: false}) | ||
fastify.register(fastifyStatic, pluginOptions, err => { | ||
t.equal(err.constructor, Error) | ||
}) | ||
}) | ||
t.test('root doesn\'t exist', t => { | ||
t.plan(1) | ||
const pluginOptions = { root: path.join(__dirname, 'foo', 'bar') } | ||
const fastify = require('fastify')({logger: false}) | ||
fastify.register(fastifyStatic, pluginOptions, err => { | ||
t.equal(err.constructor, Error) | ||
}) | ||
}) | ||
t.test('root is not a directory', t => { | ||
t.plan(1) | ||
const pluginOptions = { root: __filename } | ||
const fastify = require('fastify')({logger: false}) | ||
fastify.register(fastifyStatic, pluginOptions, err => { | ||
t.equal(err.constructor, Error) | ||
}) | ||
}) | ||
t.test('page500Path is not a string', t => { | ||
t.plan(1) | ||
const pluginOptions = { root: __dirname, page500Path: 42 } | ||
const fastify = require('fastify')({logger: false}) | ||
fastify.register(fastifyStatic, pluginOptions, err => { | ||
t.equal(err.constructor, Error) | ||
}) | ||
}) | ||
t.test('page500Path is not a file', t => { | ||
t.plan(1) | ||
const pluginOptions = { root: __dirname, page500Path: __dirname } | ||
const fastify = require('fastify')({logger: false}) | ||
fastify.register(fastifyStatic, pluginOptions, err => { | ||
t.equal(err.constructor, Error) | ||
}) | ||
}) | ||
t.test('page404Path is not a string', t => { | ||
t.plan(1) | ||
const pluginOptions = { root: __dirname, page404Path: 42 } | ||
const fastify = require('fastify')({logger: false}) | ||
fastify.register(fastifyStatic, pluginOptions, err => { | ||
t.equal(err.constructor, Error) | ||
}) | ||
}) | ||
t.test('page404Path is not a file', t => { | ||
t.plan(1) | ||
const pluginOptions = { root: __dirname, page404Path: __dirname } | ||
const fastify = require('fastify')({logger: false}) | ||
fastify.register(fastifyStatic, pluginOptions, err => { | ||
t.equal(err.constructor, Error) | ||
}) | ||
}) | ||
t.test('page403Path is not a string', t => { | ||
t.plan(1) | ||
const pluginOptions = { root: __dirname, page403Path: 42 } | ||
const fastify = require('fastify')({logger: false}) | ||
fastify.register(fastifyStatic, pluginOptions, err => { | ||
t.equal(err.constructor, Error) | ||
}) | ||
}) | ||
t.test('page403Path is not a file', t => { | ||
t.plan(1) | ||
const pluginOptions = { root: __dirname, page403Path: __dirname } | ||
const fastify = require('fastify')({logger: false}) | ||
fastify.register(fastifyStatic, pluginOptions, err => { | ||
t.equal(err.constructor, Error) | ||
}) | ||
}) | ||
}) |
20443
438