@fastify/static 8.1.0 → 8.1.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/.github/workflows/ci.yml +0 -1
- package/README.md +1 -1
- package/index.js +13 -11
- package/package.json +1 -1
- package/test/static-pre-compressed/baz.json +3 -0
- package/test/static.test.js +56 -0
package/.github/workflows/ci.yml
CHANGED
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @fastify/static
|
|
2
2
|
|
|
3
|
-
[](https://github.com/fastify/fastify-static/actions/workflows/ci.yml)
|
|
4
4
|
[](https://www.npmjs.com/package/@fastify/static)
|
|
5
5
|
[](https://github.com/neostandard/neostandard)
|
|
6
6
|
|
package/index.js
CHANGED
|
@@ -290,17 +290,19 @@ async function fastifyStatic (fastify, opts) {
|
|
|
290
290
|
if (metadata.error.code === 'ENOENT') {
|
|
291
291
|
// when preCompress is enabled and the path is a directory without a trailing slash
|
|
292
292
|
if (opts.preCompressed && encoding) {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
293
|
+
if (opts.redirect !== true) {
|
|
294
|
+
const indexPathname = findIndexFile(pathname, options.root, options.index)
|
|
295
|
+
if (indexPathname) {
|
|
296
|
+
return pumpSendToReply(
|
|
297
|
+
request,
|
|
298
|
+
reply,
|
|
299
|
+
pathname + '/',
|
|
300
|
+
rootPath,
|
|
301
|
+
undefined,
|
|
302
|
+
undefined,
|
|
303
|
+
checkedEncodings
|
|
304
|
+
)
|
|
305
|
+
}
|
|
304
306
|
}
|
|
305
307
|
}
|
|
306
308
|
|
package/package.json
CHANGED
package/test/static.test.js
CHANGED
|
@@ -2952,6 +2952,34 @@ test(
|
|
|
2952
2952
|
}
|
|
2953
2953
|
)
|
|
2954
2954
|
|
|
2955
|
+
test(
|
|
2956
|
+
'will redirect to preCompressed index without trailing slash when redirect is true',
|
|
2957
|
+
async (t) => {
|
|
2958
|
+
const pluginOptions = {
|
|
2959
|
+
root: path.join(__dirname, '/static-pre-compressed'),
|
|
2960
|
+
prefix: '/static-pre-compressed/',
|
|
2961
|
+
preCompressed: true,
|
|
2962
|
+
redirect: true,
|
|
2963
|
+
}
|
|
2964
|
+
|
|
2965
|
+
const fastify = Fastify()
|
|
2966
|
+
|
|
2967
|
+
fastify.register(fastifyStatic, pluginOptions)
|
|
2968
|
+
t.after(() => fastify.close())
|
|
2969
|
+
|
|
2970
|
+
const response = await fastify.inject({
|
|
2971
|
+
method: 'GET',
|
|
2972
|
+
url: '/static-pre-compressed/dir',
|
|
2973
|
+
headers: {
|
|
2974
|
+
'accept-encoding': 'gzip, deflate, br'
|
|
2975
|
+
}
|
|
2976
|
+
})
|
|
2977
|
+
|
|
2978
|
+
t.assert.deepStrictEqual(response.statusCode, 301)
|
|
2979
|
+
t.assert.deepStrictEqual(response.headers.location, '/static-pre-compressed/dir/')
|
|
2980
|
+
}
|
|
2981
|
+
)
|
|
2982
|
+
|
|
2955
2983
|
test(
|
|
2956
2984
|
'will serve precompressed gzip index in subdir',
|
|
2957
2985
|
async (t) => {
|
|
@@ -3038,6 +3066,34 @@ test(
|
|
|
3038
3066
|
}
|
|
3039
3067
|
)
|
|
3040
3068
|
|
|
3069
|
+
test(
|
|
3070
|
+
'will not redirect but serve a file if preCompressed but no compressed file exists and redirect is true',
|
|
3071
|
+
async (t) => {
|
|
3072
|
+
const pluginOptions = {
|
|
3073
|
+
root: path.join(__dirname, '/static-pre-compressed'),
|
|
3074
|
+
prefix: '/static-pre-compressed/',
|
|
3075
|
+
preCompressed: true,
|
|
3076
|
+
redirect: true
|
|
3077
|
+
}
|
|
3078
|
+
|
|
3079
|
+
const fastify = Fastify()
|
|
3080
|
+
|
|
3081
|
+
fastify.register(fastifyStatic, pluginOptions)
|
|
3082
|
+
t.after(() => fastify.close())
|
|
3083
|
+
|
|
3084
|
+
const response = await fastify.inject({
|
|
3085
|
+
method: 'GET',
|
|
3086
|
+
url: '/static-pre-compressed/baz.json',
|
|
3087
|
+
headers: {
|
|
3088
|
+
'accept-encoding': '*'
|
|
3089
|
+
}
|
|
3090
|
+
})
|
|
3091
|
+
|
|
3092
|
+
t.assert.deepStrictEqual(response.statusCode, 200)
|
|
3093
|
+
t.assert.deepStrictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
|
|
3094
|
+
}
|
|
3095
|
+
)
|
|
3096
|
+
|
|
3041
3097
|
test(
|
|
3042
3098
|
'nonexistent index with precompressed option',
|
|
3043
3099
|
async (t) => {
|