@fastify/static 5.0.1 → 5.0.2

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
@@ -159,7 +159,7 @@ Default: `true`
159
159
 
160
160
  If set to `true`, `@fastify/static` adds a wildcard route to serve files.
161
161
  If set to `false`, `@fastify/static` globs the filesystem for all defined
162
- files in the served folder (`${root}/**/*`), and just creates the routes needed for
162
+ files in the served folder (`${root}/**/**`), and just creates the routes needed for
163
163
  those and it will not serve the newly added file on the filesystem.
164
164
 
165
165
  The default options of https://www.npmjs.com/package/glob are applied
package/index.d.ts CHANGED
@@ -58,7 +58,7 @@ interface SendOptions {
58
58
  etag?: boolean;
59
59
  extensions?: string[];
60
60
  immutable?: boolean;
61
- index?: string[] | false;
61
+ index?: string[] | string | false;
62
62
  lastModified?: boolean;
63
63
  maxAge?: string | number;
64
64
  }
@@ -88,7 +88,7 @@ export interface FastifyStaticOptions extends SendOptions {
88
88
  etag?: boolean;
89
89
  extensions?: string[];
90
90
  immutable?: boolean;
91
- index?: string[] | false;
91
+ index?: string[] | string | false;
92
92
  lastModified?: boolean;
93
93
  maxAge?: string | number;
94
94
  }
package/index.js CHANGED
@@ -326,7 +326,7 @@ async function fastifyStatic (fastify, opts) {
326
326
  })
327
327
  }
328
328
  } else {
329
- const globPattern = '**/*'
329
+ const globPattern = '**/**'
330
330
  const indexDirs = new Map()
331
331
  const routes = new Set()
332
332
 
@@ -449,15 +449,18 @@ function getContentType (path) {
449
449
  }
450
450
 
451
451
  function findIndexFile (pathname, root, indexFiles = ['index.html']) {
452
- return indexFiles.find(filename => {
453
- const p = path.join(root, pathname, filename)
454
- try {
455
- const stats = statSync(p)
456
- return !stats.isDirectory()
457
- } catch (e) {
458
- return false
459
- }
460
- })
452
+ if (Array.isArray(indexFiles)) {
453
+ return indexFiles.find(filename => {
454
+ const p = path.join(root, pathname, filename)
455
+ try {
456
+ const stats = statSync(p)
457
+ return !stats.isDirectory()
458
+ } catch (e) {
459
+ return false
460
+ }
461
+ })
462
+ }
463
+ return false
461
464
  }
462
465
 
463
466
  // Adapted from https://github.com/fastify/fastify-compress/blob/665e132fa63d3bf05ad37df3c20346660b71a857/index.js#L451
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/static",
3
- "version": "5.0.1",
3
+ "version": "5.0.2",
4
4
  "description": "Plugin for serving static files as fast as possible.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -0,0 +1,3 @@
1
+ <html>
2
+ <body>index</body>
3
+ </html>
@@ -3480,3 +3480,77 @@ t.test('should not redirect to protocol-relative locations', (t) => {
3480
3480
  })
3481
3481
  })
3482
3482
  })
3483
+
3484
+ t.test('should not serve index if option is `false`', (t) => {
3485
+ t.plan(3)
3486
+
3487
+ const pluginOptions = {
3488
+ root: path.join(__dirname, '/static'),
3489
+ prefix: '/static',
3490
+ index: false
3491
+ }
3492
+ const fastify = Fastify()
3493
+ fastify.register(fastifyStatic, pluginOptions)
3494
+
3495
+ t.teardown(fastify.close.bind(fastify))
3496
+
3497
+ fastify.listen(0, (err) => {
3498
+ t.error(err)
3499
+
3500
+ fastify.server.unref()
3501
+
3502
+ t.test('/static/index.html', (t) => {
3503
+ t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3504
+ simple.concat({
3505
+ method: 'GET',
3506
+ url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
3507
+ }, (err, response, body) => {
3508
+ t.error(err)
3509
+ t.equal(response.statusCode, 200)
3510
+ t.equal(body.toString(), indexContent)
3511
+ genericResponseChecks(t, response)
3512
+ })
3513
+ })
3514
+
3515
+ t.test('/static', (t) => {
3516
+ t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
3517
+ simple.concat({
3518
+ method: 'GET',
3519
+ url: 'http://localhost:' + fastify.server.address().port + '/static'
3520
+ }, (err, response, body) => {
3521
+ t.error(err)
3522
+ t.equal(response.statusCode, 404)
3523
+ genericErrorResponseChecks(t, response)
3524
+ })
3525
+ })
3526
+ })
3527
+ })
3528
+
3529
+ t.test('should follow symbolic link without wildcard', (t) => {
3530
+ t.plan(5)
3531
+ const fastify = Fastify()
3532
+ fastify.register(fastifyStatic, {
3533
+ root: path.join(__dirname, '/static-symbolic-link'),
3534
+ wildcard: false
3535
+ })
3536
+ t.teardown(fastify.close.bind(fastify))
3537
+ fastify.listen(0, (err) => {
3538
+ t.error(err)
3539
+
3540
+ simple.concat({
3541
+ method: 'GET',
3542
+ url: 'http://localhost:' + fastify.server.address().port + '/origin/subdir/subdir/index.html'
3543
+ }, (err, response) => {
3544
+ t.error(err)
3545
+ t.equal(response.statusCode, 200)
3546
+ })
3547
+
3548
+ simple.concat({
3549
+ method: 'GET',
3550
+ url: 'http://localhost:' + fastify.server.address().port + '/dir/symlink/subdir/subdir/index.html'
3551
+ }, (err, response) => {
3552
+ t.error(err)
3553
+ t.equal(response.statusCode, 200)
3554
+ })
3555
+ })
3556
+ })
@@ -28,7 +28,8 @@ const options: FastifyStaticOptions = {
28
28
  }
29
29
 
30
30
  expectError<FastifyStaticOptions>({
31
- wlidcard: '**/*'
31
+ root: '',
32
+ wildcard: '**/**'
32
33
  })
33
34
 
34
35
  appWithImplicitHttp
@@ -103,3 +104,14 @@ noIndexApp
103
104
  reply.send('<h1>fastify-static</h1>')
104
105
  })
105
106
  })
107
+
108
+ const defaultIndexApp = fastify()
109
+ options.index = 'index.html'
110
+
111
+ defaultIndexApp
112
+ .register(fastifyStatic, options)
113
+ .after(() => {
114
+ defaultIndexApp.get('/', (request, reply) => {
115
+ reply.send('<h1>fastify-static</h1>')
116
+ })
117
+ })