@bufbuild/protoc-gen-connect-es

中文文档
0.13.0发布于 2023/08/18Apache-2.0近 30 天下载 1.8万

Code generator for Connect

此软件包已被标记为弃用

Connect has moved to its own org @connectrpc and has a stable v1. Run `npx @connectrpc/connect-migrate@latest` to update. See https://github.com/connectrpc/connect-es/releases/tag/v0.13.1 for details.

原始使用文档
文档目录9 节
  1. Installation
  2. Generating code
  3. With buf
  4. With protoc
  5. Plugin options
  6. target
  7. import_extension=.js
  8. keep_empty_files=true
  9. Example generated code

@bufbuild/protoc-gen-connect-es

The code generator for Connect, a simple library to work with servers and clients in ECMAScript with the type-safety of TypeScript. It generates code that is compatible with browsers and Node.js.

Learn more about Connect at github.com/connectrpc/connect-es.

Installation

protoc-gen-connect-es is a code generator plugin for Protocol Buffer compilers, like buf and protoc. It generates clients as well as server definitions from your Protocol Buffer schema, and works in tandem with @bufbuild/protoc-gen-es, the code generator plugin for all Protocol Buffer base types. The code these two plugins generate requires the runtime libraries @bufbuild/connect, and @bufbuild/protobuf.

To install buf, the plugins and their runtime libraries, run:

npm install --save-dev @bufbuild/buf @bufbuild/protoc-gen-es @bufbuild/protoc-gen-connect-es
npm install @bufbuild/connect @bufbuild/protobuf

If you want to call Connect or gRPC-web services from a web browsers, make sure to install @bufbuild/connect-web. If you want servers too, install @bufbuild/connect-node, @bufbuild/connect-fastify, or @bufbuild/connect-express

We use peer dependencies to ensure that code generator and runtime library are compatible with each other. Note that npm installs them automatically, but yarn and pnpm do not.

Generating code

With buf

Add a new configuration file buf.gen.yaml:

# buf.gen.yaml defines a local generation template.
# For details, see https://docs.buf.build/configuration/v1/buf-gen-yaml
version: v1
plugins:
  # This will invoke protoc-gen-es and write output to src/gen
  - plugin: es
    out: src/gen
    opt: target=ts
  # This will invoke protoc-gen-connect-es
  - plugin: connect-es
    out: src/gen
    opt:
      # Add more plugin options here
      - target=ts

To generate code for all protobuf files within your project, simply run:

npx buf generate

Note that buf can generate from various inputs, not just local protobuf files. For example, npx buf generate buf.build/connectrpc/eliza generates code for the module connectrpc/eliza on the Buf Schema Registry.

With protoc

PATH=$PATH:$(pwd)/node_modules/.bin \
  protoc -I . \
  --es_out src/gen \
  --es_opt target=ts \
  --connect-es_out src/gen \
  --connect-es_opt target=ts \
  a.proto b.proto c.proto

Note that we are adding node_modules/.bin to the $PATH, so that the protocol buffer compiler can find them. This happens automatically with npm scripts.

Since yarn v2 and above does not use a node_modules directory, you need to change the variable a bit:

PATH=$(dirname $(yarn bin protoc-gen-es)):$(dirname $(yarn bin protoc-gen-connect-es)):$PATH

Plugin options

target

This option controls whether the plugin generates JavaScript, TypeScript, or TypeScript declaration files.

Possible values:

  • target=js - generates a _connect.js file for every .proto input file.
  • target=ts - generates a _connect.ts file for every .proto input file.
  • target=dts - generates a _connect.d.ts file for every .proto input file.

Multiple values can be given by separating them with +, for example target=js+dts.

By default, we generate JavaScript and TypeScript declaration files, which produces the smallest code size and is the most compatible with various bundler configurations. If you prefer to generate TypeScript, use target=ts.

import_extension=.js

By default, protoc-gen-connect-es (and all other plugins based on @bufbuild/protoplugin) uses a .js file extensions in import paths, even in TypeScript files.

