dsh-remote-go 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +64 -95
- package/{client.js → dist/client.js} +7805 -5486
- package/dist/index.js +4 -0
- package/dist/src/admission.js +46 -0
- package/dist/src/api.js +442 -0
- package/dist/src/atomic-json.js +235 -0
- package/dist/src/bootstrap.js +106 -0
- package/dist/src/connect/attach.js +43 -0
- package/dist/src/connect/bootstrap.js +14 -0
- package/dist/src/connect/failures.js +44 -0
- package/dist/src/connect/guards.js +31 -0
- package/dist/src/connect/launch.js +80 -0
- package/dist/src/connect/links.js +70 -0
- package/dist/src/connect/ownership.js +220 -0
- package/dist/src/connect/pipeline.js +473 -0
- package/dist/src/connect/reattach.js +215 -0
- package/dist/src/connect/registry.js +11 -0
- package/dist/src/connect/repair.js +126 -0
- package/dist/src/connect/restart.js +316 -0
- package/dist/src/connect/stop.js +198 -0
- package/dist/src/connect/transport.js +109 -0
- package/dist/src/connect/verify.js +39 -0
- package/dist/src/connect/watchdog.js +76 -0
- package/dist/src/connect-log.js +162 -0
- package/dist/src/connect-progress.js +142 -0
- package/dist/src/constants.js +67 -0
- package/dist/src/context.js +117 -0
- package/dist/src/diagnostics.js +102 -0
- package/dist/src/download-jobs.js +230 -0
- package/dist/src/errors.js +243 -0
- package/dist/src/events.js +390 -0
- package/dist/src/host-lifecycle.js +504 -0
- package/dist/src/host-registry.js +232 -0
- package/dist/src/host-runtime.js +86 -0
- package/dist/src/host-view.js +75 -0
- package/dist/src/index.js +216 -0
- package/dist/src/kernel-adapter.js +434 -0
- package/dist/src/key-files.js +52 -0
- package/dist/src/known-hosts.js +125 -0
- package/dist/src/layout.js +43 -0
- package/dist/src/local-download.js +380 -0
- package/dist/src/local-ssh.js +30 -0
- package/dist/src/local-version.js +101 -0
- package/dist/src/node/download.js +160 -0
- package/dist/src/node-runtime.js +346 -0
- package/dist/src/probe.js +96 -0
- package/dist/src/prompts.js +270 -0
- package/dist/src/proxy/cookies.js +136 -0
- package/dist/src/proxy/headers.js +262 -0
- package/dist/src/proxy/http.js +142 -0
- package/dist/src/proxy/index.js +135 -0
- package/dist/src/proxy/limits.js +8 -0
- package/dist/src/proxy/routes.js +39 -0
- package/dist/src/proxy/upgrade.js +175 -0
- package/dist/src/redact.js +104 -0
- package/dist/src/remote-files.js +72 -0
- package/dist/src/remote-path.js +123 -0
- package/dist/src/remote-proc.js +339 -0
- package/dist/src/remote-web/registry.js +147 -0
- package/dist/src/remote-web/resolve.js +310 -0
- package/dist/src/remote-web/start.js +227 -0
- package/dist/src/remote-web/stop.js +93 -0
- package/dist/src/request-trust.js +87 -0
- package/dist/src/runtime/dispatch.js +310 -0
- package/dist/src/runtime/effects.js +91 -0
- package/dist/src/runtime/events.js +35 -0
- package/dist/src/runtime/fsm.js +120 -0
- package/dist/src/runtime/rules.js +565 -0
- package/dist/src/secrets.js +142 -0
- package/dist/src/service.js +406 -0
- package/dist/src/settings.js +144 -0
- package/dist/src/ssh-config.js +276 -0
- package/dist/src/ssh-failures.js +122 -0
- package/dist/src/ssh-port.js +4 -0
- package/dist/src/ssh-sftp.js +297 -0
- package/dist/src/ssh.js +477 -0
- package/dist/src/store.js +191 -0
- package/dist/src/tool-shared.js +181 -0
- package/dist/src/tools-exec-policy.js +167 -0
- package/dist/src/tools-exec.js +371 -0
- package/dist/src/tools-report.js +424 -0
- package/dist/src/tools.js +411 -0
- package/dist/src/tunnel.js +129 -0
- package/dist/src/util.js +149 -0
- package/dist/src/version.js +114 -0
- package/dist/src/watchdog-bus.js +29 -0
- package/dist/src/watchdog.js +156 -0
- package/index.js +5 -4
- package/locale/en.json +2 -2
- package/locale/zh.json +2 -2
- package/package.json +40 -21
- package/vendor/THIRD_PARTY_NOTICES.md +27 -1
- package/src/api.js +0 -340
- package/src/atomic-json.js +0 -208
- package/src/bootstrap.js +0 -100
- package/src/connect-log.js +0 -39
- package/src/connect-pipeline.js +0 -463
- package/src/connect-progress.js +0 -135
- package/src/constants.js +0 -31
- package/src/diagnostics.js +0 -157
- package/src/download-jobs.js +0 -217
- package/src/errors.js +0 -316
- package/src/host-lifecycle.js +0 -187
- package/src/host-registry.js +0 -145
- package/src/host-runtime.js +0 -53
- package/src/host-view.js +0 -51
- package/src/index.js +0 -162
- package/src/key-files.js +0 -49
- package/src/known-hosts.js +0 -94
- package/src/layout.js +0 -35
- package/src/local-download.js +0 -162
- package/src/local-ssh.js +0 -27
- package/src/local-version.js +0 -88
- package/src/node-runtime.js +0 -521
- package/src/probe.js +0 -89
- package/src/prompts.js +0 -132
- package/src/redact.js +0 -54
- package/src/remote-files.js +0 -71
- package/src/remote-path.js +0 -73
- package/src/remote-proc.js +0 -208
- package/src/remote-web.js +0 -300
- package/src/secrets.js +0 -79
- package/src/service.js +0 -250
- package/src/settings.js +0 -135
- package/src/ssh-config.js +0 -271
- package/src/ssh.js +0 -652
- package/src/store.js +0 -143
- package/src/tunnel.js +0 -73
- package/src/util.js +0 -153
- package/src/version.js +0 -103
- package/src/webapp.js +0 -28
- package/vendor/ssh2-entry.mjs +0 -12
- /package/{vendor → dist/vendor}/ssh2.bundle.cjs +0 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Coded, bilingual errors; `toErrorView` is the only shape the Client ever sees.
|
|
3
|
+
*/
|
|
4
|
+
import { redactObject, redactText } from './redact.js';
|
|
5
|
+
import { headTail } from './util.js';
|
|
6
|
+
const ErrorCode = Object.freeze({
|
|
7
|
+
NETWORK: 'network',
|
|
8
|
+
AUTH: 'auth',
|
|
9
|
+
HOST_KEY_CHANGED: 'host-key-changed',
|
|
10
|
+
HOST_KEY_UNKNOWN: 'host-key-unknown',
|
|
11
|
+
REMOTE_DSH_MISSING: 'remote-dsh-missing',
|
|
12
|
+
REMOTE_NODE_INSTALL_FAILED: 'remote-node-install-failed',
|
|
13
|
+
REMOTE_DSH_START_FAILED: 'remote-dsh-start-failed',
|
|
14
|
+
REMOTE_DSH_RUNNING: 'remote-dsh-running',
|
|
15
|
+
REMOTE_UI_UNREACHABLE: 'remote-ui-unreachable',
|
|
16
|
+
REMOTE_UNREACHABLE: 'remote-unreachable',
|
|
17
|
+
RESTART_UNAVAILABLE: 'restart-unavailable',
|
|
18
|
+
STOP_UNAVAILABLE: 'stop-unavailable',
|
|
19
|
+
REMOTE_FILE_UNAVAILABLE: 'remote-file-unavailable',
|
|
20
|
+
DOWNLOAD_NOT_READY: 'download-not-ready',
|
|
21
|
+
DOWNLOAD_BUSY: 'download-busy',
|
|
22
|
+
VERSION_MISMATCH: 'version-mismatch',
|
|
23
|
+
UNSUPPORTED_OS: 'unsupported-os',
|
|
24
|
+
INVALID_INPUT: 'invalid-input',
|
|
25
|
+
INTERNAL: 'internal',
|
|
26
|
+
});
|
|
27
|
+
const CODE_TABLE = {
|
|
28
|
+
[ErrorCode.NETWORK]: { retryable: true, title: { zh: '连不上这台服务器', en: 'Cannot reach this server' } },
|
|
29
|
+
[ErrorCode.AUTH]: { retryable: true, title: { zh: 'SSH 认证没有通过', en: 'SSH authentication failed' } },
|
|
30
|
+
[ErrorCode.HOST_KEY_CHANGED]: { retryable: false, title: { zh: '主机指纹变了', en: 'The host key changed' } },
|
|
31
|
+
[ErrorCode.HOST_KEY_UNKNOWN]: { retryable: true, title: { zh: '确认服务器指纹', en: 'Confirm the host key' } },
|
|
32
|
+
[ErrorCode.REMOTE_DSH_MISSING]: {
|
|
33
|
+
retryable: true,
|
|
34
|
+
title: { zh: '远端没有可用的 DSH', en: 'No usable DSH on the remote host' },
|
|
35
|
+
},
|
|
36
|
+
[ErrorCode.REMOTE_NODE_INSTALL_FAILED]: {
|
|
37
|
+
retryable: true,
|
|
38
|
+
title: { zh: '远端 Node 自动安装失败', en: 'Automatic Node installation failed' },
|
|
39
|
+
},
|
|
40
|
+
[ErrorCode.REMOTE_DSH_START_FAILED]: {
|
|
41
|
+
retryable: true,
|
|
42
|
+
title: { zh: '远端 DSH 启动失败', en: 'The remote DSH failed to start' },
|
|
43
|
+
},
|
|
44
|
+
[ErrorCode.REMOTE_DSH_RUNNING]: {
|
|
45
|
+
retryable: true,
|
|
46
|
+
title: { zh: '远端已有手工启动的 DSH', en: 'A manually started DSH is already running' },
|
|
47
|
+
},
|
|
48
|
+
[ErrorCode.REMOTE_UI_UNREACHABLE]: {
|
|
49
|
+
retryable: true,
|
|
50
|
+
title: { zh: '远端界面打不开', en: 'The remote interface is unreachable' },
|
|
51
|
+
},
|
|
52
|
+
[ErrorCode.REMOTE_UNREACHABLE]: {
|
|
53
|
+
retryable: true,
|
|
54
|
+
title: { zh: '远端 DSH 已不在运行', en: 'The remote DSH is no longer running' },
|
|
55
|
+
},
|
|
56
|
+
[ErrorCode.RESTART_UNAVAILABLE]: {
|
|
57
|
+
retryable: true,
|
|
58
|
+
title: { zh: '现在不能重启远端 DSH', en: 'The remote DSH cannot be restarted now' },
|
|
59
|
+
},
|
|
60
|
+
[ErrorCode.STOP_UNAVAILABLE]: {
|
|
61
|
+
retryable: true,
|
|
62
|
+
title: { zh: '现在不能关闭远端 DSH', en: 'The remote DSH cannot be stopped now' },
|
|
63
|
+
},
|
|
64
|
+
[ErrorCode.REMOTE_FILE_UNAVAILABLE]: {
|
|
65
|
+
retryable: true,
|
|
66
|
+
title: { zh: '无法读取远端文件', en: 'Cannot read the remote file' },
|
|
67
|
+
},
|
|
68
|
+
[ErrorCode.DOWNLOAD_NOT_READY]: {
|
|
69
|
+
retryable: true,
|
|
70
|
+
title: { zh: '下载尚未准备好', en: 'The download is not ready' },
|
|
71
|
+
},
|
|
72
|
+
[ErrorCode.DOWNLOAD_BUSY]: {
|
|
73
|
+
retryable: true,
|
|
74
|
+
title: { zh: '下载任务太多', en: 'Too many downloads are running' },
|
|
75
|
+
},
|
|
76
|
+
[ErrorCode.VERSION_MISMATCH]: {
|
|
77
|
+
retryable: true,
|
|
78
|
+
title: { zh: '远端 DSH 版本与本机不一致', en: 'Remote DSH version differs from this machine' },
|
|
79
|
+
},
|
|
80
|
+
[ErrorCode.UNSUPPORTED_OS]: {
|
|
81
|
+
retryable: false,
|
|
82
|
+
title: { zh: '远端系统暂不支持', en: 'This remote system is not supported yet' },
|
|
83
|
+
},
|
|
84
|
+
[ErrorCode.INVALID_INPUT]: { retryable: false, title: { zh: '填写的信息有误', en: 'Some fields are invalid' } },
|
|
85
|
+
[ErrorCode.INTERNAL]: { retryable: true, title: { zh: '插件内部错误', en: 'Internal plugin error' } },
|
|
86
|
+
};
|
|
87
|
+
export class RemoteGoError extends Error {
|
|
88
|
+
constructor(code, detail, options = {}) {
|
|
89
|
+
super(`${code}: ${detail.en}`);
|
|
90
|
+
this.name = 'RemoteGoError';
|
|
91
|
+
this.code = code;
|
|
92
|
+
this.detail = detail;
|
|
93
|
+
this.nextSteps = options.nextSteps ?? [];
|
|
94
|
+
this.technical = options.technical;
|
|
95
|
+
this.retryable = options.retryable ?? CODE_TABLE[code]?.retryable ?? true;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const say = (zh, en) => ({ zh, en });
|
|
99
|
+
function fail(code, detail, steps, options = {}) {
|
|
100
|
+
return new RemoteGoError(code, detail, { nextSteps: steps.map(([zh, en]) => say(zh, en)), ...options });
|
|
101
|
+
}
|
|
102
|
+
export const errors = {
|
|
103
|
+
network: (detail = say('网络不可达,可能被防火墙拦截或地址写错。', 'The network is unreachable — check the address, port and firewall.'), options) => fail(ErrorCode.NETWORK, detail, [
|
|
104
|
+
['检查地址、端口与网络连通性', 'Check the address, port and network reachability'],
|
|
105
|
+
['如果有跳板机,先确认跳板机可达', 'If a jump host is configured, verify it first'],
|
|
106
|
+
], options),
|
|
107
|
+
auth: (detail = say('远端拒绝了这次 SSH 认证。', 'The remote host rejected this SSH authentication.'), options) => fail(ErrorCode.AUTH, detail, [
|
|
108
|
+
['检查用户名与密钥 / SSH Agent', 'Check the user name and key / SSH agent'],
|
|
109
|
+
['确认密钥文件路径与权限(0600)', 'Confirm the key file path and its 0600 permissions'],
|
|
110
|
+
], options),
|
|
111
|
+
hostKeyChanged: (pinned, actual) => fail(ErrorCode.HOST_KEY_CHANGED, say(`主机指纹和已记录的不一致:记录 ${pinned},实际 ${actual}。`, `The host key no longer matches the pinned value: pinned ${pinned}, actual ${actual}.`), [
|
|
112
|
+
[
|
|
113
|
+
'确认服务器是否重装或更换了证书;确认后再更新指纹',
|
|
114
|
+
'Confirm the server was not reinstalled or tampered with before updating the pin',
|
|
115
|
+
],
|
|
116
|
+
], { retryable: false, technical: `pinned=${pinned} actual=${actual}` }),
|
|
117
|
+
hostKeyUnknown: (fingerprint) => fail(ErrorCode.HOST_KEY_UNKNOWN, say(`首次连接该服务器(指纹 ${fingerprint}),需要你确认。`, `First connection to this host (fingerprint ${fingerprint}); confirmation required.`), [['核对指纹后选择「接受并记住」或取消', 'Verify the fingerprint, then accept and remember it or cancel']]),
|
|
118
|
+
remoteDshMissing: (detail) => fail(ErrorCode.REMOTE_DSH_MISSING, detail ??
|
|
119
|
+
say('远端没有找到可用的 DSH,本机也无法自动准备运行环境。', 'No usable DSH was found and the runtime could not be prepared automatically.'), [
|
|
120
|
+
['确认远端可以访问网络,或配置离线包', 'Check remote network access, or configure an offline package'],
|
|
121
|
+
['也可以在远端手动安装 DSH 后重试', 'You can also install DSH manually on the remote host and retry'],
|
|
122
|
+
]),
|
|
123
|
+
nodeInstallFailed: (detail, options) => fail(ErrorCode.REMOTE_NODE_INSTALL_FAILED, detail ??
|
|
124
|
+
say('远端没有可用的 Node,自动安装也没有成功。', 'The remote has no usable Node and automatic installation did not succeed.'), [
|
|
125
|
+
[
|
|
126
|
+
'让远端可以访问 nodejs.org 或 npmmirror.com 后重试',
|
|
127
|
+
'Allow the remote to reach nodejs.org or npmmirror.com and retry',
|
|
128
|
+
],
|
|
129
|
+
[
|
|
130
|
+
'确认远端磁盘 ≥ 400 MB,且是受支持的 glibc 架构(musl/Alpine 不支持)',
|
|
131
|
+
'Make sure the remote has >= 400 MB free and a supported glibc architecture (musl/Alpine is not supported)',
|
|
132
|
+
],
|
|
133
|
+
], options),
|
|
134
|
+
remoteDshRunning: (detail, options) => fail(ErrorCode.REMOTE_DSH_RUNNING, detail ??
|
|
135
|
+
say('远端检测到不是本插件启动的 DSH 进程,连接已按你的选择取消。', 'The remote has a DSH process this plugin did not start; the connection was cancelled as you chose.'), [
|
|
136
|
+
[
|
|
137
|
+
'重新连接,并选择「停止它并启动受管实例」或「保留它,另起一个」',
|
|
138
|
+
'Reconnect and choose "Stop it and start the managed instance" or "Keep it and start alongside"',
|
|
139
|
+
],
|
|
140
|
+
], options),
|
|
141
|
+
remoteDshStartFailed: (tail) => fail(ErrorCode.REMOTE_DSH_START_FAILED, say('远端 DSH 进程没有成功打印 Web 地址,可能启动即失败。', 'The remote DSH process did not print its Web URL; startup likely failed.'), [
|
|
142
|
+
['查看诊断里的远端日志尾部', 'Inspect the remote log tail in diagnostics'],
|
|
143
|
+
['确认远端磁盘空间与端口可用', 'Check remote disk space and usable ports'],
|
|
144
|
+
[
|
|
145
|
+
'重试一次,若仍失败可手动在远端验证 `dsh --profile web --no-open`',
|
|
146
|
+
'Retry, or verify `dsh --profile web --no-open` manually on the remote host',
|
|
147
|
+
],
|
|
148
|
+
], { technical: tail }),
|
|
149
|
+
remoteUiUnreachable: (detail) => fail(ErrorCode.REMOTE_UI_UNREACHABLE, detail ??
|
|
150
|
+
say('隧道的本地回环端口无法完成远端界面的鉴权握手。', 'The loopback tunnel could not complete the remote UI authentication handshake.'), [
|
|
151
|
+
['点「重连」重试一次', 'Retry with Reconnect'],
|
|
152
|
+
['查看诊断中的隧道与界面可达性检查', 'Check tunnel and UI reachability in diagnostics'],
|
|
153
|
+
]),
|
|
154
|
+
/**
|
|
155
|
+
* The remote web went away and nothing adoptable came back. Kept apart from
|
|
156
|
+
* `remote-ui-unreachable`: here the tunnel may be perfectly fine and the process
|
|
157
|
+
* is what is missing, which is what the restart button exists for.
|
|
158
|
+
*/
|
|
159
|
+
remoteUnreachable: (detail, options) => fail(ErrorCode.REMOTE_UNREACHABLE, detail ??
|
|
160
|
+
say('远端 DSH 已经停止响应,而且没有找到可以重新挂载的实例;主机配置保留。', 'The remote DSH stopped responding and no instance could be re-attached to; the host configuration is kept.'), [
|
|
161
|
+
[
|
|
162
|
+
'点「重启」用同一份配置重新拉起远端进程',
|
|
163
|
+
'Use "Restart" to start the remote process again with the same settings',
|
|
164
|
+
],
|
|
165
|
+
['点「重连」按存活策略重新连接', 'Use Reconnect to connect again under the keep-alive policy'],
|
|
166
|
+
], options),
|
|
167
|
+
/**
|
|
168
|
+
* One-shot restart requested in a state that cannot support it (not connected, no live
|
|
169
|
+
* SSH, or already busy with another action). The HTTP API answers 409 for this.
|
|
170
|
+
*/
|
|
171
|
+
restartUnavailable: (detail, options) => fail(ErrorCode.RESTART_UNAVAILABLE, detail ??
|
|
172
|
+
say('只有已连接、或远端不可达但仍保留 SSH 连接的主机才能重启远端 DSH。', 'The remote DSH can only be restarted while this host is connected, or after a remote-unreachable failure that kept the SSH session.'), [
|
|
173
|
+
['先连接这台主机,再点「重启」', 'Connect the host first, then use "Restart"'],
|
|
174
|
+
['如果连接已断开,点「重连」重新建立连接', 'If the link dropped, use Reconnect to establish it again'],
|
|
175
|
+
], options),
|
|
176
|
+
/**
|
|
177
|
+
* The explicit "stop remote DSH" action was refused: wrong state, another link action in
|
|
178
|
+
* flight, or the bound process could not be proven to be this connection's own. The HTTP
|
|
179
|
+
* API answers 409 for this.
|
|
180
|
+
*/
|
|
181
|
+
stopRemoteUnavailable: (detail, options) => fail(ErrorCode.STOP_UNAVAILABLE, detail ??
|
|
182
|
+
say('只有已连接、或远端不可达但仍保留 SSH 连接的主机才能关闭远端 DSH,而且只能关闭本连接启动或接管的那一个进程。', 'The remote DSH can only be stopped while this host is connected (or after a remote-unreachable failure that kept the SSH session), and only when it is the process this connection launched or took over.'), [
|
|
183
|
+
['先连接这台主机,再点「关闭」', 'Connect the host first, then use "Stop"'],
|
|
184
|
+
[
|
|
185
|
+
'如果强烈怀疑远端进程不属于本连接,点「重连」重建连接后再试',
|
|
186
|
+
'If the remote process may not belong to this connection, use Reconnect and try again',
|
|
187
|
+
],
|
|
188
|
+
], options),
|
|
189
|
+
remoteFileUnavailable: (detail, options) => fail(ErrorCode.REMOTE_FILE_UNAVAILABLE, detail ??
|
|
190
|
+
say('远端路径不存在、不可读,或无法写入本地保存位置。', 'The remote path does not exist, is not readable, or the local destination cannot be written.'), [
|
|
191
|
+
['确认文件仍在远端,且当前 SSH 用户可以读取', 'Confirm the file still exists and the SSH user can read it'],
|
|
192
|
+
[
|
|
193
|
+
'检查文件大小;过大的文件请先在远端压缩或拆分',
|
|
194
|
+
'Check the file size; compress or split large files on the remote first',
|
|
195
|
+
],
|
|
196
|
+
], options),
|
|
197
|
+
downloadNotReady: (detail, options) => fail(ErrorCode.DOWNLOAD_NOT_READY, detail ??
|
|
198
|
+
say('这个下载还在准备中,或已经失效;请重新发起下载。', 'This download is still being prepared or has expired; start it again.'), [
|
|
199
|
+
['等待进度条完成后再试', 'Wait for the progress bar to finish and retry'],
|
|
200
|
+
['如果任务已失效,从远端文件树重新下载', 'If it expired, start the download again from the remote file tree'],
|
|
201
|
+
], options),
|
|
202
|
+
downloadBusy: (detail, options) => fail(ErrorCode.DOWNLOAD_BUSY, detail ??
|
|
203
|
+
say('同时准备的下载任务过多,请等一个完成后再试。', 'Too many downloads are being prepared at once; wait for one to finish and retry.'), [['取消不需要的下载,或稍后重试', 'Cancel an unneeded download, or retry later']], options),
|
|
204
|
+
versionMismatch: (local, remote) => fail(ErrorCode.VERSION_MISMATCH, say(`本机 DSH 是 ${local},远端是 ${remote};界面可能不一致,默认不静默混用。`, `This machine runs DSH ${local} while the remote runs ${remote}; the UI may differ and mixing silently is not allowed.`), [
|
|
205
|
+
['选择「准备同版本运行时」后重试(推荐)', 'Prepare a matching runtime and retry (recommended)'],
|
|
206
|
+
['明确选择「沿用远端版本」继续', 'Explicitly continue with the remote version'],
|
|
207
|
+
]),
|
|
208
|
+
unsupportedOs: (os) => fail(ErrorCode.UNSUPPORTED_OS, say(`V1 仅支持 Linux 远端,检测到 ${os}。`, `V1 supports Linux remotes only; detected ${os}.`), [['换一台 Linux 服务器,或等待后续版本', 'Use a Linux server, or wait for a later release']], { retryable: false }),
|
|
209
|
+
invalidInput: (field, detail) => fail(ErrorCode.INVALID_INPUT, detail ?? say(`字段「${field}」不合法。`, `The field "${field}" is invalid.`), [['修正后重试', 'Fix it and retry']], { retryable: false }),
|
|
210
|
+
internal: (cause) => fail(ErrorCode.INTERNAL, say('插件内部出现异常,可重试或查看诊断。', 'The plugin hit an internal error; retry or check diagnostics.'), [], { technical: cause instanceof Error ? `${cause.name}: ${cause.message}` : String(cause ?? '') }),
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* How much of one `ErrorView.technical` string the API will carry. The producers hand over
|
|
214
|
+
* captured process output (a remote `web.log` tail, an SFTP failure); the API response is the
|
|
215
|
+
* one place that must never carry the whole of it.
|
|
216
|
+
*/
|
|
217
|
+
export const TECHNICAL_LIMIT = 1200;
|
|
218
|
+
/**
|
|
219
|
+
* The only ErrorView a caller may send: redaction is part of the contract, not an opt-in.
|
|
220
|
+
* `technical` is the risky field — producers put remote process output there, and the first
|
|
221
|
+
* line of a remote `web.log` is the launch URL with its session token. It is redacted and
|
|
222
|
+
* length-capped, then the whole view goes through `redactObject` so a detail/next-step that
|
|
223
|
+
* quotes one of those lines is covered too (docs/api.md: "已脱敏").
|
|
224
|
+
*/
|
|
225
|
+
export function toErrorView(error) {
|
|
226
|
+
if (error instanceof RemoteGoError) {
|
|
227
|
+
const table = CODE_TABLE[error.code] ?? { retryable: true, title: { zh: '未知错误', en: 'Unknown error' } };
|
|
228
|
+
const view = {
|
|
229
|
+
code: error.code,
|
|
230
|
+
title: table.title,
|
|
231
|
+
detail: error.detail,
|
|
232
|
+
nextSteps: error.nextSteps,
|
|
233
|
+
retryable: error.retryable,
|
|
234
|
+
};
|
|
235
|
+
if (error.technical)
|
|
236
|
+
view.technical = headTail(redactText(error.technical), TECHNICAL_LIMIT);
|
|
237
|
+
return redactObject(view);
|
|
238
|
+
}
|
|
239
|
+
return toErrorView(errors.internal(error));
|
|
240
|
+
}
|
|
241
|
+
export function isRemoteGoError(error) {
|
|
242
|
+
return error instanceof RemoteGoError;
|
|
243
|
+
}
|
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* At most this many live state streams are served; a new one evicts the oldest, so a page that
|
|
3
|
+
* leaks EventSources cannot grow this set without bound. The log channel has its own cap
|
|
4
|
+
* (`MAX_SSE_LOG_CLIENTS`) because one page now opens two streams: sharing a counter would let
|
|
5
|
+
* four tabs evict each other's state stream.
|
|
6
|
+
*/
|
|
7
|
+
export const MAX_SSE_CLIENTS = 8;
|
|
8
|
+
/** At most this many live `/events/logs` streams, counted independently of the state channel. */
|
|
9
|
+
export const MAX_SSE_LOG_CLIENTS = 8;
|
|
10
|
+
/** One SSE frame may not exceed this many bytes (a runaway snapshot is dropped, not buffered). */
|
|
11
|
+
const MAX_SSE_FRAME_BYTES = 1 * 1024 * 1024;
|
|
12
|
+
/** A subscriber whose unsent backlog passes this is gone: drop it instead of buffering forever. */
|
|
13
|
+
const MAX_SSE_BUFFERED_BYTES = 1 * 1024 * 1024;
|
|
14
|
+
/** One log frame may not exceed this many bytes; a frame is split before it would cross the cap. */
|
|
15
|
+
export const MAX_LOG_FRAME_BYTES = 64 * 1024;
|
|
16
|
+
/** Rows per log frame: the same bound as the `/state` tail, so one frame can rebuild the panel. */
|
|
17
|
+
export const MAX_LOG_FRAME_ENTRIES = 120;
|
|
18
|
+
/** Coalescing window for log frames (the console can print dozens of lines a second). */
|
|
19
|
+
export const LOG_FLUSH_MS = 150;
|
|
20
|
+
/** Liveness ping on both channels; a buffering middlebox then cannot look like a quiet stream. */
|
|
21
|
+
const HEARTBEAT_MS = 15_000;
|
|
22
|
+
/** `readyState` of an `EventSource`-style client that will not be retried; kept for the Client. */
|
|
23
|
+
const SSE_COLON = ':';
|
|
24
|
+
/**
|
|
25
|
+
* Parse a `Last-Event-ID` header back into a cursor. Anything that is not exactly
|
|
26
|
+
* `instance:hostId:attempt:seq` with two safe integers is "no cursor": the Client then gets a
|
|
27
|
+
* `reset` frame, which is always a correct (if larger) answer.
|
|
28
|
+
* @param value - the raw header value, when the request carried one.
|
|
29
|
+
* @returns the cursor, or null.
|
|
30
|
+
*/
|
|
31
|
+
export function parseEventId(value) {
|
|
32
|
+
if (typeof value !== 'string')
|
|
33
|
+
return null;
|
|
34
|
+
const parts = value.split(SSE_COLON);
|
|
35
|
+
if (parts.length !== 4)
|
|
36
|
+
return null;
|
|
37
|
+
const [instance, hostId, attempt, seq] = parts;
|
|
38
|
+
const parsedAttempt = Number(attempt);
|
|
39
|
+
const parsedSeq = Number(seq);
|
|
40
|
+
if (instance === '' || hostId === '')
|
|
41
|
+
return null;
|
|
42
|
+
if (!Number.isSafeInteger(parsedAttempt) || !Number.isSafeInteger(parsedSeq))
|
|
43
|
+
return null;
|
|
44
|
+
return { instance, hostId, attempt: parsedAttempt, seq: parsedSeq };
|
|
45
|
+
}
|
|
46
|
+
/** The `id:` of one log frame; the inverse of `parseEventId`. */
|
|
47
|
+
export function formatEventId(cursor) {
|
|
48
|
+
return [cursor.instance, cursor.hostId, cursor.attempt, cursor.seq].join(SSE_COLON);
|
|
49
|
+
}
|
|
50
|
+
/** One host's pending rows for one subscriber: what a reset must carry, or only the new tail. */
|
|
51
|
+
function planFor(view, cursor, instance) {
|
|
52
|
+
// Every "cannot continue" case answers with the whole tail: the Client does not need to know
|
|
53
|
+
// what it missed, and a reset of at most one frame is a cheaper contract than a gap protocol.
|
|
54
|
+
if (cursor === null)
|
|
55
|
+
return { entries: view.entries, reset: true, seq: view.seq };
|
|
56
|
+
if (cursor.instance !== instance || cursor.hostId !== view.hostId) {
|
|
57
|
+
return { entries: view.entries, reset: true, seq: view.seq };
|
|
58
|
+
}
|
|
59
|
+
// A new attempt starts its own `seq` from 1, so a cursor from the previous attempt would either
|
|
60
|
+
// drop every new row (seq went backwards) or mix two attempts; reset either way.
|
|
61
|
+
if (cursor.attempt !== view.attempt)
|
|
62
|
+
return { entries: view.entries, reset: true, seq: view.seq };
|
|
63
|
+
// The ring only keeps 200 rows: a cursor older than the oldest retained row has a gap, and a
|
|
64
|
+
// partial replay would hide it.
|
|
65
|
+
if (cursor.seq < view.firstSeq - 1)
|
|
66
|
+
return { entries: view.entries, reset: true, seq: view.seq };
|
|
67
|
+
const entries = view.entries.filter((entry) => typeof entry.seq === 'number' && entry.seq > cursor.seq);
|
|
68
|
+
if (entries.length === 0)
|
|
69
|
+
return null;
|
|
70
|
+
return { entries, reset: false, seq: entries[entries.length - 1].seq };
|
|
71
|
+
}
|
|
72
|
+
export function createEventHub(options) {
|
|
73
|
+
const { service } = options;
|
|
74
|
+
const logger = options.logger ?? console;
|
|
75
|
+
const stateClients = new Set();
|
|
76
|
+
const logClients = new Set();
|
|
77
|
+
const dirtyHosts = new Set();
|
|
78
|
+
let flushTimer = null;
|
|
79
|
+
let disposed = false;
|
|
80
|
+
/** The documented SSE response head; both channels answer with exactly these headers. */
|
|
81
|
+
function openStream(res) {
|
|
82
|
+
res.writeHead(200, {
|
|
83
|
+
'content-type': 'text/event-stream; charset=utf-8',
|
|
84
|
+
'cache-control': 'no-cache, no-transform',
|
|
85
|
+
connection: 'keep-alive',
|
|
86
|
+
'x-accel-buffering': 'no',
|
|
87
|
+
});
|
|
88
|
+
// Flush the head now: the state channel writes its first frame immediately, but the log
|
|
89
|
+
// channel can legitimately stay silent (no host is connecting), and Node would otherwise hold
|
|
90
|
+
// the response head until the first write — leaving the browser's EventSource in CONNECTING.
|
|
91
|
+
res.flushHeaders?.();
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Write one frame, or report that the subscriber is gone (a slow consumer must not grow the
|
|
95
|
+
* process heap: once its queue is too deep it is dropped, and the browser reconnects).
|
|
96
|
+
* @returns false when the caller should remove the subscriber from its set.
|
|
97
|
+
*/
|
|
98
|
+
function writeTo(res, frame) {
|
|
99
|
+
if (res.writableEnded === true || res.destroyed === true)
|
|
100
|
+
return false;
|
|
101
|
+
if (res.writableLength > MAX_SSE_BUFFERED_BYTES) {
|
|
102
|
+
try {
|
|
103
|
+
res.end();
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
// swallow-ok: the response is already gone; the subscriber is dropped either way
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
res.write(frame);
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/** Broadcast one state-channel frame; a frame no browser could render is dropped whole. */
|
|
119
|
+
function broadcastStateChannel(frame) {
|
|
120
|
+
const bytes = Buffer.byteLength(frame);
|
|
121
|
+
if (bytes > MAX_SSE_FRAME_BYTES) {
|
|
122
|
+
logger.warn?.(`[dsh-remote-go] dropped an ${bytes}-byte SSE frame`);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
for (const res of [...stateClients]) {
|
|
126
|
+
if (!writeTo(res, frame))
|
|
127
|
+
stateClients.delete(res);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** Send one log frame to one subscriber, honoring the per-frame byte cap. */
|
|
131
|
+
function sendLog(client, frame) {
|
|
132
|
+
const bytes = Buffer.byteLength(frame);
|
|
133
|
+
if (bytes > MAX_LOG_FRAME_BYTES) {
|
|
134
|
+
// The packer splits by size, so this is a defect rather than a load: log it and skip the
|
|
135
|
+
// frame instead of letting it grow the 1 MiB global gate.
|
|
136
|
+
logger.warn?.(`[dsh-remote-go] dropped an oversized ${bytes}-byte log frame`);
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
if (!writeTo(client.res, frame)) {
|
|
140
|
+
logClients.delete(client);
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
const logEndFrame = (hostId, attempt) => `event: log-end\ndata: ${JSON.stringify({ hostId, attempt })}\n\n`;
|
|
146
|
+
/** One `log` frame; `id:` is what a reconnecting EventSource sends back as `Last-Event-ID`. */
|
|
147
|
+
function logFrame(view, instance, seq, reset, entries) {
|
|
148
|
+
const frame = {
|
|
149
|
+
hostId: view.hostId,
|
|
150
|
+
instance,
|
|
151
|
+
attempt: view.attempt,
|
|
152
|
+
seq,
|
|
153
|
+
...(reset ? { reset: true } : {}),
|
|
154
|
+
visible: view.visible,
|
|
155
|
+
entries,
|
|
156
|
+
};
|
|
157
|
+
const id = formatEventId({ instance, hostId: view.hostId, attempt: view.attempt, seq });
|
|
158
|
+
return `event: log\nid: ${id}\ndata: ${JSON.stringify(frame)}\n\n`;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Split one host's rows into frames that respect both log caps. Rows are already bounded by the
|
|
162
|
+
* ring (each message is redacted and head-tail truncated at write time), so the count cap alone
|
|
163
|
+
* is usually enough; the byte cap keeps a run of long lines from crossing the global gate.
|
|
164
|
+
*/
|
|
165
|
+
function packFrames(view, plan) {
|
|
166
|
+
const instance = service.stateInstance;
|
|
167
|
+
if (plan.entries.length === 0)
|
|
168
|
+
return [logFrame(view, instance, plan.seq, plan.reset, [])];
|
|
169
|
+
const frames = [];
|
|
170
|
+
let index = 0;
|
|
171
|
+
let first = true;
|
|
172
|
+
while (index < plan.entries.length) {
|
|
173
|
+
const chunk = [];
|
|
174
|
+
let bytes = 0;
|
|
175
|
+
while (index < plan.entries.length && chunk.length < MAX_LOG_FRAME_ENTRIES) {
|
|
176
|
+
const entry = plan.entries[index];
|
|
177
|
+
const size = Buffer.byteLength(JSON.stringify(entry)) + 1;
|
|
178
|
+
// Envelope: the fixed field names plus an id whose tail is the last row's `seq`.
|
|
179
|
+
const envelope = 320 + 2 * String(entry.seq).length;
|
|
180
|
+
if (chunk.length > 0 && bytes + size + envelope > MAX_LOG_FRAME_BYTES)
|
|
181
|
+
break;
|
|
182
|
+
chunk.push(entry);
|
|
183
|
+
bytes += size;
|
|
184
|
+
index += 1;
|
|
185
|
+
}
|
|
186
|
+
const last = chunk[chunk.length - 1];
|
|
187
|
+
const seq = last !== undefined ? last.seq : view.seq;
|
|
188
|
+
frames.push(logFrame(view, instance, seq, plan.reset && first, chunk));
|
|
189
|
+
first = false;
|
|
190
|
+
}
|
|
191
|
+
return frames;
|
|
192
|
+
}
|
|
193
|
+
/** Send one host's pending rows to one subscriber, advancing (or dropping) its cursor. */
|
|
194
|
+
function publishTo(client, view) {
|
|
195
|
+
const cursor = client.cursors.get(view.hostId) ?? null;
|
|
196
|
+
const instance = service.stateInstance;
|
|
197
|
+
const plan = planFor(view, cursor, instance);
|
|
198
|
+
if (plan !== null) {
|
|
199
|
+
for (const frame of packFrames(view, plan)) {
|
|
200
|
+
if (!sendLog(client, frame))
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (view.visible) {
|
|
204
|
+
client.cursors.set(view.hostId, { instance, hostId: view.hostId, attempt: view.attempt, seq: plan.seq });
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
// The host left the log-visible set: the frame said `visible: false`, now close this host.
|
|
208
|
+
client.cursors.delete(view.hostId);
|
|
209
|
+
sendLog(client, logEndFrame(view.hostId, view.attempt));
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (!view.visible && cursor !== null) {
|
|
213
|
+
// Nothing new to send, but the host left the set: it still gets one closing frame (the
|
|
214
|
+
// explicit `visible: false` the contract promises) and then its `log-end`.
|
|
215
|
+
sendLog(client, logFrame(view, instance, view.seq, false, []));
|
|
216
|
+
client.cursors.delete(view.hostId);
|
|
217
|
+
sendLog(client, logEndFrame(view.hostId, view.attempt));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/** Send one host's pending rows to every log subscriber (each sliced by its own cursor). */
|
|
221
|
+
function publishHost(hostId) {
|
|
222
|
+
const visible = service.logView(hostId);
|
|
223
|
+
if (visible === null)
|
|
224
|
+
return;
|
|
225
|
+
for (const client of [...logClients])
|
|
226
|
+
publishTo(client, visible);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* A state change can move hosts in and out of the log-visible set, and a host that just started
|
|
230
|
+
* connecting has no cursor anywhere yet: both are discovered here, from the same event that
|
|
231
|
+
* paints the state frame.
|
|
232
|
+
*/
|
|
233
|
+
function reconcileLogs() {
|
|
234
|
+
const visible = new Set(service.logHostIds());
|
|
235
|
+
const ids = new Set(visible);
|
|
236
|
+
for (const client of logClients) {
|
|
237
|
+
for (const hostId of client.cursors.keys())
|
|
238
|
+
ids.add(hostId);
|
|
239
|
+
}
|
|
240
|
+
for (const hostId of ids) {
|
|
241
|
+
const view = service.logView(hostId);
|
|
242
|
+
if (view !== null) {
|
|
243
|
+
for (const client of [...logClients]) {
|
|
244
|
+
if (!visible.has(hostId) && !client.cursors.has(hostId))
|
|
245
|
+
continue;
|
|
246
|
+
publishTo(client, view);
|
|
247
|
+
}
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
// The host was deleted: close every cursor that still names it.
|
|
251
|
+
for (const client of [...logClients]) {
|
|
252
|
+
const cursor = client.cursors.get(hostId);
|
|
253
|
+
if (cursor === undefined)
|
|
254
|
+
continue;
|
|
255
|
+
client.cursors.delete(hostId);
|
|
256
|
+
sendLog(client, logEndFrame(hostId, cursor.attempt));
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/** The state model changed once: one frame carrying no log rows (docs/api.md v2.12). */
|
|
261
|
+
function broadcastState() {
|
|
262
|
+
const frame = `event: state\ndata: ${JSON.stringify(service.snapshot({ logs: false }))}\n\n`;
|
|
263
|
+
broadcastStateChannel(frame);
|
|
264
|
+
reconcileLogs();
|
|
265
|
+
}
|
|
266
|
+
/** Both channels carry the download job and liveness-episode events of the state contract. */
|
|
267
|
+
const broadcastDownload = (job) => broadcastStateChannel(`event: download\ndata: ${JSON.stringify(job)}\n\n`);
|
|
268
|
+
const broadcastRemoteUnreachable = (event) => broadcastStateChannel(`event: remote-unreachable\ndata: ${JSON.stringify(event)}\n\n`);
|
|
269
|
+
function markDirty(hostId) {
|
|
270
|
+
if (disposed)
|
|
271
|
+
return;
|
|
272
|
+
dirtyHosts.add(hostId);
|
|
273
|
+
if (flushTimer !== null)
|
|
274
|
+
return;
|
|
275
|
+
flushTimer = setTimeout(() => {
|
|
276
|
+
flushTimer = null;
|
|
277
|
+
const hosts = [...dirtyHosts];
|
|
278
|
+
dirtyHosts.clear();
|
|
279
|
+
for (const hostId of hosts)
|
|
280
|
+
publishHost(hostId);
|
|
281
|
+
}, LOG_FLUSH_MS);
|
|
282
|
+
// A pending log frame must never keep the process alive on its own.
|
|
283
|
+
flushTimer.unref?.();
|
|
284
|
+
}
|
|
285
|
+
const onState = () => broadcastState();
|
|
286
|
+
const onLogs = (payload) => markDirty(payload.hostId);
|
|
287
|
+
const onDownload = (job) => broadcastDownload(job);
|
|
288
|
+
const onRemoteUnreachable = (event) => broadcastRemoteUnreachable(event);
|
|
289
|
+
service.on('state', onState);
|
|
290
|
+
service.on('logs', onLogs);
|
|
291
|
+
service.on('download', onDownload);
|
|
292
|
+
service.on('remote-unreachable', onRemoteUnreachable);
|
|
293
|
+
const heartbeat = setInterval(() => {
|
|
294
|
+
broadcastStateChannel('event: ping\ndata: {}\n\n');
|
|
295
|
+
for (const client of [...logClients]) {
|
|
296
|
+
if (!writeTo(client.res, 'event: ping\ndata: {}\n\n'))
|
|
297
|
+
logClients.delete(client);
|
|
298
|
+
}
|
|
299
|
+
}, HEARTBEAT_MS);
|
|
300
|
+
heartbeat.unref?.();
|
|
301
|
+
/** Close the oldest stream until one more fits; the page reconnects to a fresh one. */
|
|
302
|
+
function evictOldest(clients, cap, close) {
|
|
303
|
+
while (clients.size >= cap) {
|
|
304
|
+
const oldest = clients.values().next().value;
|
|
305
|
+
if (oldest === undefined)
|
|
306
|
+
break;
|
|
307
|
+
clients.delete(oldest);
|
|
308
|
+
close(oldest);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return {
|
|
312
|
+
handleState(req, res) {
|
|
313
|
+
evictOldest(stateClients, MAX_SSE_CLIENTS, (client) => {
|
|
314
|
+
try {
|
|
315
|
+
client.end();
|
|
316
|
+
}
|
|
317
|
+
catch {
|
|
318
|
+
// swallow-ok: eviction closes a stream the browser already dropped
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
openStream(res);
|
|
322
|
+
// The first frame is the current snapshot, exactly as a fresh `GET /state` would read it;
|
|
323
|
+
// the Client drops it when it already painted that revision.
|
|
324
|
+
res.write(`event: state\ndata: ${JSON.stringify(service.snapshot({ logs: false }))}\n\n`);
|
|
325
|
+
stateClients.add(res);
|
|
326
|
+
req.on('close', () => {
|
|
327
|
+
stateClients.delete(res);
|
|
328
|
+
});
|
|
329
|
+
},
|
|
330
|
+
handleLogs(req, res) {
|
|
331
|
+
evictOldest(logClients, MAX_SSE_LOG_CLIENTS, (client) => {
|
|
332
|
+
try {
|
|
333
|
+
client.res.end();
|
|
334
|
+
}
|
|
335
|
+
catch {
|
|
336
|
+
// swallow-ok: same eviction as the state channel; the stream is already gone
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
openStream(res);
|
|
340
|
+
const client = { res, cursors: new Map() };
|
|
341
|
+
// `Last-Event-ID` can only name one host (an EventSource remembers one `id`): the host it
|
|
342
|
+
// names continues from its cursor, every other host gets a reset frame.
|
|
343
|
+
const resume = parseEventId(req.headers['last-event-id']);
|
|
344
|
+
if (resume !== null)
|
|
345
|
+
client.cursors.set(resume.hostId, resume);
|
|
346
|
+
logClients.add(client);
|
|
347
|
+
for (const hostId of service.logHostIds()) {
|
|
348
|
+
const view = service.logView(hostId);
|
|
349
|
+
if (view !== null)
|
|
350
|
+
publishTo(client, view);
|
|
351
|
+
}
|
|
352
|
+
req.on('close', () => {
|
|
353
|
+
logClients.delete(client);
|
|
354
|
+
});
|
|
355
|
+
},
|
|
356
|
+
dispose() {
|
|
357
|
+
if (disposed)
|
|
358
|
+
return;
|
|
359
|
+
disposed = true;
|
|
360
|
+
service.removeListener('state', onState);
|
|
361
|
+
service.removeListener('logs', onLogs);
|
|
362
|
+
service.removeListener('download', onDownload);
|
|
363
|
+
service.removeListener('remote-unreachable', onRemoteUnreachable);
|
|
364
|
+
clearInterval(heartbeat);
|
|
365
|
+
if (flushTimer !== null) {
|
|
366
|
+
clearTimeout(flushTimer);
|
|
367
|
+
flushTimer = null;
|
|
368
|
+
}
|
|
369
|
+
dirtyHosts.clear();
|
|
370
|
+
for (const res of stateClients) {
|
|
371
|
+
try {
|
|
372
|
+
res.end();
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
// swallow-ok: dispose ends streams the browser may already have closed
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
stateClients.clear();
|
|
379
|
+
for (const client of logClients) {
|
|
380
|
+
try {
|
|
381
|
+
client.res.end();
|
|
382
|
+
}
|
|
383
|
+
catch {
|
|
384
|
+
// swallow-ok: the same dispose sweep on the log channel
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
logClients.clear();
|
|
388
|
+
},
|
|
389
|
+
};
|
|
390
|
+
}
|