Skip to content

Commit

Permalink
feat: allow specifying route constraints (#370)
Browse files Browse the repository at this point in the history
* feat: allow specifying route constraints

* style: fix lint errors

* docs: add information about constraints property

* docs: fix heading level of serveDotFiles

* fix: don't create empty constraints object
  • Loading branch information
katlyn committed Apr 2, 2023
1 parent 2649779 commit 97afe85
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
10 changes: 9 additions & 1 deletion README.md
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
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
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
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
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

0 comments on commit 97afe85

Please sign in to comment.