This is unintuitive, but necessary for ECMAScript modules in Node.js. Unfortunately, not all bundlers and tools have caught up yet, and Deno requires .ts. With this plugin option, you can replace .js extensions in import paths with the given value. For example, set

  • import_extension=none to remove the .js extension
  • import_extension=.ts to replace the .js extension with .ts

keep_empty_files=true

By default, protoc-gen-connect-es (and all other plugins based on @bufbuild/protoplugin) omit empty files from the plugin output. This option disables pruning of empty files, to allow for smooth interoperation with Bazel and similar tooling that requires all output files to be declared ahead of time. Unless you use Bazel, it is very unlikely that you need this option.

Example generated code

eliza.proto

syntax = "proto3";

package connectrpc.eliza.v1;

// ElizaService provides a way to talk to Eliza, a port of the DOCTOR script
// for Joseph Weizenbaum's original ELIZA program. Created in the mid-1960s at
// the MIT Artificial Intelligence Laboratory, ELIZA demonstrates the
// superficiality of human-computer communication. DOCTOR simulates a
// psychotherapist, and is commonly found as an Easter egg in emacs
// distributions.
service ElizaService {
  // Say is a unary RPC. Eliza responds to the prompt with a single sentence.
  rpc Say(SayRequest) returns (SayResponse) {}
}

// SayRequest is a single-sentence request.
message SayRequest {
  string sentence = 1;
}

// SayRequest is a single-sentence response.
message SayResponse {
  string sentence = 1;
}

eliza_connect.ts

/**
 * ElizaService provides a way to talk to Eliza, a port of the DOCTOR script
 * for Joseph Weizenbaum's original ELIZA program. Created in the mid-1960s at
 * the MIT Artificial Intelligence Laboratory, ELIZA demonstrates the
 * superficiality of human-computer communication. DOCTOR simulates a
 * psychotherapist, and is commonly found as an Easter egg in emacs
 * distributions.
 *
 * @generated from service connectrpc.eliza.v1.ElizaService
 */
export const ElizaService = {
  typeName: "connectrpc.eliza.v1.ElizaService",
  methods: {
    /**
     * Say is a unary RPC. Eliza responds to the prompt with a single sentence.
     *
     * @generated from rpc connectrpc.eliza.v1.ElizaService.Say
     */
    say: {
      name: "Say",
      I: SayRequest,
      O: SayResponse,
      kind: MethodKind.Unary,
    },
  }
} as const;

版本与发布记录

@bufbuild/protoc-gen-connect-es 共发布 15 个版本,首次发布于 2023/02/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.13.0latest2023/08/183 年前
0.12.02023/07/253 年前
0.11.02023/06/293 年前
0.10.12023/06/213 年前
0.10.02023/06/153 年前
0.9.12023/05/313 年前

关于 @bufbuild/protoc-gen-connect-es 的常见问题

@bufbuild/protoc-gen-connect-es 是什么?

Code generator for Connect注意:该包已被作者标记为弃用——Connect has moved to its own org @connectrpc and has a stable v1. Run `npx @connectrpc/connect-migrate@latest` to update. See https://github.com/connectrpc/connect-es/releases/tag/v0.13.1 for details.

如何安装 @bufbuild/protoc-gen-connect-es?

在项目目录运行 npm install @bufbuild/protoc-gen-connect-es,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 protoc-gen-connect-es,可以用 npx protoc-gen-connect-es 直接运行。

@bufbuild/protoc-gen-connect-es 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bufbuild__protoc-gen-connect-es 是否存在。

@bufbuild/protoc-gen-connect-es 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bufbuild/protoc-gen-connect-es 可以商用吗?

@bufbuild/protoc-gen-connect-es 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bufbuild/protoc-gen-connect-es 的下载量有多少?

根据 npm 官方下载统计,@bufbuild/protoc-gen-connect-es 在 2026/10/09 – 2026/11/07 这 30 天内被下载 17,766 次,日均约 592 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bufbuild/protoc-gen-connect-es 还在维护吗?

最新版本 0.13.0 发布于 2023/08/18(3 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/02/17。作者已将其标记为弃用,新项目建议寻找替代方案。