fastify 5.7.2 → 5.7.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.
package/AGENTS.md ADDED
@@ -0,0 +1,290 @@
1
+ # AGENTS.md - Guide for AI Agents Working with Fastify
2
+
3
+ This document provides information and guidelines for AI agents (such as GitHub Copilot, Cursor, pi, or other AI coding assistants) working with the Fastify codebase.
4
+
5
+ ## Project Overview
6
+
7
+ Fastify is a high-performance web framework for Node.js focused on:
8
+ - **Speed**: One of the fastest Node.js web frameworks
9
+ - **Extensibility**: Powerful plugin architecture with hooks and decorators
10
+ - **Schema-based**: JSON Schema validation and serialization
11
+ - **Developer experience**: Expressive API with minimal overhead
12
+ - **TypeScript support**: Full type definitions included
13
+
14
+ **Current Version**: 5.7.1 (main branch)
15
+ **Repository**: https://github.com/fastify/fastify
16
+
17
+ ## Repository Structure
18
+
19
+ ```
20
+ fastify/
21
+ ├── docs/ # Documentation (Guides and Reference)
22
+ │ ├── Guides/ # Tutorials and how-to guides
23
+ │ └── Reference/ # API documentation
24
+ ├── examples/ # Example applications and benchmarks
25
+ ├── lib/ # Core library code
26
+ ├── test/ # Test files
27
+ ├── types/ # TypeScript type definitions
28
+ ├── build/ # Build scripts
29
+ ├── integration/ # Integration tests
30
+ ├── fastify.js # Main entry point
31
+ ├── fastify.d.ts # Main TypeScript definitions
32
+ └── package.json # Dependencies and scripts
33
+ ```
34
+
35
+ ## Key Files for Agents
36
+
37
+ ### Core Files
38
+ - **`fastify.js`** - Main Fastify class and entry point
39
+ - **`fastify.d.ts`** - TypeScript type definitions (keep these in sync)
40
+ - **`lib/`** - All core functionality:
41
+ - `route.js` - Route handling
42
+ - `req-res.js` - Request and Reply objects
43
+ - `hooks.js` - Lifecycle hooks
44
+ - `plugin.js` - Plugin system
45
+ - `validation.js` - Schema validation
46
+ - `content-type-parser.js` - Body parsing
47
+ - `logger.js` - Pino logger integration
48
+
49
+ ### Configuration Files
50
+ - **`package.json`** - Scripts, dependencies, and contributors
51
+ - **`eslint.config.js`** - Linting configuration (uses neostandard)
52
+ - **`.markdownlint-cli2.yaml`** - Markdown linting rules
53
+
54
+ ### Documentation Files
55
+ - **`README.md`** - Project overview and quick start
56
+ - **`CONTRIBUTING.md`** - Contribution guidelines
57
+ - **`GOVERNANCE.md`** - Links to organization governance
58
+ - **`SECURITY.md`** - Security policy
59
+ - **`docs/Guides/Contributing.md`** - Detailed contributing guide
60
+ - **`docs/Guides/Style-Guide.md`** - Coding style conventions
61
+
62
+ ## Testing Conventions
63
+
64
+ ### Test Framework
65
+ Fastify uses **Borp** (a custom test runner) for testing.
66
+
67
+ ### Test Structure
68
+ - Tests are in the **`test/`** directory
69
+ - Test files follow the pattern: `test/<module>.test.js`
70
+ - Integration tests are in **`integration/`** directory
71
+
72
+ ### Running Tests
73
+
74
+ ```bash
75
+ # Run all tests
76
+ npm test
77
+
78
+ # Run unit tests only
79
+ npm run unit
80
+
81
+ # Run tests with coverage
82
+ npm run coverage
83
+
84
+ # Run tests in watch mode
85
+ npm run test:watch
86
+
87
+ # Run TypeScript type tests
88
+ npm run test:typescript
89
+
90
+ # Run CI tests (minimal)
91
+ npm run test:ci
92
+ ```
93
+
94
+ ### Test Requirements
95
+ - **100% line coverage** is required for all changes (enforced by CI)
96
+ - Tests must pass on all supported Node.js versions
97
+ - TypeScript types must be tested (using `tsd`)
98
+
99
+ ## Code Style and Conventions
100
+
101
+ ### Linting
102
+ - Uses **Neostandard** JavaScript style guide
103
+ - ESLint is configured in `eslint.config.js`
104
+ - Run `npm run lint` to check code style
105
+ - Run `npm run lint:fix` to auto-fix issues
106
+
107
+ ### Key Style Rules (from Style-Guide.md)
108
+ - Use `const` and `let`, never `var`
109
+ - Use arrow functions for callbacks
110
+ - Use async/await instead of promises
111
+ - Follow semicolon usage (neostandard enforces this)
112
+ - Use template literals for string interpolation
113
+ - Prefer functional methods (`map`, `filter`, `reduce`) over loops
114
+ - Error-first callback pattern for async operations where needed
115
+
116
+ ### Naming Conventions
117
+ - **Files**: kebab-case (`content-type-parser.js`)
118
+ - **Variables**: camelCase
119
+ - **Constants**: UPPER_SNAKE_CASE
120
+ - **Classes**: PascalCase
121
+ - **Private methods**: prefixed with `_`
122
+
123
+ ## Common Tasks
124
+
125
+ ### Adding a New Feature
126
+ 1. Implement the feature in `lib/`
127
+ 2. Add tests in `test/`
128
+ 3. Update TypeScript types in `fastify.d.ts` or `types/`
129
+ 4. Update documentation in `docs/`
130
+ 5. Run `npm test` to ensure all tests pass
131
+ 6. Run `npm run lint` to check code style
132
+ 7. Add changelog entry for release
133
+
134
+ ### Fixing a Bug
135
+ 1. Add a failing test case in `test/`
136
+ 2. Fix the bug in `lib/`
137
+ 3. Ensure all tests pass
138
+ 4. Check if TypeScript types need updating
139
+ 5. Update documentation if behavior changes
140
+
141
+ ### Working with Plugins
142
+ - See `docs/Guides/Write-Plugin.md` for plugin authoring
143
+ - See `docs/Guides/Plugins-Guide.md` for plugin usage
144
+ - Plugin example: `lib/plugin.js`
145
+
146
+ ## Architecture Highlights
147
+
148
+ ### Core Components
149
+
150
+ 1. **Server (`fastify.js`)**
151
+ - Main Fastify class
152
+ - Server initialization and configuration
153
+ - Plugin system integration (via `avvio`)
154
+
155
+ 2. **Routing (`lib/route.js`)**
156
+ - Uses `find-my-way` for fast route matching
157
+ - Route registration and lookup
158
+ - Shorthand methods (get, post, put, delete, etc.)
159
+
160
+ 3. **Request/Response (`lib/req-res.js`)**
161
+ - Request object extensions
162
+ - Reply object with fluent API
163
+ - Decorator support
164
+
165
+ 4. **Hooks (`lib/hooks.js`)**
166
+ - Lifecycle hooks (onRequest, preHandler, etc.)
167
+ - Hook execution order and timing
168
+
169
+ 5. **Validation (`lib/validation.js`)**
170
+ - JSON Schema validation via AJV
171
+ - Response serialization
172
+ - Built-in error serializer
173
+
174
+ 6. **Content Type Parser (`lib/content-type-parser.js`)**
175
+ - Request body parsing
176
+ - Custom parser support
177
+ - JSON and other formats
178
+
179
+ ### Plugin System
180
+ - Plugins are loaded asynchronously via `avvio`
181
+ - Supports encapsulation (scoped plugins)
182
+ - Hooks and decorators can be scoped
183
+ - See `lib/plugin.js` for implementation
184
+
185
+ ## TypeScript Integration
186
+
187
+ - TypeScript definitions are in `fastify.d.ts` and `types/`
188
+ - Types must be tested with `tsd`
189
+ - Run `npm run test:typescript` to verify types
190
+ - Keep types in sync with JavaScript implementation
191
+
192
+ ## Performance Considerations
193
+
194
+ Fastify prioritizes performance:
195
+ - **Routes**: Pre-compiled functions for fast matching
196
+ - **Validation**: Compiled JSON Schema validators
197
+ - **Serialization**: Compiled serializers (fast-json-stringify)
198
+ - **Logging**: Low-overhead Pino logger
199
+ - **Caching**: Route context caching with `toad-cache`
200
+
201
+ When making changes:
202
+ - Profile performance impact for hot paths
203
+ - Use benchmarks in `examples/benchmark/`
204
+ - Run `npm run benchmark` to measure
205
+
206
+ ## Documentation Updates
207
+
208
+ Documentation is critical for Fastify. When changing behavior:
209
+
210
+ 1. Update relevant docs in `docs/Reference/` for API changes
211
+ 2. Update `docs/Guides/` for usage pattern changes
212
+ 3. Check for broken links (CI validates this)
213
+ 4. Update examples in `examples/` if needed
214
+ 5. Run `npm run lint:markdown` to check docs
215
+
216
+ ## Pre-commit Checks
217
+
218
+ Before submitting changes, ensure:
219
+
220
+ 1. ✅ All tests pass: `npm test`
221
+ 2. ✅ 100% coverage: `npm run coverage`
222
+ 3. ✅ Linting passes: `npm run lint`
223
+ 4. ✅ TypeScript types pass: `npm run test:typescript`
224
+ 5. ✅ Markdown linting passes: `npm run lint:markdown`
225
+ 6. ✅ Documentation is updated
226
+ 7. ✅ Examples still work if affected
227
+
228
+ ## Working with CI
229
+
230
+ Fastify uses GitHub Actions for CI. Workflows are in `.github/workflows/`:
231
+ - **`ci.yml`** - Main CI pipeline
232
+ - **`package-manager-ci.yml`** - Tests multiple package managers
233
+ - **`website.yml`** - Website deployment
234
+
235
+ ## Agent-Specific Tips
236
+
237
+ ### When Generating Code
238
+ 1. Check existing patterns in `lib/` before creating new patterns
239
+ 2. Follow the established error handling patterns
240
+ 3. Use async/await consistently
241
+ 4. Add appropriate hooks if extending lifecycle
242
+ 5. Consider TypeScript types from the start
243
+
244
+ ### When Refactoring
245
+ 1. Ensure all tests still pass
246
+ 2. Don't change public APIs without semver consideration
247
+ 3. Update TypeScript definitions if signatures change
248
+ 4. Check for deprecation needs
249
+ 5. Update documentation for changed behavior
250
+
251
+ ### When Analyzing Issues
252
+ 1. Check `test/` for usage examples
253
+ 2. Review relevant `docs/Reference/` files
254
+ 3. Look at similar implementations in `lib/`
255
+ 4. Consider the plugin system and encapsulation
256
+ 5. Check hook timing and order
257
+
258
+ ### Common Gotchas
259
+ - **Encapsulation**: Plugins are isolated - decorators don't leak
260
+ - **Hook order**: Hooks run in specific order (see docs/Reference/Hooks.md)
261
+ - **Async boot**: Server starts asynchronously - use `ready()` or `after()`
262
+ - **Error handling**: Use Fastify error classes from `@fastify/error`
263
+ - **Validation**: Schemas are compiled - changes require recompilation
264
+
265
+ ## Key Dependencies
266
+
267
+ - **`avvio`** - Plugin loading and boot
268
+ - **`find-my-way`** - Fast HTTP router
269
+ - **`fast-json-stringify`** - Response serialization
270
+ - **`pino`** - Logging
271
+ - **`@fastify/ajv-compiler`** - JSON Schema validation
272
+ - **`light-my-request`** - HTTP injection for testing
273
+
274
+ ## Contact and Resources
275
+
276
+ - **Documentation**: https://fastify.dev/
277
+ - **Discord**: https://discord.gg/fastify
278
+ - **GitHub Issues**: https://github.com/fastify/fastify/issues
279
+ - **GitHub Discussions**: https://github.com/fastify/fastify/discussions
280
+ - **Help**: https://github.com/fastify/help
281
+
282
+ ## Version Information
283
+
284
+ - **Main branch**: Fastify v5
285
+ - **v4 branch**: https://github.com/fastify/fastify/tree/4.x
286
+ - **LTS Policy**: See `docs/Reference/LTS.md`
287
+
288
+ ---
289
+
290
+ This document is maintained by the Fastify team. For questions or suggestions, please open an issue or discussion.
package/SECURITY.md CHANGED
@@ -46,10 +46,11 @@ explicitly enabled via configuration options
46
46
  ## Reporting vulnerabilities
