@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.
@@ -4,7 +4,6 @@ on:
4
4
  push:
5
5
  branches:
6
6
  - main
7
- - master
8
7
  - next
9
8
  - 'v*'
10
9
  paths-ignore:
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @fastify/static
2
2
 
3
- [![CI](https://github.com/fastify/fastify-static/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/fastify-static/actions/workflows/ci.yml)
3
+ [![CI](https://github.com/fastify/fastify-static/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-static/actions/workflows/ci.yml)
4
4
  [![NPM version](https://img.shields.io/npm/v/@fastify/static.svg?style=flat)](https://www.npmjs.com/package/@fastify/static)
5
5
  [![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](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
- const indexPathname = findIndexFile(pathname, options.root, options.index)
294
- if (indexPathname) {
295
- return pumpSendToReply(
296
- request,
297
- reply,
298
- pathname + '/',
299
- rootPath,
300
- undefined,
301
- undefined,
302
- checkedEncodings
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/static",
3
- "version": "8.1.0",
3
+ "version": "8.1.1",
4
4
  "description": "Plugin for serving static files as fast as possible.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -0,0 +1,3 @@
1
+ {
2
+ "baz": "baz"
3
+ }
@@ -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) => {