Skip to content

Commit

Permalink
Expose filename to output stream. (fastify#61)
Browse files Browse the repository at this point in the history
This is needed for additional plugins to perform transformations which
require knowledge of the path on the local filesystem.
  • Loading branch information
coreyfarrell authored and mcollina committed May 20, 2018
1 parent 204fd87 commit fd721e9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ If an error occurs while trying to send a file, the error will be passed
to Fastify's error handler. You can set a custom error handler with
[`fastify.setErrorHandler()`](https://www.fastify.io/docs/latest/Server-Methods/#seterrorhandler).

### 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)
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ function fastifyStatic (fastify, opts, next) {

function pumpSendToReply (request, reply, pathname) {
const stream = send(request.raw, pathname, sendOptions)
var resolvedFilename
stream.on('file', function (file) {
resolvedFilename = file
})

const wrap = new PassThrough({
flush (cb) {
Expand All @@ -49,6 +53,11 @@ function fastifyStatic (fastify, opts, next) {
wrap.socket = request.raw.socket
wrap.finished = false

Object.defineProperty(wrap, 'filename', {
get () {
return resolvedFilename
}
})
Object.defineProperty(wrap, 'statusCode', {
get () {
return reply.res.statusCode
Expand Down Expand Up @@ -104,7 +113,7 @@ function checkRootPathForErrors (rootPath) {
return new Error('"root" option must be an absolute path')
}

let pathStat
var pathStat

try {
pathStat = statSync(rootPath)
Expand Down
53 changes: 53 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,59 @@ t.test('register /static/', t => {
})
})

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 => {
t.plan(2)

Expand Down

0 comments on commit fd721e9

Please sign in to comment.