47
47
 
48
48
  Individuals who find potential vulnerabilities in Fastify are invited to
49
- complete a vulnerability report via the dedicated pages:
49
+ complete a vulnerability report via the GitHub Security page:
50
50
 
51
- 1. [HackerOne](https://hackerone.com/fastify)
52
- 2. [GitHub Security Advisory](https://github.com/fastify/fastify/security/advisories/new)
51
+ https://github.com/fastify/fastify/security/advisories/new
52
+
53
+ Note: Our [HackerOne](https://hackerone.com/fastify) program is now closed.
53
54
 
54
55
  ### Strict measures when reporting vulnerabilities
55
56
 
@@ -671,9 +671,20 @@ fastify.get('/json', options, function (request, reply) {
671
671
  If you pass a string to `send` without a `Content-Type`, it will be sent as
672
672
  `text/plain; charset=utf-8`. If you set the `Content-Type` header and pass a
673
673
  string to `send`, it will be serialized with the custom serializer if one is
674
- set, otherwise, it will be sent unmodified (unless the `Content-Type` header is
675
- set to `application/json; charset=utf-8`, in which case it will be
676
- JSON-serialized like an object see the section above).
674
+ set, otherwise, it will be sent unmodified.
675
+
676
+ > **Note:** Even when the `Content-Type` header is set to `application/json`,
677
+ > strings are sent unmodified by default. To serialize a string as JSON, you
678
+ > must set a custom serializer:
679
+
680
+ ```js
681
+ fastify.get('/json-string', async function (request, reply) {
682
+ reply
683
+ .type('application/json; charset=utf-8')
684
+ .serializer(JSON.stringify)
685
+ .send('Hello') // Returns "Hello" (JSON-encoded string)
686
+ })
687
+ ```
677
688
  ```js
678
689
  fastify.get('/json', options, function (request, reply) {
679
690
  reply.send('plain string')
package/lib/reply.js CHANGED
@@ -687,6 +687,7 @@ function sendWebStream (payload, res, reply) {
687
687
 
688
688
  let sourceOpen = true
689
689
  let errorLogged = false
690
+ let waitingDrain = false
690
691
  const reader = payload.getReader()
691
692
 
692
693
  eos(res, function (err) {
@@ -719,7 +720,20 @@ function sendWebStream (payload, res, reply) {
719
720
  reader.cancel().catch(noop)
720
721
  return
721
722
  }
722
- res.write(result.value)
723
+ const shouldContinue = res.write(result.value)
724
+ if (shouldContinue === false) {
725
+ waitingDrain = true
726
+ res.once('drain', onDrain)
727
+ return
728
+ }
729
+ reader.read().then(onRead, onReadError)
730
+ }
731
+
732
+ function onDrain () {
733
+ if (!waitingDrain || !sourceOpen || res.destroyed) {
734
+ return
735
+ }
736
+ waitingDrain = false
723
737
  reader.read().then(onRead, onReadError)
724
738
  }
725
739
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastify",
3
- "version": "5.7.2",
3
+ "version": "5.7.3",
4
4
  "description": "Fast and low overhead web framework, for Node.js",
5
5
  "main": "fastify.js",
6
6
  "type": "commonjs",
@@ -6,6 +6,7 @@ const fs = require('node:fs')
6
6
  const { Readable } = require('node:stream')
7
7
  const { fetch: undiciFetch } = require('undici')
8
8
  const http = require('node:http')
9
+ const { setTimeout: sleep } = require('node:timers/promises')
9
10
 
10
11
  test('should response with a ReadableStream', async (t) => {
11
12
  t.plan(2)
@@ -427,6 +428,78 @@ test('WebStream should cancel reader when response is destroyed', (t, done) => {
427
428
  })
428
429
  })
429
430
 
431
+ test('WebStream should respect backpressure', async (t) => {
432
+ t.plan(3)
433
+
434
+ const fastify = Fastify()
435
+ t.after(() => fastify.close())
436
+
437
+ let drainEmittedAt = 0
438
+ let secondWriteAt = 0
439
+ let resolveSecondWrite
440
+ const secondWrite = new Promise((resolve) => {
441
+ resolveSecondWrite = resolve
442
+ })
443
+
444
+ fastify.get('/', function (request, reply) {
445
+ const raw = reply.raw
446
+ const originalWrite = raw.write.bind(raw)
447
+ const bufferedChunks = []
448
+ let wroteFirstChunk = false
449
+
450
+ raw.once('drain', () => {
451
+ for (const bufferedChunk of bufferedChunks) {
452
+ originalWrite(bufferedChunk)
453
+ }
454
+ })
455
+
456
+ raw.write = function (chunk, encoding, cb) {
457
+ if (!wroteFirstChunk) {
458
+ wroteFirstChunk = true
459
+ bufferedChunks.push(Buffer.from(chunk))
460
+ sleep(100).then(() => {
461
+ drainEmittedAt = Date.now()
462
+ raw.emit('drain')
463
+ })
464
+ if (typeof cb === 'function') {
465
+ cb()
466
+ }
467
+ return false
468
+ }
469
+ if (!secondWriteAt) {
470
+ secondWriteAt = Date.now()
471
+ resolveSecondWrite()
472
+ }
473
+ return originalWrite(chunk, encoding, cb)
474
+ }
475
+
476
+ const stream = new ReadableStream({
477
+ start (controller) {
478
+ controller.enqueue(Buffer.from('chunk-1'))
479
+ },
480
+ pull (controller) {
481
+ controller.enqueue(Buffer.from('chunk-2'))
482
+ controller.close()
483
+ }
484
+ })
485
+
486
+ reply.header('content-type', 'text/plain').send(stream)
487
+ })
488
+
489
+ await fastify.listen({ port: 0 })
490
+
491
+ const response = await undiciFetch(`http://localhost:${fastify.server.address().port}/`)
492
+ const bodyPromise = response.text()
493
+
494
+ await secondWrite
495
+ await sleep(120)
496
+ const body = await bodyPromise
497
+
498
+ t.assert.strictEqual(response.status, 200)
499
+ t.assert.strictEqual(body, 'chunk-1chunk-2')
500
+ t.assert.ok(secondWriteAt >= drainEmittedAt)
501
+ })
502
+
430
503
  test('WebStream should warn when headers already sent', async (t) => {
431
504
  t.plan(2)
432
505
 
package/.idea/fastify.iml DELETED
@@ -1,12 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="WEB_MODULE" version="4">
3
- <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$">
5
- <excludeFolder url="file://$MODULE_DIR$/.tmp" />
6
- <excludeFolder url="file://$MODULE_DIR$/temp" />
7
- <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
- </content>
9
- <orderEntry type="inheritedJdk" />
10
- <orderEntry type="sourceFolder" forTests="false" />
11
- </component>
12
- </module>
@@ -1,6 +0,0 @@
1
- <component name="InspectionProjectProfileManager">
2
- <profile version="1.0">
3
- <option name="myName" value="Project Default" />
4
- <inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
5
- </profile>
6
- </component>
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="JavaScriptLibraryMappings">
4
- <includedPredefinedLibrary name="Node.js Core" />
5
- </component>
6
- </project>
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="EslintConfiguration">
4
- <option name="fix-on-save" value="true" />
5
- </component>
6
- </project>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/fastify.iml" filepath="$PROJECT_DIR$/.idea/fastify.iml" />
6
- </modules>
7
- </component>
8
- </project>
package/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="" vcs="Git" />
5
- </component>
6
- </project>