fastify 3.17.0 → 3.19.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.
@@ -15,7 +15,8 @@ type TestPayloadType = {
15
15
 
16
16
  // Synchronous Tests
17
17
 
18
- server.addHook('onRequest', (request, reply, done) => {
18
+ server.addHook('onRequest', function (request, reply, done) {
19
+ expectType<FastifyInstance>(this)
19
20
  expectType<FastifyRequest>(request)
20
21
  expectType<FastifyReply>(reply)
21
22
  expectAssignable<(err?: FastifyError) => void>(done)
@@ -23,7 +24,8 @@ server.addHook('onRequest', (request, reply, done) => {
23
24
  expectType<void>(done(new Error()))
24
25
  })
25
26
 
26
- server.addHook('preParsing', (request, reply, payload, done) => {
27
+ server.addHook('preParsing', function (request, reply, payload, done) {
28
+ expectType<FastifyInstance>(this)
27
29
  expectType<FastifyRequest>(request)
28
30
  expectType<FastifyReply>(reply)
29
31
  expectType<RequestPayload>(payload)
@@ -32,7 +34,8 @@ server.addHook('preParsing', (request, reply, payload, done) => {
32
34
  expectType<void>(done(new Error()))
33
35
  })
34
36
 
35
- server.addHook('preValidation', (request, reply, done) => {
37
+ server.addHook('preValidation', function (request, reply, done) {
38
+ expectType<FastifyInstance>(this)
36
39
  expectType<FastifyRequest>(request)
37
40
  expectType<FastifyReply>(reply)
38
41
  expectAssignable<(err?: FastifyError) => void>(done)
@@ -40,7 +43,8 @@ server.addHook('preValidation', (request, reply, done) => {
40
43
  expectType<void>(done(new Error()))
41
44
  })
42
45
 
43
- server.addHook('preHandler', (request, reply, done) => {
46
+ server.addHook('preHandler', function (request, reply, done) {
47
+ expectType<FastifyInstance>(this)
44
48
  expectType<FastifyRequest>(request)
45
49
  expectType<FastifyReply>(reply)
46
50
  expectAssignable<(err?: FastifyError) => void>(done)
@@ -49,6 +53,7 @@ server.addHook('preHandler', (request, reply, done) => {
49
53
  })
50
54
 
51
55
  server.addHook<TestPayloadType>('preSerialization', function (request, reply, payload, done) {
56
+ expectType<FastifyInstance>(this)
52
57
  expectType<FastifyRequest>(request)
53
58
  expectType<FastifyReply>(reply)
54
59
  expectType<TestPayloadType>(payload) // we expect this to be unknown when not specified like in the previous test
@@ -58,7 +63,8 @@ server.addHook<TestPayloadType>('preSerialization', function (request, reply, pa
58
63
  expectError<void>(done(new Error(), 'foobar'))
59
64
  })
60
65
 
61
- server.addHook<TestPayloadType>('onSend', (request, reply, payload, done) => {
66
+ server.addHook<TestPayloadType>('onSend', function (request, reply, payload, done) {
67
+ expectType<FastifyInstance>(this)
62
68
  expectType<FastifyRequest>(request)
63
69
  expectType<FastifyReply>(reply)
64
70
  expectType<TestPayloadType>(payload)
@@ -68,7 +74,8 @@ server.addHook<TestPayloadType>('onSend', (request, reply, payload, done) => {
68
74
  expectError<void>(done(new Error(), 'foobar'))
69
75
  })
70
76
 
71
- server.addHook('onResponse', (request, reply, done) => {
77
+ server.addHook('onResponse', function (request, reply, done) {
78
+ expectType<FastifyInstance>(this)
72
79
  expectType<FastifyRequest>(request)
73
80
  expectType<FastifyReply>(reply)
74
81
  expectAssignable<(err?: FastifyError) => void>(done)
@@ -76,7 +83,8 @@ server.addHook('onResponse', (request, reply, done) => {
76
83
  expectType<void>(done(new Error()))
77
84
  })
78
85
 
79
- server.addHook('onTimeout', (request, reply, done) => {
86
+ server.addHook('onTimeout', function (request, reply, done) {
87
+ expectType<FastifyInstance>(this)
80
88
  expectType<FastifyRequest>(request)
81
89
  expectType<FastifyReply>(reply)
82
90
  expectAssignable<(err?: FastifyError) => void>(done)
@@ -84,7 +92,8 @@ server.addHook('onTimeout', (request, reply, done) => {
84
92
  expectType<void>(done(new Error()))
85
93
  })
86
94
 
87
- server.addHook('onError', (request, reply, error, done) => {
95
+ server.addHook('onError', function (request, reply, error, done) {
96
+ expectType<FastifyInstance>(this)
88
97
  expectType<FastifyRequest>(request)
89
98
  expectType<FastifyReply>(reply)
90
99
  expectType<FastifyError>(error)
@@ -92,7 +101,8 @@ server.addHook('onError', (request, reply, error, done) => {
92
101
  expectType<void>(done())
93
102
  })
94
103
 
95
- server.addHook('onRoute', (opts) => {
104
+ server.addHook('onRoute', function (opts) {
105
+ expectType<FastifyInstance>(this)
96
106
  expectType<RouteOptions & { routePath: string; path: string; prefix: string}>(opts)
97
107
  })
98
108
 
@@ -104,6 +114,7 @@ server.addHook('onRegister', (instance, done) => {
104
114
  })
105
115
 
106
116
  server.addHook('onReady', function (done) {
117
+ expectType<FastifyInstance>(this)
107
118
  expectAssignable<(err?: FastifyError) => void>(done)
108
119
  expectAssignable<(err?: NodeJS.ErrnoException) => void>(done)
109
120
  expectType<void>(done(new Error()))
@@ -118,50 +129,59 @@ server.addHook('onClose', (instance, done) => {
118
129
 
119
130
  // Asynchronous
120
131
 
121
- server.addHook('onRequest', async (request, reply) => {
132
+ server.addHook('onRequest', async function (request, reply) {
133
+ expectType<FastifyInstance>(this)
122
134
  expectType<FastifyRequest>(request)
123
135
  expectType<FastifyReply>(reply)
124
136
  })
125
137
 
126
- server.addHook('preParsing', async (request, reply, payload) => {
138
+ server.addHook('preParsing', async function (request, reply, payload) {
139
+ expectType<FastifyInstance>(this)
127
140
  expectType<FastifyRequest>(request)
128
141
  expectType<FastifyReply>(reply)
129
142
  expectType<RequestPayload>(payload)
130
143
  })
131
144
 
132
- server.addHook('preValidation', async (request, reply) => {
145
+ server.addHook('preValidation', async function (request, reply) {
146
+ expectType<FastifyInstance>(this)
133
147
  expectType<FastifyRequest>(request)
134
148
  expectType<FastifyReply>(reply)
135
149
  })
136
150
 
137
- server.addHook('preHandler', (request, reply) => {
151
+ server.addHook('preHandler', async function (request, reply) {
152
+ expectType<FastifyInstance>(this)
138
153
  expectType<FastifyRequest>(request)
139
154
  expectType<FastifyReply>(reply)
140
155
  })
141
156
 
142
157
  server.addHook<TestPayloadType>('preSerialization', async function (request, reply, payload) {
158
+ expectType<FastifyInstance>(this)
143
159
  expectType<FastifyRequest>(request)
144
160
  expectType<FastifyReply>(reply)
145
161
  expectType<TestPayloadType>(payload) // we expect this to be unknown when not specified like in the previous test
146
162
  })
147
163
 
148
- server.addHook<TestPayloadType>('onSend', async (request, reply, payload) => {
164
+ server.addHook<TestPayloadType>('onSend', async function (request, reply, payload) {
165
+ expectType<FastifyInstance>(this)
149
166
  expectType<FastifyRequest>(request)
150
167
  expectType<FastifyReply>(reply)
151
168
  expectType<TestPayloadType>(payload)
152
169
  })
153
170
 
154
- server.addHook('onResponse', async (request, reply) => {
171
+ server.addHook('onResponse', async function (request, reply) {
172
+ expectType<FastifyInstance>(this)
155
173
  expectType<FastifyRequest>(request)
156
174
  expectType<FastifyReply>(reply)
157
175
  })
158
176
 
159
- server.addHook('onTimeout', async (request, reply) => {
177
+ server.addHook('onTimeout', async function (request, reply) {
178
+ expectType<FastifyInstance>(this)
160
179
  expectType<FastifyRequest>(request)
161
180
  expectType<FastifyReply>(reply)
162
181
  })
163
182
 
164
- server.addHook('onError', async (request, reply, error) => {
183
+ server.addHook('onError', async function (request, reply, error) {
184
+ expectType<FastifyInstance>(this)
165
185
  expectType<FastifyRequest>(request)
166
186
  expectType<FastifyReply>(reply)
167
187
  expectType<FastifyError>(error)
@@ -171,6 +191,10 @@ server.addHook('onRegister', async (instance) => {
171
191
  expectType<FastifyInstance>(instance)
172
192
  })
173
193
 
194
+ server.addHook('onReady', async function () {
195
+ expectType<FastifyInstance>(this)
196
+ })
197
+
174
198
  server.addHook('onClose', async (instance) => {
175
199
  expectType<FastifyInstance>(instance)
176
200
  })
@@ -2,6 +2,7 @@ import { expectType, expectError } from 'tsd'
2
2
  import fastify, { FastifyLogFn, LogLevel, FastifyLoggerInstance, FastifyError, FastifyRequest, FastifyReply } from '../../fastify'
3
3
  import { Server, IncomingMessage, ServerResponse } from 'http'
4
4
  import * as pino from 'pino'
5
+ import * as fs from 'fs'
5
6
 
6
7
  expectType<FastifyLoggerInstance>(fastify().log)
7
8
 
@@ -154,3 +155,9 @@ const serverAutoInferredSerializerObjectOption = fastify({
154
155
  })
155
156
 
156
157
  expectType<FastifyLoggerInstance>(serverAutoInferredSerializerObjectOption.log)
158
+
159
+ const passStreamAsOption = fastify({
160
+ logger: {
161
+ stream: fs.createWriteStream('/tmp/stream.out')
162
+ }
163
+ })
@@ -632,7 +632,7 @@ test('Should trigger a warning when a versioned route is registered via version
632
632
  t.plan(4)
633
633
 
634
634
  function onWarning (code) {
635
- t.equal(code, 'FSTDEP006')
635
+ t.equal(code, 'FSTDEP008')
636
636
  }
637
637
  const warning = {
638
638
  emit: onWarning
package/types/hooks.d.ts CHANGED
@@ -27,6 +27,7 @@ export interface onRequestHookHandler<
27
27
  ContextConfig = ContextConfigDefault
28
28
  > {
29
29
  (
30
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
30
31
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
31
32
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
32
33
  done: HookHandlerDoneFunction
@@ -41,6 +42,7 @@ export interface onRequestAsyncHookHandler<
41
42
  ContextConfig = ContextConfigDefault
42
43
  > {
43
44
  (
45
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
44
46
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
45
47
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
46
48
  ): Promise<unknown>;
@@ -58,6 +60,7 @@ export interface preParsingHookHandler<
58
60
  ContextConfig = ContextConfigDefault
59
61
  > {
60
62
  (
63
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
61
64
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
62
65
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
63
66
  payload: RequestPayload,
@@ -73,6 +76,7 @@ export interface preParsingAsyncHookHandler<
73
76
  ContextConfig = ContextConfigDefault
74
77
  > {
75
78
  (
79
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
76
80
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
77
81
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
78
82
  payload: RequestPayload,
@@ -90,6 +94,7 @@ export interface preValidationHookHandler<
90
94
  ContextConfig = ContextConfigDefault
91
95
  > {
92
96
  (
97
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
93
98
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
94
99
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
95
100
  done: HookHandlerDoneFunction
@@ -104,6 +109,7 @@ export interface preValidationAsyncHookHandler<
104
109
  ContextConfig = ContextConfigDefault
105
110
  > {
106
111
  (
112
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
107
113
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
108
114
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
109
115
  ): Promise<unknown>;
@@ -120,6 +126,7 @@ export interface preHandlerHookHandler<
120
126
  ContextConfig = ContextConfigDefault
121
127
  > {
122
128
  (
129
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
123
130
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
124
131
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
125
132
  done: HookHandlerDoneFunction
@@ -134,6 +141,7 @@ export interface preHandlerAsyncHookHandler<
134
141
  ContextConfig = ContextConfigDefault
135
142
  > {
136
143
  (
144
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
137
145
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
138
146
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
139
147
  ): Promise<unknown>;
@@ -159,6 +167,7 @@ export interface preSerializationHookHandler<
159
167
  ContextConfig = ContextConfigDefault
160
168
  > {
161
169
  (
170
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
162
171
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
163
172
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
164
173
  payload: PreSerializationPayload,
@@ -175,6 +184,7 @@ export interface preSerializationAsyncHookHandler<
175
184
  ContextConfig = ContextConfigDefault
176
185
  > {
177
186
  (
187
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
178
188
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
179
189
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
180
190
  payload: PreSerializationPayload
@@ -194,6 +204,7 @@ export interface onSendHookHandler<
194
204
  ContextConfig = ContextConfigDefault
195
205
  > {
196
206
  (
207
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
197
208
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
198
209
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
199
210
  payload: OnSendPayload,
@@ -210,6 +221,7 @@ export interface onSendAsyncHookHandler<
210
221
  ContextConfig = ContextConfigDefault
211
222
  > {
212
223
  (
224
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
213
225
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
214
226
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
215
227
  payload: OnSendPayload,
@@ -228,6 +240,7 @@ export interface onResponseHookHandler<
228
240
  ContextConfig = ContextConfigDefault
229
241
  > {
230
242
  (
243
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
231
244
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
232
245
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
233
246
  done: HookHandlerDoneFunction
@@ -242,6 +255,7 @@ export interface onResponseAsyncHookHandler<
242
255
  ContextConfig = ContextConfigDefault
243
256
  > {
244
257
  (
258
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
245
259
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
246
260
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
247
261
  ): Promise<unknown>;
@@ -259,6 +273,7 @@ export interface onTimeoutHookHandler<
259
273
  ContextConfig = ContextConfigDefault
260
274
  > {
261
275
  (
276
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
262
277
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
263
278
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
264
279
  done: HookHandlerDoneFunction
@@ -273,6 +288,7 @@ export interface onTimeoutAsyncHookHandler<
273
288
  ContextConfig = ContextConfigDefault
274
289
  > {
275
290
  (
291
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
276
292
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
277
293
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
278
294
  ): Promise<unknown>;
@@ -293,6 +309,7 @@ export interface onErrorHookHandler<
293
309
  TError extends Error = FastifyError
294
310
  > {
295
311
  (
312
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
296
313
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
297
314
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
298
315
  error: TError,
@@ -309,6 +326,7 @@ export interface onErrorAsyncHookHandler<
309
326
  TError extends Error = FastifyError
310
327
  > {
311
328
  (
329
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
312
330
  request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
313
331
  reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
314
332
  error: TError
@@ -328,6 +346,7 @@ export interface onRouteHookHandler<
328
346
  ContextConfig = ContextConfigDefault
329
347
  > {
330
348
  (
349
+ this: FastifyInstance<RawServer, RawRequest, RawReply>,
331
350
  opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> & { routePath: string; path: string; prefix: string }
332
351
  ): Promise<unknown> | void;
333
352
  }
@@ -354,12 +373,15 @@ export interface onRegisterHookHandler<
354
373
  */
355
374
  export interface onReadyHookHandler {
356
375
  (
376
+ this: FastifyInstance,
357
377
  done: HookHandlerDoneFunction
358
378
  ): void;
359
379
  }
360
380
 
361
381
  export interface onReadyAsyncHookHandler {
362
- (): Promise<unknown>;
382
+ (
383
+ this: FastifyInstance,
384
+ ): Promise<unknown>;
363
385
  }
364
386
  /**
365
387
  * Triggered when fastify.close() is invoked to stop the server. It is useful when plugins need a "shutdown" event, for example to close an open connection to a database.
package/types/logger.d.ts CHANGED
@@ -109,6 +109,10 @@ export interface PrettyOptions {
109
109
  suppressFlushSyncWarning?: boolean;
110
110
  }
111
111
 
112
+ export interface FastifyLoggerStreamDestination {
113
+ write(msg: string): void;
114
+ }
115
+
112
116
  /**
113
117
  * Fastify Custom Logger options. To enable configuration of all Pino options,
114
118
  * refer to this example:
@@ -143,4 +147,5 @@ export interface FastifyLoggerOptions<
143
147
  level?: string;
144
148
  genReqId?: (req: RawRequest) => string;
145
149
  prettyPrint?: boolean | PrettyOptions;
150
+ stream?: FastifyLoggerStreamDestination;
146
151
  }
@@ -1,43 +0,0 @@
1
- 'use strict'
2
-
3
- const t = require('tap')
4
- const test = t.test
5
- const proxyquire = require('proxyquire')
6
-
7
- test('should output an undefined version in case of package.json not available', t => {
8
- const Fastify = proxyquire('../..', { fs: { accessSync: () => { throw Error('error') } } })
9
- t.plan(1)
10
- const srv = Fastify()
11
- t.equal(srv.version, undefined)
12
- })
13
-
14
- test('should output an undefined version in case of package.json is not the fastify one', t => {
15
- const Fastify = proxyquire('../..', { fs: { accessSync: () => { }, readFileSync: () => JSON.stringify({ name: 'foo', version: '6.6.6' }) } })
16
- t.plan(1)
17
- const srv = Fastify()
18
- t.equal(srv.version, undefined)
19
- })
20
-
21
- test('should skip the version check if the version is undefined', t => {
22
- const Fastify = proxyquire('../..', { fs: { accessSync: () => { }, readFileSync: () => JSON.stringify({ name: 'foo', version: '6.6.6' }) } })
23
- t.plan(3)
24
- const srv = Fastify()
25
- t.equal(srv.version, undefined)
26
-
27
- plugin[Symbol.for('skip-override')] = false
28
- plugin[Symbol.for('plugin-meta')] = {
29
- name: 'plugin',
30
- fastify: '>=99.0.0'
31
- }
32
-
33
- srv.register(plugin)
34
-
35
- srv.ready((err) => {
36
- t.error(err)
37
- t.pass('everything right')
38
- })
39
-
40
- function plugin (instance, opts, done) {
41
- done()
42
- }
43
- })