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

feat: allow specifying route constraints #370

Merged
merged 5 commits into from
Apr 2, 2023
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const path = require('path')
fastify.register(require('@fastify/static'), {
root: path.join(__dirname, 'public'),
prefix: '/public/', // optional: default '/'
constraints: { host: 'example.com' } // optional: default {}
})

fastify.get('/another/path', function (req, reply) {
Expand Down Expand Up @@ -118,6 +119,13 @@ Default: `'/'`

A URL path prefix used to create a virtual mount path for the static directory.

#### `constraints`

Default: `{}`

Constraints that will be added to registered routes. See Fastify's documentation for
[route constraints](https://www.fastify.io/docs/latest/Reference/Routes/#constraints).

#### `prefixAvoidTrailingSlash`

Default: `false`
Expand Down Expand Up @@ -201,7 +209,7 @@ Default: `undefined`
Under the hood we use [send](https://github.com/pillarjs/send#index) lib that by default supports "index.html" files.
To disable this set false or to supply a new index pass a string or an array in preferred order.

### `serveDotFiles`
#### `serveDotFiles`

Default: `false`

Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ async function fastifyStatic (fastify, opts) {

// Set the schema hide property if defined in opts or true by default
const routeOpts = {
constraints: opts.constraints,
schema: {
hide: typeof opts.schemaHide !== 'undefined' ? opts.schemaHide : true
},
Expand Down
53 changes: 53 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,59 @@ t.test('register /static and /static2', (t) => {
})
})

t.test('register /static with constraints', (t) => {
t.plan(3)

const pluginOptions = {
root: path.join(__dirname, '/static'),
prefix: '/static',
constraints: {
host: 'example.com'
}
}
const fastify = Fastify()
fastify.register(fastifyStatic, pluginOptions)

t.teardown(fastify.close.bind(fastify))

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

fastify.server.unref()

t.test('example.com/static/index.html', (t) => {
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html',
headers: {
host: 'example.com'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), indexContent)
genericResponseChecks(t, response)
})
})

t.test('not-example.com/static/index.html', (t) => {
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html',
headers: {
host: 'not-example.com'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 404)
genericErrorResponseChecks(t, response)
})
})
})
})

t.test('payload.filename is set', (t) => {
t.plan(3)

Expand Down
3 changes: 2 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Leo <https://github.com/leomelzer>
/// <reference types="node" />

import { FastifyPluginAsync, FastifyRequest } from 'fastify';
import {FastifyPluginAsync, FastifyRequest, RouteOptions} from 'fastify';
import { Stats } from 'fs';

declare module "fastify" {
Expand Down Expand Up @@ -104,6 +104,7 @@ declare namespace fastifyStatic {
index?: string[] | string | false;
lastModified?: boolean;
maxAge?: string | number;
constraints?: RouteOptions['constraints'];
}

export const fastifyStatic: FastifyStaticPlugin;
Expand Down
6 changes: 5 additions & 1 deletion types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Server } from 'http';
import { expectAssignable, expectError, expectType } from 'tsd'
import * as fastifyStaticStar from '..';
import fastifyStatic, {
FastifyStaticOptions,
FastifyStaticOptions,
fastifyStatic as fastifyStaticNamed,
} from '..'

Expand Down Expand Up @@ -55,6 +55,10 @@ const options: FastifyStaticOptions = {
preCompressed: false,
allowedPath: (pathName: string, root: string, request: FastifyRequest) => {
return true;
},
constraints: {
host: /.*\.example\.com/,
version: '1.0.2'
}
}

Expand Down