@fastify/static 5.0.0 → 6.0.0

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.
@@ -1,46 +1,17 @@
1
1
  name: CI
2
- 'on':
2
+
3
+ on:
3
4
  push:
4
5
  paths-ignore:
5
- - docs/**
6
+ - 'docs/**'
6
7
  - '*.md'
7
8
  pull_request:
8
9
  paths-ignore:
9
- - docs/**
10
+ - 'docs/**'
10
11
  - '*.md'
12
+
11
13
  jobs:
12
14
  test:
13
- runs-on: ${{ matrix.os }}
14
- strategy:
15
- matrix:
16
- node-version:
17
- - 10
18
- - 12
19
- - 14
20
- - 16
21
- os:
22
- - macos-latest
23
- - ubuntu-latest
24
- - windows-latest
25
- steps:
26
- - uses: actions/checkout@v3
27
- - name: Use Node.js
28
- uses: actions/setup-node@v3
29
- with:
30
- node-version: ${{ matrix.node-version }}
31
- - name: Install Dependencies
32
- run: |
33
- npm install --ignore-scripts
34
- - name: Run Tests
35
- run: |
36
- npm test
37
- automerge:
38
- needs: test
39
- runs-on: ubuntu-latest
40
- permissions:
41
- pull-requests: write
42
- contents: write
43
- steps:
44
- - uses: fastify/github-action-merge-dependabot@v3
45
- with:
46
- github-token: ${{ secrets.GITHUB_TOKEN }}
15
+ uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
16
+ with:
17
+ lint: true
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
- # fastify-static
1
+ # @fastify/static
2
2
 
3
3
  ![CI](https://github.com/fastify/fastify-static/workflows/CI/badge.svg)
4
- [![NPM version](https://img.shields.io/npm/v/fastify-static.svg?style=flat)](https://www.npmjs.com/package/fastify-static)
4
+ [![NPM version](https://img.shields.io/npm/v/@fastify/static.svg?style=flat)](https://www.npmjs.com/package/@fastify/static)
5
5
  [![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-static/badge.svg)](https://snyk.io/test/github/fastify/fastify-static)
6
6
  [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
7
7
 
@@ -12,7 +12,7 @@ Please refer to [this branch](https://github.com/fastify/fastify-static/tree/1.x
12
12
 
13
13
  ## Install
14
14
 
15
- `npm install --save fastify-static`
15
+ `npm install --save @fastify/static`
16
16
 
17
17
  ## Usage
18
18
 
@@ -20,7 +20,7 @@ Please refer to [this branch](https://github.com/fastify/fastify-static/tree/1.x
20
20
  const fastify = require('fastify')()
21
21
  const path = require('path')
22
22
 
23
- fastify.register(require('fastify-static'), {
23
+ fastify.register(require('@fastify/static'), {
24
24
  root: path.join(__dirname, 'public'),
25
25
  prefix: '/public/', // optional: default '/'
26
26
  })
@@ -42,7 +42,7 @@ fastify.get('/another/path', function (req, reply) {
42
42
 
43
43
  ```js
44
44
  const fastify = require('fastify')()
45
- const fastifyStatic = require('fastify-static')
45
+ const fastifyStatic = require('@fastify/static')
46
46
  const path = require('path')
47
47
  // first plugin
48
48
  fastify.register(fastifyStatic, {
@@ -64,7 +64,7 @@ fastify.register(fastifyStatic, {
64
64
  const fastify = require('fastify')()
65
65
  const path = require('path')
66
66
 
67
- fastify.register(require('fastify-static'), {
67
+ fastify.register(require('@fastify/static'), {
68
68
  root: path.join(__dirname, 'public'),
69
69
  prefix: '/public/', // optional: default '/'
70
70
  })
@@ -145,7 +145,7 @@ You're able to alter this options when calling `reply.download('filename.html',
145
145
 
146
146
  Default: `false`
147
147
 
148
- If set to `true`, `fastify-static` redirects to the directory with a trailing slash.
148
+ If set to `true`, `@fastify/static` redirects to the directory with a trailing slash.
149
149
 
150
150
  This option cannot be set to `true` with `wildcard` set to `false` on a server
151
151
  with `ignoreTrailingSlash` set to `true`.
@@ -157,9 +157,9 @@ slash will trigger your app's 404 handler using `reply.callNotFound()`.
157
157
 
158
158
  Default: `true`
159
159
 
160
- If set to `true`, `fastify-static` adds a wildcard route to serve files.
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
160
+ If set to `true`, `@fastify/static` adds a wildcard route to serve files.
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
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
@@ -196,7 +196,7 @@ Note: Multi-root is not supported within the `list` option.
196
196
  **Example:**
197
197
 
198
198
  ```js
199
- fastify.register(require('fastify-static'), {
199
+ fastify.register(require('@fastify/static'), {
200
200
  root: path.join(__dirname, 'public'),
201
201
  prefix: '/public/',
202
202
  index: false
@@ -235,7 +235,7 @@ will return the response as json independent of `list.format`.
235
235
  **Example:**
236
236
 
237
237
  ```js
238
- fastify.register(require('fastify-static'), {
238
+ fastify.register(require('@fastify/static'), {
239
239
  root: path.join(__dirname, 'public'),
240
240
  prefix: '/public/',
241
241
  list: {
@@ -290,7 +290,7 @@ Note: if a file with the same name exists, the actual file is sent.
290
290
  **Example:**
291
291
 
292
292
  ```js
293
- fastify.register(require('fastify-static'), {
293
+ fastify.register(require('@fastify/static'), {
294
294
  root: path.join(__dirname, '/static'),
295
295
  prefix: '/public',
296
296
  prefixAvoidTrailingSlash: true,
@@ -408,7 +408,7 @@ If you would just like to use the reply decorator and not serve whole directorie
408
408
  #### Disabling reply decorator
409
409
 
410
410
  The reply object is decorated with a `sendFile` function by default. If you want to
411
- disable this, pass the option `{ decorateReply: false }`. If fastify-static is
411
+ disable this, pass the option `{ decorateReply: false }`. If @fastify/static is
412
412
  registered to multiple prefixes in the same route only one can initialize reply
413
413
  decorators.
414
414
 
@@ -5,7 +5,7 @@ const fastify = require('fastify')({ logger: { level: 'trace' } })
5
5
 
6
6
  fastify
7
7
  // Compress everything.
8
- .register(require('fastify-compress'), { threshold: 0 })
8
+ .register(require('@fastify/compress'), { threshold: 0 })
9
9
  .register(require('../'), {
10
10
  // An absolute path containing static files to serve.
11
11
  root: path.join(__dirname, '/public')
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,11 +88,11 @@ 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
  }
95
95
 
96
- declare const fastifyStatic: FastifyPluginCallback<FastifyStaticOptions>
96
+ export declare const fastifyStatic: FastifyPluginCallback<FastifyStaticOptions>
97
97
 
98
98
  export default fastifyStatic;
package/index.js CHANGED
@@ -121,6 +121,7 @@ async function fastifyStatic (fastify, opts) {
121
121
 
122
122
  wrap.getHeader = reply.getHeader.bind(reply)
123
123
  wrap.setHeader = reply.header.bind(reply)
124
+ wrap.removeHeader = () => {}
124
125
  wrap.finished = false
125
126
 
126
127
  Object.defineProperty(wrap, 'filename', {
@@ -274,7 +275,7 @@ async function fastifyStatic (fastify, opts) {
274
275
  schema: {
275
276
  hide: typeof opts.schemaHide !== 'undefined' ? opts.schemaHide : true
276
277
  },
277
- errorHandler: fastify.errorHandler ? errorHandler : undefined
278
+ errorHandler
278
279
  }
279
280
 
280
281
  if (opts.decorateReply !== false) {
@@ -326,7 +327,7 @@ async function fastifyStatic (fastify, opts) {
326
327
  })
327
328
  }
328
329
  } else {
329
- const globPattern = '**/*'
330
+ const globPattern = '**/**'
330
331
  const indexDirs = new Map()
331
332
  const routes = new Set()
332
333
 
@@ -338,7 +339,7 @@ async function fastifyStatic (fastify, opts) {
338
339
  file = file
339
340
  .replace(rootPath.replace(/\\/g, '/'), '')
340
341
  .replace(/^\//, '')
341
- const route = encodeURI(prefix + file).replace(/\/\//g, '/')
342
+ const route = (prefix + file).replace(/\/\//g, '/')
342
343
  if (routes.has(route)) {
343
344
  continue
344
345
  }
@@ -449,15 +450,21 @@ function getContentType (path) {
449
450
  }
450
451
 
451
452
  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
- })
453
+ // TODO remove istanbul ignore
454
+ /* istanbul ignore else */
455
+ if (Array.isArray(indexFiles)) {
456
+ return indexFiles.find(filename => {
457
+ const p = path.join(root, pathname, filename)
458
+ try {
459
+ const stats = statSync(p)
460
+ return !stats.isDirectory()
461
+ } catch (e) {
462
+ return false
463
+ }
464
+ })
465
+ }
466
+ /* istanbul ignore next */
467
+ return false
461
468
  }
462
469
 
463
470
  // Adapted from https://github.com/fastify/fastify-compress/blob/665e132fa63d3bf05ad37df3c20346660b71a857/index.js#L451
@@ -504,6 +511,6 @@ function getRedirectUrl (url) {
504
511
  }
505
512
 
506
513
  module.exports = fp(fastifyStatic, {
507
- fastify: '3.x',
508
- name: 'fastify-static'
514
+ fastify: '4.x',
515
+ name: '@fastify/static'
509
516
  })
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@fastify/static",
3
- "version": "5.0.0",
3
+ "version": "6.0.0",
4
4
  "description": "Plugin for serving static files as fast as possible.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
7
7
  "scripts": {
8
8
  "lint": "standard | snazzy",
9
9
  "lint:fix": "standard --fix",
10
- "unit": "tap --no-check-coverage test/*.test.js",
10
+ "unit": "tap test/*.test.js",
11
11
  "typescript": "tsd",
12
12
  "test": "npm run lint && npm run unit && npm run typescript",
13
13
  "example": "node example/server.js",
14
- "coverage": "tap --cov --coverage-report=html --no-check-coverage test",
14
+ "coverage": "tap --cov --coverage-report=html test",
15
15
  "coveralls": "tap test/*test.js test/*/*.test.js --cov"
16
16
  },
17
17
  "repository": {
@@ -35,17 +35,17 @@
35
35
  "glob": "^7.1.4",
36
36
  "p-limit": "^3.1.0",
37
37
  "readable-stream": "^3.4.0",
38
- "send": "^0.17.1"
38
+ "send": "^0.18.0"
39
39
  },
40
40
  "devDependencies": {
41
+ "@fastify/compress": "^6.0.0",
41
42
  "@types/node": "^17.0.0",
42
43
  "@typescript-eslint/eslint-plugin": "^2.29.0",
43
44
  "@typescript-eslint/parser": "^2.29.0",
44
45
  "concat-stream": "^2.0.0",
45
46
  "coveralls": "^3.0.4",
46
47
  "eslint-plugin-typescript": "^0.14.0",
47
- "fastify": "^3.7.0",
48
- "fastify-compress": "^4.0.0",
48
+ "fastify": "^4.0.0-rc.2",
49
49
  "handlebars": "^4.7.6",
50
50
  "pre-commit": "^1.2.2",
51
51
  "proxyquire": "^2.1.0",
@@ -19,7 +19,7 @@ const helper = {
19
19
  const fastify = Fastify()
20
20
  fastify.register(mock, options)
21
21
  t.teardown(fastify.close.bind(fastify))
22
- fastify.listen(0, err => {
22
+ fastify.listen({ port: 0 }, err => {
23
23
  t.error(err)
24
24
  fastify.server.unref()
25
25
  f('http://localhost:' + fastify.server.address().port)
@@ -102,7 +102,7 @@ t.test('dir list wrong options', t => {
102
102
  for (const case_ of cases) {
103
103
  const fastify = Fastify()
104
104
  fastify.register(fastifyStatic, case_.options)
105
- fastify.listen(0, err => {
105
+ fastify.listen({ port: 0 }, err => {
106
106
  t.equal(err.message, case_.error.message)
107
107
  fastify.server.unref()
108
108
  })
@@ -0,0 +1,3 @@
1
+ <html>
2
+ <body>index</body>
3
+ </html>
@@ -9,8 +9,7 @@ const http = require('http')
9
9
  const t = require('tap')
10
10
  const simple = require('simple-get')
11
11
  const Fastify = require('fastify')
12
- const { kErrorHandler } = require('fastify/lib/symbols')
13
- const compress = require('fastify-compress')
12
+ const compress = require('@fastify/compress')
14
13
  const concat = require('concat-stream')
15
14
  const pino = require('pino')
16
15
  const proxyquire = require('proxyquire')
@@ -81,7 +80,7 @@ t.test('register /static prefixAvoidTrailingSlash', t => {
81
80
 
82
81
  t.teardown(fastify.close.bind(fastify))
83
82
 
84
- fastify.listen(0, (err) => {
83
+ fastify.listen({ port: 0 }, (err) => {
85
84
  t.error(err)
86
85
 
87
86
  fastify.server.unref()
@@ -241,7 +240,7 @@ t.test('register /static', (t) => {
241
240
 
242
241
  t.teardown(fastify.close.bind(fastify))
243
242
 
244
- fastify.listen(0, (err) => {
243
+ fastify.listen({ port: 0 }, (err) => {
245
244
  t.error(err)
246
245
 
247
246
  fastify.server.unref()
@@ -386,7 +385,7 @@ t.test('register /static/', t => {
386
385
 
387
386
  t.teardown(fastify.close.bind(fastify))
388
387
 
389
- fastify.listen(0, (err) => {
388
+ fastify.listen({ port: 0 }, (err) => {
390
389
  t.error(err)
391
390
 
392
391
  fastify.server.unref()
@@ -565,7 +564,7 @@ t.test('register /static and /static2', (t) => {
565
564
 
566
565
  t.teardown(fastify.close.bind(fastify))
567
566
 
568
- fastify.listen(0, (err) => {
567
+ fastify.listen({ port: 0 }, (err) => {
569
568
  t.error(err)
570
569
 
571
570
  fastify.server.unref()
@@ -642,7 +641,7 @@ t.test('payload.filename is set', (t) => {
642
641
 
643
642
  t.teardown(fastify.close.bind(fastify))
644
643
 
645
- fastify.listen(0, (err) => {
644
+ fastify.listen({ port: 0 }, (err) => {
646
645
  t.error(err)
647
646
 
648
647
  fastify.server.unref()
@@ -687,14 +686,14 @@ t.test('error responses can be customized with fastify.setErrorHandler()', t =>
687
686
  const fastify = Fastify()
688
687
 
689
688
  fastify.setErrorHandler(function errorHandler (err, request, reply) {
690
- reply.type('text/plain').send(err.status + ' Custom error message')
689
+ reply.code(403).type('text/plain').send(err.status + ' Custom error message')
691
690
  })
692
691
 
693
692
  fastify.register(fastifyStatic, pluginOptions)
694
693
 
695
694
  t.teardown(fastify.close.bind(fastify))
696
695
 
697
- fastify.listen(0, err => {
696
+ fastify.listen({ port: 0 }, err => {
698
697
  t.error(err)
699
698
 
700
699
  fastify.server.unref()
@@ -732,7 +731,7 @@ t.test('not found responses can be customized with fastify.setNotFoundHandler()'
732
731
 
733
732
  t.teardown(fastify.close.bind(fastify))
734
733
 
735
- fastify.listen(0, err => {
734
+ fastify.listen({ port: 0 }, err => {
736
735
  t.error(err)
737
736
 
738
737
  fastify.server.unref()
@@ -773,7 +772,7 @@ t.test('fastify.setNotFoundHandler() is called for dotfiles when when send is co
773
772
 
774
773
  t.teardown(fastify.close.bind(fastify))
775
774
 
776
- fastify.listen(0, err => {
775
+ fastify.listen({ port: 0 }, err => {
777
776
  t.error(err)
778
777
 
779
778
  fastify.server.unref()
@@ -814,7 +813,7 @@ t.test('serving disabled', (t) => {
814
813
 
815
814
  t.teardown(fastify.close.bind(fastify))
816
815
 
817
- fastify.listen(0, (err) => {
816
+ fastify.listen({ port: 0 }, (err) => {
818
817
  t.error(err)
819
818
 
820
819
  fastify.server.unref()
@@ -871,7 +870,7 @@ t.test('sendFile', (t) => {
871
870
  return reply.sendFile('/index.html', { maxAge })
872
871
  })
873
872
 
874
- fastify.listen(0, (err) => {
873
+ fastify.listen({ port: 0 }, (err) => {
875
874
  t.error(err)
876
875
 
877
876
  fastify.server.unref()
@@ -954,7 +953,7 @@ t.test('sendFile disabled', (t) => {
954
953
  }
955
954
  })
956
955
 
957
- fastify.listen(0, (err) => {
956
+ fastify.listen({ port: 0 }, (err) => {
958
957
  t.error(err)
959
958
 
960
959
  fastify.server.unref()
@@ -983,7 +982,7 @@ t.test('allowedPath option', (t) => {
983
982
  }
984
983
  const fastify = Fastify()
985
984
  fastify.register(fastifyStatic, pluginOptions)
986
- fastify.listen(0, (err) => {
985
+ fastify.listen({ port: 0 }, (err) => {
987
986
  t.error(err)
988
987
 
989
988
  fastify.server.unref()
@@ -1072,7 +1071,7 @@ t.test('download', (t) => {
1072
1071
  })
1073
1072
  })
1074
1073
 
1075
- fastify.listen(0, (err) => {
1074
+ fastify.listen({ port: 0 }, (err) => {
1076
1075
  t.error(err)
1077
1076
 
1078
1077
  fastify.server.unref()
@@ -1190,7 +1189,7 @@ t.test('sendFile disabled', (t) => {
1190
1189
  }
1191
1190
  })
1192
1191
 
1193
- fastify.listen(0, (err) => {
1192
+ fastify.listen({ port: 0 }, (err) => {
1194
1193
  t.error(err)
1195
1194
 
1196
1195
  fastify.server.unref()
@@ -1230,7 +1229,7 @@ t.test('download disabled', (t) => {
1230
1229
  }
1231
1230
  })
1232
1231
 
1233
- fastify.listen(0, (err) => {
1232
+ fastify.listen({ port: 0 }, (err) => {
1234
1233
  t.error(err)
1235
1234
 
1236
1235
  fastify.server.unref()
@@ -1272,7 +1271,7 @@ t.test('root not found warning', (t) => {
1272
1271
  )
1273
1272
  const fastify = Fastify({ logger })
1274
1273
  fastify.register(fastifyStatic, pluginOptions)
1275
- fastify.listen(0, (err) => {
1274
+ fastify.listen({ port: 0 }, (err) => {
1276
1275
  t.error(err)
1277
1276
  fastify.server.unref()
1278
1277
  destination.end()
@@ -1329,7 +1328,7 @@ t.test('setHeaders option', (t) => {
1329
1328
 
1330
1329
  t.teardown(fastify.close.bind(fastify))
1331
1330
 
1332
- fastify.listen(0, (err) => {
1331
+ fastify.listen({ port: 0 }, (err) => {
1333
1332
  t.error(err)
1334
1333
 
1335
1334
  fastify.server.unref()
@@ -1360,7 +1359,7 @@ t.test('maxAge option', (t) => {
1360
1359
 
1361
1360
  t.teardown(fastify.close.bind(fastify))
1362
1361
 
1363
- fastify.listen(0, (err) => {
1362
+ fastify.listen({ port: 0 }, (err) => {
1364
1363
  t.error(err)
1365
1364
 
1366
1365
  fastify.server.unref()
@@ -1499,7 +1498,7 @@ t.test('register no prefix', (t) => {
1499
1498
 
1500
1499
  t.teardown(fastify.close.bind(fastify))
1501
1500
 
1502
- fastify.listen(0, (err) => {
1501
+ fastify.listen({ port: 0 }, (err) => {
1503
1502
  t.error(err)
1504
1503
 
1505
1504
  fastify.server.unref()
@@ -1607,7 +1606,7 @@ t.test('with fastify-compress', t => {
1607
1606
 
1608
1607
  t.teardown(fastify.close.bind(fastify))
1609
1608
 
1610
- fastify.listen(0, err => {
1609
+ fastify.listen({ port: 0 }, err => {
1611
1610
  t.error(err)
1612
1611
 
1613
1612
  t.test('deflate', function (t) {
@@ -1643,7 +1642,7 @@ t.test('with fastify-compress', t => {
1643
1642
  })
1644
1643
  })
1645
1644
  t.test('register /static/ with schemaHide true', t => {
1646
- t.plan(4)
1645
+ t.plan(5)
1647
1646
 
1648
1647
  const pluginOptions = {
1649
1648
  root: path.join(__dirname, '/static'),
@@ -1661,7 +1660,7 @@ t.test('register /static/ with schemaHide true', t => {
1661
1660
 
1662
1661
  t.teardown(fastify.close.bind(fastify))
1663
1662
 
1664
- fastify.listen(0, (err) => {
1663
+ fastify.listen({ port: 0 }, (err) => {
1665
1664
  t.error(err)
1666
1665
 
1667
1666
  fastify.server.unref()
@@ -1683,7 +1682,7 @@ t.test('register /static/ with schemaHide true', t => {
1683
1682
  })
1684
1683
 
1685
1684
  t.test('register /static/ with schemaHide false', t => {
1686
- t.plan(4)
1685
+ t.plan(5)
1687
1686
 
1688
1687
  const pluginOptions = {
1689
1688
  root: path.join(__dirname, '/static'),
@@ -1701,7 +1700,7 @@ t.test('register /static/ with schemaHide false', t => {
1701
1700
 
1702
1701
  t.teardown(fastify.close.bind(fastify))
1703
1702
 
1704
- fastify.listen(0, (err) => {
1703
+ fastify.listen({ port: 0 }, (err) => {
1705
1704
  t.error(err)
1706
1705
 
1707
1706
  fastify.server.unref()
@@ -1723,7 +1722,7 @@ t.test('register /static/ with schemaHide false', t => {
1723
1722
  })
1724
1723
 
1725
1724
  t.test('register /static/ without schemaHide', t => {
1726
- t.plan(4)
1725
+ t.plan(5)
1727
1726
 
1728
1727
  const pluginOptions = {
1729
1728
  root: path.join(__dirname, '/static'),
@@ -1740,7 +1739,7 @@ t.test('register /static/ without schemaHide', t => {
1740
1739
 
1741
1740
  t.teardown(fastify.close.bind(fastify))
1742
1741
 
1743
- fastify.listen(0, (err) => {
1742
+ fastify.listen({ port: 0 }, (err) => {
1744
1743
  t.error(err)
1745
1744
 
1746
1745
  fastify.server.unref()
@@ -1768,16 +1767,12 @@ t.test('fastify with exposeHeadRoutes', t => {
1768
1767
  root: path.join(__dirname, '/static'),
1769
1768
  wildcard: false
1770
1769
  }
1771
- const fastify = Fastify({ exposeHeadRoutes: true })
1770
+ const fastify = Fastify()
1772
1771
  fastify.register(fastifyStatic, pluginOptions)
1773
1772
 
1774
- fastify.get('/*', (request, reply) => {
1775
- reply.send({ hello: 'world' })
1776
- })
1777
-
1778
1773
  t.teardown(fastify.close.bind(fastify))
1779
1774
 
1780
- fastify.listen(0, err => {
1775
+ fastify.listen({ port: 0 }, err => {
1781
1776
  t.error(err)
1782
1777
 
1783
1778
  fastify.server.unref()
@@ -1813,7 +1808,7 @@ t.test('register with wildcard false', t => {
1813
1808
 
1814
1809
  t.teardown(fastify.close.bind(fastify))
1815
1810
 
1816
- fastify.listen(0, (err) => {
1811
+ fastify.listen({ port: 0 }, (err) => {
1817
1812
  t.error(err)
1818
1813
 
1819
1814
  fastify.server.unref()
@@ -1957,7 +1952,7 @@ t.test('register with wildcard string on multiple root paths', (t) => {
1957
1952
 
1958
1953
  t.teardown(fastify.close.bind(fastify))
1959
1954
 
1960
- fastify.listen(0, (err) => {
1955
+ fastify.listen({ port: 0 }, (err) => {
1961
1956
  t.ok(err)
1962
1957
 
1963
1958
  fastify.server.unref()
@@ -1981,7 +1976,7 @@ t.test('register with wildcard false and alternative index', t => {
1981
1976
 
1982
1977
  t.teardown(fastify.close.bind(fastify))
1983
1978
 
1984
- fastify.listen(0, (err) => {
1979
+ fastify.listen({ port: 0 }, (err) => {
1985
1980
  t.error(err)
1986
1981
 
1987
1982
  fastify.server.unref()
@@ -2134,7 +2129,7 @@ t.test('register /static with wildcard false and alternative index', t => {
2134
2129
 
2135
2130
  t.teardown(fastify.close.bind(fastify))
2136
2131
 
2137
- fastify.listen(0, (err) => {
2132
+ fastify.listen({ port: 0 }, (err) => {
2138
2133
  t.error(err)
2139
2134
 
2140
2135
  fastify.server.unref()
@@ -2292,7 +2287,7 @@ t.test('register /static with redirect true', t => {
2292
2287
 
2293
2288
  t.teardown(fastify.close.bind(fastify))
2294
2289
 
2295
- fastify.listen(3001, (err) => {
2290
+ fastify.listen({ port: 0 }, (err) => {
2296
2291
  t.error(err)
2297
2292
 
2298
2293
  fastify.server.unref()
@@ -2417,7 +2412,7 @@ t.test('register /static with redirect true and wildcard false', t => {
2417
2412
 
2418
2413
  t.teardown(fastify.close.bind(fastify))
2419
2414
 
2420
- fastify.listen(3001, err => {
2415
+ fastify.listen({ port: 0 }, err => {
2421
2416
  t.error(err)
2422
2417
 
2423
2418
  fastify.server.unref()
@@ -2553,7 +2548,7 @@ t.test('trailing slash behavior with redirect = false', (t) => {
2553
2548
 
2554
2549
  t.teardown(fastify.close.bind(fastify))
2555
2550
 
2556
- fastify.listen(0, (err) => {
2551
+ fastify.listen({ port: 0 }, (err) => {
2557
2552
  t.error(err)
2558
2553
 
2559
2554
  const host = 'http://localhost:' + fastify.server.address().port
@@ -2636,7 +2631,7 @@ t.test('if dotfiles are properly served according to plugin options', (t) => {
2636
2631
  fastify.register(fastifyStatic, pluginOptions)
2637
2632
 
2638
2633
  t.teardown(fastify.close.bind(fastify))
2639
- fastify.listen(0, (err) => {
2634
+ fastify.listen({ port: 0 }, (err) => {
2640
2635
  t.error(err)
2641
2636
 
2642
2637
  simple.concat({
@@ -2663,7 +2658,7 @@ t.test('if dotfiles are properly served according to plugin options', (t) => {
2663
2658
  fastify.register(fastifyStatic, pluginOptions)
2664
2659
 
2665
2660
  t.teardown(fastify.close.bind(fastify))
2666
- fastify.listen(0, (err) => {
2661
+ fastify.listen({ port: 0 }, (err) => {
2667
2662
  t.error(err)
2668
2663
 
2669
2664
  simple.concat({
@@ -2689,7 +2684,7 @@ t.test('if dotfiles are properly served according to plugin options', (t) => {
2689
2684
  fastify.register(fastifyStatic, pluginOptions)
2690
2685
 
2691
2686
  t.teardown(fastify.close.bind(fastify))
2692
- fastify.listen(0, (err) => {
2687
+ fastify.listen({ port: 0 }, (err) => {
2693
2688
  t.error(err)
2694
2689
 
2695
2690
  simple.concat({
@@ -2722,7 +2717,7 @@ t.test('register with failing glob handler', (t) => {
2722
2717
 
2723
2718
  t.teardown(fastify.close.bind(fastify))
2724
2719
 
2725
- fastify.listen(0, (err) => {
2720
+ fastify.listen({ port: 0 }, (err) => {
2726
2721
  fastify.server.unref()
2727
2722
  t.ok(err)
2728
2723
  t.end()
@@ -2748,7 +2743,7 @@ t.test(
2748
2743
  fastify.register(fastifyStatic, pluginOptions)
2749
2744
 
2750
2745
  t.teardown(fastify.close.bind(fastify))
2751
- fastify.listen(0, (err) => {
2746
+ fastify.listen({ port: 0 }, (err) => {
2752
2747
  fastify.server.unref()
2753
2748
  t.ok(err)
2754
2749
  t.end()
@@ -2774,7 +2769,7 @@ t.test('inject support', async (t) => {
2774
2769
  })
2775
2770
 
2776
2771
  t.test('routes should use custom errorHandler premature stream close', t => {
2777
- t.plan(4)
2772
+ t.plan(5)
2778
2773
 
2779
2774
  const pluginOptions = {
2780
2775
  root: path.join(__dirname, '/static'),
@@ -2809,7 +2804,7 @@ t.test('routes should use custom errorHandler premature stream close', t => {
2809
2804
  })
2810
2805
 
2811
2806
  t.test('routes should fallback to default errorHandler', t => {
2812
- t.plan(4)
2807
+ t.plan(5)
2813
2808
 
2814
2809
  const pluginOptions = {
2815
2810
  root: path.join(__dirname, '/static'),
@@ -2845,44 +2840,6 @@ t.test('routes should fallback to default errorHandler', t => {
2845
2840
  })
2846
2841
  })
2847
2842
 
2848
- t.test('routes use default errorHandler when fastify.errorHandler is not defined', t => {
2849
- t.plan(4)
2850
-
2851
- const pluginOptions = {
2852
- root: path.join(__dirname, '/static'),
2853
- prefix: '/static/'
2854
- }
2855
-
2856
- const fastify = Fastify()
2857
- fastify[kErrorHandler] = undefined // simulate old fastify version
2858
-
2859
- fastify.addHook('onRoute', function (routeOptions) {
2860
- t.notOk(routeOptions.errorHandler instanceof Function)
2861
-
2862
- routeOptions.preHandler = (request, reply, done) => {
2863
- const fakeError = new Error()
2864
- fakeError.code = 'SOMETHING_ELSE'
2865
- done(fakeError)
2866
- }
2867
- })
2868
-
2869
- fastify.register(fastifyStatic, pluginOptions)
2870
- t.teardown(fastify.close.bind(fastify))
2871
-
2872
- fastify.inject({
2873
- method: 'GET',
2874
- url: '/static/index.html'
2875
- }, (err, response) => {
2876
- t.error(err)
2877
- t.same(JSON.parse(response.payload), {
2878
- statusCode: 500,
2879
- code: 'SOMETHING_ELSE',
2880
- error: 'Internal Server Error',
2881
- message: ''
2882
- })
2883
- })
2884
- })
2885
-
2886
2843
  t.test('precent encoded URLs in glob mode', (t) => {
2887
2844
  t.plan(4)
2888
2845
 
@@ -2896,7 +2853,7 @@ t.test('precent encoded URLs in glob mode', (t) => {
2896
2853
 
2897
2854
  t.teardown(fastify.close.bind(fastify))
2898
2855
 
2899
- fastify.listen(0, (err) => {
2856
+ fastify.listen({ port: 0 }, (err) => {
2900
2857
  t.error(err)
2901
2858
  fastify.server.unref()
2902
2859
 
@@ -2927,7 +2884,7 @@ t.test('register /static and /static2 without wildcard', t => {
2927
2884
 
2928
2885
  t.teardown(fastify.close.bind(fastify))
2929
2886
 
2930
- fastify.listen(0, err => {
2887
+ fastify.listen({ port: 0 }, err => {
2931
2888
  t.error(err)
2932
2889
 
2933
2890
  fastify.server.unref()
@@ -3463,7 +3420,7 @@ t.test('should not redirect to protocol-relative locations', (t) => {
3463
3420
  redirect: true
3464
3421
  })
3465
3422
  t.teardown(fastify.close.bind(fastify))
3466
- fastify.listen(0, (err, address) => {
3423
+ fastify.listen({ port: 0 }, (err, address) => {
3467
3424
  t.error(err)
3468
3425
  urls.forEach(([testUrl, expected, status]) => {
3469
3426
  const req = http.request(url.parse(address + testUrl), res => {
@@ -3480,3 +3437,77 @@ t.test('should not redirect to protocol-relative locations', (t) => {
3480
3437
  })
3481
3438
  })
3482
3439
  })
3440
+
3441
+ t.test('should not serve index if option is `false`', (t) => {
3442
+ t.plan(3)
3443
+
3444
+ const pluginOptions = {
3445
+ root: path.join(__dirname, '/static'),
3446
+ prefix: '/static',
3447
+ index: false
3448
+ }
3449
+ const fastify = Fastify()
3450
+ fastify.register(fastifyStatic, pluginOptions)
3451
+
3452
+ t.teardown(fastify.close.bind(fastify))
3453
+
3454
+ fastify.listen({ port: 0 }, (err) => {
3455
+ t.error(err)
3456
+
3457
+ fastify.server.unref()
3458
+
3459
+ t.test('/static/index.html', (t) => {
3460
+ t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT)
3461
+ simple.concat({
3462
+ method: 'GET',
3463
+ url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
3464
+ }, (err, response, body) => {
3465
+ t.error(err)
3466
+ t.equal(response.statusCode, 200)
3467
+ t.equal(body.toString(), indexContent)
3468
+ genericResponseChecks(t, response)
3469
+ })
3470
+ })
3471
+
3472
+ t.test('/static', (t) => {
3473
+ t.plan(2 + GENERIC_ERROR_RESPONSE_CHECK_COUNT)
3474
+ simple.concat({
3475
+ method: 'GET',
3476
+ url: 'http://localhost:' + fastify.server.address().port + '/static'
3477
+ }, (err, response, body) => {
3478
+ t.error(err)
3479
+ t.equal(response.statusCode, 404)
3480
+ genericErrorResponseChecks(t, response)
3481
+ })
3482
+ })
3483
+ })
3484
+ })
3485
+
3486
+ t.test('should follow symbolic link without wildcard', (t) => {
3487
+ t.plan(5)
3488
+ const fastify = Fastify()
3489
+ fastify.register(fastifyStatic, {
3490
+ root: path.join(__dirname, '/static-symbolic-link'),
3491
+ wildcard: false
3492
+ })
3493
+ t.teardown(fastify.close.bind(fastify))
3494
+ fastify.listen({ port: 0 }, (err) => {
3495
+ t.error(err)
3496
+
3497
+ simple.concat({
3498
+ method: 'GET',
3499
+ url: 'http://localhost:' + fastify.server.address().port + '/origin/subdir/subdir/index.html'
3500
+ }, (err, response) => {
3501
+ t.error(err)
3502
+ t.equal(response.statusCode, 200)
3503
+ })
3504
+
3505
+ simple.concat({
3506
+ method: 'GET',
3507
+ url: 'http://localhost:' + fastify.server.address().port + '/dir/symlink/subdir/subdir/index.html'
3508
+ }, (err, response) => {
3509
+ t.error(err)
3510
+ t.equal(response.statusCode, 200)
3511
+ })
3512
+ })
3513
+ })
@@ -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
+ })