@fastify/static 7.0.1 → 7.0.3

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.
Files changed (69) hide show
  1. package/.eslintrc.json +0 -0
  2. package/.gitattributes +0 -0
  3. package/.github/dependabot.yml +0 -0
  4. package/.github/stale.yml +0 -0
  5. package/.github/workflows/ci.yml +0 -0
  6. package/.taprc +0 -0
  7. package/LICENSE +0 -0
  8. package/README.md +0 -0
  9. package/example/public/.hidden/sample.json +0 -0
  10. package/example/public/images/sample.jpg +0 -0
  11. package/example/public/index.css +0 -0
  12. package/example/public/index.html +0 -0
  13. package/example/public/index.js +0 -0
  14. package/example/public2/test.css +0 -0
  15. package/example/public2/test.html +0 -0
  16. package/example/server-compress.js +0 -0
  17. package/example/server-dir-list.js +0 -0
  18. package/example/server-hidden-file.js +0 -0
  19. package/example/server.js +0 -0
  20. package/index.js +9 -3
  21. package/lib/dirList.js +0 -0
  22. package/package.json +4 -4
  23. package/test/content-type/binary +0 -0
  24. package/test/content-type/binary.br +0 -0
  25. package/test/content-type/index.css +0 -0
  26. package/test/content-type/index.css.br +0 -0
  27. package/test/content-type/index.html +0 -0
  28. package/test/content-type/index.html.br +0 -0
  29. package/test/content-type/sample.jpg +0 -0
  30. package/test/content-type/test.txt +0 -0
  31. package/test/content-type/test.txt.br +0 -0
  32. package/test/content-type.test.js +0 -0
  33. package/test/dir-list.test.js +0 -0
  34. package/test/static/.example +0 -0
  35. package/test/static/a .md +0 -0
  36. package/test/static/deep/path/for/test/index.html +0 -0
  37. package/test/static/deep/path/for/test/purpose/foo.html +0 -0
  38. package/test/static/foo.html +0 -0
  39. package/test/static/foobar.html +0 -0
  40. package/test/static/index.css +0 -0
  41. package/test/static/index.html +0 -0
  42. package/test/static/shallow/sample.jpg +0 -0
  43. package/test/static-dotfiles/.aaa +0 -0
  44. package/test/static-dotfiles/dir/index.html +0 -0
  45. package/test/static-dotfiles/test.txt +0 -0
  46. package/test/static-encode/[...]/a .md +0 -0
  47. package/test/static-hidden/.hidden/sample.json +0 -0
  48. package/test/static-pre-compressed/all-three.html +0 -0
  49. package/test/static-pre-compressed/all-three.html.br +0 -0
  50. package/test/static-pre-compressed/all-three.html.gz +0 -0
  51. package/test/static-pre-compressed/dir/index.html +0 -0
  52. package/test/static-pre-compressed/dir/index.html.br +0 -0
  53. package/test/static-pre-compressed/dir-gz/index.html +5 -0
  54. package/test/static-pre-compressed/dir-gz/index.html.gz +0 -0
  55. package/test/static-pre-compressed/empty/.gitkeep +0 -0
  56. package/test/static-pre-compressed/gzip-only.html +0 -0
  57. package/test/static-pre-compressed/gzip-only.html.gz +0 -0
  58. package/test/static-pre-compressed/index.html +0 -0
  59. package/test/static-pre-compressed/index.html.br +0 -0
  60. package/test/static-pre-compressed/sample.jpg +0 -0
  61. package/test/static-pre-compressed/sample.jpg.br +0 -0
  62. package/test/static-pre-compressed/uncompressed.html +0 -0
  63. package/test/static-symbolic-link/origin/subdir/subdir/index.html +0 -0
  64. package/test/static.test.js +71 -5
  65. package/test/static2/bar.html +0 -0
  66. package/test/static2/index.html +0 -0
  67. package/tsconfig.eslint.json +0 -0
  68. package/types/index.d.ts +0 -0
  69. package/types/index.test-d.ts +0 -0
