@maiyunnet/kebab 9.17.3 → 9.18.1

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/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export declare const VER = "9.17.3";
8
+ export declare const VER = "9.18.1";
9
9
  /** --- 框架根目录,以 / 结尾 --- */
10
10
  export declare const ROOT_PATH: string;
11
11
  /** --- 框架的 LIB,以 / 结尾 --- */
@@ -159,6 +159,8 @@ export interface IConfigConst {
159
159
  'host': string;
160
160
  'hostname': string;
161
161
  'hostport': number;
162
+ /** --- 协议与 host,如 https://example.com:8443 --- */
163
+ 'urlOrigin': string;
162
164
  'uri': IUrlParse;
163
165
  'rootPath': string;
164
166
  'modPath': string;
package/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * --- 本文件用来定义每个目录实体地址的常量 ---
7
7
  */
8
8
  /** --- 当前系统版本号 --- */
9
- export const VER = '9.17.3';
9
+ export const VER = '9.18.1';
10
10
  // --- 服务端用的路径 ---
11
11
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
12
12
  /** --- /xxx/xxx --- */
package/lib/ws.d.ts CHANGED
@@ -43,6 +43,25 @@ export interface IConnectOptions {
43
43
  'url': string;
44
44
  'auth': string;
45
45
  };
46
+ /** --- WebSocket 握手失败回调 --- */
47
+ onConnectError?: (error: unknown) => void;
48
+ }
49
+ /** --- WebSocket 管道首先结束的一侧 --- */
50
+ export type TPipeSide = 'source' | 'target';
51
+ /** --- WebSocket 管道结束前最后收到的底层事件 --- */
52
+ export type TPipeCloseEvent = 'close' | 'end' | 'timeout' | 'error';
53
+ /** --- WebSocket 管道关闭信息 --- */
54
+ export interface IPipeCloseInfo {
55
+ 'side': TPipeSide;
56
+ 'event': TPipeCloseEvent;
57
+ 'error'?: unknown;
58
+ 'code'?: number;
59
+ 'reason'?: string;
60
+ }
61
+ /** --- WebSocket 关闭信息 --- */
62
+ export interface ISocketCloseInfo {
63
+ 'code': number;
64
+ 'reason': string;
46
65
  }
47
66
  /** --- 正向代理请求的传入参数选项 --- */
48
67
  export interface IMproxyOptions {
@@ -78,10 +97,32 @@ export interface IRproxyOptions {
78
97
  'url': string;
79
98
  'auth': string;
80
99
  };
100
+ /** --- 连接目标 WebSocket 失败回调 --- */
101
+ onConnectError?: (error: unknown) => void;
102
+ /** --- 管道关闭回调,返回首先关闭的一侧及其底层事件 --- */
103
+ onClose?: (info: IPipeCloseInfo) => void;
104
+ /** --- 生成回传给另一侧的 WebSocket 关闭原因,仅在管道断开时调用 --- */
105
+ closeReason?: (info: IPipeCloseInfo) => string;
81
106
  }
