@zucker-framework/network 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,22 @@
1
+ Zucker Framework
2
+ Copyright 2026 Lind and Zucker Framework contributors
3
+
4
+ Licensed under the Apache License, Version 2.0. See LICENSE.
5
+
6
+ The framework includes TypeScript adaptations and enhancements of concepts and
7
+ implementations from hsweb-framework and jetlinks-community, both distributed
8
+ under the Apache License, Version 2.0. Individual packages use different subsets
9
+ of the framework. The TypeScript adaptations and Node.js integrations were
10
+ modified for Zucker Framework; they are not the original upstream distribution.
11
+
12
+ hsweb-framework
13
+ https://github.com/hs-web/hsweb-framework
14
+ Copyright 2020 http://hsweb.me
15
+
16
+ jetlinks-community
17
+ https://github.com/jetlinks/jetlinks-community
18
+
19
+ See THIRD_PARTY_NOTICES for license texts and attribution for bundled dependencies
20
+ and reference implementations relevant to this package. External dependencies
21
+ remain subject to their own licenses. This NOTICE is informational and does not
22
+ modify any license or grant rights to upstream trademarks.
@@ -0,0 +1,4 @@
1
+ @zucker-framework/network
2
+
3
+ No separately recorded third-party implementation is bundled in this package.
4
+ External dependencies retain their own licenses; see NOTICE for framework attribution.
@@ -0,0 +1,351 @@
1
+ import { OnModuleDestroy, Provider, DynamicModule } from '@nestjs/common';
2
+
3
+ /**
4
+ * 网络组件类型标识。
5
+ *
6
+ * 对应 jetlinks NetworkType — 用于区分 TCP / HTTP / MQTT / WebSocket 等
7
+ * 不同网络组件,以字符串 id 作为唯一键。
8
+ *
9
+ * 与 jetlinks 的差异:去掉了 Java 枚举字典依赖,改用纯接口 + 工厂函数。
10
+ */
11
+ interface NetworkType {
12
+ /** 类型唯一标识,如 'TCP_SERVER'、'HTTP_CLIENT' */
13
+ readonly id: string;
14
+ /** 可读名称(可选,默认等于 id) */
15
+ readonly name: string;
16
+ }
17
+ /**
18
+ * 用 id 创建一个 NetworkType(等价 jetlinks `NetworkType.of(id)`)。
19
+ */
20
+ declare function networkType(id: string, name?: string): NetworkType;
21
+ /**
22
+ * 内置网络类型常量,对应 jetlinks DefaultNetworkType 的通用子集。
23
+ * IoT 专用类型(CoAP 等)不迁移。
24
+ */
25
+ declare const NetworkTypes: {
26
+ readonly TCP_CLIENT: NetworkType;
27
+ readonly TCP_SERVER: NetworkType;
28
+ readonly HTTP_CLIENT: NetworkType;
29
+ readonly HTTP_SERVER: NetworkType;
30
+ readonly MQTT_CLIENT: NetworkType;
31
+ readonly MQTT_SERVER: NetworkType;
32
+ readonly WEB_SOCKET_CLIENT: NetworkType;
33
+ readonly WEB_SOCKET_SERVER: NetworkType;
34
+ readonly UDP: NetworkType;
35
+ };
36
+ /**
37
+ * 动态类型注册表 — 对齐 jetlinks NetworkTypes 的 register/lookup 模式。
38
+ *
39
+ * 允许运行时注册自定义网络类型(如 CoAP、自定义协议等),
40
+ * 与 jetlinks 中 NetworkTypes.register() 语义一致。
41
+ */
42
+ declare class NetworkTypeRegistry {
43
+ private readonly types;
44
+ constructor();
45
+ /** 注册网络类型 — 对齐 jetlinks NetworkTypes.register() */
46
+ register(type: NetworkType): void;
47
+ /** 批量注册 */
48
+ registerAll(types: NetworkType[]): void;
49
+ /** 根据 ID 查找网络类型 — 对齐 jetlinks NetworkTypes.lookup() */
50
+ lookup(id: string): NetworkType | undefined;
51
+ /** 获取所有已注册的网络类型 — 对齐 jetlinks NetworkTypes.get() */
52
+ getAll(): NetworkType[];
53
+ }
54
+ /** 全局网络类型注册表单例 */
55
+ declare const networkTypeRegistry: NetworkTypeRegistry;
56
+
57
+ /**
58
+ * 传输协议抽象。
59
+ *
60
+ * 对应 jetlinks NetworkTransport(TCP / UDP)以及 NetworkConfig.schema
61
+ * (http / mqtt / ws 等应用层协议)。
62
+ *
63
+ * 在 zucker 中将两层合并为一个 Transport 枚举 + schema 字段,
64
+ * 使消费方只需关心一个类型。
65
+ */
66
+ /** 底层传输协议 */
67
+ type TransportProtocol = 'TCP' | 'UDP';
68
+ /**
69
+ * 传输描述 — 包含底层协议 + 应用层 schema。
70
+ *
71
+ * 例如 MQTT over TCP: `{ protocol: 'TCP', schema: 'mqtt' }`
72
+ */
73
+ interface Transport {
74
+ /** 底层协议:TCP 或 UDP */
75
+ readonly protocol: TransportProtocol;
76
+ /** 应用层 schema,如 http / mqtt / ws */
77
+ readonly schema: string;
78
+ }
79
+ declare function createTransport(protocol: TransportProtocol, schema: string): Transport;
80
+ /** 常见传输常量 */
81
+ declare const Transports: {
82
+ readonly TCP: Transport;
83
+ readonly UDP: Transport;
84
+ readonly HTTP: Transport;
85
+ readonly HTTPS: Transport;
86
+ readonly MQTT: Transport;
87
+ readonly MQTTS: Transport;
88
+ readonly WS: Transport;
89
+ readonly WSS: Transport;
90
+ };
91
+
92
+ /**
93
+ * 网络组件根接口。
94
+ *
95
+ * 对应 jetlinks `Network` — 所有具体网络实现(TCP Server、MQTT Client 等)
96
+ * 的统一抽象。提供生命周期管理(shutdown / isAlive / isAutoReload)。
97
+ */
98
+ interface Network {
99
+ /** 网络组件实例唯一标识 */
100
+ readonly id: string;
101
+ /** 网络类型 */
102
+ readonly type: NetworkType;
103
+ /** 关闭网络组件,释放底层资源 */
104
+ shutdown(): Promise<void> | void;
105
+ /** 组件是否存活 */
106
+ isAlive(): boolean;
107
+ /**
108
+ * 当 isAlive() 返回 false 时,是否由 NetworkManager 自动重新加载。
109
+ * 默认 false。
110
+ */
111
+ isAutoReload(): boolean;
112
+ }
113
+ /**
114
+ * 服务端网络组件,额外暴露绑定地址。
115
+ *
116
+ * 对应 jetlinks `ServerNetwork`。
117
+ */
118
+ interface ServerNetwork extends Network {
119
+ /** 绑定的 host:port;如未绑定返回 undefined */
120
+ getBindAddress(): {
121
+ host: string;
122
+ port: number;
123
+ } | undefined;
124
+ }
125
+
126
+ /**
127
+ * 网络组件配置基础接口。
128
+ *
129
+ * 对应 jetlinks `NetworkConfig` — 抽象出所有网络组件共有的配置属性。
130
+ */
131
+ interface NetworkConfig {
132
+ /** 配置唯一标识 */
133
+ readonly id: string;
134
+ /** 传输协议 */
135
+ readonly transport: Transport;
136
+ /** 是否启用 TLS/DTLS 安全加密 */
137
+ readonly secure: boolean;
138
+ /** 安全证书标识(secure 为 true 时必须) */
139
+ readonly certId?: string;
140
+ }
141
+ /**
142
+ * 服务端网络配置。
143
+ *
144
+ * 对应 jetlinks `ServerNetworkConfig`。
145
+ */
146
+ interface ServerNetworkConfig extends NetworkConfig {
147
+ /** 监听地址 */
148
+ readonly host: string;
149
+ /** 监听端口 */
150
+ readonly port: number;
151
+ }
152
+ /**
153
+ * 客户端网络配置。
154
+ *
155
+ * 对应 jetlinks `ClientNetworkConfig`。
156
+ */
157
+ interface ClientNetworkConfig extends NetworkConfig {
158
+ /** 远端地址 */
159
+ readonly remoteHost: string;
160
+ /** 远端端口 */
161
+ readonly remotePort: number;
162
+ }
163
+ /**
164
+ * 通用可序列化的网络属性,对应 jetlinks `NetworkProperties`。
165
+ *
166
+ * 与 jetlinks 完全对齐:type + configurations map 的组合
167
+ * 让 NetworkProvider 可以按需反序列化成具体 Config。
168
+ */
169
+ interface NetworkProperties {
170
+ /** 配置 ID */
171
+ id: string;
172
+ /** 网络类型标识 */
173
+ type: string;
174
+ /** 配置名称 */
175
+ name: string;
176
+ /** 是否启用 */
177
+ enabled: boolean;
178
+ /** 自由 KV 配置 — 不同网络类型内容不同 */
179
+ configurations: Record<string, unknown>;
180
+ }
181
+
182
+ /**
183
+ * 网络组件提供商 SPI。
184
+ *
185
+ * 对应 jetlinks `NetworkProvider<P>` — 每种网络类型注册一个 Provider,
186
+ * 负责根据配置创建/重载网络实例。
187
+ *
188
+ * 泛型 P 为具体的配置类型(如 TcpServerConfig、MqttClientConfig)。
189
+ *
190
+ * 与 jetlinks 的差异:
191
+ * - 去掉 Reactor Mono,使用 async/Promise
192
+ * - 去掉 ConfigMetadata(Java UI 元数据,在 TS 中用 JSON Schema 替代)
193
+ */
194
+ interface NetworkProvider<P = unknown> {
195
+ /** 该 Provider 支持的网络类型 */
196
+ readonly type: NetworkType;
197
+ /** 根据具体配置创建网络组件 */
198
+ createNetwork(config: P): Promise<Network>;
199
+ /** 重载已有网络组件(热更新配置) */
200
+ reload(network: Network, config: P): Promise<Network>;
201
+ /**
202
+ * 将通用 NetworkProperties 转换为具体配置对象 P。
203
+ * 对应 jetlinks `NetworkProvider.createConfig(NetworkProperties)`。
204
+ */
205
+ createConfig(properties: NetworkProperties): Promise<P>;
206
+ /**
207
+ * 网络组件是否可被多个消费者复用。
208
+ *
209
+ * 例如 TCP Server 通常不可复用(一个端口只能被一个网关占用),
210
+ * 而 HTTP Client 可复用。
211
+ * 默认 false。
212
+ */
213
+ isReusable(): boolean;
214
+ }
215
+
216
+ /**
217
+ * 网络组件配置管理接口 — 用于从持久层获取 NetworkProperties。
218
+ *
219
+ * 对应 jetlinks `NetworkConfigManager`。
220
+ * 具体实现由应用层提供(数据库、文件等)。
221
+ */
222
+ interface NetworkConfigManager {
223
+ getConfig(type: NetworkType, id: string): Promise<NetworkProperties | undefined>;
224
+ }
225
+ /**
226
+ * 网络管理器接口。
227
+ *
228
+ * 对应 jetlinks `NetworkManager` — 统一管理多种类型的网络组件实例,
229
+ * 提供按类型+ID 获取、重载、关闭、销毁等操作。
230
+ */
231
+ interface INetworkManager {
232
+ /** 按类型和 ID 获取网络组件(懒创建) */
233
+ getNetwork<T extends Network = Network>(type: NetworkType, id: string): Promise<T>;
234
+ /** 获取所有已创建的网络组件 */
235
+ getNetworks(): Network[];
236
+ /** 获取所有已注册的 Provider */
237
+ getProviders(): NetworkProvider[];
238
+ /** 按类型 ID 获取 Provider */
239
+ getProvider(typeId: string): NetworkProvider | undefined;
240
+ /** 重载指定网络组件 */
241
+ reload(type: NetworkType, id: string): Promise<void>;
242
+ /** 关闭指定网络组件(不移除缓存) */
243
+ shutdown(type: NetworkType, id: string): Promise<void>;
244
+ /** 销毁指定网络组件(关闭并移除缓存) */
245
+ destroy(type: NetworkType, id: string): Promise<void>;
246
+ }
247
+ /**
248
+ * 默认网络管理器实现。
249
+ *
250
+ * 对应 jetlinks `DefaultNetworkManager` — 通过 Provider SPI 按需创建
251
+ * 网络组件实例,内部按 type + id 缓存。
252
+ *
253
+ * 与 jetlinks 的差异:
254
+ * - 去掉了 Spring BeanPostProcessor 自动发现,改为手动 registerProvider
255
+ * - 去掉了 EventBus 集群同步(zucker 已有 @zucker-framework/cluster)
256
+ * - 健康检查用 setInterval 而非 Flux.interval
257
+ */
258
+ declare class DefaultNetworkManager implements INetworkManager, OnModuleDestroy {
259
+ private readonly configManager;
260
+ private readonly logger;
261
+ /** type-id -> Provider */
262
+ private readonly providers;
263
+ /** type-id -> (network-id -> Network) */
264
+ private readonly store;
265
+ private checkTimer;
266
+ constructor(configManager: NetworkConfigManager);
267
+ registerProvider(provider: NetworkProvider<unknown>): void;
268
+ getNetwork<T extends Network = Network>(type: NetworkType, id: string): Promise<T>;
269
+ getNetworks(): Network[];
270
+ getProviders(): NetworkProvider[];
271
+ getProvider(typeId: string): NetworkProvider | undefined;
272
+ reload(type: NetworkType, id: string): Promise<void>;
273
+ shutdown(type: NetworkType, id: string): Promise<void>;
274
+ destroy(type: NetworkType, id: string): Promise<void>;
275
+ onModuleDestroy(): Promise<void>;
276
+ private getNetworkStore;
277
+ private requireProvider;
278
+ private createFromConfig;
279
+ /**
280
+ * 定期检查网络组件存活状态 — 与 jetlinks checkNetwork() 逻辑对齐。
281
+ */
282
+ private checkNetworks;
283
+ }
284
+
285
+ /**
286
+ * 消息编解码器抽象。
287
+ *
288
+ * 对应 jetlinks `DeviceMessageCodec` 的通用子集 — 去掉了 IoT 设备
289
+ * 消息特定部分,保留通用的 encode / decode / transport 关联。
290
+ *
291
+ * 在 zucker 中,Codec 用于任意协议的消息序列化/反序列化,
292
+ * 例如 MQTT payload 解析、TCP 帧编解码等。
293
+ */
294
+
295
+ /**
296
+ * 编解码上下文 — 携带编解码时可能需要的环境信息。
297
+ */
298
+ interface CodecContext {
299
+ /** 当前网络组件 ID */
300
+ readonly networkId?: string;
301
+ /** 额外上下文 KV */
302
+ readonly attributes: Record<string, unknown>;
303
+ }
304
+ /**
305
+ * 消息编解码器。
306
+ *
307
+ * 泛型参数:
308
+ * - TEncoded : 编码后的格式(如 Buffer、string)
309
+ * - TDecoded : 解码后的业务对象
310
+ */
311
+ interface Codec<TEncoded = Buffer, TDecoded = unknown> {
312
+ /** 编解码器标识 */
313
+ readonly id: string;
314
+ /** 该编解码器绑定的传输协议 */
315
+ readonly transport: Transport;
316
+ /** 将业务对象编码为传输格式 */
317
+ encode(message: TDecoded, context?: CodecContext): Promise<TEncoded> | TEncoded;
318
+ /** 将传输格式解码为业务对象 */
319
+ decode(payload: TEncoded, context?: CodecContext): Promise<TDecoded> | TDecoded;
320
+ }
321
+ /**
322
+ * JSON 编解码器 — 最常见的实现,将对象序列化为 JSON Buffer。
323
+ */
324
+ declare class JsonCodec<T = unknown> implements Codec<Buffer, T> {
325
+ readonly id: string;
326
+ readonly transport: Transport;
327
+ constructor(id: string, transport: Transport);
328
+ encode(message: T): Buffer;
329
+ decode(payload: Buffer): T;
330
+ }
331
+
332
+ declare const NETWORK_MANAGER: unique symbol;
333
+ declare const NETWORK_CONFIG_MANAGER: unique symbol;
334
+ /**
335
+ * Zucker 网络模块。
336
+ *
337
+ * 使用 `forRoot` 注册时,需提供 `NetworkConfigManager` 实现。
338
+ *
339
+ * ```ts
340
+ * ZuckerNetworkModule.forRoot({
341
+ * configManager: MyNetworkConfigManager,
342
+ * })
343
+ * ```
344
+ */
345
+ declare class ZuckerNetworkModule {
346
+ static forRoot(options: {
347
+ configManager: Provider;
348
+ }): DynamicModule;
349
+ }
350
+
351
+ export { type ClientNetworkConfig, type Codec, type CodecContext, DefaultNetworkManager, type INetworkManager, JsonCodec, NETWORK_CONFIG_MANAGER, NETWORK_MANAGER, type Network, type NetworkConfig, type NetworkConfigManager, type NetworkProperties, type NetworkProvider, type NetworkType, NetworkTypes, type ServerNetwork, type ServerNetworkConfig, type Transport, type TransportProtocol, Transports, ZuckerNetworkModule, createTransport, networkType, networkTypeRegistry };