Server-side JavaScript platform Node.js 21.0 available

Node.js 21.0 was released, a platform for running network applications in JavaScript. The Node.js 21.0 branch will be supported for 6 months. In the coming days, the stabilization of the Node.js 20 branch will be completed, which will receive LTS status and will be supported until April 2026. Maintenance of the previous LTS branch of Node.js 18.0 will last until September 2025, and the year before last LTS branch 16.0 until April 2024.

Main improvements:

  • The Fetch API has been declared stable, designed for loading resources over the network and simplifying the writing of universal JavaScript code suitable for working on the server and client sides. The implementation is based on the code from the HTTP/1.1 undici client and is as close as possible to the similar API provided in browsers. The API includes the fetch() method and Headers objects. Request and Response, representing the HTTP headers, request and response. const res = await fetch('https://nodejs.org/api/documentation.json'); if (res.ok) { const data = await res.json(); console.log(data); }
  • Support for the WebStreams API, which provides access to data streams received over the network, has been stabilized. The API makes it possible to add your own handlers that work with data as information arrives over the network, without waiting for the entire file to be downloaded. Objects available in Node.js include ReadableStream*, TransformStream*, WritableStream*, TextEncoderStream, TextDecoderStream, CompressionStream, and DecompressionStream.
  • Added experimental implementation of WebSocket client, compatible with browsers. To enable WebSocket support, the “--experimental-websocket” flag is provided.
  • Added an experimental mode for using the default implementation of JavaScript modules ESM (ECMAScript Modules, used in modules for browsers) instead of CommonJS (specific to Node.js). The change does not affect modules whose format is explicitly defined via the "type" field in package.json, specified via the "--input-type" flag, or is obvious due to the file extension (.mjs for ESM, .cjs for CommonJS). However, modules that are not explicitly defined as CommonJS (for example, have a “.js” extension) will be treated as ESM modules when the new mode is enabled. To activate new module settings, the “--experimental-default-type” flag has been proposed.
  • The V8 engine has been updated to version 11.8, used in Chromium 118, which now supports the ArrayBuffer.prototype.transfer method, the ability to group arrays (groupBy method) and WebAssembly instructions for processing constants (i32.add, i32.sub, i32.mul, i64 .add, i64.sub and i64.mul).
  • Support for the globalPreload handler has been discontinued, in favor of register and initialize calls to configure modules.
  • A “flush” option has been added to the fs.writeFile function to force data to be flushed to the drive after each write operation.
  • Improved performance of code related to URL parsing, fetch API, streams, node:fs, and HTTP.
  • Added global navigator object. For example, to obtain data about the number of CPU cores, you can use the navigator.hardwareConcurrency property.
  • In the “—test” parameter, support for glob masks has been added for selecting tests to run (for example, you can specify “—test **/*.test.js.”).
  • The bundled package manager npm 10.2.0 and the llhttp 9.1.2 parser have been updated.
  • Support for Visual Studio 2019 and versions of macOS older than 11.0 has been discontinued.

The Node.js platform can be used both for server maintenance of Web applications and for creating regular client and server network programs. To expand the functionality of applications for Node.js, a large collection of modules has been prepared, in which you can find modules with the implementation of HTTP, SMTP, XMPP, DNS, FTP, IMAP, POP3 servers and clients, modules for integration with various web frameworks, WebSocket and Ajax handlers , DBMS connectors (MySQL, PostgreSQL, SQLite, MongoDB), templating engines, CSS engines, implementations of crypto algorithms and authorization systems (OAuth), XML parsers.

To ensure the processing of a large number of parallel requests, Node.js uses an asynchronous code execution model based on non-blocking event handling and the definition of callback handlers. Supported methods for multiplexing connections are epoll, kqueue, /dev/poll, and select. For connection multiplexing, the libuv library is used, which is an add-on for libev on Unix systems and IOCP on Windows. The libeio library is used to create a thread pool, and c-ares is integrated to perform DNS queries in non-blocking mode. All system calls that cause blocking are executed inside the thread pool and then, like signal handlers, transfer the result of their work back through an unnamed pipe (pipe). The execution of JavaScript code is provided through the use of the V8 engine developed by Google (in addition, Microsoft is developing a version of Node.js with the Chakra-Core engine).

At its core, Node.js is similar to the Perl AnyEvent, Ruby Event Machine, Python Twisted frameworks, and the Tcl event implementation, but the event loop in Node.js is hidden from the developer and resembles event handling in a web application running in browser. When writing applications for node.js, you need to consider the specifics of event-driven programming, for example, instead of doing "var result = db.query("select..");" with waiting for completion of work and subsequent processing of results, Node.js uses the principle of asynchronous execution, i.e. the code is transformed into "db.query("select..", function (result) {result processing});", in which control will instantly pass to further code, and the query result will be processed as data arrives.

Source: opennet.ru

Add a comment