82
107
  export declare class Socket {
83
108
  /** --- 当前的 ws 对象 --- */
84
109
  private _ws;
110
+ /** --- 当前是否为服务端接入的连接 --- */
111
+ private readonly _isServer;
112
+ /** --- 客户端发出的帧是否掩码 --- */
113
+ private _masking;
114
+ /** --- 等待底层写入回调的消息数量 --- */
115
+ private _pendingWrites;
116
+ /** --- 是否已经因发送缓存触发背压 --- */
117
+ private _writeBlocked;
118
+ /** --- 是否已由本端主动结束写入 --- */
119
+ private _finished;
120
+ /** --- 是否已由对端结束读取 --- */
121
+ private _ended;
122
+ /** --- 底层 TCP Socket,用于设置和清理空闲超时 --- */
123
+ private _transport?;
124
+ /** --- 底层 TCP Socket 的超时监听器 --- */
125
+ private _transportTimeout?;
85
126
  constructor(request?: http.IncomingMessage, socket?: net.Socket, head?: Buffer, options?: {
86
127
  'headers'?: http.OutgoingHttpHeaders;
87
128
  'timeout'?: number;
@@ -94,6 +135,30 @@ export declare class Socket {
94
135
  connect(u: string, opt?: IConnectOptions): Promise<this | null>;
95
136
  /** --- 创建成功后第一时间绑定事件 --- */
96
137
  private _bindEvent;
138
+ /**
139
+ * --- 绑定底层 TCP Socket 的空闲超时 ---
140
+ * @param socket 底层 TCP Socket
141
+ * @param timeout 超时毫秒数,0 为不超时
142
+ */
143
+ private _bindTransport;
144
+ /** --- 清理底层 TCP Socket 监听器 --- */
145
+ private _unbindTransport;
146
+ /**
147
+ * --- 将 ws 的消息数据统一转换为 Buffer ---
148
+ * @param data ws 原始消息数据
149
+ * @returns Buffer
150
+ */
151
+ private _toBuffer;
152
+ /**
153
+ * --- 接收并派发消息;上层未就绪或主动暂停时同步暂停底层 TCP 读取 ---
154
+ * @param msg 消息
155
+ */
156
+ private _receive;
157
+ /**
158
+ * --- 派发错误,监听器尚未绑定时暂存 ---
159
+ * @param error 错误对象
160
+ */
161
+ private _emitError;
97
162
  /** --- 还未开启监听时来的数据将存在这里 --- */
98
163
  private readonly _waitMsg;
99
164
  /** --- 尚未交给消息监听器的数据量 --- */
@@ -103,7 +168,7 @@ export declare class Socket {
103
168
  /** --- 还未开启 error 监听时产生的 error 错误对象 --- */
104
169
  private _error;
105
170
  /** --- 还未开启 close 监听时是不是就已经 close --- */
106
- private _close;
171
+ private _close?;
107
172
  /** --- 绑定的自定义监听事件(未绑定则默认在 _bindEvent 执行) --- */
108
173
  private _on;
109
174
  /**
@@ -119,14 +184,20 @@ export declare class Socket {
119
184
  'data': Buffer;
120
185
  }) => void | Promise<void>): this;
121
186
  on(event: 'error', cb: (error: any) => void | Promise<void>): this;
122
- on(event: 'drain' | 'close' | 'end' | 'timeout', cb: () => void | Promise<void>): this;
187
+ on(event: 'close', cb: (info: ISocketCloseInfo) => void | Promise<void>): this;
188
+ on(event: 'drain' | 'end' | 'timeout', cb: () => void | Promise<void>): this;
123
189
  /** --- 取消监听 --- */
124
190
  off(event: 'message' | 'drain' | 'error' | 'close' | 'end' | 'timeout'): this;
125
- end(): void;
191
+ /**
192
+ * --- 正常结束 WebSocket 连接 ---
193
+ * @param code WebSocket 关闭码
194
+ * @param reason 关闭原因,超过协议上限时自动安全截断
195
+ */
196
+ end(code?: number, reason?: string): void;
126
197
  destroy(): void;
127
- /** --- 暂停向消息监听器派发数据,底层仍只保留有界缓存 --- */
198
+ /** --- 暂停向消息监听器派发数据,并暂停底层 TCP 读取 --- */
128
199
  pause(): void;
129
- /** --- 恢复向消息监听器派发数据 --- */
200
+ /** --- 恢复派发缓存消息,并恢复底层 TCP 读取 --- */
130
201
  resume(): void;
131
202
  /** --- 发送文本 --- */
132
203
  writeText(data: Buffer | string | Array<Buffer | string>): boolean;
@@ -134,6 +205,15 @@ export declare class Socket {
134
205
  writeResult(data: kebab.Json): boolean;
135
206
  /** --- 发送二进制 --- */
136
207
  writeBinary(data: string | Buffer | Array<string | Buffer>): boolean;
208
+ /**
209
+ * --- 发送消息并将 ws 的发送缓存转换为 Kebab 的背压布尔值 ---
210
+ * @param data 消息数据
211
+ * @param binary 是否为二进制消息
212
+ * @returns 是否已直接写入底层 Socket 缓存
213
+ */
214
+ private _send;
215
+ /** --- 发送缓存完全排空后派发 drain --- */
216
+ private _emitDrain;
137
217
  /** --- 当前是否是可写状态 --- */
138
218
  get writable(): boolean;
139
219
  /** --- 当前是否已经结束读取,并且无法继续读取 --- */
@@ -148,6 +228,13 @@ export declare class Socket {
148
228
  ping(data?: Buffer | string): boolean;
149
229
  /** --- 发送 ping --- */
150
230
  pong(data?: Buffer | string): boolean;
231
+ /**
232
+ * --- 发送控制帧 ---
233
+ * @param method 控制帧类型
234
+ * @param data 控制帧数据
235
+ * @returns 是否已直接写入底层 Socket 缓存
236
+ */
237
+ private _sendControl;
151
238
  }
152
239
  /**
153
240
  * --- 创建一个 ws 客户端发起 ws 请求 ---