A Socket API that works across JavaScript runtimes — announcing a WinterCG spec and Node.js implementation of connect()

A Socket API that works across JavaScript runtimes — announcing a WinterCG spec and Node.js implementation of connect()

A Socket API that works across JavaScript runtimes — announcing a WinterCG spec and Node.js implementation of connect()

Earlier this year, we announced a new API for creating outbound TCP sockets — connect(). From day one, we’ve been working with the Web-interoperable Runtimes Community Group (WinterCG) community to chart a course toward making this API a standard, available across all runtimes and platforms — including Node.js.

Today, we’re sharing that we’ve reached a new milestone in the path to making this API available across runtimes — engineers from Cloudflare and Vercel have published a draft specification of the connect() sockets API for review by the community, along with a Node.js compatible implementation of the connect() API that developers can start using today.

This implementation helps both application developers and maintainers of libraries and frameworks:

  • Maintainers of existing libraries that use the node:net and node:tls APIs can use it to more easily add support for runtimes where node:net and node:tls are not available.
  • JavaScript frameworks can use it to make connect() available in local development, making it easier for application developers to target runtimes that provide connect().
  • Why create a new standard? Why connect()?

    As we described when we first announced connect(), to-date there has not been a standard API across JavaScript runtimes for creating and working with TCP or UDP sockets. This makes it harder for maintainers of open-source libraries to ensure compatibility across runtimes, and ultimately creates friction for application developers who have to navigate which libraries work on which platforms.

    While Node.js provides the node:net and node:tls APIs, these APIs were designed over 10 years ago in the very early days of the Node.js project and remain callback-based. As a result, they can be hard to work with, and expose configuration in ways that don’t fit serverless platforms or web browsers.

    The connect() API fills this gap by incorporating the best parts of existing socket APIs and prior proposed standards, based on feedback from the JavaScript community — including contributors to Node.js. Libraries like pg (node-postgres on Github) are already using the connect() API.

    The connect() specification

    At time of writing, the draft specification of the Sockets API defines the following API:

    dictionary SocketAddress {
      DOMString hostname;
      unsigned short port;
    };
    
    typedef (DOMString or SocketAddress) AnySocketAddress;
    
    enum SecureTransportKind { "off", "on", "starttls" };
    
    [Exposed=*]
    dictionary SocketOptions {
      SecureTransportKind secureTransport = "off";
      boolean allowHalfOpen = false;
    };
    
    [Exposed=*]
    interface Connect {
      Socket connect(AnySocketAddress address, optional SocketOptions opts);
    };
    
    interface Socket {
      readonly attribute ReadableStream readable;
      readonly attribute WritableStream writable;
    
      readonly attribute Promise<undefined> closed;
      Promise<undefined> close();
    
      Socket startTls();
    };
    

    The proposed API is Promise-based and reuses existing standards whenever possible. For example, ReadableStream and WritableStream are used for the read and write ends of the socket. This makes it easy to pipe data from a TCP socket to any other library or existing code that accepts a ReadableStream as input, or to write to a TCP socket via a WritableStream.

    The entrypoint of the API is the connect() function, which takes a string containing both the hostname and port separated by a colon, or an object with discrete hostname and port fields. It returns a Socket object which represents a socket connection. An instance of this object exposes attributes and methods for working with the connection.

    A connection can be established in plain-text or TLS mode, as well as a special “starttls” mode which allows the socket to be easily upgraded to TLS after some period of plain-text data transfer, by calling the startTls() method on the Socket object. No need to create a new socket or switch to using a separate set of APIs once the socket is upgraded to use TLS.

    For example, to upgrade a socket using the startTLS pattern, you might do something like this:

    import { connect } from "@arrowood.dev/socket"
    
    const options = { secureTransport: "starttls" };
    const socket = connect("address:port", options);
    const secureSocket = socket.startTls();
    // The socket is immediately writable
    // Relies on web standard WritableStream
    const writer = secureSocket.writable.getWriter();
    const encoder = new TextEncoder();
    const encoded = encoder.encode("hello");
    await writer.write(encoded);
    

    Equivalent code using the node:net and node:tls APIs:

    import net from 'node:net'
    import tls from 'node:tls'
    
    const socket = new net.Socket(HOST, PORT);
    socket.once('connect', () => {
      const options = { socket };
      const secureSocket = tls.connect(options, () => {
        // The socket can only be written to once the
        // connection is established.
        // Polymorphic API, uses Node.js streams
        secureSocket.write('hello');
      }
    })
    

    Use the Node.js implementation of connect() in your library

    To make it easier for open-source library maintainers to adopt the connect() API, we’ve published an implementation of connect() in Node.js that allows you to publish your library such that it works across JavaScript runtimes, without having to maintain any runtime-specific code.

    To get started, install it as a dependency:

    npm install --save @arrowood.dev/socket

    And import it in your library or application:

    import { connect } from "@arrowood.dev/socket"

    What’s next for connect()?

    The wintercg/proposal-sockets-api is published as a draft, and the next step is to solicit and incorporate feedback. We’d love your feedback, particularly if you maintain an open-source library or make direct use of the node:net or node:tls APIs.

    Once feedback has been incorporated, engineers from Cloudflare, Vercel and beyond will be continuing to work towards contributing an implementation of the API directly to Node.js as a built-in API.

    Source:: CloudFlare