Socket
Socket
Sign inDemoInstall

fastify-static

Package Overview
Dependencies
Maintainers
6
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-static - npm Package Compare versions

Comparing version 0.9.0 to 0.10.0

example/server-compress.js

46

index.js

@@ -5,2 +5,3 @@ 'use strict'

const statSync = require('fs').statSync
const { PassThrough } = require('readable-stream')

@@ -35,8 +36,29 @@ const send = require('send')

function pumpSendToReply (request, reply, pathname) {
const stream = send(request.req, pathname, sendOptions)
const stream = send(request.raw, pathname, sendOptions)
// this is needed because fastify automatically
// set the type to application/octet-stream
stream.on('headers', removeType)
const wrap = new PassThrough({
flush (cb) {
this.finished = true
cb()
}
})
wrap.getHeader = reply.getHeader.bind(reply)
wrap.setHeader = reply.header.bind(reply)
wrap.socket = request.raw.socket
wrap.finished = false
Object.defineProperty(wrap, 'statusCode', {
get () {
return reply.res.statusCode
},
set (code) {
reply.code(code)
}
})
wrap.on('pipe', function () {
reply.send(wrap)
})
if (setHeaders !== undefined) {

@@ -46,3 +68,11 @@ stream.on('headers', setHeaders)

reply.send(stream)
stream.on('error', function (err) {
if (err) {
reply.send(err)
}
})
// we cannot use pump, because send error
// handling is not compatible
stream.pipe(wrap)
}

@@ -88,9 +118,5 @@

function removeType (res) {
res.setHeader('Content-Type', '')
}
module.exports = fp(fastifyStatic, {
fastify: '>= 0.42.0',
fastify: '>= 1.2.0',
name: 'fastify-static'
})
{
"name": "fastify-static",
"version": "0.9.0",
"version": "0.10.0",
"description": "Plugin for serving static files as fast as possible.",

@@ -30,2 +30,3 @@ "main": "index.js",

"fastify-plugin": "^0.2.1",
"readable-stream": "^2.3.6",
"send": "^0.16.0"

@@ -36,5 +37,6 @@ },

"fastify": "^1.1.1",
"fastify-compress": "^0.5.1",
"pre-commit": "^1.2.2",
"proxyquire": "^2.0.0",
"request": "2.83.0",
"simple-get": "^2.7.0",
"snazzy": "^7.0.0",

@@ -41,0 +43,0 @@ "standard": "^11.0.0",

@@ -7,3 +7,5 @@ 'use strict'

const t = require('tap')
const request = require('request')
const simple = require('simple-get')
const Fastify = require('fastify')
const compress = require('fastify-compress')

@@ -38,3 +40,3 @@ const fastifyStatic = require('../')

}
const fastify = require('fastify')()
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)

@@ -51,9 +53,9 @@

t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, indexContent)
t.strictEqual(body.toString(), indexContent)
genericResponseChecks(t, response)

@@ -65,5 +67,5 @@ })

t.plan(2 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static/index.css'
url: 'http://localhost:' + fastify.server.address().port + '/static/index.css'
}, (err, response, body) => {

@@ -78,9 +80,9 @@ t.error(err)

t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static/'
url: 'http://localhost:' + fastify.server.address().port + '/static/'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, indexContent)
t.strictEqual(body.toString(), indexContent)
genericResponseChecks(t, response)

@@ -92,5 +94,5 @@ })

t.plan(2)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static'
url: 'http://localhost:' + fastify.server.address().port + '/static'
}, (err, response, body) => {

@@ -104,9 +106,9 @@ t.error(err)

t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/purpose/foo.html'
url: 'http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/purpose/foo.html'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, deepContent)
t.strictEqual(body.toString(), deepContent)
genericResponseChecks(t, response)

@@ -118,9 +120,9 @@ })

t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/'
url: 'http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, innerIndex)
t.strictEqual(body.toString(), innerIndex)
genericResponseChecks(t, response)

@@ -132,5 +134,5 @@ })

t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static/this/path/doesnt/exist.html',
url: 'http://localhost:' + fastify.server.address().port + '/static/this/path/doesnt/exist.html',
followRedirect: false

