Connect for ECMAScript

repository·main·Indexed 23 days ago

https://github.com/connectrpc/connect-es

A family of libraries for building type-safe APIs across TypeScript, web browsers, and Node.js. Includes support for various frameworks and environments via packages such as @connectrpc/connect-express, @connectrpc/connect-fastify, @connectrpc/connect-next, and @connectrpc/connect-cloudflare. The ecosystem provides tools for protocol support (Connect, gRPC-Web), conformance testing, and a migration tool (@connectrpc/connect-migrate) to transition projects to the @connectrpc organization.

Tokens
25.2K
Snippets
57
Records
126
Agent score
82%

What's inside connect-es

  1. What is Connect Conformance

    main

    Connect Conformance is a package that provides common artifacts and functionality used to run conformance tests against Connect-ES packages (such as @connectrpc/connect-node or @connectrpc/connect-web).

    Note: This package does not execute the tests itself; it exports the necessary tools and logic for other packages to perform conformance testing.

  2. Overview of @connectrpc/connect

    main

    Connect is a family of libraries for building type-safe APIs across different languages and platforms. The @connectrpc/connect package provides TypeScript support for web browsers and Node.js.

    It follows a schema-first approach using Protocol Buffers. Once a schema is defined, code generation produces type-safe servers and clients. While the RPCs are type-safe end-to-end, they use regular HTTP under the hood, making them compatible with standard tools like curl and visible in network inspectors.

    Connect supports three protocols:

    1. gRPC
    2. gRPC-web
    3. Connect protocol: An optimized protocol for the web.

    This allows for high interoperability between Node.js services and clients running in web browsers, terminals, or native mobile environments.

    service ElizaService {
      rpc Say(SayRequest) returns (SayResponse) {}
    }
    const answer = await eliza.say({ sentence: "I feel happy." });
    console.log(answer);
    // {sentence: 'When you feel happy, what do you do?'}
    curl \
        --header 'Content-Type: application/json' \
        --data '{"sentence": "I feel happy."}' \
        https://demo.connectrpc.com/connectrpc.eliza.v1.ElizaService/Say
  3. Handle strict type checking for request objects in Connect v2

    main

    In Connect v2, API calls are strictly typed. You can no longer pass a message object that is a superset of the target type (e.g., passing MessageB to a method expecting MessageA). Only the exact specified target type will pass TypeScript compilation.

    To pass a message with the same fields as a different message type, use object destructuring to remove the $typeName property before creating the new message.

    // If you need to convert MessageA to MessageB by dropping the type name:
    const messageA: MessageA = ...;
    const { $typeName: _, ...properties } = messageA;
    const messageB = create(MessageBSchema, properties);
  4. How Connect RPC protocols work

    main

    Connect is a type-safe RPC protocol that runs over regular HTTP. It is compatible with curl and can be inspected via standard network tools. Connect implements three protocols to ensure interoperability:

    1. gRPC: The widely used standard.
    2. gRPC-web: For browser-based gRPC clients.
    3. Connect Protocol: An optimized protocol specifically for the web.

    Connect uses Protobuf-ES for Protobuf compliance.

  5. Update dependencies for Connect v2

    main

    In Connect v2, the protoc-gen-connect-es plugin has been removed. Connect now relies on service descriptors generated by the Protobuf-ES v2 plugin protoc-gen-es. You must remove @connectrpc/protoc-gen-connect-es from your package.json.

    Compatible dependencies should look similar to this:

      "dependencies": {
        "@bufbuild/protobuf": "^2.2.0",
        "@bufbuild/protoc-gen-es": "^2.2.0",
        "@connectrpc/connect": "^2.0.0",
    -   "@connectrpc/protoc-gen-connect-es": "^1.0.0",
        "@connectrpc/connect-web": "^2.0.0",
        "@connectrpc/connect-node": "^2.0.0",
        "@connectrpc/connect-next": "^2.0.0",
        "@connectrpc/connect-fastify": "^2.0.0",
        "@connectrpc/connect-express": "^2.0.0",
        "@connectrpc/connect-query": "^2.0.0",
        "@connectrpc/protoc-gen-connect-query": "^2.0.0",
        "@connectrpc/connect-playwright": "^0.6.0"
      }

    Note: The connect-migrate tool handles these dependency updates automatically.

  6. Migrate Protobuf messages from v1 to v2

    main

    In Protobuf-ES v2, messages are no longer generated as classes. Instead, they are generated as a schema object and a plain TypeScript type.

    Key changes:

    • Creation: Use the create function from @bufbuild/protobuf with the schema instead of the new keyword.
    • Methods: Standalone functions replace class methods (e.g., toBinary, equals, clone, toJson, toJsonString).
    • Identification: Use the $typeName property instead of .getType().typeName.
    • Validation: Use isMessage(x, Schema) instead of isMessage(x, Class).
    • Types: PlainMessage<T> and PartialMessage<T> have been removed. Use the message type directly or Omit<T, "$typeName"> if you need to exclude the type name.
  7. Upgrade generated SDKs from Connect v1 to v2

    main

    The connect-migrate tool does not automatically upgrade generated SDKs. To upgrade from v1 to v2, follow these four steps:

    1. Uninstall old generated SDKs: Remove dependencies that use the connectrpc_es plugin. These are typically named @buf/{module_owner}_{module_name}.connectrpc_es.
    2. Run the migration tool: Execute npx @connectrpc/connect-migrate@latest to update your @connect and @bufbuild package dependencies.
    3. Install new generated SDKs: Install the new SDKs using the Protobuf-ES v2 plugin (bufbuild_es). For example, replace @buf/googleapis_googleapis.connectrpc_es with @buf/googleapis_googleapis.bufbuild_es@latest.
    4. Update your application code: Update your import paths to point to the new package names and file extensions (e.g., changing .connect.js to _pb.js).
  8. Run Connect-ES conformance tests on Cloudflare

    main

    You can run conformance tests for both clients and servers for the @connectrpc/connect-cloudflare package using the conformance:client and conformance:server tasks. This is typically done via turbo in the repository environment.

    To run both client and server conformance tests, use the following command:

    npx turbo run --filter @connectrpc/connect-cloudflare conformance:client conformance:server
  9. Use the fastifyConnectPlugin to plug Connect RPCs into Fastify

    main

    You can integrate Connect RPCs into a Fastify server using the fastifyConnectPlugin. This plugin allows your server to handle gRPC, gRPC-Web, and Connect protocol requests.

    When registering the plugin, you provide a routes object (a ConnectRouter) and can optionally provide interceptors, such as the createValidateInterceptor from @connectrpc/validate for request validation.

    Note: For optimal performance with gRPC, it is recommended to enable http2: true in your Fastify configuration.

    import { fastify } from "fastify";
    import routes from "connect";
    import { fastifyConnectPlugin } from "@connectrpc/connect-fastify";
    import { createValidateInterceptor } from "@connectrpc/validate";
    
    const server = fastify({
      http2: true,
    });
    
    await server.register(fastifyConnectPlugin, {
      // Validation via Protovalidate is almost always recommended
      interceptors: [createValidateInterceptor()],  
      routes
    });
    
    await server.listen({
      host: "localhost",
      port: 8080,
    });
  10. Run Connect-Web conformance tests

    main

    Conformance tests for @connectrpc/connect-web can be run in several environments and with two different client flavors. The tests implement the conformance service described in the Connect documentation.

    Supported Environments

    • Chrome
    • Firefox
    • Safari (Requires OSX and enabling "Allow Remote Automation" in the Safari Develop menu)
    • Node.js

    Client Flavors

    • Promise: Uses createClient
    • Callback: Uses createCallbackClient

    Execution Command

    Use the following pattern to run tests: npx turbo run conformance:<environment>:<flavor>

    npx turbo run conformance:chrome:promise
  11. Migrate from Connect v1 to v2

    main

    Connect v2 introduces new features, simplifies APIs, and leverages Protobuf-ES v2.

    Requirements

    • Node.js: Version 18.14.1 or higher (Node 16 is no longer supported).
    • TypeScript: Version v4.9.5 or higher (TypeScript 4.1 is no longer supported).

    Migration Steps

    1. Check SDKs: If you use generated SDKs, review the Upgrading generated SDKs section (available in the full guide) before proceeding.
    2. Run Migration Tool: Use the @connectrpc/connect-migrate tool to automate dependency updates, plugin updates, and minor code changes.
    3. Manual Updates: The tool does not cover all code use cases. You must manually update application code and handle code regeneration.
    npx @connectrpc/connect-migrate@latest