@thejrsoft/subway-protocol 1.3.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.
Files changed (78) hide show
  1. package/ACK_MESSAGES_IMPLEMENTATION_SUMMARY.md +128 -0
  2. package/ACK_MESSAGE_DESIGN.md +457 -0
  3. package/CHANGELOG.md +58 -0
  4. package/COMMAND_VALIDATION_RULES.md +178 -0
  5. package/DOCUMENTATION_REORGANIZATION_SUMMARY.md +81 -0
  6. package/DOCUMENTATION_STRUCTURE.md +106 -0
  7. package/GATEWAY_MIGRATION_GUIDE.md +130 -0
  8. package/GATEWAY_PROTOCOL_COMPARISON.md +216 -0
  9. package/INTEGRATION_GUIDE.md +190 -0
  10. package/OPTIONAL_FIELDS_WITHOUT_DEFAULTS.md +97 -0
  11. package/PROTOCOL_UTILS_USAGE.md +278 -0
  12. package/README.md +237 -0
  13. package/TYPE_FIXES_SUMMARY.md +210 -0
  14. package/UPDATE_ENUM_VALUES.md +139 -0
  15. package/dist/asyncapi-sync.d.ts +47 -0
  16. package/dist/asyncapi-sync.d.ts.map +1 -0
  17. package/dist/asyncapi-sync.js +85 -0
  18. package/dist/asyncapi-sync.js.map +1 -0
  19. package/dist/command-factory.d.ts +62 -0
  20. package/dist/command-factory.d.ts.map +1 -0
  21. package/dist/command-factory.js +137 -0
  22. package/dist/command-factory.js.map +1 -0
  23. package/dist/command-types.d.ts +27 -0
  24. package/dist/command-types.d.ts.map +1 -0
  25. package/dist/command-types.js +31 -0
  26. package/dist/command-types.js.map +1 -0
  27. package/dist/index.d.ts +403 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +413 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/message-validator.d.ts +102 -0
  32. package/dist/message-validator.d.ts.map +1 -0
  33. package/dist/message-validator.js +640 -0
  34. package/dist/message-validator.js.map +1 -0
  35. package/dist/protocol-utils.d.ts +108 -0
  36. package/dist/protocol-utils.d.ts.map +1 -0
  37. package/dist/protocol-utils.js +293 -0
  38. package/dist/protocol-utils.js.map +1 -0
  39. package/docs/01-protocol/README.md +45 -0
  40. package/docs/01-protocol/design-rationale.md +198 -0
  41. package/docs/01-protocol/message-types.md +669 -0
  42. package/docs/01-protocol/specification.md +1466 -0
  43. package/docs/02-commands/README.md +56 -0
  44. package/docs/02-commands/batch-command.md +435 -0
  45. package/docs/02-commands/complex-command.md +537 -0
  46. package/docs/02-commands/simple-command.md +332 -0
  47. package/docs/02-commands/typed-commands.md +362 -0
  48. package/docs/03-architecture/README.md +66 -0
  49. package/docs/03-architecture/device-protocol.md +430 -0
  50. package/docs/03-architecture/edge-proxy.md +727 -0
  51. package/docs/03-architecture/routing-flow.md +893 -0
  52. package/docs/04-integration/README.md +144 -0
  53. package/docs/04-integration/backend-guide.md +551 -0
  54. package/docs/04-integration/edge-guide.md +684 -0
  55. package/docs/04-integration/gateway-guide.md +180 -0
  56. package/docs/04-integration/migration-guide.md +226 -0
  57. package/docs/05-examples/README.md +141 -0
  58. package/docs/05-examples/progress-update-examples.md +757 -0
  59. package/docs/06-reference/README.md +67 -0
  60. package/docs/06-reference/api.md +572 -0
  61. package/docs/06-reference/faq.md +302 -0
  62. package/docs/06-reference/glossary.md +232 -0
  63. package/examples/backend-upgrade.ts +279 -0
  64. package/examples/edge-multi-device.ts +513 -0
  65. package/examples/gateway-upgrade.ts +150 -0
  66. package/examples/protocol-implementation.ts +715 -0
  67. package/package.json +48 -0
  68. package/scripts/validate-asyncapi.ts +78 -0
  69. package/src/__tests__/protocol.test.ts +297 -0
  70. package/src/asyncapi-sync.ts +84 -0
  71. package/src/command-factory.ts +183 -0
  72. package/src/command-types.ts +72 -0
  73. package/src/edge-proxy.ts +494 -0
  74. package/src/gateway-extensions.ts +278 -0
  75. package/src/index.ts +792 -0
  76. package/src/message-validator.ts +726 -0
  77. package/src/protocol-utils.ts +355 -0
  78. package/tsconfig.json +24 -0
