@fastify/static 6.7.0 → 6.9.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/index.js +11 -8
- package/package.json +2 -2
- package/test/static.test.js +57 -0
package/index.js
CHANGED
|
@@ -27,6 +27,10 @@ async function fastifyStatic (fastify, opts) {
|
|
|
27
27
|
throw invalidDirListOpts
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
if (opts.dotfiles === undefined) {
|
|
31
|
+
opts.dotfiles = 'allow'
|
|
32
|
+
}
|
|
33
|
+
|
|
30
34
|
const sendOptions = {
|
|
31
35
|
root: opts.root,
|
|
32
36
|
acceptRanges: opts.acceptRanges,
|
|
@@ -37,8 +41,7 @@ async function fastifyStatic (fastify, opts) {
|
|
|
37
41
|
immutable: opts.immutable,
|
|
38
42
|
index: opts.index,
|
|
39
43
|
lastModified: opts.lastModified,
|
|
40
|
-
maxAge: opts.maxAge
|
|
41
|
-
serveDotFiles: opts.serveDotFiles ?? false
|
|
44
|
+
maxAge: opts.maxAge
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
const allowedPath = opts.allowedPath
|
|
@@ -237,7 +240,7 @@ async function fastifyStatic (fastify, opts) {
|
|
|
237
240
|
reply,
|
|
238
241
|
pathname,
|
|
239
242
|
rootPath,
|
|
240
|
-
|
|
243
|
+
rootPathOffset,
|
|
241
244
|
undefined,
|
|
242
245
|
checkedEncodings
|
|
243
246
|
)
|
|
@@ -337,7 +340,7 @@ async function fastifyStatic (fastify, opts) {
|
|
|
337
340
|
const winSeparatorRegex = new RegExp(`\\${path.win32.sep}`, 'g')
|
|
338
341
|
|
|
339
342
|
for (const rootPath of Array.isArray(sendOptions.root) ? sendOptions.root : [sendOptions.root]) {
|
|
340
|
-
const files = await globPromise(path.join(rootPath, globPattern).replace(winSeparatorRegex, path.posix.sep), { nodir: true, dot:
|
|
343
|
+
const files = await globPromise(path.join(rootPath, globPattern).replace(winSeparatorRegex, path.posix.sep), { nodir: true, dot: opts.serveDotFiles })
|
|
341
344
|
const indexes = typeof opts.index === 'undefined' ? ['index.html'] : [].concat(opts.index)
|
|
342
345
|
|
|
343
346
|
for (let file of files) {
|
|
@@ -447,12 +450,12 @@ function checkPath (fastify, rootPath) {
|
|
|
447
450
|
const supportedEncodings = ['br', 'gzip', 'deflate']
|
|
448
451
|
|
|
449
452
|
function getContentType (path) {
|
|
450
|
-
const type = send.mime.
|
|
451
|
-
|
|
452
|
-
if (!
|
|
453
|
+
const type = send.mime.getType(path)
|
|
454
|
+
|
|
455
|
+
if (!send.isUtf8MimeType(type)) {
|
|
453
456
|
return type
|
|
454
457
|
}
|
|
455
|
-
return `${type}; charset
|
|
458
|
+
return `${type}; charset=UTF-8`
|
|
456
459
|
}
|
|
457
460
|
|
|
458
461
|
function findIndexFile (pathname, root, indexFiles = ['index.html']) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/static",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.9.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",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"glob": "^8.0.1",
|
|
36
36
|
"p-limit": "^3.1.0",
|
|
37
37
|
"readable-stream": "^4.0.0",
|
|
38
|
-
"@fastify/send": "^
|
|
38
|
+
"@fastify/send": "^2.0.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@fastify/compress": "^6.0.0",
|
package/test/static.test.js
CHANGED
|
@@ -3646,3 +3646,60 @@ t.test('should serve files into hidden dir without wildcard option', (t) => {
|
|
|
3646
3646
|
})
|
|
3647
3647
|
})
|
|
3648
3648
|
})
|
|
3649
|
+
|
|
3650
|
+
t.test(
|
|
3651
|
+
'will serve pre-compressed files with .gzip if multi-root',
|
|
3652
|
+
async (t) => {
|
|
3653
|
+
const pluginOptions = {
|
|
3654
|
+
root: [path.join(__dirname, '/static-pre-compressed'), path.join(__dirname, '/static')],
|
|
3655
|
+
preCompressed: true
|
|
3656
|
+
}
|
|
3657
|
+
|
|
3658
|
+
const fastify = Fastify()
|
|
3659
|
+
|
|
3660
|
+
fastify.register(fastifyStatic, pluginOptions)
|
|
3661
|
+
t.teardown(fastify.close.bind(fastify))
|
|
3662
|
+
|
|
3663
|
+
const response = await fastify.inject({
|
|
3664
|
+
method: 'GET',
|
|
3665
|
+
url: 'all-three.html',
|
|
3666
|
+
headers: {
|
|
3667
|
+
'accept-encoding': '*, *'
|
|
3668
|
+
}
|
|
3669
|
+
})
|
|
3670
|
+
|
|
3671
|
+
genericResponseChecks(t, response)
|
|
3672
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
3673
|
+
t.equal(response.statusCode, 200)
|
|
3674
|
+
t.same(response.rawPayload, allThreeGzip)
|
|
3675
|
+
t.end()
|
|
3676
|
+
}
|
|
3677
|
+
)
|
|
3678
|
+
|
|
3679
|
+
t.test(
|
|
3680
|
+
'will still serve un-compressed files with multi-root and preCompressed as true',
|
|
3681
|
+
async (t) => {
|
|
3682
|
+
const pluginOptions = {
|
|
3683
|
+
root: [path.join(__dirname, '/static-pre-compressed'), path.join(__dirname, '/static')],
|
|
3684
|
+
preCompressed: true
|
|
3685
|
+
}
|
|
3686
|
+
|
|
3687
|
+
const fastify = Fastify()
|
|
3688
|
+
|
|
3689
|
+
fastify.register(fastifyStatic, pluginOptions)
|
|
3690
|
+
t.teardown(fastify.close.bind(fastify))
|
|
3691
|
+
|
|
3692
|
+
const response = await fastify.inject({
|
|
3693
|
+
method: 'GET',
|
|
3694
|
+
url: 'foobar.html',
|
|
3695
|
+
headers: {
|
|
3696
|
+
'accept-encoding': '*, *'
|
|
3697
|
+
}
|
|
3698
|
+
})
|
|
3699
|
+
|
|
3700
|
+
genericResponseChecks(t, response)
|
|
3701
|
+
t.equal(response.statusCode, 200)
|
|
3702
|
+
t.same(response.body, foobarContent)
|
|
3703
|
+
t.end()
|
|
3704
|
+
}
|
|
3705
|
+
)
|