hapijs / hapi

The Simple, Secure Framework Developers Trust
https://hapi.dev
Other
14.63k stars 1.34k forks source link

21.0.0 Release Notes #4386

Closed devinivy closed 1 year ago

devinivy commented 1 year ago

Summary

hapi v21.0.0 is a medium-sized release focused on modernization and miscellaneous API improvements. All modules in the hapi.js ecosystem have been updated to officially support Node.js v18, be compatible with ESM projects, and drop support for Node.js v12. Plugins in the hapi.js ecosystem now support hapi v20+.

Breaking Changes

Ecosystem-wide

New Features

Ecosystem-wide

Bug fixes

Migration Checklist

TLDR

While a more extensive migration guide exists below, the vast majority of hapi users should be looking for the following:

Node.js version

Make sure your Node.js version is v14 LTS or newer. This release uses language features that are only available in Node.js v14.15.0 or higher and will not start on older versions. Node.js v18 is the current LTS and is the recommended version.

Default server host aligned with Node.js defaults

When creating a server with Hapi.server(), if you do not specify server.options.host then hapi used to default to 0.0.0.0, i.e. all available IPv4 network interfaces. This did not jibe with IPv6 or dual-stack hosts, particularly since as of Node.js v17, the dns module would resolve a request to localhost to IPv6 network interfaces when available. So hapi now defaults server.options.host to ::1 on IPv6-compatible machines, and otherwise to 0.0.0.0, in alignment with Node.js's bare HTTP server.

Checklist:

Catbox engines are now exported as Engine

In order to be more ESM-friendly, the catbox engines (catbox-memory, catbox-redis, catbox-memcached, catbox-object) now export the engine as a property Engine rather than as the default export. There have also been changes to catbox-memcached's engine options.

Checklist:

Remove support for JSONP

JSONP (or, "JSON with Padding") was an approach to circumvent the browser same-origin policy when making AJAX requests for data. With the advent and broad client support for CORS, JSONP no longer belongs in hapi core, so the route.options.jsonp option has been removed.

Checklist:

Support only standard Stream.Readable implementations for responses

Over Node.js's lifetime, we have seen several implementations of streams, and it used to be more common for libraries to implement their own non-standard stream interfaces. The Node.js ecosystem has settled down in this regard as of Node.js v14, and this has allowed hapi to remove code catering to non-standard implementations.

Checklist:

Updated server.events interface

The server.events interface comes from the podium package, which has been upgrade to v5 with some breaking changes. Notably, async usage of emit() should be replaced with await gauge().

Checklist:

Server lifecycle no longer waits on 'start' and 'stop' event handlers

When calling await server.start() and await server.stop(), hapi previously waited for all asynchronous event handlers for 'start' and 'stop' to complete. These event handlers no longer delay server start and stop.

Checklist:

Change default x-xss-protection header to 0

The route.options.security.xss option governs the x-xss-protection security header. In the past this option has been true or false, defaulting to true which resulted in a header value of 1; mode=block. This is no longer standard practice, and is recommended against by OWASP. The option now has been expanded to take values 'disable', 'enable', or false. When set to the default value of 'disable', it results in a header value of 0.

Checklist:

Default status code for CORS preflight requests has changed

In past versions of hapi, successful CORS preflight requests have been served with the status code determined by route.options.response.emptyStatusCode, which is 204 by default. Despite the fact that these responses are indeed empty, it turns out to be compatible with a wider variety of clients to serve a 200, so in the this version of hapi we have switched the status code of CORS preflights to 200.

Checklist:

Custom response varieties ignore return value of prepare()

If you create a custom response variety using request.generateResponse(), it has been an implicit requirement that prepare(response) return response. If you returned a different response object, then response would be orphaned and it would not be closed, resulting in potential resource leaks. If you returned nothing, it would lead to a runtime error. In order to avoid this, hapi now ignores the return value of prepare().

Checklist:

hueniverse commented 1 year ago

Finally hapi can drink.

wiggisser commented 1 year ago

Actually, using hapi@21 with node v14.xx will throw an error if xx < 10 because it uses performance.eventLoopUtilization which was only introduced in node v14.10 and above ...

devinivy commented 1 year ago

Hey, that's a good point. I will update the notes to reflect that we support Node.js v14 LTS, which starts at v14.15.0.