@noldova/terminal 0.0.0-stage → 0.0.1
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/LICENSE +21 -0
- package/README.md +10 -2
- package/menus.json +53 -0
- package/module.json +28 -0
- package/package.json +50 -4
- package/settings.json +83 -0
- package/src/api/index.d.ts +131 -0
- package/src/api/index.js +11 -0
- package/src/api/window.d.ts +467 -0
- package/src/api/window.js +8 -0
- package/src/enums/escape-state.js +15 -0
- package/src/enums/escape-stripper-state.js +15 -0
- package/src/enums/mark-kind.js +18 -0
- package/src/enums/shell-family.js +14 -0
- package/src/enums/system-requirement.js +11 -0
- package/src/exceptions/agent-terminal-restart.exception.js +16 -0
- package/src/exceptions/command-exited.exception.js +23 -0
- package/src/exceptions/command-failed.exception.js +13 -0
- package/src/exceptions/command-not-started.exception.js +17 -0
- package/src/exceptions/command-output-too-long.exception.js +18 -0
- package/src/exceptions/command-timed-out.exception.js +18 -0
- package/src/exceptions/command-variable-not-set.exception.js +18 -0
- package/src/exceptions/default-shell-not-found.exception.js +14 -0
- package/src/exceptions/environment-export-invalid.exception.js +14 -0
- package/src/exceptions/environment-key-unreadable.exception.js +17 -0
- package/src/exceptions/environment-unreadable.exception.js +13 -0
- package/src/exceptions/folder-not-found.exception.js +16 -0
- package/src/exceptions/host-ended.exception.js +14 -0
- package/src/exceptions/session-not-found.exception.js +18 -0
- package/src/exceptions/shell-kind-not-found.exception.js +16 -0
- package/src/exceptions/shell-not-found.exception.js +18 -0
- package/src/exceptions/shells-unreadable.exception.js +13 -0
- package/src/exceptions/stored-lines-damaged.exception.js +15 -0
- package/src/exceptions/stored-lines-unwritable.exception.js +15 -0
- package/src/exceptions/system-root-missing.exception.js +14 -0
- package/src/exceptions/terminal.exception.js +13 -0
- package/src/exceptions/unknown-method.exception.js +16 -0
- package/src/exceptions/unsupported-system.exception.js +18 -0
- package/src/exceptions/visual-studio-unreadable.exception.js +15 -0
- package/src/exceptions/wsl-distributions-unreadable.exception.js +15 -0
- package/src/interfaces/i-command-runner.js +8 -0
- package/src/interfaces/i-disposable.js +8 -0
- package/src/interfaces/i-environment-source.js +8 -0
- package/src/interfaces/i-file-system-reader.js +8 -0
- package/src/interfaces/i-file-system-writer.js +8 -0
- package/src/interfaces/i-flow-controller-listener.js +8 -0
- package/src/interfaces/i-folder-access.js +8 -0
- package/src/interfaces/i-line-process-listener.js +8 -0
- package/src/interfaces/i-line-process-starter.js +8 -0
- package/src/interfaces/i-line-process.js +8 -0
- package/src/interfaces/i-output-batcher-listener.js +8 -0
- package/src/interfaces/i-process-signaller.js +8 -0
- package/src/interfaces/i-pseudo-terminal-listener.js +8 -0
- package/src/interfaces/i-pty-exit-event.js +8 -0
- package/src/interfaces/i-pty-process.js +8 -0
- package/src/interfaces/i-pty-spawner.js +8 -0
- package/src/interfaces/i-terminal-mirror-listener.js +8 -0
- package/src/interfaces/i-terminal-session-listener.js +8 -0
- package/src/interfaces/i-waiting-options-request.js +8 -0
- package/src/models/command-listing.js +17 -0
- package/src/models/completion-candidate.js +19 -0
- package/src/models/environment-tools.js +23 -0
- package/src/models/environment-variable.js +15 -0
- package/src/models/folder-entry.js +15 -0
- package/src/models/hosted-terminal.js +116 -0
- package/src/models/mirror-line.js +27 -0
- package/src/models/mirror-snapshot.js +19 -0
- package/src/models/one-shot-launch.js +42 -0
- package/src/models/pane-progress.js +15 -0
- package/src/models/powershell-cmdlet.js +21 -0
- package/src/models/powershell-lookup.js +17 -0
- package/src/models/powershell-parameter.js +19 -0
- package/src/models/registry-value.js +15 -0
- package/src/models/request-client.js +23 -0
- package/src/models/saved-terminal.js +25 -0
- package/src/models/shell-launch.js +23 -0
- package/src/models/shell-line.js +21 -0
- package/src/models/shell-mark.js +19 -0
- package/src/models/shell-options-reply.js +17 -0
- package/src/models/shell.js +29 -0
- package/src/models/started-shell.js +21 -0
- package/src/models/system-shortfall.js +15 -0
- package/src/models/terminal-events.js +37 -0
- package/src/models/terminal-history-paths.js +15 -0
- package/src/models/terminal-prompt.js +28 -0
- package/src/models/tracked-command.js +26 -0
- package/src/models/typed-line.js +40 -0
- package/src/models/visual-studio-installation.js +22 -0
- package/src/models/waiting-run.js +25 -0
- package/src/resources.js +1725 -0
- package/src/runtime-part.js +84 -0
- package/src/services/agent-terminal.js +137 -0
- package/src/services/agent-terminals.js +55 -0
- package/src/services/bounded-reads.js +57 -0
- package/src/services/change-signal.js +33 -0
- package/src/services/command-history.js +20 -0
- package/src/services/command-runner.js +67 -0
- package/src/services/command-tracker.js +161 -0
- package/src/services/completion-source.js +316 -0
- package/src/services/context-pty-process.js +79 -0
- package/src/services/context-pty-spawner.js +26 -0
- package/src/services/deferred-pty-process.js +94 -0
- package/src/services/escape-sequence-tracker.js +126 -0
- package/src/services/escape-stripper.js +91 -0
- package/src/services/failure-text.js +15 -0
- package/src/services/file-system-writer.js +35 -0
- package/src/services/file-system.reader.js +54 -0
- package/src/services/flow-controller.js +102 -0
- package/src/services/folder-access.js +19 -0
- package/src/services/inherited-environment.js +20 -0
- package/src/services/line-process-starter.js +17 -0
- package/src/services/line-process.js +72 -0
- package/src/services/log-file.js +57 -0
- package/src/services/mac-environment.js +81 -0
- package/src/services/mac-folder-access.js +17 -0
- package/src/services/mark-scanner.js +94 -0
- package/src/services/one-shot-command.js +119 -0
- package/src/services/output-batcher.js +62 -0
- package/src/services/output-log.js +50 -0
- package/src/services/powershell-binding.js +61 -0
- package/src/services/process-signaller.js +19 -0
- package/src/services/pseudo-terminal.js +54 -0
- package/src/services/registry-export.js +108 -0
- package/src/services/resize-wakeup.js +39 -0
- package/src/services/saved-terminals.js +52 -0
- package/src/services/shared-reads.js +41 -0
- package/src/services/shell-command.js +90 -0
- package/src/services/shell-environment.js +20 -0
- package/src/services/shell-integration.js +207 -0
- package/src/services/shell-locator.js +177 -0
- package/src/services/shell-options-queue.js +34 -0
- package/src/services/shell-words.js +80 -0
- package/src/services/system-requirements.js +46 -0
- package/src/services/terminal-audience.js +83 -0
- package/src/services/terminal-history-folder.js +97 -0
- package/src/services/terminal-history-segment.js +109 -0
- package/src/services/terminal-history.js +130 -0
- package/src/services/terminal-host.js +931 -0
- package/src/services/terminal-line-reader.js +101 -0
- package/src/services/terminal-mirror.js +221 -0
- package/src/services/terminal-owner.service.js +27 -0
- package/src/services/terminal-session.js +398 -0
- package/src/services/visual-studio.reader.js +76 -0
- package/src/services/windows-environment.js +218 -0
- package/src/services/windows-folder-access.js +23 -0
- package/src/services/wsl-distribution.reader.js +49 -0
- package/src/services/xterm-headless-loader.js +14 -0
- package/src/tools/enums/command-status.js +13 -0
- package/src/tools/enums/run-method.js +11 -0
- package/src/tools/exceptions/abort-reason-unknown.exception.js +17 -0
- package/src/tools/exceptions/stale-terminal.exception.js +16 -0
- package/src/tools/interfaces/i-agent-command.js +8 -0
- package/src/tools/interfaces/i-agent-terminal.js +8 -0
- package/src/tools/interfaces/i-agent-terminals.js +8 -0
- package/src/tools/interfaces/i-output-source.js +8 -0
- package/src/tools/interfaces/i-terminal-prompt.js +8 -0
- package/src/tools/models/agent-terminal-access.js +25 -0
- package/src/tools/models/agent-terminal-opening.js +29 -0
- package/src/tools/models/command-line.js +28 -0
- package/src/tools/models/key-sequence.js +27 -0
- package/src/tools/models/output-chunk.js +18 -0
- package/src/tools/models/output-excerpt.js +31 -0
- package/src/tools/models/permission-ask.js +34 -0
- package/src/tools/models/read-cursor.js +26 -0
- package/src/tools/models/read-cursors.js +25 -0
- package/src/tools/models/terminal-open-input.js +34 -0
- package/src/tools/models/terminal-output-report.js +61 -0
- package/src/tools/models/terminal-read-input.js +39 -0
- package/src/tools/models/terminal-run-input.js +52 -0
- package/src/tools/models/terminal-target-input.js +34 -0
- package/src/tools/models/terminal-write-input.js +38 -0
- package/src/tools/models/tool-call-context.js +61 -0
- package/src/tools/services/abort-reason-reader.js +25 -0
- package/src/tools/services/permission-details.js +42 -0
- package/src/tools/services/permission-gate.js +35 -0
- package/src/tools/services/signal-race.js +24 -0
- package/src/tools/services/terminal-access-policy.js +57 -0
- package/src/tools/services/terminal-close-tool.js +57 -0
- package/src/tools/services/terminal-interrupt-tool.js +57 -0
- package/src/tools/services/terminal-list-tool.js +69 -0
- package/src/tools/services/terminal-open-tool.js +71 -0
- package/src/tools/services/terminal-read-tool.js +58 -0
- package/src/tools/services/terminal-run-tool.js +78 -0
- package/src/tools/services/terminal-text-escaper.js +40 -0
- package/src/tools/services/terminal-tool.js +47 -0
- package/src/tools/services/terminal-tools.js +60 -0
- package/src/tools/services/terminal-write-tool.js +75 -0
- package/src/tools/services/tool-failures.js +28 -0
- package/src/tools/services/tool-results.js +13 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { TerminalShellKind, TerminalSize } from "@noldova/terminal.protocol";
|
|
10
|
+
import { OneShotLaunch } from "../models/one-shot-launch.js";
|
|
11
|
+
import { Resources } from "../resources.js";
|
|
12
|
+
import { CommandStatus } from "../tools/enums/command-status.js";
|
|
13
|
+
import { RunMethod } from "../tools/enums/run-method.js";
|
|
14
|
+
import { ChangeSignal } from "./change-signal.js";
|
|
15
|
+
import { FailureText } from "./failure-text.js";
|
|
16
|
+
import { OutputLog } from "./output-log.js";
|
|
17
|
+
import { PseudoTerminal } from "./pseudo-terminal.js";
|
|
18
|
+
export class OneShotCommand {
|
|
19
|
+
output = new OutputLog();
|
|
20
|
+
changes = new ChangeSignal();
|
|
21
|
+
log;
|
|
22
|
+
terminal = null;
|
|
23
|
+
hasEnded = false;
|
|
24
|
+
isKilled = false;
|
|
25
|
+
code = null;
|
|
26
|
+
timer;
|
|
27
|
+
method = RunMethod.OneShot;
|
|
28
|
+
logFile;
|
|
29
|
+
started;
|
|
30
|
+
constructor(host, shell, folder, command, timeoutMilliseconds, logFile, log) {
|
|
31
|
+
this.logFile = logFile.path;
|
|
32
|
+
this.log = log;
|
|
33
|
+
this.output.subscribe(t => logFile.append(t));
|
|
34
|
+
this.timer = timeoutMilliseconds > 0 ? setTimeout(() => this.interrupt(), timeoutMilliseconds).unref() : undefined;
|
|
35
|
+
this.started = this.startAsync(host, shell, folder, command);
|
|
36
|
+
}
|
|
37
|
+
get status() {
|
|
38
|
+
if (this.hasEnded)
|
|
39
|
+
return this.isKilled ? CommandStatus.Killed : CommandStatus.Exited;
|
|
40
|
+
return this.output.isAwaitingSecret ? CommandStatus.AwaitingInput : CommandStatus.Running;
|
|
41
|
+
}
|
|
42
|
+
get exitCode() {
|
|
43
|
+
return this.code;
|
|
44
|
+
}
|
|
45
|
+
get cwd() {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
get isRunning() {
|
|
49
|
+
return !this.hasEnded;
|
|
50
|
+
}
|
|
51
|
+
readSince(offset) {
|
|
52
|
+
return this.output.readSince(offset);
|
|
53
|
+
}
|
|
54
|
+
waitForOutputAsync(offset, timeoutMilliseconds, signal) {
|
|
55
|
+
return this.changes.waitUntilAsync(() => this.hasEnded || this.output.endOffset > offset, timeoutMilliseconds, signal);
|
|
56
|
+
}
|
|
57
|
+
waitForEndAsync(timeoutMilliseconds, signal) {
|
|
58
|
+
return this.changes.waitUntilAsync(() => this.hasEnded, timeoutMilliseconds, signal);
|
|
59
|
+
}
|
|
60
|
+
write(data) {
|
|
61
|
+
this.terminal?.write(data);
|
|
62
|
+
}
|
|
63
|
+
async endAsync() {
|
|
64
|
+
await this.started;
|
|
65
|
+
if (this.terminal !== null && !this.hasEnded)
|
|
66
|
+
await this.terminal.endAsync();
|
|
67
|
+
}
|
|
68
|
+
interrupt() {
|
|
69
|
+
if (this.hasEnded || this.isKilled)
|
|
70
|
+
return false;
|
|
71
|
+
this.isKilled = true;
|
|
72
|
+
void this.endAsync();
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
static toLaunch(shell, command) {
|
|
76
|
+
if (shell.kind === TerminalShellKind.Wsl)
|
|
77
|
+
return new OneShotLaunch(shell.file, [...shell.args, ...Resources.wslOneShotArguments, command]);
|
|
78
|
+
switch (path.win32.parse(shell.file).name.toLowerCase()) {
|
|
79
|
+
case Resources.cmdExecutable:
|
|
80
|
+
return OneShotCommand.toCmdLaunch(shell, command);
|
|
81
|
+
case Resources.pwshExecutable:
|
|
82
|
+
case Resources.windowsPowerShellExecutable:
|
|
83
|
+
return new OneShotLaunch(shell.file, [...Resources.powerShellOneShotArguments, shell.startCommand === null ? command : `${shell.startCommand}${Resources.lineFeed}${command}`]);
|
|
84
|
+
default:
|
|
85
|
+
return new OneShotLaunch(shell.file, [...Resources.posixOneShotArguments, command]);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async startAsync(host, shell, folder, command) {
|
|
89
|
+
try {
|
|
90
|
+
const launch = OneShotCommand.toLaunch(shell, command);
|
|
91
|
+
const process = await host.spawnAsync(launch.file, launch.args, folder, new TerminalSize(Resources.agentColumns, Resources.agentRows), launch.environment);
|
|
92
|
+
this.terminal = new PseudoTerminal(process, {
|
|
93
|
+
onData: t => {
|
|
94
|
+
this.output.append(t);
|
|
95
|
+
this.changes.notify();
|
|
96
|
+
},
|
|
97
|
+
onExit: t => this.end(t)
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
const message = Resources.formatOneShotFailed(FailureText.read(error));
|
|
102
|
+
this.log.write(message);
|
|
103
|
+
this.output.append(message);
|
|
104
|
+
this.end(null);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
end(exitCode) {
|
|
108
|
+
clearTimeout(this.timer);
|
|
109
|
+
this.code = exitCode;
|
|
110
|
+
this.hasEnded = true;
|
|
111
|
+
this.changes.notify();
|
|
112
|
+
}
|
|
113
|
+
static toCmdLaunch(shell, command) {
|
|
114
|
+
const [first, ...start] = shell.args;
|
|
115
|
+
if (first?.toLowerCase() !== Resources.keepOpenArgument || start.length === 0)
|
|
116
|
+
return new OneShotLaunch(shell.file, Resources.formatCmdOneShotLine(command));
|
|
117
|
+
return new OneShotLaunch(shell.file, Resources.formatCmdStartedOneShotLine(Resources.formatCmdStart(start), shell.file, Resources.oneShotCommandVariable), { [Resources.oneShotCommandVariable]: command });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { TerminalInputMark } from "@noldova/terminal.protocol";
|
|
9
|
+
import { Resources } from "../resources.js";
|
|
10
|
+
export class OutputBatcher {
|
|
11
|
+
listener;
|
|
12
|
+
parts = [];
|
|
13
|
+
length = 0;
|
|
14
|
+
offset = 0;
|
|
15
|
+
timer = null;
|
|
16
|
+
inputs = [];
|
|
17
|
+
constructor(listener) {
|
|
18
|
+
this.listener = listener;
|
|
19
|
+
}
|
|
20
|
+
append(data, inputs = []) {
|
|
21
|
+
const start = this.offset + this.length;
|
|
22
|
+
for (const input of inputs)
|
|
23
|
+
this.inputs.push(new TerminalInputMark(start + input.offset, input.edge));
|
|
24
|
+
if (data.length === 0)
|
|
25
|
+
return;
|
|
26
|
+
this.parts.push(data);
|
|
27
|
+
this.length += data.length;
|
|
28
|
+
if (this.length >= Resources.batchLength)
|
|
29
|
+
this.flush();
|
|
30
|
+
else
|
|
31
|
+
this.timer ??= setTimeout(() => this.flush(), Resources.batchMilliseconds);
|
|
32
|
+
}
|
|
33
|
+
flush() {
|
|
34
|
+
this.cancelTimer();
|
|
35
|
+
const data = this.parts.join("");
|
|
36
|
+
this.parts = [];
|
|
37
|
+
this.length = 0;
|
|
38
|
+
let start = 0;
|
|
39
|
+
while (start < data.length) {
|
|
40
|
+
const end = OutputBatcher.toBatchEnd(data, start);
|
|
41
|
+
const offset = this.offset;
|
|
42
|
+
this.offset += end - start;
|
|
43
|
+
const after = this.inputs.findIndex(t => t.offset > this.offset);
|
|
44
|
+
this.listener.onBatch(offset, data.slice(start, end), this.inputs.splice(0, after < 0 ? this.inputs.length : after));
|
|
45
|
+
start = end;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
dispose() {
|
|
49
|
+
this.cancelTimer();
|
|
50
|
+
}
|
|
51
|
+
cancelTimer() {
|
|
52
|
+
if (this.timer === null)
|
|
53
|
+
return;
|
|
54
|
+
clearTimeout(this.timer);
|
|
55
|
+
this.timer = null;
|
|
56
|
+
}
|
|
57
|
+
static toBatchEnd(data, start) {
|
|
58
|
+
const end = Math.min(start + Resources.batchLength, data.length);
|
|
59
|
+
const last = data.charCodeAt(end - 1);
|
|
60
|
+
return end < data.length && last >= Resources.outputBatcherFirstHighSurrogate && last <= Resources.outputBatcherLastHighSurrogate ? end - 1 : end;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { Registration } from "@noldova/desktop.runtime";
|
|
9
|
+
import { Resources } from "../resources.js";
|
|
10
|
+
import { OutputChunk } from "../tools/models/output-chunk.js";
|
|
11
|
+
import { EscapeStripper } from "./escape-stripper.js";
|
|
12
|
+
export class OutputLog {
|
|
13
|
+
stripper = new EscapeStripper();
|
|
14
|
+
limit;
|
|
15
|
+
listeners = new Set();
|
|
16
|
+
text = "";
|
|
17
|
+
startOffset = 0;
|
|
18
|
+
constructor(limit = Resources.maximumAgentTextLength) {
|
|
19
|
+
this.limit = limit;
|
|
20
|
+
}
|
|
21
|
+
get endOffset() {
|
|
22
|
+
return this.startOffset + this.text.length;
|
|
23
|
+
}
|
|
24
|
+
get isAwaitingSecret() {
|
|
25
|
+
return Resources.secretPromptPattern.test(this.text.slice(this.text.lastIndexOf(Resources.lineFeed) + 1));
|
|
26
|
+
}
|
|
27
|
+
append(data) {
|
|
28
|
+
const text = this.stripper.strip(data);
|
|
29
|
+
if (text.length === 0)
|
|
30
|
+
return;
|
|
31
|
+
this.text += text;
|
|
32
|
+
if (this.text.length > Resources.outputLogTrimFactor * this.limit) {
|
|
33
|
+
const excess = this.text.length - this.limit;
|
|
34
|
+
this.text = this.text.slice(excess);
|
|
35
|
+
this.startOffset += excess;
|
|
36
|
+
}
|
|
37
|
+
for (const listener of this.listeners)
|
|
38
|
+
listener(text);
|
|
39
|
+
}
|
|
40
|
+
readRange(start, end) {
|
|
41
|
+
return this.text.slice(Math.max(start - this.startOffset, 0), Math.max(end - this.startOffset, 0));
|
|
42
|
+
}
|
|
43
|
+
readSince(offset) {
|
|
44
|
+
return new OutputChunk(this.readRange(offset, this.endOffset), this.endOffset);
|
|
45
|
+
}
|
|
46
|
+
subscribe(listener) {
|
|
47
|
+
this.listeners.add(listener);
|
|
48
|
+
return new Registration(() => this.listeners.delete(listener));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { Resources } from "../resources.js";
|
|
9
|
+
export class PowerShellBinding {
|
|
10
|
+
isWindows;
|
|
11
|
+
constructor(isWindows) {
|
|
12
|
+
this.isWindows = isWindows;
|
|
13
|
+
}
|
|
14
|
+
bindsWildcardPath(words) {
|
|
15
|
+
const [command, ...rest] = words.map(t => t.toLowerCase());
|
|
16
|
+
if (command === undefined)
|
|
17
|
+
return false;
|
|
18
|
+
const cmdlet = Resources.powerShellPathCmdlets
|
|
19
|
+
.find(t => t.names.includes(command) || (this.isWindows && t.windowsAliases.includes(command)));
|
|
20
|
+
if (cmdlet === undefined)
|
|
21
|
+
return false;
|
|
22
|
+
const parameters = [...cmdlet.parameters, ...Resources.powerShellCommonParameters];
|
|
23
|
+
const named = [];
|
|
24
|
+
let pending = null;
|
|
25
|
+
let positional = 0;
|
|
26
|
+
let ended = false;
|
|
27
|
+
for (const word of rest) {
|
|
28
|
+
if (pending !== null) {
|
|
29
|
+
named.push(pending.slot);
|
|
30
|
+
pending = null;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (ended || !word.startsWith(Resources.powerShellParameterPrefix)) {
|
|
34
|
+
positional++;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (word === Resources.powerShellEndOfParameters) {
|
|
38
|
+
ended = true;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const separator = word.indexOf(Resources.powerShellParameterValueSeparator);
|
|
42
|
+
const name = word.slice(Resources.powerShellParameterPrefix.length, separator < 0 ? word.length : separator);
|
|
43
|
+
const fixed = this.matches(parameters, name);
|
|
44
|
+
const found = fixed.length > 0 ? fixed : this.matches(cmdlet.dynamicParameters, name);
|
|
45
|
+
const parameter = found.length === 1 ? found.at(0) : undefined;
|
|
46
|
+
if (parameter === undefined)
|
|
47
|
+
return false;
|
|
48
|
+
if (separator >= 0 || parameter.isSwitch)
|
|
49
|
+
named.push(parameter.slot);
|
|
50
|
+
else
|
|
51
|
+
pending = parameter;
|
|
52
|
+
}
|
|
53
|
+
if (pending !== null)
|
|
54
|
+
return pending.name === Resources.powerShellPathParameter;
|
|
55
|
+
return cmdlet.positions.filter(t => !named.includes(t)).at(positional) === Resources.powerShellPathParameter;
|
|
56
|
+
}
|
|
57
|
+
matches(parameters, name) {
|
|
58
|
+
const exact = parameters.find(t => t.name === name || t.aliases.includes(name));
|
|
59
|
+
return exact !== undefined ? [exact] : parameters.filter(t => [t.name, ...t.aliases].some(n => n.startsWith(name)));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { Resources } from "../resources.js";
|
|
9
|
+
export class ProcessSignaller {
|
|
10
|
+
isRunning(pid) {
|
|
11
|
+
try {
|
|
12
|
+
process.kill(pid, 0);
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
return error instanceof Error && "code" in error && error.code === Resources.processAliveCode;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
export class PseudoTerminal {
|
|
9
|
+
process;
|
|
10
|
+
listener;
|
|
11
|
+
hasExited = false;
|
|
12
|
+
isEnding = false;
|
|
13
|
+
constructor(process, listener) {
|
|
14
|
+
this.process = process;
|
|
15
|
+
this.listener = listener;
|
|
16
|
+
this.process.onData(t => this.listener.onData(t));
|
|
17
|
+
this.process.onExit(t => {
|
|
18
|
+
this.hasExited = true;
|
|
19
|
+
if (!this.isEnding)
|
|
20
|
+
void this.endAsync();
|
|
21
|
+
this.listener.onExit(PseudoTerminal.toExitCode(t));
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
get pid() {
|
|
25
|
+
return this.process.pid;
|
|
26
|
+
}
|
|
27
|
+
write(data) {
|
|
28
|
+
if (!this.hasExited)
|
|
29
|
+
this.process.write(data);
|
|
30
|
+
}
|
|
31
|
+
resize(size) {
|
|
32
|
+
if (!this.hasExited)
|
|
33
|
+
this.process.resize(size.columns, size.rows);
|
|
34
|
+
}
|
|
35
|
+
async readForegroundProcessGroupAsync() {
|
|
36
|
+
return this.hasExited ? null : await this.process.readForegroundProcessGroupAsync();
|
|
37
|
+
}
|
|
38
|
+
pause() {
|
|
39
|
+
if (!this.hasExited && !this.isEnding)
|
|
40
|
+
this.process.pause();
|
|
41
|
+
}
|
|
42
|
+
resume() {
|
|
43
|
+
if (!this.hasExited)
|
|
44
|
+
this.process.resume();
|
|
45
|
+
}
|
|
46
|
+
endAsync() {
|
|
47
|
+
this.isEnding = true;
|
|
48
|
+
this.resume();
|
|
49
|
+
return this.process.endAsync();
|
|
50
|
+
}
|
|
51
|
+
static toExitCode(event) {
|
|
52
|
+
return event.signal === undefined || event.signal === 0 ? event.exitCode ?? null : null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { EnvironmentExportInvalidException } from "../exceptions/environment-export-invalid.exception.js";
|
|
9
|
+
import { RegistryValue } from "../models/registry-value.js";
|
|
10
|
+
import { Resources } from "../resources.js";
|
|
11
|
+
export class RegistryExport {
|
|
12
|
+
text;
|
|
13
|
+
index = 0;
|
|
14
|
+
constructor(text) {
|
|
15
|
+
this.text = text;
|
|
16
|
+
}
|
|
17
|
+
static parse(text) {
|
|
18
|
+
return new RegistryExport(text).readValues();
|
|
19
|
+
}
|
|
20
|
+
readValues() {
|
|
21
|
+
const values = new Map();
|
|
22
|
+
let isInKey = false;
|
|
23
|
+
while (this.index < this.text.length) {
|
|
24
|
+
if (this.text[this.index] === Resources.registryExportKeyStart) {
|
|
25
|
+
if (isInKey)
|
|
26
|
+
break;
|
|
27
|
+
isInKey = true;
|
|
28
|
+
}
|
|
29
|
+
if (this.text[this.index] !== Resources.registryExportQuote) {
|
|
30
|
+
this.readData();
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const name = this.readQuoted();
|
|
34
|
+
if (this.text[this.index] !== Resources.registryExportEquals)
|
|
35
|
+
throw new EnvironmentExportInvalidException();
|
|
36
|
+
this.index++;
|
|
37
|
+
if (this.text[this.index] === Resources.registryExportQuote) {
|
|
38
|
+
const value = this.readQuoted();
|
|
39
|
+
this.readData();
|
|
40
|
+
if (name !== "")
|
|
41
|
+
values.set(name, new RegistryValue(value, false));
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const typeEnd = this.text.indexOf(Resources.registryExportTypeEnd, this.index);
|
|
45
|
+
const lineEnd = this.text.indexOf(Resources.lineFeed, this.index);
|
|
46
|
+
if (typeEnd === -1 || (lineEnd !== -1 && typeEnd > lineEnd))
|
|
47
|
+
throw new EnvironmentExportInvalidException();
|
|
48
|
+
const type = this.text.slice(this.index, typeEnd);
|
|
49
|
+
this.index = typeEnd + 1;
|
|
50
|
+
const data = this.readData();
|
|
51
|
+
if (name !== "" && (type === Resources.registryExportExpandableType || type === Resources.registryExportTextType))
|
|
52
|
+
values.set(name, new RegistryValue(RegistryExport.decode(data), type === Resources.registryExportExpandableType));
|
|
53
|
+
}
|
|
54
|
+
return values;
|
|
55
|
+
}
|
|
56
|
+
readQuoted() {
|
|
57
|
+
let value = "";
|
|
58
|
+
this.index++;
|
|
59
|
+
for (;;) {
|
|
60
|
+
const character = this.text[this.index];
|
|
61
|
+
if (character === undefined)
|
|
62
|
+
throw new EnvironmentExportInvalidException();
|
|
63
|
+
if (character === Resources.registryExportQuote) {
|
|
64
|
+
this.index++;
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
if (character === Resources.registryExportEscape) {
|
|
68
|
+
const escaped = this.text[this.index + 1];
|
|
69
|
+
if (escaped === undefined)
|
|
70
|
+
throw new EnvironmentExportInvalidException();
|
|
71
|
+
value += escaped;
|
|
72
|
+
this.index += 2;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
value += character;
|
|
76
|
+
this.index++;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
readData() {
|
|
81
|
+
let data = "";
|
|
82
|
+
let isFirst = true;
|
|
83
|
+
for (;;) {
|
|
84
|
+
const end = this.text.indexOf(Resources.lineFeed, this.index);
|
|
85
|
+
let line = this.text.slice(this.index, end === -1 ? this.text.length : end);
|
|
86
|
+
this.index = end === -1 ? this.text.length : end + 1;
|
|
87
|
+
if (line.endsWith(Resources.carriageReturn))
|
|
88
|
+
line = line.slice(0, -1);
|
|
89
|
+
if (!isFirst)
|
|
90
|
+
line = line.trimStart();
|
|
91
|
+
isFirst = false;
|
|
92
|
+
if (!line.endsWith(Resources.registryExportEscape) || end === -1)
|
|
93
|
+
return data + line;
|
|
94
|
+
data += line.slice(0, -1);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
static decode(data) {
|
|
98
|
+
const bytes = data.trim() === "" ? [] : data.split(Resources.registryExportByteSeparator).map(t => {
|
|
99
|
+
const digits = t.trim();
|
|
100
|
+
if (!Resources.registryExportByteDigits.test(digits))
|
|
101
|
+
throw new EnvironmentExportInvalidException();
|
|
102
|
+
return Number.parseInt(digits, Resources.hexadecimalRadix);
|
|
103
|
+
});
|
|
104
|
+
const text = new TextDecoder(Resources.registryExportUtf16Label).decode(Uint8Array.from(bytes));
|
|
105
|
+
const end = text.indexOf(Resources.registryExportTerminator);
|
|
106
|
+
return end === -1 ? text : text.slice(0, end);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { Resources } from "../resources.js";
|
|
9
|
+
export class ResizeWakeup {
|
|
10
|
+
write;
|
|
11
|
+
output = null;
|
|
12
|
+
isStalled = false;
|
|
13
|
+
constructor(write) {
|
|
14
|
+
this.write = write;
|
|
15
|
+
}
|
|
16
|
+
watch() {
|
|
17
|
+
this.stop();
|
|
18
|
+
this.output = "";
|
|
19
|
+
}
|
|
20
|
+
receive(data) {
|
|
21
|
+
if (this.output === null)
|
|
22
|
+
return;
|
|
23
|
+
this.output += data;
|
|
24
|
+
if (Resources.escapesOnly.test(this.output))
|
|
25
|
+
this.isStalled = Resources.resizeStall.test(this.output);
|
|
26
|
+
else
|
|
27
|
+
this.stop();
|
|
28
|
+
}
|
|
29
|
+
beforeInput() {
|
|
30
|
+
const isStalled = this.isStalled;
|
|
31
|
+
this.stop();
|
|
32
|
+
if (isStalled)
|
|
33
|
+
this.write(Resources.abortKey);
|
|
34
|
+
}
|
|
35
|
+
stop() {
|
|
36
|
+
this.output = null;
|
|
37
|
+
this.isStalled = false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { Migration } from "@noldova/desktop.runtime";
|
|
9
|
+
import { SavedTerminal } from "../models/saved-terminal.js";
|
|
10
|
+
import { Resources } from "../resources.js";
|
|
11
|
+
import { FailureText } from "./failure-text.js";
|
|
12
|
+
export class SavedTerminals {
|
|
13
|
+
static migrations = [
|
|
14
|
+
new Migration(Resources.savedTerminalsCreateTerminalsMigration, [Resources.savedTerminalsCreateTerminalsStatement]),
|
|
15
|
+
new Migration(Resources.savedTerminalsAddOwnerMigration, [Resources.savedTerminalsAddOwnerStatement]),
|
|
16
|
+
new Migration(Resources.savedTerminalsAddOpensTabMigration, [Resources.savedTerminalsAddOpensTabStatement]),
|
|
17
|
+
new Migration(Resources.savedTerminalsAddNameMigration, [Resources.savedTerminalsAddNameStatement])
|
|
18
|
+
];
|
|
19
|
+
database;
|
|
20
|
+
log;
|
|
21
|
+
constructor(database, log) {
|
|
22
|
+
this.database = database;
|
|
23
|
+
this.log = log;
|
|
24
|
+
}
|
|
25
|
+
list() {
|
|
26
|
+
return this.database.readAll(Resources.savedTerminalsListQuery)
|
|
27
|
+
.map(t => new SavedTerminal(String(t[Resources.savedTerminalsSessionIdColumn]), String(t[Resources.savedTerminalsShellIdColumn]), String(t[Resources.savedTerminalsFolderColumn]), Number(t[Resources.savedTerminalsIsPanelTerminalColumn]) === 1, t[Resources.savedTerminalsOwnerColumn] === null ? null : String(t[Resources.savedTerminalsOwnerColumn]), Number(t[Resources.savedTerminalsOpensTabColumn]) === 1, t[Resources.savedTerminalsNameColumn] === null ? null : String(t[Resources.savedTerminalsNameColumn])));
|
|
28
|
+
}
|
|
29
|
+
save(terminal) {
|
|
30
|
+
this.write(t => Resources.formatSavedTerminalUnwritable(terminal.sessionId, t), () => this.database.run(Resources.savedTerminalsSaveStatement, terminal.sessionId, terminal.shellId, terminal.folder, terminal.isPanelTerminal ? 1 : 0, terminal.owner, terminal.opensTab ? 1 : 0));
|
|
31
|
+
}
|
|
32
|
+
moveTo(sessionId, folder) {
|
|
33
|
+
this.write(t => Resources.formatSavedTerminalUnwritable(sessionId, t), () => this.database.run(Resources.savedTerminalsMoveStatement, folder, sessionId));
|
|
34
|
+
}
|
|
35
|
+
rename(sessionId, name) {
|
|
36
|
+
this.write(t => Resources.formatSavedTerminalUnwritable(sessionId, t), () => this.database.run(Resources.savedTerminalsRenameStatement, name, sessionId));
|
|
37
|
+
}
|
|
38
|
+
forget(sessionId) {
|
|
39
|
+
this.write(t => Resources.formatSavedTerminalUnwritable(sessionId, t), () => this.database.run(Resources.savedTerminalsForgetStatement, sessionId));
|
|
40
|
+
}
|
|
41
|
+
forgetOwner(owner) {
|
|
42
|
+
this.write(t => Resources.formatOwnerTerminalsUnwritable(owner, t), () => this.database.run(Resources.savedTerminalsForgetOwnerStatement, owner));
|
|
43
|
+
}
|
|
44
|
+
write(format, action) {
|
|
45
|
+
try {
|
|
46
|
+
action();
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
this.log.write(format(FailureText.read(error)));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
export class SharedReads {
|
|
9
|
+
reader;
|
|
10
|
+
entries = new Map();
|
|
11
|
+
texts = new Map();
|
|
12
|
+
programs = new Map();
|
|
13
|
+
realPaths = new Map();
|
|
14
|
+
constructor(reader) {
|
|
15
|
+
this.reader = reader;
|
|
16
|
+
}
|
|
17
|
+
readEntriesAsync(path) {
|
|
18
|
+
return this.share(this.entries, path, () => this.reader.readEntriesAsync(path));
|
|
19
|
+
}
|
|
20
|
+
readTextOrNullAsync(path) {
|
|
21
|
+
return this.share(this.texts, path, () => this.reader.readTextOrNullAsync(path));
|
|
22
|
+
}
|
|
23
|
+
isProgramAsync(path) {
|
|
24
|
+
return this.share(this.programs, path, () => this.reader.isProgramAsync(path));
|
|
25
|
+
}
|
|
26
|
+
readRealPathOrNullAsync(path) {
|
|
27
|
+
return this.share(this.realPaths, path, () => this.reader.readRealPathOrNullAsync(path).catch((error) => {
|
|
28
|
+
if (error instanceof Error && "code" in error)
|
|
29
|
+
return null;
|
|
30
|
+
throw error;
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
share(running, path, read) {
|
|
34
|
+
const shared = running.get(path);
|
|
35
|
+
if (shared !== undefined)
|
|
36
|
+
return shared;
|
|
37
|
+
const started = read().finally(() => running.delete(path));
|
|
38
|
+
running.set(path, started);
|
|
39
|
+
return started;
|
|
40
|
+
}
|
|
41
|
+
}
|