fastify 4.5.0 → 4.5.3

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.
@@ -102,11 +102,15 @@ section.
102
102
  - [`@fastify/schedule`](https://github.com/fastify/fastify-schedule) Plugin for
103
103
  scheduling periodic jobs, based on
104
104
  [toad-scheduler](https://github.com/kibertoad/toad-scheduler).
105
+ - [`@fastify/secure-session`](https://github.com/fastify/fastify-secure-session)
106
+ Create a secure stateless cookie session for Fastify.
105
107
  - [`@fastify/sensible`](https://github.com/fastify/fastify-sensible) Defaults
106
108
  for Fastify that everyone can agree on. It adds some useful decorators such as
107
109
  HTTP errors and assertions, but also more request and reply methods.
108
110
  - [`@fastify/session`](https://github.com/fastify/session) a session plugin for
109
111
  Fastify.
112
+ - [`@fastify/soap-client`](https://github.com/fastify/fastify-soap-client) a SOAP
113
+ client plugin for Fastify.
110
114
  - [`@fastify/static`](https://github.com/fastify/fastify-static) Plugin for
111
115
  serving static files as fast as possible.
112
116
  - [`@fastify/swagger`](https://github.com/fastify/fastify-swagger) Plugin for
@@ -485,8 +489,6 @@ section.
485
489
  - [`fastify-schema-to-typescript`](https://github.com/thomasthiebaud/fastify-schema-to-typescript)
486
490
  Generate typescript types based on your JSON/YAML validation schemas so they
487
491
  are always in sync.
488
- - [`fastify-secure-session`](https://github.com/mcollina/fastify-secure-session)
489
- Create a secure stateless cookie session for Fastify.
490
492
  - [`fastify-sentry`](https://github.com/alex-ppg/fastify-sentry) Fastify plugin
491
493
  to add the Sentry SDK error handler to requests.
492
494
  - [`fastify-sequelize`](https://github.com/lyquocnam/fastify-sequelize) Fastify
@@ -497,8 +499,6 @@ section.
497
499
  `fastify-caching`.
498
500
  - [`fastify-slonik`](https://github.com/Unbuttun/fastify-slonik) Fastify Slonik
499
501
  plugin, with this you can use slonik in every part of your server.
500
- - [`fastify-soap-client`](https://github.com/fastify/fastify-soap-client) a SOAP
501
- client plugin for Fastify.
502
502
  - [`fastify-socket.io`](https://github.com/alemagio/fastify-socket.io) a
503
503
  Socket.io plugin for Fastify.
504
504
  - [`fastify-split-validator`](https://github.com/MetCoder95/fastify-split-validator)
package/fastify.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const VERSION = '4.5.0'
3
+ const VERSION = '4.5.3'
4
4
 
5
5
  const Avvio = require('avvio')
6
6
  const http = require('http')
package/lib/context.js CHANGED
@@ -44,6 +44,7 @@ function Context ({
44
44
  this.onTimeout = null
45
45
  this.preHandler = null
46
46
  this.onResponse = null
47
+ this.preSerialization = null
47
48
  this.config = config
48
49
  this.errorHandler = errorHandler || server[kErrorHandler]
49
50
  this._middie = null
package/lib/decorate.js CHANGED
@@ -19,7 +19,7 @@ const {
19
19
  const warning = require('./warnings')
20
20
 
21
21
  function decorate (instance, name, fn, dependencies) {
22
- if (instance.hasOwnProperty(name)) {
22
+ if (Object.prototype.hasOwnProperty.call(instance, name)) {
23
23
  throw new FST_ERR_DEC_ALREADY_PRESENT(name)
24
24
  }
25
25
 
@@ -37,7 +37,7 @@ function decorate (instance, name, fn, dependencies) {
37
37
 
38
38
  function decorateConstructor (konstructor, name, fn, dependencies) {
39
39
  const instance = konstructor.prototype
40
- if (instance.hasOwnProperty(name) || hasKey(konstructor, name)) {
40
+ if (Object.prototype.hasOwnProperty.call(instance, name) || hasKey(konstructor, name)) {
41
41
  throw new FST_ERR_DEC_ALREADY_PRESENT(name)
42
42
  }
43
43
 
package/lib/fourOhFour.js CHANGED
@@ -10,7 +10,8 @@ const {
10
10
  kCanSetNotFoundHandler,
11
11
  kFourOhFourLevelInstance,
12
12
  kFourOhFourContext,
13
- kHooks
13
+ kHooks,
14
+ kErrorHandler
14
15
  } = require('./symbols.js')
15
16
  const { lifecycleHooks } = require('./hooks')
16
17
  const { buildErrorHandler } = require('./error-handler.js')
@@ -148,6 +149,7 @@ function fourOhFour (options) {
148
149
  .map(h => h.bind(this))
149
150
  context[hook] = toSet.length ? toSet : null
150
151
  }
152
+ context.errorHandler = opts.errorHandler ? buildErrorHandler(this[kErrorHandler], opts.errorHandler) : this[kErrorHandler]
151
153
  })
152
154
 
153
155
  if (this[kFourOhFourContext] !== null && prefix === '/') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify",
3
- "version": "4.5.0",
3
+ "version": "4.5.3",
4
4
  "description": "Fast and low overhead web framework, for Node.js",
5
5
  "main": "fastify.js",
6
6
  "type": "commonjs",
@@ -177,7 +177,7 @@
177
177
  "abstract-logging": "^2.0.1",
178
178
  "avvio": "^8.1.3",
179
179
  "find-my-way": "^7.0.0",
180
- "light-my-request": "^5.0.0",
180
+ "light-my-request": "^5.5.1",
181
181
  "pino": "^8.0.0",
182
182
  "process-warning": "^2.0.0",
183
183
  "proxy-addr": "^2.0.7",
package/test/404s.test.js CHANGED
@@ -1944,3 +1944,68 @@ test('Send 404 when frameworkError calls reply.callNotFound', t => {
1944
1944
 
1945
1945
  t.end()
1946
1946
  })
1947
+
1948
+ test('hooks are applied to not found handlers /1', async ({ equal }) => {
1949
+ const fastify = Fastify()
1950
+
1951
+ // adding await here is fundamental for this test
1952
+ await fastify.register(async function (fastify) {
1953
+ })
1954
+
1955
+ fastify.setErrorHandler(function (_, request, reply) {
1956
+ return reply.code(401).send({ error: 'Unauthorized' })
1957
+ })
1958
+
1959
+ fastify.addHook('preValidation', async function (request, reply) {
1960
+ throw new Error('kaboom')
1961
+ })
1962
+
1963
+ const { statusCode } = await fastify.inject('/')
1964
+ equal(statusCode, 401)
1965
+ })
1966
+
1967
+ test('hooks are applied to not found handlers /2', async ({ equal }) => {
1968
+ const fastify = Fastify()
1969
+
1970
+ async function plugin (fastify) {
1971
+ fastify.setErrorHandler(function (_, request, reply) {
1972
+ return reply.code(401).send({ error: 'Unauthorized' })
1973
+ })
1974
+ }
1975
+
1976
+ plugin[Symbol.for('skip-override')] = true
1977
+
1978
+ fastify.register(plugin)
1979
+
1980
+ fastify.addHook('preValidation', async function (request, reply) {
1981
+ throw new Error('kaboom')
1982
+ })
1983
+
1984
+ const { statusCode } = await fastify.inject('/')
1985
+ equal(statusCode, 401)
1986
+ })
1987
+
1988
+ test('hooks are applied to not found handlers /3', async ({ equal, fail }) => {
1989
+ const fastify = Fastify()
1990
+
1991
+ async function plugin (fastify) {
1992
+ fastify.setNotFoundHandler({ errorHandler }, async () => {
1993
+ fail('this should never be called')
1994
+ })
1995
+
1996
+ function errorHandler (_, request, reply) {
1997
+ return reply.code(401).send({ error: 'Unauthorized' })
1998
+ }
1999
+ }
2000
+
2001
+ plugin[Symbol.for('skip-override')] = true
2002
+
2003
+ fastify.register(plugin)
2004
+
2005
+ fastify.addHook('preValidation', async function (request, reply) {
2006
+ throw new Error('kaboom')
2007
+ })
2008
+
2009
+ const { statusCode } = await fastify.inject('/')
2010
+ equal(statusCode, 401)
2011
+ })
@@ -423,6 +423,28 @@ test('promise was fulfilled with undefined', t => {
423
423
  })
424
424
  })
425
425
 
426
+ test('promise was fulfilled with undefined using inject', async (t) => {
427
+ const stream = split(JSON.parse)
428
+ const fastify = Fastify({
429
+ logger: {
430
+ stream,
431
+ level: 'error'
432
+ }
433
+ })
434
+
435
+ fastify.get('/', async (req, reply) => {
436
+ })
437
+
438
+ stream.once('data', line => {
439
+ t.fail('should not log an error')
440
+ })
441
+
442
+ const res = await fastify.inject('/')
443
+
444
+ t.equal(res.body, '')
445
+ t.equal(res.statusCode, 200)
446
+ })
447
+
426
448
  test('error is not logged because promise was fulfilled with undefined but response was sent before promise resolution', t => {
427
449
  t.plan(4)
428
450
 
@@ -642,7 +642,7 @@ test('should register empty values', t => {
642
642
 
643
643
  fastify.register((instance, opts, done) => {
644
644
  instance.decorate('test', null)
645
- t.ok(instance.hasOwnProperty('test'))
645
+ t.ok(Object.prototype.hasOwnProperty.call(instance, 'test'))
646
646
  done()
647
647
  })
648
648
 
@@ -710,3 +710,46 @@ test('preSerializationEnd should handle errors if the serialize method throws',
710
710
 
711
711
  t.end()
712
712
  })
713
+
714
+ t.test('nested hooks to do not crash on 404', t => {
715
+ t.plan(2)
716
+ const fastify = Fastify()
717
+
718
+ fastify.get('/hello', (req, reply) => {
719
+ reply.send({ hello: 'world' })
720
+ })
721
+
722
+ fastify.register(async function (fastify) {
723
+ fastify.get('/something', (req, reply) => {
724
+ reply.callNotFound()
725
+ })
726
+
727
+ fastify.setNotFoundHandler(async (request, reply) => {
728
+ reply.statusCode = 404
729
+ return { status: 'nested-not-found' }
730
+ })
731
+
732
+ fastify.setErrorHandler(async (error, request, reply) => {
733
+ reply.statusCode = 500
734
+ return { status: 'nested-error', error }
735
+ })
736
+ }, { prefix: '/nested' })
737
+
738
+ fastify.setNotFoundHandler(async (request, reply) => {
739
+ reply.statusCode = 404
740
+ return { status: 'not-found' }
741
+ })
742
+
743
+ fastify.setErrorHandler(async (error, request, reply) => {
744
+ reply.statusCode = 500
745
+ return { status: 'error', error }
746
+ })
747
+
748
+ fastify.inject({
749
+ method: 'GET',
750
+ url: '/nested/something'
751
+ }, (err, res) => {
752
+ t.error(err)
753
+ t.equal(res.statusCode, 404)
754
+ })
755
+ })
@@ -138,7 +138,7 @@ test('decorate should recognize getter/setter objects', t => {
138
138
  this._a = val
139
139
  }
140
140
  })
141
- t.equal(one.hasOwnProperty('foo'), true)
141
+ t.equal(Object.prototype.hasOwnProperty.call(one, 'foo'), true)
142
142
  t.equal(one.foo, undefined)
143
143
  one.foo = 'a'
144
144
  t.equal(one.foo, 'a')
@@ -154,6 +154,6 @@ test('decorate should recognize getter/setter objects', t => {
154
154
  decorator.add.call(two, 'foo', {
155
155
  getter: () => 'a getter'
156
156
  })
157
- t.equal(two.hasOwnProperty('foo'), true)
157
+ t.equal(Object.prototype.hasOwnProperty.call(two, 'foo'), true)
158
158
  t.equal(two.foo, 'a getter')
159
159
  })
@@ -174,7 +174,7 @@ export interface FastifyInstance<
174
174
  route<
175
175
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
176
176
  ContextConfig = ContextConfigDefault,
177
- SchemaCompiler = FastifySchema,
177
+ SchemaCompiler extends FastifySchema = FastifySchema,
178
178
  >(opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
179
179
 
180
180
  get: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
package/types/route.d.ts CHANGED
@@ -24,7 +24,7 @@ export interface RouteShorthandOptions<
24
24
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
25
25
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
26
26
  ContextConfig = ContextConfigDefault,
27
- SchemaCompiler = FastifySchema,
27
+ SchemaCompiler extends FastifySchema = FastifySchema,
28
28
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
29
29
  Logger extends FastifyLoggerInstance = FastifyLoggerInstance
30
30
  > {
@@ -84,7 +84,7 @@ export interface RouteShorthandOptionsWithHandler<
84
84
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
85
85
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
86
86
  ContextConfig = ContextConfigDefault,
87
- SchemaCompiler = FastifySchema,
87
+ SchemaCompiler extends FastifySchema = FastifySchema,
88
88
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
89
89
  Logger extends FastifyLoggerInstance = FastifyLoggerInstance
90
90
  > extends RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> {
@@ -100,16 +100,16 @@ export interface RouteShorthandMethod<
100
100
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
101
101
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
102
102
  > {
103
- <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler = FastifySchema, Logger extends FastifyLoggerInstance = FastifyLoggerInstance>(
103
+ <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler extends FastifySchema = FastifySchema, Logger extends FastifyLoggerInstance = FastifyLoggerInstance>(
104
104
  path: string,
105
105
  opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>,
106
106
  handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
107
107
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
108
- <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler = FastifySchema, Logger extends FastifyLoggerInstance = FastifyLoggerInstance>(
108
+ <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler extends FastifySchema = FastifySchema, Logger extends FastifyLoggerInstance = FastifyLoggerInstance>(
109
109
  path: string,
110
110
  handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
111
111
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
112
- <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler = FastifySchema, Logger extends FastifyLoggerInstance = FastifyLoggerInstance>(
112
+ <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler extends FastifySchema = FastifySchema, Logger extends FastifyLoggerInstance = FastifyLoggerInstance>(
113
113
  path: string,
114
114
  opts: RouteShorthandOptionsWithHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
115
115
  ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
@@ -124,7 +124,7 @@ export interface RouteOptions<
124
124
  RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
125
125
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
126
126
  ContextConfig = ContextConfigDefault,
127
- SchemaCompiler = FastifySchema,
127
+ SchemaCompiler extends FastifySchema = FastifySchema,
128
128
  TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
129
129
  Logger extends FastifyLoggerInstance = FastifyLoggerInstance
130
130
  > extends RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> {