fastify 4.15.0 → 4.16.1

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.
Files changed (42) hide show
  1. package/README.md +3 -3
  2. package/docs/Guides/Database.md +7 -8
  3. package/docs/Guides/Ecosystem.md +16 -7
  4. package/docs/Guides/Getting-Started.md +1 -1
  5. package/docs/Guides/Migration-Guide-V4.md +21 -0
  6. package/docs/Guides/Plugins-Guide.md +1 -1
  7. package/docs/Guides/Prototype-Poisoning.md +31 -39
  8. package/docs/Guides/Recommendations.md +1 -1
  9. package/docs/Guides/Write-Type-Provider.md +3 -3
  10. package/docs/Reference/Hooks.md +42 -9
  11. package/docs/Reference/Reply.md +2 -2
  12. package/docs/Reference/Routes.md +13 -2
  13. package/docs/Reference/Server.md +19 -3
  14. package/docs/Reference/Type-Providers.md +1 -1
  15. package/docs/Reference/TypeScript.md +3 -3
  16. package/docs/index.md +2 -2
  17. package/examples/benchmark/parser.js +47 -0
  18. package/fastify.js +26 -23
  19. package/lib/error-serializer.js +9 -162
  20. package/lib/hooks.js +3 -0
  21. package/lib/server.js +9 -4
  22. package/lib/validation.js +10 -8
  23. package/lib/warnings.js +2 -0
  24. package/package.json +8 -7
  25. package/test/close.test.js +91 -0
  26. package/test/route-hooks.test.js +29 -0
  27. package/test/route.test.js +1 -1
  28. package/test/schema-feature.test.js +128 -35
  29. package/test/serial/logger.0.test.js +861 -0
  30. package/test/serial/logger.1.test.js +862 -0
  31. package/test/serial/tap-parallel-not-ok +0 -0
  32. package/test/server.test.js +10 -0
  33. package/test/types/hooks.test-d.ts +66 -11
  34. package/test/types/import.js +1 -1
  35. package/test/types/instance.test-d.ts +2 -0
  36. package/test/types/route.test-d.ts +106 -5
  37. package/test/types/type-provider.test-d.ts +77 -10
  38. package/types/hooks.d.ts +28 -0
  39. package/types/instance.d.ts +20 -1
  40. package/types/logger.d.ts +1 -1
  41. package/types/route.d.ts +41 -11
  42. package/test/logger.test.js +0 -1721
@@ -6,6 +6,7 @@ const fp = require('fastify-plugin')
6
6
  const deepClone = require('rfdc')({ circles: true, proto: false })
7
7
  const Ajv = require('ajv')
8
8
  const { kSchemaController } = require('../lib/symbols.js')
9
+ const warning = require('../lib/warnings')
9
10
 
10
11
  const echoParams = (req, reply) => { reply.send(req.params) }
11
12
  const echoBody = (req, reply) => { reply.send(req.body) }
@@ -253,88 +254,180 @@ test('Should not change the input schemas', t => {
253
254
  })
254
255
  })
255
256
 
