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/file.d.ts CHANGED
@@ -78,6 +78,7 @@ export declare function fequals(fp_left: string, fp_right: string, { print }?: {
78
78
  export interface FWriteOptions {
79
79
  print?: boolean;
80
80
  mtime?: number;
81
+ flush?: boolean;
81
82
  }
82
83
  /** 写入 data 到 fp 路径或句柄所指的文件
83
84
  会在因不存在父文件夹导致写入失败时,自动创建父文件夹,并再次尝试写入
@@ -90,7 +91,8 @@ export interface FWriteOptions {
90
91
  - any: 通过 JSON.stringify 转为文本后写入文件
91
92
  - options?:
92
93
  - print?: `true`
93
- - mtime?: 在写入后设置修改时间 (utc 毫秒数) */
94
+ - mtime?: 在写入后设置修改时间 (utc 毫秒数)
95
+ - flush?: 用 filehandle.sync() 刷到硬盘,确保已写入 */
94
96
  export declare function fwrite(fp: string, data: string | Uint8Array | any, options?: FWriteOptions): Promise<string>;
95
97
  export declare function fwrite(fp: FileHandle, data: string | Uint8Array | any, options?: FWriteOptions): Promise<FileHandle>;
96
98
  export declare function fwrite(fp: string | FileHandle, data: string | Uint8Array | any, options?: FWriteOptions): Promise<string | FileHandle>;
package/file.js CHANGED
@@ -2,7 +2,7 @@ import { promises as fsp, default as fs } from 'fs';
2
2
  import { isArrayBuffer, isUint8Array } from 'util/types';
3
3
  import { t } from "./i18n/instance.js";
4
4
  import { noop, to_json } from "./prototype.js";
5
- import { pack, parse } from "./io.js";
5
+ import { pack, parse } from "./io.common.js";
6
6
  import { path } from "./path.js";
7
7
  import { check, Lock, WritableMemoryStream, url_width, throttle, decode, strcmp } from "./utils.js";
8
8
  import { noprint } from "./process.js";
@@ -87,7 +87,7 @@ export async function fequals(fp_left, fp_right, { print = true } = {}) {
87
87
  return false;
88
88
  }
89
89
  }
90
- export async function fwrite(fp, data, { print = true, mtime } = {}) {
90
+ export async function fwrite(fp, data, { print = true, mtime, flush } = {}) {
91
91
  const is_handle = typeof fp === 'object' && fp && 'fd' in fp;
92
92
  if (is_handle) {
93
93
  if (print)
@@ -113,13 +113,14 @@ export async function fwrite(fp, data, { print = true, mtime } = {}) {
113
113
  }
114
114
  return fp;
115
115
  }
116
+ const write_options = { flush };
116
117
  try {
117
- await fsp.writeFile(fp, data);
118
+ await fsp.writeFile(fp, data, write_options);
118
119
  }
119
120
  catch (error) {
120
121
  if (error.code === 'ENOENT' && !is_handle) {
121
122
  await fmkdir(fp.fdir, noprint);
122
- await fsp.writeFile(fp, data);
123
+ await fsp.writeFile(fp, data, write_options);
123
124
  }
124
125
  else
125
126
  throw error;
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from './prototype.ts';
2
- export * from './io.ts';
2
+ export * from './io.common.ts';
3
3
  export * from './utils.ts';
4
4
  export * from './file.ts';
5
5
  export * from './process.ts';
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from "./prototype.js";
2
- export * from "./io.js";
2
+ export * from "./io.common.js";
3
3
  export * from "./utils.js";
4
4
  export * from "./file.js";
5
5
  export * from "./process.js";
@@ -1,4 +1,4 @@
1
- import './prototype.ts';
1
+ import './prototype.common.ts';
2
2
  /** 二进制消息格式, 按 key 的顺序序列化值 */
3
3
  export interface Message<TData = any> {
4
4
  /** rpc id: 在 rpc 系统中认为是唯一的。用来在单个 websocket 连接上同时进行多个 rpc 请求。
@@ -1,5 +1,5 @@
1
- import "./prototype.browser.js";
2
- import { decode, encode_into, ceil2, seq } from "./utils.browser.js";
1
+ import "./prototype.common.js";
2
+ import { decode, encode_into, ceil2, seq } from "./utils.common.js";
3
3
  // 类型 | 编码
4
4
  // --------------- | -------------------
5
5
  // small int + | 0x00 - 0x1f (0 - 31 自然数)
@@ -51,7 +51,7 @@ let dv = buf.dataview;
51
51
  let p = 0;
52
52
  // --- 编码
53
53
  /** 编码缓冲区,持续往里写入,满了就用一个两倍大小的替换,将旧的未写完的部分复制过来 */
54
- let buffer = new Uint8Array(8 * 2 ** 20);
54
+ let buffer = new Uint8Array(8 * 2 ** 20 /* Buffer.poolSize */);
55
55
  /** 编码缓冲区对应的 dataview */
56
56
  let dataview = buffer.dataview;
57
57
  /** 指针,编码缓冲区可以写入的位置,只增不减,除非换新 buffer */
@@ -132,7 +132,7 @@ function _parse() {
132
132
  case 0xcc: {
133
133
  const values = _parse();
134
134
  let message = {};
135
- for (let i = 0; i < values.length; i++)
135
+ for (let i = 0; i < values.length; ++i)
136
136
  message[message_keys[i]] = values[i];
137
137
  return message;
138
138
  }
@@ -191,19 +191,19 @@ function parse_string(len) {
191
191
  }
192
192
  function parse_array(len) {
193
193
  let a = new Array(len);
194
- for (let i = 0; i < len; i++)
194
+ for (let i = 0; i < len; ++i)
195
195
  a[i] = _parse();
196
196
  return a;
197
197
  }
198
198
  function parse_set(size) {
199
199
  let s = new Set();
200
- for (let i = 0; i < size; i++)
200
+ for (let i = 0; i < size; ++i)
201
201
  s.add(_parse());
202
202
  return s;
203
203
  }
204
204
  function parse_object(nentries) {
205
205
  let o = {};
206
- for (let i = 0; i < nentries; i++) {
206
+ for (let i = 0; i < nentries; ++i) {
207
207
  const key = _parse();
208
208
  o[key] = _parse();
209
209
  }
@@ -211,7 +211,7 @@ function parse_object(nentries) {
211
211
  }
212
212
  function parse_map(nentries) {
213
213
  let map = new Map();
214
- for (let i = 0; i < nentries; i++)
214
+ for (let i = 0; i < nentries; ++i)
215
215
  map.set(_parse(), _parse());
216
216
  return map;
217
217
  }
@@ -357,7 +357,7 @@ function _pack(value) {
357
357
  dataview.setUint32(q, length, true);
358
358
  q += 4;
359
359
  }
360
- for (let i = 0; i < length; i++)
360
+ for (let i = 0; i < length; ++i)
361
361
  _pack(value[i]);
362
362
  }
363
363
  else if (value instanceof Uint8Array) {
@@ -509,7 +509,7 @@ function pack_string(value, qstr) {
509
509
  const { length } = value;
510
510
  if (length <= 0x3f) {
511
511
  let j = qstr;
512
- for (let i = 0; i < length; i++) {
512
+ for (let i = 0; i < length; ++i) {
513
513
  let c1 = value.charCodeAt(i), c2 = 0;
514
514
  if (c1 < 0x80)
515
515
  buffer[j++] = c1;
@@ -519,7 +519,7 @@ function pack_string(value, qstr) {
519
519
  }
520
520
  else if ((c1 & 0xfc00) === 0xd800 && ((c2 = value.charCodeAt(i + 1)) & 0xfc00) === 0xdc00) {
521
521
  c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
522
- i++;
522
+ ++i;
523
523
  buffer[j++] = (c1 >> 18) | 0xf0;
524
524
  buffer[j++] = ((c1 >> 12) & 0x3f) | 0x80;
525
525
  buffer[j++] = ((c1 >> 6) & 0x3f) | 0x80;
@@ -537,4 +537,4 @@ function pack_string(value, qstr) {
537
537
  return encode_into(value, buffer.subarray(qstr))
538
538
  .written;
539
539
  }
540
- //# sourceMappingURL=io.browser.js.map
540
+ //# sourceMappingURL=io.common.js.map
package/net.browser.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type Message } from './io.browser.ts';
1
+ import { type Message } from './io.common.ts';
2
2
  import { Lock } from './utils.browser.ts';
3
3
  import { type BasicAuth, type BearerAuth, type RemoteKeeperOptions } from './net.common.ts';
4
4
  export * from './net.common.ts';
package/net.browser.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { t } from "./i18n/instance.js";
2
2
  import { rethrow } from "./prototype.browser.js"; // to_time_str()
3
- import { message_symbol, pack, parse } from "./io.browser.js";
3
+ import { message_symbol, pack, parse } from "./io.common.js";
4
4
  import { assert, genid, delay, Lock, timeout, check } from "./utils.browser.js";
5
5
  import { drop_request_headers } from "./net.common.js";
6
6
  export * from "./net.common.js";
@@ -131,14 +131,14 @@ export async function request(url, options = {}) {
131
131
  }
132
132
  /** 发起 http 请求并将响应体作为 json 解析 */
133
133
  export async function request_json(url, options) {
134
- const resp = await request(url, options);
135
- if (!resp)
134
+ const body = await request(url, options);
135
+ if (!body)
136
136
  return;
137
137
  try {
138
- return JSON.parse(resp);
138
+ return JSON.parse(body);
139
139
  }
140
140
  catch (error) {
141
- console.error(resp);
141
+ console.error(body);
142
142
  throw error;
143
143
  }
144
144
  }
@@ -355,62 +355,62 @@ export class Remote {
355
355
  作为 websocket 连接发起方,不需要传入 websocket
356
356
  作为 websocket 连接接收方,需要传入使用的 websocket 连接,确保这个这个连接的状态 */
357
357
  async connect(websocket) {
358
- if (this.initiator)
359
- if (this.lwebsocket.resource?.readyState === WebSocket.OPEN)
358
+ if (!this.initiator)
359
+ if (websocket.readyState !== WebSocket.OPEN)
360
+ throw new Error('传入的 websocket 连接已断开');
361
+ else
360
362
  return;
361
- else if (!this.url)
362
- throw new Error('创建 Remote 时传入的 websocket 连接已断开');
363
- else {
364
- let reconnected = false;
365
- // 假设有多个请求想要并发连接 websocket, 且此时 websocket 是断开的状态
366
- // 应该排队依次连接,而不是后续的连接直接使用第一次连接的 promise,后续调用还是应该尝试重连(不止连接一次)
367
- await this.lwebsocket.request(async (websocket) => {
368
- // 保存的 rpc 状态在 this.handlers, websocket 无关,因此即使断开重连也不影响 rpc 的运行,即
369
- // 底层连接断开后自动重连对上层应该是无感知的,除非再次连接时失败
370
- if (websocket?.readyState === WebSocket.OPEN)
371
- return;
372
- // 重连
373
- try {
374
- this.lwebsocket.resource = await connect_websocket(this.url, {
375
- on_message: this._on_message,
376
- on_error: this._on_error,
377
- print: this.print
378
- });
379
- reconnected = true;
380
- }
381
- catch (error) {
382
- this._on_error(error);
383
- throw error;
384
- }
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
385
381
  });
386
- if (this.keeper) {
387
- const { heartbeat_interval, func, args } = this.keeper;
388
- // 首次连接成功时,开始心跳保活
389
- if (!this.keeping) {
390
- this.keeping = true;
391
- (async () => {
392
- for (;;) {
393
- await delay(heartbeat_interval);
394
- if (this.disconnected)
395
- break;
396
- if (!this.reconnecting)
397
- try {
398
- await timeout(1000 * 2, this.call('echo'), undefined, this.print);
399
- }
400
- catch (error) {
401
- if (this.print)
402
- console.log(error.message);
403
- this._on_error(error);
404
- }
405
- }
406
- })();
407
- }
408
- if (reconnected && func)
409
- await this.call(func, args);
410
- }
382
+ reconnected = true;
383
+ }
384
+ catch (error) {
385
+ this._on_error(error);
386
+ throw error;
411
387
  }
412
- else if (websocket.readyState !== WebSocket.OPEN)
413
- throw new Error('传入的 websocket 连接已断开');
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
414
  }
415
415
  /** 作为 websocket 连接发起方手动关闭到对端的 websocket 连接 */
416
416
  disconnect() {
package/net.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { type Readable } from 'stream';
2
2
  import type { WebSocket, CloseEvent, ErrorEvent } from 'ws';
3
3
  import type { Cookie, CookieJar, MemoryCookieStore } from 'tough-cookie';
4
- import { type Message } from './io.ts';
4
+ import { type Message } from './io.common.ts';
5
5
  import type { Encoding } from './file.ts';
6
6
  import { inspect, Lock } from './utils.ts';
7
7
  import { type BasicAuth, type BearerAuth, type RemoteKeeperOptions } from './net.common.ts';
package/net.js CHANGED
@@ -3,7 +3,7 @@ import { buffer as stream_to_buffer, text as stream_to_text } from 'stream/consu
3
3
  import { isReadable } from 'stream';
4
4
  import { t } from "./i18n/instance.js";
5
5
  import { rethrow } from "./prototype.js";
6
- import { message_symbol, pack, parse } from "./io.js";
6
+ import { message_symbol, pack, parse } from "./io.common.js";
7
7
  import { inspect, assert, genid, delay, Lock, pipe_with_error, map_values, unique, timeout, check, colored } from "./utils.js";
8
8
  import { drop_request_headers } from "./net.common.js";
9
9
  export * from "./net.common.js";
@@ -100,7 +100,7 @@ export async function request(url, options = {}) {
100
100
  let headers = {
101
101
  'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,ja-JP;q=0.6,ja;q=0.5',
102
102
  'accept-encoding': 'gzip, deflate, br',
103
- 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
103
+ 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36',
104
104
  'sec-ch-ua-platform': '"Windows"',
105
105
  'sec-ch-ua-platform-version': '"19.0.0"',
106
106
  };
@@ -544,62 +544,62 @@ export class Remote {
544
544
  作为 websocket 连接发起方,不需要传入 websocket
545
545
  作为 websocket 连接接收方,需要传入使用的 websocket 连接,确保这个这个连接的状态 */
546
546
  async connect(websocket) {
547
- if (this.initiator)
548
- if (this.lwebsocket.resource?.readyState === WebSocketOpen)
547
+ if (!this.initiator)
548
+ if (websocket.readyState !== WebSocketOpen)
549
+ throw new Error('传入的 websocket 连接已断开');
550
+ else
549
551
  return;
550
- else if (!this.url)
551
- throw new Error('创建 Remote 时传入的 websocket 连接已断开');
552
- else {
553
- let reconnected = false;
554
- // 假设有多个请求想要并发连接 websocket, 且此时 websocket 是断开的状态
555
- // 应该排队依次连接,而不是后续的连接直接使用第一次连接的 promise,后续调用还是应该尝试重连(不止连接一次)
556
- await this.lwebsocket.request(async (websocket) => {
557
- // 保存的 rpc 状态在 this.handlers, websocket 无关,因此即使断开重连也不影响 rpc 的运行,即
558
- // 底层连接断开后自动重连对上层应该是无感知的,除非再次连接时失败
559
- if (websocket?.readyState === WebSocketOpen)
560
- return;
561
- // 重连
562
- try {
563
- this.lwebsocket.resource = await connect_websocket(this.url, {
564
- on_message: this._on_message,
565
- on_error: this._on_error,
566
- print: this.print
567
- });
568
- reconnected = true;
569
- }
570
- catch (error) {
571
- this._on_error(error);
572
- throw error;
573
- }
552
+ if (this.lwebsocket.resource?.readyState === WebSocketOpen)
553
+ return;
554
+ if (!this.url)
555
+ throw new Error('创建 Remote 时传入的 websocket 连接已断开');
556
+ let reconnected = false;
557
+ // 假设有多个请求想要并发连接 websocket, 且此时 websocket 是断开的状态
558
+ // 应该排队依次连接,而不是后续的连接直接使用第一次连接的 promise,后续调用还是应该尝试重连(不止连接一次)
559
+ await this.lwebsocket.request(async (websocket) => {
560
+ // 保存的 rpc 状态在 this.handlers, 与 websocket 无关,因此即使断开重连也不影响 rpc 的运行,即
561
+ // 底层连接断开后自动重连对上层应该是无感知的,除非再次连接时失败
562
+ if (websocket?.readyState === WebSocketOpen)
563
+ return;
564
+ // 重连
565
+ try {
566
+ this.lwebsocket.resource = await connect_websocket(this.url, {
567
+ on_message: this._on_message,
568
+ on_error: this._on_error,
569
+ print: this.print
574
570
  });
575
- if (this.keeper) {
576
- const { heartbeat_interval, func, args } = this.keeper;
577
- // 首次连接成功时,开始心跳保活
578
- if (!this.keeping) {
579
- this.keeping = true;
580
- (async () => {
581
- for (;;) {
582
- await delay(heartbeat_interval);
583
- if (this.disconnected)
584
- break;
585
- if (!this.reconnecting)
586
- try {
587
- await timeout(1000 * 2, this.call('echo'), undefined, this.print);
588
- }
589
- catch (error) {
590
- if (this.print)
591
- console.log(error.message);
592
- this._on_error(error);
593
- }
594
- }
595
- })();
596
- }
597
- if (reconnected && func)
598
- await this.call(func, args);
599
- }
571
+ reconnected = true;
572
+ }
573
+ catch (error) {
574
+ this._on_error(error);
575
+ throw error;
600
576
  }
601
- else if (websocket.readyState !== WebSocketOpen)
602
- throw new Error('传入的 websocket 连接已断开');
577
+ });
578
+ if (!this.keeper)
579
+ return;
580
+ const { heartbeat_interval, func, args } = this.keeper;
581
+ // 首次连接成功时,开始心跳保活
582
+ if (!this.keeping) {
583
+ this.keeping = true;
584
+ (async () => {
585
+ for (;;) {
586
+ await delay(heartbeat_interval);
587
+ if (this.disconnected)
588
+ break;
589
+ if (!this.reconnecting)
590
+ try {
591
+ await timeout(1000 * 2, this.call('echo'), undefined, this.print);
592
+ }
593
+ catch (error) {
594
+ if (this.print)
595
+ console.log(error.message);
596
+ this._on_error(error);
597
+ }
598
+ }
599
+ })();
600
+ }
601
+ if (reconnected && func)
602
+ await this.call(func, args);
603
603
  }
604
604
  /** 作为 websocket 连接发起方手动关闭到对端的 websocket 连接 */
605
605
  disconnect() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xshell",
3
- "version": "1.2.71",
3
+ "version": "1.2.73",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "bin": {
@@ -53,23 +53,23 @@
53
53
  "@babel/parser": "^7.28.0",
54
54
  "@babel/traverse": "^7.28.0",
55
55
  "@koa/cors": "^5.0.0",
56
- "@stylistic/eslint-plugin": "^5.2.2",
56
+ "@stylistic/eslint-plugin": "^5.2.3",
57
57
  "@svgr/webpack": "^8.1.0",
58
58
  "@types/sass-loader": "^8.0.9",
59
59
  "@types/ws": "^8.18.1",
60
- "@typescript-eslint/eslint-plugin": "^8.38.0",
61
- "@typescript-eslint/parser": "^8.38.0",
62
- "@typescript-eslint/utils": "^8.38.0",
60
+ "@typescript-eslint/eslint-plugin": "^8.39.0",
61
+ "@typescript-eslint/parser": "^8.39.0",
62
+ "@typescript-eslint/utils": "^8.39.0",
63
63
  "archiver": "^7.0.1",
64
- "chalk": "^5.4.1",
64
+ "chalk": "^5.5.0",
65
65
  "commander": "^14.0.0",
66
66
  "css-loader": "^7.1.2",
67
67
  "emoji-regex": "^10.4.0",
68
- "eslint": "^9.32.0",
68
+ "eslint": "^9.33.0",
69
69
  "eslint-plugin-import": "^2.32.0",
70
70
  "eslint-plugin-react": "^7.37.5",
71
71
  "https-proxy-agent": "^7.0.6",
72
- "i18next": "^25.3.2",
72
+ "i18next": "^25.3.4",
73
73
  "i18next-scanner": "^4.6.0",
74
74
  "koa": "^3.0.1",
75
75
  "koa-compress": "^5.1.1",
@@ -80,7 +80,7 @@
80
80
  "react": "^19.1.1",
81
81
  "react-i18next": "^15.6.1",
82
82
  "resolve-path": "^1.4.0",
83
- "sass": "^1.89.2",
83
+ "sass": "^1.90.0",
84
84
  "sass-loader": "^16.0.5",
85
85
  "source-map-loader": "^5.0.0",
86
86
  "strip-ansi": "^7.1.0",
@@ -104,7 +104,7 @@
104
104
  "@types/koa": "^3.0.0",
105
105
  "@types/koa-compress": "^4.0.6",
106
106
  "@types/mime-types": "^3.0.1",
107
- "@types/node": "^24.0.12",
107
+ "@types/node": "^24.2.1",
108
108
  "@types/react": "^19.1.9",
109
109
  "@types/tough-cookie": "^4.0.5",
110
110
  "@types/ua-parser-js": "^0.7.39",
@@ -0,0 +1 @@
1
+ export * from './platform.common.ts';
@@ -0,0 +1,31 @@
1
+ import { ident } from "./prototype.browser.js";
2
+ import { set_platform } from "./platform.common.js";
3
+ import { encoder } from "./utils.browser.js";
4
+ export * from "./platform.common.js";
5
+ function get_buffer(length) {
6
+ return new Uint8Array(length);
7
+ }
8
+ function encode(str) {
9
+ return encoder.encode(str);
10
+ }
11
+ async function delay(milliseconds, { signal } = {}) {
12
+ signal?.throwIfAborted();
13
+ return new Promise((resolve, reject) => {
14
+ function on_signal_abort() {
15
+ clearTimeout(timeout);
16
+ reject(signal.reason);
17
+ }
18
+ signal?.addEventListener('abort', on_signal_abort);
19
+ let timeout = setTimeout(() => {
20
+ signal?.removeEventListener('abort', on_signal_abort);
21
+ resolve();
22
+ }, milliseconds);
23
+ });
24
+ }
25
+ set_platform({
26
+ get_buffer,
27
+ encode,
28
+ delay,
29
+ strip_ansi: ident
30
+ });
31
+ //# sourceMappingURL=platform.browser.js.map
@@ -0,0 +1,10 @@
1
+ import type { TimerOptions } from 'timers';
2
+ export declare let platform: Platform;
3
+ export declare function set_platform(_platform: Platform): void;
4
+ interface Platform {
5
+ get_buffer(length: number): Uint8Array;
6
+ encode(str: string): Uint8Array;
7
+ delay(milliseconds: number, options?: TimerOptions): Promise<void>;
8
+ strip_ansi(str: string): string;
9
+ }
10
+ export {};
@@ -0,0 +1,5 @@
1
+ export let platform;
2
+ export function set_platform(_platform) {
3
+ platform = _platform;
4
+ }
5
+ //# sourceMappingURL=platform.common.js.map
package/platform.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import './prototype.ts';
2
+ export * from './platform.common.ts';
package/platform.js ADDED
@@ -0,0 +1,22 @@
1
+ import timers from 'timers/promises';
2
+ import strip_ansi from 'strip-ansi';
3
+ import "./prototype.js";
4
+ import { set_platform } from "./platform.common.js";
5
+ export * from "./platform.common.js";
6
+ function get_buffer(length) {
7
+ return Buffer.allocUnsafe(length);
8
+ }
9
+ function encode(str) {
10
+ // 用 Buffer.from 是因为可以利用 buffer pool,避免 encoder.encode 创建大量小且独立的 array buffer
11
+ return Buffer.from(str);
12
+ }
13
+ async function delay(milliseconds, options) {
14
+ return timers.setTimeout(milliseconds, undefined, options);
15
+ }
16
+ set_platform({
17
+ get_buffer,
18
+ encode,
19
+ delay,
20
+ strip_ansi
21
+ });
22
+ //# sourceMappingURL=platform.js.map
@@ -1 +1,2 @@
1
+ import './platform.browser.ts';
1
2
  export * from './prototype.common.ts';