@fastify/static 6.5.1 → 6.6.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
CHANGED
|
@@ -201,11 +201,17 @@ Default: `undefined`
|
|
|
201
201
|
Under the hood we use [send](https://github.com/pillarjs/send#index) lib that by default supports "index.html" files.
|
|
202
202
|
To disable this set false or to supply a new index pass a string or an array in preferred order.
|
|
203
203
|
|
|
204
|
+
### `serveDotFiles`
|
|
205
|
+
|
|
206
|
+
Default: `false`
|
|
207
|
+
|
|
208
|
+
When `true`, files in hidden directories (e.g. `.foo`) will be served.
|
|
209
|
+
|
|
204
210
|
#### `list`
|
|
205
211
|
|
|
206
212
|
Default: `undefined`
|
|
207
213
|
|
|
208
|
-
If set, it
|
|
214
|
+
If set, it provides the directory list calling the directory path.
|
|
209
215
|
|
|
210
216
|
Default response is json.
|
|
211
217
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"hello": "world"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const path = require('path')
|
|
4
|
+
const fastify = require('fastify')({ logger: { level: 'trace' } })
|
|
5
|
+
|
|
6
|
+
fastify
|
|
7
|
+
.register(require('../'), {
|
|
8
|
+
// An absolute path containing static files to serve.
|
|
9
|
+
root: path.join(__dirname, '/public'),
|
|
10
|
+
wildcard: false,
|
|
11
|
+
serveDotFiles: true
|
|
12
|
+
})
|
|
13
|
+
.listen({ port: 3000 }, err => {
|
|
14
|
+
if (err) throw err
|
|
15
|
+
})
|
package/index.js
CHANGED
|
@@ -37,7 +37,8 @@ async function fastifyStatic (fastify, opts) {
|
|
|
37
37
|
immutable: opts.immutable,
|
|
38
38
|
index: opts.index,
|
|
39
39
|
lastModified: opts.lastModified,
|
|
40
|
-
maxAge: opts.maxAge
|
|
40
|
+
maxAge: opts.maxAge,
|
|
41
|
+
serveDotFiles: opts.serveDotFiles ?? false
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
const allowedPath = opts.allowedPath
|
|
@@ -336,7 +337,7 @@ async function fastifyStatic (fastify, opts) {
|
|
|
336
337
|
const winSeparatorRegex = new RegExp(`\\${path.win32.sep}`, 'g')
|
|
337
338
|
|
|
338
339
|
for (const rootPath of Array.isArray(sendOptions.root) ? sendOptions.root : [sendOptions.root]) {
|
|
339
|
-
const files = await globPromise(path.join(rootPath, globPattern).replace(winSeparatorRegex, path.posix.sep), { nodir: true })
|
|
340
|
+
const files = await globPromise(path.join(rootPath, globPattern).replace(winSeparatorRegex, path.posix.sep), { nodir: true, dot: sendOptions.serveDotFiles })
|
|
340
341
|
const indexes = typeof opts.index === 'undefined' ? ['index.html'] : [].concat(opts.index)
|
|
341
342
|
|
|
342
343
|
for (let file of files) {
|
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"hello": "world"}
|
package/test/static.test.js
CHANGED
|
@@ -51,6 +51,7 @@ const uncompressedStatic = fs
|
|
|
51
51
|
.toString('utf8')
|
|
52
52
|
const fooContent = fs.readFileSync('./test/static/foo.html').toString('utf8')
|
|
53
53
|
const barContent = fs.readFileSync('./test/static2/bar.html').toString('utf8')
|
|
54
|
+
const jsonHiddenContent = fs.readFileSync('./test/static-hidden/.hidden/sample.json').toString('utf8')
|
|
54
55
|
|
|
55
56
|
const GENERIC_RESPONSE_CHECK_COUNT = 5
|
|
56
57
|
function genericResponseChecks (t, response) {
|
|
@@ -3552,3 +3553,96 @@ t.test('should follow symbolic link without wildcard', (t) => {
|
|
|
3552
3553
|
})
|
|
3553
3554
|
})
|
|
3554
3555
|
})
|
|
3556
|
+
|
|
3557
|
+
t.test('should serve files into hidden dir with wildcard `false`', (t) => {
|
|
3558
|
+
t.plan(9)
|
|
3559
|
+
|
|
3560
|
+
const pluginOptions = {
|
|
3561
|
+
root: path.join(__dirname, '/static-hidden'),
|
|
3562
|
+
wildcard: false,
|
|
3563
|
+
serveDotFiles: true
|
|
3564
|
+
}
|
|
3565
|
+
const fastify = Fastify()
|
|
3566
|
+
fastify.register(fastifyStatic, pluginOptions)
|
|
3567
|
+
|
|
3568
|
+
t.teardown(fastify.close.bind(fastify))
|
|
3569
|
+
|
|
3570
|
+
fastify.listen({ port: 0 }, (err) => {
|
|
3571
|
+
t.error(err)
|
|
3572
|
+
|
|
3573
|
+
fastify.server.unref()
|
|
3574
|
+
|
|
3575
|
+
simple.concat({
|
|
3576
|
+
method: 'GET',
|
|
3577
|
+
url: 'http://localhost:' + fastify.server.address().port + '/.hidden/sample.json'
|
|
3578
|
+
}, (err, response, body) => {
|
|
3579
|
+
t.error(err)
|
|
3580
|
+
t.equal(response.statusCode, 200)
|
|
3581
|
+
t.equal(body.toString(), jsonHiddenContent)
|
|
3582
|
+
t.ok(/application\/(json)/.test(response.headers['content-type']))
|
|
3583
|
+
t.ok(response.headers.etag)
|
|
3584
|
+
t.ok(response.headers['last-modified'])
|
|
3585
|
+
t.ok(response.headers.date)
|
|
3586
|
+
t.ok(response.headers['cache-control'])
|
|
3587
|
+
})
|
|
3588
|
+
})
|
|
3589
|
+
})
|
|
3590
|
+
|
|
3591
|
+
t.test('should not found hidden file with wildcard is `false`', (t) => {
|
|
3592
|
+
t.plan(3)
|
|
3593
|
+
|
|
3594
|
+
const pluginOptions = {
|
|
3595
|
+
root: path.join(__dirname, '/static-hidden'),
|
|
3596
|
+
wildcard: false
|
|
3597
|
+
}
|
|
3598
|
+
const fastify = Fastify()
|
|
3599
|
+
fastify.register(fastifyStatic, pluginOptions)
|
|
3600
|
+
|
|
3601
|
+
t.teardown(fastify.close.bind(fastify))
|
|
3602
|
+
|
|
3603
|
+
fastify.listen({ port: 0 }, (err) => {
|
|
3604
|
+
t.error(err)
|
|
3605
|
+
|
|
3606
|
+
fastify.server.unref()
|
|
3607
|
+
|
|
3608
|
+
simple.concat({
|
|
3609
|
+
method: 'GET',
|
|
3610
|
+
url: 'http://localhost:' + fastify.server.address().port + '/.hidden/sample.json'
|
|
3611
|
+
}, (err, response, body) => {
|
|
3612
|
+
t.error(err)
|
|
3613
|
+
t.equal(response.statusCode, 404)
|
|
3614
|
+
})
|
|
3615
|
+
})
|
|
3616
|
+
})
|
|
3617
|
+
|
|
3618
|
+
t.test('should serve files into hidden dir without wildcard option', (t) => {
|
|
3619
|
+
t.plan(9)
|
|
3620
|
+
|
|
3621
|
+
const pluginOptions = {
|
|
3622
|
+
root: path.join(__dirname, '/static-hidden')
|
|
3623
|
+
}
|
|
3624
|
+
const fastify = Fastify()
|
|
3625
|
+
fastify.register(fastifyStatic, pluginOptions)
|
|
3626
|
+
|
|
3627
|
+
t.teardown(fastify.close.bind(fastify))
|
|
3628
|
+
|
|
3629
|
+
fastify.listen({ port: 0 }, (err) => {
|
|
3630
|
+
t.error(err)
|
|
3631
|
+
|
|
3632
|
+
fastify.server.unref()
|
|
3633
|
+
|
|
3634
|
+
simple.concat({
|
|
3635
|
+
method: 'GET',
|
|
3636
|
+
url: 'http://localhost:' + fastify.server.address().port + '/.hidden/sample.json'
|
|
3637
|
+
}, (err, response, body) => {
|
|
3638
|
+
t.error(err)
|
|
3639
|
+
t.equal(response.statusCode, 200)
|
|
3640
|
+
t.equal(body.toString(), jsonHiddenContent)
|
|
3641
|
+
t.ok(/application\/(json)/.test(response.headers['content-type']))
|
|
3642
|
+
t.ok(response.headers.etag)
|
|
3643
|
+
t.ok(response.headers['last-modified'])
|
|
3644
|
+
t.ok(response.headers.date)
|
|
3645
|
+
t.ok(response.headers['cache-control'])
|
|
3646
|
+
})
|
|
3647
|
+
})
|
|
3648
|
+
})
|