Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rootPath overload parameter to sendFile function #118

Merged
merged 1 commit into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
chris-shaw-2011 marked this conversation as resolved.
Show resolved Hide resolved
})
})

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