xshell 1.3.5 → 1.3.6

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/net.d.ts CHANGED
@@ -2,7 +2,7 @@ import net from 'node:net';
2
2
  import { type Readable } from 'node:stream';
3
3
  import type { Cookie, CookieJar, MemoryCookieStore } from 'tough-cookie';
4
4
  import type { Encoding } from './file.ts';
5
- import { inspect, type Deferred2 } from './utils.ts';
5
+ import { inspect, type Deferred2, type OnTimeout } from './utils.ts';
6
6
  import { type BasicAuth, type BearerAuth } from './net.common.ts';
7
7
  export * from './net.common.ts';
8
8
  export declare enum MyProxy {
@@ -111,7 +111,9 @@ export declare function request(url: string | URL, options: RequestOptions): Pro
111
111
  /** 发起 http 请求并将响应体作为 json 解析 */
112
112
  export declare function request_json<T = any>(url: string | URL, options?: RequestOptions): Promise<T>;
113
113
  export interface ConnectOptions {
114
+ local_port?: number;
114
115
  timeout?: number;
116
+ on_timeout?: OnTimeout;
115
117
  print?: {
116
118
  error: boolean;
117
119
  connect: boolean;
@@ -119,12 +121,14 @@ export interface ConnectOptions {
119
121
  }
120
122
  /** 建立 tcp 连接,超时时间为 2 秒 (timeout 选项),返回已连接的 socket
121
123
  连接中遇到的错误会 reject,之后取消监听 error 事件,由调用者 attach 处理
124
+ 连接超时则自动 socket.resetAndDestroy
122
125
  - hostname: 要连接到的 ip 或域名
123
126
  - port: 要连接的端口
124
127
  - options?:
128
+ - local_port?: `随机端口` 使用的本地端口
125
129
  - timeout?: `2000` 超时时间
126
130
  - print?: 打印连接日志,错误日志
127
131
  - error?: `true`
128
132
  - connect: `false` */
129
- export declare function connect(hostname: string, port: number, { timeout: _timeout, print }?: ConnectOptions): Deferred2<net.Socket>;
133
+ export declare function connect(hostname: string, port: number, { local_port, timeout: _timeout, on_timeout, print }?: ConnectOptions): Deferred2<net.Socket>;
130
134
  export declare function get_socket_ip(socket: net.Socket): string;
package/net.js CHANGED
@@ -295,14 +295,16 @@ export async function request_json(url, options) {
295
295
  }
296
296
  /** 建立 tcp 连接,超时时间为 2 秒 (timeout 选项),返回已连接的 socket
297
297
  连接中遇到的错误会 reject,之后取消监听 error 事件,由调用者 attach 处理
298
+ 连接超时则自动 socket.resetAndDestroy
298
299
  - hostname: 要连接到的 ip 或域名
299
300
  - port: 要连接的端口
300
301
  - options?:
302
+ - local_port?: `随机端口` 使用的本地端口
301
303
  - timeout?: `2000` 超时时间
302
304
  - print?: 打印连接日志,错误日志
303
305
  - error?: `true`
304
306
  - connect: `false` */
305
- export function connect(hostname, port, { timeout: _timeout = 2000, print = { error: true, connect: false } } = {}) {
307
+ export function connect(hostname, port, { local_port, timeout: _timeout = 2000, on_timeout, print = { error: true, connect: false } } = {}) {
306
308
  let pconnected = defer2();
307
309
  let timeouted = false;
308
310
  function get_message(message) {
@@ -311,6 +313,7 @@ export function connect(hostname, port, { timeout: _timeout = 2000, print = { er
311
313
  let socket = net.connect({
312
314
  host: hostname,
313
315
  port,
316
+ localPort: local_port,
314
317
  noDelay: true
315
318
  }, () => {
316
319
  if (pconnected.settled) {
@@ -341,11 +344,21 @@ export function connect(hostname, port, { timeout: _timeout = 2000, print = { er
341
344
  }
342
345
  };
343
346
  socket.once('error', on_error);
344
- let timeout = setTimeout(() => {
347
+ let timeout = setTimeout(async () => {
345
348
  // 执行到这里一定没有 settled
346
349
  timeouted = true;
347
350
  socket.resetAndDestroy();
348
- pconnected.reject(new TimeoutError(get_message('超时了')));
351
+ if (typeof on_timeout === 'function') {
352
+ pconnected.resolve(null);
353
+ try {
354
+ await on_timeout(new TimeoutError(get_message('超时了')));
355
+ }
356
+ catch (error) {
357
+ pconnected.reject(error);
358
+ }
359
+ }
360
+ else
361
+ pconnected.reject(new TimeoutError(on_timeout || get_message('超时了')));
349
362
  }, _timeout);
350
363
  return pconnected;
351
364
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xshell",
3
- "version": "1.3.5",
3
+ "version": "1.3.6",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "bin": {
package/path.d.ts CHANGED
@@ -54,7 +54,7 @@ export declare function extname(path: string): string;
54
54
  /** `/` */
55
55
  export declare const sep = "/";
56
56
  /** The platform-specific file delimiter. ';' or ':'. */
57
- export declare const delimiter: ";" | ":";
57
+ export declare const delimiter: ":" | ";";
58
58
  /** Returns an object from a path string - the opposite of format().
59
59
  @param path path to evaluate.
60
60
  @throws {TypeError} if `path` is not a string. */
@@ -81,7 +81,7 @@ export declare let path: {
81
81
  basename: typeof basename;
82
82
  extname: typeof extname;
83
83
  sep: string;
84
- delimiter: ";" | ":";
84
+ delimiter: ":" | ";";
85
85
  parse: typeof parse;
86
86
  format: typeof format;
87
87
  toNamespacedPath: typeof toNamespacedPath;
package/utils.common.d.ts CHANGED
@@ -63,6 +63,7 @@ export declare function decode(buffer: Uint8Array): string;
63
63
  /** 比较两个 buffer 内容是否相同,第二个可以传入 string 自动编码转换后比较,
64
64
  高频调用时建议提前编码 right 并缓存 */
65
65
  export declare function buffer_equals(left: Uint8Array, right: Uint8Array | string): boolean;
66
+ export type OnTimeout = string | ((error: TimeoutError) => void | Promise<void>);
66
67
  /** 在指定的时间 (milliseconds) 内运行某个任务,超时之后抛出错误或调用 on_timeout
67
68
  - milliseconds: 限时毫秒数
68
69
  - action?: 要等待运行的任务, async function 或 promise
@@ -74,7 +75,7 @@ export declare function buffer_equals(left: Uint8Array, right: Uint8Array | stri
74
75
  - on_timeout 报错: timeout 函数最终抛出这个错误
75
76
  - 若不传: 直接抛出 TimeoutError
76
77
  - print?: `true` 打印已超时任务的错误 */
77
- export declare function timeout<TReturn>(milliseconds: number, action: Promise<TReturn> | (() => Promise<TReturn>), on_timeout?: string | ((error: TimeoutError) => void | Promise<void>), print?: boolean): Deferred2<TReturn>;
78
+ export declare function timeout<TReturn>(milliseconds: number, action: Promise<TReturn> | (() => Promise<TReturn>), on_timeout?: OnTimeout, print?: boolean): Deferred2<TReturn>;
78
79
  /** 轮询尝试 action 共 times 次,每次间隔 duration
79
80
  action 返回 trusy 值时认为成功,返回 action 的结果
80
81
  如果次数用尽仍然失败,返回 null */