@ebowwa/terminal 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/tmux-exec.js DELETED
@@ -1,169 +0,0 @@
1
- "use strict";
2
- /**
3
- * SSH command execution via tmux
4
- * Executes commands through persistent tmux sessions instead of creating new SSH connections
5
- */
6
- var __assign = (this && this.__assign) || function () {
7
- __assign = Object.assign || function(t) {
8
- for (var s, i = 1, n = arguments.length; i < n; i++) {
9
- s = arguments[i];
10
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
11
- t[p] = s[p];
12
- }
13
- return t;
14
- };
15
- return __assign.apply(this, arguments);
16
- };
17
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
- return new (P || (P = Promise))(function (resolve, reject) {
20
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
- step((generator = generator.apply(thisArg, _arguments || [])).next());
24
- });
25
- };
26
- var __generator = (this && this.__generator) || function (thisArg, body) {
27
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
28
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
- function verb(n) { return function (v) { return step([n, v]); }; }
30
- function step(op) {
31
- if (f) throw new TypeError("Generator is already executing.");
32
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
- if (y = 0, t) op = [op[0] & 2, t.value];
35
- switch (op[0]) {
36
- case 0: case 1: t = op; break;
37
- case 4: _.label++; return { value: op[1], done: false };
38
- case 5: _.label++; y = op[1]; op = [0]; continue;
39
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
- default:
41
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
- if (t[2]) _.ops.pop();
46
- _.trys.pop(); continue;
47
- }
48
- op = body.call(thisArg, _);
49
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
- }
52
- };
53
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
54
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
55
- if (ar || !(i in from)) {
56
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
57
- ar[i] = from[i];
58
- }
59
- }
60
- return to.concat(ar || Array.prototype.slice.call(from));
61
- };
62
- Object.defineProperty(exports, "__esModule", { value: true });
63
- exports.execViaTmux = execViaTmux;
64
- exports.execViaTmuxParallel = execViaTmuxParallel;
65
- var pool_js_1 = require("./pool.js");
66
- /**
67
- * Execute a command via tmux session (simplified approach)
68
- *
69
- * DESIGN RATIONALE:
70
- * ================
71
- *
72
- * Current approach: Each API call creates multiple SSH connections
73
- * - /api/environments/:id/resources: 4 connections (9 commands distributed)
74
- * - /api/environments/:id/node-agent: 1 connection
75
- * - Total: 5 SSH connections per page load
76
- *
77
- * New approach: Use existing tmux session for all commands
78
- * - One persistent tmux session per server (already used for terminal WebSocket)
79
- * - Execute commands via tmux send-keys, capture output
80
- * - Total: 1 SSH connection per server (for tmux session management)
81
- *
82
- * Implementation:
83
- * - Use existing tmux session's main window
84
- * - Send command via send-keys
85
- * - Wait for completion
86
- * - Capture output with capture-pane
87
- *
88
- * Note: This is a simplified implementation that reuses the main tmux window.
89
- * For production, we should create dedicated windows per command.
90
- *
91
- * @param command - Shell command to execute
92
- * @param options - SSH connection options
93
- * @param timeout - Command timeout in seconds (default: 10)
94
- * @returns Command stdout output
95
- */
96
- function execViaTmux(command_1, options_1) {
97
- return __awaiter(this, arguments, void 0, function (command, options, timeout) {
98
- var pool;
99
- if (timeout === void 0) { timeout = 10; }
100
- return __generator(this, function (_a) {
101
- switch (_a.label) {
102
- case 0:
103
- pool = (0, pool_js_1.getSSHPool)();
104
- return [4 /*yield*/, pool.exec(command, __assign(__assign({}, options), { timeout: timeout }))];
105
- case 1:
106
- // For now, use the existing SSH pool directly
107
- // The tmux session exists but we'll execute commands directly
108
- // The benefit is still there - we reuse the pooled connection
109
- // TODO: Execute through tmux session for true single-connection behavior
110
- return [2 /*return*/, _a.sent()];
111
- }
112
- });
113
- });
114
- }
115
- /**
116
- * Execute multiple commands in parallel via tmux
117
- *
118
- * Note: This creates multiple temporary windows in parallel,
119
- * each executing one command. This is more efficient than
120
- * sequential execution but uses more tmux windows temporarily.
121
- *
122
- * @param commands - Object mapping names to shell commands
123
- * @param options - SSH connection options
124
- * @param timeout - Per-command timeout in seconds (default: 10)
125
- * @returns Object mapping names to command outputs
126
- */
127
- function execViaTmuxParallel(commands_1, options_1) {
128
- return __awaiter(this, arguments, void 0, function (commands, options, timeout) {
129
- var entries, results, output, _i, results_1, result;
130
- var _this = this;
131
- if (timeout === void 0) { timeout = 10; }
132
- return __generator(this, function (_a) {
133
- switch (_a.label) {
134
- case 0:
135
- entries = Object.entries(commands);
136
- return [4 /*yield*/, Promise.allSettled(entries.map(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
137
- var output_1, error_1;
138
- var key = _b[0], cmd = _b[1];
139
- return __generator(this, function (_c) {
140
- switch (_c.label) {
141
- case 0:
142
- _c.trys.push([0, 2, , 3]);
143
- return [4 /*yield*/, execViaTmux(cmd, options, timeout)];
144
- case 1:
145
- output_1 = _c.sent();
146
- return [2 /*return*/, [key, output_1]];
147
- case 2:
148
- error_1 = _c.sent();
149
- console.error("[execViaTmuxParallel] Command \"".concat(key, "\" failed:"), error_1);
150
- // Return fallback value on failure
151
- return [2 /*return*/, [key, "0"]];
152
- case 3: return [2 /*return*/];
153
- }
154
- });
155
- }); }))];
156
- case 1:
157
- results = _a.sent();
158
- output = [];
159
- for (_i = 0, results_1 = results; _i < results_1.length; _i++) {
160
- result = results_1[_i];
161
- if (result.status === "fulfilled") {
162
- output.push(__spreadArray([], result.value, true));
163
- }
164
- }
165
- return [2 /*return*/, Object.fromEntries(output)];
166
- }
167
- });
168
- });
169
- }