Fastify v4 GA

Photo courtesy of Ahmed Galal @ Unsplash

After two years we are excited to announce the GA release of the fourth major version of Fastify! This release has been more than a year in the making and we can't wait to share with you all the new features that we think will shape the developer experience in the future.

Before doing so, we would like to acknowledge the tremendous job done by the wide Fastify team and the community, Fastify v3 had been a wildly successful release (we reached more than 1 million downloads per month and counting). We provided our most extended major release ever, with more than 20 minor releases, without breaking changes, without impacting the performances and the developer experience. This is a testament to the framework's stability and the effort the team and community put into maintaining this project.

In this new major, we focused on stabilizing the API, improving the developer experience, and upgrading our core dependencies, but this is not all! Continue reading to learn more.

Avoid the "resolved with undefined" problem once and for all

Since its inception, Fastify has combined a callback-based API (`reply.send({ hello: 'world'})`) with a promise-based one (`return { hello: 'world'}`). However this has caused a lot of confusion when mixing them, resulting in `FST_ERR_PROMISE_NOT_FULFILLED` being logged.. and most people did not know how to fix it. Since v4, we are making it mandatory to `return reply`. if you want to call `reply.send` after the route handler has completed its execution.

New error handling composition

This is one of the most significant improvements of this release. Before Fastify v4, the error handling was inconsistent, each custom error handler was on its own, and if it threw an error, the final response would have been different based on where in your code that error had been thrown. In Fastify v4, custom error handlers are encapsulated, and if a custom error handler throws inside a plugin, the parent one will catch that error and handle it. As a result, the errors that your application will send back will always be consistent.

New Pino Transports

Our out-of-the-box logger Pino, has been upgraded to v8, which means that from now on Pino transports will no longer need to live in a separate process, but can take advantage of worker threads! This will vastly improve the developer experience and simplify configuration. Take a look at this blog post to know more.

New Pino Types

Pino@8 ships with its own types! So you can now specify all the options for Pino straight from the Fastify factory, without installing any additional types.

Type providers out of the box

Type Providers are a TypeScript only feature that enables Fastify to statically infer type information directly from inline JSON Schema. They are an alternative to specifying generic arguments on routes; and can greatly reduce the need to keep associated types for each schema defined in your project.

You can see every other change that we are shipping in Fastify v4 here.

Please let us know about any compatibility and upgrade issues at fastify/help!

Faster cold start

Fastify uses AJV under the hood for request validation and response serialization. The current bottleneck was the use of `uri-js` which was slowing down the cold start by its processing speed and its size. Fastify team developed a lighter alternative: https://github.com/fastify/fast-uri

By then the startup time had improved by 150%.

Reference:
https://github.com/ajv-validator/ajv/pull/1862

Route registration is now synchronous

Due to the changes in https://github.com/fastify/fastify/pull/2954, the following code would work anymore in Fastify v4:

Instead, we should write

This change enables us to provide better error reporting for route definition.

find-my-way v5 and v6

find-my-way is Fastify router. The list of changes in find-my-way is staggering. Here are the main highlights:

  1. Fixed parameter decoding https://github.com/delvedor/find-my-way/pull/282 and https://github.com/delvedor/find-my-way/pull/207
  2. Improved performance when using constraints.
  3. Many optimizations, leading an increase in the number of operations per second between 89% to 298% for most cases.
Image at https://user-images.githubusercontent.com/31734731/169591618-7f242c69-1a30-4bec-9f1f-a48f166e6d57.png

All those improvements have been led by Ivan Tymoshenko, from Kyiv, Ukraine.

Deprecation of variadic .listen signature

The improvements to developer experience are not limited to users of Fastify. With this release, we are starting to make the Fastify contributor experience better by simplifying code where possible. This effort begins with deprecating the variadic signature of the fastify.listen() method. Prior to this release, all of the following invocations of this method were valid:

  • `fastify.listen(8000)`
  • `fastify.listen(8000, ‘127.0.0.1’)`
  • `fastify.listen(8000, ‘127.0.0.1’, 511)`
  • `fastify.listen(8000, (err) => { if (err) throw err })`
  • `fastify.listen({ port: 8000, host: ‘127.0.0.1’ }, (err) => { if (err) throw err })`
  • And many other permutations

Starting with Fastify v4, the .listen method accepts an options object and an optional callback. Any other combination of arguments will result in a deprecation warning in v4. This will ultimately simplify the code that needs to be maintained within Fastify, making contributions like listening on both IPv4 and IPv6 at the same time easier to create. It should also result in easier to maintain TypeScript types, and, as a result, better Intellisense for the .listen method.

So, with Fastify v4, the following invocations are valid:

  • `fastify.listen()`
  • `fastify.listen({ port: 8000 })`
  • `fastify.listen({ port: 8000 }, (err) => { if (err) throw err })`

Acknowledgments

  • More than 3200 commits
  • More than 215 releases,
  • More than 1700 forks
  • More than 2 million downloads per month
  • 213 officially recognized plugins.

This wouldn't have been possible without the help of our fantastic team, which we would like to thank for all their hard work:

We also want to thank our amazing contributors, for all the bug reports, pull requests, and faith in the Fastify vision!

Long Term Support

Fastify v3 will reach End of Life on 30 June 2023, but until then we will continue to maintain this version with security patches and critical fixes.

Fastify v4 is covered by our LTS support cycle. This means at least six months of active support is provided by our core team, plus an additional six months of security fixes after LTS goes into maintenance, which will occur upon the release of Fastify v5.

Happy coding!

The Fastify Team

--

--

--

Fast and low overhead web framework, for Node.js — https://www.fastify.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Embedded System

Going Serverless with AWS Lambda, Serverless Framework and faunaDB.

The pains of Software Tester

GitLab runners on AWS Spot Instances

How To Mint A Free Anonymouse Through Our Smart Contract

Let’s Get Serious About Readability

Adding a new ability to our prototype — The Triple Shot

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fastify

Fastify

Fast and low overhead web framework, for Node.js — https://www.fastify.io/

More from Medium

Building a rest API using Typescript, Express and with Dependency Injection pattern.

How I Created the Focus App Using React and Rust

How to Secure Your Express Application

Pure Functions with Typescript