protobuf-fastdsl 0.1.5 → 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 +37 -0
- package/dist/chunk-ZIB6RCYI.js +947 -0
- package/dist/index.d.ts +40 -46
- package/dist/index.js +326 -997
- package/dist/runtime-map-DxZF0_WT.d.ts +54 -0
- package/dist/runtime.d.ts +8 -5
- package/dist/runtime.js +143 -0
- package/package.json +8 -3
- package/protobuf.d.ts +36 -0
package/README.md
CHANGED
|
@@ -33,6 +33,23 @@ export default defineConfig({
|
|
|
33
33
|
});
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
+
可选地,你可以开启 runtime map 产物(默认关闭):
|
|
37
|
+
|
|
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
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
36
53
|
**2. 定义 protobuf schema(TypeScript 接口):**
|
|
37
54
|
|
|
38
55
|
```ts
|
|
@@ -74,6 +91,17 @@ const user = protobuf_decode_UserProfile(bytes);
|
|
|
74
91
|
|
|
75
92
|
如果忘记配置插件,`protobuf_encode` / `protobuf_decode` 会在运行时抛出错误提示。
|
|
76
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
|
+
|
|
77
105
|
## 跨文件类型引用
|
|
78
106
|
|
|
79
107
|
接口定义和编解码调用可以在不同文件中,插件会自动跟踪 import 链:
|
|
@@ -109,6 +137,15 @@ const bytes = encode<UserProfile>(data); // → protobuf_encode_UserProfile(dat
|
|
|
109
137
|
const user = decode<UserProfile>(bytes); // → protobuf_decode_UserProfile(bytes)
|
|
110
138
|
```
|
|
111
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
|
+
|
|
112
149
|
## 泛型单态化
|
|
113
150
|
|
|
114
151
|
定义泛型 protobuf 模板,使用具体类型实例化:
|