protobuf-fastdsl 0.1.3 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # protobuf-fastdsl
2
2
 
3
- 一个 Vite 插件,将 TypeScript protobuf 接口在构建时编译为**完全内联**、零依赖的编解码函数。
3
+ TypeScript 接口在构建时编译为**完全内联**、零依赖的 protobuf 编解码函数。
4
4
 
5
5
  无 `.proto` 文件。无运行时库。无函数调用开销。只需 TypeScript 接口 → 确定化的二进制编解码代码。
6
6
 
@@ -8,9 +8,11 @@
8
8
 
9
9
  - **零运行时** — 所有 wire-format 逻辑在编译时内联
10
10
  - **TypeScript 原生** — 用 `pb<N, Type>` 标记接口字段即可定义 schema
11
+ - **跨文件类型** — 支持 `import type` / `import` 引用其他文件的接口定义
11
12
  - **泛型单态化** — `Wrapper<Wrapper<string>>` → 自动生成具体编解码函数
12
13
  - **重复字段** — `pb_repeated<N, Type>` 编译为 `Type[]`
13
14
  - **编译期预计算 Tag** — 字段标签在编译时折叠为字面量字节
15
+ - **安全的 Fallback** — 未经插件转换时运行会直接抛错,不会静默产生错误结果
14
16
 
15
17
  ## 安装
16
18
 
@@ -20,30 +22,41 @@ npm install protobuf-fastdsl
20
22
 
21
23
  ## 快速开始
22
24
 
23
- **1. 在 `vite.config.ts` 中添加插件:**
25
+ **1. 配置构建工具插件:**
24
26
 
25
27
  ```ts
26
- import protobufVite from 'protobuf-fastdsl';
28
+ // vite.config.ts
29
+ import protobufPlugin from 'protobuf-fastdsl/vite';
27
30
 
28
31
  export default defineConfig({
29
- plugins: [protobufVite()],
32
+ plugins: [protobufPlugin()],
30
33
  });
31
34
  ```
32
35
 
33
- **2. `tsconfig.json` 中添加类型声明:**
36
+ 可选地,你可以开启 runtime map 产物(默认关闭):
34
37
 
35
- ```json
36
- {
37
- "compilerOptions": {
38
- "types": ["protobuf-fastdsl/types"]
39
- }
40
- }
38
+ ```ts
39
+ import protobufPlugin from 'protobuf-fastdsl/vite';
40
+
41
+ export default defineConfig({
42
+ plugins: [
43
+ protobufPlugin({
44
+ runtimeMap: {
45
+ enabled: true,
46
+ fileName: 'protobuf-fastdsl.runtime-map.json',
47
+ },
48
+ }),
49
+ ],
50
+ });
41
51
  ```
42
52
 
43
- **3. TypeScript 接口定义 schema:**
53
+ **2. 定义 protobuf schema(TypeScript 接口):**
44
54
 
45
55
  ```ts
46
- interface UserProfile {
56
+ // schema/user.ts
57
+ import type { pb, pb_repeated, uint_32, bool } from 'protobuf-fastdsl';
58
+
59
+ export interface UserProfile {
47
60
  id: pb<1, uint_32>;
48
61
  username: pb<2, string>;
49
62
  active: pb<3, bool>;
@@ -51,9 +64,12 @@ interface UserProfile {
51
64
  }
52
65
  ```
53
66
 
54
- **4. 使用 `protobuf_encode` / `protobuf_decode`:**
67
+ **3. 编解码:**
55
68
 
56
69
  ```ts
70
+ import { protobuf_encode, protobuf_decode } from 'protobuf-fastdsl';
71
+ import type { UserProfile } from './schema/user';
72
+
57
73
  const bytes = protobuf_encode<UserProfile>({
58
74
  id: 42,
59
75
  username: 'alice',
@@ -65,7 +81,7 @@ const user = protobuf_decode<UserProfile>(bytes);
65
81
  // user.id === 42, user.tags === ['admin', 'dev']
66
82
  ```
67
83
 
68
- 构建时,插件会将上述代码转换为:
84
+ 构建时,插件将上述代码转换为:
69
85
 
70
86
  ```js
71
87
  // 预计算的 tag 字面量,内联 varint 循环,零函数调用
@@ -73,6 +89,63 @@ const bytes = protobuf_encode_UserProfile({ id: 42, ... });
73
89
  const user = protobuf_decode_UserProfile(bytes);
74
90
  ```
75
91
 
92
+ 如果忘记配置插件,`protobuf_encode` / `protobuf_decode` 会在运行时抛出错误提示。
93
+
94
+ 如果你在构建阶段启用了 `runtimeMap`,也可以在**非 Vite 运行场景**下显式启用 map 回退(默认关闭):
95
+
96
+ ```ts
97
+ import runtimeMap from './protobuf-fastdsl.runtime-map.json';
98
+ import { protobuf_enableRuntimeMapFallback } from 'protobuf-fastdsl';
99
+
100
+ protobuf_enableRuntimeMapFallback(runtimeMap);
101
+ ```
102
+
103
+ 该回退模式会根据调用栈命中 call-site map,再按需动态生成对应类型及其依赖消息的编解码函数。
104
+
105
+ ## 跨文件类型引用
106
+
107
+ 接口定义和编解码调用可以在不同文件中,插件会自动跟踪 import 链:
108
+
109
+ ```ts
110
+ // inner.ts
111
+ import type { pb, uint_32 } from 'protobuf-fastdsl';
112
+
113
+ export interface Inner {
114
+ value: pb<1, uint_32>;
115
+ }
116
+
117
+ // outer.ts
118
+ import { protobuf_encode } from 'protobuf-fastdsl';
119
+ import type { Inner } from './inner';
120
+ import type { pb } from 'protobuf-fastdsl';
121
+
122
+ interface Outer {
123
+ inner: pb<1, Inner>;
124
+ }
125
+
126
+ const bytes = protobuf_encode<Outer>({ inner: { value: 42 } });
127
+ ```
128
+
129
+ 支持传递性导入(A → B → C),插件会递归解析所有依赖。
130
+
131
+ ## 别名导入
132
+
133
+ ```ts
134
+ import { protobuf_encode as encode, protobuf_decode as decode } from 'protobuf-fastdsl';
135
+
136
+ const bytes = encode<UserProfile>(data); // → protobuf_encode_UserProfile(data)
137
+ const user = decode<UserProfile>(bytes); // → protobuf_decode_UserProfile(bytes)
138
+ ```
139
+
140
+ 也支持 namespace 形式:
141
+
142
+ ```ts
143
+ import * as pb from 'protobuf-fastdsl';
144
+
145
+ const bytes = pb.protobuf_encode<UserProfile>(data);
146
+ const user = pb.protobuf_decode<UserProfile>(bytes);
147
+ ```
148
+
76
149
  ## 泛型单态化
77
150
 
78
151
  定义泛型 protobuf 模板,使用具体类型实例化:
@@ -95,8 +168,8 @@ const data = protobuf_encode<Wrapper<Wrapper<string>>>({
95
168
  |---------------|-----------------|-----------|
96
169
  | `uint_32`, `int_32` | `number` | Varint |
97
170
  | `uint_64`, `int_64` | `bigint` | Varint |
98
- | `sint_32` | `number` | Varint |
99
- | `sint_64` | `bigint` | Varint |
171
+ | `sint_32` | `number` | Varint (ZigZag) |
172
+ | `sint_64` | `bigint` | Varint (ZigZag) |
100
173
  | `bool` | `boolean` | Varint |
101
174
  | `string` | `string` | LengthDelimited |
102
175
  | `bytes` | `Uint8Array` | LengthDelimited |
@@ -111,6 +184,14 @@ const data = protobuf_encode<Wrapper<Wrapper<string>>>({
111
184
 
112
185
  说明:
113
186
  - 所有 64 位整数类型在 TypeScript 中统一映射为 `bigint`
187
+ - `sint_32` / `sint_64` 使用 ZigZag 编码,适用于频繁出现负数的场景
188
+
189
+ ## 包入口
190
+
191
+ | 路径 | 用途 |
192
+ |------|------|
193
+ | `protobuf-fastdsl` | 用户代码 — `protobuf_encode`、`protobuf_decode`、所有类型 |
194
+ | `protobuf-fastdsl/vite` | Vite 插件 |
114
195
 
115
196
  ## ⚡ 性能测试
116
197