Skip to content

Commit 1d0a2cf

Browse files
committed
Merge branch 'master' into feature/494-add-support-to-logLevel
2 parents 788b5c0 + e5faab5 commit 1d0a2cf

File tree

10 files changed

+2197
-2953
lines changed

10 files changed

+2197
-2953
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ updates:
99
- package-ecosystem: "npm"
1010
directory: "/"
1111
schedule:
12-
interval: "weekly"
12+
interval: "monthly"
1313
open-pull-requests-limit: 10

.taprc

Lines changed: 0 additions & 2 deletions
This file was deleted.

README.md

Lines changed: 65 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,13 @@ fastify.get('/path/without/cache/control', function (req, reply) {
115115

116116
#### `root` (required)
117117

118-
The absolute path of the directory that contains the files to serve.
119-
The file to serve will be determined by combining `req.url` with the
120-
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.
121121

122-
You can also provide an array of directories containing files to serve.
123-
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.
124125

125126
#### `prefix`
126127

@@ -132,7 +133,7 @@ A URL path prefix used to create a virtual mount path for the static directory.
132133

133134
Default: `{}`
134135

135-
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
136137
[route constraints](https://fastify.dev/docs/latest/Reference/Routes/#constraints).
137138

138139
#### `logLevel`
@@ -145,21 +146,21 @@ Set log level for registered routes.
145146

146147
Default: `false`
147148

148-
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.
149150

150151
#### `schemaHide`
151152

152153
Default: `true`
153154

154-
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.
155156

156157
#### `setHeaders`
157158

158159
Default: `undefined`
159160

160161
A function to set custom headers on the response. Alterations to the headers
161162
must be done synchronously. The function is called as `fn(res, path, stat)`,
162-
where the arguments are:
163+
with the arguments:
163164

164165
- `res` The response object.
165166
- `path` The path of the file that is being sent.
@@ -172,7 +173,7 @@ The following options are also supported and will be passed directly to the
172173

173174
- [`acceptRanges`](https://www.npmjs.com/package/@fastify/send#acceptranges)
174175
- [`contentType`](https://www.npmjs.com/package/@fastify/send#contenttype)
175-
- [`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
176177
- [`dotfiles`](https://www.npmjs.com/package/@fastify/send#dotfiles)
177178
- [`etag`](https://www.npmjs.com/package/@fastify/send#etag)
178179
- [`extensions`](https://www.npmjs.com/package/@fastify/send#extensions)
@@ -181,69 +182,64 @@ The following options are also supported and will be passed directly to the
181182
- [`lastModified`](https://www.npmjs.com/package/@fastify/send#lastmodified)
182183
- [`maxAge`](https://www.npmjs.com/package/@fastify/send#maxage)
183184

184-
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.
185186

186187
#### `redirect`
187188

188189
Default: `false`
189190

190191
If set to `true`, `@fastify/static` redirects to the directory with a trailing slash.
191192

192-
This option cannot be set to `true` with `wildcard` set to `false` on a server
193-
with `ignoreTrailingSlash` set to `true`.
193+
This option cannot be `true` if `wildcard` is `false` and `ignoreTrailingSlash` is `true`.
194194

195-
If this option is set to `false`, then requesting directories without trailing
196-
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()`.
197196

198197
#### `wildcard`
199198

200199
Default: `true`
201200

202-
If set to `true`, `@fastify/static` adds a wildcard route to serve files.
203-
If set to `false`, `@fastify/static` globs the filesystem for all defined
204-
files in the served folder (`${root}/**/**`), and just creates the routes needed for
205-
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.
206205

207-
The default options of https://www.npmjs.com/package/glob are applied
208-
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.
209208

210-
This option cannot be set to `false` with `redirect` set to `true` on a server
211-
with `ignoreTrailingSlash` set to `true`.
209+
This option cannot be `false` if `redirect` is `true` and `ignoreTrailingSlash` is `true`.
212210

213211
#### `allowedPath`
214212

215213
Default: `(pathName, root, request) => true`
216214

217-
This function allows filtering the served files. Also, with the help of the request object a more complex path authentication is possible.
218-
If the function returns `true`, the file will be served.
219-
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.
220217

221218
#### `index`
222219

223220
Default: `undefined`
224221

225-
Under the hood we use [send](https://github.com/pillarjs/send#index) lib that by default supports "index.html" files.
226-
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.
227224

228225
#### `serveDotFiles`
229226

230227
Default: `false`
231228

232-
When `true`, files in hidden directories (e.g. `.foo`) will be served.
229+
If `true`, serves files in hidden directories (e.g., `.foo`).
233230

234231
#### `list`
235232

236233
Default: `undefined`
237234

238-
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.
239237

240-
Default response is json.
238+
Multi-root is not supported within the `list` option.
241239

242-
Note:
243-
- Multi-root is not supported within the `list` option.
244-
- If `dotfiles` option value is `deny` or `ignore`, dotfiles will be excluded.
240+
If `dotfiles` is `deny` or `ignore`, dotfiles are excluded.
245241

246-
**Example:**
242+
Example:
247243

248244
```js
249245
fastify.register(require('@fastify/static'), {
@@ -257,7 +253,7 @@ fastify.register(require('@fastify/static'), {
257253
Request
258254

259255
```bash
260-
GET .../public
256+
GET /public
261257
```
262258

263259
Response
@@ -272,17 +268,17 @@ Default: `json`
272268

273269
Options: `html`, `json`
274270

275-
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.
276272

277-
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`.
278274

279275
```bash
280-
GET .../public/assets?format=json
276+
GET /public/assets?format=json
281277
```
282278

283-
will return the response as json independent of `list.format`.
279+
Returns the response as JSON, regardless of `list.format`.
284280

285-
**Example:**
281+
Example:
286282

287283
```js
288284
fastify.register(require('@fastify/static'), {
@@ -309,7 +305,7 @@ fastify.register(require('@fastify/static'), {
309305
Request
310306

311307
```bash
312-
GET .../public
308+
GET /public
313309
```
314310

315311
Response
@@ -333,11 +329,11 @@ Response
333329

334330
Default: `['']`
335331

336-
Directory list can respond to different routes, declared in `list.names` options.
332+
Directory list can respond to different routes declared in `list.names`.
337333

338-
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.
339335
340-
**Example:**
336+
Example:
341337

342338
```js
343339
fastify.register(require('@fastify/static'), {
@@ -351,20 +347,20 @@ fastify.register(require('@fastify/static'), {
351347
})
352348
```
353349

354-
Dir list respond with the same content to
350+
Dir list respond with the same content to:
355351

356352
```bash
357-
GET .../public
358-
GET .../public/
359-
GET .../public/index
360-
GET .../public/index.json
353+
GET /public
354+
GET /public/
355+
GET /public/index
356+
GET /public/index.json
361357
```
362358

363359
#### `list.extendedFolderInfo`
364360

365361
Default: `undefined`
366362

367-
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.
368364

369365
```js
370366
render(dirs, files) {
@@ -378,15 +374,15 @@ render(dirs, files) {
378374
}
379375
```
380376

381-
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.
382378
383379
#### `list.jsonFormat`
384380

385381
Default: `names`
386382

387383
Options: `names`, `extended`
388384

389-
This option determines the output format when `json` is selected.
385+
Determines the output format when `json` is selected.
390386

391387
`names`:
392388
```json
@@ -410,13 +406,11 @@ This option determines the output format when `json` is selected.
410406
"name": "dir1",
411407
"stats": {
412408
"dev": 2100,
413-
"size": 4096,
414-
...
409+
"size": 4096
415410
},
416411
"extendedInfo": {
417412
"fileCount": 4,
418-
"totalSize": 51233,
419-
...
413+
"totalSize": 51233
420414
}
421415
}
422416
],
@@ -425,8 +419,7 @@ This option determines the output format when `json` is selected.
425419
"name": "file1.txt",
426420
"stats": {
427421
"dev": 2200,
428-
"size": 554,
429-
...
422+
"size": 554
430423
}
431424
}
432425
]
@@ -437,9 +430,9 @@ This option determines the output format when `json` is selected.
437430

438431
Default: `false`
439432

440-
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.
441434

442-
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:
443436

444437
```
445438
./public
@@ -453,22 +446,21 @@ Assume this structure with the compressed asset as a sibling of the un-compresse
453446

454447
#### Disable serving
455448

456-
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`.
457451

458452
#### Disabling reply decorator
459453

460-
The reply object is decorated with a `sendFile` function by default. If you want to
461-
disable this, pass the option `{ decorateReply: false }`. If @fastify/static is
462-
registered to multiple prefixes in the same route only one can initialize reply
463-
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.
464457

465458
#### Handling 404s
466459

467-
If a request matches the URL `prefix` but a file cannot be found for the
468-
request, Fastify's 404 handler will be called. You can set a custom 404
469-
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).
470462

471-
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:
472464

473465
```js
474466
const app = require('fastify')();
@@ -489,13 +481,12 @@ This code will send the `index.html` for the paths `docs`, `docs/`, and `docs/in
489481

490482
### Handling Errors
491483

492-
If an error occurs while trying to send a file, the error will be passed
493-
to Fastify's error handler. You can set a custom error handler with
494-
[`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).
495486

496487
### Payload `stream.path`
497488

498-
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`.
499490

500491
```js
501492
fastify.addHook('onSend', function (req, reply, payload, next) {

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ async function fastifyStatic (fastify, opts) {
7878
if (opts.decorateReply !== false) {
7979
fastify.decorateReply('sendFile', function (filePath, rootPath, options) {
8080
const opts = typeof rootPath === 'object' ? rootPath : options
81-
const root = typeof rootPath === 'string' ? rootPath : opts && opts.root
81+
const root = typeof rootPath === 'string' ? rootPath : opts?.root
8282
pumpSendToReply(
8383
this.request,
8484
this,
@@ -109,7 +109,7 @@ async function fastifyStatic (fastify, opts) {
109109

110110
if (opts.serve !== false) {
111111
if (opts.wildcard && typeof opts.wildcard !== 'boolean') {
112-
throw new Error('"wildcard" option must be a boolean')
112+
throw new TypeError('"wildcard" option must be a boolean')
113113
}
114114
if (opts.wildcard === undefined || opts.wildcard === true) {
115115
fastify.route({
@@ -444,7 +444,7 @@ function checkRootPathForErrors (fastify, rootPath) {
444444

445445
function checkPath (fastify, rootPath) {
446446
if (typeof rootPath !== 'string') {
447-
throw new Error('"root" option must be a string')
447+
throw new TypeError('"root" option must be a string')
448448
}
449449
if (path.isAbsolute(rootPath) === false) {
450450
throw new Error('"root" option must be an absolute path')

lib/dirList.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const dirList = {
108108
*/
109109
send: async function ({ reply, dir, options, route, prefix, dotfiles }) {
110110
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`')
112112
}
113113

114114
let entries

0 commit comments

Comments
 (0)