@jiangtaste/baiwei-sdk 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.
Files changed (61) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +360 -0
  3. package/dist/core/BaiweiClient.d.ts +38 -0
  4. package/dist/core/BaiweiClient.js +94 -0
  5. package/dist/core/BaiweiSession.d.ts +56 -0
  6. package/dist/core/BaiweiSession.js +229 -0
  7. package/dist/core/Client.d.ts +74 -0
  8. package/dist/core/Client.js +127 -0
  9. package/dist/core/MessageAssembler.d.ts +17 -0
  10. package/dist/core/MessageAssembler.js +76 -0
  11. package/dist/core/PendingRequestStore.d.ts +18 -0
  12. package/dist/core/PendingRequestStore.js +33 -0
  13. package/dist/core/Session.d.ts +43 -0
  14. package/dist/core/Session.js +330 -0
  15. package/dist/core/SessionConnector.d.ts +14 -0
  16. package/dist/core/SessionConnector.js +62 -0
  17. package/dist/index.d.ts +11 -0
  18. package/dist/index.js +27 -0
  19. package/dist/services/BaseService.d.ts +11 -0
  20. package/dist/services/BaseService.js +17 -0
  21. package/dist/services/ControlService.d.ts +33 -0
  22. package/dist/services/ControlService.js +67 -0
  23. package/dist/services/DeviceService.d.ts +26 -0
  24. package/dist/services/DeviceService.js +51 -0
  25. package/dist/services/GatewayService.d.ts +11 -0
  26. package/dist/services/GatewayService.js +33 -0
  27. package/dist/services/RoomService.d.ts +11 -0
  28. package/dist/services/RoomService.js +27 -0
  29. package/dist/services/SceneService.d.ts +12 -0
  30. package/dist/services/SceneService.js +39 -0
  31. package/dist/services/UserService.d.ts +21 -0
  32. package/dist/services/UserService.js +70 -0
  33. package/dist/transport/TcpClient.d.ts +38 -0
  34. package/dist/transport/TcpClient.js +315 -0
  35. package/dist/types/device-catalog.d.ts +42 -0
  36. package/dist/types/device-catalog.js +16 -0
  37. package/dist/types/device-state.d.ts +56 -0
  38. package/dist/types/device-state.js +2 -0
  39. package/dist/types/index.d.ts +8 -0
  40. package/dist/types/index.js +24 -0
  41. package/dist/types/messages.d.ts +53 -0
  42. package/dist/types/messages.js +32 -0
  43. package/dist/types/options.d.ts +27 -0
  44. package/dist/types/options.js +7 -0
  45. package/dist/types/room.d.ts +9 -0
  46. package/dist/types/room.js +2 -0
  47. package/dist/types/scene.d.ts +22 -0
  48. package/dist/types/scene.js +2 -0
  49. package/dist/types/user.d.ts +17 -0
  50. package/dist/types/user.js +2 -0
  51. package/dist/types.d.ts +212 -0
  52. package/dist/types.js +50 -0
  53. package/dist/utils/IdGenerator.d.ts +19 -0
  54. package/dist/utils/IdGenerator.js +49 -0
  55. package/dist/utils/MessageIdGenerator.d.ts +4 -0
  56. package/dist/utils/MessageIdGenerator.js +12 -0
  57. package/dist/utils/logger.d.ts +33 -0
  58. package/dist/utils/logger.js +115 -0
  59. package/dist/utils/time.d.ts +2 -0
  60. package/dist/utils/time.js +14 -0
  61. package/package.json +45 -0
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ /** 对外统一导出的类型入口。 */
2
+ export * from "./device-catalog";
3
+ export * from "./device-state";
4
+ export * from "./messages";
5
+ export * from "./options";
6
+ export * from "./room";
7
+ export * from "./scene";
8
+ export * from "./user";
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ /** 对外统一导出的类型入口。 */
18
+ __exportStar(require("./device-catalog"), exports);
19
+ __exportStar(require("./device-state"), exports);
20
+ __exportStar(require("./messages"), exports);
21
+ __exportStar(require("./options"), exports);
22
+ __exportStar(require("./room"), exports);
23
+ __exportStar(require("./scene"), exports);
24
+ __exportStar(require("./user"), exports);
@@ -0,0 +1,53 @@
1
+ /** 协议消息的枚举与基础结构定义。 */
2
+ export declare enum MsgClass {
3
+ GATEWAY_MGMT = "gateway_mgmt",
4
+ USER_MGMT = "user_mgmt",
5
+ CONTROL_MGMT = "control_mgmt",
6
+ DEVICE_MGMT = "device_mgmt",
7
+ ROOM_MGMT = "room_mgmt",
8
+ SCENE_MGMT = "scene_mgmt"
9
+ }
10
+ export declare enum MsgName {
11
+ ZB_NET_OPEN = "zb_net_open",
12
+ USER_LOGIN = "user_login",
13
+ DEVICE_CONTROL = "device_control",
14
+ DEVICE_STATE_GET = "device_state_get",
15
+ DEVICE_STATE_REPORT = "device_state_report",
16
+ DEVICE_QUERY = "device_query",
17
+ ROOM_QUERY = "room_query",
18
+ SCENE_LIST = "scene_list",
19
+ SCENE_CALL = "scene_call"
20
+ }
21
+ export declare enum MsgType {
22
+ GET = "get",
23
+ SET = "set",
24
+ RESPONSE = "response",
25
+ REPORT = "report"
26
+ }
27
+ export interface BaiweiMessageBase {
28
+ api_version: string;
29
+ appId?: string;
30
+ from: string;
31
+ to: string;
32
+ msg_class: MsgClass;
33
+ msg_name: MsgName;
34
+ msg_type: MsgType;
35
+ msg_id: string;
36
+ token?: string;
37
+ end?: number;
38
+ status?: number;
39
+ }
40
+ export type BaiweiMessage<T = Record<string, any>> = BaiweiMessageBase & T;
41
+ export type MessageRequest<TPayload = Record<string, any>> = {
42
+ msgClass: MsgClass;
43
+ msgName: MsgName;
44
+ msgType?: MsgType;
45
+ payload?: TPayload;
46
+ };
47
+ export type MessageResponse<TPayload = Record<string, any>> = {
48
+ msgId: string;
49
+ msgClass: MsgClass;
50
+ msgName: MsgName;
51
+ msgType: MsgType;
52
+ payload: TPayload;
53
+ };
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MsgType = exports.MsgName = exports.MsgClass = void 0;
4
+ /** 协议消息的枚举与基础结构定义。 */
5
+ var MsgClass;
6
+ (function (MsgClass) {
7
+ MsgClass["GATEWAY_MGMT"] = "gateway_mgmt";
8
+ MsgClass["USER_MGMT"] = "user_mgmt";
9
+ MsgClass["CONTROL_MGMT"] = "control_mgmt";
10
+ MsgClass["DEVICE_MGMT"] = "device_mgmt";
11
+ MsgClass["ROOM_MGMT"] = "room_mgmt";
12
+ MsgClass["SCENE_MGMT"] = "scene_mgmt";
13
+ })(MsgClass || (exports.MsgClass = MsgClass = {}));
14
+ var MsgName;
15
+ (function (MsgName) {
16
+ MsgName["ZB_NET_OPEN"] = "zb_net_open";
17
+ MsgName["USER_LOGIN"] = "user_login";
18
+ MsgName["DEVICE_CONTROL"] = "device_control";
19
+ MsgName["DEVICE_STATE_GET"] = "device_state_get";
20
+ MsgName["DEVICE_STATE_REPORT"] = "device_state_report";
21
+ MsgName["DEVICE_QUERY"] = "device_query";
22
+ MsgName["ROOM_QUERY"] = "room_query";
23
+ MsgName["SCENE_LIST"] = "scene_list";
24
+ MsgName["SCENE_CALL"] = "scene_call";
25
+ })(MsgName || (exports.MsgName = MsgName = {}));
26
+ var MsgType;
27
+ (function (MsgType) {
28
+ MsgType["GET"] = "get";
29
+ MsgType["SET"] = "set";
30
+ MsgType["RESPONSE"] = "response";
31
+ MsgType["REPORT"] = "report";
32
+ })(MsgType || (exports.MsgType = MsgType = {}));
@@ -0,0 +1,27 @@
1
+ import { Logger } from "../utils/logger";
2
+ /** SDK 运行参数与默认值定义。 */
3
+ export declare const DEFAULT_APP_ID = "010";
4
+ export declare const DEFAULT_CLIENT_ID = "baiwei-sdk";
5
+ export declare const DEFAULT_TIMEOUT_MS = 5000;
6
+ export interface BaiweiClientOptions {
7
+ host: string;
8
+ port: number;
9
+ gatewaySN: string;
10
+ clientId?: string;
11
+ timeoutMs?: number;
12
+ logger?: Logger;
13
+ debug?: boolean;
14
+ }
15
+ export interface BaiweiSessionOptions extends BaiweiClientOptions {
16
+ clientId: string;
17
+ timeoutMs: number;
18
+ }
19
+ export interface BaiweiTcpOptions extends BaiweiSessionOptions {
20
+ autoReconnect?: boolean;
21
+ reconnectInterval?: number;
22
+ maxPacketSize?: number;
23
+ maxCacheSize?: number;
24
+ keepAlive?: boolean;
25
+ keepAliveInitialDelay?: number;
26
+ noDelay?: boolean;
27
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_TIMEOUT_MS = exports.DEFAULT_CLIENT_ID = exports.DEFAULT_APP_ID = void 0;
4
+ /** SDK 运行参数与默认值定义。 */
5
+ exports.DEFAULT_APP_ID = "010";
6
+ exports.DEFAULT_CLIENT_ID = "baiwei-sdk";
7
+ exports.DEFAULT_TIMEOUT_MS = 5_000;
@@ -0,0 +1,9 @@
1
+ /** 房间相关的数据结构。 */
2
+ export interface Room {
3
+ id: number;
4
+ name: string;
5
+ create_time?: string;
6
+ }
7
+ export interface RoomList {
8
+ room_list?: Room[];
9
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,22 @@
1
+ import { BaiweiDeviceState } from "./device-state";
2
+ /** 场景相关的数据结构。 */
3
+ export interface SceneList {
4
+ scene_list: Scene[];
5
+ }
6
+ export interface Scene {
7
+ id: number;
8
+ name: string;
9
+ room_id: number;
10
+ delay: number;
11
+ type: number;
12
+ device_id: number;
13
+ picture_id: number;
14
+ status: number;
15
+ create_time: string;
16
+ instruct_list: SceneInstructList<any>[];
17
+ }
18
+ export interface SceneInstructList<T> extends BaiweiDeviceState<T> {
19
+ id: number;
20
+ type: number;
21
+ delay: number;
22
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,17 @@
1
+ /** 登录接口返回的用户信息。 */
2
+ export interface AdminLoginOptions {
3
+ userPwd?: string;
4
+ }
5
+ export interface UserPasswordLoginOptions {
6
+ userName: string;
7
+ userPwd: string;
8
+ }
9
+ export type UserLoginOptions = AdminLoginOptions | UserPasswordLoginOptions;
10
+ /** 登录接口返回的用户信息。 */
11
+ export interface UserResponse {
12
+ user: {
13
+ type: number;
14
+ need_sync_time: number;
15
+ token: string;
16
+ };
17
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,212 @@
1
+ import { Logger } from "./utils/logger";
2
+ export interface BaiweiClientOptions {
3
+ host: string;
4
+ port: number;
5
+ gatewaySN: string;
6
+ clientId?: string;
7
+ timeoutMs?: number;
8
+ logger?: Logger;
9
+ debug?: boolean;
10
+ }
11
+ export interface BaiweiSessionOptions extends BaiweiClientOptions {
12
+ clientId: string;
13
+ }
14
+ export interface BaiweiTcpOptions extends BaiweiSessionOptions {
15
+ /** Auto reconnect after unexpected close/error (default: true) */
16
+ autoReconnect?: boolean;
17
+ /** Reconnect interval in ms (default: 5000) */
18
+ reconnectInterval?: number;
19
+ /** Max allowed packet size in bytes (default: 65535, protocol hard limit) */
20
+ maxPacketSize?: number;
21
+ /** Max allowed cache size in bytes (default: 4x packet size) */
22
+ maxCacheSize?: number;
23
+ /** Enable TCP keep-alive (default: true) */
24
+ keepAlive?: boolean;
25
+ /** Initial delay for keep-alive probes in ms (default: 15000) */
26
+ keepAliveInitialDelay?: number;
27
+ /** Disable Nagle (default: true) */
28
+ noDelay?: boolean;
29
+ }
30
+ export interface BaiweiMessageBase {
31
+ api_version: string;
32
+ appId?: string;
33
+ from: string;
34
+ to: string;
35
+ msg_class: MsgClass;
36
+ msg_name: MsgName;
37
+ msg_type: MsgType;
38
+ msg_id: string;
39
+ token?: string;
40
+ end?: number;
41
+ status?: number;
42
+ }
43
+ export type BaiweiMessage<T = Record<string, any>> = BaiweiMessageBase & T;
44
+ export declare enum BaiweiProductType {
45
+ ON_OFF_LIGHT = "On/Off Light",
46
+ ON_OFF_SWITCH = "On/Off Switch",
47
+ WINDOW_COVER = "Window Covering Device",
48
+ IAS_ZONE = "IAS Zone",
49
+ AIR_BOX = "Air Box",
50
+ SCENE_SELECTOR = "Scene Selector",
51
+ AC_GATEWAY = "AC gateway",
52
+ NEW_WIND = "New wind controller",
53
+ FLOOR_HEAT = "Floor heat controller"
54
+ }
55
+ export interface DeviceQuery {
56
+ type_list: DeviceTypeList[];
57
+ }
58
+ export interface DeviceTypeList {
59
+ product_type: BaiweiProductType;
60
+ device_list: BaiweiDevice[];
61
+ }
62
+ export interface BaiweiDevice {
63
+ device_id: string;
64
+ product_id: number;
65
+ device_attr: string;
66
+ device_name: string;
67
+ room_id: number;
68
+ network_type: number;
69
+ create_time: string;
70
+ acoutside_id: number;
71
+ acgateway_id: number;
72
+ product_type: string;
73
+ product_name: string;
74
+ node_id: number;
75
+ endpoint: number;
76
+ address: number;
77
+ sn: string;
78
+ mac: string;
79
+ com: string;
80
+ node_type: string;
81
+ soft_ver: string;
82
+ hard_ver: string;
83
+ model: string;
84
+ }
85
+ export interface DeviceStateList<T> {
86
+ device_list: BaiweiDeviceState<T>[];
87
+ }
88
+ export interface BaiweiDeviceState<T = any> {
89
+ device_id: string;
90
+ device_status: T;
91
+ }
92
+ export interface BaiweiDeviceStateReport<T = any> {
93
+ device: BaiweiDeviceState<T>;
94
+ }
95
+ export interface ACStatus {
96
+ state: string;
97
+ acoutside_id: number;
98
+ acgateway_id: number;
99
+ sys_mode: string;
100
+ coolpoint: number;
101
+ heatpoint: number;
102
+ wind_level: string;
103
+ curr_temp: number;
104
+ err_code: number;
105
+ }
106
+ export interface FloorHeatStatus {
107
+ state: string;
108
+ sys_mode: string;
109
+ work_mode: string;
110
+ lock_mode: string;
111
+ temp: number;
112
+ coolpoint: number;
113
+ heatpoint: number;
114
+ }
115
+ export interface IASZoneStatus {
116
+ state: string;
117
+ status: string;
118
+ }
119
+ export interface NewWindStatus {
120
+ state: string;
121
+ sys_mode: string;
122
+ work_mode: string;
123
+ lock_mode: string;
124
+ temp: number;
125
+ coolpoint: number;
126
+ heatpoint: number;
127
+ }
128
+ export interface LightState {
129
+ state: string;
130
+ }
131
+ export interface SceneStatus {
132
+ sceneId: number;
133
+ status: number;
134
+ state: string;
135
+ }
136
+ export interface WindowCoverStatus {
137
+ state: string;
138
+ level: number;
139
+ }
140
+ export interface SceneList {
141
+ scene_list: Scene[];
142
+ }
143
+ export interface Scene {
144
+ id: number;
145
+ name: string;
146
+ room_id: number;
147
+ delay: number;
148
+ type: number;
149
+ device_id: number;
150
+ picture_id: number;
151
+ status: number;
152
+ create_time: string;
153
+ instruct_list: SceneInstructList<any>[];
154
+ }
155
+ export interface SceneInstructList<T> extends BaiweiDeviceState<T> {
156
+ id: number;
157
+ type: number;
158
+ delay: number;
159
+ }
160
+ export interface Room {
161
+ id: number;
162
+ name: string;
163
+ create_time?: string;
164
+ }
165
+ export interface RoomList {
166
+ room_list?: Room[];
167
+ }
168
+ export interface UserResponse {
169
+ user: {
170
+ type: number;
171
+ need_sync_time: number;
172
+ token: string;
173
+ };
174
+ }
175
+ export declare enum MsgClass {
176
+ GATEWAY_MGMT = "gateway_mgmt",
177
+ USER_MGMT = "user_mgmt",
178
+ CONTROL_MGMT = "control_mgmt",
179
+ DEVICE_MGMT = "device_mgmt",
180
+ ROOM_MGMT = "room_mgmt",
181
+ SCENE_MGMT = "scene_mgmt"
182
+ }
183
+ export declare enum MsgName {
184
+ ZB_NET_OPEN = "zb_net_open",
185
+ USER_LOGIN = "user_login",
186
+ DEVICE_CONTROL = "device_control",
187
+ DEVICE_STATE_GET = "device_state_get",
188
+ DEVICE_STATE_REPORT = "device_state_report",
189
+ DEVICE_QUERY = "device_query",
190
+ ROOM_QUERY = "room_query",
191
+ SCENE_LIST = "scene_list",
192
+ SCENE_CALL = "scene_call"
193
+ }
194
+ export declare enum MsgType {
195
+ GET = "get",
196
+ SET = "set",
197
+ RESPONSE = "response",
198
+ REPORT = "report"
199
+ }
200
+ export type MessageRequest<TPayload = Record<string, any>> = {
201
+ msgClass: MsgClass;
202
+ msgName: MsgName;
203
+ msgType?: MsgType;
204
+ payload?: TPayload;
205
+ };
206
+ export type MessageResponse<TPayload = Record<string, any>> = {
207
+ msgId: string;
208
+ msgClass: MsgClass;
209
+ msgName: MsgName;
210
+ msgType: MsgType;
211
+ payload: TPayload;
212
+ };
package/dist/types.js ADDED
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MsgType = exports.MsgName = exports.MsgClass = exports.BaiweiProductType = void 0;
4
+ var BaiweiProductType;
5
+ (function (BaiweiProductType) {
6
+ BaiweiProductType["ON_OFF_LIGHT"] = "On/Off Light";
7
+ BaiweiProductType["ON_OFF_SWITCH"] = "On/Off Switch";
8
+ BaiweiProductType["WINDOW_COVER"] = "Window Covering Device";
9
+ BaiweiProductType["IAS_ZONE"] = "IAS Zone";
10
+ BaiweiProductType["AIR_BOX"] = "Air Box";
11
+ BaiweiProductType["SCENE_SELECTOR"] = "Scene Selector";
12
+ BaiweiProductType["AC_GATEWAY"] = "AC gateway";
13
+ BaiweiProductType["NEW_WIND"] = "New wind controller";
14
+ BaiweiProductType["FLOOR_HEAT"] = "Floor heat controller";
15
+ // BW_CAT_EYE = "BW Cateye",
16
+ })(BaiweiProductType || (exports.BaiweiProductType = BaiweiProductType = {}));
17
+ var MsgClass;
18
+ (function (MsgClass) {
19
+ MsgClass["GATEWAY_MGMT"] = "gateway_mgmt";
20
+ MsgClass["USER_MGMT"] = "user_mgmt";
21
+ MsgClass["CONTROL_MGMT"] = "control_mgmt";
22
+ MsgClass["DEVICE_MGMT"] = "device_mgmt";
23
+ MsgClass["ROOM_MGMT"] = "room_mgmt";
24
+ MsgClass["SCENE_MGMT"] = "scene_mgmt";
25
+ })(MsgClass || (exports.MsgClass = MsgClass = {}));
26
+ var MsgName;
27
+ (function (MsgName) {
28
+ // gateway
29
+ MsgName["ZB_NET_OPEN"] = "zb_net_open";
30
+ // user
31
+ MsgName["USER_LOGIN"] = "user_login";
32
+ // control
33
+ MsgName["DEVICE_CONTROL"] = "device_control";
34
+ MsgName["DEVICE_STATE_GET"] = "device_state_get";
35
+ MsgName["DEVICE_STATE_REPORT"] = "device_state_report";
36
+ // device
37
+ MsgName["DEVICE_QUERY"] = "device_query";
38
+ // room
39
+ MsgName["ROOM_QUERY"] = "room_query";
40
+ // scene
41
+ MsgName["SCENE_LIST"] = "scene_list";
42
+ MsgName["SCENE_CALL"] = "scene_call";
43
+ })(MsgName || (exports.MsgName = MsgName = {}));
44
+ var MsgType;
45
+ (function (MsgType) {
46
+ MsgType["GET"] = "get";
47
+ MsgType["SET"] = "set";
48
+ MsgType["RESPONSE"] = "response";
49
+ MsgType["REPORT"] = "report";
50
+ })(MsgType || (exports.MsgType = MsgType = {}));
@@ -0,0 +1,19 @@
1
+ export declare class IdGenerator {
2
+ private readonly prefix;
3
+ private counter;
4
+ constructor(prefix?: string);
5
+ /**
6
+ * 获取下一个 msg_id
7
+ * 格式:prefix-001 / prefix-002 / ... / prefix-999
8
+ * 超过 999 自动从 001 重置
9
+ */
10
+ next(): string;
11
+ /**
12
+ * 获取当前前缀(用于调试/测试)
13
+ */
14
+ getPrefix(): string;
15
+ /**
16
+ * 手动重置计数器(可选)
17
+ */
18
+ resetCounter(start?: number): void;
19
+ }
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IdGenerator = void 0;
4
+ /**
5
+ * 生成 5 位随机前缀(A-Z + 0-9)
6
+ */
7
+ function randomPrefix(length = 5) {
8
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
9
+ let prefix = "";
10
+ for (let i = 0; i < length; i++) {
11
+ prefix += chars.charAt(Math.floor(Math.random() * chars.length));
12
+ }
13
+ return prefix;
14
+ }
15
+ class IdGenerator {
16
+ prefix;
17
+ counter;
18
+ constructor(prefix) {
19
+ // prefix 未指定则自动生成 5 位随机前缀
20
+ this.prefix = prefix ?? randomPrefix(5);
21
+ this.counter = 1;
22
+ }
23
+ /**
24
+ * 获取下一个 msg_id
25
+ * 格式:prefix-001 / prefix-002 / ... / prefix-999
26
+ * 超过 999 自动从 001 重置
27
+ */
28
+ next() {
29
+ if (this.counter > 999) {
30
+ this.counter = 1;
31
+ }
32
+ const suffix = this.counter.toString().padStart(3, "0");
33
+ this.counter++;
34
+ return `${this.prefix}-${suffix}`;
35
+ }
36
+ /**
37
+ * 获取当前前缀(用于调试/测试)
38
+ */
39
+ getPrefix() {
40
+ return this.prefix;
41
+ }
42
+ /**
43
+ * 手动重置计数器(可选)
44
+ */
45
+ resetCounter(start = 1) {
46
+ this.counter = start;
47
+ }
48
+ }
49
+ exports.IdGenerator = IdGenerator;
@@ -0,0 +1,4 @@
1
+ /** 生成协议请求使用的随机消息 ID。 */
2
+ export declare class MessageIdGenerator {
3
+ next(): string;
4
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MessageIdGenerator = void 0;
4
+ const BASE = 10_000_000_000;
5
+ const RANGE = 90_000_000_000;
6
+ /** 生成协议请求使用的随机消息 ID。 */
7
+ class MessageIdGenerator {
8
+ next() {
9
+ return String(Math.floor(BASE + Math.random() * RANGE));
10
+ }
11
+ }
12
+ exports.MessageIdGenerator = MessageIdGenerator;
@@ -0,0 +1,33 @@
1
+ export type LogLever = "debug" | "info" | "warn" | "error";
2
+ export interface Logger {
3
+ debug: (...args: any[]) => void;
4
+ info: (...args: any[]) => void;
5
+ warn: (...args: any[]) => void;
6
+ error: (...args: any[]) => void;
7
+ }
8
+ /**
9
+ * 对日志中的敏感字段做浅/深层脱敏。
10
+ * 兼容普通对象和 JSON 字符串两种输入。
11
+ */
12
+ export declare function maskSensitive(value: unknown, keys?: readonly string[]): unknown;
13
+ type WrapLoggerOptions = {
14
+ /** debug 级别是否脱敏:默认 false(即不脱敏) */
15
+ maskDebug?: boolean;
16
+ /** 自定义敏感 key 规则 */
17
+ maskKeys?: string[];
18
+ /** 当未传入 logger 时,默认会加时间戳;可显式开关 */
19
+ withTimestamp?: boolean;
20
+ /** 日志前缀(例如 SDK 名称、模块名) */
21
+ prefix?: string;
22
+ /** 自定义时间格式化:默认 new Date().toISOString() */
23
+ formatTime?: (d: Date) => string;
24
+ };
25
+ /**
26
+ * 统一拿到一个可用的 logger:
27
+ * - 默认使用 console
28
+ * - 返回的 logger 保证 debug/info/warn/error 都是函数
29
+ * - 默认:debug 不脱敏,info/warn/error 脱敏
30
+ * - 当不传入 logger 时:会在日志前自动加时间戳与可选前缀
31
+ */
32
+ export declare function createLogger(input?: Logger, opts?: WrapLoggerOptions): Required<Logger>;
33
+ export {};