fastify 6.0.0-alpha.4 → 6.0.0-alpha.5

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.
@@ -8,7 +8,7 @@ const Schema = require('fluent-json-schema')
8
8
  const { waitForCb } = require('./helper')
9
9
  const { kRequestContentType } = require('../lib/symbols')
10
10
  const { spyWarning } = require('process-warning')
11
- const { FSTSEC002 } = require('../lib/warnings')
11
+ const { FSTSEC002, FSTSEC003 } = require('../lib/warnings')
12
12
 
13
13
  const customSchemaCompilers = {
14
14
  body: new AJV({
@@ -1921,6 +1921,52 @@ test('inline and local $ref header schemas do not emit FSTSEC002', async t => {
1921
1921
  t.assert.strictEqual(spyData.callCount(), 0)
1922
1922
  })
1923
1923
 
1924
+ test('header schema with a $ref inside propertyNames emits FSTSEC003 (case-normalization does not reach it)', async t => {
1925
+ const spyData = spyWarning(FSTSEC003)
1926
+ t.after(spyData.restore)
1927
+
1928
+ const fastify = Fastify()
1929
+ fastify.post('/', {
1930
+ schema: {
1931
+ headers: {
1932
+ type: 'object',
1933
+ propertyNames: { $ref: '#/definitions/denied' },
1934
+ definitions: {
1935
+ denied: { not: { enum: ['X-Debug-Override'] } }
1936
+ }
1937
+ }
1938
+ }
1939
+ }, async () => ({ ok: true }))
1940
+
1941
+ await fastify.ready()
1942
+
1943
+ t.assert.strictEqual(spyData.callCount(), 1)
1944
+ t.assert.deepStrictEqual(spyData.calls[0].arguments, ['POST', '/', '#/definitions/denied'])
1945
+ })
1946
+
1947
+ test('header schema detects a $ref inside propertyNames nested in an array', async t => {
1948
+ const spyData = spyWarning(FSTSEC003)
1949
+ t.after(spyData.restore)
1950
+
1951
+ const fastify = Fastify()
1952
+ fastify.post('/', {
1953
+ schema: {
1954
+ headers: {
1955
+ type: 'object',
1956
+ propertyNames: { not: { anyOf: [{ $ref: '#/definitions/denied' }] } },
1957
+ definitions: {
1958
+ denied: { enum: ['X-Debug-Override'] }
1959
+ }
1960
+ }
1961
+ }
1962
+ }, async () => ({ ok: true }))
1963
+
1964
+ await fastify.ready()
1965
+
1966
+ t.assert.strictEqual(spyData.callCount(), 1)
1967
+ t.assert.deepStrictEqual(spyData.calls[0].arguments, ['POST', '/', '#/definitions/denied'])
1968
+ })
1969
+
1924
1970
  test('header schema normalization traverses dependent schema keywords', async t => {
1925
1971
  const fastify = Fastify({
1926
1972
  ajv: { customOptions: { strictSchema: false } }
@@ -1993,3 +2039,207 @@ test('header schema detects an external $ref nested in an array', async t => {
1993
2039
  'http://example.com/nested-admin-headers#'
1994
2040
  ])
1995
2041
  })
2042
+
2043
+ test('header schema propertyNames: not-enum deny-list is lowercased', async t => {
2044
+ const fastify = Fastify()
2045
+
2046
+ // A `propertyNames` subschema validates each header name itself, so strings in
2047
+ // its `enum` are header names. They must be lowercased the same way as
2048
+ // `required` entries, otherwise a deny-list written in the usual header case
2049
+ // never matches the lowercased request object.
2050
+ fastify.get('/', {
2051
+ schema: {
2052
+ headers: {
2053
+ type: 'object',
2054
+ propertyNames: { not: { enum: ['X-Debug-Override'] } }
2055
+ }
2056
+ }
2057
+ }, async () => ({ ok: true }))
2058
+
2059
+ await fastify.ready()
2060
+
2061
+ const blocked = await fastify.inject({
2062
+ method: 'GET',
2063
+ url: '/',
2064
+ headers: { 'X-Debug-Override': '1' }
2065
+ })
2066
+ t.assert.strictEqual(blocked.statusCode, 400)
2067
+ t.assert.strictEqual(blocked.json().code, 'FST_ERR_VALIDATION')
2068
+
2069
+ const ok = await fastify.inject({
2070
+ method: 'GET',
2071
+ url: '/',
2072
+ headers: { 'X-Other': '1' }
2073
+ })
2074
+ t.assert.strictEqual(ok.statusCode, 200)
2075
+ })
2076
+
2077
+ test('header schema propertyNames: non-string enum items are left untouched', async t => {
2078
+ const fastify = Fastify({
2079
+ ajv: { customOptions: { strictSchema: false } }
2080
+ })
2081
+
2082
+ fastify.get('/', {
2083
+ schema: {
2084
+ headers: {
2085
+ type: 'object',
2086
+ propertyNames: { not: { enum: ['X-Debug-Override', 42, null] } }
2087
+ }
2088
+ }
2089
+ }, async () => ({ ok: true }))
2090
+
2091
+ await fastify.ready()
2092
+
2093
+ const blocked = await fastify.inject({
2094
+ method: 'GET',
2095
+ url: '/',
2096
+ headers: { 'X-Debug-Override': '1' }
2097
+ })
2098
+ t.assert.strictEqual(blocked.statusCode, 400)
2099
+
2100
+ const ok = await fastify.inject({
2101
+ method: 'GET',
2102
+ url: '/',
2103
+ headers: { 'X-Other': '1' }
2104
+ })
2105
+ t.assert.strictEqual(ok.statusCode, 200)
2106
+ })
2107
+
2108
+ test('header schema propertyNames: not-const deny-list is lowercased', async t => {
2109
+ const fastify = Fastify()
2110
+
2111
+ fastify.get('/', {
2112
+ schema: {
2113
+ headers: {
2114
+ type: 'object',
2115
+ propertyNames: { not: { const: 'X-Debug-Override' } }
2116
+ }
2117
+ }
2118
+ }, async () => ({ ok: true }))
2119
+
2120
+ await fastify.ready()
2121
+
2122
+ const blocked = await fastify.inject({
2123
+ method: 'GET',
2124
+ url: '/',
2125
+ headers: { 'X-Debug-Override': '1' }
2126
+ })
2127
+ t.assert.strictEqual(blocked.statusCode, 400)
2128
+ t.assert.strictEqual(blocked.json().code, 'FST_ERR_VALIDATION')
2129
+ })
2130
+
2131
+ test('header schema propertyNames: enum/const are lowercased inside combinators', async t => {
2132
+ const fastify = Fastify()
2133
+
2134
+ // The header-name `enum`/`const` can be nested under combinators inside
2135
+ // `propertyNames`; the lowercasing context must propagate through them.
2136
+ fastify.get('/anyOf', {
2137
+ schema: {
2138
+ headers: {
2139
+ type: 'object',
2140
+ propertyNames: { not: { anyOf: [{ enum: ['X-Debug-Override'] }] } }
2141
+ }
2142
+ }
2143
+ }, async () => ({ ok: true }))
2144
+
2145
+ fastify.get('/oneOf', {
2146
+ schema: {
2147
+ headers: {
2148
+ type: 'object',
2149
+ propertyNames: { not: { oneOf: [{ const: 'X-Debug-Override' }] } }
2150
+ }
2151
+ }
2152
+ }, async () => ({ ok: true }))
2153
+
2154
+ await fastify.ready()
2155
+
2156
+ for (const url of ['/anyOf', '/oneOf']) {
2157
+ const blocked = await fastify.inject({
2158
+ method: 'GET',
2159
+ url,
2160
+ headers: { 'X-Debug-Override': '1' }
2161
+ })
2162
+ t.assert.strictEqual(blocked.statusCode, 400, `${url} should block the header`)
2163
+ t.assert.strictEqual(blocked.json().code, 'FST_ERR_VALIDATION')
2164
+ }
2165
+ })
2166
+
2167
+ test('header schema propertyNames: lowercase-equivalent schema behaves identically', async t => {
2168
+ const fastify = Fastify()
2169
+
2170
+ fastify.get('/', {
2171
+ schema: {
2172
+ headers: {
2173
+ type: 'object',
2174
+ propertyNames: { not: { enum: ['x-debug-override'] } }
2175
+ }
2176
+ }
2177
+ }, async () => ({ ok: true }))
2178
+
2179
+ await fastify.ready()
2180
+
2181
+ const blocked = await fastify.inject({
2182
+ method: 'GET',
2183
+ url: '/',
2184
+ headers: { 'X-Debug-Override': '1' }
2185
+ })
2186
+ t.assert.strictEqual(blocked.statusCode, 400)
2187
+ t.assert.strictEqual(blocked.json().code, 'FST_ERR_VALIDATION')
2188
+ })
2189
+
2190
+ test('header schema propertyNames: enum/const values in header value schemas are not lowercased', async t => {
2191
+ const fastify = Fastify()
2192
+
2193
+ // `enum`/`const` inside a property value subschema hold header *values*, not
2194
+ // names, and must be preserved with their original case.
2195
+ fastify.get('/enum', {
2196
+ schema: {
2197
+ headers: {
2198
+ type: 'object',
2199
+ properties: { 'X-Mode': { type: 'string', enum: ['MIXED', 'lowercase'] } }
2200
+ }
2201
+ }
2202
+ }, async () => ({ ok: true }))
2203
+
2204
+ fastify.get('/const', {
2205
+ schema: {
2206
+ headers: {
2207
+ type: 'object',
2208
+ properties: { 'X-Mode': { type: 'string', const: 'MIXED' } }
2209
+ }
2210
+ }
2211
+ }, async () => ({ ok: true }))
2212
+
2213
+ await fastify.ready()
2214
+
2215
+ const enumMixed = await fastify.inject({
2216
+ method: 'GET',
2217
+ url: '/enum',
2218
+ headers: { 'X-Mode': 'MIXED' }
2219
+ })
2220
+ t.assert.strictEqual(enumMixed.statusCode, 200)
2221
+
2222
+ const constMixed = await fastify.inject({
2223
+ method: 'GET',
2224
+ url: '/const',
2225
+ headers: { 'X-Mode': 'MIXED' }
2226
+ })
2227
+ t.assert.strictEqual(constMixed.statusCode, 200)
2228
+ })
2229
+
2230
+ test('header schema propertyNames lowercasing does not mutate the input schema', async t => {
2231
+ const headers = {
2232
+ type: 'object',
2233
+ propertyNames: { not: { enum: ['X-Debug-Override'], const: 'X-Other' } }
2234
+ }
2235
+ const schema = { headers }
2236
+ const schemaBefore = JSON.stringify(schema)
2237
+ const headersBefore = JSON.stringify(headers)
2238
+
2239
+ const fastify = Fastify()
2240
+ fastify.get('/', { schema }, async () => ({ ok: true }))
2241
+ await fastify.ready()
2242
+
2243
+ t.assert.strictEqual(JSON.stringify(schema), schemaBefore)
2244
+ t.assert.strictEqual(JSON.stringify(headers), headersBefore)
2245
+ })
@@ -79,7 +79,6 @@ expect(errorCodes.FST_ERR_ROUTE_BODY_VALIDATION_SCHEMA_NOT_SUPPORTED).type.toBeA
79
79
  expect(errorCodes.FST_ERR_ROUTE_BODY_LIMIT_OPTION_NOT_INT).type.toBeAssignableTo<FastifyErrorConstructor>()
80
80
  expect(errorCodes.FST_ERR_ROUTE_REWRITE_NOT_STR).type.toBeAssignableTo<FastifyErrorConstructor>()
81
81
  expect(errorCodes.FST_ERR_ROUTE_MISSING_CONTENT_TYPE).type.toBeAssignableTo<FastifyErrorConstructor>()
82
- expect(errorCodes.FST_ERR_ROUTE_MISSING_CONTENT).type.toBeAssignableTo<FastifyErrorConstructor>()
83
82
  expect(errorCodes.FST_ERR_REOPENED_CLOSE_SERVER).type.toBeAssignableTo<FastifyErrorConstructor>()
84
83
  expect(errorCodes.FST_ERR_REOPENED_SERVER).type.toBeAssignableTo<FastifyErrorConstructor>()
85
84
  expect(errorCodes.FST_ERR_INSTANCE_ALREADY_STARTED).type.toBeAssignableTo<FastifyErrorConstructor>()
@@ -3,6 +3,7 @@ import * as http from 'node:http'
3
3
  import * as http2 from 'node:http2'
4
4
  import * as https from 'node:https'
5
5
  import { Socket } from 'node:net'
6
+ import pino from 'pino'
6
7
  import { expect } from 'tstyche'
7
8
  import fastify, {
8
9
  ConnectionError,
@@ -180,6 +181,10 @@ const customLogger = {
180
181
  debug: () => { },
181
182
  child: () => customLogger
182
183
  }
184
+ const pinoLogger = pino()
185
+ const pinoLoggerServer: FastifyInstance = fastify({ loggerInstance: pinoLogger })
186
+
187
+ expect(pinoLoggerServer).type.toBeAssignableTo<FastifyInstance>()
183
188
  expect(fastify({ logger: customLogger })).type.toBeAssignableTo<
184
189
  FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse, FastifyBaseLogger>
185
190
  >()
@@ -58,6 +58,15 @@ const asyncPreHandler = async (request: FastifyRequest) => {
58
58
 
59
59
  fastify().get('/', { preHandler: asyncPreHandler }, async () => 'this is an example')
60
60
 
61
+ fastify().route({
62
+ method: 'GET',
63
+ url: '/',
64
+ onRequest: async function () {
65
+ expect(this).type.toBe<FastifyInstance>()
66
+ },
67
+ handler: async () => 'ok'
68
+ })
69
+
61
70
  fastify().get(
62
71
  '/',
63
72
  { config: { foo: 'bar', bar: 100, includeMessage: true } },
@@ -495,6 +504,12 @@ expect(fastify().hasRoute({
495
504
  method: 'GET'
496
505
  })).type.toBe<boolean>()
497
506
 
507
+ fastify().hasRoute({
508
+ url: '/',
509
+ // @ts-expect-error Type 'string[]' is not assignable to type 'HTTPMethods'.
510
+ method: ['GET', 'POST']
511
+ })
512
+
498
513
  expect(fastify().hasRoute({
499
514
  url: '/',
500
515
  method: 'GET',
@@ -539,6 +554,12 @@ expect(
539
554
  })
540
555
  ).type.toBe<Omit<FindMyWayFindResult<RawServerDefault>, 'store'>>()
541
556
 
557
+ fastify().findRoute({
558
+ url: '/',
559
+ // @ts-expect-error Type 'string[]' is not assignable to type 'HTTPMethods'.
560
+ method: ['GET', 'POST']
561
+ })
562
+
542
563
  // we should not expose store
543
564
  expect(fastify().findRoute({
544
565
  url: '/',
package/types/errors.d.ts CHANGED
@@ -83,7 +83,6 @@ export type FastifyErrorCodes = Record<
83
83
  'FST_ERR_ROUTE_HANDLER_TIMEOUT_OPTION_NOT_INT' |
84
84
  'FST_ERR_ROUTE_REWRITE_NOT_STR' |
85
85
  'FST_ERR_ROUTE_MISSING_CONTENT_TYPE' |
86
- 'FST_ERR_ROUTE_MISSING_CONTENT' |
87
86
  'FST_ERR_REOPENED_CLOSE_SERVER' |
88
87
  'FST_ERR_REOPENED_SERVER' |
89
88
  'FST_ERR_INSTANCE_ALREADY_STARTED' |
@@ -215,13 +215,13 @@ export interface FastifyInstance<
215
215
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
216
216
  ContextConfig = ContextConfigDefault,
217
217
  SchemaCompiler extends FastifySchema = FastifySchema
218
- >(opts: Pick<RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>, 'method' | 'url' | 'constraints'>): boolean;
218
+ >(opts: Pick<RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>, 'url' | 'constraints'> & { method: HTTPMethods }): boolean;
219
219
 
220
220
  findRoute<
221
221
  RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
222
222
  ContextConfig = ContextConfigDefault,
223
223
  SchemaCompiler extends FastifySchema = FastifySchema
224
- >(opts: Pick<RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>, 'method' | 'url' | 'constraints'>): Omit<FindMyWayFindResult<RawServer>, 'store'>;
224
+ >(opts: Pick<RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>, 'url' | 'constraints'> & { method: HTTPMethods }): Omit<FindMyWayFindResult<RawServer>, 'store'>;
225
225
 
226
226
  // addHook: overloads
227
227
 
@@ -579,7 +579,7 @@ export interface FastifyInstance<
579
579
  * which allows for modifying or adding child logger bindings and logger options, or
580
580
  * returning a completely custom child logger implementation.
581
581
  */
582
- childLoggerFactory: FastifyChildLoggerFactory<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
582
+ childLoggerFactory: FastifyChildLoggerFactory<RawServer, RawRequest, RawReply, FastifyBaseLogger, TypeProvider>;
583
583
 
584
584
  /**
585
585
  * Hook function that is called when creating a child logger instance for each request
@@ -598,7 +598,7 @@ export interface FastifyInstance<
598
598
  * }
599
599
  * ```
600
600
  */
601
- setChildLoggerFactory(factory: FastifyChildLoggerFactory<RawServer, RawRequest, RawReply, Logger,
601
+ setChildLoggerFactory(factory: FastifyChildLoggerFactory<RawServer, RawRequest, RawReply, FastifyBaseLogger,
602
602
  TypeProvider>): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
603
603
 
604
604
  /**
package/types/route.d.ts CHANGED
@@ -46,7 +46,8 @@ export interface RouteConstraint {
46
46
  /**
47
47
  * Route shorthand options for the various shorthand methods
48
48
  */
49
- type RouteShorthandHook<T extends (...args: any) => any> = (...args: Parameters<T>) => void | Promise<unknown>
49
+ type RouteShorthandHook<T extends (this: any, ...args: any) => any> =
50
+ (this: ThisParameterType<T>, ...args: Parameters<T>) => void | Promise<unknown>
50
51
 
51
52
  export interface RouteShorthandOptions<
52
53
  RawServer extends RawServerBase = RawServerDefault,
@@ -78,7 +79,7 @@ export interface RouteShorthandOptions<
78
79
  reply: FastifyReply<RouteGeneric, RawServer, RawRequest, RawReply, ContextConfig, NoInfer<SchemaCompiler>,
79
80
  TypeProvider>
80
81
  ) => void;
81
- childLoggerFactory?: FastifyChildLoggerFactory<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
82
+ childLoggerFactory?: FastifyChildLoggerFactory<RawServer, RawRequest, RawReply, FastifyBaseLogger, TypeProvider>;
82
83
  schemaErrorFormatter?: SchemaErrorFormatter;
83
84
 
84
85
  // hooks