@fastify/static 6.5.1 → 6.6.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/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 provide the directory list calling the directory path.
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
@@ -4,7 +4,7 @@ const path = require('path')
4
4
  const statSync = require('fs').statSync
5
5
  const { PassThrough } = require('readable-stream')
6
6
  const glob = require('glob')
7
- const send = require('send')
7
+ const send = require('@fastify/send')
8
8
  const contentDisposition = require('content-disposition')
9
9
  const fp = require('fastify-plugin')
10
10
  const util = require('util')
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/static",
3
- "version": "6.5.1",
3
+ "version": "6.6.1",
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
- "send": "^0.18.0"
38
+ "@fastify/send": "^1.0.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@fastify/compress": "^6.0.0",
@@ -54,7 +54,7 @@
54
54
  "snazzy": "^9.0.0",
55
55
  "standard": "^17.0.0",
56
56
  "tap": "^16.0.0",
57
- "tsd": "^0.24.1"
57
+ "tsd": "^0.25.0"
58
58
  },
59
59
  "tsd": {
60
60
  "directory": "test/types"
@@ -0,0 +1 @@
1
+ {"hello": "world"}
@@ -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) {
@@ -1335,7 +1336,7 @@ t.test('send options', (t) => {
1335
1336
  }
1336
1337
  const fastify = Fastify({ logger: false })
1337
1338
  const fastifyStatic = require('proxyquire')('../', {
1338
- send: function sendStub (req, pathName, options) {
1339
+ '@fastify/send': function sendStub (req, pathName, options) {
1339
1340
  t.equal(pathName, '/index.html')
1340
1341
  t.equal(options.root, path.join(__dirname, '/static'))
1341
1342
  t.equal(options.acceptRanges, 'acceptRanges')
@@ -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
+ })
package/types/index.d.ts CHANGED
@@ -73,6 +73,7 @@ declare namespace fastifyStatic {
73
73
  index?: string[] | string | false;
74
74
  lastModified?: boolean;
75
75
  maxAge?: string | number;
76
+ serveDotFiles?: boolean;
76
77
  }
77
78
 
78
79
  export interface FastifyStaticOptions extends SendOptions {