@@ -146,5 +148,5 @@ }, (err, response, body) => {

t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static/../index.js',
url: 'http://localhost:' + fastify.server.address().port + '/static/../index.js',
followRedirect: false

@@ -160,6 +162,6 @@ }, (err, response, body) => {

t.plan(2)
request.get({
simple.concat({
method: 'GET',
// foobar is in static
uri: 'http://localhost:' + fastify.server.address().port + '/foobar.html'
url: 'http://localhost:' + fastify.server.address().port + '/foobar.html'
}, (err, response, body) => {

@@ -180,3 +182,3 @@ t.error(err)

}
const fastify = require('fastify')()
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)

@@ -193,9 +195,9 @@

t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, indexContent)
t.strictEqual(body.toString(), indexContent)
genericResponseChecks(t, response)

@@ -207,5 +209,5 @@ })

t.plan(2 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static/index.css'
url: 'http://localhost:' + fastify.server.address().port + '/static/index.css'
}, (err, response, body) => {

@@ -220,9 +222,9 @@ t.error(err)

t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static/'
url: 'http://localhost:' + fastify.server.address().port + '/static/'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, indexContent)
t.strictEqual(body.toString(), indexContent)
genericResponseChecks(t, response)

@@ -234,5 +236,5 @@ })

t.plan(2)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static'
url: 'http://localhost:' + fastify.server.address().port + '/static'
}, (err, response, body) => {

@@ -246,9 +248,9 @@ t.error(err)

t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/purpose/foo.html'
url: 'http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/purpose/foo.html'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, deepContent)
t.strictEqual(body.toString(), deepContent)
genericResponseChecks(t, response)

@@ -260,9 +262,9 @@ })

t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/'
url: 'http://localhost:' + fastify.server.address().port + '/static/deep/path/for/test/'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, innerIndex)
t.strictEqual(body.toString(), innerIndex)
genericResponseChecks(t, response)

@@ -274,5 +276,5 @@ })

t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static/this/path/doesnt/exist.html',
url: 'http://localhost:' + fastify.server.address().port + '/static/this/path/doesnt/exist.html',
followRedirect: false

@@ -288,5 +290,5 @@ }, (err, response, body) => {

t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/static/../index.js',
url: 'http://localhost:' + fastify.server.address().port + '/static/../index.js',
followRedirect: false

@@ -308,3 +310,3 @@ }, (err, response, body) => {

}
const fastify = require('fastify')()
const fastify = Fastify()

@@ -327,5 +329,5 @@ fastify.setErrorHandler(function errorHandler (err, request, reply) {

request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/../index.js',
url: 'http://localhost:' + fastify.server.address().port + '/../index.js',
followRedirect: false

@@ -336,3 +338,3 @@ }, (err, response, body) => {

t.strictEqual(response.headers['content-type'], 'text/plain')
t.strictEqual(body, '403 Custom error message')
t.strictEqual(body.toString(), '403 Custom error message')
})

@@ -349,3 +351,3 @@ })

}
const fastify = require('fastify')()
const fastify = Fastify()

@@ -368,5 +370,5 @@ fastify.setNotFoundHandler(function notFoundHandler (request, reply) {

request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/path/does/not/exist.html',
url: 'http://localhost:' + fastify.server.address().port + '/path/does/not/exist.html',
followRedirect: false

@@ -377,3 +379,3 @@ }, (err, response, body) => {

t.strictEqual(response.headers['content-type'], 'text/plain')
t.strictEqual(body, '/path/does/not/exist.html Not Found')
t.strictEqual(body.toString(), '/path/does/not/exist.html Not Found')
})

@@ -391,3 +393,3 @@ })

}
const fastify = require('fastify')()
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)

@@ -406,5 +408,5 @@

t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/foo/bar',
url: 'http://localhost:' + fastify.server.address().port + '/foo/bar',
followRedirect: false

@@ -414,3 +416,3 @@ }, (err, response, body) => {

t.strictEqual(response.statusCode, 200)
t.strictEqual(body, indexContent)
t.strictEqual(body.toString(), indexContent)
genericResponseChecks(t, response)

@@ -425,3 +427,3 @@ })

const pluginOptions = {root: path.join(__dirname, 'static')}
const fastify = require('fastify')({logger: false})
const fastify = Fastify({logger: false})
t.doesNotThrow(() => fastify.register(fastifyStatic, pluginOptions))

