Skip to content

Commit

Permalink
feat: add ignoreDuplicateSlashes option (#3929)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko authored May 26, 2022
1 parent be1000e commit 39830f8
Show file tree
Hide file tree
Showing 14 changed files with 475 additions and 98 deletions.
2 changes: 2 additions & 0 deletions build/build-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 33,7 @@ const defaultInitOptions = {
disableRequestLogging: false,
jsonShorthand: true,
ignoreTrailingSlash: false,
ignoreDuplicateSlashes: false,
maxParamLength: 100,
onProtoPoisoning: 'error',
onConstructorPoisoning: 'error',
Expand Down Expand Up @@ -76,6 77,7 @@ const schema = {
then: { setDefaultValue: true }
},
ignoreTrailingSlash: { type: 'boolean', default: defaultInitOptions.ignoreTrailingSlash },
ignoreDuplicateSlashes: { type: 'boolean', default: defaultInitOptions.ignoreDuplicateSlashes },
disableRequestLogging: {
type: 'boolean',
default: false
Expand Down
2 changes: 1 addition & 1 deletion docs/Reference/Routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 114,7 @@ fastify.route(options)
* `slash`: Will register only `/prefix/`.
* `no-slash`: Will register only `/prefix`.

Note: this option does not override `ignoreTrailingSlashes` in [Server](./Server.md) configuration.
Note: this option does not override `ignoreTrailingSlash` in [Server](./Server.md) configuration.

* `request` is defined in [Request](./Request.md).

Expand Down
24 changes: 24 additions & 0 deletions docs/Reference/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 17,7 @@ describes the properties available in that options object.
- [`maxRequestsPerSocket`](#maxrequestspersocket)
- [`requestTimeout`](#requesttimeout)
- [`ignoreTrailingSlash`](#ignoretrailingslash)
- [`ignoreDuplicateSlashes`](#ignoreduplicateslashes)
- [`maxParamLength`](#maxparamlength)
- [`bodyLimit`](#bodylimit)
- [`onProtoPoisoning`](#onprotopoisoning)
Expand Down Expand Up @@ -203,6 204,29 @@ fastify.get('/bar', function (req, reply) {
})
```

### `ignoreDuplicateSlashes`
<a id="factory-ignore-duplicate-slashes"></a>

Fastify uses [find-my-way](https://github.com/delvedor/find-my-way) to handle
routing. You can use `ignoreDuplicateSlashes` option to remove duplicate slashes
from the path. It removes duplicate slashes in the route path and in the request
URL. This option applies to *all* route registrations for the resulting server instance.

Note that when `ignoreTrailingSlash` and `ignoreDuplicateSlashes` are both set to true, Fastify will remove duplicate slashes, and then trailing slashes, meaning //a//b//c// will be converted to /a/b/c.

Default: `false`

```js
const fastify = require('fastify')({
ignoreDuplicateSlashes: true
})

// registers "/foo/bar/"
fastify.get('///foo//bar//', function (req, reply) {
reply.send('foo')
})
```

### `maxParamLength`
<a id="factory-max-param-length"></a>

Expand Down
1 change: 1 addition & 0 deletions fastify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 106,7 @@ export type FastifyServerOptions<
Logger extends FastifyBaseLogger = FastifyLoggerInstance
> = {
ignoreTrailingSlash?: boolean,
ignoreDuplicateSlashes?: boolean,
connectionTimeout?: number,
keepAliveTimeout?: number,
maxRequestsPerSocket?: number,
Expand Down
1 change: 1 addition & 0 deletions fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 166,7 @@ function fastify (options) {
onBadUrl,
constraints,
ignoreTrailingSlash: options.ignoreTrailingSlash || defaultInitOptions.ignoreTrailingSlash,
ignoreDuplicateSlashes: options.ignoreDuplicateSlashes || defaultInitOptions.ignoreDuplicateSlashes,
maxParamLength: options.maxParamLength || defaultInitOptions.maxParamLength,
caseSensitive: options.caseSensitive,
allowUnsafeRegex: options.allowUnsafeRegex || defaultInitOptions.allowUnsafeRegex,
Expand Down
Loading

0 comments on commit 39830f8

Please sign in to comment.