@fastify/middie
Advanced tools
Comparing version 8.2.0 to 8.3.0
41
index.js
'use strict' | ||
const fp = require('fastify-plugin') | ||
const Middie = require('./engine') | ||
const Middie = require('./lib/engine') | ||
const kMiddlewares = Symbol('fastify-middie-middlewares') | ||
const kMiddie = Symbol('fastify-middie-instance') | ||
const kMiddieHasMiddlewares = Symbol('fastify-middie-has-middlewares') | ||
const { FST_ERR_MIDDIE_INVALID_HOOK } = require('./lib/errors') | ||
const supportedHooksWithPayload = [ | ||
'onError', | ||
'onSend', | ||
'preParsing', | ||
'preSerialization' | ||
] | ||
const supportedHooksWithoutPayload = [ | ||
'onRequest', | ||
'onResponse', | ||
'onTimeout', | ||
'preHandler', | ||
'preValidation' | ||
] | ||
const supportedHooks = [...supportedHooksWithPayload, ...supportedHooksWithoutPayload] | ||
function fastifyMiddie (fastify, options, next) { | ||
fastify.decorate('use', use) | ||
fastify[kMiddlewares] = [] | ||
fastify[kMiddieHasMiddlewares] = false | ||
fastify[kMiddie] = Middie(onMiddieEnd) | ||
const hook = options.hook || 'onRequest' | ||
if (!supportedHooks.includes(hook)) { | ||
next(new FST_ERR_MIDDIE_INVALID_HOOK(hook)) | ||
return | ||
} | ||
fastify | ||
.addHook(options.hook || 'onRequest', runMiddie) | ||
.addHook(hook, supportedHooksWithPayload.includes(hook) | ||
? runMiddieWithPayload | ||
: runMiddie) | ||
.addHook('onRegister', onRegister) | ||
@@ -28,2 +57,3 @@ | ||
} | ||
this[kMiddieHasMiddlewares] = true | ||
return this | ||
@@ -33,3 +63,3 @@ } | ||
function runMiddie (req, reply, next) { | ||
if (this[kMiddlewares].length > 0) { | ||
if (this[kMiddieHasMiddlewares]) { | ||
req.raw.originalUrl = req.raw.url | ||
@@ -51,2 +81,6 @@ req.raw.id = req.id | ||
function runMiddieWithPayload (req, reply, _payload, next) { | ||
runMiddie.bind(this)(req, reply, next) | ||
} | ||
function onMiddieEnd (err, req, res, next) { | ||
@@ -60,2 +94,3 @@ next(err) | ||
instance[kMiddie] = Middie(onMiddieEnd) | ||
instance[kMiddieHasMiddlewares] = false | ||
instance.decorate('use', use) | ||
@@ -62,0 +97,0 @@ for (const middleware of middlewares) { |
{ | ||
"name": "@fastify/middie", | ||
"version": "8.2.0", | ||
"version": "8.3.0", | ||
"description": "Middleware engine for Fastify", | ||
@@ -31,15 +31,15 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "git+https://github.com/fastify/middleman.git" | ||
"url": "git+https://github.com/fastify/middie.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/fastify/middleman/issues" | ||
"url": "https://github.com/fastify/middie/issues" | ||
}, | ||
"homepage": "https://github.com/fastify/middleman#readme", | ||
"homepage": "https://github.com/fastify/middie#readme", | ||
"devDependencies": { | ||
"@fastify/pre-commit": "^2.0.2", | ||
"@types/connect": "^3.4.33", | ||
"@types/node": "^18.0.1", | ||
"@types/node": "^20.1.0", | ||
"cors": "^2.8.5", | ||
"fastify": "^4.0.0-rc.2", | ||
"helmet": "^6.0.0", | ||
"helmet": "^7.0.0", | ||
"serve-static": "^1.14.1", | ||
@@ -52,2 +52,3 @@ "simple-get": "^4.0.0", | ||
"dependencies": { | ||
"@fastify/error": "^3.2.0", | ||
"fastify-plugin": "^4.0.0", | ||
@@ -54,0 +55,0 @@ "path-to-regexp": "^6.1.0", |
@@ -96,6 +96,17 @@ # @fastify/middie | ||
If you want to change the Fastify hook that the middleware will be attached to, pass a `hook` option like so: | ||
It is possible to change the Fastify hook that the middleware will be attached to. Supported lifecycle hooks are: | ||
- `onRequest` | ||
- `preParsing` | ||
- `preValidation` | ||
- `preHandler` | ||
- `preSerialization` | ||
- `onSend` | ||
- `onResponse` | ||
- `onError` | ||
- `onTimeout` | ||
*Note you can access `req.body` from the `preValidation` lifecycle step onwards. Take a look at the [Lifecycle](https://www.fastify.io/docs/latest/Reference/Lifecycle/) documentation page to see the order of the steps.* | ||
To change the hook, pass a `hook` option like so: | ||
*Note you can access `req.body` from the `preParsing`, `onError`, `preSerialization` and `onSend` lifecycle steps. Take a look at the [Lifecycle](https://www.fastify.io/docs/latest/Reference/Lifecycle/) documentation page to see the order of the steps.* | ||
```js | ||
@@ -102,0 +113,0 @@ const fastify = require('fastify')() |
@@ -9,2 +9,3 @@ 'use strict' | ||
const middiePlugin = require('../index') | ||
const { FST_ERR_MIDDIE_INVALID_HOOK } = require('../lib/errors') | ||
@@ -630,3 +631,5 @@ test('Should support connect style middlewares', t => { | ||
test('register the middleware at a different hook', async t => { | ||
test('register the middleware at preHandler hook', async t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
@@ -658,1 +661,131 @@ t.teardown(fastify.close) | ||
}) | ||
test('register the middleware at preParsing hook', async t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
t.teardown(fastify.close) | ||
let onRequestCalled = false | ||
await fastify.register(middiePlugin, { | ||
hook: 'preParsing' | ||
}) | ||
fastify.use(function (req, res, next) { | ||
t.ok(onRequestCalled) | ||
next() | ||
}) | ||
fastify.addHook('onRequest', function (req, reply, next) { | ||
onRequestCalled = true | ||
next() | ||
}) | ||
fastify.get('/', async (req, reply) => { | ||
return { hello: 'world' } | ||
}) | ||
const res = await fastify.inject('/') | ||
t.same(res.json(), { hello: 'world' }) | ||
}) | ||
test('register the middleware at preValidation hook', async t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
t.teardown(fastify.close) | ||
let onRequestCalled = false | ||
await fastify.register(middiePlugin, { | ||
hook: 'preValidation' | ||
}) | ||
fastify.use(function (req, res, next) { | ||
t.ok(onRequestCalled) | ||
next() | ||
}) | ||
fastify.addHook('onRequest', function (req, reply, next) { | ||
onRequestCalled = true | ||
next() | ||
}) | ||
fastify.get('/', async (req, reply) => { | ||
return { hello: 'world' } | ||
}) | ||
const res = await fastify.inject('/') | ||
t.same(res.json(), { hello: 'world' }) | ||
}) | ||
test('register the middleware at preSerialization hook', async t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
t.teardown(fastify.close) | ||
let onRequestCalled = false | ||
await fastify.register(middiePlugin, { | ||
hook: 'preSerialization' | ||
}) | ||
fastify.use(function (req, res, next) { | ||
t.ok(onRequestCalled) | ||
next() | ||
}) | ||
fastify.addHook('onRequest', function (req, reply, next) { | ||
onRequestCalled = true | ||
next() | ||
}) | ||
fastify.get('/', async (req, reply) => { | ||
return { hello: 'world' } | ||
}) | ||
const res = await fastify.inject('/') | ||
t.same(res.json(), { hello: 'world' }) | ||
}) | ||
test('register the middleware at onSend hook', async t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
t.teardown(fastify.close) | ||
let onRequestCalled = false | ||
await fastify.register(middiePlugin, { | ||
hook: 'onSend' | ||
}) | ||
fastify.use(function (req, res, next) { | ||
t.ok(onRequestCalled) | ||
next() | ||
}) | ||
fastify.addHook('onRequest', function (req, reply, next) { | ||
onRequestCalled = true | ||
next() | ||
}) | ||
fastify.get('/', async (req, reply) => { | ||
return { hello: 'world' } | ||
}) | ||
const res = await fastify.inject('/') | ||
t.same(res.json(), { hello: 'world' }) | ||
}) | ||
test('throw error when registering middie at onRequestAborted hook', async t => { | ||
const fastify = Fastify() | ||
t.teardown(fastify.close) | ||
t.rejects(() => fastify.register(middiePlugin, { | ||
hook: 'onRequestAborted' | ||
}), new FST_ERR_MIDDIE_INVALID_HOOK('onRequestAborted') | ||
) | ||
}) |
'use strict' | ||
const middie = require('../engine') | ||
const middie = require('../lib/engine') | ||
const t = require('tap') | ||
@@ -5,0 +5,0 @@ const http = require('http') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66299
18
2079
263
2
4
2
+ Added@fastify/error@^3.2.0
+ Added@fastify/error@3.4.1(transitive)