@fastify/static 6.9.0 → 6.10.1
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/example/public/images/sample.jpg +0 -0
- package/index.js +26 -0
- package/package.json +2 -2
- package/test/static.test.js +100 -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
|
|
|
Binary file
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const path = require('path')
|
|
4
|
+
const url = require('url')
|
|
4
5
|
const statSync = require('fs').statSync
|
|
5
6
|
const { PassThrough } = require('readable-stream')
|
|
6
7
|
const glob = require('glob')
|
|
@@ -14,6 +15,7 @@ const encodingNegotiator = require('@fastify/accept-negotiator')
|
|
|
14
15
|
const dirList = require('./lib/dirList')
|
|
15
16
|
|
|
16
17
|
async function fastifyStatic (fastify, opts) {
|
|
18
|
+
opts.root = normalizeRoot(opts.root)
|
|
17
19
|
checkRootPathForErrors(fastify, opts.root)
|
|
18
20
|
|
|
19
21
|
const setHeaders = opts.setHeaders
|
|
@@ -278,6 +280,7 @@ async function fastifyStatic (fastify, opts) {
|
|
|
278
280
|
|
|
279
281
|
// Set the schema hide property if defined in opts or true by default
|
|
280
282
|
const routeOpts = {
|
|
283
|
+
constraints: opts.constraints,
|
|
281
284
|
schema: {
|
|
282
285
|
hide: typeof opts.schemaHide !== 'undefined' ? opts.schemaHide : true
|
|
283
286
|
},
|
|
@@ -393,6 +396,29 @@ async function fastifyStatic (fastify, opts) {
|
|
|
393
396
|
pumpSendToReply(req, reply, file, rootPath)
|
|
394
397
|
}
|
|
395
398
|
}
|
|
399
|
+
function normalizeRoot (root) {
|
|
400
|
+
if (root === undefined) {
|
|
401
|
+
return root
|
|
402
|
+
}
|
|
403
|
+
if (root instanceof URL && root.protocol === 'file:') {
|
|
404
|
+
return url.fileURLToPath(root)
|
|
405
|
+
}
|
|
406
|
+
if (Array.isArray(root)) {
|
|
407
|
+
const result = []
|
|
408
|
+
for (let i = 0, il = root.length; i < il; ++i) {
|
|
409
|
+
if (root[i] instanceof URL && root[i].protocol === 'file:') {
|
|
410
|
+
result.push(url.fileURLToPath(root[i]))
|
|
411
|
+
} else {
|
|
412
|
+
result.push(root[i])
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
return result
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return root
|
|
420
|
+
}
|
|
421
|
+
|
|
396
422
|
function checkRootPathForErrors (fastify, rootPath) {
|
|
397
423
|
if (rootPath === undefined) {
|
|
398
424
|
throw new Error('"root" option is required')
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/static",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.10.1",
|
|
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
|
|
|
@@ -3703,3 +3756,50 @@ t.test(
|
|
|
3703
3756
|
t.end()
|
|
3704
3757
|
}
|
|
3705
3758
|
)
|
|
3759
|
+
t.test(
|
|
3760
|
+
'converts URL to path',
|
|
3761
|
+
async (t) => {
|
|
3762
|
+
t.plan(2 + GENERIC_RESPONSE_CHECK_COUNT)
|
|
3763
|
+
const pluginOptions = {
|
|
3764
|
+
root: url.pathToFileURL(path.join(__dirname, '/static'))
|
|
3765
|
+
}
|
|
3766
|
+
|
|
3767
|
+
const fastify = Fastify()
|
|
3768
|
+
|
|
3769
|
+
fastify.register(fastifyStatic, pluginOptions)
|
|
3770
|
+
const response = await fastify.inject({
|
|
3771
|
+
method: 'GET',
|
|
3772
|
+
url: 'foobar.html',
|
|
3773
|
+
headers: {
|
|
3774
|
+
'accept-encoding': '*, *'
|
|
3775
|
+
}
|
|
3776
|
+
})
|
|
3777
|
+
genericResponseChecks(t, response)
|
|
3778
|
+
t.equal(response.statusCode, 200)
|
|
3779
|
+
t.same(response.body, foobarContent)
|
|
3780
|
+
}
|
|
3781
|
+
)
|
|
3782
|
+
|
|
3783
|
+
t.test(
|
|
3784
|
+
'converts array of URLs to path, contains string path',
|
|
3785
|
+
async (t) => {
|
|
3786
|
+
t.plan(2 + GENERIC_RESPONSE_CHECK_COUNT)
|
|
3787
|
+
const pluginOptions = {
|
|
3788
|
+
root: [url.pathToFileURL(path.join(__dirname, '/static')), path.join(__dirname, 'static-dotfiles'), url.pathToFileURL(path.join(__dirname, '/static-pre-compressed'))]
|
|
3789
|
+
}
|
|
3790
|
+
|
|
3791
|
+
const fastify = Fastify()
|
|
3792
|
+
|
|
3793
|
+
fastify.register(fastifyStatic, pluginOptions)
|
|
3794
|
+
const response = await fastify.inject({
|
|
3795
|
+
method: 'GET',
|
|
3796
|
+
url: 'foobar.html',
|
|
3797
|
+
headers: {
|
|
3798
|
+
'accept-encoding': '*, *'
|
|
3799
|
+
}
|
|
3800
|
+
})
|
|
3801
|
+
genericResponseChecks(t, response)
|
|
3802
|
+
t.equal(response.statusCode, 200)
|
|
3803
|
+
t.same(response.body, foobarContent)
|
|
3804
|
+
}
|
|
3805
|
+
)
|
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
|
|