From 23c16182268b4912c6d0cae17fc419e973b1e0b7 Mon Sep 17 00:00:00 2001 From: Katlyn Lorimer Date: Fri, 31 Mar 2023 00:29:55 -0800 Subject: [PATCH 1/5] feat: allow specifying route constraints --- index.js | 3 +++ test/static.test.js | 53 +++++++++++++++++++++++++++++++++++++++++++ types/index.d.ts | 3 ++- types/index.test-d.ts | 6 ++++- 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 73f1400..55562c9 100644 --- a/index.js +++ b/index.js @@ -57,6 +57,8 @@ async function fastifyStatic (fastify, opts) { : opts.prefix + '/' } + if (opts.constraints === undefined) opts.constraints = {} + function pumpSendToReply ( request, reply, @@ -278,6 +280,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 }, diff --git a/test/static.test.js b/test/static.test.js index 79bdcc7..2473008 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -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) diff --git a/types/index.d.ts b/types/index.d.ts index 56a0cc2..8fd29bb 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2,7 +2,7 @@ // Leo /// -import { FastifyPluginAsync, FastifyRequest } from 'fastify'; +import {FastifyPluginAsync, FastifyRequest, RouteOptions} from 'fastify'; import { Stats } from 'fs'; declare module "fastify" { @@ -104,6 +104,7 @@ declare namespace fastifyStatic { index?: string[] | string | false; lastModified?: boolean; maxAge?: string | number; + constraints?: RouteOptions['constraints'] } export const fastifyStatic: FastifyStaticPlugin; diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 2dd0801..719263b 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -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 '..' @@ -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' } } From c2687b61b95c71ee0b0b9c5e6648bf0281b3112d Mon Sep 17 00:00:00 2001 From: Katlyn Lorimer Date: Fri, 31 Mar 2023 00:45:14 -0800 Subject: [PATCH 2/5] style: fix lint errors --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index 8fd29bb..9e2224d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -104,7 +104,7 @@ declare namespace fastifyStatic { index?: string[] | string | false; lastModified?: boolean; maxAge?: string | number; - constraints?: RouteOptions['constraints'] + constraints?: RouteOptions['constraints']; } export const fastifyStatic: FastifyStaticPlugin; From 55331764123df731119997ade052de2f03836818 Mon Sep 17 00:00:00 2001 From: Katlyn Lorimer Date: Fri, 31 Mar 2023 00:55:46 -0800 Subject: [PATCH 3/5] docs: add information about constraints property --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 6bcf79e..3617c79 100644 --- a/README.md +++ b/README.md @@ -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) { @@ -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` From e798484f67dcca47ee97c22dad3bddc9f200cb24 Mon Sep 17 00:00:00 2001 From: Katlyn Lorimer Date: Fri, 31 Mar 2023 00:56:44 -0800 Subject: [PATCH 4/5] docs: fix heading level of serveDotFiles --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3617c79..2e67fee 100644 --- a/README.md +++ b/README.md @@ -209,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` From f60cc9d19e586d51a23860f63d50391d29c1136a Mon Sep 17 00:00:00 2001 From: Katlyn Lorimer Date: Sat, 1 Apr 2023 12:42:14 -0800 Subject: [PATCH 5/5] fix: don't create empty constraints object --- index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/index.js b/index.js index 55562c9..a763100 100644 --- a/index.js +++ b/index.js @@ -57,8 +57,6 @@ async function fastifyStatic (fastify, opts) { : opts.prefix + '/' } - if (opts.constraints === undefined) opts.constraints = {} - function pumpSendToReply ( request, reply,