256
- test('Should throw if the schema body is undefined', t => {
257
- t.plan(2)
257
+ test('Should emit warning if the schema headers is undefined', t => {
258
+ t.plan(4)
258
259
  const fastify = Fastify()
259
260
 
260
- fastify.get('/:id', {
261
+ process.on('warning', onWarning)
262
+ function onWarning (warning) {
263
+ t.equal(warning.name, 'FastifyWarning')
264
+ t.equal(warning.code, 'FSTWRN001')
265
+ }
266
+
267
+ t.teardown(() => {
268
+ process.removeListener('warning', onWarning)
269
+ warning.emitted.set('FSTWRN001', false)
270
+ })
271
+
272
+ fastify.post('/:id', {
261
273
  handler: echoParams,
262
274
  schema: {
263
- body: undefined
275
+ headers: undefined
264
276
  }
265
277
  })
266
278
 
267
- fastify.ready(err => {
268
- t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
269
- t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error body schema is undefined')
279
+ fastify.inject({
280
+ method: 'POST',
281
+ url: '/123'
282
+ }, (error, res) => {
283
+ t.error(error)
284
+ t.equal(res.statusCode, 200)
270
285
  })
271
286
  })
272
287
 
273
- test('Should throw if the schema headers is undefined', t => {
274
- t.plan(2)
288
+ test('Should emit warning if the schema body is undefined', t => {
289
+ t.plan(4)
275
290
  const fastify = Fastify()
276
291
 
277
- fastify.get('/:id', {
292
+ process.on('warning', onWarning)
293
+ function onWarning (warning) {
294
+ t.equal(warning.name, 'FastifyWarning')
295
+ t.equal(warning.code, 'FSTWRN001')
296
+ }
297
+
298
+ t.teardown(() => {
299
+ process.removeListener('warning', onWarning)
300
+ warning.emitted.set('FSTWRN001', false)
301
+ })
302
+
303
+ fastify.post('/:id', {
278
304
  handler: echoParams,
279
305
  schema: {
280
- headers: undefined
306
+ body: undefined
281
307
  }
282
308
  })
283
309
 
284
- fastify.ready(err => {
285
- t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
286
- t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error headers schema is undefined')
310
+ fastify.inject({
311
+ method: 'POST',
312
+ url: '/123'
313
+ }, (error, res) => {
314
+ t.error(error)
315
+ t.equal(res.statusCode, 200)
287
316
  })
288
317
  })
289
318
 
290
- test('Should throw if the schema params is undefined', t => {
291
- t.plan(2)
319
+ test('Should emit warning if the schema query is undefined', t => {
320
+ t.plan(4)
292
321
  const fastify = Fastify()
293
322
 
294
- fastify.get('/:id', {
323
+ process.on('warning', onWarning)
324
+ function onWarning (warning) {
325
+ t.equal(warning.name, 'FastifyWarning')
326
+ t.equal(warning.code, 'FSTWRN001')
327
+ }
328
+
329
+ t.teardown(() => {
330
+ process.removeListener('warning', onWarning)
331
+ warning.emitted.set('FSTWRN001', false)
332
+ })
333
+
334
+ fastify.post('/:id', {
295
335
  handler: echoParams,
296
336
  schema: {
297
- params: undefined
337
+ querystring: undefined
298
338
  }
299
339
  })
300
340
 
301
- fastify.ready(err => {
302
- t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
303
- t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error params schema is undefined')
341
+ fastify.inject({
342
+ method: 'POST',
343
+ url: '/123'
344
+ }, (error, res) => {
345
+ t.error(error)
346
+ t.equal(res.statusCode, 200)
304
347
  })
305
348
  })
306
349
 
307
- test('Should throw if the schema query is undefined', t => {
308
- t.plan(2)
350
+ test('Should emit warning if the schema params is undefined', t => {
351
+ t.plan(4)
309
352
  const fastify = Fastify()
310
353
 
311
- fastify.get('/:id', {
354
+ process.on('warning', onWarning)
355
+ function onWarning (warning) {
356
+ t.equal(warning.name, 'FastifyWarning')
357
+ t.equal(warning.code, 'FSTWRN001')
358
+ }
359
+
360
+ t.teardown(() => {
361
+ process.removeListener('warning', onWarning)
362
+ warning.emitted.set('FSTWRN001', false)
363
+ })
364
+
365
+ fastify.post('/:id', {
312
366
  handler: echoParams,
313
367
  schema: {
314
- querystring: undefined
368
+ params: undefined
315
369
  }
316
370
  })
317
371
 
318
- fastify.ready(err => {
319
- t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
320
- t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error querystring schema is undefined')
372
+ fastify.inject({
373
+ method: 'POST',
374
+ url: '/123'
375
+ }, (error, res) => {
376
+ t.error(error)
377
+ t.equal(res.statusCode, 200)
321
378
  })
322
379
  })
323
380
 
324
- test('Should throw if the schema query is undefined', t => {
325
- t.plan(2)
381
+ test('Should emit a warning for every route with undefined schema', t => {
382
+ t.plan(16)
326
383
  const fastify = Fastify()
327
384
 
328
- fastify.get('/:id', {
385
+ let runs = 0
386
+ const expectedWarningEmitted = [0, 1, 2, 3]
387
+ // It emits 4 warnings:
388
+ // - 2 - GET and HEAD for /undefinedParams/:id
389
+ // - 2 - GET and HEAD for /undefinedBody/:id
390
+ // => 3 x 4 assertions = 12 assertions
391
+ function onWarning (warning) {
392
+ t.equal(warning.name, 'FastifyWarning')
393
+ t.equal(warning.code, 'FSTWRN001')
394
+ t.equal(runs++, expectedWarningEmitted.shift())
395
+ }
396
+
397
+ process.on('warning', onWarning)
398
+ t.teardown(() => {
399
+ process.removeListener('warning', onWarning)
400
+ warning.emitted.set('FSTWRN001', false)
401
+ })
402
+
403
+ fastify.get('/undefinedParams/:id', {
329
404
  handler: echoParams,
330
405
  schema: {
331
- querystring: undefined
406
+ params: undefined
332
407
  }
333
408
  })
334
409
 
335
- fastify.ready(err => {
336
- t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
337
- t.equal(err.message, 'Failed building the validation schema for GET: /:id, due to error querystring schema is undefined')
410
+ fastify.get('/undefinedBody/:id', {
411
+ handler: echoParams,
412
+ schema: {
413
+ body: undefined
414
+ }
415
+ })
416
+
417
+ fastify.inject({
418
+ method: 'GET',
419
+ url: '/undefinedParams/123'
420
+ }, (error, res) => {
421
+ t.error(error)
422
+ t.equal(res.statusCode, 200)
423
+ })
424
+
425
+ fastify.inject({
426
+ method: 'GET',
427
+ url: '/undefinedBody/123'
428
+ }, (error, res) => {
429
+ t.error(error)
430
+ t.equal(res.statusCode, 200)
338
431
  })
339
432
  })
340
433