xshell 1.0.196 → 1.0.199
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/Terminal.d.ts +1 -0
- package/Terminal.js +5 -2
- package/apps.js +6 -6
- package/builder.js +9 -9
- package/file.js +27 -27
- package/net.browser.d.ts +9 -4
- package/net.browser.js +35 -28
- package/net.d.ts +9 -4
- package/net.js +48 -37
- package/package.json +12 -12
- package/process.d.ts +4 -1
- package/process.js +2 -1
- package/server.js +10 -10
- package/utils.browser.d.ts +3 -2
- package/utils.browser.js +7 -4
- package/utils.d.ts +3 -2
- package/utils.js +7 -4
- package/xlint.js +2 -0
package/Terminal.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export declare function Terminal({ font }: {
|
|
|
7
7
|
font: string;
|
|
8
8
|
}): import("react/jsx-runtime").JSX.Element;
|
|
9
9
|
declare class TerminalModel extends Model<TerminalModel> {
|
|
10
|
+
pterm: import("./utils.browser.ts").Deferred<XTerminal>;
|
|
10
11
|
term: XTerminal;
|
|
11
12
|
fit_addon: FitAddon;
|
|
12
13
|
stdio_id: number;
|
package/Terminal.js
CHANGED
|
@@ -8,7 +8,7 @@ import { WebLinksAddon } from '@xterm/addon-web-links';
|
|
|
8
8
|
// 没有 ui
|
|
9
9
|
// import { SearchAddon } from '@xterm/addon-search'
|
|
10
10
|
import { Model } from 'react-object-model';
|
|
11
|
-
import { assert, genid } from './utils.browser.js';
|
|
11
|
+
import { assert, defer, genid } from './utils.browser.js';
|
|
12
12
|
export function Terminal({ font }) {
|
|
13
13
|
let rterminal = useRef();
|
|
14
14
|
useEffect(() => {
|
|
@@ -34,14 +34,17 @@ export function Terminal({ font }) {
|
|
|
34
34
|
term.loadAddon(link_addon);
|
|
35
35
|
// term.loadAddon(search_addon)
|
|
36
36
|
term.open(rterminal.current);
|
|
37
|
-
|
|
37
|
+
if (document.createElement('canvas').getContext('webgl2'))
|
|
38
|
+
term.loadAddon(new WebglAddon());
|
|
38
39
|
fit_addon.fit();
|
|
40
|
+
terminal.pterm.resolve(term);
|
|
39
41
|
terminal.set({ term, fit_addon });
|
|
40
42
|
})();
|
|
41
43
|
}, []);
|
|
42
44
|
return _jsx("div", { className: 'term', ref: rterminal });
|
|
43
45
|
}
|
|
44
46
|
class TerminalModel extends Model {
|
|
47
|
+
pterm = defer();
|
|
45
48
|
term;
|
|
46
49
|
fit_addon;
|
|
47
50
|
stdio_id = 0;
|
package/apps.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import util from 'util';
|
|
2
2
|
import { call_nodejs, platform, username } from './process.js';
|
|
3
|
-
import {
|
|
3
|
+
import { check } from './utils.js';
|
|
4
4
|
import { path } from './path.js';
|
|
5
5
|
export let npm = {
|
|
6
6
|
bin: platform == 'win32' ? `C:/Users/${username}/AppData/Roaming/npm/node_modules/pnpm/bin/pnpm.cjs` : '/usr/bin/pnpm',
|
|
@@ -68,11 +68,11 @@ export let oss = {
|
|
|
68
68
|
- cdn?: `false` false 时返回 oss 链接,true 时返回 cdn 链接
|
|
69
69
|
- copy?: `false` 是否复制到剪贴板 */
|
|
70
70
|
async upload(fp_remote, data, { private: _private = false, print = true, cdn = false, } = {}) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
check(this.client, 'OSS 应该已经初始化了');
|
|
72
|
+
check(!fp_remote.startsWith('/'), 'fp 不能以 / 开头');
|
|
73
|
+
check(!fp_remote.isdir, '不能使用 oss.upload 上传文件夹,请使用 oss.upload_dir');
|
|
74
74
|
if (typeof data === 'string')
|
|
75
|
-
|
|
75
|
+
check(path.isAbsolute(data), 'oss.upload 传入 data 参数类型为 string 时,必须为本地文件完整路径');
|
|
76
76
|
if (data instanceof Uint8Array && !Buffer.isBuffer(data))
|
|
77
77
|
data = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
78
78
|
await this.client.put(fp_remote, data);
|
|
@@ -89,7 +89,7 @@ export let oss = {
|
|
|
89
89
|
},
|
|
90
90
|
/** 获取经过签名后可访问的 url */
|
|
91
91
|
async get_url(fp) {
|
|
92
|
-
|
|
92
|
+
check(this.client, 'OSS 应该已经初始化了');
|
|
93
93
|
const url = new URL(this.client.signatureUrl(fp, { expires: /* 十年 */ 3600 * 24 * 365 * 10 }));
|
|
94
94
|
const url_ = `${this.fpd_cdn}${url.pathname.slice(1)}${url.search}`;
|
|
95
95
|
return url_;
|
package/builder.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { fileURLToPath } from 'url';
|
|
2
2
|
import { noprint } from './process.js';
|
|
3
|
-
import { Lock,
|
|
3
|
+
import { Lock, check, filter_values, not_empty } from './utils.js';
|
|
4
4
|
import { fcopy, fmkdir, fwrite } from './file.js';
|
|
5
5
|
import { path } from './path.js';
|
|
6
6
|
const monaco_files = [
|
|
@@ -172,7 +172,7 @@ export class Bundler {
|
|
|
172
172
|
this.globals = globals;
|
|
173
173
|
this.exclude_modules = exclude_modules;
|
|
174
174
|
if (dependencies) {
|
|
175
|
-
|
|
175
|
+
check(target === 'web');
|
|
176
176
|
this.dependencies = dependencies;
|
|
177
177
|
}
|
|
178
178
|
if (expose && commonjs2)
|
|
@@ -307,6 +307,7 @@ export class Bundler {
|
|
|
307
307
|
// https://webpack.js.org/loaders/sass-loader
|
|
308
308
|
loader: get_loader('sass-loader'),
|
|
309
309
|
options: {
|
|
310
|
+
api: 'modern-compiler',
|
|
310
311
|
...sass ? {
|
|
311
312
|
implementation: sass
|
|
312
313
|
} : {},
|
|
@@ -393,7 +394,6 @@ export class Bundler {
|
|
|
393
394
|
};
|
|
394
395
|
}
|
|
395
396
|
async build(print = true) {
|
|
396
|
-
let timer = new Timer();
|
|
397
397
|
if (!this.lcompiler) {
|
|
398
398
|
const { default: Webpack } = await import('webpack');
|
|
399
399
|
this.config.plugins = [
|
|
@@ -447,8 +447,8 @@ export class Bundler {
|
|
|
447
447
|
if (print) {
|
|
448
448
|
const statstr = stats.toString(this.config.stats)
|
|
449
449
|
.replace(new RegExp(`\\n\\s*.*${this.name}.* compiled .*successfully.* in (.*)`), '').trimEnd();
|
|
450
|
-
console.log(
|
|
451
|
-
`${this.name}
|
|
450
|
+
console.log((statstr ? `${statstr}\n` : '') +
|
|
451
|
+
`${this.name} 成功构建到 ${this.fpd_out}`.green);
|
|
452
452
|
}
|
|
453
453
|
}
|
|
454
454
|
async close() {
|
|
@@ -512,8 +512,8 @@ export class Bundler {
|
|
|
512
512
|
if (print.files)
|
|
513
513
|
console.log(`已构建 ${fp_html}`);
|
|
514
514
|
}));
|
|
515
|
-
if (print.
|
|
516
|
-
console.log(`${this.name}
|
|
515
|
+
if (print.files)
|
|
516
|
+
console.log(`${this.name} 的所有 html 页面构建完成`);
|
|
517
517
|
}
|
|
518
518
|
resolve_dependencies(dependency_ids = this.dependencies, resolveds = new Set()) {
|
|
519
519
|
dependency_ids.forEach(id => {
|
|
@@ -541,8 +541,8 @@ export class Bundler {
|
|
|
541
541
|
}).flat();
|
|
542
542
|
}
|
|
543
543
|
async copy_files({ dependencies = this.dependencies, production = this.production, assets = this.assets, fpd_root = this.fpd_root, fpd_out = this.fpd_out, print = { info: true, files: false } } = {}) {
|
|
544
|
-
if (print.
|
|
545
|
-
console.log(`复制 ${this.name}
|
|
544
|
+
if (print.files)
|
|
545
|
+
console.log(`复制 ${this.name} 的依赖文件到 ${this.fpd_out}`);
|
|
546
546
|
if (dependencies.length)
|
|
547
547
|
await fmkdir(`${fpd_out}vendors/`, { print: false });
|
|
548
548
|
// 输出路径 -> 原路径,用来保证只复制一次且是同样的映射
|
package/file.js
CHANGED
|
@@ -3,7 +3,7 @@ import { isUint8Array } from 'util/types';
|
|
|
3
3
|
import { path } from './path.js';
|
|
4
4
|
import { t } from './i18n/instance.js';
|
|
5
5
|
import { to_json } from './prototype.js';
|
|
6
|
-
import {
|
|
6
|
+
import { check, Lock, WritableMemoryStream } from './utils.js';
|
|
7
7
|
export { fsp };
|
|
8
8
|
export const encodings = ['utf-8', 'gb18030', 'shift-jis', 'utf-16le'];
|
|
9
9
|
export const ramdisk = fexists('T:/TEMP/', { print: false });
|
|
@@ -31,12 +31,12 @@ export async function fopen(fp, flags = 'r', { mode, print } = {}) {
|
|
|
31
31
|
}
|
|
32
32
|
export async function fread(fp, { dir, encoding = 'utf-8', print = true } = {}) {
|
|
33
33
|
if (dir) {
|
|
34
|
-
|
|
34
|
+
check(dir.isdir, t('dir 必须以 / 结尾'));
|
|
35
35
|
fp = dir + fp;
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
check(!fp.isdir, t('fp 必须是文件,不能以 / 结尾'));
|
|
38
|
+
check(path.isAbsolute(fp), `${t('fp 必须是绝对路径:')} ${fp}`);
|
|
39
|
+
check(encoding !== 'auto');
|
|
40
40
|
if (print)
|
|
41
41
|
console.log(t('读取'), fp);
|
|
42
42
|
switch (encoding) {
|
|
@@ -82,7 +82,7 @@ export async function fwrite(fp, data, { print = true } = {}) {
|
|
|
82
82
|
console.log(t('写入'), fp.fp);
|
|
83
83
|
}
|
|
84
84
|
else {
|
|
85
|
-
|
|
85
|
+
check(path.isAbsolute(fp), `${t('fp 必须是绝对路径,当前为:')} ${fp}`);
|
|
86
86
|
if (print)
|
|
87
87
|
console.log(t('写入'), fp);
|
|
88
88
|
}
|
|
@@ -103,10 +103,10 @@ export async function fwrite(fp, data, { print = true } = {}) {
|
|
|
103
103
|
}
|
|
104
104
|
/** 追加内容到文件末尾,如果文件不存在会自动创建 */
|
|
105
105
|
export async function fappend(fp, data, { print = true } = {}) {
|
|
106
|
-
|
|
106
|
+
check(path.isAbsolute(fp), `${t('fp 必须是绝对路径,当前为:')} ${fp}`);
|
|
107
107
|
if (print)
|
|
108
108
|
console.log(t('追加'), fp);
|
|
109
|
-
|
|
109
|
+
check(isUint8Array(data) || typeof data === 'string');
|
|
110
110
|
try {
|
|
111
111
|
await fsp.appendFile(fp, data);
|
|
112
112
|
}
|
|
@@ -121,8 +121,8 @@ export async function fappend(fp, data, { print = true } = {}) {
|
|
|
121
121
|
}
|
|
122
122
|
export async function flist(fpd, options = {}) {
|
|
123
123
|
const { filter, deep = false, absolute = false, print = true, stats = false } = options;
|
|
124
|
-
|
|
125
|
-
|
|
124
|
+
check(path.isAbsolute(fpd), t("flist: 参数 fpd: '{{fpd}}' 必须是绝对路径", { fpd }));
|
|
125
|
+
check(fpd.isdir, t("flist: 参数 fpd: '{{fpd}}' 必须以 / 结尾", { fpd }));
|
|
126
126
|
if (!path.isAbsolute(fpd))
|
|
127
127
|
throw new Error(t('参数 fpd: ') + fpd + t(' 必须是绝对路径'));
|
|
128
128
|
if (!fpd.isdir)
|
|
@@ -179,13 +179,13 @@ export async function flist(fpd, options = {}) {
|
|
|
179
179
|
return fps;
|
|
180
180
|
}
|
|
181
181
|
export async function fstat(fp) {
|
|
182
|
-
|
|
182
|
+
check(path.isAbsolute(fp), t("fstat: 参数 fp: '{{fp}}' 必须是绝对路径", { fp }));
|
|
183
183
|
let stat = await fsp.stat(fp, { bigint: true });
|
|
184
184
|
stat.fp = fp;
|
|
185
185
|
return stat;
|
|
186
186
|
}
|
|
187
187
|
export async function flstat(fp) {
|
|
188
|
-
|
|
188
|
+
check(path.isAbsolute(fp), t("flstat: 参数 fp: '{{fp}}' 必须是绝对路径", { fp }));
|
|
189
189
|
let stat = await fsp.lstat(fp, { bigint: true });
|
|
190
190
|
stat.fp = fp;
|
|
191
191
|
return stat;
|
|
@@ -205,8 +205,8 @@ export async function ffstat(handle) {
|
|
|
205
205
|
- options?:
|
|
206
206
|
- print?: `true` */
|
|
207
207
|
export async function fdelete(fp, { print = true } = {}) {
|
|
208
|
-
|
|
209
|
-
|
|
208
|
+
check(fp.length >= 6, `fp: ${fp} ${t('不能太短,防止误删文件')}`);
|
|
209
|
+
check(path.isAbsolute(fp), t('fp 必须是绝对路径'));
|
|
210
210
|
const { isdir } = fp;
|
|
211
211
|
try {
|
|
212
212
|
await fsp.rm(fp, { recursive: true });
|
|
@@ -248,8 +248,8 @@ export async function fdclear(fpd, { print = true } = {}) {
|
|
|
248
248
|
fcopy('D:/temp/camera/', 'D:/camera/') */
|
|
249
249
|
export async function fcopy(fp_src, fp_dst, { print = true, overwrite = true, filter, } = {}) {
|
|
250
250
|
const { isdir } = fp_src;
|
|
251
|
-
|
|
252
|
-
|
|
251
|
+
check(isdir === fp_dst.isdir, t('fp_src 和 fp_dst 必须同为文件路径或文件夹路径'));
|
|
252
|
+
check(path.isAbsolute(fp_src) && path.isAbsolute(fp_dst), t('fp_src 和 fp_dst 必须为完整路径'));
|
|
253
253
|
if (!isdir && filter)
|
|
254
254
|
throw new Error(t('filter 选项只适用于 fp_src 为文件夹'));
|
|
255
255
|
if (print)
|
|
@@ -297,8 +297,8 @@ export async function fcopy(fp_src, fp_dst, { print = true, overwrite = true, fi
|
|
|
297
297
|
@example
|
|
298
298
|
fmove('D:/temp/camera/', 'D:/camera/') */
|
|
299
299
|
export async function fmove(fp_src, fp_dst, { overwrite = false, print = true } = {}) {
|
|
300
|
-
|
|
301
|
-
|
|
300
|
+
check(fp_src.isdir === fp_dst.isdir, t('fp_src 和 fp_dst 必须同为文件路径或文件夹路径'));
|
|
301
|
+
check(path.isAbsolute(fp_src) && path.isAbsolute(fp_dst), t('fp_src 和 fp_dst 必须为完整路径'));
|
|
302
302
|
if (print)
|
|
303
303
|
console.log(t('移动'), fp_src, '->', fp_dst);
|
|
304
304
|
if (!overwrite && fexists(fp_dst, { print: false }))
|
|
@@ -330,11 +330,11 @@ export async function fmove(fp_src, fp_dst, { overwrite = false, print = true }
|
|
|
330
330
|
- overwrite?: `true` 默认覆盖(不检查效率更高) better performance without check */
|
|
331
331
|
export async function frename(fp, fp_, { fpd, print = true, overwrite = true } = {}) {
|
|
332
332
|
if (fpd) {
|
|
333
|
-
|
|
333
|
+
check(fpd.isdir);
|
|
334
334
|
fp = fpd + fp;
|
|
335
335
|
fp_ = fpd + fp_;
|
|
336
336
|
}
|
|
337
|
-
|
|
337
|
+
check(path.isAbsolute(fp) && path.isAbsolute(fp_), t('fp 和 fp_ 必须是绝对路径'));
|
|
338
338
|
if (print)
|
|
339
339
|
console.log(t('重命名'), fp, '->', fp_);
|
|
340
340
|
if (!overwrite && fexists(fp_, { print: false }))
|
|
@@ -350,8 +350,8 @@ export async function frename(fp, fp_, { fpd, print = true, overwrite = true } =
|
|
|
350
350
|
- print?: `true`
|
|
351
351
|
- mode?: `'0o777'` */
|
|
352
352
|
export async function fmkdir(fpd, { print = true, mode, } = {}) {
|
|
353
|
-
|
|
354
|
-
|
|
353
|
+
check(path.isAbsolute(fpd), t('fpd 必须是绝对路径: ') + fpd);
|
|
354
|
+
check(fpd.isdir, t('fpd 必须以 / 结尾: ') + fpd);
|
|
355
355
|
// 类似 D:/ 这样的根路径,调用 fsp.mkdir 会报错无权限
|
|
356
356
|
if (fpd.length === 3 &&
|
|
357
357
|
'A' <= fpd[0] && fpd[0] <= 'Z' && fpd[1] === ':' && fpd[2] === '/')
|
|
@@ -376,10 +376,10 @@ export async function fmkdir(fpd, { print = true, mode, } = {}) {
|
|
|
376
376
|
- fp_real: 现在真实文件/文件夹的路径
|
|
377
377
|
- fp_link: 目标链接文件/文件夹的路径 */
|
|
378
378
|
export async function flink(fp_real, fp_link, { junction = false, print = true, skip_existing = false } = {}) {
|
|
379
|
-
|
|
379
|
+
check(path.isAbsolute(fp_real) && path.isAbsolute(fp_link), 'fp 必须是绝对路径');
|
|
380
380
|
const is_fpd_real = fp_real.isdir;
|
|
381
381
|
const is_fpd_link = fp_link.isdir;
|
|
382
|
-
|
|
382
|
+
check(is_fpd_real === is_fpd_link, 'fp_real 和 fp_link 必须同为文件路径或文件夹路径');
|
|
383
383
|
if (fexists(fp_link, { print: false }))
|
|
384
384
|
if (!skip_existing)
|
|
385
385
|
throw new Error(`存在同名${is_fpd_link ? '文件夹' : '文件'}: ${fp_link},无法创建链接`);
|
|
@@ -434,7 +434,7 @@ async function _zip(data, fp_zip, { dirname, print = { files: true, info: true }
|
|
|
434
434
|
.map(fp => ([dirname + fp, fpd_src + fp])));
|
|
435
435
|
}
|
|
436
436
|
else {
|
|
437
|
-
|
|
437
|
+
check(!dirname, 'dirname 在传入 fpd_src 时才生效');
|
|
438
438
|
entries = data;
|
|
439
439
|
}
|
|
440
440
|
if (print.info)
|
|
@@ -463,8 +463,8 @@ async function _zip(data, fp_zip, { dirname, print = { files: true, info: true }
|
|
|
463
463
|
archive.append(Buffer.isBuffer(fdata) ? fdata : Buffer.from(fdata.buffer), { name: fp });
|
|
464
464
|
}
|
|
465
465
|
else {
|
|
466
|
-
|
|
467
|
-
|
|
466
|
+
check(fp.isdir === fdata.isdir);
|
|
467
|
+
check(path.isAbsolute(fdata));
|
|
468
468
|
if (print.files)
|
|
469
469
|
console.log(`压缩 ${fdata} -> ${fp}`);
|
|
470
470
|
if (fp.isdir)
|
package/net.browser.d.ts
CHANGED
|
@@ -90,12 +90,14 @@ export declare class WebSocketConnectionError extends Error {
|
|
|
90
90
|
- on_error?: 在 websocket 出错和非正常关闭 (close, error 事件) 时都调用,可以根据 error.type 来区分,error 的类型是 WebSocketConnectionError,
|
|
91
91
|
type 为 'close' 时有 code 和 reason 属性
|
|
92
92
|
- on_close?: 和 websocket 的 'close' 事件不相同,只在正常关闭 (close code 为 1000) 时才调用,否则都会调用 on_error
|
|
93
|
-
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes
|
|
94
|
-
|
|
93
|
+
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes
|
|
94
|
+
- print?: 是否打印连接、关闭信息 */
|
|
95
|
+
export declare function connect_websocket(url: string | URL, { protocols, on_message, on_error, on_close, print }: {
|
|
95
96
|
protocols?: string[];
|
|
96
97
|
on_message(data: ArrayBuffer | string, websocket: WebSocket): any;
|
|
97
98
|
on_error?(error: WebSocketConnectionError, websocket: WebSocket): any;
|
|
98
99
|
on_close?(event: CloseEvent, websocket: WebSocket): any;
|
|
100
|
+
print?: boolean;
|
|
99
101
|
}): Promise<WebSocket>;
|
|
100
102
|
/** 接收到消息后的处理函数
|
|
101
103
|
返回值可以是:
|
|
@@ -185,8 +187,10 @@ export declare class Remote {
|
|
|
185
187
|
一元 rpc 接收方不需要设置 handlers, 发送方需要 */
|
|
186
188
|
handlers: Map<number, MessageHandler<any[]>>;
|
|
187
189
|
keeper?: RemoteKeeperOptions;
|
|
188
|
-
/** `
|
|
190
|
+
/** `true` 是否打印连接信息、错误信息 */
|
|
189
191
|
print: boolean;
|
|
192
|
+
/** `false` 打印所有交互的 rpc messages */
|
|
193
|
+
verbose: boolean;
|
|
190
194
|
first_error: boolean;
|
|
191
195
|
keeping: boolean;
|
|
192
196
|
reconnecting: boolean;
|
|
@@ -196,10 +200,11 @@ export declare class Remote {
|
|
|
196
200
|
static pack({ id, func, data, done, error }: Message): Uint8Array;
|
|
197
201
|
/** 作为 websocket 连接发起方,传入 url 或 websocket,定义远程 Remote
|
|
198
202
|
作为 websocket 连接接收方,不传 url 和 websocket,定义本地 Remote */
|
|
199
|
-
constructor({ url, funcs, print, websocket, keeper, on_error, }?: {
|
|
203
|
+
constructor({ url, funcs, print, verbose, websocket, keeper, on_error, }?: {
|
|
200
204
|
url?: string;
|
|
201
205
|
funcs?: Remote['funcs'];
|
|
202
206
|
print?: boolean;
|
|
207
|
+
verbose?: boolean;
|
|
203
208
|
websocket?: WebSocket;
|
|
204
209
|
keeper?: RemoteKeeperOptions;
|
|
205
210
|
on_error?(error: WebSocketConnectionError, websocket?: WebSocket): void;
|
package/net.browser.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { t } from './i18n/instance.js';
|
|
2
2
|
import './prototype.browser.js'; // to_time_str()
|
|
3
|
-
import { assert, concat, genid, delay, Lock, encode, decode, timeout } from './utils.browser.js';
|
|
3
|
+
import { assert, concat, genid, delay, Lock, encode, decode, timeout, check } from './utils.browser.js';
|
|
4
4
|
const drop_request_headers = new Set([
|
|
5
5
|
// : 开头的 key
|
|
6
6
|
// sec-*
|
|
@@ -186,26 +186,29 @@ export class WebSocketConnectionError extends Error {
|
|
|
186
186
|
- on_error?: 在 websocket 出错和非正常关闭 (close, error 事件) 时都调用,可以根据 error.type 来区分,error 的类型是 WebSocketConnectionError,
|
|
187
187
|
type 为 'close' 时有 code 和 reason 属性
|
|
188
188
|
- on_close?: 和 websocket 的 'close' 事件不相同,只在正常关闭 (close code 为 1000) 时才调用,否则都会调用 on_error
|
|
189
|
-
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes
|
|
190
|
-
|
|
189
|
+
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes
|
|
190
|
+
- print?: 是否打印连接、关闭信息 */
|
|
191
|
+
export async function connect_websocket(url, { protocols, on_message, on_error, on_close, print = true }) {
|
|
191
192
|
let websocket = new WebSocket(url, protocols);
|
|
192
193
|
// https://stackoverflow.com/questions/11821096/what-is-the-difference-between-an-arraybuffer-and-a-blob/39951543
|
|
193
194
|
websocket.binaryType = 'arraybuffer';
|
|
194
195
|
return new Promise((resolve, reject) => {
|
|
195
196
|
let settled = false;
|
|
196
197
|
websocket.addEventListener('open', event => {
|
|
197
|
-
|
|
198
|
-
(websocket.
|
|
199
|
-
|
|
198
|
+
if (print)
|
|
199
|
+
console.log(websocket.url +
|
|
200
|
+
(websocket.protocol ? ' ' + websocket.protocol.bracket() : '') +
|
|
201
|
+
t(' 已连接'));
|
|
200
202
|
settled = true;
|
|
201
203
|
resolve(websocket);
|
|
202
204
|
});
|
|
203
205
|
websocket.addEventListener('close', event => {
|
|
204
|
-
if (event.code === 1000) // 正常关闭
|
|
206
|
+
if (event.code === 1000) { // 正常关闭
|
|
205
207
|
if (on_close)
|
|
206
208
|
on_close(event, websocket);
|
|
207
|
-
else
|
|
209
|
+
else if (print)
|
|
208
210
|
console.log(`${websocket.url} ${t('已正常关闭')}`);
|
|
211
|
+
}
|
|
209
212
|
else { // 异常关闭,认为发生了错误,进行错误处理
|
|
210
213
|
const error = new WebSocketConnectionError(websocket.url, protocols, event, `${t('连接被关闭')}, code: ${event.code}${event.reason ? `, ${t('原因')}: ${event.reason}` : ''}`);
|
|
211
214
|
if (settled)
|
|
@@ -278,8 +281,10 @@ export class Remote {
|
|
|
278
281
|
一元 rpc 接收方不需要设置 handlers, 发送方需要 */
|
|
279
282
|
handlers = new Map();
|
|
280
283
|
keeper;
|
|
284
|
+
/** `true` 是否打印连接信息、错误信息 */
|
|
285
|
+
print = true;
|
|
281
286
|
/** `false` 打印所有交互的 rpc messages */
|
|
282
|
-
|
|
287
|
+
verbose = false;
|
|
283
288
|
first_error = true;
|
|
284
289
|
keeping = false;
|
|
285
290
|
reconnecting = false;
|
|
@@ -305,7 +310,6 @@ export class Remote {
|
|
|
305
310
|
return message;
|
|
306
311
|
}
|
|
307
312
|
static pack({ id, func, data = [], done, error }) {
|
|
308
|
-
assert('length' in data, 'message.data 必须是数组');
|
|
309
313
|
let data_ = new Array(data.length);
|
|
310
314
|
let bins = [];
|
|
311
315
|
let bufs = [];
|
|
@@ -335,12 +339,12 @@ export class Remote {
|
|
|
335
339
|
}
|
|
336
340
|
/** 作为 websocket 连接发起方,传入 url 或 websocket,定义远程 Remote
|
|
337
341
|
作为 websocket 连接接收方,不传 url 和 websocket,定义本地 Remote */
|
|
338
|
-
constructor({ url, funcs, print, websocket, keeper, on_error, } = {}) {
|
|
339
|
-
|
|
342
|
+
constructor({ url, funcs, print, verbose, websocket, keeper, on_error, } = {}) {
|
|
343
|
+
check(!(url && websocket), '构建 Remote 时 url 和 websocket 最多只能传一个');
|
|
340
344
|
this.initiator = Boolean(url || websocket);
|
|
341
345
|
if (url)
|
|
342
346
|
this.url = url;
|
|
343
|
-
|
|
347
|
+
check(!funcs?.echo);
|
|
344
348
|
this.funcs = this.initiator ? funcs : {
|
|
345
349
|
...funcs,
|
|
346
350
|
echo({ data }) {
|
|
@@ -349,12 +353,14 @@ export class Remote {
|
|
|
349
353
|
};
|
|
350
354
|
if (print !== undefined)
|
|
351
355
|
this.print = print;
|
|
356
|
+
if (verbose !== undefined)
|
|
357
|
+
this.verbose = verbose;
|
|
352
358
|
if (on_error)
|
|
353
359
|
this.on_error = on_error;
|
|
354
360
|
if (this.initiator)
|
|
355
361
|
this.lwebsocket = new Lock(websocket || null);
|
|
356
362
|
if (keeper) {
|
|
357
|
-
|
|
363
|
+
check(this.initiator && url);
|
|
358
364
|
this.keeper = {
|
|
359
365
|
reconnect_interval: 1000 * 5,
|
|
360
366
|
heartbeat_interval: 1000 * 60,
|
|
@@ -380,14 +386,13 @@ export class Remote {
|
|
|
380
386
|
this.reconnecting = false;
|
|
381
387
|
if (!this.disconnected)
|
|
382
388
|
try {
|
|
383
|
-
await timeout(3000, async () => {
|
|
384
|
-
await this.connect();
|
|
385
|
-
});
|
|
389
|
+
await timeout(3000, async () => { await this.connect(); }, undefined, this.print);
|
|
386
390
|
this.first_error = true;
|
|
387
391
|
}
|
|
388
392
|
catch (error) {
|
|
389
393
|
// 重连失败的错误这里需要简单打印下,_on_error 不会打印,这里也不继续往上抛了
|
|
390
|
-
|
|
394
|
+
if (this.print)
|
|
395
|
+
console.log(error.message);
|
|
391
396
|
// 重连由 this.connect 里面调用 this._on_error 处理
|
|
392
397
|
}
|
|
393
398
|
})();
|
|
@@ -397,8 +402,10 @@ export class Remote {
|
|
|
397
402
|
};
|
|
398
403
|
/** 使用者自定义的在连接后出错时的处理 */
|
|
399
404
|
on_error(error, websocket) {
|
|
400
|
-
if (this.keeper)
|
|
401
|
-
|
|
405
|
+
if (this.keeper) {
|
|
406
|
+
if (this.print)
|
|
407
|
+
console.log(error.message);
|
|
408
|
+
}
|
|
402
409
|
else
|
|
403
410
|
throw error;
|
|
404
411
|
}
|
|
@@ -424,7 +431,8 @@ export class Remote {
|
|
|
424
431
|
try {
|
|
425
432
|
this.lwebsocket.resource = await connect_websocket(this.url, {
|
|
426
433
|
on_message: this._on_message,
|
|
427
|
-
on_error: this._on_error
|
|
434
|
+
on_error: this._on_error,
|
|
435
|
+
print: this.print
|
|
428
436
|
});
|
|
429
437
|
reconnected = true;
|
|
430
438
|
}
|
|
@@ -446,12 +454,11 @@ export class Remote {
|
|
|
446
454
|
break;
|
|
447
455
|
if (!this.reconnecting)
|
|
448
456
|
try {
|
|
449
|
-
await timeout(1000 * 2, async () => {
|
|
450
|
-
await this.call('echo', []);
|
|
451
|
-
});
|
|
457
|
+
await timeout(1000 * 2, async () => { await this.call('echo', []); }, undefined, this.print);
|
|
452
458
|
}
|
|
453
459
|
catch (error) {
|
|
454
|
-
|
|
460
|
+
if (this.print)
|
|
461
|
+
console.log(error.message);
|
|
455
462
|
this._on_error(error);
|
|
456
463
|
}
|
|
457
464
|
}
|
|
@@ -474,8 +481,8 @@ export class Remote {
|
|
|
474
481
|
作为 websocket 连接接收方,必传 websocket 参数
|
|
475
482
|
发送或连接出错时自动清理 message.id 对应的 handler */
|
|
476
483
|
async send(message, websocket) {
|
|
477
|
-
|
|
478
|
-
if (this.
|
|
484
|
+
check(!message.data || message.data.every(arg => arg !== undefined), `message.data 数组中不能有 undefined 的项, 因为 json 序列化后会变为 null. (func: ${message.func}, id: ${message.id})`);
|
|
485
|
+
if (this.verbose)
|
|
479
486
|
console.log('remote.send:', message);
|
|
480
487
|
try {
|
|
481
488
|
await this.connect(websocket);
|
|
@@ -496,7 +503,7 @@ export class Remote {
|
|
|
496
503
|
async handle(data, websocket) {
|
|
497
504
|
const message = Remote.parse(data);
|
|
498
505
|
const { id, func, done } = message;
|
|
499
|
-
if (this.
|
|
506
|
+
if (this.verbose)
|
|
500
507
|
console.log('remote.handle:', message);
|
|
501
508
|
let handler;
|
|
502
509
|
if (func)
|
package/net.d.ts
CHANGED
|
@@ -147,15 +147,17 @@ export declare class WebSocketConnectionError extends Error {
|
|
|
147
147
|
- on_error?: 在 websocket 出错和非正常关闭 (close, error 事件) 时都调用,可以根据 error.type 来区分,error 的类型是 WebSocketConnectionError,
|
|
148
148
|
type 为 'close' 时有 code 和 reason 属性
|
|
149
149
|
- on_close?: 和 websocket 的 'close' 事件不相同,只在正常关闭 (close code 为 1000) 时才调用,否则都会调用 on_error
|
|
150
|
-
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes
|
|
150
|
+
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes
|
|
151
|
+
- print?: 是否打印连接、关闭信息 */
|
|
151
152
|
export declare function connect_websocket(url: string | URL, { protocols, max_payload, // 8 GB
|
|
152
|
-
on_message, on_error, on_close, proxy }: {
|
|
153
|
+
on_message, on_error, on_close, proxy, print, }: {
|
|
153
154
|
protocols?: string[];
|
|
154
155
|
max_payload?: number;
|
|
155
156
|
on_message(data: ArrayBuffer | string, websocket: WebSocket): any;
|
|
156
157
|
on_error?(error: WebSocketConnectionError, websocket: WebSocket): any;
|
|
157
158
|
on_close?(event: CloseEvent, websocket: WebSocket): any;
|
|
158
159
|
proxy?: string;
|
|
160
|
+
print?: boolean;
|
|
159
161
|
}): Promise<WebSocket>;
|
|
160
162
|
/** 接收到消息后的处理函数
|
|
161
163
|
返回值可以是:
|
|
@@ -245,8 +247,10 @@ export declare class Remote {
|
|
|
245
247
|
一元 rpc 接收方不需要设置 handlers, 发送方需要 */
|
|
246
248
|
handlers: Map<number, MessageHandler<any[]>>;
|
|
247
249
|
keeper?: RemoteKeeperOptions;
|
|
248
|
-
/** `
|
|
250
|
+
/** `true` 是否打印连接信息、错误信息 */
|
|
249
251
|
print: boolean;
|
|
252
|
+
/** `false` 打印所有交互的 rpc messages */
|
|
253
|
+
verbose: boolean;
|
|
250
254
|
first_error: boolean;
|
|
251
255
|
keeping: boolean;
|
|
252
256
|
reconnecting: boolean;
|
|
@@ -256,10 +260,11 @@ export declare class Remote {
|
|
|
256
260
|
static pack({ id, func, data, done, error }: Message): Uint8Array;
|
|
257
261
|
/** 作为 websocket 连接发起方,传入 url 或 websocket,定义远程 Remote
|
|
258
262
|
作为 websocket 连接接收方,不传 url 和 websocket,定义本地 Remote */
|
|
259
|
-
constructor({ url, funcs, print, websocket, keeper, on_error, }?: {
|
|
263
|
+
constructor({ url, funcs, print, verbose, websocket, keeper, on_error, }?: {
|
|
260
264
|
url?: string;
|
|
261
265
|
funcs?: Remote['funcs'];
|
|
262
266
|
print?: boolean;
|
|
267
|
+
verbose?: boolean;
|
|
263
268
|
websocket?: WebSocket;
|
|
264
269
|
keeper?: RemoteKeeperOptions;
|
|
265
270
|
on_error?(error: WebSocketConnectionError, websocket?: WebSocket): void;
|
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 './prototype.js';
|
|
6
|
-
import { inspect, concat, assert, genid, delay, Lock, encode, decode, pipe_with_error, map_values, unique, timeout } from './utils.js';
|
|
6
|
+
import { inspect, concat, assert, genid, delay, Lock, encode, decode, pipe_with_error, map_values, unique, timeout, check } from './utils.js';
|
|
7
7
|
export const WebSocketConnecting = 0;
|
|
8
8
|
export const WebSocketOpen = 1;
|
|
9
9
|
export const WebSocketClosing = 2;
|
|
@@ -38,13 +38,17 @@ const drop_request_headers = new Set([
|
|
|
38
38
|
'upgrade',
|
|
39
39
|
]);
|
|
40
40
|
let proxy_agents = {};
|
|
41
|
-
async function request_retry(url, options,
|
|
41
|
+
async function request_retry(url, options, _timeout, retries = 0, count = 0) {
|
|
42
42
|
let { default: undici, request: undici_request } = await import('undici');
|
|
43
43
|
undici_request ??= undici.request;
|
|
44
44
|
try {
|
|
45
|
-
if (
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
if (_timeout > 0) {
|
|
46
|
+
// 设置给 undici 设置 timeout, signal 不一定管用,还是得自己兜底
|
|
47
|
+
options.signal = AbortSignal.timeout(_timeout);
|
|
48
|
+
return await timeout(_timeout + 300 /* 为 undici 兜底 */, async () => undici.request(url, options));
|
|
49
|
+
}
|
|
50
|
+
else
|
|
51
|
+
return await undici.request(url, options);
|
|
48
52
|
}
|
|
49
53
|
catch (error) {
|
|
50
54
|
if (error.name === 'TimeoutError')
|
|
@@ -52,7 +56,7 @@ async function request_retry(url, options, timeout, retries = 0, count = 0) {
|
|
|
52
56
|
const duration = 2 ** count;
|
|
53
57
|
console.log(`${t('等待 {{duration}} 秒后重试 request (已尝试 {{_count}} 次) …', { duration, _count: count + 1 }).yellow} ${url.toString().blue.underline}`);
|
|
54
58
|
await delay(1000 * duration);
|
|
55
|
-
return request_retry(url, options,
|
|
59
|
+
return request_retry(url, options, _timeout, retries, count + 1);
|
|
56
60
|
}
|
|
57
61
|
else
|
|
58
62
|
throw Object.assign(new Error(`request 超时: ${url.toString()}`), { name: 'TimeoutError' });
|
|
@@ -127,11 +131,11 @@ export async function request(url, options = {}) {
|
|
|
127
131
|
// todo: 强制手动处理重定向,来正确处理 cookie ?
|
|
128
132
|
maxRedirections: redirect === 'follow' ? 5 : 0,
|
|
129
133
|
// 下面这些 timeout 都不是总的时间,没法用
|
|
130
|
-
|
|
134
|
+
headersTimeout: timeout,
|
|
131
135
|
// 从收完 headers 开始算
|
|
132
|
-
|
|
136
|
+
bodyTimeout: timeout,
|
|
133
137
|
// @ts-ignore 没有类型声明,实际可用
|
|
134
|
-
|
|
138
|
+
connectTimeout: timeout,
|
|
135
139
|
headers,
|
|
136
140
|
// --- body
|
|
137
141
|
body: (() => {
|
|
@@ -392,9 +396,10 @@ let websocket_proxy_agents = {};
|
|
|
392
396
|
- on_error?: 在 websocket 出错和非正常关闭 (close, error 事件) 时都调用,可以根据 error.type 来区分,error 的类型是 WebSocketConnectionError,
|
|
393
397
|
type 为 'close' 时有 code 和 reason 属性
|
|
394
398
|
- on_close?: 和 websocket 的 'close' 事件不相同,只在正常关闭 (close code 为 1000) 时才调用,否则都会调用 on_error
|
|
395
|
-
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes
|
|
399
|
+
https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes
|
|
400
|
+
- print?: 是否打印连接、关闭信息 */
|
|
396
401
|
export async function connect_websocket(url, { protocols, max_payload = 2 ** 33, // 8 GB
|
|
397
|
-
on_message, on_error, on_close, proxy }) {
|
|
402
|
+
on_message, on_error, on_close, proxy, print = true, }) {
|
|
398
403
|
const { WebSocket } = await import('ws');
|
|
399
404
|
const { HttpsProxyAgent } = await import('https-proxy-agent');
|
|
400
405
|
let websocket = new WebSocket(url, protocols, {
|
|
@@ -410,9 +415,10 @@ on_message, on_error, on_close, proxy }) {
|
|
|
410
415
|
return new Promise((resolve, reject) => {
|
|
411
416
|
let settled = false;
|
|
412
417
|
websocket.addEventListener('open', event => {
|
|
413
|
-
|
|
414
|
-
(websocket.
|
|
415
|
-
|
|
418
|
+
if (print)
|
|
419
|
+
console.log(websocket.url +
|
|
420
|
+
(websocket.protocol ? ' ' + websocket.protocol.bracket() : '') +
|
|
421
|
+
t(' 已连接'));
|
|
416
422
|
settled = true;
|
|
417
423
|
resolve(websocket);
|
|
418
424
|
});
|
|
@@ -422,13 +428,14 @@ on_message, on_error, on_close, proxy }) {
|
|
|
422
428
|
// 接着马上 close 事件也被调用,此时 settled,马上调用了 on_error 函数,弄乱了顺序
|
|
423
429
|
// error 的错误信息比较多,而且通过 await 得到的栈也比较清晰,这里延后调用 on_close 和 on_error,放到微任务队列之后的 timers 队列中
|
|
424
430
|
setTimeout(() => {
|
|
425
|
-
if (event.code === 1000) // 正常关闭
|
|
431
|
+
if (event.code === 1000) { // 正常关闭
|
|
426
432
|
if (on_close)
|
|
427
433
|
on_close(event, websocket);
|
|
428
|
-
else
|
|
434
|
+
else if (print)
|
|
429
435
|
console.log(`${websocket.url} ${t('已正常关闭')}`);
|
|
436
|
+
}
|
|
430
437
|
else { // 异常关闭,认为发生了错误,进行错误处理
|
|
431
|
-
|
|
438
|
+
// websocket close 事件时已经 settled
|
|
432
439
|
const error = new WebSocketConnectionError(websocket.url, protocols, event, `${t('连接被关闭')}, code: ${event.code}${event.reason ? `, ${t('原因')}: ${event.reason}` : ''}`);
|
|
433
440
|
if (on_error)
|
|
434
441
|
on_error(error, websocket);
|
|
@@ -492,8 +499,10 @@ export class Remote {
|
|
|
492
499
|
一元 rpc 接收方不需要设置 handlers, 发送方需要 */
|
|
493
500
|
handlers = new Map();
|
|
494
501
|
keeper;
|
|
502
|
+
/** `true` 是否打印连接信息、错误信息 */
|
|
503
|
+
print = true;
|
|
495
504
|
/** `false` 打印所有交互的 rpc messages */
|
|
496
|
-
|
|
505
|
+
verbose = false;
|
|
497
506
|
first_error = true;
|
|
498
507
|
keeping = false;
|
|
499
508
|
reconnecting = false;
|
|
@@ -519,7 +528,6 @@ export class Remote {
|
|
|
519
528
|
return message;
|
|
520
529
|
}
|
|
521
530
|
static pack({ id, func, data = [], done, error }) {
|
|
522
|
-
assert('length' in data, 'message.data 必须是数组');
|
|
523
531
|
let data_ = new Array(data.length);
|
|
524
532
|
let bins = [];
|
|
525
533
|
let bufs = [];
|
|
@@ -549,12 +557,12 @@ export class Remote {
|
|
|
549
557
|
}
|
|
550
558
|
/** 作为 websocket 连接发起方,传入 url 或 websocket,定义远程 Remote
|
|
551
559
|
作为 websocket 连接接收方,不传 url 和 websocket,定义本地 Remote */
|
|
552
|
-
constructor({ url, funcs, print, websocket, keeper, on_error, } = {}) {
|
|
553
|
-
|
|
560
|
+
constructor({ url, funcs, print, verbose, websocket, keeper, on_error, } = {}) {
|
|
561
|
+
check(!(url && websocket), '构建 Remote 时 url 和 websocket 最多只能传一个');
|
|
554
562
|
this.initiator = Boolean(url || websocket);
|
|
555
563
|
if (url)
|
|
556
564
|
this.url = url;
|
|
557
|
-
|
|
565
|
+
check(!funcs?.echo);
|
|
558
566
|
this.funcs = this.initiator ? funcs : {
|
|
559
567
|
...funcs,
|
|
560
568
|
echo({ data }) {
|
|
@@ -563,12 +571,14 @@ export class Remote {
|
|
|
563
571
|
};
|
|
564
572
|
if (print !== undefined)
|
|
565
573
|
this.print = print;
|
|
574
|
+
if (verbose !== undefined)
|
|
575
|
+
this.verbose = verbose;
|
|
566
576
|
if (on_error)
|
|
567
577
|
this.on_error = on_error;
|
|
568
578
|
if (this.initiator)
|
|
569
579
|
this.lwebsocket = new Lock(websocket || null);
|
|
570
580
|
if (keeper) {
|
|
571
|
-
|
|
581
|
+
check(this.initiator && url);
|
|
572
582
|
this.keeper = {
|
|
573
583
|
reconnect_interval: 1000 * 5,
|
|
574
584
|
heartbeat_interval: 1000 * 60,
|
|
@@ -594,14 +604,13 @@ export class Remote {
|
|
|
594
604
|
this.reconnecting = false;
|
|
595
605
|
if (!this.disconnected)
|
|
596
606
|
try {
|
|
597
|
-
await timeout(3000, async () => {
|
|
598
|
-
await this.connect();
|
|
599
|
-
});
|
|
607
|
+
await timeout(3000, async () => { await this.connect(); }, undefined, this.print);
|
|
600
608
|
this.first_error = true;
|
|
601
609
|
}
|
|
602
610
|
catch (error) {
|
|
603
611
|
// 重连失败的错误这里需要简单打印下,_on_error 不会打印,这里也不继续往上抛了
|
|
604
|
-
|
|
612
|
+
if (this.print)
|
|
613
|
+
console.log(error.message);
|
|
605
614
|
// 重连由 this.connect 里面调用 this._on_error 处理
|
|
606
615
|
}
|
|
607
616
|
})();
|
|
@@ -611,8 +620,10 @@ export class Remote {
|
|
|
611
620
|
};
|
|
612
621
|
/** 使用者自定义的在连接后出错时的处理 */
|
|
613
622
|
on_error(error, websocket) {
|
|
614
|
-
if (this.keeper)
|
|
615
|
-
|
|
623
|
+
if (this.keeper) {
|
|
624
|
+
if (this.print)
|
|
625
|
+
console.log(error.message);
|
|
626
|
+
}
|
|
616
627
|
else
|
|
617
628
|
throw error;
|
|
618
629
|
}
|
|
@@ -638,7 +649,8 @@ export class Remote {
|
|
|
638
649
|
try {
|
|
639
650
|
this.lwebsocket.resource = await connect_websocket(this.url, {
|
|
640
651
|
on_message: this._on_message,
|
|
641
|
-
on_error: this._on_error
|
|
652
|
+
on_error: this._on_error,
|
|
653
|
+
print: this.print
|
|
642
654
|
});
|
|
643
655
|
reconnected = true;
|
|
644
656
|
}
|
|
@@ -660,12 +672,11 @@ export class Remote {
|
|
|
660
672
|
break;
|
|
661
673
|
if (!this.reconnecting)
|
|
662
674
|
try {
|
|
663
|
-
await timeout(1000 * 2, async () => {
|
|
664
|
-
await this.call('echo', []);
|
|
665
|
-
});
|
|
675
|
+
await timeout(1000 * 2, async () => { await this.call('echo', []); }, undefined, this.print);
|
|
666
676
|
}
|
|
667
677
|
catch (error) {
|
|
668
|
-
|
|
678
|
+
if (this.print)
|
|
679
|
+
console.log(error.message);
|
|
669
680
|
this._on_error(error);
|
|
670
681
|
}
|
|
671
682
|
}
|
|
@@ -688,8 +699,8 @@ export class Remote {
|
|
|
688
699
|
作为 websocket 连接接收方,必传 websocket 参数
|
|
689
700
|
发送或连接出错时自动清理 message.id 对应的 handler */
|
|
690
701
|
async send(message, websocket) {
|
|
691
|
-
|
|
692
|
-
if (this.
|
|
702
|
+
check(!message.data || message.data.every(arg => arg !== undefined), `message.data 数组中不能有 undefined 的项, 因为 json 序列化后会变为 null. (func: ${message.func}, id: ${message.id})`);
|
|
703
|
+
if (this.verbose)
|
|
693
704
|
console.log('remote.send:', message);
|
|
694
705
|
try {
|
|
695
706
|
await this.connect(websocket);
|
|
@@ -710,7 +721,7 @@ export class Remote {
|
|
|
710
721
|
async handle(data, websocket) {
|
|
711
722
|
const message = Remote.parse(data);
|
|
712
723
|
const { id, func, done } = message;
|
|
713
|
-
if (this.
|
|
724
|
+
if (this.verbose)
|
|
714
725
|
console.log('remote.handle:', message);
|
|
715
726
|
let handler;
|
|
716
727
|
if (func)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xshell",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.199",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"bin": {
|
|
@@ -53,13 +53,13 @@
|
|
|
53
53
|
"@babel/parser": "^7.26.2",
|
|
54
54
|
"@babel/traverse": "^7.25.9",
|
|
55
55
|
"@koa/cors": "^5.0.0",
|
|
56
|
-
"@stylistic/eslint-plugin": "^2.
|
|
56
|
+
"@stylistic/eslint-plugin": "^2.11.0",
|
|
57
57
|
"@svgr/webpack": "^8.1.0",
|
|
58
58
|
"@types/sass-loader": "^8.0.9",
|
|
59
59
|
"@types/ws": "^8.5.13",
|
|
60
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
61
|
-
"@typescript-eslint/parser": "^8.
|
|
62
|
-
"@typescript-eslint/utils": "^8.
|
|
60
|
+
"@typescript-eslint/eslint-plugin": "^8.15.0",
|
|
61
|
+
"@typescript-eslint/parser": "^8.15.0",
|
|
62
|
+
"@typescript-eslint/utils": "^8.15.0",
|
|
63
63
|
"@xterm/addon-fit": "^0.10.0",
|
|
64
64
|
"@xterm/addon-web-links": "^0.11.0",
|
|
65
65
|
"@xterm/addon-webgl": "^0.18.0",
|
|
@@ -75,13 +75,13 @@
|
|
|
75
75
|
"commander": "^12.1.0",
|
|
76
76
|
"css-loader": "^7.1.2",
|
|
77
77
|
"emoji-regex": "^10.4.0",
|
|
78
|
-
"eslint": "^9.
|
|
78
|
+
"eslint": "^9.15.0",
|
|
79
79
|
"eslint-plugin-import": "^2.31.0",
|
|
80
80
|
"eslint-plugin-react": "^7.37.2",
|
|
81
81
|
"gulp-sort": "^2.0.0",
|
|
82
82
|
"hash-string": "^1.0.0",
|
|
83
83
|
"https-proxy-agent": "^7.0.5",
|
|
84
|
-
"i18next": "^23.16.
|
|
84
|
+
"i18next": "^23.16.8",
|
|
85
85
|
"i18next-scanner": "^4.6.0",
|
|
86
86
|
"koa": "^2.15.3",
|
|
87
87
|
"koa-compress": "^5.1.1",
|
|
@@ -91,10 +91,10 @@
|
|
|
91
91
|
"mime-types": "^2.1.35",
|
|
92
92
|
"ora": "^8.1.1",
|
|
93
93
|
"react": "^18.3.1",
|
|
94
|
-
"react-i18next": "^15.1.
|
|
94
|
+
"react-i18next": "^15.1.1",
|
|
95
95
|
"react-object-model": "^1.2.17",
|
|
96
96
|
"resolve-path": "^1.4.0",
|
|
97
|
-
"sass": "^1.
|
|
97
|
+
"sass": "^1.81.0",
|
|
98
98
|
"sass-loader": "^16.0.3",
|
|
99
99
|
"source-map-loader": "^5.0.0",
|
|
100
100
|
"strip-ansi": "^7.1.0",
|
|
@@ -104,8 +104,8 @@
|
|
|
104
104
|
"ts-loader": "^9.5.1",
|
|
105
105
|
"tslib": "^2.8.1",
|
|
106
106
|
"typescript": "^5.6.3",
|
|
107
|
-
"ua-parser-js": "^2.0.0-rc.
|
|
108
|
-
"undici": "^6.
|
|
107
|
+
"ua-parser-js": "^2.0.0-rc.3",
|
|
108
|
+
"undici": "^6.21.0",
|
|
109
109
|
"vinyl": "^3.0.0",
|
|
110
110
|
"vinyl-fs": "^4.0.0",
|
|
111
111
|
"webpack": "^5.96.1",
|
|
@@ -126,7 +126,7 @@
|
|
|
126
126
|
"@types/koa-compress": "^4.0.6",
|
|
127
127
|
"@types/lodash": "^4.17.13",
|
|
128
128
|
"@types/mime-types": "^2.1.4",
|
|
129
|
-
"@types/node": "^22.9.
|
|
129
|
+
"@types/node": "^22.9.1",
|
|
130
130
|
"@types/react": "^18.3.12",
|
|
131
131
|
"@types/through2": "^2.0.41",
|
|
132
132
|
"@types/tough-cookie": "^4.0.5",
|
package/process.d.ts
CHANGED
|
@@ -82,6 +82,7 @@ export interface CallOptions extends StartOptions {
|
|
|
82
82
|
stdout?: (chunk: string) => void;
|
|
83
83
|
stderr?: (chunk: string) => void;
|
|
84
84
|
};
|
|
85
|
+
on_child?: (child: ChildProcess) => void;
|
|
85
86
|
}
|
|
86
87
|
export interface CallResult<TOutput extends string | Buffer = string> {
|
|
87
88
|
pid: number;
|
|
@@ -111,10 +112,12 @@ export interface CallError {
|
|
|
111
112
|
- encoding?: `'utf-8'` 子进程输出编码, 设置为 binary 时返回 stdout, stderr 类型为 Buffer; 否则是 string
|
|
112
113
|
child output encoding. When set to binary, return stdout, stderr is of type Buffer; otherwise it is string
|
|
113
114
|
- print?: `true` print 选项,支持设置细项 print option (with details)
|
|
115
|
+
- printers?: 实时处理 stdout 和 stderr 的每个 chunk
|
|
114
116
|
- stdio?: `'pipe'` 设置为 'ignore' 时忽略 stdio 处理 when 'ignore' then ignore stdio processing
|
|
115
117
|
- input?: string, 启动子进程之后写入到子进程 stdin 中的内容,写完后关闭子进程 stdin (pty 不关闭)
|
|
116
118
|
- detached?: `false` 是否断开和 child 的关系 (ignore stdio, unref) whether to break the connection with child (ignore stdio, unref)
|
|
117
|
-
- throw_code?: `true` code 不为 0 时是否抛出异常 whether to throw Error when code is not 0
|
|
119
|
+
- throw_code?: `true` code 不为 0 时是否抛出异常 whether to throw Error when code is not 0
|
|
120
|
+
- on_child: 可以传入回调函数及时获取通过 start 创建的子进程,便于执行 kill、获得 pid 等操作 */
|
|
118
121
|
export declare function call(exe: string, args?: string[]): Promise<CallResult<string>>;
|
|
119
122
|
export declare function call(exe: string, args?: string[], options?: CallOptions & {
|
|
120
123
|
encoding: 'binary';
|
package/process.js
CHANGED
|
@@ -116,7 +116,7 @@ export async function start_nodejs(js, args = [], options) {
|
|
|
116
116
|
return start(exe_nodejs, [js, ...args], options);
|
|
117
117
|
}
|
|
118
118
|
export async function call(exe, args = [], options = {}) {
|
|
119
|
-
const { encoding = 'utf-8', throw_code = true, printers } = options;
|
|
119
|
+
const { encoding = 'utf-8', throw_code = true, printers, on_child } = options;
|
|
120
120
|
let { stdio = 'pipe', print = true } = options;
|
|
121
121
|
if (typeof print === 'boolean')
|
|
122
122
|
print = {
|
|
@@ -138,6 +138,7 @@ export async function call(exe, args = [], options = {}) {
|
|
|
138
138
|
...options,
|
|
139
139
|
print: false,
|
|
140
140
|
});
|
|
141
|
+
on_child?.(child);
|
|
141
142
|
// --- collect output
|
|
142
143
|
let stdouts = [];
|
|
143
144
|
let stderrs = [];
|
package/server.js
CHANGED
|
@@ -2,13 +2,13 @@ import { createServer as http_create_server, } from 'http';
|
|
|
2
2
|
import { createSecureServer as http2_create_server } from 'http2';
|
|
3
3
|
import { createSecureContext } from 'tls';
|
|
4
4
|
import zlib from 'zlib';
|
|
5
|
-
import
|
|
5
|
+
import fs from 'fs';
|
|
6
6
|
import { buffer as stream_to_buffer } from 'stream/consumers';
|
|
7
7
|
import util from 'util';
|
|
8
8
|
// --- my libs
|
|
9
9
|
import { t } from './i18n/instance.js';
|
|
10
10
|
import { request as _request, Remote } from './net.js';
|
|
11
|
-
import { inspect, output_width,
|
|
11
|
+
import { inspect, output_width, check, range_to_numbers, encode, filter_keys, filter_values, consume_stream, assert } from './utils.js';
|
|
12
12
|
import { flist, fread, fstat } from './file.js';
|
|
13
13
|
// ------------ my server
|
|
14
14
|
export class Server {
|
|
@@ -57,7 +57,7 @@ export class Server {
|
|
|
57
57
|
this.http = http;
|
|
58
58
|
if (http2)
|
|
59
59
|
this.http2 = http2;
|
|
60
|
-
|
|
60
|
+
check(this.http || this.http2, 'http 和 http2 至少启用一个');
|
|
61
61
|
if (http_port !== undefined)
|
|
62
62
|
this.http_port = http_port;
|
|
63
63
|
if (http2_port !== undefined)
|
|
@@ -73,7 +73,7 @@ export class Server {
|
|
|
73
73
|
else if (funcs)
|
|
74
74
|
this.remote = new Remote({ funcs });
|
|
75
75
|
if (stdio_subscribable !== undefined) {
|
|
76
|
-
|
|
76
|
+
check(remote || funcs);
|
|
77
77
|
this.stdio_subscribable = stdio_subscribable;
|
|
78
78
|
}
|
|
79
79
|
}
|
|
@@ -125,7 +125,7 @@ export class Server {
|
|
|
125
125
|
return [domain, { key, cert }];
|
|
126
126
|
})));
|
|
127
127
|
const default_ctx = lazy_secure_ctxs[this.default_hostnames.find(hostname => hostname in lazy_secure_ctxs)];
|
|
128
|
-
|
|
128
|
+
check(default_ctx);
|
|
129
129
|
this.http2_server = http2_create_server({
|
|
130
130
|
SNICallback(servername, callback) {
|
|
131
131
|
let lazy_ctx = lazy_secure_ctxs[servername] ||
|
|
@@ -571,7 +571,7 @@ export class Server {
|
|
|
571
571
|
- download?: `undefined` 在 response.headers 中加上 content-disposition: attachment 指示浏览器下载文件
|
|
572
572
|
- assets_root?: 替换模板 xxx.template.html 中 {root} 占位符 */
|
|
573
573
|
async fsend(ctx, fp, { root, absolute, download, assets_root, } = {}) {
|
|
574
|
-
|
|
574
|
+
check(absolute || root?.isdir, t('fsend 必须传 absolute 选项或 root 文件夹'));
|
|
575
575
|
const { request, request: { method } } = ctx;
|
|
576
576
|
let { response } = ctx;
|
|
577
577
|
if (!absolute) {
|
|
@@ -630,7 +630,7 @@ export class Server {
|
|
|
630
630
|
return fp;
|
|
631
631
|
}
|
|
632
632
|
if (assets_root) {
|
|
633
|
-
|
|
633
|
+
check(fp.endsWith('.html'), '传入 assets_root 参数时 fp 应该为 .html 文件');
|
|
634
634
|
response.body = (await fread(fp, { print: false }))
|
|
635
635
|
.replaceAll('{root}', assets_root);
|
|
636
636
|
return fp;
|
|
@@ -651,17 +651,17 @@ export class Server {
|
|
|
651
651
|
response.status = 206;
|
|
652
652
|
response.set('content-length', String(chunksize));
|
|
653
653
|
response.set('content-range', `bytes ${start}-${end}/${stats.size}`);
|
|
654
|
-
response.body = createReadStream(fp, { start, end });
|
|
654
|
+
response.body = fs.createReadStream(fp, { start, end });
|
|
655
655
|
}
|
|
656
656
|
catch {
|
|
657
657
|
response.status = 416;
|
|
658
658
|
response.set('content-length', String(stats.size));
|
|
659
659
|
response.set('content-range', `bytes */${stats.size}`);
|
|
660
|
-
response.body = createReadStream(fp);
|
|
660
|
+
response.body = fs.createReadStream(fp);
|
|
661
661
|
}
|
|
662
662
|
else {
|
|
663
663
|
response.set('content-length', String(stats.size));
|
|
664
|
-
response.body = createReadStream(fp);
|
|
664
|
+
response.body = fs.createReadStream(fp);
|
|
665
665
|
}
|
|
666
666
|
return fp;
|
|
667
667
|
}
|
package/utils.browser.d.ts
CHANGED
|
@@ -28,8 +28,9 @@ export declare class TimeoutError extends Error {
|
|
|
28
28
|
}
|
|
29
29
|
/** 在指定的时间 (milliseconds) 内运行某个任务,超时之后
|
|
30
30
|
- 如果传入了 on_timeout 参数: 调用 on_timeout,返回 null
|
|
31
|
-
- 如果没传入 on_timeout 参数: 抛出 TimeoutError
|
|
32
|
-
|
|
31
|
+
- 如果没传入 on_timeout 参数: 抛出 TimeoutError
|
|
32
|
+
- print?: 打印已超时任务的错误 */
|
|
33
|
+
export declare function timeout<TReturn>(milliseconds: number, action: () => Promise<TReturn>, on_timeout?: () => void | Promise<void>, print?: boolean): Promise<TReturn>;
|
|
33
34
|
/** https://stackoverflow.com/questions/63297164/how-to-only-accept-arraybuffer-as-parameter */
|
|
34
35
|
export type StrictArrayBuffer = ArrayBuffer & {
|
|
35
36
|
buffer?: undefined;
|
package/utils.browser.js
CHANGED
|
@@ -80,8 +80,9 @@ export class TimeoutError extends Error {
|
|
|
80
80
|
}
|
|
81
81
|
/** 在指定的时间 (milliseconds) 内运行某个任务,超时之后
|
|
82
82
|
- 如果传入了 on_timeout 参数: 调用 on_timeout,返回 null
|
|
83
|
-
- 如果没传入 on_timeout 参数: 抛出 TimeoutError
|
|
84
|
-
|
|
83
|
+
- 如果没传入 on_timeout 参数: 抛出 TimeoutError
|
|
84
|
+
- print?: 打印已超时任务的错误 */
|
|
85
|
+
export async function timeout(milliseconds, action, on_timeout, print = true) {
|
|
85
86
|
const error = new TimeoutError();
|
|
86
87
|
return new Promise((resolve, reject) => {
|
|
87
88
|
let done = false;
|
|
@@ -112,8 +113,10 @@ export async function timeout(milliseconds, action, on_timeout) {
|
|
|
112
113
|
resolve(await action());
|
|
113
114
|
}
|
|
114
115
|
catch (error) {
|
|
115
|
-
if (rejected)
|
|
116
|
-
|
|
116
|
+
if (rejected) {
|
|
117
|
+
if (print)
|
|
118
|
+
console.log(`已超时任务的错误: ${error.message}`);
|
|
119
|
+
}
|
|
117
120
|
else {
|
|
118
121
|
rejected = true;
|
|
119
122
|
reject(error);
|
package/utils.d.ts
CHANGED
|
@@ -89,8 +89,9 @@ export declare class TimeoutError extends Error {
|
|
|
89
89
|
}
|
|
90
90
|
/** 在指定的时间 (milliseconds) 内运行某个任务,超时之后
|
|
91
91
|
- 如果传入了 on_timeout 参数: 调用 on_timeout,返回 null
|
|
92
|
-
- 如果没传入 on_timeout 参数: 抛出 TimeoutError
|
|
93
|
-
|
|
92
|
+
- 如果没传入 on_timeout 参数: 抛出 TimeoutError
|
|
93
|
+
- print?: 打印已超时任务的错误 */
|
|
94
|
+
export declare function timeout<TReturn>(milliseconds: number, action: () => Promise<TReturn>, on_timeout?: () => void | Promise<void>, print?: boolean): Promise<TReturn>;
|
|
94
95
|
/** https://stackoverflow.com/questions/63297164/how-to-only-accept-arraybuffer-as-parameter */
|
|
95
96
|
export type StrictArrayBuffer = ArrayBuffer & {
|
|
96
97
|
buffer?: undefined;
|
package/utils.js
CHANGED
|
@@ -251,8 +251,9 @@ export class TimeoutError extends Error {
|
|
|
251
251
|
}
|
|
252
252
|
/** 在指定的时间 (milliseconds) 内运行某个任务,超时之后
|
|
253
253
|
- 如果传入了 on_timeout 参数: 调用 on_timeout,返回 null
|
|
254
|
-
- 如果没传入 on_timeout 参数: 抛出 TimeoutError
|
|
255
|
-
|
|
254
|
+
- 如果没传入 on_timeout 参数: 抛出 TimeoutError
|
|
255
|
+
- print?: 打印已超时任务的错误 */
|
|
256
|
+
export async function timeout(milliseconds, action, on_timeout, print = true) {
|
|
256
257
|
const error = new TimeoutError();
|
|
257
258
|
return new Promise((resolve, reject) => {
|
|
258
259
|
let done = false;
|
|
@@ -283,8 +284,10 @@ export async function timeout(milliseconds, action, on_timeout) {
|
|
|
283
284
|
resolve(await action());
|
|
284
285
|
}
|
|
285
286
|
catch (error) {
|
|
286
|
-
if (rejected)
|
|
287
|
-
|
|
287
|
+
if (rejected) {
|
|
288
|
+
if (print)
|
|
289
|
+
console.log(`已超时任务的错误: ${error.message}`);
|
|
290
|
+
}
|
|
288
291
|
else {
|
|
289
292
|
rejected = true;
|
|
290
293
|
reject(error);
|
package/xlint.js
CHANGED
|
@@ -795,6 +795,8 @@ export const xlint_config = {
|
|
|
795
795
|
// ------------ 括号
|
|
796
796
|
// a => { } 而不是 (a) => { }
|
|
797
797
|
'@stylistic/arrow-parens': ['error', 'as-needed', { requireForBlockBody: false }],
|
|
798
|
+
// arrow function 在 return 单条表达式时去掉后面的大括号, a => 1
|
|
799
|
+
'arrow-body-style': 'error',
|
|
798
800
|
// 不要多余的大括号
|
|
799
801
|
// if (true)
|
|
800
802
|
// console.log()
|