Skip to content

Commit

Permalink
Add rootPath overload parameter to sendFile function (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-shaw-2011 authored and mcollina committed Jan 26, 2020
1 parent 07b09b2 commit c590050
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ fastify.get('/another/path', function (req, reply) {
reply.sendFile('myHtml.html') // serving path.join(__dirname, 'public', 'myHtml.html') directly
})

fastify.get('/path/with/different/root', function (req, reply) {
reply.sendFile('myHtml.html', path.join(__dirname, 'build')) // serving a file from a different root location
})

```

### Options
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type HttpResponse = ServerResponse | Http2ServerResponse;

declare module "fastify" {
interface FastifyReply<HttpResponse> {
sendFile(filename: string): FastifyReply<HttpResponse>;
sendFile(filename: string, rootPath?: string): FastifyReply<HttpResponse>;
}
}

Expand Down
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ function fastifyStatic (fastify, opts, next) {
maxAge: opts.maxAge
}

function pumpSendToReply (request, reply, pathname) {
const stream = send(request.raw, pathname, sendOptions)
function pumpSendToReply (request, reply, pathname, rootPath) {
var options = Object.assign({}, sendOptions)

if (rootPath) {
options.root = rootPath
}

const stream = send(request.raw, pathname, options)
var resolvedFilename
stream.on('file', function (file) {
resolvedFilename = file
Expand Down Expand Up @@ -106,8 +112,8 @@ function fastifyStatic (fastify, opts, next) {
const schema = { schema: { hide: typeof opts.schemaHide !== 'undefined' ? opts.schemaHide : true } }

if (opts.decorateReply !== false) {
fastify.decorateReply('sendFile', function (filePath) {
pumpSendToReply(this.request, this, filePath)
fastify.decorateReply('sendFile', function (filePath, rootPath) {
pumpSendToReply(this.request, this, filePath, rootPath)
})
}

Expand Down
34 changes: 33 additions & 1 deletion test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ t.test('serving disabled', t => {
})

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

const pluginOptions = {
root: path.join(__dirname, '/static'),
Expand All @@ -527,6 +527,10 @@ t.test('sendFile', t => {
reply.sendFile('/index.html')
})

fastify.get('/root/path/override/test', (request, reply) => {
reply.sendFile('/foo.html', path.join(__dirname, 'static', 'deep', 'path', 'for', 'test', 'purpose'))
})

fastify.listen(0, err => {
t.error(err)

Expand All @@ -545,6 +549,34 @@ t.test('sendFile', t => {
genericResponseChecks(t, response)
})
})

t.test('reply.sendFile() with rootPath', t => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/root/path/override/test',
followRedirect: false
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(body.toString(), deepContent)
genericResponseChecks(t, response)
})
})

t.test('reply.sendFile() again without root path', t => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
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(), indexContent)
genericResponseChecks(t, response)
})
})
})
})

Expand Down

0 comments on commit c590050

Please sign in to comment.