fastify-static
Advanced tools
Comparing version 0.11.0 to 0.12.0
19
index.js
@@ -36,2 +36,6 @@ 'use strict' | ||
const stream = send(request.raw, pathname, sendOptions) | ||
var resolvedFilename | ||
stream.on('file', function (file) { | ||
resolvedFilename = file | ||
}) | ||
@@ -53,2 +57,7 @@ const wrap = new PassThrough({ | ||
Object.defineProperty(wrap, 'filename', { | ||
get () { | ||
return resolvedFilename | ||
} | ||
}) | ||
Object.defineProperty(wrap, 'statusCode', { | ||
@@ -91,5 +100,7 @@ get () { | ||
fastify.decorateReply('sendFile', function (filePath) { | ||
pumpSendToReply(this.request, this, filePath) | ||
}) | ||
if (opts.decorateReply !== false) { | ||
fastify.decorateReply('sendFile', function (filePath) { | ||
pumpSendToReply(this.request, this, filePath) | ||
}) | ||
} | ||
@@ -110,3 +121,3 @@ next() | ||
let pathStat | ||
var pathStat | ||
@@ -113,0 +124,0 @@ try { |
{ | ||
"name": "fastify-static", | ||
"version": "0.11.0", | ||
"version": "0.12.0", | ||
"description": "Plugin for serving static files as fast as possible.", | ||
@@ -42,4 +42,4 @@ "main": "index.js", | ||
"standard": "^11.0.0", | ||
"tap": "^11.1.4" | ||
"tap": "^12.0.0" | ||
} | ||
} |
@@ -74,2 +74,9 @@ # fastify-static | ||
#### Disabling reply decorator | ||
The reply object is decorated with a `sendFile` function by default. If you want to | ||
disable this, pass the option `{ decorateReply: false }`. If fastify-static is | ||
registers to multiple prefixes in the same route only one can initialize reply | ||
decorators. | ||
#### Handling 404s | ||
@@ -87,4 +94,15 @@ | ||
### Payload `stream.filename` | ||
If you need to access the filename inside the `onSend` hook, you can use `payload.filename`. | ||
```js | ||
fastify.addHook('onSend', function (req, reply, payload, next) { | ||
console.log(payload.filename) | ||
next() | ||
}) | ||
``` | ||
## License | ||
Licensed under [MIT](./LICENSE) |
@@ -309,2 +309,55 @@ 'use strict' | ||
t.test('payload.filename is set', t => { | ||
t.plan(3) | ||
const pluginOptions = { | ||
root: path.join(__dirname, '/static'), | ||
prefix: '/static/' | ||
} | ||
const fastify = Fastify() | ||
var gotFilename | ||
fastify.register(fastifyStatic, pluginOptions) | ||
fastify.addHook('onSend', function (req, reply, payload, next) { | ||
gotFilename = payload.filename | ||
next() | ||
}) | ||
t.tearDown(fastify.close.bind(fastify)) | ||
fastify.listen(0, err => { | ||
t.error(err) | ||
fastify.server.unref() | ||
t.test('/static/index.html', t => { | ||
t.plan(5 + GENERIC_RESPONSE_CHECK_COUNT) | ||
simple.concat({ | ||
method: 'GET', | ||
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html' | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
t.strictEqual(body.toString(), indexContent) | ||
t.is(typeof gotFilename, 'string') | ||
t.strictEqual(gotFilename, path.join(pluginOptions.root, 'index.html')) | ||
genericResponseChecks(t, response) | ||
}) | ||
}) | ||
t.test('/static/this/path/doesnt/exist.html', t => { | ||
t.plan(3 + GENERIC_ERROR_RESPONSE_CHECK_COUNT) | ||
simple.concat({ | ||
method: 'GET', | ||
url: 'http://localhost:' + fastify.server.address().port + '/static/this/path/doesnt/exist.html', | ||
followRedirect: false | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 404) | ||
t.is(typeof gotFilename, 'undefined') | ||
genericErrorResponseChecks(t, response) | ||
}) | ||
}) | ||
}) | ||
}) | ||
t.test('error responses can be customized with fastify.setErrorHandler()', t => { | ||
@@ -421,2 +474,41 @@ t.plan(2) | ||
t.test('sendFile disabled', t => { | ||
t.plan(2) | ||
const pluginOptions = { | ||
root: path.join(__dirname, '/static'), | ||
prefix: '/static', | ||
decorateReply: false | ||
} | ||
const fastify = Fastify() | ||
fastify.register(fastifyStatic, pluginOptions) | ||
fastify.get('/foo/bar', function (req, reply) { | ||
if (typeof reply.sendFile === 'undefined') { | ||
reply.send('pass') | ||
} else { | ||
reply.send('fail') | ||
} | ||
}) | ||
fastify.listen(0, err => { | ||
t.error(err) | ||
fastify.server.unref() | ||
t.test('reply.sendFile undefined', t => { | ||
t.plan(3) | ||
simple.concat({ | ||
method: 'GET', | ||
url: 'http://localhost:' + fastify.server.address().port + '/foo/bar', | ||
followRedirect: false | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
t.strictEqual(body.toString(), 'pass') | ||
}) | ||
}) | ||
}) | ||
}) | ||
t.test('prefix default', t => { | ||
@@ -423,0 +515,0 @@ t.plan(1) |
36932
934
107
16