xshell 1.2.71 → 1.2.73

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/utils.common.d.ts CHANGED
@@ -1,4 +1,6 @@
1
+ import type { TimerOptions } from 'timers';
1
2
  import { type Mapper } from './prototype.common.ts';
3
+ export declare function delay(milliseconds: number, options?: TimerOptions): Promise<void>;
2
4
  export declare function assert<T>(assertion: T, message?: string): T;
3
5
  /** 做参数校验,逻辑检查 */
4
6
  export declare function check<T>(condition: T, message?: string): T;
@@ -49,6 +51,30 @@ export declare function filter_values<TObj extends Record<string, any>>(obj: TOb
49
51
  export declare function pick<TObject = any>(obj: TObject, keys: (keyof TObject)[]): Partial<TObject>;
50
52
  /** 忽略对象中的 keys, 返回新对象 */
51
53
  export declare function omit<TObj>(obj: TObj, omit_keys: string[]): TObj;
54
+ /** 拼接 TypedArrays 生成一个完整的 Uint8Array */
55
+ export declare function concat(arrays: ArrayBufferView[]): Uint8Array<ArrayBufferLike>;
56
+ export declare let encoder: TextEncoder;
57
+ /** 将字符串简单的编码为 utf-8 的 buffer (Uint8Array)。高频使用或者在流式处理时,考虑使用 TextEncoder 的 encodeInto 方法 */
58
+ export declare function encode(str: string): Uint8Array<ArrayBufferLike>;
59
+ export declare function encode_into(str: string, buf: Uint8Array): TextEncoderEncodeIntoResult;
60
+ /** 将 utf-8 buffer (Uint8Array) 简单的解码为 string。
61
+ 在流式处理 (buffer 可能不完整) 时,应使用独立的 TextDecoder 实例调用 decode(buffer, { stream: true }) */
62
+ export declare function decode(buffer: Uint8Array): string;
63
+ /** 比较两个 buffer 内容是否相同,第二个可以传入 string 自动编码转换后比较,
64
+ 高频调用时建议提前编码 right 并缓存 */
65
+ export declare function buffer_equals(left: Uint8Array, right: Uint8Array | string): boolean;
66
+ /** 在指定的时间 (milliseconds) 内运行某个任务,超时之后抛出错误或调用 on_timeout
67
+ - milliseconds: 限时毫秒数
68
+ - action?: 要等待运行的任务, async function 或 promise
69
+ - on_timeout?: 超时后调用的函数
70
+ - 如果传入了 on_timeout 参数: 调用 on_timeout,然后 timeout 函数正常返回 null
71
+ - 如果没传入 on_timeout 参数: 抛出 TimeoutError
72
+ - print?: 打印已超时任务的错误 */
73
+ export declare function timeout<TReturn>(milliseconds: number, action: Promise<TReturn> | (() => Promise<TReturn>), on_timeout?: () => void | Promise<void>, print?: boolean): Promise<TReturn>;
74
+ /** 轮询尝试 action 共 times 次,每次间隔 duration
75
+ action 返回 trusy 值时认为成功,返回 action 的结果
76
+ 如果次数用尽仍然失败,返回 null */
77
+ export declare function poll<TResult>(duration: number, times: number, action: (breaker: () => void) => Promise<TResult>): Promise<TResult>;
52
78
  /** 模糊过滤字符串列表或对象列表,常用于根据用户输入补全或搜索过滤
53
79
  如果有完全匹配的,只返回那一项的数组
54
80
  - query: 查询字符串,要求为全小写
@@ -127,11 +153,6 @@ export declare class Lock<TResource = void> {
127
153
  /** 通过 await lock.request() 锁定资源以便独占访问,锁定资源后在 action 回调中操作资源,回调 promise 完成 (fullfilled 或 rejected) 后自动释放资源 */
128
154
  request<TResult>(action: LockedAction<TResource, TResult>, signal?: AbortSignal): Promise<TResult>;
129
155
  }
130
- export declare let encoder: TextEncoder;
131
- export declare function encode_into(str: string, buf: Uint8Array): TextEncoderEncodeIntoResult;
132
- /** 将 utf-8 buffer (Uint8Array) 简单的解码为 string。
133
- 在流式处理 (buffer 可能不完整) 时,应使用独立的 TextDecoder 实例调用 decode(buffer, { stream: true }) */
134
- export declare function decode(buffer: Uint8Array): string;
135
156
  /** 过滤符合 pattern 的行 */
