@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,91 @@
|
|
|
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 { EscapeStripperState } from "../enums/escape-stripper-state.js";
|
|
9
|
+
import { Resources } from "../resources.js";
|
|
10
|
+
export class EscapeStripper {
|
|
11
|
+
state = EscapeStripperState.Ground;
|
|
12
|
+
hasCarriageReturn = false;
|
|
13
|
+
strip(data) {
|
|
14
|
+
let text = "";
|
|
15
|
+
let index = 0;
|
|
16
|
+
while (index < data.length) {
|
|
17
|
+
if (this.state === EscapeStripperState.Ground) {
|
|
18
|
+
const end = EscapeStripper.findSpecial(data, index);
|
|
19
|
+
if (end > index) {
|
|
20
|
+
text += (this.hasCarriageReturn ? Resources.lineFeed : "") + data.slice(index, end);
|
|
21
|
+
this.hasCarriageReturn = false;
|
|
22
|
+
index = end;
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const silentEnd = EscapeStripper.findSilentEnd(data, index);
|
|
26
|
+
if (silentEnd > index) {
|
|
27
|
+
this.hasCarriageReturn ||= data.slice(index, silentEnd).includes(Resources.carriageReturn);
|
|
28
|
+
index = silentEnd;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
text += this.read(data.charAt(index), data.charCodeAt(index));
|
|
33
|
+
index++;
|
|
34
|
+
}
|
|
35
|
+
return text;
|
|
36
|
+
}
|
|
37
|
+
read(character, code) {
|
|
38
|
+
switch (this.state) {
|
|
39
|
+
case EscapeStripperState.Ground:
|
|
40
|
+
return this.readControl(code);
|
|
41
|
+
case EscapeStripperState.Escape:
|
|
42
|
+
this.state = EscapeStripper.afterEscape(character, code);
|
|
43
|
+
break;
|
|
44
|
+
case EscapeStripperState.Intermediate:
|
|
45
|
+
if (code < Resources.escapeStripperFirstIntermediate || code > Resources.escapeStripperLastIntermediate)
|
|
46
|
+
this.state = EscapeStripperState.Ground;
|
|
47
|
+
break;
|
|
48
|
+
case EscapeStripperState.Control:
|
|
49
|
+
if (code === Resources.escapeStripperEscape)
|
|
50
|
+
this.state = EscapeStripperState.Escape;
|
|
51
|
+
else if (code >= Resources.escapeStripperFirstControlSequenceFinal && code <= Resources.escapeStripperLastControlSequenceFinal)
|
|
52
|
+
this.state = EscapeStripperState.Ground;
|
|
53
|
+
break;
|
|
54
|
+
case EscapeStripperState.String:
|
|
55
|
+
if (code === Resources.escapeStripperBell)
|
|
56
|
+
this.state = EscapeStripperState.Ground;
|
|
57
|
+
else if (code === Resources.escapeStripperEscape)
|
|
58
|
+
this.state = EscapeStripperState.StringEscape;
|
|
59
|
+
break;
|
|
60
|
+
case EscapeStripperState.StringEscape:
|
|
61
|
+
this.state = character === Resources.escapeStripperStringTerminatorFinal ? EscapeStripperState.Ground : EscapeStripperState.String;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
return "";
|
|
65
|
+
}
|
|
66
|
+
readControl(code) {
|
|
67
|
+
if (code === Resources.escapeStripperEscape) {
|
|
68
|
+
this.state = EscapeStripperState.Escape;
|
|
69
|
+
return "";
|
|
70
|
+
}
|
|
71
|
+
this.hasCarriageReturn = false;
|
|
72
|
+
return Resources.lineFeed;
|
|
73
|
+
}
|
|
74
|
+
static findSpecial(data, start) {
|
|
75
|
+
Resources.escapeStripperSpecial.lastIndex = start;
|
|
76
|
+
return Resources.escapeStripperSpecial.exec(data)?.index ?? data.length;
|
|
77
|
+
}
|
|
78
|
+
static findSilentEnd(data, start) {
|
|
79
|
+
Resources.escapeStripperSilent.lastIndex = start;
|
|
80
|
+
return Resources.escapeStripperSilent.test(data) ? Resources.escapeStripperSilent.lastIndex : start;
|
|
81
|
+
}
|
|
82
|
+
static afterEscape(character, code) {
|
|
83
|
+
if (character === Resources.escapeStripperControlSequenceIntroducer)
|
|
84
|
+
return EscapeStripperState.Control;
|
|
85
|
+
if (character === Resources.escapeStripperOperatingSystemCommandIntroducer || character === Resources.escapeStripperDeviceControlIntroducer
|
|
86
|
+
|| character === Resources.escapeStripperStartOfStringIntroducer || character === Resources.escapeStripperPrivacyMessageIntroducer
|
|
87
|
+
|| character === Resources.escapeStripperApplicationCommandIntroducer)
|
|
88
|
+
return EscapeStripperState.String;
|
|
89
|
+
return code >= Resources.escapeStripperFirstIntermediate && code <= Resources.escapeStripperLastIntermediate ? EscapeStripperState.Intermediate : EscapeStripperState.Ground;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
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 FailureText {
|
|
10
|
+
static read(error) {
|
|
11
|
+
if (!(error instanceof Error))
|
|
12
|
+
return String(error);
|
|
13
|
+
return error.cause === undefined ? error.message : Resources.formatFailureWithCause(error.message, FailureText.read(error.cause));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
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 { appendFile, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
9
|
+
import { Resources } from "../resources.js";
|
|
10
|
+
export class FileSystemWriter {
|
|
11
|
+
createPrivateFolderAsync(prefix) {
|
|
12
|
+
return mkdtemp(prefix);
|
|
13
|
+
}
|
|
14
|
+
async createMissingFolderAsync(path, mode) {
|
|
15
|
+
try {
|
|
16
|
+
await mkdir(path, { mode });
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
if (!(error instanceof Error && "code" in error && error.code === Resources.fileSystemWriterExistingPathCode))
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
writeTextAsync(path, text) {
|
|
24
|
+
return writeFile(path, text, { encoding: Resources.utf8Encoding, mode: Resources.fileSystemWriterPrivateFileMode, flag: Resources.fileSystemWriterCreateOnlyFlag });
|
|
25
|
+
}
|
|
26
|
+
appendTextAsync(path, text) {
|
|
27
|
+
return appendFile(path, text, { encoding: Resources.utf8Encoding, mode: Resources.fileSystemWriterPrivateFileMode });
|
|
28
|
+
}
|
|
29
|
+
removeFileAsync(path) {
|
|
30
|
+
return rm(path, { force: true });
|
|
31
|
+
}
|
|
32
|
+
removeFolderAsync(path) {
|
|
33
|
+
return rm(path, { recursive: true, force: true });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { lstat, readdir, readFile, realpath, stat } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { FolderEntry } from "../models/folder-entry.js";
|
|
4
|
+
import { Resources } from "../resources.js";
|
|
5
|
+
export class FileSystemReader {
|
|
6
|
+
async isFileAsync(path) {
|
|
7
|
+
return (await FileSystemReader.readOrAsync(() => stat(path), null, Resources.unreadablePathCodes))?.isFile() ?? false;
|
|
8
|
+
}
|
|
9
|
+
async isProgramAsync(path) {
|
|
10
|
+
try {
|
|
11
|
+
return (await stat(path)).isFile();
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
if (!FileSystemReader.hasCode(error, Resources.unreadablePathCodes))
|
|
15
|
+
throw error;
|
|
16
|
+
if (!FileSystemReader.hasCode(error, Resources.accessDeniedCodes))
|
|
17
|
+
return false;
|
|
18
|
+
return (await FileSystemReader.readOrAsync(() => lstat(path), null, Resources.unreadablePathCodes))?.isSymbolicLink() === true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async isFolderAsync(path) {
|
|
22
|
+
return (await FileSystemReader.readOrAsync(() => stat(path), null, Resources.unreadablePathCodes))?.isDirectory() ?? false;
|
|
23
|
+
}
|
|
24
|
+
readTextOrNullAsync(path) {
|
|
25
|
+
return FileSystemReader.readOrAsync(() => readFile(path, Resources.utf8Encoding), null, Resources.unreadablePathCodes);
|
|
26
|
+
}
|
|
27
|
+
readBytesOrNullAsync(path) {
|
|
28
|
+
return FileSystemReader.readOrAsync(() => readFile(path), null, Resources.missingPathCodes);
|
|
29
|
+
}
|
|
30
|
+
async readFolderNamesAsync(path) {
|
|
31
|
+
return (await FileSystemReader.readOrAsync(() => readdir(path, { withFileTypes: true }), [], Resources.unreadablePathCodes))
|
|
32
|
+
.filter(t => t.isDirectory()).map(t => t.name);
|
|
33
|
+
}
|
|
34
|
+
async readEntriesAsync(path) {
|
|
35
|
+
const entries = await FileSystemReader.readOrAsync(() => readdir(path, { withFileTypes: true }), [], Resources.unreadablePathCodes);
|
|
36
|
+
return Promise.all(entries.map(async (t) => new FolderEntry(t.name, t.isDirectory() || (t.isSymbolicLink() && await this.isFolderAsync(join(path, t.name))))));
|
|
37
|
+
}
|
|
38
|
+
readRealPathOrNullAsync(path) {
|
|
39
|
+
return FileSystemReader.readOrAsync(() => realpath(path), null, Resources.unreadablePathCodes);
|
|
40
|
+
}
|
|
41
|
+
static async readOrAsync(read, fallback, codes) {
|
|
42
|
+
try {
|
|
43
|
+
return await read();
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (FileSystemReader.hasCode(error, codes))
|
|
47
|
+
return fallback;
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
static hasCode(error, codes) {
|
|
52
|
+
return error instanceof Error && "code" in error && codes.includes(String(error.code));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
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 { PaneProgress } from "../models/pane-progress.js";
|
|
9
|
+
import { Resources } from "../resources.js";
|
|
10
|
+
export class FlowController {
|
|
11
|
+
listener;
|
|
12
|
+
panes = new Map();
|
|
13
|
+
producedOffset = 0;
|
|
14
|
+
mirrorOffset = 0;
|
|
15
|
+
storingBacklog = 0;
|
|
16
|
+
isProducerPaused = false;
|
|
17
|
+
constructor(listener) {
|
|
18
|
+
this.listener = listener;
|
|
19
|
+
}
|
|
20
|
+
get isPaused() {
|
|
21
|
+
return this.isProducerPaused;
|
|
22
|
+
}
|
|
23
|
+
produce(length) {
|
|
24
|
+
this.producedOffset += length;
|
|
25
|
+
this.update();
|
|
26
|
+
}
|
|
27
|
+
acknowledgeMirror(offset, storingBacklog) {
|
|
28
|
+
this.mirrorOffset = Math.max(this.mirrorOffset, offset);
|
|
29
|
+
this.storingBacklog = storingBacklog;
|
|
30
|
+
this.update();
|
|
31
|
+
}
|
|
32
|
+
acknowledgeStoring(storingBacklog) {
|
|
33
|
+
this.storingBacklog = storingBacklog;
|
|
34
|
+
this.update();
|
|
35
|
+
}
|
|
36
|
+
attach(paneId, offset) {
|
|
37
|
+
this.detach(paneId);
|
|
38
|
+
this.panes.set(paneId, new PaneProgress(offset));
|
|
39
|
+
this.update();
|
|
40
|
+
}
|
|
41
|
+
acknowledge(paneId, offset) {
|
|
42
|
+
const pane = this.panes.get(paneId);
|
|
43
|
+
if (pane === undefined)
|
|
44
|
+
return;
|
|
45
|
+
pane.offset = Math.max(pane.offset, Math.min(offset, this.producedOffset));
|
|
46
|
+
this.update();
|
|
47
|
+
}
|
|
48
|
+
setVisible(paneId, isVisible) {
|
|
49
|
+
const pane = this.panes.get(paneId);
|
|
50
|
+
if (pane === undefined)
|
|
51
|
+
return;
|
|
52
|
+
pane.isVisible = isVisible;
|
|
53
|
+
this.update();
|
|
54
|
+
}
|
|
55
|
+
detach(paneId) {
|
|
56
|
+
const pane = this.panes.get(paneId);
|
|
57
|
+
if (pane === undefined)
|
|
58
|
+
return false;
|
|
59
|
+
FlowController.stopStallTimer(pane);
|
|
60
|
+
this.panes.delete(paneId);
|
|
61
|
+
this.update();
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
dispose() {
|
|
65
|
+
for (const pane of this.panes.values())
|
|
66
|
+
FlowController.stopStallTimer(pane);
|
|
67
|
+
this.panes.clear();
|
|
68
|
+
}
|
|
69
|
+
update() {
|
|
70
|
+
let lag = this.producedOffset - this.mirrorOffset + this.storingBacklog;
|
|
71
|
+
for (const pane of this.panes.values())
|
|
72
|
+
lag = Math.max(lag, this.lagOf(pane));
|
|
73
|
+
if (!this.isProducerPaused && lag > Resources.highWatermark) {
|
|
74
|
+
this.isProducerPaused = true;
|
|
75
|
+
this.listener.onPause();
|
|
76
|
+
}
|
|
77
|
+
else if (this.isProducerPaused && lag <= Resources.lowWatermark) {
|
|
78
|
+
this.isProducerPaused = false;
|
|
79
|
+
this.listener.onResume();
|
|
80
|
+
}
|
|
81
|
+
const stallLag = this.isProducerPaused ? Resources.lowWatermark : Resources.highWatermark;
|
|
82
|
+
for (const [paneId, pane] of this.panes) {
|
|
83
|
+
if (this.lagOf(pane) > stallLag)
|
|
84
|
+
pane.stallTimer ??= setTimeout(() => this.stall(paneId), Resources.stallMilliseconds);
|
|
85
|
+
else
|
|
86
|
+
FlowController.stopStallTimer(pane);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
lagOf(pane) {
|
|
90
|
+
return pane.isVisible ? this.producedOffset - pane.offset : 0;
|
|
91
|
+
}
|
|
92
|
+
stall(paneId) {
|
|
93
|
+
this.detach(paneId);
|
|
94
|
+
this.listener.onStalled(paneId);
|
|
95
|
+
}
|
|
96
|
+
static stopStallTimer(pane) {
|
|
97
|
+
if (pane.stallTimer === null)
|
|
98
|
+
return;
|
|
99
|
+
clearTimeout(pane.stallTimer);
|
|
100
|
+
pane.stallTimer = null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -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
|
+
import { MacFolderAccess } from "./mac-folder-access.js";
|
|
10
|
+
import { WindowsFolderAccess } from "./windows-folder-access.js";
|
|
11
|
+
export class FolderAccess {
|
|
12
|
+
static create(platform, runner, environment) {
|
|
13
|
+
if (platform === Resources.windowsPlatform)
|
|
14
|
+
return new WindowsFolderAccess(runner, environment[Resources.systemRootVariable]);
|
|
15
|
+
if (platform === Resources.macPlatform)
|
|
16
|
+
return new MacFolderAccess(runner);
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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 InheritedEnvironment {
|
|
9
|
+
environment;
|
|
10
|
+
constructor(environment) {
|
|
11
|
+
this.environment = environment;
|
|
12
|
+
}
|
|
13
|
+
start() {
|
|
14
|
+
}
|
|
15
|
+
readAsync() {
|
|
16
|
+
return Promise.resolve(this.environment);
|
|
17
|
+
}
|
|
18
|
+
end() {
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
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 { LineProcess } from "./line-process.js";
|
|
9
|
+
export class LineProcessStarter {
|
|
10
|
+
context;
|
|
11
|
+
constructor(context) {
|
|
12
|
+
this.context = context;
|
|
13
|
+
}
|
|
14
|
+
start(file, args, listener) {
|
|
15
|
+
return new LineProcess(this.context, file, args, listener);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
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 { ProcessRequest, ProcessStartException } from "@noldova/desktop.runtime";
|
|
10
|
+
import { Resources } from "../resources.js";
|
|
11
|
+
import { FailureText } from "./failure-text.js";
|
|
12
|
+
export class LineProcess {
|
|
13
|
+
listener;
|
|
14
|
+
log;
|
|
15
|
+
name;
|
|
16
|
+
stopping = new AbortController();
|
|
17
|
+
started;
|
|
18
|
+
output = "";
|
|
19
|
+
constructor(context, file, args, listener) {
|
|
20
|
+
this.name = path.basename(file);
|
|
21
|
+
this.listener = listener;
|
|
22
|
+
this.log = context.log;
|
|
23
|
+
this.started = Promise.resolve()
|
|
24
|
+
.then(() => context.startProcessAsync(new ProcessRequest(file, [...args], path.dirname(file), {}, [], this.stopping.signal)))
|
|
25
|
+
.then(t => this.attach(t), (error) => {
|
|
26
|
+
this.refuse(error);
|
|
27
|
+
return null;
|
|
28
|
+
})
|
|
29
|
+
.catch((error) => {
|
|
30
|
+
this.report(error);
|
|
31
|
+
return null;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
write(line) {
|
|
35
|
+
void this.started.then(t => t?.input.write(`${line}${Resources.lineFeed}`));
|
|
36
|
+
}
|
|
37
|
+
kill() {
|
|
38
|
+
this.stopping.abort();
|
|
39
|
+
}
|
|
40
|
+
attach(process) {
|
|
41
|
+
process.output.setEncoding(Resources.utf8Encoding);
|
|
42
|
+
process.output.on(Resources.dataEvent, (text) => this.receive(text));
|
|
43
|
+
process.errors.resume();
|
|
44
|
+
process.input.on(Resources.lineProcessErrorEvent, (error) => this.listener.onExit(FailureText.read(error)));
|
|
45
|
+
const closed = new Promise(resolve => process.output.once(Resources.closeEvent, () => resolve()));
|
|
46
|
+
void Promise.all([process.exited, closed])
|
|
47
|
+
.then(([exit]) => this.listener.onExit(Resources.formatProcessExited(this.name, exit.code ?? exit.signal)))
|
|
48
|
+
.catch((error) => this.report(error));
|
|
49
|
+
return process;
|
|
50
|
+
}
|
|
51
|
+
refuse(error) {
|
|
52
|
+
if (this.stopping.signal.aborted && error === this.stopping.signal.reason) {
|
|
53
|
+
this.listener.onExit(Resources.lineProcessStopped);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (!(error instanceof ProcessStartException))
|
|
57
|
+
this.report(error);
|
|
58
|
+
this.listener.onExit(FailureText.read(error));
|
|
59
|
+
}
|
|
60
|
+
report(error) {
|
|
61
|
+
this.log.write(Resources.formatLineProcessFailed(this.name, FailureText.read(error)));
|
|
62
|
+
}
|
|
63
|
+
receive(text) {
|
|
64
|
+
const output = `${this.output}${text}`;
|
|
65
|
+
const end = output.lastIndexOf(Resources.lineFeed) + 1;
|
|
66
|
+
this.output = output.slice(end);
|
|
67
|
+
for (const line of output.slice(0, end).split(Resources.lineFeed).slice(0, -1))
|
|
68
|
+
this.listener.onLine(line.replace(Resources.lineProcessTrailingCarriageReturn, ""));
|
|
69
|
+
if (this.output.length > Resources.maximumLineLength)
|
|
70
|
+
this.kill();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
import { FailureText } from "./failure-text.js";
|
|
10
|
+
export class LogFile {
|
|
11
|
+
writer;
|
|
12
|
+
log;
|
|
13
|
+
pending = "";
|
|
14
|
+
length = 0;
|
|
15
|
+
writing = Promise.resolve();
|
|
16
|
+
isWriting = false;
|
|
17
|
+
hasFailed = false;
|
|
18
|
+
path;
|
|
19
|
+
constructor(writer, path, log) {
|
|
20
|
+
this.writer = writer;
|
|
21
|
+
this.path = path;
|
|
22
|
+
this.log = log;
|
|
23
|
+
}
|
|
24
|
+
append(text) {
|
|
25
|
+
if (this.hasFailed || this.length >= Resources.maximumLogFileLength)
|
|
26
|
+
return;
|
|
27
|
+
const kept = text.slice(0, Resources.maximumLogFileLength - this.length);
|
|
28
|
+
this.length += kept.length;
|
|
29
|
+
this.pending += kept;
|
|
30
|
+
if (this.length >= Resources.maximumLogFileLength) {
|
|
31
|
+
this.pending += Resources.formatLogFileEnd(Resources.maximumLogFileLength);
|
|
32
|
+
this.log.write(Resources.formatLogFileFull(this.path, Resources.maximumLogFileLength));
|
|
33
|
+
}
|
|
34
|
+
if (!this.isWriting) {
|
|
35
|
+
this.isWriting = true;
|
|
36
|
+
this.writing = this.writePendingAsync();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
flushAsync() {
|
|
40
|
+
return this.writing;
|
|
41
|
+
}
|
|
42
|
+
async writePendingAsync() {
|
|
43
|
+
while (this.pending.length > 0 && !this.hasFailed) {
|
|
44
|
+
const text = this.pending;
|
|
45
|
+
this.pending = "";
|
|
46
|
+
try {
|
|
47
|
+
await this.writer.appendTextAsync(this.path, text);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
this.hasFailed = true;
|
|
51
|
+
this.log.write(Resources.formatLogFailed(this.path, FailureText.read(error)));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
this.pending = "";
|
|
55
|
+
this.isWriting = false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
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 { posix } from "node:path";
|
|
9
|
+
import { Resources } from "../resources.js";
|
|
10
|
+
import { FileSystemReader } from "./file-system.reader.js";
|
|
11
|
+
export class MacEnvironment {
|
|
12
|
+
environment;
|
|
13
|
+
starter;
|
|
14
|
+
fallbackLocale;
|
|
15
|
+
hasLocaleAsync;
|
|
16
|
+
reading = null;
|
|
17
|
+
process = null;
|
|
18
|
+
answer = null;
|
|
19
|
+
timer;
|
|
20
|
+
constructor(environment, starter, fallbackLocale, hasLocaleAsync) {
|
|
21
|
+
this.environment = environment;
|
|
22
|
+
this.starter = starter;
|
|
23
|
+
this.fallbackLocale = fallbackLocale;
|
|
24
|
+
this.hasLocaleAsync = hasLocaleAsync;
|
|
25
|
+
}
|
|
26
|
+
static hasSystemLocaleAsync(name) {
|
|
27
|
+
return new FileSystemReader().isFileAsync(posix.join(Resources.localeFolder, name, Resources.characterTypeVariable));
|
|
28
|
+
}
|
|
29
|
+
start() {
|
|
30
|
+
void this.readAsync().catch(() => undefined);
|
|
31
|
+
}
|
|
32
|
+
readAsync() {
|
|
33
|
+
this.reading ??= Resources.localeVariables.some(t => (this.environment[t] ?? "") !== "")
|
|
34
|
+
? Promise.resolve(this.environment)
|
|
35
|
+
: this.readLocaleAsync().then(t => this.withUtf8LocaleAsync(t)).catch((error) => {
|
|
36
|
+
this.reading = null;
|
|
37
|
+
throw error;
|
|
38
|
+
});
|
|
39
|
+
return this.reading;
|
|
40
|
+
}
|
|
41
|
+
end() {
|
|
42
|
+
this.stop();
|
|
43
|
+
}
|
|
44
|
+
readLocaleAsync() {
|
|
45
|
+
return new Promise(resolve => {
|
|
46
|
+
this.answer = resolve;
|
|
47
|
+
this.timer = setTimeout(() => this.stop(), Resources.appleLocaleReadMilliseconds);
|
|
48
|
+
this.process = this.starter.start(Resources.defaultsFile, Resources.appleLocaleArguments, {
|
|
49
|
+
onLine: line => this.finish(MacEnvironment.toLocale(line) ?? this.fallbackLocale),
|
|
50
|
+
onExit: () => this.finish(this.fallbackLocale)
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
stop() {
|
|
55
|
+
this.process?.kill();
|
|
56
|
+
this.finish(this.fallbackLocale);
|
|
57
|
+
}
|
|
58
|
+
finish(locale) {
|
|
59
|
+
clearTimeout(this.timer);
|
|
60
|
+
this.process = null;
|
|
61
|
+
this.answer?.(locale);
|
|
62
|
+
this.answer = null;
|
|
63
|
+
}
|
|
64
|
+
async withUtf8LocaleAsync(locale) {
|
|
65
|
+
const maximized = new Intl.Locale(locale).maximize();
|
|
66
|
+
const name = maximized.region === undefined ? null : Resources.formatUtf8Locale(maximized.language, maximized.region);
|
|
67
|
+
return name !== null && await this.hasLocaleAsync(name)
|
|
68
|
+
? { ...this.environment, [Resources.languageVariable]: name }
|
|
69
|
+
: { ...this.environment, [Resources.characterTypeVariable]: Resources.utf8Locale };
|
|
70
|
+
}
|
|
71
|
+
static toLocale(line) {
|
|
72
|
+
try {
|
|
73
|
+
return new Intl.Locale(line.replace(Resources.macEnvironmentLocaleModifier, "").replaceAll(Resources.macEnvironmentLocaleSeparator, Resources.macEnvironmentLanguageTagSeparator)).toString();
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
if (error instanceof RangeError)
|
|
77
|
+
return null;
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
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 MacFolderAccess {
|
|
10
|
+
runner;
|
|
11
|
+
constructor(runner) {
|
|
12
|
+
this.runner = runner;
|
|
13
|
+
}
|
|
14
|
+
async restrictAsync(folder) {
|
|
15
|
+
await this.runner.runAsync(Resources.chmodPath, [...Resources.aclRemovalArguments, folder], Resources.folderAccessMilliseconds);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
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 { MarkKind } from "../enums/mark-kind.js";
|
|
9
|
+
import { ShellMark } from "../models/shell-mark.js";
|
|
10
|
+
import { Resources } from "../resources.js";
|
|
11
|
+
const INTRODUCERS = [Resources.markIntroducer, Resources.eightBitMarkIntroducer];
|
|
12
|
+
const LONGEST_INTRODUCER = Math.max(...INTRODUCERS.map(t => t.length));
|
|
13
|
+
const TERMINATORS = [Resources.bell, Resources.eightBitStringTerminator];
|
|
14
|
+
const CANCELS = [Resources.cancel, Resources.substitute];
|
|
15
|
+
export class MarkScanner {
|
|
16
|
+
nonce;
|
|
17
|
+
carry = "";
|
|
18
|
+
constructor(nonce) {
|
|
19
|
+
this.nonce = nonce;
|
|
20
|
+
}
|
|
21
|
+
scan(data, offset) {
|
|
22
|
+
const text = this.carry + data;
|
|
23
|
+
const textOffset = offset - this.carry.length;
|
|
24
|
+
const marks = [];
|
|
25
|
+
this.carry = "";
|
|
26
|
+
let index = 0;
|
|
27
|
+
while (index < text.length) {
|
|
28
|
+
const [start, introducer] = MarkScanner.findIntroducer(text, index);
|
|
29
|
+
if (start < 0) {
|
|
30
|
+
this.carry = MarkScanner.toIntroducerPrefix(text, index);
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
const payloadStart = start + introducer.length;
|
|
34
|
+
const [payloadEnd, end] = MarkScanner.findTerminator(text, payloadStart);
|
|
35
|
+
if (payloadEnd < 0) {
|
|
36
|
+
this.carry = text.length - start <= Resources.maximumMarkLength ? text.slice(start) : MarkScanner.toIntroducerPrefix(text, payloadStart);
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
if (end < 0) {
|
|
40
|
+
index = payloadEnd;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (end - start > Resources.maximumMarkLength) {
|
|
44
|
+
index = end;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const mark = this.parse(text.slice(payloadStart, payloadEnd), textOffset + end);
|
|
48
|
+
if (mark !== null)
|
|
49
|
+
marks.push(mark);
|
|
50
|
+
index = end;
|
|
51
|
+
}
|
|
52
|
+
return marks;
|
|
53
|
+
}
|
|
54
|
+
parse(payload, offset) {
|
|
55
|
+
const [kindText, nonce, ...values] = payload.replace(Resources.markScannerIgnoredControl, "").split(Resources.markScannerFieldSeparator);
|
|
56
|
+
const kind = Resources.markKindLetters.get(String(kindText));
|
|
57
|
+
if (kind === undefined || nonce !== this.nonce)
|
|
58
|
+
return null;
|
|
59
|
+
const fields = values.map(t => t.replace(Resources.markScannerEscapedCharacter, (_, code) => String.fromCharCode(Number.parseInt(code, Resources.hexadecimalRadix))));
|
|
60
|
+
return new ShellMark(kind, fields.length === 0 ? null : fields.join(Resources.markScannerFieldSeparator), offset, fields);
|
|
61
|
+
}
|
|
62
|
+
static findTerminator(text, from) {
|
|
63
|
+
for (let index = from; index < text.length; index++) {
|
|
64
|
+
const terminator = TERMINATORS.find(t => text.startsWith(t, index));
|
|
65
|
+
if (terminator !== undefined)
|
|
66
|
+
return [index, index + terminator.length];
|
|
67
|
+
if (CANCELS.some(t => text.startsWith(t, index)))
|
|
68
|
+
return [index, -1];
|
|
69
|
+
if (!text.startsWith(Resources.escape, index))
|
|
70
|
+
continue;
|
|
71
|
+
if (index + Resources.escape.length === text.length)
|
|
72
|
+
return [-1, -1];
|
|
73
|
+
return text.startsWith(Resources.stringTerminator, index) ? [index, index + Resources.stringTerminator.length] : [index, -1];
|
|
74
|
+
}
|
|
75
|
+
return [-1, -1];
|
|
76
|
+
}
|
|
77
|
+
static findIntroducer(text, from) {
|
|
78
|
+
let found = [-1, ""];
|
|
79
|
+
for (const introducer of INTRODUCERS) {
|
|
80
|
+
const start = text.indexOf(introducer, from);
|
|
81
|
+
if (start >= 0 && (found[0] < 0 || start < found[0]))
|
|
82
|
+
found = [start, introducer];
|
|
83
|
+
}
|
|
84
|
+
return found;
|
|
85
|
+
}
|
|
86
|
+
static toIntroducerPrefix(text, from) {
|
|
87
|
+
for (let length = Math.min(LONGEST_INTRODUCER - 1, text.length - from); length > 0; length--) {
|
|
88
|
+
const prefix = text.slice(text.length - length);
|
|
89
|
+
if (INTRODUCERS.some(t => t.startsWith(prefix)))
|
|
90
|
+
return prefix;
|
|
91
|
+
}
|
|
92
|
+
return "";
|
|
93
|
+
}
|
|
94
|
+
}
|