4.20

NodeJs 18 - What's new, what to expect next

Image
NodeJs version 18 brings new features and adds some experimental APIs, with JavaScript engine updated to V8 10.1

The NodeJS 18 has been released, marked as stable

The version 18.x is not yet marked as LTS but in the nearest future it will replace the current LTS version 16.x.


NodeJs Versions

Node.js major release is updated every six months. But not all of them are marked as LTS (Long Term Support). Currently the LTS version is 16.x code name Gallium (with the latest version 16.17.0 being released as of 2022-08-16).
The newest stable version, NodeJs 18.x code name Hydrogen (released on April 19 2022) is available for download, marked as stable, being supported as an "edge" transitional version to the next LTS version, perhaps 20.x, not yet announced.
The LTS releases typically guarantees that critical bugs will be fixed for a total of 30 months. Production applications should only use Active LTS or Maintenance LTS releases.

NodeJs 18.x major features

  • a new experimental fetch API: it starts the process of fetching a resource from the network
  • a new experimental test runner: now is possible to import the node:test module to write the unit tests and report results in Test Anything Protocol (TAP) format
  • Web Streams API: it is possible now to start programmatically access streams of data received over the network and process them by chunks, without needing to generate a buffer, string, or blob ; the response body is of the type ReadableStream ; will expose the experimental implementation of the Web Streams API on the global scope thus the following APIs are now globally available:
    • ReadableStream
    • ReadableStreamDefaultReader
    • ReadableStreamBYOBReader
    • ReadableStreamBYOBRequest
    • ReadableByteStreamController
    • ReadableStreamDefaultController
    • TransformStream
    • TransformStreamDefaultController
    • WritableStream
    • WritableStreamDefaultWriter
    • WritableStreamDefaultController
    • ByteLengthQueuingStrategy
    • CountQueuingStrategy
    • TextEncoderStream
    • TextDecoderStream
    • CompressionStream
    • DecompressionStream
  • HTTP Timeouts: the server.headersTimeout limits the amount of time in milliseconds the parser will wait to receive the complete HTTP headers from the client
  • V8 JavaScript engine updated to V8 10.1:
    • improves the overall execution speed
    • introduces two new array methods: findLast and findLastIndex
    • adds improvements to the Intl.Locale API ; adds the Intl.supportedValuesOf function

Sample code: the new Fetch API
The fetch only rejects when a network error is encountered. It does not reject on HTTP errors.
const fetchSomeData = async () => {
	const response = await fetch('http://127.0.0.1/fetch/data');
	if (response.ok) {
		const data = await response.json();
		console.log(data);
	} else {
		console.error(`${response.status} ${response.statusText}`);
	}
};

fetchSomeData();

Sample code: Two synchronous and two asynchronous tests (test runner)
The first test passes because 7 == 7. The second test fails because 7 != 8.
import test from 'node:test';
import assert from 'assert';

test('sync test passing', (t) => {
  assert.strictEqual(7, 7);
});

test('sync test failing', (t) => {
  assert.strictEqual(7, 8);
});
The first test passes because 7 == 7. The second test fails because 7 != 8.
import test from 'node:test';
import assert from 'assert';

test('async test passing', async (t) => {
  assert.strictEqual(7, 7);
});

test('async test failing', async (t) => {
  assert.strictEqual(7, 8);
});

Other changes in NodeJs 18.x:
  • crypto: allow zero-length IKM in HKDF and in webcrypto PBKDF2 ; allow zero-length secret KeyObject
  • http: make idle http parser count configurable ; add drop request event for http server
  • net: add local family ; add ability to reset a tcp socket
  • src: print source map error source on demand
  • tls: pass a valid socket on tlsClientError ; move tls.parseCertString to end-of-life
  • events: add CustomEvent ; expose CustomEvent on global with CLI flag
  • lib: improved diagnostics_channel subscribe/unsubscribe ; implement WebAssembly Web API
  • util: add tokens to parseArgs
  • esm: ESM Loader hooks now support multiple custom loaders, and composition is achieved via "chaining" ; custom loaders are a powerful mechanism for controlling an application, providing extensive control over loading modules
  • ssl: fix OpenSSL - AES OCB fails to encrypt some bytes
  • dns: DNS rebinding in --inspect via invalid IP addresses ; accept 'IPv4' and 'IPv6' for family ; remove dns.lookup and dnsPromises.lookup options type coercion
  • encoding: HTTP Request Smuggling fixes: Flawed Parsing of Transfer-Encoding ; Improper Delimiting of Header Fields ; Incorrect Parsing of Multi-line Transfer-Encoding
  • security: fix a possible DLL Hijacking on Windows
  • crypto: remove Node.js-specific webcrypto extensions ; add CFRG curves to Web Crypto API
  • report: add more heap infos in process report
  • fs: make params in writing methods optional ; runtime deprecate string coercion in fs.write and fs.writeFileSync
  • process: runtime deprecate multipleResolves