Socket
Socket
Sign inDemoInstall

fastify-static

Package Overview
Dependencies
19
Maintainers
6
Versions
55
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.4.1 to 0.5.0

25

index.js

@@ -23,3 +23,20 @@ 'use strict'

const root = opts.root
const setHeaders = opts.setHeaders
if (setHeaders !== undefined && typeof setHeaders !== 'function') {
return next(new TypeError('The `setHeaders` option must be a function'))
}
const sendOptions = {
root: opts.root,
acceptRanges: opts.acceptRanges,
cacheControl: opts.cacheControl,
dotfiles: opts.dotfiles,
etag: opts.etag,
extensions: opts.extensions,
immutable: opts.immutable,
index: opts.index,
lastModified: opts.lastModified,
maxAge: opts.maxAge
}
const page500 = opts.page500Path || DEFAULT_500_PAGE

@@ -45,4 +62,8 @@ const page403 = opts.page403Path || DEFAULT_403_PAGE

function pumpSendToReply (req, reply, pathname) {
const sendStream = send(req, pathname, { root })
const sendStream = send(req, pathname, sendOptions)
if (setHeaders !== undefined) {
sendStream.on('headers', setHeaders)
}
sendStream.on('error', function (err) {

@@ -49,0 +70,0 @@ if (err.statusCode === 404) return serve404(req, reply.res)

9

package.json
{
"name": "fastify-static",
"version": "0.4.1",
"version": "0.5.0",
"description": "Plugin for serving static files as fast as possible.",

@@ -28,3 +28,3 @@ "main": "index.js",

"dependencies": {
"fastify-plugin": "^0.1.0",
"fastify-plugin": "^0.2.1",
"send": "^0.16.0"

@@ -34,9 +34,10 @@ },

"coveralls": "^3.0.0",
"fastify": "^0.35.0",
"fastify": "^0.36.0",
"pre-commit": "^1.2.2",
"proxyquire": "^1.8.0",
"request": "^2.81.0",
"snazzy": "^7.0.0",
"standard": "^10.0.3",
"tap": "^10.7.2"
"tap": "^11.0.0"
}
}

@@ -30,4 +30,50 @@ # fastify-static

### Options
#### `root` (required)
The absolute path of the directory that contains the files to serve.
The file to serve will be determined by combining `req.url` with the
provided root directory.
#### `prefix`
Default: `'/'`
A URL path prefix used to create a virtual mount path for the static directory.
#### `page404Path`, `page403Path`, `page500Path`
The absolute path to an HTML file to send as a response for the corresponding
error status code. A generic error page is sent by default.
#### `setHeaders`
Default: `undefined`
A function to set custom headers on the response. Alterations to the headers
must be done synchronously. The function is called as `fn(res, path, stat)`,
where the arguments are:
- `res` The response object.
- `path` The path of the file that is being sent.
- `stat` The stat object of the file that is being sent.
#### `send` Options
The following options are also supported and will be passed directly to the
[`send`](https://www.npmjs.com/package/send) module:
- [`acceptRanges`](https://www.npmjs.com/package/send#acceptranges)
- [`cacheControl`](https://www.npmjs.com/package/send#cachecontrol)
- [`dotfiles`](https://www.npmjs.com/package/send#dotfiles)
- [`etag`](https://www.npmjs.com/package/send#etag)
- [`extensions`](https://www.npmjs.com/package/send#extensions)
- [`immutable`](https://www.npmjs.com/package/send#immutable)
- [`index`](https://www.npmjs.com/package/send#index)
- [`lastModified`](https://www.npmjs.com/package/send#lastmodified)
- [`maxAge`](https://www.npmjs.com/package/send#maxage)
## License
Licensed under [MIT](./LICENSE)

@@ -267,3 +267,3 @@ 'use strict'

t.test('send', t => {
t.test('sendFile', t => {
t.plan(2)

@@ -287,3 +287,3 @@

t.test('reply.send()', t => {
t.test('reply.sendFile()', t => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)

@@ -311,5 +311,72 @@ request.get({

t.test('errors', t => {
t.test('send options', t => {
t.plan(11)
const pluginOptions = {
root: path.join(__dirname, '/static'),
acceptRanges: 'acceptRanges',
cacheControl: 'cacheControl',
dotfiles: 'dotfiles',
etag: 'etag',
extensions: 'extensions',
immutable: 'immutable',
index: 'index',
lastModified: 'lastModified',
maxAge: 'maxAge'
}
const fastify = require('fastify')({logger: false})
const fastifyStatic = require('proxyquire')('../', {
send: function sendStub (req, pathName, options) {
t.strictEqual(pathName, '/index.html')
t.strictEqual(options.root, path.join(__dirname, '/static'))
t.strictEqual(options.acceptRanges, 'acceptRanges')
t.strictEqual(options.cacheControl, 'cacheControl')
t.strictEqual(options.dotfiles, 'dotfiles')
t.strictEqual(options.etag, 'etag')
t.strictEqual(options.extensions, 'extensions')
t.strictEqual(options.immutable, 'immutable')
t.strictEqual(options.index, 'index')
t.strictEqual(options.lastModified, 'lastModified')
t.strictEqual(options.maxAge, 'maxAge')
return { on: () => {}, pipe: () => {} }
}
})
fastify.register(fastifyStatic, pluginOptions)
fastify.inject({url: '/index.html'})
})
t.test('setHeaders option', t => {
t.plan(6 + GENERIC_RESPONSE_CHECK_COUNT)
const pluginOptions = {
root: path.join(__dirname, 'static'),
setHeaders: function (res, pathName) {
t.strictEqual(pathName, path.join(__dirname, 'static/index.html'))
res.setHeader('X-Test-Header', 'test')
}
}
const fastify = require('fastify')()
fastify.register(fastifyStatic, pluginOptions)
fastify.listen(0, err => {
t.error(err)
fastify.server.unref()
request.get({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/index.html',
followRedirect: false
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['x-test-header'], 'test')
t.strictEqual(body, indexContent)
genericResponseChecks(t, response)
})
})
})
t.test('errors', t => {
t.plan(12)
t.test('no root', t => {

@@ -413,2 +480,11 @@ t.plan(1)

})
t.test('setHeaders is not a function', t => {
t.plan(1)
const pluginOptions = { root: __dirname, setHeaders: 'headers' }
const fastify = require('fastify')({logger: false})
fastify.register(fastifyStatic, pluginOptions, err => {
t.equal(err.constructor, TypeError)
})
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc