@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,83 @@
|
|
|
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 TerminalAudience {
|
|
9
|
+
windows = new Map();
|
|
10
|
+
apps = new Set();
|
|
11
|
+
shownOwners = new Map();
|
|
12
|
+
hiddenWindows = new Map();
|
|
13
|
+
see(client) {
|
|
14
|
+
let windows = this.windows.get(client.connection);
|
|
15
|
+
if (windows === undefined) {
|
|
16
|
+
windows = new Set();
|
|
17
|
+
this.windows.set(client.connection, windows);
|
|
18
|
+
if (client.isApp)
|
|
19
|
+
this.apps.add(client.connection);
|
|
20
|
+
}
|
|
21
|
+
if (client.window !== null)
|
|
22
|
+
windows.add(client.window);
|
|
23
|
+
}
|
|
24
|
+
forget(connection, window) {
|
|
25
|
+
if (window !== null) {
|
|
26
|
+
this.windows.get(connection)?.delete(window);
|
|
27
|
+
this.shownOwners.get(connection)?.delete(window);
|
|
28
|
+
this.hiddenWindows.get(connection)?.delete(window);
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
this.windows.delete(connection);
|
|
32
|
+
this.shownOwners.delete(connection);
|
|
33
|
+
this.hiddenWindows.delete(connection);
|
|
34
|
+
return this.apps.delete(connection);
|
|
35
|
+
}
|
|
36
|
+
show(client, owner, isHidden) {
|
|
37
|
+
if (client.window === null)
|
|
38
|
+
return;
|
|
39
|
+
let hidden = this.hiddenWindows.get(client.connection);
|
|
40
|
+
if (hidden === undefined) {
|
|
41
|
+
hidden = new Set();
|
|
42
|
+
this.hiddenWindows.set(client.connection, hidden);
|
|
43
|
+
}
|
|
44
|
+
if (isHidden)
|
|
45
|
+
hidden.add(client.window);
|
|
46
|
+
else
|
|
47
|
+
hidden.delete(client.window);
|
|
48
|
+
let owners = this.shownOwners.get(client.connection);
|
|
49
|
+
if (owners === undefined) {
|
|
50
|
+
owners = new Map();
|
|
51
|
+
this.shownOwners.set(client.connection, owners);
|
|
52
|
+
}
|
|
53
|
+
if (owner === null)
|
|
54
|
+
owners.delete(client.window);
|
|
55
|
+
else
|
|
56
|
+
owners.set(client.window, owner);
|
|
57
|
+
}
|
|
58
|
+
shows(terminal) {
|
|
59
|
+
for (const [connection, owners] of this.shownOwners)
|
|
60
|
+
if (terminal.isReachedBy(connection, this.apps.has(connection)) && [...owners.values()].some(t => t === terminal.owner))
|
|
61
|
+
return true;
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
isOpen(client) {
|
|
65
|
+
return client.window === null ? this.windows.has(client.connection) : this.windows.get(client.connection)?.has(client.window) === true;
|
|
66
|
+
}
|
|
67
|
+
isHidden(client) {
|
|
68
|
+
return client.window !== null && this.hiddenWindows.get(client.connection)?.has(client.window) === true;
|
|
69
|
+
}
|
|
70
|
+
isApp(connection) {
|
|
71
|
+
return this.apps.has(connection);
|
|
72
|
+
}
|
|
73
|
+
publish(channel, terminal, payload) {
|
|
74
|
+
for (const [connection, windows] of this.windows) {
|
|
75
|
+
if (!terminal.isReachedBy(connection, this.apps.has(connection)))
|
|
76
|
+
continue;
|
|
77
|
+
if (windows.size === 0)
|
|
78
|
+
channel.publishTo(connection, null, payload);
|
|
79
|
+
for (const window of windows)
|
|
80
|
+
channel.publishTo(connection, window, payload);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
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 { TerminalHistoryPaths } from "../models/terminal-history-paths.js";
|
|
10
|
+
import { Resources } from "../resources.js";
|
|
11
|
+
import { FailureText } from "./failure-text.js";
|
|
12
|
+
export class TerminalHistoryFolder {
|
|
13
|
+
writer;
|
|
14
|
+
reader;
|
|
15
|
+
signaller;
|
|
16
|
+
temporaryFolder;
|
|
17
|
+
pid;
|
|
18
|
+
log;
|
|
19
|
+
access;
|
|
20
|
+
folder = null;
|
|
21
|
+
constructor(writer, reader, signaller, temporaryFolder, pid, log, access = null) {
|
|
22
|
+
this.writer = writer;
|
|
23
|
+
this.reader = reader;
|
|
24
|
+
this.signaller = signaller;
|
|
25
|
+
this.temporaryFolder = temporaryFolder;
|
|
26
|
+
this.pid = pid;
|
|
27
|
+
this.log = log;
|
|
28
|
+
this.access = access;
|
|
29
|
+
}
|
|
30
|
+
async pathsAsync(sessionId) {
|
|
31
|
+
this.folder ??= this.createAsync();
|
|
32
|
+
let folder;
|
|
33
|
+
try {
|
|
34
|
+
folder = await this.folder;
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
this.folder = null;
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
if (folder === null) {
|
|
41
|
+
this.folder = null;
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
return new TerminalHistoryPaths(path.join(folder, Resources.formatTerminalHistoryFile(sessionId, Resources.terminalHistoryFirstSegment)), path.join(folder, Resources.formatTerminalHistoryFile(sessionId, Resources.terminalHistorySecondSegment)));
|
|
45
|
+
}
|
|
46
|
+
async removeAsync() {
|
|
47
|
+
const creating = this.folder;
|
|
48
|
+
this.folder = null;
|
|
49
|
+
if (creating === null)
|
|
50
|
+
return;
|
|
51
|
+
let folder;
|
|
52
|
+
try {
|
|
53
|
+
folder = await creating;
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
this.log.write(Resources.formatTerminalHistoryCreateFailed(FailureText.read(error)));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (folder !== null)
|
|
60
|
+
await this.removeFolderAsync(folder);
|
|
61
|
+
}
|
|
62
|
+
async createAsync() {
|
|
63
|
+
await this.removeLeftoversAsync();
|
|
64
|
+
const folder = await this.writer.createPrivateFolderAsync(path.join(this.temporaryFolder, Resources.formatTerminalHistoryFolder(this.pid)));
|
|
65
|
+
try {
|
|
66
|
+
await this.access?.restrictAsync(folder);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
this.log.write(Resources.formatTerminalHistoryNotPrivate(FailureText.read(error)));
|
|
70
|
+
await this.removeFolderAsync(folder);
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
return folder;
|
|
74
|
+
}
|
|
75
|
+
async removeLeftoversAsync() {
|
|
76
|
+
for (const name of await this.reader.readFolderNamesAsync(this.temporaryFolder)) {
|
|
77
|
+
const pid = TerminalHistoryFolder.toPid(name);
|
|
78
|
+
if (pid !== null && pid !== this.pid && !this.signaller.isRunning(pid))
|
|
79
|
+
await this.removeFolderAsync(path.join(this.temporaryFolder, name));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async removeFolderAsync(folder) {
|
|
83
|
+
try {
|
|
84
|
+
await this.writer.removeFolderAsync(folder);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
this.log.write(Resources.formatTerminalHistoryRemovalFailed(folder, FailureText.read(error)));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
static toPid(name) {
|
|
91
|
+
if (!name.startsWith(Resources.terminalHistoryFolderPrefix))
|
|
92
|
+
return null;
|
|
93
|
+
const match = Resources.terminalHistoryFolderFolderSuffix.exec(name.slice(Resources.terminalHistoryFolderPrefix.length));
|
|
94
|
+
const pid = Number(match?.[1]);
|
|
95
|
+
return Number.isSafeInteger(pid) && pid > 0 ? pid : null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
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 { open, rm } from "node:fs/promises";
|
|
9
|
+
import { JsonException } from "@noldova/context.json";
|
|
10
|
+
import { TerminalLine } from "@noldova/terminal.protocol";
|
|
11
|
+
import { StoredLinesDamagedException } from "../exceptions/stored-lines-damaged.exception.js";
|
|
12
|
+
import { Resources } from "../resources.js";
|
|
13
|
+
export class TerminalHistorySegment {
|
|
14
|
+
blockOffsets = [];
|
|
15
|
+
handle = null;
|
|
16
|
+
batch = [];
|
|
17
|
+
lines = 0;
|
|
18
|
+
bytes = 0;
|
|
19
|
+
batchBytes = 0;
|
|
20
|
+
written = 0;
|
|
21
|
+
path;
|
|
22
|
+
first;
|
|
23
|
+
constructor(path, first) {
|
|
24
|
+
this.path = path;
|
|
25
|
+
this.first = first;
|
|
26
|
+
}
|
|
27
|
+
get end() {
|
|
28
|
+
return this.first + this.lines;
|
|
29
|
+
}
|
|
30
|
+
get size() {
|
|
31
|
+
return this.bytes;
|
|
32
|
+
}
|
|
33
|
+
get hasBatch() {
|
|
34
|
+
return this.batch.length > 0;
|
|
35
|
+
}
|
|
36
|
+
append(text, bytes) {
|
|
37
|
+
if (this.lines % Resources.terminalHistoryBlockLines === 0)
|
|
38
|
+
this.blockOffsets.push(this.bytes);
|
|
39
|
+
this.lines += 1;
|
|
40
|
+
this.bytes += bytes;
|
|
41
|
+
this.batchBytes += bytes;
|
|
42
|
+
this.batch.push(text);
|
|
43
|
+
}
|
|
44
|
+
discard() {
|
|
45
|
+
const bytes = this.batchBytes;
|
|
46
|
+
this.batch = [];
|
|
47
|
+
this.batchBytes = 0;
|
|
48
|
+
return bytes;
|
|
49
|
+
}
|
|
50
|
+
take() {
|
|
51
|
+
const buffer = Buffer.from(this.batch.join(""), Resources.utf8Encoding);
|
|
52
|
+
this.discard();
|
|
53
|
+
return buffer;
|
|
54
|
+
}
|
|
55
|
+
async writeAsync(buffer) {
|
|
56
|
+
await (await this.fileAsync()).write(buffer, 0, buffer.length, this.written);
|
|
57
|
+
this.written += buffer.length;
|
|
58
|
+
}
|
|
59
|
+
async readAsync(start, count) {
|
|
60
|
+
const lines = [];
|
|
61
|
+
const relative = start - this.first;
|
|
62
|
+
const block = Math.floor(relative / Resources.terminalHistoryBlockLines);
|
|
63
|
+
let skip = relative - block * Resources.terminalHistoryBlockLines;
|
|
64
|
+
let position = this.blockOffsets[block] ?? this.written;
|
|
65
|
+
let pending = Buffer.alloc(0);
|
|
66
|
+
const chunk = Buffer.alloc(Resources.terminalHistoryReadBytes);
|
|
67
|
+
while (lines.length < count) {
|
|
68
|
+
const { bytesRead } = await (await this.fileAsync()).read(chunk, 0, chunk.length, position);
|
|
69
|
+
if (bytesRead === 0)
|
|
70
|
+
throw new StoredLinesDamagedException();
|
|
71
|
+
position += bytesRead;
|
|
72
|
+
let data = Buffer.concat([pending, chunk.subarray(0, bytesRead)]);
|
|
73
|
+
let newline = data.indexOf(Resources.lineFeedByte);
|
|
74
|
+
while (newline >= 0 && lines.length < count) {
|
|
75
|
+
if (skip > 0)
|
|
76
|
+
skip -= 1;
|
|
77
|
+
else
|
|
78
|
+
lines.push(TerminalHistorySegment.parse(data.subarray(0, newline).toString(Resources.utf8Encoding)));
|
|
79
|
+
data = data.subarray(newline + 1);
|
|
80
|
+
newline = data.indexOf(Resources.lineFeedByte);
|
|
81
|
+
}
|
|
82
|
+
pending = data;
|
|
83
|
+
}
|
|
84
|
+
return lines;
|
|
85
|
+
}
|
|
86
|
+
async closeAsync() {
|
|
87
|
+
if (this.handle !== null)
|
|
88
|
+
await this.handle.close();
|
|
89
|
+
this.handle = null;
|
|
90
|
+
}
|
|
91
|
+
async removeAsync() {
|
|
92
|
+
await this.closeAsync();
|
|
93
|
+
await rm(this.path, { force: true });
|
|
94
|
+
}
|
|
95
|
+
async fileAsync() {
|
|
96
|
+
this.handle ??= await open(this.path, Resources.terminalHistoryFlags, Resources.terminalHistoryMode);
|
|
97
|
+
return this.handle;
|
|
98
|
+
}
|
|
99
|
+
static parse(text) {
|
|
100
|
+
try {
|
|
101
|
+
return TerminalLine.fromJson(JSON.parse(text));
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
if (error instanceof SyntaxError || error instanceof JsonException)
|
|
105
|
+
throw new StoredLinesDamagedException(error);
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
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 { TerminalLinePage, TerminalLineRange } from "@noldova/terminal.protocol";
|
|
9
|
+
import { StoredLinesUnwritableException } from "../exceptions/stored-lines-unwritable.exception.js";
|
|
10
|
+
import { Resources } from "../resources.js";
|
|
11
|
+
import { TerminalHistorySegment } from "./terminal-history-segment.js";
|
|
12
|
+
export class TerminalHistory {
|
|
13
|
+
paths;
|
|
14
|
+
segmentLimit;
|
|
15
|
+
onWritten;
|
|
16
|
+
queue = Promise.resolve();
|
|
17
|
+
older = null;
|
|
18
|
+
newer;
|
|
19
|
+
dropped = 0;
|
|
20
|
+
unwritten = 0;
|
|
21
|
+
failure = null;
|
|
22
|
+
isClosed = false;
|
|
23
|
+
constructor(paths, limit, onWritten) {
|
|
24
|
+
this.paths = paths;
|
|
25
|
+
this.segmentLimit = Math.floor(limit / 2);
|
|
26
|
+
this.onWritten = onWritten;
|
|
27
|
+
this.newer = paths === null ? null : new TerminalHistorySegment(paths.first, 0);
|
|
28
|
+
if (paths === null)
|
|
29
|
+
this.fail(null);
|
|
30
|
+
}
|
|
31
|
+
get stored() {
|
|
32
|
+
if (this.newer === null)
|
|
33
|
+
return new TerminalLineRange(0, 0, 0);
|
|
34
|
+
return new TerminalLineRange(this.older?.first ?? this.newer.first, this.newer.end, this.dropped);
|
|
35
|
+
}
|
|
36
|
+
get backlog() {
|
|
37
|
+
return this.unwritten;
|
|
38
|
+
}
|
|
39
|
+
append(line) {
|
|
40
|
+
if (this.paths === null || this.newer === null || this.failure !== null || this.isClosed)
|
|
41
|
+
return;
|
|
42
|
+
const text = `${JSON.stringify(line.toJson())}${Resources.lineFeed}`;
|
|
43
|
+
const bytes = Buffer.byteLength(text, Resources.utf8Encoding);
|
|
44
|
+
const segment = this.newer.size > 0 && this.newer.size + bytes > this.segmentLimit
|
|
45
|
+
? this.rotate(this.paths, this.newer)
|
|
46
|
+
: this.newer;
|
|
47
|
+
if (!segment.hasBatch)
|
|
48
|
+
void this.enqueueAsync(() => this.writeAsync(segment));
|
|
49
|
+
segment.append(text, bytes);
|
|
50
|
+
this.unwritten += bytes;
|
|
51
|
+
}
|
|
52
|
+
clear() {
|
|
53
|
+
if (this.isClosed || this.paths === null || this.newer === null)
|
|
54
|
+
return;
|
|
55
|
+
const end = this.newer.end;
|
|
56
|
+
for (const segment of this.segments)
|
|
57
|
+
this.discard(segment, true);
|
|
58
|
+
this.older = null;
|
|
59
|
+
this.newer = new TerminalHistorySegment(this.paths.first, end);
|
|
60
|
+
this.dropped = 0;
|
|
61
|
+
}
|
|
62
|
+
readAsync(start, limit) {
|
|
63
|
+
return this.enqueueAsync(() => this.readPageAsync(start, limit));
|
|
64
|
+
}
|
|
65
|
+
async closeAsync() {
|
|
66
|
+
this.isClosed = true;
|
|
67
|
+
const removals = this.segments.map(segment => {
|
|
68
|
+
this.unwritten -= segment.discard();
|
|
69
|
+
return this.enqueueAsync(() => segment.removeAsync());
|
|
70
|
+
});
|
|
71
|
+
return (await Promise.allSettled(removals)).filter(t => t.status === "rejected").map(t => t.reason);
|
|
72
|
+
}
|
|
73
|
+
get segments() {
|
|
74
|
+
return [this.older, this.newer].filter(t => t !== null);
|
|
75
|
+
}
|
|
76
|
+
rotate(paths, newer) {
|
|
77
|
+
const path = this.older === null ? paths.second : this.older.path;
|
|
78
|
+
if (this.older !== null) {
|
|
79
|
+
this.dropped += this.older.end - this.older.first;
|
|
80
|
+
this.discard(this.older, false);
|
|
81
|
+
}
|
|
82
|
+
this.older = newer;
|
|
83
|
+
this.newer = new TerminalHistorySegment(path, newer.end);
|
|
84
|
+
return this.newer;
|
|
85
|
+
}
|
|
86
|
+
discard(segment, isRemoved) {
|
|
87
|
+
this.unwritten -= segment.discard();
|
|
88
|
+
void this.enqueueAsync(() => isRemoved ? segment.removeAsync() : segment.closeAsync()).catch((error) => this.fail(error));
|
|
89
|
+
}
|
|
90
|
+
enqueueAsync(operation) {
|
|
91
|
+
const result = this.queue.then(operation);
|
|
92
|
+
this.queue = result.then(() => undefined, () => undefined);
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
writeAsync(segment) {
|
|
96
|
+
return this.storeAsync(segment, segment.take());
|
|
97
|
+
}
|
|
98
|
+
async storeAsync(segment, buffer) {
|
|
99
|
+
if (buffer.length === 0)
|
|
100
|
+
return;
|
|
101
|
+
try {
|
|
102
|
+
await segment.writeAsync(buffer);
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
this.fail(error);
|
|
106
|
+
}
|
|
107
|
+
this.unwritten -= buffer.length;
|
|
108
|
+
this.onWritten();
|
|
109
|
+
}
|
|
110
|
+
fail(error) {
|
|
111
|
+
this.failure = new StoredLinesUnwritableException(error);
|
|
112
|
+
}
|
|
113
|
+
async readPageAsync(start, limit) {
|
|
114
|
+
const stored = this.stored;
|
|
115
|
+
const segments = this.segments;
|
|
116
|
+
const pending = segments.map(t => [t, t.take()]);
|
|
117
|
+
for (const [segment, buffer] of pending)
|
|
118
|
+
await this.storeAsync(segment, buffer);
|
|
119
|
+
if (this.failure !== null)
|
|
120
|
+
throw this.failure;
|
|
121
|
+
const first = Math.min(Math.max(start, stored.start), stored.end);
|
|
122
|
+
const last = Math.min(first + limit, stored.end);
|
|
123
|
+
const lines = [];
|
|
124
|
+
for (const segment of segments) {
|
|
125
|
+
const from = Math.min(Math.max(first, segment.first), segment.end);
|
|
126
|
+
lines.push(...await segment.readAsync(from, Math.max(Math.min(last, segment.end) - from, 0)));
|
|
127
|
+
}
|
|
128
|
+
return new TerminalLinePage(first, lines, stored);
|
|
129
|
+
}
|
|
130
|
+
}
|