@bufbuilding/protoc-gen-connection-es (英语)
Connect 的代码生成器, 用于服务器和客户端的简单库 在 ECMAScript 中,带有 TypeScript 的类型安全。 它生成兼容的代码 浏览器和Node.js.
更多关于连接的信息 github.com/连接rpc/连接-es。 。 。 .
安装
protoc-gen-connect-es 是协议缓冲编译器的代码生成器插件,
喜欢 库夫 财务报告和审定财务报表 原型。 。 。 .
它从您的协议缓冲计生成客户端和服务器定义,并与
@ bufbuilt/ protoc- gen- s, (中文(简体) ),
用于所有协议缓冲基准类型的代码生成器插件。 这两位的密码
插件生成需要运行时库 @ bufbuilding/ 连接, (中文(简体) ),
财务报告和审定财务报表 @ bufbuilding/ protobuf。 。 。 .
要安装 buf,插件及其运行时库,运行:
npm install --save-dev @bufbuild/buf @bufbuild/protoc-gen-es @bufbuild/protoc-gen-connect-es
npm install @bufbuild/connect @bufbuild/protobuf
如果您想从网页浏览器调用 Connect 或 gRPC-web 服务, 请确保安装 @ bufbuilding/ 连接- web如果您也想要服务器, 安装 @ bufbuilding/ 连接- 节点, (中文(简体) ), @ bufbuilding/ 连接- fastify,或 @ bufbuilding/ 连接- express
我们使用对等依赖,以确保代码生成器和运行时库 相互兼容。 注意 npm 自动安装它们, 但是纱线 和pnpm没有.
生成代码
与buf (英语)
添加新配置文件 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
要生成您项目中所有 protobuf 文件的代码, 只需运行 :
npx buf generate
请注意: buf 可以从各种 投入, (中文(简体) ),
不只是本地原生文件。 譬如说, npx buf generate buf.build/connectrpc/eliza
生成模块的代码 连接/ eliza 在布夫斯克马
书记官处.
带着原生物
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
请注意,我们正在增加 node_modules/.bin 页:1 $PATH,以便协议
缓冲编译器可以找到它们. Npm 脚本会自动发生这种情况.
由于线条v2及以上不使用 node_modules 目录,您需要
将变量更改一点 :
PATH=$(dirname $(yarn bin protoc-gen-es)):$(dirname $(yarn bin protoc-gen-connect-es)):$PATH
插件选项
target
此选项控制插件是否生成 JavaScript, TypeScript, 或类型脚本声明文件.
可能的值 :
target=js- 生成一个_connect.js每个文件.proto输入文件.target=ts- 生成一个_connect.ts每个文件.proto输入文件.target=dts- 生成一个_connect.d.ts每个文件.proto输入文件.
可以通过将它们与 +,例如
target=js+dts。 。 。 .
默认情况下, 我们生成 JavaScript 和 TypeScript 声明文件, 该文件
生成最小的代码大小, 并且与各种代码最相容
捆绑配置. 如果您喜欢生成类型脚本, 请使用 target=ts。 。 。 .
import_extension=.js
默认, 原生基因连接
(和基于 @ bufbuilding/ protoplugin (英语)(中文(简体) )
使用a .js 导入路径中的文件扩展名,甚至在TypeScript文件中.
这是不直观的,但必需的 节点.js中的ECMAScript模块。 。 。 .
不幸的是,并非所有的捆绑者和工具都赶上了,而德诺
需求 .ts。使用此插件选项,可以替换 .js 扩展
在带有给定值的导入路径中。 例如,设定
import_extension=none删除.js扩展import_extension=.ts替换.js扩展名.ts
keep_empty_files=true
默认, 原生基因连接 (和基于 @ bufbuilding/ protoplugin (英语)(中文(简体) ) 从插件输出中删除空文件 。 此选项禁用运行 空文件,以便与 Bazel 和类似文件进行平稳的交互 工具,该工具需要所有输出文件提前宣布. 除非你用Bazel 否则你不可能需要这个选择.
生成代码示例
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;