🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@fastify/middie

Package Overview
Dependencies
Maintainers
19
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/middie - npm Package Compare versions

Comparing version
9.0.2
to
9.0.3
+6
eslint.config.js
'use strict'
module.exports = require('neostandard')({
ignores: require('neostandard').resolveIgnoresFromGitignore(),
ts: true
})
+1
-1

@@ -20,5 +20,5 @@ name: CI

test:
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5.0.0
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5
with:
license-check: true
lint: true

@@ -83,3 +83,3 @@ 'use strict'

function onMiddieEnd (err, req, res, next) {
function onMiddieEnd (err, _req, _res, next) {
next(err)

@@ -86,0 +86,0 @@ }

@@ -120,4 +120,3 @@ 'use strict'

function sanitizeUrl (url) {
/* eslint-disable-next-line no-var */
for (var i = 0, len = url.length; i < len; i++) {
for (let i = 0, len = url.length; i < len; i++) {
const charCode = url.charCodeAt(i)

@@ -124,0 +123,0 @@ if (charCode === 63 || charCode === 35) {

{
"name": "@fastify/middie",
"version": "9.0.2",
"version": "9.0.3",
"description": "Middleware engine for Fastify",

@@ -10,5 +10,5 @@ "main": "index.js",

"coverage": "tap --cov --coverage-report=html test.js",
"lint": "standard",
"lint:fix": "standard --fix",
"test": "standard && tap test/*.test.js && tsd",
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "tap test/*.test.js && tsd",
"test:unit": "tap",

@@ -28,2 +28,15 @@ "test:typescript": "tsd"

"url": "http://delved.org"
},
{
"name": "Manuel Spigolon",
"email": "behemoth89@gmail.com"
},
{
"name": "Aras Abbasi",
"email": "aras.abbasi@gmail.com"
},
{
"name": "Frazer Smith",
"email": "frazer.dev@icloud.com",
"url": "https://github.com/fdawgs"
}

@@ -40,2 +53,12 @@ ],

"homepage": "https://github.com/fastify/middie#readme",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/fastify"
},
{
"type": "opencollective",
"url": "https://opencollective.com/fastify"
}
],
"devDependencies": {

@@ -46,7 +69,8 @@ "@fastify/pre-commit": "^2.1.0",

"cors": "^2.8.5",
"eslint": "^9.17.0",
"fastify": "^5.0.0",
"helmet": "^7.1.0",
"helmet": "^8.0.0",
"neostandard": "^0.12.0",
"serve-static": "^1.15.0",
"simple-get": "^4.0.1",
"standard": "^17.1.0",
"tap": "^18.7.2",

@@ -53,0 +77,0 @@ "tsd": "^0.31.0"

# @fastify/middie
![CI](https://github.com/fastify/middie/workflows/CI/badge.svg)
[![CI](https://github.com/fastify/middie/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/middie/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/@fastify/middie.svg?style=flat)](https://www.npmjs.com/package/@fastify/middie)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

@@ -109,3 +109,3 @@ *@fastify/middie* is the plugin that adds middleware support on steroids to [Fastify](https://www.npmjs.com/package/fastify).

*Note you can access `req.body` from the `preParsing`, `onError`, `preSerialization` and `onSend` lifecycle steps. Take a look at the [Lifecycle](https://www.fastify.dev/docs/latest/Reference/Lifecycle/) documentation page to see the order of the steps.*
*Note you can access `req.body` from the `preParsing`, `onError`, `preSerialization`, and `onSend` lifecycle steps. Take a look at the [Lifecycle](https://www.fastify.dev/docs/latest/Reference/Lifecycle/) documentation page to see the order of the steps.*

@@ -141,3 +141,3 @@ ```js

If you need to run a middleware only under certain path(s), just pass the path as first parameter to use and you are done!
If you need to run a middleware only under certain path(s), just pass the path as the first parameter to use and you are done!

@@ -167,3 +167,3 @@ ```js

Middie use [`path-to-regexp`](http://npm.im/path-to-regexp) to convert paths to regular expressions.
Middie uses [`path-to-regexp`](http://npm.im/path-to-regexp) to convert paths to regular expressions.
This might cause potential [ReDoS](https://en.wikipedia.org/wiki/ReDoS) attacks in your applications if

@@ -205,3 +205,3 @@ certain patterns are used. Use it with care.

#### Keep the context
If you need it you can also keep the context of the calling function by calling `run` with `run(req, res, this)`, in this way you can avoid closures allocation.
If you need it you can also keep the context of the calling function by calling `run` with `run(req, res, this)`, avoiding closures allocation.

@@ -227,3 +227,3 @@ ```js

#### Restrict middleware execution to a certain path(s)
If you need to run a middleware only under certains path(s), just pass the path as first parameter to `use` and you are done!
If you need to run a middleware only under certain path(s), just pass the path as the first parameter to `use` and you are done!

@@ -247,3 +247,3 @@ *Note that this does support routes with parameters, e.g. `/user/:id/comments`, but all the matched parameters will be discarded*

To guarantee compatibility with Express, adding a prefix uses [`path-to-regexp`](https://www.npmjs.com/package/path-to-regexp) to compute
a `RegExp`, which is then used to math every request: it is significantly slower.
a `RegExp`, which is then used to match every request: it is significantly slower.

@@ -264,3 +264,3 @@ ## TypeScript support

## Acknowledgements
## Acknowledgments

@@ -267,0 +267,0 @@ This project is kindly sponsored by:

@@ -17,3 +17,3 @@ 'use strict'

.after(() => {
fastify.use(function (req, res, next) {
fastify.use(function (_req, _res, next) {
t.pass('middleware called')

@@ -24,3 +24,3 @@ next()

fastify.addHook('onRequest', function (req, res, next) {
fastify.addHook('onRequest', function (_req, _res, next) {
t.pass('onRequest called')

@@ -30,3 +30,3 @@ next()

fastify.addHook('preHandler', function (request, reply, next) {
fastify.addHook('preHandler', function (_request, _reply, next) {
t.pass('preHandler called')

@@ -36,3 +36,3 @@ next()

fastify.addHook('onSend', function (request, reply, payload, next) {
fastify.addHook('onSend', function (_request, _reply, _payload, next) {
t.pass('onSend called')

@@ -42,3 +42,3 @@ next()

fastify.addHook('onResponse', function (request, reply, next) {
fastify.addHook('onResponse', function (_request, _reply, next) {
t.pass('onResponse called')

@@ -48,3 +48,3 @@ next()

fastify.get('/', function (req, reply) {
fastify.get('/', function (_req, reply) {
reply.send({ hello: 'world' })

@@ -63,3 +63,3 @@ })

headers: { 'Content-Type': 'application/json' }
}, (err, response, body) => {
}, (err, response) => {
t.error(err)

@@ -78,4 +78,4 @@ t.equal(response.statusCode, 404)

fastify.register(fp(function (instance, options, next) {
instance.addHook('onRequest', function (req, res, next) {
fastify.register(fp(function (instance, _options, next) {
instance.addHook('onRequest', function (_req, _res, next) {
t.pass('onRequest called')

@@ -85,3 +85,3 @@ next()

instance.use(function (req, res, next) {
instance.use(function (_req, _res, next) {
t.pass('middleware called')

@@ -91,3 +91,3 @@ next()

instance.addHook('preHandler', function (request, reply, next) {
instance.addHook('preHandler', function (_request, _reply, next) {
t.pass('preHandler called')

@@ -97,3 +97,3 @@ next()

instance.addHook('onSend', function (request, reply, payload, next) {
instance.addHook('onSend', function (_request, _reply, _payload, next) {
t.pass('onSend called')

@@ -103,3 +103,3 @@ next()

instance.addHook('onResponse', function (request, reply, next) {
instance.addHook('onResponse', function (_request, _reply, next) {
t.pass('onResponse called')

@@ -112,3 +112,3 @@ next()

fastify.get('/', function (req, reply) {
fastify.get('/', function (_req, reply) {
reply.send({ hello: 'world' })

@@ -124,3 +124,3 @@ })

headers: { 'Content-Type': 'application/json' }
}, (err, response, body) => {
}, (err, response) => {
t.error(err)

@@ -139,4 +139,4 @@ t.equal(response.statusCode, 404)

const plugin = fp((instance, opts, next) => {
instance.addHook('onRequest', function (req, res, next) {
const plugin = fp((instance, _opts, next) => {
instance.addHook('onRequest', function (_req, _res, next) {
t.pass('onRequest called')

@@ -146,3 +146,3 @@ next()

instance.use(function (req, res, next) {
instance.use(function (_req, _res, next) {
t.pass('middleware called')

@@ -152,3 +152,3 @@ next()

instance.addHook('preHandler', function (request, reply, next) {
instance.addHook('preHandler', function (_request, _reply, next) {
t.pass('preHandler called')

@@ -158,3 +158,3 @@ next()

instance.addHook('onSend', function (request, reply, payload, next) {
instance.addHook('onSend', function (_request, _reply, _payload, next) {
t.pass('onSend called')

@@ -164,3 +164,3 @@ next()

instance.addHook('onResponse', function (request, reply, next) {
instance.addHook('onResponse', function (_request, _reply, next) {
t.pass('onResponse called')

@@ -175,7 +175,7 @@ next()

fastify.get('/', function (req, reply) {
fastify.get('/', function (_req, reply) {
reply.send({ hello: 'world' })
})
fastify.setNotFoundHandler(function (req, reply) {
fastify.setNotFoundHandler(function (_req, reply) {
reply.code(404).send('this was not found')

@@ -207,3 +207,3 @@ })

.after(() => {
fastify.use(function (req, res, next) {
fastify.use(function (_req, _res, next) {
t.pass('middleware called')

@@ -214,3 +214,3 @@ next()

fastify.addHook('onRequest', function (req, res, next) {
fastify.addHook('onRequest', function (_req, _res, next) {
t.pass('onRequest called')

@@ -220,3 +220,3 @@ next()

fastify.addHook('preHandler', function (request, reply, next) {
fastify.addHook('preHandler', function (_request, _reply, next) {
t.pass('preHandler called')

@@ -226,3 +226,3 @@ next()

fastify.addHook('onSend', function (request, reply, payload, next) {
fastify.addHook('onSend', function (_request, _reply, _payload, next) {
t.pass('onSend called')

@@ -232,3 +232,3 @@ next()

fastify.addHook('onResponse', function (request, reply, next) {
fastify.addHook('onResponse', function (_request, _reply, next) {
t.pass('onResponse called')

@@ -238,8 +238,8 @@ next()

fastify.register(function (f, opts, next) {
f.setNotFoundHandler(function (req, reply) {
fastify.register(function (f, _opts, next) {
f.setNotFoundHandler(function (_req, reply) {
reply.code(404).send('this was not found 2')
})
f.addHook('onRequest', function (req, res, next) {
f.addHook('onRequest', function (_req, _res, next) {
t.pass('onRequest 2 called')

@@ -249,3 +249,3 @@ next()

f.use(function (req, res, next) {
f.use(function (_req, _res, next) {
t.pass('middleware 2 called')

@@ -255,3 +255,3 @@ next()

f.addHook('preHandler', function (request, reply, next) {
f.addHook('preHandler', function (_request, _reply, next) {
t.pass('preHandler 2 called')

@@ -261,3 +261,3 @@ next()

f.addHook('onSend', function (request, reply, payload, next) {
f.addHook('onSend', function (_request, _reply, _payload, next) {
t.pass('onSend 2 called')

@@ -267,3 +267,3 @@ next()

f.addHook('onResponse', function (request, reply, next) {
f.addHook('onResponse', function (_request, _reply, next) {
t.pass('onResponse 2 called')

@@ -286,3 +286,3 @@ next()

headers: { 'Content-Type': 'application/json' }
}, (err, response, body) => {
}, (err, response) => {
t.error(err)

@@ -301,3 +301,3 @@ t.equal(response.statusCode, 404)

.after(() => {
fastify.use(function (req, res, next) {
fastify.use(function (_req, _res, next) {
t.pass('middleware called')

@@ -308,3 +308,3 @@ next()

fastify.get('/', function (req, reply) {
fastify.get('/', function (_req, reply) {
reply.send({ hello: 'world' })

@@ -323,3 +323,3 @@ })

headers: { 'Content-Type': 'application/json' }
}, (err, response, body) => {
}, (err, response) => {
t.error(err)

@@ -338,3 +338,3 @@ t.equal(response.statusCode, 404)

.after(() => {
fastify.use(function (req, res, next) {
fastify.use(function (_req, _res, next) {
t.pass('middleware called')

@@ -345,8 +345,8 @@ next()

fastify.register(function (f, opts, next) {
f.setNotFoundHandler(function (req, reply) {
fastify.register(function (f, _opts, next) {
f.setNotFoundHandler(function (_req, reply) {
reply.code(404).send('this was not found 2')
})
f.use(function (req, res, next) {
f.use(function (_req, _res, next) {
t.pass('middleware 2 called')

@@ -369,3 +369,3 @@ next()

headers: { 'Content-Type': 'application/json' }
}, (err, response, body) => {
}, (err, response) => {
t.error(err)

@@ -372,0 +372,0 @@ t.equal(response.statusCode, 404)

@@ -20,3 +20,3 @@ 'use strict'

fastify.get('/', async (req, reply) => {
fastify.get('/', async () => {
return { hello: 'world' }

@@ -48,3 +48,3 @@ })

fastify.get('/', async (req, reply) => {
fastify.get('/', async () => {
return { hello: 'world' }

@@ -54,3 +54,3 @@ })

const address = await fastify.listen({ port: 0 })
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
sget({

@@ -79,3 +79,3 @@ method: 'GET',

fastify.get('/', async (req, reply) => {
fastify.get('/', async () => {
return { hello: 'world' }

@@ -85,3 +85,3 @@ })

const address = await fastify.listen({ port: 0 })
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
sget({

@@ -110,7 +110,7 @@ method: 'GET',

fastify.get('/cors/hello', async (req, reply) => {
fastify.get('/cors/hello', async () => {
return { hello: 'world' }
})
fastify.get('/', async (req, reply) => {
fastify.get('/', async () => {
return { hello: 'world' }

@@ -124,3 +124,3 @@ })

url: address + '/cors/hello'
}, (err, res, data) => {
}, (err, res) => {
t.error(err)

@@ -135,3 +135,3 @@ t.match(res.headers, {

url: address
}, (err, res, data) => {
}, (err, res) => {
t.error(err)

@@ -148,7 +148,7 @@ t.notOk(res.headers['access-control-allow-origin'])

fastify.register((instance, opts, next) => {
fastify.register((instance, _opts, next) => {
instance.register(middiePlugin)
.after(() => { instance.use(middleware) })
instance.get('/plugin', (req, reply) => {
instance.get('/plugin', (_req, reply) => {
reply.send('ok')

@@ -160,3 +160,3 @@ })

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.send('ok')

@@ -170,3 +170,3 @@ })

url: address
}, (err, res, data) => {
}, (err) => {
t.error(err)

@@ -177,3 +177,3 @@ fastify.close()

function middleware (req, res, next) {
function middleware () {
t.fail('Shuld not be called')

@@ -190,5 +190,5 @@ }

fastify.register((instance, opts, next) => {
fastify.register((instance, _opts, next) => {
instance.use(middleware)
instance.get('/plugin', (req, reply) => {
instance.get('/plugin', (_req, reply) => {
reply.send('ok')

@@ -200,3 +200,3 @@ })

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.send('ok')

@@ -210,3 +210,3 @@ })

url: address
}, (err, res, data) => {
}, (err) => {
t.error(err)

@@ -217,3 +217,3 @@ fastify.close()

function middleware (req, res, next) {
function middleware () {
t.fail('Shuld not be called')

@@ -232,5 +232,5 @@ }

fastify.register((instance, opts, next) => {
fastify.register((instance, _opts, next) => {
instance.use(cors())
instance.get('/plugin', (req, reply) => {
instance.get('/plugin', (_req, reply) => {
reply.send('ok')

@@ -242,3 +242,3 @@ })

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.send('ok')

@@ -252,3 +252,3 @@ })

url: address + '/plugin'
}, (err, res, data) => {
}, (err, res) => {
t.error(err)

@@ -263,3 +263,3 @@ t.match(res.headers, {

url: address
}, (err, res, data) => {
}, (err, res) => {
t.error(err)

@@ -285,5 +285,5 @@ t.notMatch(res.headers, {

fastify.register((instance, opts, next) => {
fastify.register((instance, _opts, next) => {
instance.use(middleware2)
instance.get('/plugin', (req, reply) => {
instance.get('/plugin', (_req, reply) => {
reply.send('ok')

@@ -295,3 +295,3 @@ })

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.send('ok')

@@ -305,3 +305,3 @@ })

url: address + '/plugin'
}, (err, res, data) => {
}, (err, res) => {
t.error(err)

@@ -317,3 +317,3 @@ t.match(res.headers, {

url: address
}, (err, res, data) => {
}, (err, res) => {
t.error(err)

@@ -326,3 +326,3 @@ t.match(res.headers, {

function middleware1 (req, res, next) {
function middleware1 (_req, res, next) {
res.setHeader('x-middleware-1', true)

@@ -332,3 +332,3 @@ next()

function middleware2 (req, res, next) {
function middleware2 (_req, res, next) {
res.setHeader('x-middleware-2', true)

@@ -351,11 +351,11 @@ next()

fastify.register((instance, opts, next) => {
fastify.register((instance, _opts, next) => {
instance.use(middleware2)
instance.get('/', (req, reply) => {
instance.get('/', (_req, reply) => {
reply.send('ok')
})
instance.register((i, opts, next) => {
instance.register((i, _opts, next) => {
i.use(middleware3)
i.get('/nested', (req, reply) => {
i.get('/nested', (_req, reply) => {
reply.send('ok')

@@ -370,3 +370,3 @@ })

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.send('ok')

@@ -380,3 +380,3 @@ })

url: address + '/plugin/nested'
}, (err, res, data) => {
}, (err, res) => {
t.error(err)

@@ -393,3 +393,3 @@ t.match(res.headers, {

url: address + '/plugin'
}, (err, res, data) => {
}, (err, res) => {
t.error(err)

@@ -405,3 +405,3 @@ t.match(res.headers, {

url: address
}, (err, res, data) => {
}, (err, res) => {
t.error(err)

@@ -414,3 +414,3 @@ t.match(res.headers, {

function middleware1 (req, res, next) {
function middleware1 (_req, res, next) {
res.setHeader('x-middleware-1', true)

@@ -420,3 +420,3 @@ next()

function middleware2 (req, res, next) {
function middleware2 (_req, res, next) {
res.setHeader('x-middleware-2', true)

@@ -426,3 +426,3 @@ next()

function middleware3 (req, res, next) {
function middleware3 (_req, res, next) {
res.setHeader('x-middleware-3', true)

@@ -448,3 +448,3 @@ next()

fastify.get('/', async (req, reply) => {
fastify.get('/', async () => {
return { hello: 'world' }

@@ -458,3 +458,3 @@ })

url: address
}, (err, res, data) => {
}, (err) => {
t.error(err)

@@ -465,3 +465,3 @@ fastify.close()

function middleware1 (req, res, next) {
function middleware1 (_req, _res, next) {
t.equal(order.shift(), 1)

@@ -471,3 +471,3 @@ next()

function middleware2 (req, res, next) {
function middleware2 (_req, _res, next) {
t.equal(order.shift(), 2)

@@ -477,3 +477,3 @@ next()

function middleware3 (req, res, next) {
function middleware3 (_req, _res, next) {
t.equal(order.shift(), 3)

@@ -498,3 +498,3 @@ next()

fastify.get('/', async (req, reply) => {
fastify.get('/', async () => {
return { hello: 'world' }

@@ -508,3 +508,3 @@ })

url: address
}, (err, res, data) => {
}, (err, res, _data) => {
t.error(err)

@@ -516,11 +516,11 @@ t.equal(res.statusCode, 500)

function middleware1 (req, res, next) {
function middleware1 (_req, _res, next) {
next(new Error('middleware1'))
}
function middleware2 (req, res, next) {
function middleware2 () {
t.fail('this should not be executed')
}
function middleware3 (req, res, next) {
function middleware3 () {
t.fail('this should not be executed')

@@ -535,3 +535,3 @@ }

fastify.setErrorHandler((err, req, reply) => {
fastify.setErrorHandler((err, _req, reply) => {
t.equal(err.message, 'middleware2')

@@ -550,3 +550,3 @@ reply.send(err)

fastify.get('/', async (req, reply) => {
fastify.get('/', async () => {
return { hello: 'world' }

@@ -560,3 +560,3 @@ })

url: address
}, (err, res, data) => {
}, (err, res, _data) => {
t.error(err)

@@ -568,3 +568,3 @@ t.equal(res.statusCode, 500)

function middleware1 (req, res, next) {
function middleware1 (_req, _res, next) {
t.pass('called')

@@ -574,7 +574,7 @@ next()

function middleware2 (req, res, next) {
function middleware2 (_req, _res, next) {
next(new Error('middleware2'))
}
function middleware3 (req, res, next) {
function middleware3 () {
t.fail('We should not be here')

@@ -597,19 +597,19 @@ }

fastify.addHook('preValidation', (req, reply, next) => {
fastify.addHook('preValidation', () => {
t.fail('We should not be here')
})
fastify.addHook('preParsing', (req, reply, payload, next) => {
fastify.addHook('preParsing', () => {
t.fail('We should not be here')
})
fastify.addHook('preHandler', (req, reply, next) => {
fastify.addHook('preHandler', () => {
t.fail('We should not be here')
})
fastify.addHook('onSend', (req, reply, next) => {
fastify.addHook('onSend', () => {
t.fail('We should not be here')
})
fastify.addHook('onResponse', (req, reply, next) => {
fastify.addHook('onResponse', (_req, _reply, next) => {
t.ok('called')

@@ -619,3 +619,3 @@ next()

fastify.get('/', (req, reply) => {
fastify.get('/', () => {
t.fail('We should not be here')

@@ -630,3 +630,3 @@ })

json: true
}, (err, res, data) => {
}, (err, _res, data) => {
t.error(err)

@@ -638,7 +638,7 @@ t.same(data, { hello: 'world' })

function middleware1 (req, res, next) {
function middleware1 (_req, res, _next) {
res.end(JSON.stringify({ hello: 'world' }))
}
function middleware2 (req, res, next) {
function middleware2 (_req, _res, _next) {
t.fail('We should not be here')

@@ -655,4 +655,4 @@ }

fastify.register((instance, opts, next) => {
instance.use('/world', (req, res, next) => {
fastify.register((instance, _opts, next) => {
instance.use('/world', (_req, res, next) => {
res.setHeader('x-foo', 'bar')

@@ -662,3 +662,3 @@ next()

instance.get('/world', (req, reply) => {
instance.get('/world', (_req, reply) => {
reply.send({ hello: 'world' })

@@ -695,3 +695,3 @@ })

fastify.use(function (req, res, next) {
fastify.use(function (_req, _res, next) {
t.ok(onRequestCalled)

@@ -701,3 +701,3 @@ next()

fastify.addHook('onRequest', function (req, reply, next) {
fastify.addHook('onRequest', function (_req, _reply, next) {
onRequestCalled = true

@@ -707,3 +707,3 @@ next()

fastify.get('/', async (req, reply) => {
fastify.get('/', async () => {
return { hello: 'world' }

@@ -728,3 +728,3 @@ })

fastify.use(function (req, res, next) {
fastify.use(function (_req, _res, next) {
t.ok(onRequestCalled)

@@ -734,3 +734,3 @@ next()

fastify.addHook('onRequest', function (req, reply, next) {
fastify.addHook('onRequest', function (_req, _reply, next) {
onRequestCalled = true

@@ -740,3 +740,3 @@ next()

fastify.get('/', async (req, reply) => {
fastify.get('/', async () => {
return { hello: 'world' }

@@ -761,3 +761,3 @@ })

fastify.use(function (req, res, next) {
fastify.use(function (_req, _res, next) {
t.ok(onRequestCalled)

@@ -767,3 +767,3 @@ next()

fastify.addHook('onRequest', function (req, reply, next) {
fastify.addHook('onRequest', function (_req, _reply, next) {
onRequestCalled = true

@@ -773,3 +773,3 @@ next()

fastify.get('/', async (req, reply) => {
fastify.get('/', async () => {
return { hello: 'world' }

@@ -794,3 +794,3 @@ })

fastify.use(function (req, res, next) {
fastify.use(function (_req, _res, next) {
t.ok(onRequestCalled)

@@ -800,3 +800,3 @@ next()

fastify.addHook('onRequest', function (req, reply, next) {
fastify.addHook('onRequest', function (_req, _reply, next) {
onRequestCalled = true

@@ -806,3 +806,3 @@ next()

fastify.get('/', async (req, reply) => {
fastify.get('/', async () => {
return { hello: 'world' }

@@ -827,3 +827,3 @@ })

fastify.use(function (req, res, next) {
fastify.use(function (_req, _res, next) {
t.ok(onRequestCalled)

@@ -833,3 +833,3 @@ next()

fastify.addHook('onRequest', function (req, reply, next) {
fastify.addHook('onRequest', function (_req, _reply, next) {
onRequestCalled = true

@@ -839,3 +839,3 @@ next()

fastify.get('/', async (req, reply) => {
fastify.get('/', async () => {
return { hello: 'world' }

@@ -842,0 +842,0 @@ })

@@ -40,3 +40,3 @@ 'use strict'

t.equal(instance.use(function (req, res, next) {
t.equal(instance.use(function (_req, _res, next) {
t.pass('function called')

@@ -63,6 +63,6 @@ next()

instance.use(function (req, res, next) {
instance.use(function (_req, _res, next) {
t.equal(counter++, 0, 'first function called')
next()
}).use(function (req, res, next) {
}).use(function (_req, _res, next) {
t.equal(counter++, 1, 'second function called')

@@ -78,3 +78,3 @@ next()

const instance = middie(function (err, a, b) {
const instance = middie(function (err) {
t.ok(err, 'error is forwarded')

@@ -87,5 +87,5 @@ })

instance.use(function (req, res, next) {
instance.use(function (_req, _res, next) {
next(new Error('kaboom'))
}).use(function (req, res, next) {
}).use(function (_req, _res, next) {
t.fail('this should never be called')

@@ -112,3 +112,3 @@ next()

t.equal(instance.use(function (req, res, next) {
t.equal(instance.use(function (_req, _res, next) {
t.ok('function called')

@@ -118,3 +118,3 @@ next()

t.equal(instance.use('/test', function (req, res, next) {
t.equal(instance.use('/test', function (_req, _res, next) {
t.ok('function called')

@@ -124,3 +124,3 @@ next()

t.equal(instance.use('/test', function (req, res, next) {
t.equal(instance.use('/test', function (req, _res, next) {
t.equal('/', req.url)

@@ -130,3 +130,3 @@ next()

t.equal(instance.use('/no-call', function (req, res, next) {
t.equal(instance.use('/no-call', function (_req, _res, next) {
t.fail('should not call this function')

@@ -153,3 +153,3 @@ next()

t.equal(instance.use(function (req, res, next) {
t.equal(instance.use(function (_req, _res, next) {
t.ok('function called')

@@ -159,3 +159,3 @@ next()

t.equal(instance.use('/test', function (req, res, next) {
t.equal(instance.use('/test', function (_req, _res, next) {
t.ok('function called')

@@ -165,3 +165,3 @@ next()

t.equal(instance.use('/test', function (req, res, next) {
t.equal(instance.use('/test', function (req, _res, next) {
t.equal('/other/one', req.url)

@@ -188,3 +188,3 @@ next()

t.equal(instance.use(function (req, res, next) {
t.equal(instance.use(function (_req, _res, next) {
t.ok('function called')

@@ -194,3 +194,3 @@ next()

t.equal(instance.use('/tasks-api', function (req, res, next) {
t.equal(instance.use('/tasks-api', function (_req, _res, next) {
t.ok('function called')

@@ -200,3 +200,3 @@ next()

t.equal(instance.use('/tasks-api', function (req, res, next) {
t.equal(instance.use('/tasks-api', function (req, _res, next) {
t.equal('/task', req.url)

@@ -223,3 +223,3 @@ next()

t.equal(instance.use(function (req, res, next) {
t.equal(instance.use(function (_req, _res, next) {
t.ok('function called')

@@ -229,3 +229,3 @@ next()

t.equal(instance.use(['/test', '/other-path'], function (req, res, next) {
t.equal(instance.use(['/test', '/other-path'], function (_req, _res, next) {
t.ok('function called')

@@ -235,3 +235,3 @@ next()

t.equal(instance.use(['/no-call', 'other-path'], function (req, res, next) {
t.equal(instance.use(['/no-call', 'other-path'], function (_req, _res, next) {
t.fail('should not call this function')

@@ -258,6 +258,6 @@ next()

t.equal(instance.use([function (req, res, next) {
t.equal(instance.use([function (_req, _res, next) {
t.ok('function called')
next()
}, function (req, res, next) {
}, function (_req, _res, next) {
t.ok('function called')

@@ -267,6 +267,6 @@ next()

t.equal(instance.use('/test', [function (req, res, next) {
t.equal(instance.use('/test', [function (_req, _res, next) {
t.ok('function called')
next()
}, function (req, res, next) {
}, function (_req, _res, next) {
t.ok('function called')

@@ -293,6 +293,6 @@ next()

t.equal(instance.use([function (req, res, next) {
t.equal(instance.use([function (_req, _res, next) {
t.ok('function called')
next()
}, function (req, res, next) {
}, function (_req, _res, next) {
t.ok('function called')

@@ -302,6 +302,6 @@ next()

t.equal(instance.use(['/test', '/other-path'], [function (req, res, next) {
t.equal(instance.use(['/test', '/other-path'], [function (_req, _res, next) {
t.ok('function called')
next()
}, function (req, res, next) {
}, function (_req, _res, next) {
t.ok('function called')

@@ -328,3 +328,3 @@ next()

t.equal(instance.use('/test', function (req, res, next) {
t.equal(instance.use('/test', function (_req, _res, next) {
t.pass('function called')

@@ -351,3 +351,3 @@ next()

t.equal(instance.use(function (req, res, next) {
t.equal(instance.use(function (_req, _res, next) {
t.pass('function called')

@@ -371,3 +371,3 @@ next()

instance.use(function (req, res, next) {
instance.use(function (req, _res, next) {
t.equal(req.originalUrl, '/test')

@@ -422,3 +422,3 @@ next()

t.plan(2)
const instance = middie(function (err, req, res) {
const instance = middie(function (err, req) {
t.error(err)

@@ -458,3 +458,3 @@ t.same(req, {

prefixes.forEach(function (o) {
instance.use(o.prefix, function (req, res, next) {
instance.use(o.prefix, function (req, _res, next) {
if (req[o.name]) throw new Error('Called twice!')

@@ -471,3 +471,3 @@ req[o.name] = true

t.plan(3)
const instance = middie(function (err, req, res) {
const instance = middie(function (err, req) {
t.error(err)

@@ -484,3 +484,3 @@ t.same(req, {

instance.use('/path/', function (req, res, next) {
instance.use('/path/', function (_req, _res, next) {
t.pass('function called')

@@ -490,3 +490,3 @@ next()

instance.use('/path/inner', function (req, res, next) {
instance.use('/path/inner', function (_req, _res, next) {
t.fail()

@@ -517,7 +517,7 @@ next()

instance
.use(function (req, res, next) {
.use(function (_req, res, next) {
res.end('hello')
next()
})
.use(function (req, res, next) {
.use(function () {
t.fail('we should not be here')

@@ -540,7 +540,7 @@ })

instance
.use(function (req, res, next) {
.use(function (_req, res, next) {
res.end('bye')
next()
})
.use(function (req, res, next) {
.use(function () {
t.fail('we should not be here')

@@ -547,0 +547,0 @@ })

@@ -44,3 +44,3 @@ 'use strict'

},
(err, res, data) => {
(err) => {
t.error(err)

@@ -81,3 +81,3 @@ }

},
(err, res, data) => {
(err) => {
t.error(err)

@@ -96,3 +96,3 @@ }

fastify.use(cors())
fastify.use((req, res, next) => {
fastify.use((req, _res, next) => {
t.equal('body' in req, false)

@@ -110,3 +110,3 @@ next()

},
(err, res, data) => {
(err) => {
t.error(err)

@@ -113,0 +113,0 @@ }

@@ -12,3 +12,3 @@ 'use strict'

.after(() => {
fastify.use(function (req, res, next) {
fastify.use(function (_req, _res, next) {
next(new Error('middlware failed'))

@@ -21,3 +21,3 @@ })

fastify.addHook('onSend', function (request, reply, payload, next) {
fastify.addHook('onSend', function (request, reply, _payload, next) {
t.equal(request.testDecorator, 'testDecoratorVal')

@@ -28,3 +28,3 @@ t.equal(reply.testDecorator, 'testDecoratorVal')

fastify.get('/', (req, reply) => {
fastify.get('/', (_req, reply) => {
reply.send('hello')

@@ -31,0 +31,0 @@ })

@@ -22,3 +22,3 @@ 'use strict'

.after(() => {
const useRes = instance.use(function (req, res, next) {
const useRes = instance.use(function (_req, _res, next) {
t.pass('middleware called')

@@ -31,3 +31,3 @@ next()

instance.get('/', function (request, reply) {
instance.get('/', function (_request, reply) {
reply.send({ hello: 'world' })

@@ -62,3 +62,3 @@ })

instance.get('/', function (request, reply) {
instance.get('/', function (_request, reply) {
reply.send({ hello: 'world' })

@@ -75,3 +75,3 @@ })

url: 'http://localhost:' + instance.server.address().port
}, (err, response, body) => {
}, (err, response) => {
t.error(err)

@@ -92,3 +92,3 @@ t.equal(response.headers['access-control-allow-origin'], '*')

instance.get('/', function (request, reply) {
instance.get('/', function (_request, reply) {
reply.send({ hello: 'world' })

@@ -105,3 +105,3 @@ })

url: 'http://localhost:' + instance.server.address().port
}, (err, response, body) => {
}, (err, response) => {
t.error(err)

@@ -123,3 +123,3 @@ t.ok(response.headers['x-xss-protection'])

instance.get('/', function (request, reply) {
instance.get('/', function (_request, reply) {
reply.send({ hello: 'world' })

@@ -136,3 +136,3 @@ })

url: 'http://localhost:' + instance.server.address().port
}, (err, response, body) => {
}, (err, response) => {
t.error(err)

@@ -151,19 +151,19 @@ t.ok(response.headers['x-xss-protection'])

.after(() => {
instance.use(function (req, res, next) {
instance.use(function (req, _res, next) {
req.global = true
next()
})
instance.use('', function (req, res, next) {
instance.use('', function (req, _res, next) {
req.global2 = true
next()
})
instance.use('/', function (req, res, next) {
instance.use('/', function (req, _res, next) {
req.root = true
next()
})
instance.use('/prefix', function (req, res, next) {
instance.use('/prefix', function (req, _res, next) {
req.prefixed = true
next()
})
instance.use('/prefix/', function (req, res, next) {
instance.use('/prefix/', function (req, _res, next) {
req.slashed = true

@@ -199,3 +199,3 @@ next()

json: true
}, (err, response, body) => {
}, (err, _response, body) => {
t.error(err)

@@ -216,3 +216,3 @@ t.same(body, {

json: true
}, (err, response, body) => {
}, (err, _response, body) => {
t.error(err)

@@ -235,3 +235,3 @@ t.same(body, {

json: true
}, (err, response, body) => {
}, (err, _response, body) => {
t.error(err)

@@ -254,3 +254,3 @@ t.same(body, {

json: true
}, (err, response, body) => {
}, (err, _response, body) => {
t.error(err)

@@ -275,7 +275,7 @@ t.same(body, {

.after(() => {
instance.use(function (req, res, next) {
instance.use(function (_req, res, _next) {
res.end('hello')
})
instance.use(function (req, res, next) {
instance.use(function () {
t.fail('we should not be here')

@@ -285,3 +285,3 @@ })

instance.addHook('onRequest', (req, res, next) => {
instance.addHook('onRequest', (_req, _res, next) => {
t.ok('called')

@@ -291,11 +291,11 @@ next()

instance.addHook('preHandler', (req, reply, next) => {
instance.addHook('preHandler', (_req, _reply, _next) => {
t.fail('this should not be called')
})
instance.addHook('onSend', (req, reply, payload, next) => {
instance.addHook('onSend', (_req, _reply, _payload, _next) => {
t.fail('this should not be called')
})
instance.addHook('onResponse', (request, reply, next) => {
instance.addHook('onResponse', (_request, _reply, next) => {
t.ok('called')

@@ -305,3 +305,3 @@ next()

instance.get('/', function (request, reply) {
instance.get('/', function () {
t.fail('we should no be here')

@@ -325,3 +325,3 @@ })

instance.addHook('onRequest', (req, res, next) => {
instance.addHook('onRequest', (_req, _res, next) => {
t.ok('called')

@@ -333,3 +333,3 @@ next()

.after(() => {
instance.use(function (req, res, next) {
instance.use(function (_req, res, next) {
const stream = fs.createReadStream(process.cwd() + '/test/middleware.test.js', 'utf8')

@@ -340,3 +340,3 @@ stream.pipe(res)

instance.use(function (req, res, next) {
instance.use(function () {
t.fail('we should not be here')

@@ -346,11 +346,11 @@ })

instance.addHook('preHandler', (req, reply, next) => {
instance.addHook('preHandler', () => {
t.fail('this should not be called')
})
instance.addHook('onSend', (req, reply, payload, next) => {
instance.addHook('onSend', () => {
t.fail('this should not be called')
})
instance.addHook('onResponse', (request, reply, next) => {
instance.addHook('onResponse', (_request, _reply, next) => {
t.ok('called')

@@ -360,3 +360,3 @@ next()

instance.get('/', function (request, reply) {
instance.get('/', function () {
t.fail('we should no be here')

@@ -379,4 +379,4 @@ })

f.register(function (instance, opts, next) {
instance.use(function (req, res, next) {
f.register(function (instance, _opts, next) {
instance.use(function (_req, _res, next) {
t.ok('first middleware called')

@@ -386,3 +386,3 @@ next()

instance.get('/', function (request, reply) {
instance.get('/', function (_request, reply) {
reply.send({ hello: 'world' })

@@ -394,4 +394,4 @@ })

f.register(fp(function (instance, opts, next) {
instance.use(function (req, res, next) {
f.register(fp(function (instance, _opts, next) {
instance.use(function (_req, _res, next) {
t.ok('second middleware called')

@@ -416,4 +416,4 @@ next()

f.register(fp(function (instance, opts, next) {
instance.use(function (req, res, next) {
f.register(fp(function (instance, _opts, next) {
instance.use(function (req, _res, next) {
t.equal(req.previous, undefined)

@@ -424,4 +424,4 @@ req.previous = 1

instance.register(fp(function (i, opts, next) {
i.use(function (req, res, next) {
instance.register(fp(function (i, _opts, next) {
i.use(function (req, _res, next) {
t.equal(req.previous, 2)

@@ -434,3 +434,3 @@ req.previous = 3

instance.use(function (req, res, next) {
instance.use(function (req, _res, next) {
t.equal(req.previous, 1)

@@ -444,4 +444,4 @@ req.previous = 2

f.register(function (instance, opts, next) {
instance.use(function (req, res, next) {
f.register(function (instance, _opts, next) {
instance.use(function (req, _res, next) {
t.equal(req.previous, 3)

@@ -457,4 +457,4 @@ req.previous = 4

instance.register(fp(function (i, opts, next) {
i.use(function (req, res, next) {
instance.register(fp(function (i, _opts, next) {
i.use(function (req, _res, next) {
t.equal(req.previous, 4)

@@ -461,0 +461,0 @@ req.previous = 5

import * as connect from 'connect'
import { FastifyPluginCallback } from 'fastify'
import * as http from "http";
import * as http from 'node:http'
declare module "fastify" {
declare module 'fastify' {
interface FastifyInstance {

@@ -19,3 +19,3 @@ use(fn: fastifyMiddie.Handler): this;

}
/**

@@ -30,8 +30,8 @@ * @deprecated Use FastifyMiddieOptions instead

}
export type NextFunction = (err?: any) => void;
export type SimpleHandleFunction = (req: http.IncomingMessage & IncomingMessageExtended, res: http.ServerResponse) => void;
export type NextHandleFunction = (req: connect.IncomingMessage & IncomingMessageExtended, res: http.ServerResponse, next: NextFunction) => void;
export type NextFunction = (err?: any) => void
export type SimpleHandleFunction = (req: http.IncomingMessage & IncomingMessageExtended, res: http.ServerResponse) => void
export type NextHandleFunction = (req: connect.IncomingMessage & IncomingMessageExtended, res: http.ServerResponse, next: NextFunction) => void
export type Handler = SimpleHandleFunction | NextHandleFunction
export const fastifyMiddie: FastifyMiddie

@@ -41,3 +41,3 @@ export { fastifyMiddie as default }

declare function fastifyMiddie(...params: Parameters<FastifyMiddie>): ReturnType<FastifyMiddie>
declare function fastifyMiddie (...params: Parameters<FastifyMiddie>): ReturnType<FastifyMiddie>
export = fastifyMiddie

@@ -1,11 +0,11 @@

import fastify from "fastify";
import middiePlugin, {MiddiePluginOptions, IncomingMessageExtended, FastifyMiddieOptions} from "..";
import { expectAssignable, expectType, expectDeprecated } from "tsd";
import fastify from 'fastify'
import middiePlugin, { MiddiePluginOptions, IncomingMessageExtended, FastifyMiddieOptions } from '..'
import { expectAssignable, expectType, expectDeprecated } from 'tsd'
const app = fastify();
app.register(middiePlugin);
const app = fastify()
app.register(middiePlugin)
expectAssignable<MiddiePluginOptions>({})
expectAssignable<IncomingMessageExtended>({ body: { foo: 'bar' }, query: { bar: 'foo' }})
expectAssignable<IncomingMessageExtended>({ body: { foo: 'bar' }, query: { bar: 'foo' } })
expectAssignable<IncomingMessageExtended>({})

@@ -12,0 +12,0 @@