package/.eslintrc.json CHANGED
File without changes
package/.gitattributes CHANGED
File without changes
File without changes
package/.github/stale.yml CHANGED
File without changes
File without changes
package/.taprc CHANGED
File without changes
package/LICENSE CHANGED
File without changes
package/README.md CHANGED
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/example/server.js CHANGED
File without changes
package/index.js CHANGED
@@ -111,8 +111,13 @@ async function fastifyStatic (fastify, opts) {
111
111
  throw new Error('"wildcard" option must be a boolean')
112
112
  }
113
113
  if (opts.wildcard === undefined || opts.wildcard === true) {
114
- fastify.get(prefix + '*', { ...routeOpts, exposeHeadRoute: true }, (req, reply) => {
115
- pumpSendToReply(req, reply, '/' + req.params['*'], sendOptions.root)
114
+ fastify.route({
115
+ ...routeOpts,
116
+ method: ['HEAD', 'GET'],
117
+ path: prefix + '*',
118
+ handler (req, reply) {
119
+ pumpSendToReply(req, reply, '/' + req.params['*'], sendOptions.root)
120
+ }
116
121
  })
117
122
  if (opts.redirect === true && prefix !== opts.prefix) {
118
123
  fastify.get(opts.prefix, routeOpts, (req, reply) => {
@@ -174,6 +179,7 @@ async function fastifyStatic (fastify, opts) {
174
179
  pumpOptions,
175
180
  checkedEncodings
176
181
  ) {
182
+ const pathnameOrig = pathname
177
183
  const options = Object.assign({}, sendOptions, pumpOptions)
178
184
 
179
185
  if (rootPath) {
@@ -346,7 +352,7 @@ async function fastifyStatic (fastify, opts) {
346
352
  return pumpSendToReply(
347
353
  request,
348
354
  reply,
349
- pathname,
355
+ pathnameOrig,
350
356
  rootPath,
351
357
  rootPathOffset,
352
358
  undefined,
package/lib/dirList.js CHANGED
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/static",
3
- "version": "7.0.1",
3
+ "version": "7.0.3",
4
4
  "description": "Plugin for serving static files as fast as possible.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -42,8 +42,8 @@
42
42
  "@fastify/compress": "^7.0.0",
43
43
  "@fastify/pre-commit": "^2.0.2",
44
44
  "@types/node": "^20.1.0",
45
- "@typescript-eslint/eslint-plugin": "^6.3.0",
46
- "@typescript-eslint/parser": "^6.3.0",
45
+ "@typescript-eslint/eslint-plugin": "^7.1.0",
46
+ "@typescript-eslint/parser": "^7.1.0",
47
47
  "concat-stream": "^2.0.0",
48
48
  "coveralls": "^3.0.4",
49
49
  "eslint-plugin-typescript": "^0.14.0",
@@ -55,7 +55,7 @@
55
55
  "snazzy": "^9.0.0",
56
56
  "standard": "^17.0.0",
57
57
  "tap": "^16.0.0",
58
- "tsd": "^0.30.0",
58
+ "tsd": "^0.31.0",
59
59
  "typescript": "^5.1.6"
60
60
  },
61
61
  "tsd": {
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/test/static/a .md CHANGED
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,5 @@
1
+ <html>
2
+ <body>
3
+ dir-gz index
4
+ </body>
5
+ </html>
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -46,6 +46,9 @@ const indexBr = fs.readFileSync(
46
46
  const dirIndexBr = fs.readFileSync(
47
47
  './test/static-pre-compressed/dir/index.html.br'
48
48
  )
49
+ const dirIndexGz = fs.readFileSync(
50
+ './test/static-pre-compressed/dir-gz/index.html.gz'
51
+ )
49
52
  const uncompressedStatic = fs
50
53
  .readFileSync('./test/static-pre-compressed/uncompressed.html')
51
54
  .toString('utf8')
@@ -1737,7 +1740,7 @@ t.test('with fastify-compress', t => {
1737
1740
  })
1738
1741
  })
1739
1742
  t.test('register /static/ with schemaHide true', t => {
1740
- t.plan(4)
1743
+ t.plan(3)
1741
1744
 
1742
1745
  const pluginOptions = {
1743
1746
  root: path.join(__dirname, '/static'),
@@ -1777,7 +1780,7 @@ t.test('register /static/ with schemaHide true', t => {
1777
1780
  })
1778
1781
 
1779
1782
  t.test('register /static/ with schemaHide false', t => {
1780
- t.plan(4)
1783
+ t.plan(3)
1781
1784
 
1782
1785
  const pluginOptions = {
1783
1786
  root: path.join(__dirname, '/static'),
@@ -1817,7 +1820,7 @@ t.test('register /static/ with schemaHide false', t => {
1817
1820
  })
1818
1821
 
1819
1822
  t.test('register /static/ without schemaHide', t => {
1820
- t.plan(4)
1823
+ t.plan(3)
1821
1824
 
1822
1825
  const pluginOptions = {
1823
1826
  root: path.join(__dirname, '/static'),
@@ -2954,7 +2957,7 @@ t.test('inject support', async (t) => {
2954
2957
  })
2955
2958
 
2956
2959
  t.test('routes should use custom errorHandler premature stream close', t => {
2957
- t.plan(4)
2960
+ t.plan(3)
2958
2961
 
2959
2962
  const pluginOptions = {
2960
2963
  root: path.join(__dirname, '/static'),
@@ -2989,7 +2992,7 @@ t.test('routes should use custom errorHandler premature stream close', t => {
2989
2992
  })
2990
2993
 
2991
2994
  t.test('routes should fallback to default errorHandler', t => {
2992
- t.plan(4)
2995
+ t.plan(3)
2993
2996
 
2994
2997
  const pluginOptions = {
2995
2998
  root: path.join(__dirname, '/static'),
@@ -3496,6 +3499,35 @@ t.test(
3496
3499
  }
3497
3500
  )
3498
3501
 
3502
+ t.test(
3503
+ 'will serve precompressed gzip index in subdir',
3504
+ async (t) => {
3505
+ const pluginOptions = {
3506
+ root: path.join(__dirname, '/static-pre-compressed'),
3507
+ preCompressed: true
3508
+ }
3509
+
3510
+ const fastify = Fastify()
3511
+
3512
+ fastify.register(fastifyStatic, pluginOptions)
3513
+ t.teardown(fastify.close.bind(fastify))
3514
+
3515
+ const response = await fastify.inject({
3516
+ method: 'GET',
3517
+ url: '/dir-gz',
3518
+ headers: {
3519
+ 'accept-encoding': 'gzip, deflate, br'
3520
+ }
3521
+ })
3522
+
3523
+ genericResponseChecks(t, response)
3524
+ t.equal(response.headers['content-encoding'], 'gzip')
3525
+ t.equal(response.statusCode, 200)
3526
+ t.same(response.rawPayload, dirIndexGz)
3527
+ t.end()
3528
+ }
3529
+ )
3530
+
3499
3531
  t.test(
3500
3532
  'will serve precompressed index with alternative index option',
3501
3533
  async (t) => {
@@ -3930,3 +3962,37 @@ t.test(
3930
3962
  t.same(response2.body, aContent)
3931
3963
  }
3932
3964
  )
3965
+
3966
+ t.test('content-length in head route should not return zero when using wildcard', t => {
3967
+ t.plan(6)
3968
+
3969
+ const pluginOptions = {
3970
+ root: path.join(__dirname, '/static')
3971
+ }
3972
+ const fastify = Fastify()
3973
+
3974
+ fastify.register(fastifyStatic, pluginOptions)
3975
+
3976
+ t.teardown(fastify.close.bind(fastify))
3977
+
3978
+ fastify.listen({ port: 0 }, err => {
3979
+ t.error(err)
3980
+
3981
+ fastify.server.unref()
3982
+
3983
+ const file = fs.readFileSync(path.join(__dirname, '/static/index.html'))
3984
+ const contentLength = Buffer.byteLength(file).toString()
3985
+
3986
+ simple.concat({
3987
+ method: 'HEAD',
3988
+ url: 'http://localhost:' + fastify.server.address().port + '/index.html',
3989
+ followRedirect: false
3990
+ }, (err, response, body) => {
3991
+ t.error(err)
3992
+ t.equal(response.statusCode, 200)
3993
+ t.equal(response.headers['content-type'], 'text/html; charset=UTF-8')
3994
+ t.equal(response.headers['content-length'], contentLength)
3995
+ t.equal(body.toString(), '')
3996
+ })
3997
+ })
3998
+ })
File without changes
File without changes
File without changes
package/types/index.d.ts CHANGED
File without changes
File without changes