@@ -0,0 +1,278 @@
1
+ /**
2
+ * Gateway 特定的协议扩展
3
+ * 基于 PROTOCOL_SPECIFICATION.md v2.0
4
+ */
5
+
6
+ import {
7
+ BaseMessage,
8
+ MessageType,
9
+ Priority,
10
+ Command,
11
+ CommandMessage,
12
+ CommandType
13
+ } from './index';
14
+
15
+ // Gateway 特殊 clientId
16
+ export const GATEWAY_SITE_ID = 'gateway';
17
+
18
+
19
+ // 批量消息
20
+ export interface BatchMessage extends BaseMessage {
21
+ type: 'batch';
22
+ messages: BaseMessage[];
23
+ }
24
+
25
+
26
+ // 认证信息
27
+ export interface AuthInfo {
28
+ method: 'token' | 'certificate' | 'apikey';
29
+ credentials: string;
30
+ }
31
+
32
+ // 扩展的注册消息(支持认证)
33
+ export interface ExtendedRegisterMessage extends BaseMessage {
34
+ type: MessageType.REGISTER;
35
+ clientId: string;
36
+ clientType: string;
37
+ clientInfo?: {
38
+ version?: string;
39
+ platform?: string;
40
+ capabilities?: string[];
41
+ };
42
+ auth?: AuthInfo; // 认证信息
43
+ features?: string[]; // 支持的特性
44
+ }
45
+
46
+ // 进度更新详情
47
+ export interface ProgressDetails {
48
+ bytesDownloaded?: number;
49
+ totalBytes?: number;
50
+ speed?: string;
51
+ estimatedTimeRemaining?: number;
52
+ }
53
+
54
+ // 扩展的进度更新消息
55
+ export interface ExtendedProgressUpdateMessage extends BaseMessage {
56
+ type: MessageType.PROGRESS_UPDATE;
57
+ requestRef: string;
58
+ progress: number;
59
+ phase?: string;
60
+ message?: string;
61
+ details?: ProgressDetails;
62
+ }
63
+
64
+ // Gateway 命令代码
65
+ export enum GatewayCommand {
66
+ // 设备管理
67
+ GET_DEVICE_STATUS = 'GET_DEVICE_STATUS',
68
+ LIST_DEVICES = 'LIST_DEVICES',
69
+ DISCONNECT_DEVICE = 'DISCONNECT_DEVICE',
70
+
71
+ // 统计信息
72
+ GET_STATS = 'GET_STATS',
73
+ GET_METRICS = 'GET_METRICS',
74
+
75
+ // 配置管理
76
+ GET_GATEWAY_CONFIG = 'GET_GATEWAY_CONFIG',
77
+ UPDATE_GATEWAY_CONFIG = 'UPDATE_GATEWAY_CONFIG',
78
+
79
+ // 路由管理
80
+ GET_ROUTES = 'GET_ROUTES',
81
+ ADD_ROUTE = 'ADD_ROUTE',
82
+ REMOVE_ROUTE = 'REMOVE_ROUTE'
83
+ }
84
+
85
+ // 设备信息
86
+ export interface DeviceInfo {
87
+ clientId: string;
88
+ clientType: string;
89
+ status: 'online' | 'offline';
90
+ connectedAt: string;
91
+ lastHeartbeat: string;
92
+ clientInfo?: Record<string, any>;
93
+ statistics?: {
94
+ commandsSent: number;
95
+ commandsReceived: number;
96
+ errors: number;
97
+ uptime: number;
98
+ };
99
+ }
100
+
101
+ // Gateway 统计信息
102
+ export interface GatewayStats {
103
+ uptime: number;
104
+ connections: {
105
+ total: number;
106
+ byType: Record<string, number>;
107
+ byStatus: Record<string, number>;
108
+ };
109
+ messages: {
110
+ received: number;
111
+ sent: number;
112
+ errors: number;
113
+ byType: Record<string, number>;
114
+ };
115
+ performance: {
116
+ averageLatency: number;
117
+ messageRate: number;
118
+ errorRate: number;
119
+ };
120
+ }
121
+
122
+ // 消息签名
123
+ export interface SignedMessage extends BaseMessage {
124
+ signature?: string; // 消息签名
125
+ signatureMethod?: 'sha256' | 'sha512';
126
+ }
127
+
128
+ // Gateway 工具类
129
+ export class GatewayUtils {
130
+ /**
131
+ * 检查是否为 Gateway 命令
132
+ */
133
+ static isGatewayCommand(message: CommandMessage): boolean {
134
+ return message.targetClientId === GATEWAY_SITE_ID;
135
+ }
136
+
137
+ /**
138
+ * 检查是否为批量命令
139
+ */
140
+ static isBatchCommand(message: CommandMessage): boolean {
141
+ return message.command?.commandType === CommandType.BATCH;
142
+ }
143
+
144
+ /**
145
+ * 检查命令类型
146
+ */
147
+ static getCommandType(message: CommandMessage): CommandType {
148
+ // 从 command 对象中获取 commandType
149
+ if (message.command?.commandType) {
150
+ return message.command.commandType as CommandType;
151
+ }
152
+ // 如果有 filter 或 clientId 为 *,则为 batch 类型
153
+ if (message.filter || message.clientId === BROADCAST_SITE_ID) {
154
+ return CommandType.BATCH;
155
+ }
156
+ // 默认为 simple 类型
157
+ return CommandType.SIMPLE;
158
+ }
159
+
160
+ /**
161
+ * 创建查询设备状态的命令
162
+ */
163
+ static createDeviceStatusQuery(
164
+ targetClientId: string,
165
+ requestRef: string
166
+ ): CommandMessage {
167
+ return {
168
+ type: MessageType.COMMAND,
169
+ requestRef,
170
+ targetClientId: GATEWAY_SITE_ID,
171
+ command: {
172
+ commandType: CommandType.SIMPLE,
173
+ commandCode: GatewayCommand.GET_DEVICE_STATUS,
174
+ deviceType: 'gateway',
175
+ deviceId: 0,
176
+ operationType: 'read',
177
+ parameters: { targetClientId }
178
+ },
179
+ priority: Priority.NORMAL,
180
+ timeout: 5000,
181
+ timestamp: new Date().toISOString(),
182
+ version: '1.0'
183
+ };
184
+ }
185
+
186
+ /**
187
+ * 创建列出设备的命令
188
+ */
189
+ static createListDevicesCommand(
190
+ filter?: DeviceFilter,
191
+ requestRef?: string
192
+ ): CommandMessage {
193
+ return {
194
+ type: MessageType.COMMAND,
195
+ requestRef: requestRef || `list-${Date.now()}`,
196
+ targetClientId: GATEWAY_SITE_ID,
197
+ command: {
198
+ commandType: CommandType.SIMPLE,
199
+ commandCode: GatewayCommand.LIST_DEVICES,
200
+ deviceType: 'gateway',
201
+ operationType: 'read',
202
+ parameters: { filter: filter || {} }
203
+ },
204
+ priority: Priority.NORMAL,
205
+ timeout: 5000,
206
+ timestamp: new Date().toISOString(),
207
+ version: '1.0'
208
+ };
209
+ }
210
+
211
+
212
+ /**
213
+ * 创建complex类型命令(持续响应)
214
+ */
215
+ static createComplexCommand(
216
+ targetClientId: string,
217
+ command: Command,
218
+ options?: {
219
+ priority?: Priority;
220
+ timeout?: number;
221
+ requestRef?: string;
222
+ }
223
+ ): CommandMessage {
224
+ return {
225
+ type: MessageType.COMMAND,
226
+ requestRef: options?.requestRef || `complex-${Date.now()}`,
227
+ targetClientId,
228
+ command: {
229
+ ...command,
230
+ commandType: CommandType.COMPLEX
231
+ },
232
+ priority: options?.priority || Priority.NORMAL,
233
+ timeout: options?.timeout || 30000, // Complex命令通常需要更长时间
234
+ timestamp: new Date().toISOString(),
235
+ version: '1.0'
236
+ };
237
+ }
238
+
239
+ /**
240
+ * 签名消息
241
+ */
242
+ static signMessage(
243
+ message: BaseMessage,
244
+ secret: string,
245
+ method: 'sha256' | 'sha512' = 'sha256'
246
+ ): SignedMessage {
247
+ // 实际实现需要加密库
248
+ const signature = `${method}:mock-signature-${Date.now()}`;
249
+
250
+ return {
251
+ ...message,
252
+ signature,
253
+ signatureMethod: method
254
+ };
255
+ }
256
+
257
+ /**
258
+ * 验证消息签名
259
+ */
260
+ static verifyMessageSignature(
261
+ message: SignedMessage,
262
+ secret: string
263
+ ): boolean {
264
+ if (!message.signature || !message.signatureMethod) {
265
+ return false;
266
+ }
267
+
268
+ // 实际实现需要验证签名
269
+ return true;
270
+ }
271
+ }
272
+
273
+ // 导出所有扩展类型
274
+ export type GatewayMessage =
275
+ | ExtendedCommandMessage
276
+ | ExtendedRegisterMessage
277
+ | ExtendedProgressUpdateMessage
278
+ | BatchMessage;