xshell 1.2.89 → 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.
- package/antd.sass +4 -31
- package/builder.js +5 -14
- package/file.d.ts +2 -2
- package/file.js +1 -1
- package/i18n/dict.json +12 -0
- package/i18n/scanner/index.d.ts +1 -0
- package/i18n/scanner/index.js +14 -5
- package/net.browser.d.ts +1 -142
- package/net.browser.js +1 -408
- package/net.common.d.ts +178 -0
- package/net.common.js +564 -0
- package/net.d.ts +2 -173
- package/net.js +1 -466
- package/package.json +29 -30
- package/path.d.ts +2 -2
- package/platform.browser.js +9 -1
- package/platform.common.d.ts +9 -0
- package/platform.js +11 -1
- package/prototype.common.d.ts +2 -2
- package/prototype.common.js +8 -8
- package/react.development.js +9199 -6036
- package/react.development.js.map +1 -1
- package/react.production.js +4958 -4306
- package/react.production.js.map +1 -1
- package/repl.js +5 -1
- package/server.d.ts +26 -11
- package/server.js +203 -65
- package/utils.browser.d.ts +1 -1
- package/utils.browser.js +3 -1
- package/utils.common.d.ts +11 -9
- package/utils.common.js +50 -35
- package/utils.js +7 -1
- package/i18n/utils.d.ts +0 -1
- package/i18n/utils.js +0 -11
package/net.browser.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { t } from "./i18n/instance.js";
|
|
2
|
-
import {
|
|
3
|
-
import { message_symbol, pack, parse } from "./io.common.js";
|
|
4
|
-
import { assert, genid, delay, Lock, timeout, check } from "./utils.browser.js";
|
|
2
|
+
import { assert, delay } from "./utils.browser.js";
|
|
5
3
|
import { drop_request_headers } from "./net.common.js";
|
|
6
4
|
export * from "./net.common.js";
|
|
7
5
|
async function fetch_retry(url, options, timeout, retries = 0, count = 0) {
|
|
@@ -142,409 +140,4 @@ export async function request_json(url, options) {
|
|
|
142
140
|
throw error;
|
|
143
141
|
}
|
|
144
142
|
}
|
|
145
|
-
export class WebSocketConnectionError extends Error {
|
|
146
|
-
name = 'WebSocketConnectionError';
|
|
147
|
-
// 这里不保留 websocket 引用,防止循环引用导致 JSON 序列化失败
|
|
148
|
-
url;
|
|
149
|
-
protocols;
|
|
150
|
-
event;
|
|
151
|
-
type;
|
|
152
|
-
code;
|
|
153
|
-
reason;
|
|
154
|
-
constructor(url, protocols, event, message) {
|
|
155
|
-
super(`${url}${protocols ? ' ' + protocols.join(', ').bracket() : ''} ${t('连接出错了')}. ${message || ''}`);
|
|
156
|
-
this.url = url;
|
|
157
|
-
this.protocols = protocols;
|
|
158
|
-
this.event = event;
|
|
159
|
-
this.type = event.type;
|
|
160
|
-
if (this.type === 'close') {
|
|
161
|
-
this.code = event.code;
|
|
162
|
-
this.reason = event.reason;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
/** 连接 websocket url, 设置各种事件监听器。在 open 事件后 resolve, 返回 websocket
|
|
167
|
-
遇到 error 时会创建 WebSocketConnectionError:
|
|
168
|
-
- reject 掉返回的 promise (若此时未 settle)
|
|
169
|
-
- 作为参数调用 on_error (已 settle 且有 on_error 回调)
|
|
170
|
-
可以用 WebSocket.bufferedAmount 来显示大消息的发送进度
|
|
171
|
-
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/bufferedAmount
|
|
172
|
-
- url
|
|
173
|
-
- options:
|
|
174
|
-
- protocols?
|
|
175
|
-
- on_message: 根据 websocket frame 的 opcode 不同 (text frame 或 binary frame),event 中的 data 对应为 ArrayBuffer 或者 string
|
|
176
|
-
https://datatracker.ietf.org/doc/html/rfc6455#section-5.2
|
|
177
|
-
- on_error?: 在 websocket 出错和非正常关闭 (close, error 事件) 时都调用,可以根据 error.type 来区分,error 的类型是 WebSocketConnectionError,
|
|
178
|
-
type 为 'close' 时有 code 和 reason 属性
|
|
179
|
-
- on_close?: 和 websocket 的 'close' 事件不相同,只在正常关闭 (close code 为 1000) 时才调用,否则都会调用 on_error
|
|
180
|
-
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes
|
|
181
|
-
- print?: 是否打印连接、关闭信息 */
|
|
182
|
-
export async function connect_websocket(url, { protocols, on_message, on_error, on_close, print = true }) {
|
|
183
|
-
let websocket = new WebSocket(url, protocols);
|
|
184
|
-
// https://stackoverflow.com/questions/11821096/what-is-the-difference-between-an-arraybuffer-and-a-blob/39951543
|
|
185
|
-
websocket.binaryType = 'arraybuffer';
|
|
186
|
-
return new Promise((resolve, reject) => {
|
|
187
|
-
let settled = false;
|
|
188
|
-
websocket.addEventListener('open', event => {
|
|
189
|
-
if (print)
|
|
190
|
-
console.log(websocket.url +
|
|
191
|
-
(websocket.protocol ? ' ' + websocket.protocol.bracket() : '') +
|
|
192
|
-
t(' 已连接'));
|
|
193
|
-
settled = true;
|
|
194
|
-
resolve(websocket);
|
|
195
|
-
});
|
|
196
|
-
websocket.addEventListener('close', event => {
|
|
197
|
-
if (event.code === 1000) { // 正常关闭
|
|
198
|
-
if (on_close)
|
|
199
|
-
on_close(event, websocket);
|
|
200
|
-
else if (print)
|
|
201
|
-
console.log(`${websocket.url} ${t('已正常关闭')}`);
|
|
202
|
-
}
|
|
203
|
-
else { // 异常关闭,认为发生了错误,进行错误处理
|
|
204
|
-
const error = new WebSocketConnectionError(websocket.url, protocols, event, `${t('连接被关闭')}, code: ${event.code}${event.reason ? `, ${t('原因')}: ${event.reason}` : ''}`);
|
|
205
|
-
if (settled)
|
|
206
|
-
if (on_error)
|
|
207
|
-
on_error(error, websocket);
|
|
208
|
-
else // 既然用户不传 on_error, 就当 unhandled error 抛出来
|
|
209
|
-
throw error;
|
|
210
|
-
else {
|
|
211
|
-
settled = true;
|
|
212
|
-
reject(error);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
websocket.addEventListener('error', event => {
|
|
217
|
-
const error = new WebSocketConnectionError(websocket.url, protocols, event);
|
|
218
|
-
// https://blog.insiderattack.net/promises-next-ticks-and-immediates-nodejs-event-loop-part-3-9226cbe7a6aa
|
|
219
|
-
// close 的错误信息比较多,这里延后触发 error 事件,放到微任务队列之后的 timers 队列中
|
|
220
|
-
setTimeout(() => {
|
|
221
|
-
if (settled)
|
|
222
|
-
if (on_error)
|
|
223
|
-
on_error(error, websocket);
|
|
224
|
-
else // 既然用户不传 on_error, 就当 unhandled error 抛出来
|
|
225
|
-
throw error;
|
|
226
|
-
else {
|
|
227
|
-
settled = true;
|
|
228
|
-
reject(error);
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
websocket.addEventListener('message', event => {
|
|
233
|
-
on_message(event.data, websocket);
|
|
234
|
-
});
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
/** 通过创建 remote 对象对 websocket rpc 进行抽象
|
|
238
|
-
创建 remote 对象时传入 funcs 注册处理函数,使得对端能通过 (rpc message).func 调用
|
|
239
|
-
使用 remote.handle 方法处理对端发来的 websocket message,对于 websocket 连接接收方需要手动绑定 websocket message 事件到 remote.handle
|
|
240
|
-
使用 remote.call 进行一元 rpc
|
|
241
|
-
使用 remote.send 结合 message.id 进行复杂 rpc
|
|
242
|
-
创建后等到首个 remote.call 或 remote.send 时自动建立实际 websocket 连接
|
|
243
|
-
rpc 状态与底层连接的状态无关,如果是传入 url 创建的 remote 实例,remote.send 时检测到断线会自动建立新的 websocket 连接
|
|
244
|
-
|
|
245
|
-
@example
|
|
246
|
-
// Zero 继承自 Remote 并通过 call 实现了一些方法
|
|
247
|
-
let zero = new Zero({ local: true })
|
|
248
|
-
|
|
249
|
-
// 一元 rpc
|
|
250
|
-
await zero.repl_ts('1234')
|
|
251
|
-
|
|
252
|
-
// 订阅流
|
|
253
|
-
const id = genid()
|
|
254
|
-
|
|
255
|
-
zero.handlers.set(id, ({ data: [chunk] }: Message<[Uint8Array]>) => {
|
|
256
|
-
term.write(chunk)
|
|
257
|
-
})
|
|
258
|
-
|
|
259
|
-
zero.send({ id, func: 'subscribe_stdio' }) */
|
|
260
|
-
export class Remote {
|
|
261
|
-
/** 在构造 Remote 时, this.initiator = Boolean(url || websocket)
|
|
262
|
-
- true: 作为 websocket 连接发起方
|
|
263
|
-
- false: 作为 websocket 连接接收方 */
|
|
264
|
-
initiator;
|
|
265
|
-
/** 作为 websocket 连接发起方,对端的 url 地址 */
|
|
266
|
-
url;
|
|
267
|
-
/** 作为 websocket 连接发起方有 websocket lock */
|
|
268
|
-
lwebsocket;
|
|
269
|
-
/** websocket 连接发起方,接收方,都能被对端通过 (rpc message).func 调用的 rpc 函数 */
|
|
270
|
-
funcs;
|
|
271
|
-
/** map<id, message handler>: 通过 (rpc message).id 找到对应的 handler
|
|
272
|
-
一元 rpc 接收方不需要设置 handlers, 发送方需要 */
|
|
273
|
-
handlers = new Map();
|
|
274
|
-
keeper;
|
|
275
|
-
/** `true` 是否打印连接信息、错误信息 */
|
|
276
|
-
print = true;
|
|
277
|
-
/** `false` 打印所有交互的 rpc messages */
|
|
278
|
-
verbose = false;
|
|
279
|
-
first_error = true;
|
|
280
|
-
keeping = false;
|
|
281
|
-
reconnecting = false;
|
|
282
|
-
disconnected = false;
|
|
283
|
-
/** 作为 websocket 连接发起方,传入 url 或 websocket,定义远程 Remote
|
|
284
|
-
作为 websocket 连接接收方,不传 url 和 websocket,定义本地 Remote */
|
|
285
|
-
constructor({ url, funcs, print, verbose, websocket, keeper, on_error, } = {}) {
|
|
286
|
-
check(!(url && websocket), '构建 Remote 时 url 和 websocket 最多只能传一个');
|
|
287
|
-
this.initiator = Boolean(url || websocket);
|
|
288
|
-
if (url)
|
|
289
|
-
this.url = url;
|
|
290
|
-
check(!funcs?.echo);
|
|
291
|
-
this.funcs = {
|
|
292
|
-
...funcs,
|
|
293
|
-
echo({ data }) {
|
|
294
|
-
return data;
|
|
295
|
-
}
|
|
296
|
-
};
|
|
297
|
-
if (print !== undefined)
|
|
298
|
-
this.print = print;
|
|
299
|
-
if (verbose !== undefined)
|
|
300
|
-
this.verbose = verbose;
|
|
301
|
-
if (on_error)
|
|
302
|
-
this.on_error = on_error;
|
|
303
|
-
if (this.initiator)
|
|
304
|
-
this.lwebsocket = new Lock(websocket || null);
|
|
305
|
-
if (keeper) {
|
|
306
|
-
check(this.initiator && url);
|
|
307
|
-
this.keeper = {
|
|
308
|
-
reconnect_interval: 5000,
|
|
309
|
-
heartbeat_interval: 1000 * 60,
|
|
310
|
-
error_delay: 2000,
|
|
311
|
-
...keeper
|
|
312
|
-
};
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
/** 统一处理首次连接和连接后的 websocket 错误 */
|
|
316
|
-
_on_error = (error, websocket) => {
|
|
317
|
-
if (this.keeper && !this.reconnecting && !this.disconnected) // 在一段时间后调度错误重连
|
|
318
|
-
(async () => {
|
|
319
|
-
this.reconnecting = true;
|
|
320
|
-
const { error_delay, reconnect_interval } = this.keeper;
|
|
321
|
-
if (this.first_error) {
|
|
322
|
-
this.first_error = false;
|
|
323
|
-
await delay(error_delay);
|
|
324
|
-
}
|
|
325
|
-
else
|
|
326
|
-
await delay(reconnect_interval);
|
|
327
|
-
this.reconnecting = false;
|
|
328
|
-
if (!this.disconnected)
|
|
329
|
-
try {
|
|
330
|
-
await timeout(3000, this.connect(), undefined, this.print);
|
|
331
|
-
this.first_error = true;
|
|
332
|
-
}
|
|
333
|
-
catch (error) {
|
|
334
|
-
// 重连失败的错误这里需要简单打印下,_on_error 不会打印,这里也不继续往上抛了
|
|
335
|
-
if (this.print)
|
|
336
|
-
console.log(error.message);
|
|
337
|
-
// 重连由 this.connect 里面调用 this._on_error 处理
|
|
338
|
-
}
|
|
339
|
-
})();
|
|
340
|
-
this.on_error(error, websocket);
|
|
341
|
-
};
|
|
342
|
-
_on_message = (data, websocket) => {
|
|
343
|
-
this.handle(new Uint8Array(data), websocket);
|
|
344
|
-
};
|
|
345
|
-
/** 使用者自定义的在 websocket 连接出错时,或者 handlers 出错时的处理 */
|
|
346
|
-
on_error(error, websocket) {
|
|
347
|
-
// 使用者未定义 Remote 如何处理 error 时,一般来说直接忽略即可,因为 handlers 中报错了也会返回给对端
|
|
348
|
-
if (this.print)
|
|
349
|
-
console.log(error && error instanceof WebSocketConnectionError ? error.message : error);
|
|
350
|
-
// 这里继续往上层抛没有太大意义,上面一般都是 websocket on_message 这些
|
|
351
|
-
}
|
|
352
|
-
/** 幂等,保证 websocket 已连接,否则抛出异常
|
|
353
|
-
一般情况不需要手动调用,在其它方法中会自动调用这个方法,除非需要手动建立 websocket 连接并确保成功
|
|
354
|
-
连接断开后,通过传入 url 参数构造的 remote 实例会自动创建新的 websocket 连接,而通过传入 websocket 参数构造的实例只会检查连接状态,在断开时抛出异常
|
|
355
|
-
作为 websocket 连接发起方,不需要传入 websocket
|
|
356
|
-
作为 websocket 连接接收方,需要传入使用的 websocket 连接,确保这个这个连接的状态 */
|
|
357
|
-
async connect(websocket) {
|
|
358
|
-
if (!this.initiator)
|
|
359
|
-
if (websocket.readyState !== WebSocket.OPEN)
|
|
360
|
-
throw new Error('传入的 websocket 连接已断开');
|
|
361
|
-
else
|
|
362
|
-
return;
|
|
363
|
-
if (this.lwebsocket.resource?.readyState === WebSocket.OPEN)
|
|
364
|
-
return;
|
|
365
|
-
if (!this.url)
|
|
366
|
-
throw new Error('创建 Remote 时传入的 websocket 连接已断开');
|
|
367
|
-
let reconnected = false;
|
|
368
|
-
// 假设有多个请求想要并发连接 websocket, 且此时 websocket 是断开的状态
|
|
369
|
-
// 应该排队依次连接,而不是后续的连接直接使用第一次连接的 promise,后续调用还是应该尝试重连(不止连接一次)
|
|
370
|
-
await this.lwebsocket.request(async (websocket) => {
|
|
371
|
-
// 保存的 rpc 状态在 this.handlers, 与 websocket 无关,因此即使断开重连也不影响 rpc 的运行,即
|
|
372
|
-
// 底层连接断开后自动重连对上层应该是无感知的,除非再次连接时失败
|
|
373
|
-
if (websocket?.readyState === WebSocket.OPEN)
|
|
374
|
-
return;
|
|
375
|
-
// 重连
|
|
376
|
-
try {
|
|
377
|
-
this.lwebsocket.resource = await connect_websocket(this.url, {
|
|
378
|
-
on_message: this._on_message,
|
|
379
|
-
on_error: this._on_error,
|
|
380
|
-
print: this.print
|
|
381
|
-
});
|
|
382
|
-
reconnected = true;
|
|
383
|
-
}
|
|
384
|
-
catch (error) {
|
|
385
|
-
this._on_error(error);
|
|
386
|
-
throw error;
|
|
387
|
-
}
|
|
388
|
-
});
|
|
389
|
-
if (!this.keeper)
|
|
390
|
-
return;
|
|
391
|
-
const { heartbeat_interval, func, args } = this.keeper;
|
|
392
|
-
// 首次连接成功时,开始心跳保活
|
|
393
|
-
if (!this.keeping) {
|
|
394
|
-
this.keeping = true;
|
|
395
|
-
(async () => {
|
|
396
|
-
for (;;) {
|
|
397
|
-
await delay(heartbeat_interval);
|
|
398
|
-
if (this.disconnected)
|
|
399
|
-
break;
|
|
400
|
-
if (!this.reconnecting)
|
|
401
|
-
try {
|
|
402
|
-
await timeout(1000 * 2, this.call('echo'), undefined, this.print);
|
|
403
|
-
}
|
|
404
|
-
catch (error) {
|
|
405
|
-
if (this.print)
|
|
406
|
-
console.log(error.message);
|
|
407
|
-
this._on_error(error);
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
})();
|
|
411
|
-
}
|
|
412
|
-
if (reconnected && func)
|
|
413
|
-
await this.call(func, args);
|
|
414
|
-
}
|
|
415
|
-
/** 作为 websocket 连接发起方手动关闭到对端的 websocket 连接 */
|
|
416
|
-
disconnect() {
|
|
417
|
-
this.disconnected = true;
|
|
418
|
-
this.lwebsocket.resource?.close(1000);
|
|
419
|
-
}
|
|
420
|
-
/** 发送 message 到对端 remote
|
|
421
|
-
作为 websocket 连接发起方,不需要传入 websocket
|
|
422
|
-
作为 websocket 连接接收方,必传 websocket 参数
|
|
423
|
-
发送或连接出错时自动清理 message.id 对应的 handler */
|
|
424
|
-
async send(message, websocket) {
|
|
425
|
-
if (this.verbose)
|
|
426
|
-
console.log('remote.send:', message);
|
|
427
|
-
try {
|
|
428
|
-
await this.connect(websocket);
|
|
429
|
-
message[message_symbol] = true;
|
|
430
|
-
(websocket || this.lwebsocket.resource).send(pack(message));
|
|
431
|
-
}
|
|
432
|
-
catch (error) {
|
|
433
|
-
if (message.id)
|
|
434
|
-
this.handlers.delete(message.id);
|
|
435
|
-
throw error;
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
/** 处理接收到的 websocket message 并解析, 根据 message.id 或 message.func 分发到对应的 handler 进行处理,
|
|
439
|
-
handler 处理完成后:
|
|
440
|
-
- 传了 func: 调用函数的情况下 (通常是一元 rpc),总是将返回值包装为 message 回传
|
|
441
|
-
- 未传 func: 通过 id 调用,如果 handler 返回非 undefined 的值,也包装为 message 回传
|
|
442
|
-
|
|
443
|
-
如果 message.done == true 则对端指示当前 remote 可以清理 handler
|
|
444
|
-
使用 Uint8Array 作为参数更灵活 https://stackoverflow.com/a/74505197/7609214
|
|
445
|
-
这个方法一般不会抛出错误,也不需要 await,一般在 websocket on_message 时使用 */
|
|
446
|
-
async handle(data, websocket) {
|
|
447
|
-
let message;
|
|
448
|
-
try {
|
|
449
|
-
check(data[0] === 0xcc, 'message 格式错误');
|
|
450
|
-
message = parse(data);
|
|
451
|
-
}
|
|
452
|
-
catch (error) {
|
|
453
|
-
this.on_error(error);
|
|
454
|
-
return;
|
|
455
|
-
}
|
|
456
|
-
const { id, func, done } = message;
|
|
457
|
-
if (this.verbose)
|
|
458
|
-
console.log('remote.handle:', message);
|
|
459
|
-
let handler;
|
|
460
|
-
if (func) {
|
|
461
|
-
handler = this.funcs[func];
|
|
462
|
-
// 传了 func 调用函数的情况下,如果 message.data 为 undefined, 默认为 [ ]
|
|
463
|
-
if (message.data === undefined)
|
|
464
|
-
message.data = [];
|
|
465
|
-
}
|
|
466
|
-
else {
|
|
467
|
-
handler = this.handlers.get(id);
|
|
468
|
-
if (done && handler)
|
|
469
|
-
this.handlers.delete(id);
|
|
470
|
-
}
|
|
471
|
-
try {
|
|
472
|
-
if (handler) {
|
|
473
|
-
const data = await handler(message, websocket);
|
|
474
|
-
if (func || data !== undefined)
|
|
475
|
-
await this.send({ id, data }, websocket);
|
|
476
|
-
}
|
|
477
|
-
else
|
|
478
|
-
throw message.error || new Error(`找不到 rpc handler: ${func ? `func: ${func.quote()}` : `id: ${id}`}`);
|
|
479
|
-
}
|
|
480
|
-
catch (error) {
|
|
481
|
-
// handler 出错并不意味着 rpc 一定会结束,可能 error 是运行中的正常数据,所以不能清理 handler
|
|
482
|
-
if (websocket.readyState === WebSocket.OPEN &&
|
|
483
|
-
!message.error // 防止无限循环往对方发送 error, 只有在对方无错误时才可以发送
|
|
484
|
-
)
|
|
485
|
-
await this.send({ id, error, /* 不能设置 done 清理对面 handler, 理由同上 */ }, websocket);
|
|
486
|
-
// 这里继续往上层抛没有太大意义,上面一般都是 websocket on_message 这些,交给自定义或默认的 on_error 处理
|
|
487
|
-
this.on_error(error);
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
/** 调用对端 remote 中的 func, 只适用于最简单的一元 rpc (请求, 响应)
|
|
491
|
-
作为 websocket 连接发起方,不需要传入 websocket
|
|
492
|
-
作为 websocket 连接接收方,必传 websocket 参数 */
|
|
493
|
-
async call(func, args, websocket) {
|
|
494
|
-
return new Promise(async (resolve, reject) => {
|
|
495
|
-
const id = genid();
|
|
496
|
-
this.handlers.set(id, ({ error, data }) => {
|
|
497
|
-
if (error)
|
|
498
|
-
reject(error);
|
|
499
|
-
else
|
|
500
|
-
resolve(data);
|
|
501
|
-
this.handlers.delete(id);
|
|
502
|
-
});
|
|
503
|
-
try {
|
|
504
|
-
await this.send({ id, func, data: args }, websocket); // 不需要 done: true, 因为对面的 remote.handlers 中不会有这个 id 的 handler
|
|
505
|
-
}
|
|
506
|
-
catch (error) {
|
|
507
|
-
reject(error);
|
|
508
|
-
}
|
|
509
|
-
});
|
|
510
|
-
}
|
|
511
|
-
/** 调用对端 remote 中的 func, 开始订阅并接收连续的消息 (订阅流)
|
|
512
|
-
- func: 订阅处理函数
|
|
513
|
-
- on_data: 接收开始订阅后的数据
|
|
514
|
-
- options?:
|
|
515
|
-
- on_error?: 处理开始订阅后的错误
|
|
516
|
-
- websocket?: 作为 websocket 连接接收方,必传 websocket 参数 */
|
|
517
|
-
async subscribe(func, on_data, { on_error = rethrow, websocket } = {}) {
|
|
518
|
-
const id = genid();
|
|
519
|
-
let psubscribed = new Promise((resolve, reject) => {
|
|
520
|
-
let first = true;
|
|
521
|
-
this.handlers.set(id, ({ error, data }) => {
|
|
522
|
-
if (error) {
|
|
523
|
-
if (first) {
|
|
524
|
-
first = false;
|
|
525
|
-
this.handlers.delete(id);
|
|
526
|
-
reject(error);
|
|
527
|
-
}
|
|
528
|
-
else
|
|
529
|
-
on_error(error);
|
|
530
|
-
return;
|
|
531
|
-
}
|
|
532
|
-
if (first) {
|
|
533
|
-
first = false;
|
|
534
|
-
resolve({ id, data: data });
|
|
535
|
-
return;
|
|
536
|
-
}
|
|
537
|
-
on_data(data);
|
|
538
|
-
});
|
|
539
|
-
});
|
|
540
|
-
try {
|
|
541
|
-
await this.send({ id, func }, websocket);
|
|
542
|
-
}
|
|
543
|
-
catch (error) {
|
|
544
|
-
this.handlers.delete(id);
|
|
545
|
-
throw error;
|
|
546
|
-
}
|
|
547
|
-
return psubscribed;
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
143
|
//# sourceMappingURL=net.browser.js.map
|
package/net.common.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { type Message } from './io.common.ts';
|
|
2
|
+
import { Lock, TimeoutError } from './utils.common.ts';
|
|
1
3
|
/** 对于 request() 函数来说无意义的 headers,会自动过滤掉 */
|
|
2
4
|
export declare const drop_request_headers: Set<string>;
|
|
3
5
|
export interface BasicAuth {
|
|
@@ -26,3 +28,179 @@ export interface RemoteKeeperOptions {
|
|
|
26
28
|
heartbeat_interval?: number;
|
|
27
29
|
error_delay?: number;
|
|
28
30
|
}
|
|
31
|
+
export declare const WebSocketConnecting = 0;
|
|
32
|
+
export declare const WebSocketOpen = 1;
|
|
33
|
+
export declare const WebSocketClosing = 2;
|
|
34
|
+
export declare const WebSocketClosed = 3;
|
|
35
|
+
export declare const websocket_states: readonly ["connecting", "open", "closing", "closed"];
|
|
36
|
+
export declare class WebSocketConnectionError extends Error {
|
|
37
|
+
name: "WebSocketConnectionError";
|
|
38
|
+
url: string;
|
|
39
|
+
protocols?: string[];
|
|
40
|
+
type?: 'close' | 'error';
|
|
41
|
+
/** close 事件时为 close code (非 1000 的 number), error 事件为 error code (可能是 string) */
|
|
42
|
+
code?: string | number;
|
|
43
|
+
reason?: string;
|
|
44
|
+
event?: CloseEvent | ErrorEvent;
|
|
45
|
+
address?: string;
|
|
46
|
+
errno?: number;
|
|
47
|
+
port?: number;
|
|
48
|
+
syscall?: string;
|
|
49
|
+
static get_reason_string(code: number | string, reason?: string): string | number;
|
|
50
|
+
constructor(url: string, protocols: string[] | undefined, event?: CloseEvent | ErrorEvent, message?: string);
|
|
51
|
+
}
|
|
52
|
+
/** 连接 websocket url, 设置各种事件监听器。在 open 事件后 resolve, 返回 websocket
|
|
53
|
+
遇到 error, close 事件时会创建 WebSocketConnectionError:
|
|
54
|
+
- reject 掉返回的 promise (若此时未 settle)
|
|
55
|
+
- 作为参数调用 on_error (已 settle 且有 on_error 回调)
|
|
56
|
+
可以用 WebSocket.bufferedAmount 来查询大消息的发送进度
|
|
57
|
+
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/bufferedAmount
|
|
58
|
+
- url
|
|
59
|
+
- options:
|
|
60
|
+
- print?: 是否打印连接、关闭信息
|
|
61
|
+
- protocols?
|
|
62
|
+
- max_payload?: `8 gb` (仅 nodejs 环境有效)
|
|
63
|
+
- proxy?: string (仅 nodejs 环境有效)
|
|
64
|
+
- on_message: 根据 websocket frame 的 opcode 不同 (text frame 或 binary frame),event 中的 data 对应为 ArrayBuffer 或者 string
|
|
65
|
+
https://datatracker.ietf.org/doc/html/rfc6455#section-5.2
|
|
66
|
+
- on_error?: 在 websocket 出错和非正常关闭 (close, error 事件) 时都调用,可以根据 error.type 来区分,error 的类型是 WebSocketConnectionError,
|
|
67
|
+
type 为 'close' 时有 code 和 reason 属性
|
|
68
|
+
- on_close?: 和 websocket 的 'close' 事件不相同,只在正常关闭 (close code 为 1000) 时才调用,否则都会调用 on_error
|
|
69
|
+
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes */
|
|
70
|
+
export declare function connect_websocket(url: string | URL, { print, protocols, max_payload, // 8 GB
|
|
71
|
+
proxy, on_message, on_error, on_close, }: {
|
|
72
|
+
print?: boolean;
|
|
73
|
+
protocols?: string[];
|
|
74
|
+
max_payload?: number;
|
|
75
|
+
proxy?: string;
|
|
76
|
+
on_message(data: ArrayBuffer | string, websocket: WebSocket): any;
|
|
77
|
+
on_error?(error: WebSocketConnectionError, websocket: WebSocket): any;
|
|
78
|
+
on_close?(event: CloseEvent, websocket: WebSocket): any;
|
|
79
|
+
}): Promise<WebSocket>;
|
|
80
|
+
/** 接收到消息后的处理函数
|
|
81
|
+
返回值会自动被 await,然后可能被封装为 message 回传 */
|
|
82
|
+
export type MessageHandler<TData = any> = (message: Message<TData>, remote: Remote) => void | any | Promise<void | any>;
|
|
83
|
+
export interface RemoteOptions {
|
|
84
|
+
/** 对端名称,方便接收端提供更明确的报错信息 */
|
|
85
|
+
name?: string;
|
|
86
|
+
/** 作为 websocket 连接发起方,对端的 url 地址 */
|
|
87
|
+
url?: string;
|
|
88
|
+
/** 作为 websocket 连接接收方,将已有的 websocket 连接转为 rpc 通道 */
|
|
89
|
+
websocket?: WebSocket;
|
|
90
|
+
/** 能被对端调用的函数 */
|
|
91
|
+
funcs?: Remote['funcs'];
|
|
92
|
+
/** `false` 是否启用主动探测连接,在连接状态下以 30s 间隔发送心跳包确保连接状态 */
|
|
93
|
+
probe?: boolean;
|
|
94
|
+
/** websocket 连接建立成功时调用对端 register 函数的参数 (仅发起方有效) */
|
|
95
|
+
args?: any[];
|
|
96
|
+
/** `true` 是否打印连接信息、错误信息 */
|
|
97
|
+
print?: boolean;
|
|
98
|
+
/** `false` 打印所有交互的 rpc messages */
|
|
99
|
+
verbose?: boolean;
|
|
100
|
+
/** 使用者自定义的在 websocket 连接出错时,或者 handlers 出错时的处理
|
|
101
|
+
用户设置后会覆盖默认的 print 错误功能 */
|
|
102
|
+
on_error?(error: WebSocketConnectionError | Error, remote: Remote): void;
|
|
103
|
+
}
|
|
104
|
+
/** 通过创建 remote 对象对 websocket rpc 进行抽象
|
|
105
|
+
使用 remote.call() 进行一元 rpc
|
|
106
|
+
使用 remote.send() 结合 message.id 进行复杂 rpc
|
|
107
|
+
|
|
108
|
+
可传入 funcs 注册函数,使得对端能调用,传入后,
|
|
109
|
+
连接发起方在检测到连接断开后间隔 2s (首次) / 10s 尝试重连
|
|
110
|
+
|
|
111
|
+
可启用 probe 选项启用主动探测连接,进一步确保连接可靠性
|
|
112
|
+
|
|
113
|
+
有两种创建方法:
|
|
114
|
+
- 传入 url 主动创建 websocket 连接
|
|
115
|
+
创建后等到首个 remote.call 或 remote.send 时建立实际 websocket 连接
|
|
116
|
+
- 传入 websocket,监听事件,将已有的 websocket 连接转为 rpc 通道
|
|
117
|
+
|
|
118
|
+
rpc 状态与底层连接的状态无关,如果是发起方,remote.send 时检测到断线会自动尝试建立新的 websocket 连接 */
|
|
119
|
+
export declare class Remote {
|
|
120
|
+
/** 对端名称,方便接收端提供更明确的报错信息 */
|
|
121
|
+
name?: string;
|
|
122
|
+
/** 作为 websocket 连接发起方,对端的 url 地址 */
|
|
123
|
+
url?: string;
|
|
124
|
+
/** 能被对端调用的函数 */
|
|
125
|
+
funcs: Record<string, MessageHandler>;
|
|
126
|
+
/** `false` 是否启用主动探测连接,在连接状态下以 30s 间隔发送心跳包确保连接状态 */
|
|
127
|
+
probe: boolean;
|
|
128
|
+
/** websocket 连接建立成功时调用对端 register 函数的参数 (仅发起方有效) */
|
|
129
|
+
args?: any[];
|
|
130
|
+
/** `true` 是否打印连接信息、错误信息 */
|
|
131
|
+
print: boolean;
|
|
132
|
+
/** `false` 打印所有交互的 rpc messages */
|
|
133
|
+
verbose: boolean;
|
|
134
|
+
/** 防止作为 websocket 连接发起方时并发创建 websocket 连接 */
|
|
135
|
+
lwebsocket: Lock<WebSocket>;
|
|
136
|
+
/** map<id, message handler>: 通过 (rpc message).id 找到对应的 handler
|
|
137
|
+
一元 rpc 接收方不需要设置 handlers, 发送方需要 */
|
|
138
|
+
handlers: Map<number, MessageHandler<any>>;
|
|
139
|
+
/** 是否正在定时主动检测重连 */
|
|
140
|
+
probing: boolean;
|
|
141
|
+
/** websocket 检测到错误,_on_error 被调用,此时会尝试一段时间后重连 */
|
|
142
|
+
reconnecting: NodeJS.Timeout;
|
|
143
|
+
/** 用户是否手动调用了 disconnect() 主动断开了连接 (仅发起方有效) */
|
|
144
|
+
disconnected: boolean;
|
|
145
|
+
/** on_error 接收到的,正常 websocket 连接发生的首个非 TimeoutError 错误 */
|
|
146
|
+
error: WebSocketConnectionError | Error;
|
|
147
|
+
/** 作为 websocket 连接发起方,传入 url
|
|
148
|
+
作为 websocket 连接接收方,传入 websocket + name */
|
|
149
|
+
constructor({ name, url, websocket, funcs, print, verbose, probe, args, on_error, }?: RemoteOptions);
|
|
150
|
+
/** 统一处理首次连接和连接后的 websocket 错误 */
|
|
151
|
+
_on_error: (error: WebSocketConnectionError | Error) => void;
|
|
152
|
+
reconnect: () => void;
|
|
153
|
+
_on_message: (data: ArrayBuffer) => void;
|
|
154
|
+
/** 使用者自定义的在 websocket 连接出错时,或者 handlers 出错时的处理
|
|
155
|
+
用户设置后会覆盖默认的 print 错误功能 */
|
|
156
|
+
on_error(error: WebSocketConnectionError | Error, remote: Remote): void;
|
|
157
|
+
print_error(error: WebSocketConnectionError | Error): void;
|
|
158
|
+
/** 检查是否已经有 error, websocket 状态是否正常 (open | connecting)
|
|
159
|
+
返回 true: 连接正常,false: 连接异常 */
|
|
160
|
+
check_connection(): boolean;
|
|
161
|
+
/** 幂等,保证 websocket 已连接,否则抛出异常
|
|
162
|
+
通常用于手动建立 websocket 连接并确保成功
|
|
163
|
+
连接断开后,作为发起方会自动创建新的 websocket 连接;
|
|
164
|
+
作为接收方只会检查连接状态,在断开时抛出异常
|
|
165
|
+
保存的 rpc 状态在 this.handlers, 与 websocket 无关,因此即使断开重连也不影响 rpc 的运行,即
|
|
166
|
+
底层连接断开后自动重连对上层应该是无感知的,除非再次连接时失败 */
|
|
167
|
+
connect(): Promise<void>;
|
|
168
|
+
_connect: () => Promise<void>;
|
|
169
|
+
/** 尝试建立连接,通常用于开始保活
|
|
170
|
+
重连错误会在 this.connect 里面调用 this._on_error 处理 */
|
|
171
|
+
try_connect(): Promise<void>;
|
|
172
|
+
/** 开始心跳保活 */
|
|
173
|
+
probe_connection(print_timeout?: boolean): Promise<void>;
|
|
174
|
+
/** 检测连接,返回连接错误,出错时触发 on_error 打印错误信息
|
|
175
|
+
返回值:
|
|
176
|
+
- 连接正常返回: undefined
|
|
177
|
+
- 连接错误返回: 实际的连接错误,通常是 WebSocketConnectionError | TimeoutError */
|
|
178
|
+
test(print_timeout?: boolean): Promise<WebSocketConnectionError | TimeoutError | Error>;
|
|
179
|
+
/** 手动关闭到对端的 websocket 连接 */
|
|
180
|
+
disconnect(): void;
|
|
181
|
+
/** 发送 message 到对端 remote
|
|
182
|
+
发送或连接出错时自动清理 message.id 对应的 handler */
|
|
183
|
+
send(message: Message): Promise<void>;
|
|
184
|
+
/** 能发就发一下,忽略发送失败, best effort */
|
|
185
|
+
try_send(message: Message): void;
|
|
186
|
+
/** 处理接收到的 websocket message 并解析, 根据 message.id 或 message.func 分发到对应的 handler 进行处理,
|
|
187
|
+
handler 处理完成后:
|
|
188
|
+
- 传了 func: 调用函数的情况下 (通常是一元 rpc),总是将返回值包装为 message 回传
|
|
189
|
+
- 未传 func: 通过 id 调用,如果 handler 返回非 undefined 的值,也包装为 message 回传
|
|
190
|
+
|
|
191
|
+
如果 message.done == true 则对端指示当前 remote 可以清理 handler
|
|
192
|
+
使用 Uint8Array 作为参数更灵活 https://stackoverflow.com/a/74505197/7609214
|
|
193
|
+
这个方法一般不会抛出错误,也不需要 await,一般在 websocket on_message 时使用 */
|
|
194
|
+
handle(data: Uint8Array): Promise<void>;
|
|
195
|
+
/** 调用对端 remote 中的 func, 只适用于最简单的一元 rpc (请求, 响应) */
|
|
196
|
+
call<TReturn = any>(func: string, args?: any[]): Promise<TReturn>;
|
|
197
|
+
/** 调用对端 remote 中的 func, 开始订阅并接收连续的消息 (订阅流)
|
|
198
|
+
- func: 订阅处理函数
|
|
199
|
+
- on_data: 接收开始订阅后的数据
|
|
200
|
+
- options?:
|
|
201
|
+
- on_error?: 处理开始订阅后的错误 */
|
|
202
|
+
subscribe<TData, TSubscribed = void>(func: string, on_data: (data: TData) => void, on_error?: Remote['on_error']): Promise<{
|
|
203
|
+
id: number;
|
|
204
|
+
data: TSubscribed;
|
|
205
|
+
}>;
|
|
206
|
+
}
|