136
157
  export declare function grep(str: string, pattern: string | RegExp): string;
137
158
  export declare function lowercase_first_letter(str: string): string;
package/utils.common.js CHANGED
@@ -1,5 +1,9 @@
1
1
  import { t } from "./i18n/instance.js";
2
2
  import { ident, is_key_type, noop, not_empty, select, to_snake_case } from "./prototype.common.js";
3
+ import { platform } from "./platform.common.js";
4
+ export async function delay(milliseconds, options) {
5
+ return platform.delay(milliseconds, options);
6
+ }
3
7
  export function assert(assertion, message) {
4
8
  if (!assertion) {
5
9
  debugger;
@@ -135,6 +139,118 @@ export function omit(obj, omit_keys) {
135
139
  const omit_keys_ = new Set(omit_keys);
136
140
  return filter_keys(obj, key => !omit_keys_.has(key));
137
141
  }
142
+ /** 拼接 TypedArrays 生成一个完整的 Uint8Array */
143
+ export function concat(arrays) {
144
+ let length = 0;
145
+ for (const a of arrays)
146
+ length += a.byteLength;
147
+ let buf = platform.get_buffer(length);
148
+ let offset = 0;
149
+ for (const a of arrays) {
150
+ const uint8view = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
151
+ buf.set(uint8view, offset);
152
+ offset += uint8view.byteLength;
153
+ }
154
+ return buf;
155
+ }
156
+ export let encoder = new TextEncoder();
157
+ /** 将字符串简单的编码为 utf-8 的 buffer (Uint8Array)。高频使用或者在流式处理时,考虑使用 TextEncoder 的 encodeInto 方法 */
158
+ export function encode(str) {
159
+ return platform.encode(str);
160
+ }
161
+ export function encode_into(str, buf) {
162
+ // 这个是直接用 v8 String::WriteUtf8 最高效的方法了
163
+ return encoder.encodeInto(str, buf);
164
+ }
165
+ let decoder = new TextDecoder();
166
+ /** 将 utf-8 buffer (Uint8Array) 简单的解码为 string。
167
+ 在流式处理 (buffer 可能不完整) 时,应使用独立的 TextDecoder 实例调用 decode(buffer, { stream: true }) */
168
+ export function decode(buffer) {
169
+ return decoder.decode(buffer);
170
+ }
171
+ /** 比较两个 buffer 内容是否相同,第二个可以传入 string 自动编码转换后比较,
172
+ 高频调用时建议提前编码 right 并缓存 */
173
+ export function buffer_equals(left, right) {
174
+ if (typeof right === 'string')
175
+ right = encode(right);
176
+ if (left.length !== right.length)
177
+ return false;
178
+ for (let i = 0; i < left.length; i++)
179
+ if (left[i] !== right[i])
180
+ return false;
181
+ return true;
182
+ }
183
+ /** 在指定的时间 (milliseconds) 内运行某个任务,超时之后抛出错误或调用 on_timeout
184
+ - milliseconds: 限时毫秒数
185
+ - action?: 要等待运行的任务, async function 或 promise
186
+ - on_timeout?: 超时后调用的函数
187
+ - 如果传入了 on_timeout 参数: 调用 on_timeout,然后 timeout 函数正常返回 null
188
+ - 如果没传入 on_timeout 参数: 抛出 TimeoutError
189
+ - print?: 打印已超时任务的错误 */
190
+ export async function timeout(milliseconds, action, on_timeout, print = true) {
191
+ const error = new TimeoutError();
192
+ return new Promise((resolve, reject) => {
193
+ let done = false;
194
+ let rejected = false;
195
+ (async () => {
196
+ await delay(milliseconds);
197
+ if (!done)
198
+ if (on_timeout)
199
+ try {
200
+ await on_timeout();
201
+ resolve(null);
202
+ }
203
+ catch (error) {
204
+ if (rejected)
205
+ throw error; // 会成为 unhandled rejection
206
+ else {
207
+ rejected = true;
208
+ reject(error);
209
+ }
210
+ }
211
+ else {
212
+ rejected = true;
213
+ reject(error);
214
+ }
215
+ })();
216
+ (async () => {
217
+ try {
218
+ resolve(await (typeof action === 'function' ? action() : action));
219
+ }
220
+ catch (error) {
221
+ if (rejected) {
222
+ if (print)
223
+ console.log(`已超时任务的错误: ${error.message}`);
224
+ }
225
+ else {
226
+ rejected = true;
227
+ reject(error);
228
+ }
229
+ }
230
+ finally {
231
+ done = true;
232
+ }
233
+ })();
234
+ });
235
+ }
236
+ /** 轮询尝试 action 共 times 次,每次间隔 duration
237
+ action 返回 trusy 值时认为成功,返回 action 的结果
238
+ 如果次数用尽仍然失败,返回 null */
239
+ export async function poll(duration, times, action) {
240
+ let break_flag = false;
241
+ function _break() {
242
+ break_flag = true;
243
+ }
244
+ for (let i = 0; i < times; ++i) {
245
+ const result = await action(_break);
246
+ if (result)
247
+ return result;
248
+ if (break_flag)
249
+ break;
250
+ await delay(duration);
251
+ }
252
+ return null;
253
+ }
138
254
  /** 模糊过滤字符串列表或对象列表,常用于根据用户输入补全或搜索过滤
139
255
  如果有完全匹配的,只返回那一项的数组
140
256
  - query: 查询字符串,要求为全小写
@@ -334,17 +450,6 @@ export class Lock {
334
450
  });
335
451
  }
336
452
  }
337
- export let encoder = new TextEncoder();
338
- export function encode_into(str, buf) {
339
- // 这个是直接用 v8 String::WriteUtf8 最高效的方法了
340
- return encoder.encodeInto(str, buf);
341
- }
342
- let decoder = new TextDecoder();
343
- /** 将 utf-8 buffer (Uint8Array) 简单的解码为 string。
344
- 在流式处理 (buffer 可能不完整) 时,应使用独立的 TextDecoder 实例调用 decode(buffer, { stream: true }) */
345
- export function decode(buffer) {
346
- return decoder.decode(buffer);
347
- }
348
453
  /** 过滤符合 pattern 的行 */
349
454
  export function grep(str, pattern) {
350
455
  return str.split_lines()
package/utils.d.ts CHANGED
@@ -1,33 +1,20 @@
1
1
  import { Writable, Transform, type Readable, type Duplex, type TransformCallback } from 'stream';
2
2
  import util from 'util';
3
- import type { TimerOptions } from 'timers';
4
3
  import './prototype.ts';
4
+ import './platform.ts';
5
5
  export * from './utils.common.ts';
6
6
  /** `180` 输出字符宽度 */
7
7
  export declare const output_width = 180;
8
8
  export declare const url_width = 52;
9
9
  export declare function set_inspect_options(colors?: boolean): void;
10
- /** 拼接 TypedArrays 生成一个完整的 Uint8Array */
11
- export declare function concat(arrays: ArrayBufferView[]): Buffer<ArrayBuffer>;
12
10
  export declare function typed_array_to_buffer(view: ArrayBufferView): Buffer<ArrayBufferLike>;
13
11
  /** 每天在固定时间执行操作
14
12
  - hour: 0 - 23 之间的整数,在这个点执行
15
13
  - action */
16
14
  export declare function schedule_everyday(hour: number, action: () => Promise<void>): Promise<void>;
17
15
  export declare function log_line(): void;
18
- export declare function delay(milliseconds: number, options?: TimerOptions): Promise<void>;
19
- /** 在指定的时间 (milliseconds) 内运行某个任务,超时之后抛出错误或调用 on_timeout
20
- - milliseconds: 限时毫秒数
21
- - action?: 要等待运行的任务, async function 或 promise
22
- - on_timeout?: 超时后调用的函数
23
- - 如果传入了 on_timeout 参数: 调用 on_timeout,然后 timeout 函数正常返回 null
24
- - 如果没传入 on_timeout 参数: 抛出 TimeoutError
25
- - print?: 打印已超时任务的错误 */
26
- export declare function timeout<TReturn>(milliseconds: number, action: Promise<TReturn> | (() => Promise<TReturn>), on_timeout?: () => void | Promise<void>, print?: boolean): Promise<TReturn>;
27
16
  export declare function sha256(data: string | Uint8Array): string;
28
17
  export declare function sha1(data: string | Uint8Array): string;
29
- /** 将字符串简单的编码为 utf-8 的 buffer (Uint8Array)。高频使用或者在流式处理时,考虑使用 TextEncoder 的 encodeInto 方法 */
30
- export declare function encode(str: string): Buffer<ArrayBuffer>;
31
18
  export declare function has_chinese(str: string): boolean;
32
19
  export declare function escape_line_feed(str: string): string;
33
20
  /** util.inspect(obj)
@@ -86,10 +73,3 @@ export declare function consume_stream(stream: Readable, ignore_error?: boolean)
86
73
  - reverse?: `false` 在 range 内从后往前生成 */
87
74
  export declare function range_to_numbers(range: string, reverse?: boolean): Generator<number, void, unknown>;
88
75
  export declare function decode_buffer_hex_string(str: string): string;
89
- /** 轮询尝试 action 共 times 次,每次间隔 duration
90
- action 返回 trusy 值时认为成功,返回 action 的结果
91
- 如果次数用尽仍然失败,返回 null */
92
- export declare function poll<TResult>(duration: number, times: number, action: (breaker: () => void) => Promise<TResult>): Promise<TResult>;
93
- /** 比较两个 buffer 内容是否相同,第二个可以传入 string 自动编码转换后比较,
94
- 高频调用时建议提前编码 right 并缓存 */
95
- export declare function buffer_equals(left: Uint8Array, right: Uint8Array | string): boolean;
package/utils.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import { Stream, Writable, Transform } from 'stream';
2
2
  import util from 'util';
3
3
  import ncrypto from 'crypto';
4
- import timers from 'timers/promises';
5
4
  import "./prototype.js";
6
- import { assert, decode, defer, TimeoutError } from "./utils.common.js";
5
+ import "./platform.js";
6
+ import { assert, decode, defer, delay } from "./utils.common.js";
7
7
  export * from "./utils.common.js";
8
8
  /** `180` 输出字符宽度 */
9
9
  export const output_width = 180;
@@ -24,20 +24,6 @@ export function set_inspect_options(colors = true) {
24
24
  util.inspect.styles.date = 'magenta';
25
25
  util.inspect.styles.special = 'white';
26
26
  }
27
- /** 拼接 TypedArrays 生成一个完整的 Uint8Array */
28
- export function concat(arrays) {
29
- let length = 0;
30
- for (const a of arrays)
31
- length += a.byteLength;
32
- let buf = Buffer.allocUnsafe(length);
33
- let offset = 0;
34
- for (const a of arrays) {
35
- const uint8view = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
36
- buf.set(uint8view, offset);
37
- offset += uint8view.byteLength;
38
- }
39
- return buf;
40
- }
41
27
  export function typed_array_to_buffer(view) {
42
28
  return Buffer.from(view.buffer, view.byteOffset, view.byteLength);
43
29
  }
@@ -62,73 +48,12 @@ export async function schedule_everyday(hour, action) {
62
48
  export function log_line() {
63
49
  console.log('---');
64
50
  }
65
- export async function delay(milliseconds, options) {
66
- return timers.setTimeout(milliseconds, undefined, options);
67
- }
68
- /** 在指定的时间 (milliseconds) 内运行某个任务,超时之后抛出错误或调用 on_timeout
69
- - milliseconds: 限时毫秒数
70
- - action?: 要等待运行的任务, async function 或 promise
71
- - on_timeout?: 超时后调用的函数
72
- - 如果传入了 on_timeout 参数: 调用 on_timeout,然后 timeout 函数正常返回 null
73
- - 如果没传入 on_timeout 参数: 抛出 TimeoutError
74
- - print?: 打印已超时任务的错误 */
75
- export async function timeout(milliseconds, action, on_timeout, print = true) {
76
- const error = new TimeoutError();
77
- return new Promise((resolve, reject) => {
78
- let done = false;
79
- let rejected = false;
80
- (async () => {
81
- await delay(milliseconds);
82
- if (!done)
83
- if (on_timeout)
84
- try {
85
- await on_timeout();
86
- resolve(null);
87
- }
88
- catch (error) {
89
- if (rejected)
90
- throw error; // 会成为 unhandled rejection
91
- else {
92
- rejected = true;
93
- reject(error);
94
- }
95
- }
96
- else {
97
- rejected = true;
98
- reject(error);
99
- }
100
- })();
101
- (async () => {
102
- try {
103
- resolve(await (typeof action === 'function' ? action() : action));
104
- }
105
- catch (error) {
106
- if (rejected) {
107
- if (print)
108
- console.log(`已超时任务的错误: ${error.message}`);
109
- }
110
- else {
111
- rejected = true;
112
- reject(error);
113
- }
114
- }
115
- finally {
116
- done = true;
117
- }
118
- })();
119
- });
120
- }
121
51
  export function sha256(data) {
122
52
  return ncrypto.hash('sha256', data);
123
53
  }
124
54
  export function sha1(data) {
125
55
  return ncrypto.hash('sha1', data);
126
56
  }
127
- /** 将字符串简单的编码为 utf-8 的 buffer (Uint8Array)。高频使用或者在流式处理时,考虑使用 TextEncoder 的 encodeInto 方法 */
128
- export function encode(str) {
129
- // 用 Buffer.from 是因为可以利用 buffer pool,避免 encoder.encode 创建大量小且独立的 array buffer
130
- return Buffer.from(str);
131
- }
132
57
  export function has_chinese(str) {
133
58
  return /[\u4E00-\u9FA5]/.test(str);
134
59
  }
@@ -377,34 +302,4 @@ export function decode_buffer_hex_string(str) {
377
302
  return decode(Buffer.from(str.split(' ')
378
303
  .map(x => parseInt(x, 16))));
379
304
  }
380
- /** 轮询尝试 action 共 times 次,每次间隔 duration
381
- action 返回 trusy 值时认为成功,返回 action 的结果
382
- 如果次数用尽仍然失败,返回 null */
383
- export async function poll(duration, times, action) {
384
- let break_flag = false;
385
- function _break() {
386
- break_flag = true;
387
- }
388
- for (let i = 0; i < times; ++i) {
389
- const result = await action(_break);
390
- if (result)
391
- return result;
392
- if (break_flag)
393
- break;
394
- await delay(duration);
395
- }
396
- return null;
397
- }
398
- /** 比较两个 buffer 内容是否相同,第二个可以传入 string 自动编码转换后比较,
399
- 高频调用时建议提前编码 right 并缓存 */
400
- export function buffer_equals(left, right) {
401
- if (typeof right === 'string')
402
- right = encode(right);
403
- if (left.length !== right.length)
404
- return false;
405
- for (let i = 0; i < left.length; i++)
406
- if (left[i] !== right[i])
407
- return false;
408
- return true;
409
- }
410
305
  //# sourceMappingURL=utils.js.map
package/xlint.js CHANGED
@@ -840,7 +840,7 @@ export const xlint_config = {
840
840
  // 用单引号
841
841
  '@stylistic/jsx-quotes': ['error', 'prefer-single'],
842
842
  // 用单引号
843
- '@stylistic/quotes': ['error', 'single', { avoidEscape: true, allowTemplateLiterals: false }],
843
+ '@stylistic/quotes': ['error', 'single', { avoidEscape: true, allowTemplateLiterals: 'avoidEscape' }],
844
844
  // 不要冗余的引号包裹属性
845
845
  '@stylistic/quote-props': ['error', 'as-needed', { keywords: false, unnecessary: true }],
846
846
  // ------------ 其它
package/io.browser.d.ts DELETED
@@ -1,22 +0,0 @@
1
- import './prototype.browser.ts';
2
- /** 二进制消息格式, 按 key 的顺序序列化值 */
3
- export interface Message<TData = any> {
4
- /** rpc id: 在 rpc 系统中认为是唯一的。用来在单个 websocket 连接上同时进行多个 rpc 请求。
5
- 多个相同 id 的 message 组成一个请求流 */
6
- id?: number;
7
- /** 只在 rpc 发起时指定被调用的 function name,发起时 rpc 时必传 */
8
- func?: string;
9
- /** 传输的数据
10
- - rpc 发起方调用 func 的参数(此时为数组),或者请求流 message 携带的数据
11
- - 调用结果(通常为单个返回值)或者响应流的数据,传给请求发起方 */
12
- data?: TData;
13
- /** 通知对方这里产生的错误,本质上类似 data 也是一种数据,并不代表 rpc 的结束,后续可能继续有 rpc message 交换 */
14
- error?: Error;
15
- /** 通过这个 flag 主动表明这是发往对方的最后一个 message, 对方可以销毁 handler 了
16
- 并非强制,可以设置,由双方的函数自己约定 */
17
- done?: boolean;
18
- }
19
- /** 用这个符号来标识 message 对象 */
20
- export declare const message_symbol: unique symbol;
21
- export declare function parse<TReturn>(_buf: Uint8Array): TReturn;
22
- export declare function pack(obj: any): Uint8Array<ArrayBuffer>;