@fastify/static 6.8.0 → 6.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -1
- package/index.js +2 -1
- package/package.json +2 -2
- package/test/static.test.js +110 -0
- package/types/index.d.ts +2 -1
- package/types/index.test-d.ts +5 -1
package/README.md
CHANGED
|
@@ -26,6 +26,7 @@ const path = require('path')
|
|
|
26
26
|
fastify.register(require('@fastify/static'), {
|
|
27
27
|
root: path.join(__dirname, 'public'),
|
|
28
28
|
prefix: '/public/', // optional: default '/'
|
|
29
|
+
constraints: { host: 'example.com' } // optional: default {}
|
|
29
30
|
})
|
|
30
31
|
|
|
31
32
|
fastify.get('/another/path', function (req, reply) {
|
|
@@ -118,6 +119,13 @@ Default: `'/'`
|
|
|
118
119
|
|
|
119
120
|
A URL path prefix used to create a virtual mount path for the static directory.
|
|
120
121
|
|
|
122
|
+
#### `constraints`
|
|
123
|
+
|
|
124
|
+
Default: `{}`
|
|
125
|
+
|
|
126
|
+
Constraints that will be added to registered routes. See Fastify's documentation for
|
|
127
|
+
[route constraints](https://www.fastify.io/docs/latest/Reference/Routes/#constraints).
|
|
128
|
+
|
|
121
129
|
#### `prefixAvoidTrailingSlash`
|
|
122
130
|
|
|
123
131
|
Default: `false`
|
|
@@ -201,7 +209,7 @@ Default: `undefined`
|
|
|
201
209
|
Under the hood we use [send](https://github.com/pillarjs/send#index) lib that by default supports "index.html" files.
|
|
202
210
|
To disable this set false or to supply a new index pass a string or an array in preferred order.
|
|
203
211
|
|
|
204
|
-
|
|
212
|
+
#### `serveDotFiles`
|
|
205
213
|
|
|
206
214
|
Default: `false`
|
|
207
215
|
|
package/index.js
CHANGED
|
@@ -240,7 +240,7 @@ async function fastifyStatic (fastify, opts) {
|
|
|
240
240
|
reply,
|
|
241
241
|
pathname,
|
|
242
242
|
rootPath,
|
|
243
|
-
|
|
243
|
+
rootPathOffset,
|
|
244
244
|
undefined,
|
|
245
245
|
checkedEncodings
|
|
246
246
|
)
|
|
@@ -278,6 +278,7 @@ async function fastifyStatic (fastify, opts) {
|
|
|
278
278
|
|
|
279
279
|
// Set the schema hide property if defined in opts or true by default
|
|
280
280
|
const routeOpts = {
|
|
281
|
+
constraints: opts.constraints,
|
|
281
282
|
schema: {
|
|
282
283
|
hide: typeof opts.schemaHide !== 'undefined' ? opts.schemaHide : true
|
|
283
284
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/static",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.10.0",
|
|
4
4
|
"description": "Plugin for serving static files as fast as possible.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"snazzy": "^9.0.0",
|
|
55
55
|
"standard": "^17.0.0",
|
|
56
56
|
"tap": "^16.0.0",
|
|
57
|
-
"tsd": "^0.
|
|
57
|
+
"tsd": "^0.28.0"
|
|
58
58
|
},
|
|
59
59
|
"tsd": {
|
|
60
60
|
"directory": "test/types"
|
package/test/static.test.js
CHANGED
|
@@ -625,6 +625,59 @@ t.test('register /static and /static2', (t) => {
|
|
|
625
625
|
})
|
|
626
626
|
})
|
|
627
627
|
|
|
628
|
+
t.test('register /static with constraints', (t) => {
|
|
629
|
+
t.plan(3)
|
|
630
|
+
|
|
631
|
+
const pluginOptions = {
|
|
632
|
+
root: path.join(__dirname, '/static'),
|
|
633
|
+
prefix: '/static',
|
|
634
|
+
constraints: {
|
|
635
|
+
host: 'example.com'
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
const fastify = Fastify()
|
|
639
|
+
fastify.register(fastifyStatic, pluginOptions)
|
|
640
|
+
|
|
641
|
+
t.teardown(fastify.close.bind(fastify))
|
|
642
|
+
|
|
643
|
+
fastify.listen({ port: 0 }, (err) => {
|
|
644
|
+
t.error(err)
|
|
645
|
+
|
|
646
|
+
fastify.server.unref()
|
|
647
|
+
|
|
648
|
+
t.test('example.com/static/index.html', (t) => {
|
|
649
|
+
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
|
|
650
|
+
simple.concat({
|
|
651
|
+
method: 'GET',
|
|
652
|
+
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html',
|
|
653
|
+
headers: {
|
|
654
|
+
host: 'example.com'
|
|
655
|
+
}
|
|
656
|
+
}, (err, response, body) => {
|
|
657
|
+
t.error(err)
|
|
658
|
+
t.equal(response.statusCode, 200)
|
|
659
|
+
t.equal(body.toString(), indexContent)
|
|
660
|
+
genericResponseChecks(t, response)
|
|
661
|
+
})
|
|
662
|
+
})
|
|
663
|
+
|
|
664
|
+
t.test('not-example.com/static/index.html', (t) => {
|
|
665
|
+
t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
|
|
666
|
+
simple.concat({
|
|
667
|
+
method: 'GET',
|
|
668
|
+
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html',
|
|
669
|
+
headers: {
|
|
670
|
+
host: 'not-example.com'
|
|
671
|
+
}
|
|
672
|
+
}, (err, response, body) => {
|
|
673
|
+
t.error(err)
|
|
674
|
+
t.equal(response.statusCode, 404)
|
|
675
|
+
genericErrorResponseChecks(t, response)
|
|
676
|
+
})
|
|
677
|
+
})
|
|
678
|
+
})
|
|
679
|
+
})
|
|
680
|
+
|
|
628
681
|
t.test('payload.filename is set', (t) => {
|
|
629
682
|
t.plan(3)
|
|
630
683
|
|
|
@@ -3646,3 +3699,60 @@ t.test('should serve files into hidden dir without wildcard option', (t) => {
|
|
|
3646
3699
|
})
|
|
3647
3700
|
})
|
|
3648
3701
|
})
|
|
3702
|
+
|
|
3703
|
+
t.test(
|
|
3704
|
+
'will serve pre-compressed files with .gzip if multi-root',
|
|
3705
|
+
async (t) => {
|
|
3706
|
+
const pluginOptions = {
|
|
3707
|
+
root: [path.join(__dirname, '/static-pre-compressed'), path.join(__dirname, '/static')],
|
|
3708
|
+
preCompressed: true
|
|
3709
|
+
}
|
|
3710
|
+
|
|
3711
|
+
const fastify = Fastify()
|
|
3712
|
+
|
|
3713
|
+
fastify.register(fastifyStatic, pluginOptions)
|
|
3714
|
+
t.teardown(fastify.close.bind(fastify))
|
|
3715
|
+
|
|
3716
|
+
const response = await fastify.inject({
|
|
3717
|
+
method: 'GET',
|
|
3718
|
+
url: 'all-three.html',
|
|
3719
|
+
headers: {
|
|
3720
|
+
'accept-encoding': '*, *'
|
|
3721
|
+
}
|
|
3722
|
+
})
|
|
3723
|
+
|
|
3724
|
+
genericResponseChecks(t, response)
|
|
3725
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
3726
|
+
t.equal(response.statusCode, 200)
|
|
3727
|
+
t.same(response.rawPayload, allThreeGzip)
|
|
3728
|
+
t.end()
|
|
3729
|
+
}
|
|
3730
|
+
)
|
|
3731
|
+
|
|
3732
|
+
t.test(
|
|
3733
|
+
'will still serve un-compressed files with multi-root and preCompressed as true',
|
|
3734
|
+
async (t) => {
|
|
3735
|
+
const pluginOptions = {
|
|
3736
|
+
root: [path.join(__dirname, '/static-pre-compressed'), path.join(__dirname, '/static')],
|
|
3737
|
+
preCompressed: true
|
|
3738
|
+
}
|
|
3739
|
+
|
|
3740
|
+
const fastify = Fastify()
|
|
3741
|
+
|
|
3742
|
+
fastify.register(fastifyStatic, pluginOptions)
|
|
3743
|
+
t.teardown(fastify.close.bind(fastify))
|
|
3744
|
+
|
|
3745
|
+
const response = await fastify.inject({
|
|
3746
|
+
method: 'GET',
|
|
3747
|
+
url: 'foobar.html',
|
|
3748
|
+
headers: {
|
|
3749
|
+
'accept-encoding': '*, *'
|
|
3750
|
+
}
|
|
3751
|
+
})
|
|
3752
|
+
|
|
3753
|
+
genericResponseChecks(t, response)
|
|
3754
|
+
t.equal(response.statusCode, 200)
|
|
3755
|
+
t.same(response.body, foobarContent)
|
|
3756
|
+
t.end()
|
|
3757
|
+
}
|
|
3758
|
+
)
|
package/types/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Leo <https://github.com/leomelzer>
|
|
3
3
|
/// <reference types="node" />
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import {FastifyPluginAsync, FastifyRequest, RouteOptions} from 'fastify';
|
|
6
6
|
import { Stats } from 'fs';
|
|
7
7
|
|
|
8
8
|
declare module "fastify" {
|
|
@@ -104,6 +104,7 @@ declare namespace fastifyStatic {
|
|
|
104
104
|
index?: string[] | string | false;
|
|
105
105
|
lastModified?: boolean;
|
|
106
106
|
maxAge?: string | number;
|
|
107
|
+
constraints?: RouteOptions['constraints'];
|
|
107
108
|
}
|
|
108
109
|
|
|
109
110
|
export const fastifyStatic: FastifyStaticPlugin;
|
package/types/index.test-d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Server } from 'http';
|
|
|
3
3
|
import { expectAssignable, expectError, expectType } from 'tsd'
|
|
4
4
|
import * as fastifyStaticStar from '..';
|
|
5
5
|
import fastifyStatic, {
|
|
6
|
-
FastifyStaticOptions,
|
|
6
|
+
FastifyStaticOptions,
|
|
7
7
|
fastifyStatic as fastifyStaticNamed,
|
|
8
8
|
} from '..'
|
|
9
9
|
|
|
@@ -55,6 +55,10 @@ const options: FastifyStaticOptions = {
|
|
|
55
55
|
preCompressed: false,
|
|
56
56
|
allowedPath: (pathName: string, root: string, request: FastifyRequest) => {
|
|
57
57
|
return true;
|
|
58
|
+
},
|
|
59
|
+
constraints: {
|
|
60
|
+
host: /.*\.example\.com/,
|
|
61
|
+
version: '1.0.2'
|
|
58
62
|
}
|
|
59
63
|
}
|
|
60
64
|
|