@@ -444,3 +446,3 @@ })

}
const fastify = require('fastify')({logger: false})
const fastify = Fastify({logger: false})
const fastifyStatic = require('proxyquire')('../', {

@@ -476,3 +478,3 @@ send: function sendStub (req, pathName, options) {

}
const fastify = require('fastify')()
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)

@@ -487,5 +489,5 @@

request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/index.html',
url: 'http://localhost:' + fastify.server.address().port + '/index.html',
followRedirect: false

@@ -496,3 +498,3 @@ }, (err, response, body) => {

t.strictEqual(response.headers['x-test-header'], 'test')
t.strictEqual(body, indexContent)
t.strictEqual(body.toString(), indexContent)
genericResponseChecks(t, response)

@@ -509,3 +511,3 @@ })

const pluginOptions = {}
const fastify = require('fastify')({logger: false})
const fastify = Fastify({logger: false})
fastify.register(fastifyStatic, pluginOptions)

@@ -520,3 +522,3 @@ .ready(err => {

const pluginOptions = { root: 42 }
const fastify = require('fastify')({logger: false})
const fastify = Fastify({logger: false})
fastify.register(fastifyStatic, pluginOptions)

@@ -531,3 +533,3 @@ .ready(err => {

const pluginOptions = { root: './my/path' }
const fastify = require('fastify')({logger: false})
const fastify = Fastify({logger: false})
fastify.register(fastifyStatic, pluginOptions)

@@ -542,3 +544,3 @@ .ready(err => {

const pluginOptions = { root: path.join(__dirname, 'foo', 'bar') }
const fastify = require('fastify')({logger: false})
const fastify = Fastify({logger: false})
fastify.register(fastifyStatic, pluginOptions)

@@ -553,3 +555,3 @@ .ready(err => {

const pluginOptions = { root: __filename }
const fastify = require('fastify')({logger: false})
const fastify = Fastify({logger: false})
fastify.register(fastifyStatic, pluginOptions)

@@ -564,3 +566,3 @@ .ready(err => {

const pluginOptions = { root: __dirname, setHeaders: 'headers' }
const fastify = require('fastify')({logger: false})
const fastify = Fastify({logger: false})
fastify.register(fastifyStatic, pluginOptions)

@@ -579,3 +581,3 @@ .ready(err => {

}
const fastify = require('fastify')()
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)

@@ -596,9 +598,9 @@

t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/index.html'
url: 'http://localhost:' + fastify.server.address().port + '/index.html'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, indexContent)
t.strictEqual(body.toString(), indexContent)
genericResponseChecks(t, response)

@@ -610,5 +612,5 @@ })

t.plan(2 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/index.css'
url: 'http://localhost:' + fastify.server.address().port + '/index.css'
}, (err, response, body) => {

@@ -623,5 +625,5 @@ t.error(err)

t.plan(3)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {

@@ -636,9 +638,9 @@ t.error(err)

t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/deep/path/for/test/purpose/foo.html'
url: 'http://localhost:' + fastify.server.address().port + '/deep/path/for/test/purpose/foo.html'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, deepContent)
t.strictEqual(body.toString(), deepContent)
genericResponseChecks(t, response)

@@ -650,9 +652,9 @@ })

t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/deep/path/for/test/'
url: 'http://localhost:' + fastify.server.address().port + '/deep/path/for/test/'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body, innerIndex)
t.strictEqual(body.toString(), innerIndex)
genericResponseChecks(t, response)

@@ -664,5 +666,5 @@ })

t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/this/path/doesnt/exist.html',
url: 'http://localhost:' + fastify.server.address().port + '/this/path/doesnt/exist.html',
followRedirect: false

@@ -678,5 +680,5 @@ }, (err, response, body) => {

t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
request.get({
simple.concat({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port + '/../index.js',
url: 'http://localhost:' + fastify.server.address().port + '/../index.js',
followRedirect: false

@@ -691,1 +693,49 @@ }, (err, response, body) => {

})
t.test('with fastify-compress', t => {
t.plan(3)
const pluginOptions = {
root: path.join(__dirname, '/static')
}
const fastify = Fastify()
fastify.register(compress, { threshold: 0 })
fastify.register(fastifyStatic, pluginOptions)
t.tearDown(fastify.close.bind(fastify))
fastify.listen(0, err => {
t.error(err)
t.test('deflate', function (t) {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/index.html',
headers: {
'accept-encoding': ['deflate']
}
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-encoding'], 'deflate')
genericResponseChecks(t, response)
})
})
t.test('gzip', function (t) {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/index.html'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-encoding'], 'gzip')
genericResponseChecks(t, response)
})
})
})
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc