@fastify/static 8.0.3 → 8.1.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.
@@ -9,5 +9,5 @@ updates:
9
9
  - package-ecosystem: "npm"
10
10
  directory: "/"
11
11
  schedule:
12
- interval: "weekly"
12
+ interval: "monthly"
13
13
  open-pull-requests-limit: 10
package/README.md CHANGED
@@ -1,21 +1,30 @@
1
1
  # @fastify/static
2
2
 
3
- ![CI](https://github.com/fastify/fastify-static/workflows/CI/badge.svg)
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)
4
4
  [![NPM version](https://img.shields.io/npm/v/@fastify/static.svg?style=flat)](https://www.npmjs.com/package/@fastify/static)
5
- [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
5
+ [![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)
6
6
 
7
- Plugin for serving static files as fast as possible. Supports Fastify version `4.x`.
8
-
9
- | Fastify version | branch |
10
- | --------------- | -------------------------------------------------------------------- |
11
- | `^4.x` | This branch |
12
- | `^3.x` | [`v5.x`](https://github.com/fastify/fastify-static/tree/v5.x) |
13
- | `^2.x` | [`2.x`](https://github.com/fastify/fastify-static/tree/2.x) |
14
- | `^1.11.x` | [`1.x`](https://github.com/fastify/fastify-static/tree/1.x) |
7
+ Plugin for serving static files as fast as possible.
15
8
 
16
9
  ## Install
10
+ ```
11
+ npm i @fastify/static
12
+ ```
13
+
14
+ ### Compatibility
15
+
16
+ | Plugin version | Fastify version |
17
+ | ---------------|-----------------|
18
+ | `^8.x` | `^5.x` |
19
+ | `^7.x` | `^4.x` |
20
+ | `^5.x` | `^3.x` |
21
+ | `^2.x` | `^2.x` |
22
+ | `^1.x` | `^1.x` |
17
23
 
18
- `npm i @fastify/static`
24
+
25
+ Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin
26
+ in the table above.
27
+ See [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) for more details.
19
28
 
20
29
  ## Usage
21
30
 
@@ -106,12 +115,13 @@ fastify.get('/path/without/cache/control', function (req, reply) {
106
115
 
107
116
  #### `root` (required)
108
117
 
109
- The absolute path of the directory that contains the files to serve.
110
- The file to serve will be determined by combining `req.url` with the
111
- provided root directory.
118
+ The absolute path of the directory containing the files to serve.
119
+ The file to serve is determined by combining `req.url` with the
120
+ root directory.
112
121
 
113
- You can also provide an array of directories containing files to serve.
114
- This is useful for serving multiple static directories under a single prefix. Files are served in a "first found, first served" manner, so the order in which you list the directories is important. For best performance, you should always list your main asset directory first. Duplicate paths will raise an error.
122
+ An array of directories can be provided to serve multiple static directories
123
+ under a single prefix. Files are served in a "first found, first served" manner,
124
+ so list directories in order of priority. Duplicate paths will raise an error.
115
125
 
116
126
  #### `prefix`
117
127
 
@@ -123,20 +133,26 @@ A URL path prefix used to create a virtual mount path for the static directory.
123
133
 
124
134
  Default: `{}`
125
135
 
126
- Constraints that will be added to registered routes. See Fastify's documentation for
136
+ Constraints to add to registered routes. See Fastify's documentation for
127
137
  [route constraints](https://fastify.dev/docs/latest/Reference/Routes/#constraints).
128
138
 
139
+ #### `logLevel`
140
+
141
+ Default: `info`
142
+
143
+ Set log level for registered routes.
144
+
129
145
  #### `prefixAvoidTrailingSlash`
130
146
 
131
147
  Default: `false`
132
148
 
133
- If set to false prefix will get trailing "/" at the end. If set to true, prefix will not append "/" to prefix.
149
+ If `false`, the prefix gets a trailing "/". If `true`, no trailing "/" is added to the prefix.
134
150
 
135
151
  #### `schemaHide`
136
152
 
137
153
  Default: `true`
138
154
 
139
- A flag that define if the fastify route hide-schema attribute is hidden or not
155
+ A flag that defines if the fastify route hide-schema attribute is hidden or not.
140
156
 
141
157
  #### `setHeaders`
142
158
 
@@ -144,7 +160,7 @@ Default: `undefined`
144
160
 
145
161
  A function to set custom headers on the response. Alterations to the headers
146
162
  must be done synchronously. The function is called as `fn(res, path, stat)`,
147
- where the arguments are:
163
+ with the arguments:
148
164
 
149
165
  - `res` The response object.
150
166
  - `path` The path of the file that is being sent.
@@ -157,7 +173,7 @@ The following options are also supported and will be passed directly to the
157
173
 
158
174
  - [`acceptRanges`](https://www.npmjs.com/package/@fastify/send#acceptranges)
159
175
  - [`contentType`](https://www.npmjs.com/package/@fastify/send#contenttype)
160
- - [`cacheControl`](https://www.npmjs.com/package/@fastify/send#cachecontrol) (Enable or disable setting Cache-Control response header, defaults to true. **Important:** If you want to provide a custom Cache-Control response header, this option must be false.)
176
+ - [`cacheControl`](https://www.npmjs.com/package/@fastify/send#cachecontrol) - Enable or disable setting Cache-Control response header (defaults to `true`). To provide a custom Cache-Control header, set this option to false
161
177
  - [`dotfiles`](https://www.npmjs.com/package/@fastify/send#dotfiles)
162
178
  - [`etag`](https://www.npmjs.com/package/@fastify/send#etag)
163
179
  - [`extensions`](https://www.npmjs.com/package/@fastify/send#extensions)
@@ -166,7 +182,7 @@ The following options are also supported and will be passed directly to the
166
182
  - [`lastModified`](https://www.npmjs.com/package/@fastify/send#lastmodified)
167
183
  - [`maxAge`](https://www.npmjs.com/package/@fastify/send#maxage)
168
184
 
169
- You're able to alter this options when calling `reply.download('filename.html', options)` or `reply.download('filename.html', 'otherfilename.html', options)` on each response to a request.
185
+ These options can be altered when calling `reply.sendFile('filename.html', options)` or `reply.sendFile('filename.html', 'otherfilename.html', options)` on each response.
170
186
 
171
187
  #### `redirect`
172
188
 
@@ -174,61 +190,56 @@ Default: `false`
174
190
 
175
191
  If set to `true`, `@fastify/static` redirects to the directory with a trailing slash.
176
192
 
177
- This option cannot be set to `true` with `wildcard` set to `false` on a server
178
- with `ignoreTrailingSlash` set to `true`.
193
+ This option cannot be `true` if `wildcard` is `false` and `ignoreTrailingSlash` is `true`.
179
194
 
180
- If this option is set to `false`, then requesting directories without trailing
181
- slash will trigger your app's 404 handler using `reply.callNotFound()`.
195
+ If `false`, requesting directories without a trailing slash triggers the app's 404 handler using `reply.callNotFound()`.
182
196
 
183
197
  #### `wildcard`
184
198
 
185
199
  Default: `true`
186
200
 
187
- If set to `true`, `@fastify/static` adds a wildcard route to serve files.
188
- If set to `false`, `@fastify/static` globs the filesystem for all defined
189
- files in the served folder (`${root}/**/**`), and just creates the routes needed for
190
- those and it will not serve the newly added file on the filesystem.
201
+ If `true`, `@fastify/static` adds a wildcard route to serve files.
202
+ If `false`, it globs the filesystem for all defined files in the
203
+ served folder (`${root}/**/**`) and creates the necessary routes,
204
+ but will not serve newly added files.
191
205
 
192
- The default options of https://www.npmjs.com/package/glob are applied
193
- for getting the file list.
206
+ The default options of [`glob`](https://www.npmjs.com/package/glob)
207
+ are applied for getting the file list.
194
208
 
195
- This option cannot be set to `false` with `redirect` set to `true` on a server
196
- with `ignoreTrailingSlash` set to `true`.
209
+ This option cannot be `false` if `redirect` is `true` and `ignoreTrailingSlash` is `true`.
197
210
 
198
211
  #### `allowedPath`
199
212
 
200
213
  Default: `(pathName, root, request) => true`
201
214
 
202
- This function allows filtering the served files. Also, with the help of the request object a more complex path authentication is possible.
203
- If the function returns `true`, the file will be served.
204
- If the function returns `false`, Fastify's 404 handler will be called.
215
+ This function filters served files. Using the request object, complex path authentication is possible.
216
+ Returning `true` serves the file; returning `false` calls Fastify's 404 handler.
205
217
 
206
218
  #### `index`
207
219
 
208
220
  Default: `undefined`
209
221
 
210
- Under the hood we use [send](https://github.com/pillarjs/send#index) lib that by default supports "index.html" files.
211
- To disable this set false or to supply a new index pass a string or an array in preferred order.
222
+ Under the hood, [`@fastify/send`](https://www.npmjs.com/package/@fastify/send) supports "index.html" files by default.
223
+ To disable this, set `false`, or supply a new index by passing a string or an array in preferred order.
212
224
 
213
225
  #### `serveDotFiles`
214
226
 
215
227
  Default: `false`
216
228
 
217
- When `true`, files in hidden directories (e.g. `.foo`) will be served.
229
+ If `true`, serves files in hidden directories (e.g., `.foo`).
218
230
 
219
231
  #### `list`
220
232
 
221
233
  Default: `undefined`
222
234
 
223
- If set, it provides the directory list calling the directory path.
235
+ If set, provides the directory list by calling the directory path.
236
+ Default response is JSON.
224
237
 
225
- Default response is json.
238
+ Multi-root is not supported within the `list` option.
226
239
 
227
- Note:
228
- - Multi-root is not supported within the `list` option.
229
- - If `dotfiles` option value is `deny` or `ignore`, dotfiles will be excluded.
240
+ If `dotfiles` is `deny` or `ignore`, dotfiles are excluded.
230
241
 
231
- **Example:**
242
+ Example:
232
243
 
233
244
  ```js
234
245
  fastify.register(require('@fastify/static'), {
@@ -242,7 +253,7 @@ fastify.register(require('@fastify/static'), {
242
253
  Request
243
254
 
244
255
  ```bash
245
- GET .../public
256
+ GET /public
246
257
  ```
247
258
 
248
259
  Response
@@ -257,17 +268,17 @@ Default: `json`
257
268
 
258
269
  Options: `html`, `json`
259
270
 
260
- Directory list can be also in `html` format; in that case, `list.render` function is required.
271
+ Directory list can be in `html` format; in that case, `list.render` function is required.
261
272
 
262
- You can override the option with URL parameter `format`. Options are `html` and `json`.
273
+ This option can be overridden by the URL parameter `format`. Options are `html` and `json`.
263
274
 
264
275
  ```bash
265
- GET .../public/assets?format=json
276
+ GET /public/assets?format=json
266
277
  ```
267
278
 
268
- will return the response as json independent of `list.format`.
279
+ Returns the response as JSON, regardless of `list.format`.
269
280
 
270
- **Example:**
281
+ Example:
271
282
 
272
283
  ```js
273
284
  fastify.register(require('@fastify/static'), {
@@ -294,7 +305,7 @@ fastify.register(require('@fastify/static'), {
294
305
  Request
295
306
 
296
307
  ```bash
297
- GET .../public
308
+ GET /public
298
309
  ```
299
310
 
300
311
  Response
@@ -318,11 +329,11 @@ Response
318
329
 
319
330
  Default: `['']`
320
331
 
321
- Directory list can respond to different routes, declared in `list.names` options.
332
+ Directory list can respond to different routes declared in `list.names`.
322
333
 
323
- Note: if a file with the same name exists, the actual file is sent.
334
+ > 🛈 Note: If a file with the same name exists, the actual file is sent.
324
335
 
325
- **Example:**
336
+ Example:
326
337
 
327
338
  ```js
328
339
  fastify.register(require('@fastify/static'), {
@@ -336,20 +347,20 @@ fastify.register(require('@fastify/static'), {
336
347
  })
337
348
  ```
338
349
 
339
- Dir list respond with the same content to
350
+ Dir list respond with the same content to:
340
351
 
341
352
  ```bash
342
- GET .../public
343
- GET .../public/
344
- GET .../public/index
345
- GET .../public/index.json
353
+ GET /public
354
+ GET /public/
355
+ GET /public/index
356
+ GET /public/index.json
346
357
  ```
347
358
 
348
359
  #### `list.extendedFolderInfo`
349
360
 
350
361
  Default: `undefined`
351
362
 
352
- If `true` some extended information for folders will be accessible in `list.render` and in the json response.
363
+ If `true`, extended information for folders will be accessible in `list.render` and the JSON response.
353
364
 
354
365
  ```js
355
366
  render(dirs, files) {
@@ -363,7 +374,7 @@ render(dirs, files) {
363
374
  }
364
375
  ```
365
376
 
366
- Warning: This will slightly decrease the performance, especially for deeply nested file structures.
377
+ > ⚠ Warning: This will slightly decrease the performance, especially for deeply nested file structures.
367
378
 
368
379
  #### `list.jsonFormat`
369
380
 
@@ -371,7 +382,7 @@ Default: `names`
371
382
 
372
383
  Options: `names`, `extended`
373
384
 
374
- This option determines the output format when `json` is selected.
385
+ Determines the output format when `json` is selected.
375
386
 
376
387
  `names`:
377
388
  ```json
@@ -395,13 +406,11 @@ This option determines the output format when `json` is selected.
395
406
  "name": "dir1",
396
407
  "stats": {
397
408
  "dev": 2100,
398
- "size": 4096,
399
- ...
409
+ "size": 4096
400
410
  },
401
411
  "extendedInfo": {
402
412
  "fileCount": 4,
403
- "totalSize": 51233,
404
- ...
413
+ "totalSize": 51233
405
414
  }
406
415
  }
407
416
  ],
@@ -410,8 +419,7 @@ This option determines the output format when `json` is selected.
410
419
  "name": "file1.txt",
411
420
  "stats": {
412
421
  "dev": 2200,
413
- "size": 554,
414
- ...
422
+ "size": 554
415
423
  }
416
424
  }
417
425
  ]
@@ -422,9 +430,9 @@ This option determines the output format when `json` is selected.
422
430
 
423
431
  Default: `false`
424
432
 
425
- Try to send the brotli encoded asset first (when supported within the `Accept-Encoding` headers), retry for gzip, then the fall back to the original `pathname`. You may choose to skip compression for smaller files that don't benefit from it.
433
+ First, try to send the brotli encoded asset (if supported by `Accept-Encoding` headers), then gzip, and finally the original `pathname`. Skip compression for smaller files that do not benefit from it.
426
434
 
427
- Assume this structure with the compressed asset as a sibling of the un-compressed counterpart:
435
+ Assume this structure with the compressed asset as a sibling of the uncompressed counterpart:
428
436
 
429
437
  ```
430
438
  ./public
@@ -438,22 +446,21 @@ Assume this structure with the compressed asset as a sibling of the un-compresse
438
446
 
439
447
  #### Disable serving
440
448
 
441
- If you would just like to use the reply decorator and not serve whole directories automatically, you can simply pass the option `{ serve: false }`. This will prevent the plugin from serving everything under `root`.
449
+ To use only the reply decorator without serving directories, pass `{ serve: false }`.
450
+ This prevents the plugin from serving everything under `root`.
442
451
 
443
452
  #### Disabling reply decorator
444
453
 
445
- The reply object is decorated with a `sendFile` function by default. If you want to
446
- disable this, pass the option `{ decorateReply: false }`. If @fastify/static is
447
- registered to multiple prefixes in the same route only one can initialize reply
448
- decorators.
454
+ The reply object is decorated with a `sendFile` function by default. To disable this,
455
+ pass `{ decorateReply: false }`. If `@fastify/static` is registered to multiple prefixes
456
+ in the same route, only one can initialize reply decorators.
449
457
 
450
458
  #### Handling 404s
451
459
 
452
- If a request matches the URL `prefix` but a file cannot be found for the
453
- request, Fastify's 404 handler will be called. You can set a custom 404
454
- handler with [`fastify.setNotFoundHandler()`](https://fastify.dev/docs/latest/Reference/Server/#setnotfoundhandler).
460
+ If a request matches the URL `prefix` but no file is found, Fastify's 404
461
+ handler is called. Set a custom 404 handler with [`fastify.setNotFoundHandler()`](https://fastify.dev/docs/latest/Reference/Server/#setnotfoundhandler).
455
462
 
456
- When registering `@fastify/static` within an encapsulated context, the `wildcard` option may need to be set to `false` in order to support index resolution and nested not-found-handler:
463
+ When registering `@fastify/static` within an encapsulated context, the `wildcard` option may need to be set to `false` to support index resolution and nested not-found-handler:
457
464
 
458
465
  ```js
459
466
  const app = require('fastify')();
@@ -474,13 +481,12 @@ This code will send the `index.html` for the paths `docs`, `docs/`, and `docs/in
474
481
 
475
482
  ### Handling Errors
476
483
 
477
- If an error occurs while trying to send a file, the error will be passed
478
- to Fastify's error handler. You can set a custom error handler with
479
- [`fastify.setErrorHandler()`](https://fastify.dev/docs/latest/Reference/Server/#seterrorhandler).
484
+ If an error occurs while sending a file, it is passed to Fastify's error handler.
485
+ Set a custom handler with [`fastify.setErrorHandler()`](https://fastify.dev/docs/latest/Reference/Server/#seterrorhandler).
480
486
 
481
487
  ### Payload `stream.path`
482
488
 
483
- If you need to access the file path inside the `onSend` hook, you can use `payload.path`.
489
+ Access the file path inside the `onSend` hook using `payload.path`.
484
490
 
485
491
  ```js
486
492
  fastify.addHook('onSend', function (req, reply, payload, next) {
@@ -491,4 +497,4 @@ fastify.addHook('onSend', function (req, reply, payload, next) {
491
497
 
492
498
  ## License
493
499
 
494
- Licensed under [MIT](./LICENSE)
500
+ Licensed under [MIT](./LICENSE).
package/eslint.config.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  module.exports = require('neostandard')({
4
+ ignores: require('neostandard').resolveIgnoresFromGitignore(),
4
5
  noJsx: true,
5
6
  ts: true,
6
7
  })
package/index.js CHANGED
@@ -64,6 +64,7 @@ async function fastifyStatic (fastify, opts) {
64
64
  schema: {
65
65
  hide: opts.schemaHide !== undefined ? opts.schemaHide : true
66
66
  },
67
+ logLevel: opts.logLevel,
67
68
  errorHandler (error, request, reply) {
68
69
  if (error?.code === 'ERR_STREAM_PREMATURE_CLOSE') {
69
70
  reply.request.raw.destroy()
@@ -77,7 +78,7 @@ async function fastifyStatic (fastify, opts) {
77
78
  if (opts.decorateReply !== false) {
78
79
  fastify.decorateReply('sendFile', function (filePath, rootPath, options) {
79
80
  const opts = typeof rootPath === 'object' ? rootPath : options
80
- const root = typeof rootPath === 'string' ? rootPath : opts && opts.root
81
+ const root = typeof rootPath === 'string' ? rootPath : opts?.root
81
82
  pumpSendToReply(
82
83
  this.request,
83
84
  this,
@@ -108,7 +109,7 @@ async function fastifyStatic (fastify, opts) {
108
109
 
109
110
  if (opts.serve !== false) {
110
111
  if (opts.wildcard && typeof opts.wildcard !== 'boolean') {
111
- throw new Error('"wildcard" option must be a boolean')
112
+ throw new TypeError('"wildcard" option must be a boolean')
112
113
  }
113
114
  if (opts.wildcard === undefined || opts.wildcard === true) {
114
115
  fastify.route({
@@ -443,7 +444,7 @@ function checkRootPathForErrors (fastify, rootPath) {
443
444
 
444
445
  function checkPath (fastify, rootPath) {
445
446
  if (typeof rootPath !== 'string') {
446
- throw new Error('"root" option must be a string')
447
+ throw new TypeError('"root" option must be a string')
447
448
  }
448
449
  if (path.isAbsolute(rootPath) === false) {
449
450
  throw new Error('"root" option must be an absolute path')
package/lib/dirList.js CHANGED
@@ -108,7 +108,7 @@ const dirList = {
108
108
  */
109
109
  send: async function ({ reply, dir, options, route, prefix, dotfiles }) {
110
110
  if (reply.request.query.format === 'html' && typeof options.render !== 'function') {
111
- throw new Error('The `list.render` option must be a function and is required with the URL parameter `format=html`')
111
+ throw new TypeError('The `list.render` option must be a function and is required with the URL parameter `format=html`')
112
112
  }
113
113
 
114
114
  let entries
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@fastify/static",
3
- "version": "8.0.3",
3
+ "version": "8.1.0",
4
4
  "description": "Plugin for serving static files as fast as possible.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
7
7
  "types": "types/index.d.ts",
8
8
  "scripts": {
9
- "coverage": "npm run test:unit -- --coverage-report=html",
9
+ "coverage": "c8 --reporter html borp --coverage --check-coverage --lines 100",
10
10
  "lint": "eslint",
11
11
  "lint:fix": "eslint --fix",
12
12
  "test": "npm run test:unit && npm run test:typescript",
13
13
  "test:typescript": "tsd",
14
- "test:unit": "tap",
14
+ "test:unit": "borp -C --check-coverage --lines 100",
15
15
  "example": "node example/server.js"
16
16
  },
17
17
  "repository": {
@@ -23,11 +23,40 @@
23
23
  "static"
24
24
  ],
25
25
  "author": "Tommaso Allevi - @allevo",
26
+ "contributors": [
27
+ {
28
+ "name": "Matteo Collina",
29
+ "email": "hello@matteocollina.com"
30
+ },
31
+ {
32
+ "name": "Manuel Spigolon",
33
+ "email": "behemoth89@gmail.com"
34
+ },
35
+ {
36
+ "name": "Aras Abbasi",
37
+ "email": "aras.abbasi@gmail.com"
38
+ },
39
+ {
40
+ "name": "Frazer Smith",
41
+ "email": "frazer.dev@icloud.com",
42
+ "url": "https://github.com/fdawgs"
43
+ }
44
+ ],
26
45
  "license": "MIT",
27
46
  "bugs": {
28
47
  "url": "https://github.com/fastify/fastify-static/issues"
29
48
  },
30
49
  "homepage": "https://github.com/fastify/fastify-static",
50
+ "funding": [
51
+ {
52
+ "type": "github",
53
+ "url": "https://github.com/sponsors/fastify"
54
+ },
55
+ {
56
+ "type": "opencollective",
57
+ "url": "https://opencollective.com/fastify"
58
+ }
59
+ ],
31
60
  "dependencies": {
32
61
  "@fastify/accept-negotiator": "^2.0.0",
33
62
  "@fastify/send": "^3.2.0",
@@ -40,14 +69,14 @@
40
69
  "@fastify/compress": "^8.0.0",
41
70
  "@fastify/pre-commit": "^2.1.0",
42
71
  "@types/node": "^22.0.0",
72
+ "borp": "^0.19.0",
73
+ "c8": "^10.1.3",
43
74
  "concat-stream": "^2.0.0",
44
- "eslint": "^9.9.0",
75
+ "eslint": "^9.17.0",
45
76
  "fastify": "^5.1.0",
46
- "neostandard": "^0.11.3",
77
+ "neostandard": "^0.12.0",
47
78
  "pino": "^9.1.0",
48
79
  "proxyquire": "^2.1.3",
49
- "simple-get": "^4.0.1",
50
- "tap": "^18.7.1",
51
80
  "tsd": "^0.31.0"
52
81
  },
53
82
  "tsd": {