tacticremote 1.7.0 → 1.8.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 +28 -0
- package/README.md +33 -41
- package/bin/tacticremote.js +41 -101
- package/package.json +30 -42
- package/bin/tactic-remote-server.js +0 -636
- package/lib/authChallengeManager.js +0 -1
- package/lib/bonjourPublisher.js +0 -68
- package/lib/claudeSessionManager.js +0 -1
- package/lib/claudeTransport.js +0 -1
- package/lib/errorCodes.js +0 -1
- package/lib/execSafe.js +0 -1
- package/lib/fileService.js +0 -1
- package/lib/hookInstaller.js +0 -253
- package/lib/outputParser.js +0 -1
- package/lib/pushClient.js +0 -1
- package/lib/rateLimiter.js +0 -1
- package/lib/serverIdentity.js +0 -1
- package/lib/streamJsonParser.js +0 -1
- package/lib/tmuxManager.js +0 -1
- package/lib/uploadService.js +0 -1
- package/lib/validators.js +0 -1
- package/middleware/authMiddleware.js +0 -152
- package/routes/httpHandler.js +0 -111
- package/routes/wsHandler.js +0 -1094
- package/server.js +0 -236
- package/shared/server-core/authChallengeManager.js +0 -108
- package/shared/server-core/claudeSessionManager.js +0 -222
- package/shared/server-core/claudeTransport.js +0 -176
- package/shared/server-core/errorCodes.js +0 -91
- package/shared/server-core/execSafe.js +0 -185
- package/shared/server-core/fileService.js +0 -155
- package/shared/server-core/outputParser.js +0 -174
- package/shared/server-core/protocol-manifest.json +0 -59
- package/shared/server-core/protocol-manifest.schema.json +0 -90
- package/shared/server-core/protocolManifest.js +0 -42
- package/shared/server-core/pushClient.js +0 -198
- package/shared/server-core/rateLimiter.js +0 -98
- package/shared/server-core/serverConnectionLifecycle.js +0 -118
- package/shared/server-core/serverHandshake.js +0 -72
- package/shared/server-core/serverIdentity.js +0 -102
- package/shared/server-core/streamJsonParser.js +0 -321
- package/shared/server-core/tmuxManager.js +0 -596
- package/shared/server-core/unixProcessLifecycle.js +0 -96
- package/shared/server-core/unixServerHooks.js +0 -148
- package/shared/server-core/unixWsRuntime.js +0 -412
- package/shared/server-core/uploadService.js +0 -321
- package/shared/server-core/validators.js +0 -274
- package/shared/server-core/windowsServerHooks.js +0 -174
- package/shared/server-core/windowsWsRuntime.js +0 -413
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ClaudeTransport — manages a single `claude` child process in stream-json mode.
|
|
3
|
-
*
|
|
4
|
-
* Spawns `claude --print --output-format stream-json --input-format stream-json`
|
|
5
|
-
* and communicates via stdin/stdout NDJSON (newline-delimited JSON).
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
const { spawn } = require('child_process');
|
|
9
|
-
const { EventEmitter } = require('events');
|
|
10
|
-
|
|
11
|
-
const STDERR_MAX_BYTES = 4096;
|
|
12
|
-
const SHUTDOWN_SIGTERM_TIMEOUT = 5000;
|
|
13
|
-
const SHUTDOWN_SIGKILL_TIMEOUT = 10000;
|
|
14
|
-
|
|
15
|
-
class ClaudeTransport extends EventEmitter {
|
|
16
|
-
constructor({ cwd, sessionId, additionalArgs = [] }) {
|
|
17
|
-
super();
|
|
18
|
-
this.cwd = cwd;
|
|
19
|
-
this.sessionId = sessionId;
|
|
20
|
-
this.additionalArgs = additionalArgs;
|
|
21
|
-
|
|
22
|
-
this.proc = null;
|
|
23
|
-
this._lineBuf = '';
|
|
24
|
-
this._stderrBuf = '';
|
|
25
|
-
this._didRequestShutdown = false;
|
|
26
|
-
this._didReportError = false;
|
|
27
|
-
this._shutdownTimers = [];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
start() {
|
|
31
|
-
const args = [
|
|
32
|
-
'--print',
|
|
33
|
-
'--output-format', 'stream-json',
|
|
34
|
-
'--input-format', 'stream-json',
|
|
35
|
-
'--verbose',
|
|
36
|
-
'--include-partial-messages',
|
|
37
|
-
'--include-hook-events',
|
|
38
|
-
'--session-id', this.sessionId,
|
|
39
|
-
...this.additionalArgs
|
|
40
|
-
];
|
|
41
|
-
|
|
42
|
-
this.proc = spawn('claude', args, {
|
|
43
|
-
cwd: this.cwd,
|
|
44
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
45
|
-
shell: false,
|
|
46
|
-
env: { ...process.env, TACTIC_SESSION_ID: this.sessionId }
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
this.proc.stdout.on('data', (chunk) => {
|
|
50
|
-
this._lineBuf += chunk.toString('utf8');
|
|
51
|
-
let nl;
|
|
52
|
-
while ((nl = this._lineBuf.indexOf('\n')) !== -1) {
|
|
53
|
-
const line = this._lineBuf.slice(0, nl);
|
|
54
|
-
this._lineBuf = this._lineBuf.slice(nl + 1);
|
|
55
|
-
if (line.trim()) {
|
|
56
|
-
this._handleLine(line.trim());
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
this.proc.stderr.on('data', (chunk) => {
|
|
62
|
-
this._stderrBuf += chunk.toString('utf8');
|
|
63
|
-
if (this._stderrBuf.length > STDERR_MAX_BYTES) {
|
|
64
|
-
this._stderrBuf = this._stderrBuf.slice(-STDERR_MAX_BYTES);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
this.proc.stdin.on('error', (err) => {
|
|
69
|
-
if (err.code === 'EPIPE' || err.code === 'ERR_STREAM_DESTROYED') {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
console.error('[ClaudeTransport] stdin error:', err.message);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
this.proc.on('error', (err) => {
|
|
76
|
-
this._didReportError = true;
|
|
77
|
-
this.emit('crash', {
|
|
78
|
-
code: null,
|
|
79
|
-
signal: null,
|
|
80
|
-
stderr: err.message,
|
|
81
|
-
reason: 'spawn_error'
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
this.proc.on('exit', (code, signal) => {
|
|
86
|
-
this._clearShutdownTimers();
|
|
87
|
-
if (this._didRequestShutdown) {
|
|
88
|
-
this.emit('shutdown', { clean: code === 0 });
|
|
89
|
-
} else if (!this._didReportError) {
|
|
90
|
-
if (code !== 0) {
|
|
91
|
-
this.emit('crash', {
|
|
92
|
-
code,
|
|
93
|
-
signal,
|
|
94
|
-
stderr: this._stderrBuf,
|
|
95
|
-
reason: 'unexpected_exit'
|
|
96
|
-
});
|
|
97
|
-
} else {
|
|
98
|
-
this.emit('shutdown', { clean: true });
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
return this;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
send(jsonObj) {
|
|
107
|
-
if (!this.proc || !this.proc.stdin || !this.proc.stdin.writable || this.proc.stdin.destroyed) {
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
try {
|
|
111
|
-
this.proc.stdin.write(JSON.stringify(jsonObj) + '\n');
|
|
112
|
-
return true;
|
|
113
|
-
} catch (err) {
|
|
114
|
-
if (err.code !== 'EPIPE' && err.code !== 'ERR_STREAM_DESTROYED') {
|
|
115
|
-
console.error('[ClaudeTransport] send error:', err.message);
|
|
116
|
-
}
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
shutdown() {
|
|
122
|
-
if (this._didRequestShutdown) return;
|
|
123
|
-
this._didRequestShutdown = true;
|
|
124
|
-
|
|
125
|
-
if (!this.proc || this.proc.exitCode !== null) {
|
|
126
|
-
this.emit('shutdown', { clean: true });
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
try {
|
|
131
|
-
this.proc.kill('SIGTERM');
|
|
132
|
-
} catch (_err) {
|
|
133
|
-
// Process may already be gone
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const killTimer = setTimeout(() => {
|
|
137
|
-
if (this.proc && this.proc.exitCode === null) {
|
|
138
|
-
try {
|
|
139
|
-
this.proc.kill('SIGKILL');
|
|
140
|
-
} catch (_err) {
|
|
141
|
-
// Process already gone
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}, SHUTDOWN_SIGTERM_TIMEOUT);
|
|
145
|
-
|
|
146
|
-
const forceTimer = setTimeout(() => {
|
|
147
|
-
if (this.proc && this.proc.exitCode === null) {
|
|
148
|
-
this.emit('shutdown', { clean: false });
|
|
149
|
-
}
|
|
150
|
-
}, SHUTDOWN_SIGKILL_TIMEOUT);
|
|
151
|
-
|
|
152
|
-
this._shutdownTimers = [killTimer, forceTimer];
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
get isAlive() {
|
|
156
|
-
return !!(this.proc && this.proc.exitCode === null);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
_handleLine(line) {
|
|
160
|
-
try {
|
|
161
|
-
const parsed = JSON.parse(line);
|
|
162
|
-
this.emit('json_event', parsed);
|
|
163
|
-
} catch (_err) {
|
|
164
|
-
// Ignore non-JSON output from claude.
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
_clearShutdownTimers() {
|
|
169
|
-
for (const timer of this._shutdownTimers) {
|
|
170
|
-
clearTimeout(timer);
|
|
171
|
-
}
|
|
172
|
-
this._shutdownTimers = [];
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
module.exports = { ClaudeTransport };
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Unified error codes shared between server variants and clients.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const ErrorCodes = {
|
|
6
|
-
AUTH_FAILED: 'AUTH_FAILED',
|
|
7
|
-
AUTH_RATE_LIMITED: 'AUTH_RATE_LIMITED',
|
|
8
|
-
|
|
9
|
-
SESSION_NOT_FOUND: 'SESSION_NOT_FOUND',
|
|
10
|
-
SESSION_INVALID_NAME: 'SESSION_INVALID_NAME',
|
|
11
|
-
SESSION_CREATE_FAILED: 'SESSION_CREATE_FAILED',
|
|
12
|
-
|
|
13
|
-
PATH_ACCESS_DENIED: 'PATH_ACCESS_DENIED',
|
|
14
|
-
PATH_NOT_FOUND: 'PATH_NOT_FOUND',
|
|
15
|
-
PATH_INVALID: 'PATH_INVALID',
|
|
16
|
-
|
|
17
|
-
COMMAND_INVALID: 'COMMAND_INVALID',
|
|
18
|
-
COMMAND_EXECUTION_FAILED: 'COMMAND_EXECUTION_FAILED',
|
|
19
|
-
|
|
20
|
-
CONNECTION_TIMEOUT: 'CONNECTION_TIMEOUT',
|
|
21
|
-
CONNECTION_CLOSED: 'CONNECTION_CLOSED',
|
|
22
|
-
|
|
23
|
-
UPLOAD_TOO_LARGE: 'UPLOAD_TOO_LARGE',
|
|
24
|
-
UPLOAD_INVALID_TYPE: 'UPLOAD_INVALID_TYPE',
|
|
25
|
-
UPLOAD_NOT_FOUND: 'UPLOAD_NOT_FOUND',
|
|
26
|
-
UPLOAD_CHUNK_ERROR: 'UPLOAD_CHUNK_ERROR',
|
|
27
|
-
UPLOAD_PATH_DENIED: 'UPLOAD_PATH_DENIED',
|
|
28
|
-
|
|
29
|
-
INVALID_REQUEST: 'INVALID_REQUEST',
|
|
30
|
-
INTERNAL_ERROR: 'INTERNAL_ERROR',
|
|
31
|
-
RATE_LIMITED: 'RATE_LIMITED'
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
function createError(code, message, details = {}) {
|
|
35
|
-
const error = {
|
|
36
|
-
type: 'error',
|
|
37
|
-
code,
|
|
38
|
-
error: message,
|
|
39
|
-
retryable: isRetryable(code),
|
|
40
|
-
...details
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
if (details.retryAfter !== undefined) {
|
|
44
|
-
error.retryAfterMs = details.retryAfter * 1000;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return error;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function createRateLimitError(retryAfter) {
|
|
51
|
-
return createError(
|
|
52
|
-
ErrorCodes.RATE_LIMITED,
|
|
53
|
-
'Too many requests, please try again later',
|
|
54
|
-
{ retryAfter }
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function createSessionNotFoundError(sessionName) {
|
|
59
|
-
return createError(
|
|
60
|
-
ErrorCodes.SESSION_NOT_FOUND,
|
|
61
|
-
`Session not found: ${sessionName}`,
|
|
62
|
-
{ session: sessionName }
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function createPathAccessDeniedError(targetPath) {
|
|
67
|
-
return createError(
|
|
68
|
-
ErrorCodes.PATH_ACCESS_DENIED,
|
|
69
|
-
`Access denied to path: ${targetPath}`,
|
|
70
|
-
{ path: targetPath }
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function isRetryable(code) {
|
|
75
|
-
const retryableCodes = [
|
|
76
|
-
ErrorCodes.CONNECTION_TIMEOUT,
|
|
77
|
-
ErrorCodes.RATE_LIMITED,
|
|
78
|
-
ErrorCodes.AUTH_RATE_LIMITED,
|
|
79
|
-
ErrorCodes.INTERNAL_ERROR
|
|
80
|
-
];
|
|
81
|
-
return retryableCodes.includes(code);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
module.exports = {
|
|
85
|
-
ErrorCodes,
|
|
86
|
-
createError,
|
|
87
|
-
createRateLimitError,
|
|
88
|
-
createSessionNotFoundError,
|
|
89
|
-
createPathAccessDeniedError,
|
|
90
|
-
isRetryable
|
|
91
|
-
};
|
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Safe command execution module
|
|
3
|
-
* Uses spawn instead of exec to prevent command injection.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const { spawn } = require('child_process');
|
|
7
|
-
const os = require('os');
|
|
8
|
-
|
|
9
|
-
const TMUX_SOCKET_NAME = process.env.CLAUDE_REMOTE_TMUX_SOCKET || `tacticremote-${os.userInfo().username}`;
|
|
10
|
-
|
|
11
|
-
// Ensure common package-manager install paths are available to spawned processes.
|
|
12
|
-
(function expandPath() {
|
|
13
|
-
const extra = process.platform === 'darwin'
|
|
14
|
-
? ['/opt/homebrew/bin', '/usr/local/bin', '/opt/homebrew/sbin', '/usr/local/sbin']
|
|
15
|
-
: ['/usr/local/bin'];
|
|
16
|
-
const current = (process.env.PATH || '').split(':');
|
|
17
|
-
const missing = extra.filter(p => !current.includes(p));
|
|
18
|
-
if (missing.length) {
|
|
19
|
-
process.env.PATH = [...missing, ...current].join(':');
|
|
20
|
-
}
|
|
21
|
-
})();
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Execute a command safely using spawn (no shell interpolation)
|
|
25
|
-
* @param {string} cmd - The command to execute
|
|
26
|
-
* @param {string[]} args - Array of arguments
|
|
27
|
-
* @param {object} opts - Options for spawn
|
|
28
|
-
* @param {number} [opts.timeout] - Timeout in ms (default 30000, 0 = no timeout)
|
|
29
|
-
* @param {number} [opts.maxBuffer] - Maximum bytes per stream before abort (default 2MB)
|
|
30
|
-
* @returns {Promise<string>} - stdout output
|
|
31
|
-
*/
|
|
32
|
-
function execSafe(cmd, args = [], opts = {}) {
|
|
33
|
-
const { timeout = 30000, maxBuffer = 2 * 1024 * 1024, ...spawnOpts } = opts;
|
|
34
|
-
|
|
35
|
-
return new Promise((resolve, reject) => {
|
|
36
|
-
const child = spawn(cmd, args, {
|
|
37
|
-
shell: false,
|
|
38
|
-
...spawnOpts
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
let stdout = '';
|
|
42
|
-
let stderr = '';
|
|
43
|
-
let timedOut = false;
|
|
44
|
-
let bufferExceeded = false;
|
|
45
|
-
let exceededStream = '';
|
|
46
|
-
let timer = null;
|
|
47
|
-
let killTimer = null;
|
|
48
|
-
|
|
49
|
-
if (timeout > 0) {
|
|
50
|
-
timer = setTimeout(() => {
|
|
51
|
-
timedOut = true;
|
|
52
|
-
child.kill('SIGTERM');
|
|
53
|
-
killTimer = setTimeout(() => {
|
|
54
|
-
try {
|
|
55
|
-
child.kill('SIGKILL');
|
|
56
|
-
} catch (_err) {
|
|
57
|
-
// Ignore kill errors when process has already exited.
|
|
58
|
-
}
|
|
59
|
-
}, 5000);
|
|
60
|
-
}, timeout);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
child.stdout.on('data', (data) => {
|
|
64
|
-
stdout += data.toString();
|
|
65
|
-
if (!bufferExceeded && stdout.length > maxBuffer) {
|
|
66
|
-
bufferExceeded = true;
|
|
67
|
-
exceededStream = 'stdout';
|
|
68
|
-
child.kill('SIGTERM');
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
child.stderr.on('data', (data) => {
|
|
73
|
-
stderr += data.toString();
|
|
74
|
-
if (!bufferExceeded && stderr.length > maxBuffer) {
|
|
75
|
-
bufferExceeded = true;
|
|
76
|
-
exceededStream = 'stderr';
|
|
77
|
-
child.kill('SIGTERM');
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
child.on('close', (code) => {
|
|
82
|
-
if (timer) clearTimeout(timer);
|
|
83
|
-
if (killTimer) clearTimeout(killTimer);
|
|
84
|
-
if (timedOut) {
|
|
85
|
-
const error = new Error(`Command timed out after ${timeout}ms: ${cmd} ${args.join(' ')}`);
|
|
86
|
-
error.code = 'ETIMEDOUT';
|
|
87
|
-
reject(error);
|
|
88
|
-
} else if (bufferExceeded) {
|
|
89
|
-
const error = new Error(`Command exceeded maxBuffer (${maxBuffer} bytes) on ${exceededStream}: ${cmd} ${args.join(' ')}`);
|
|
90
|
-
error.code = 'EMAXBUFFER';
|
|
91
|
-
error.stream = exceededStream;
|
|
92
|
-
reject(error);
|
|
93
|
-
} else if (code === 0) {
|
|
94
|
-
resolve(stdout.trim());
|
|
95
|
-
} else {
|
|
96
|
-
const error = new Error(stderr || `Command failed with code ${code}`);
|
|
97
|
-
error.code = code;
|
|
98
|
-
error.stderr = stderr;
|
|
99
|
-
reject(error);
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
child.on('error', (err) => {
|
|
104
|
-
if (timer) clearTimeout(timer);
|
|
105
|
-
if (killTimer) clearTimeout(killTimer);
|
|
106
|
-
reject(err);
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Execute tmux command safely.
|
|
113
|
-
* @param {string} subCommand - tmux subcommand (e.g., 'send-keys', 'capture-pane')
|
|
114
|
-
* @param {string[]} args - Arguments for the subcommand
|
|
115
|
-
* @returns {Promise<string>}
|
|
116
|
-
*/
|
|
117
|
-
function execTmux(subCommand, args = []) {
|
|
118
|
-
const env = { ...process.env };
|
|
119
|
-
delete env.TMUX;
|
|
120
|
-
delete env.TMUX_PANE;
|
|
121
|
-
return execSafe('tmux', ['-L', TMUX_SOCKET_NAME, subCommand, ...args], { env });
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
async function tmuxSessionExists(sessionName) {
|
|
125
|
-
try {
|
|
126
|
-
// = prefix forces exact-name match. Critical here: without it, tmux
|
|
127
|
-
// falls back to prefix matching and reports "foo" exists just because
|
|
128
|
-
// "foo-2" does — which previously caused kill-session to delete the
|
|
129
|
-
// wrong session in the phantom-cleanup branch of createSession.
|
|
130
|
-
await execTmux('has-session', ['-t', '=' + sessionName]);
|
|
131
|
-
return true;
|
|
132
|
-
} catch {
|
|
133
|
-
return false;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function tmuxCapture(sessionName, lines = 500) {
|
|
138
|
-
return execTmux('capture-pane', ['-t', sessionName, '-p', '-S', `-${lines}`]);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function tmuxSendKeys(sessionName, keys) {
|
|
142
|
-
return execTmux('send-keys', ['-t', sessionName, ...keys]);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
async function tmuxNewSession(sessionName, workingDir, cols, rows) {
|
|
146
|
-
const args = ['-d', '-s', sessionName, '-c', workingDir, '-e', `TACTIC_SESSION_ID=${sessionName}`];
|
|
147
|
-
if (cols && rows) {
|
|
148
|
-
args.push('-x', String(cols), '-y', String(rows));
|
|
149
|
-
}
|
|
150
|
-
await execTmux('new-session', args);
|
|
151
|
-
await execTmux('set-option', ['-t', sessionName, 'history-limit', '50000']);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function tmuxKillSession(sessionName) {
|
|
155
|
-
// = prefix forces exact-name match. Without it, killing "foo" when only
|
|
156
|
-
// "foo-2" exists (e.g. after a race between kill and phantom-cleanup)
|
|
157
|
-
// would silently kill "foo-2" via tmux prefix matching.
|
|
158
|
-
return execTmux('kill-session', ['-t', '=' + sessionName]);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function tmuxListSessions() {
|
|
162
|
-
return execTmux('list-sessions', ['-F', '#{session_name}:#{session_attached}:#{pane_current_path}:#{@tactic_session_path}']);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function tmuxGetPanePath(sessionName) {
|
|
166
|
-
return execTmux('display-message', ['-t', sessionName, '-p', '#{pane_current_path}']);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function tmuxResizeWindow(sessionName, cols, rows) {
|
|
170
|
-
return execTmux('resize-window', ['-t', sessionName, '-x', String(cols), '-y', String(rows)]);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
module.exports = {
|
|
174
|
-
execSafe,
|
|
175
|
-
execTmux,
|
|
176
|
-
TMUX_SOCKET_NAME,
|
|
177
|
-
tmuxSessionExists,
|
|
178
|
-
tmuxCapture,
|
|
179
|
-
tmuxSendKeys,
|
|
180
|
-
tmuxNewSession,
|
|
181
|
-
tmuxKillSession,
|
|
182
|
-
tmuxListSessions,
|
|
183
|
-
tmuxGetPanePath,
|
|
184
|
-
tmuxResizeWindow
|
|
185
|
-
};
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* File system service shared across server variants.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const path = require('path');
|
|
7
|
-
const { execFileSync } = require('child_process');
|
|
8
|
-
const { validatePath } = require('./validators');
|
|
9
|
-
const { ErrorCodes, createError } = require('./errorCodes');
|
|
10
|
-
|
|
11
|
-
function changePath(newPath, allowedBasePath, currentPath) {
|
|
12
|
-
const validation = validatePath(newPath, { allowedBasePath, currentPath });
|
|
13
|
-
|
|
14
|
-
if (!validation.valid) {
|
|
15
|
-
return {
|
|
16
|
-
success: false,
|
|
17
|
-
error: createError(ErrorCodes.PATH_INVALID, validation.error)
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const expandedPath = validation.path;
|
|
22
|
-
|
|
23
|
-
if (!fs.existsSync(expandedPath)) {
|
|
24
|
-
return {
|
|
25
|
-
success: false,
|
|
26
|
-
error: createError(ErrorCodes.PATH_NOT_FOUND, `Path does not exist: ${newPath}`)
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (!fs.statSync(expandedPath).isDirectory()) {
|
|
31
|
-
return {
|
|
32
|
-
success: false,
|
|
33
|
-
error: createError(ErrorCodes.PATH_INVALID, `Not a directory: ${newPath}`)
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
success: true,
|
|
39
|
-
path: expandedPath
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function createFolder(folderPath, allowedBasePath, currentPath) {
|
|
44
|
-
const validation = validatePath(folderPath, { allowedBasePath, currentPath });
|
|
45
|
-
|
|
46
|
-
if (!validation.valid) {
|
|
47
|
-
return {
|
|
48
|
-
success: false,
|
|
49
|
-
error: createError(ErrorCodes.PATH_INVALID, validation.error)
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const expandedPath = validation.path;
|
|
54
|
-
|
|
55
|
-
if (fs.existsSync(expandedPath)) {
|
|
56
|
-
return {
|
|
57
|
-
success: false,
|
|
58
|
-
error: createError(ErrorCodes.PATH_INVALID, `Path already exists: ${folderPath}`)
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
try {
|
|
63
|
-
fs.mkdirSync(expandedPath, { recursive: true });
|
|
64
|
-
return {
|
|
65
|
-
success: true,
|
|
66
|
-
path: expandedPath
|
|
67
|
-
};
|
|
68
|
-
} catch (err) {
|
|
69
|
-
return {
|
|
70
|
-
success: false,
|
|
71
|
-
error: createError(ErrorCodes.PATH_ACCESS_DENIED, err.message)
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function listDirectory(dirPath, allowedBasePath, currentPath) {
|
|
77
|
-
const validation = validatePath(dirPath, { allowBrowse: true, allowedBasePath, currentPath });
|
|
78
|
-
|
|
79
|
-
if (!validation.valid) {
|
|
80
|
-
return {
|
|
81
|
-
success: false,
|
|
82
|
-
error: createError(ErrorCodes.PATH_INVALID, validation.error)
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const expandedPath = validation.path;
|
|
87
|
-
|
|
88
|
-
if (!fs.existsSync(expandedPath)) {
|
|
89
|
-
return {
|
|
90
|
-
success: false,
|
|
91
|
-
error: createError(ErrorCodes.PATH_NOT_FOUND, `Directory does not exist: ${dirPath}`)
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (!fs.statSync(expandedPath).isDirectory()) {
|
|
96
|
-
return {
|
|
97
|
-
success: false,
|
|
98
|
-
error: createError(ErrorCodes.PATH_INVALID, `Not a directory: ${dirPath}`)
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
try {
|
|
103
|
-
const entries = fs.readdirSync(expandedPath, { withFileTypes: true });
|
|
104
|
-
|
|
105
|
-
const items = entries
|
|
106
|
-
.filter(entry => !entry.name.startsWith('.'))
|
|
107
|
-
.map(entry => ({
|
|
108
|
-
name: entry.name,
|
|
109
|
-
isDirectory: entry.isDirectory(),
|
|
110
|
-
path: path.join(expandedPath, entry.name)
|
|
111
|
-
}))
|
|
112
|
-
.sort((a, b) => {
|
|
113
|
-
if (a.isDirectory && !b.isDirectory) return -1;
|
|
114
|
-
if (!a.isDirectory && b.isDirectory) return 1;
|
|
115
|
-
return a.name.localeCompare(b.name);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
const gitStatusMap = {};
|
|
119
|
-
try {
|
|
120
|
-
const output = execFileSync('git', ['-C', expandedPath, 'status', '--short', '--porcelain'], { timeout: 3000, encoding: 'utf-8' });
|
|
121
|
-
for (const line of output.split('\n')) {
|
|
122
|
-
if (line.length < 4) continue;
|
|
123
|
-
const status = line.substring(0, 2).trim();
|
|
124
|
-
let filePath = line.substring(3).trim();
|
|
125
|
-
if (filePath.includes(' -> ')) filePath = filePath.split(' -> ')[1];
|
|
126
|
-
const topLevel = filePath.split('/')[0];
|
|
127
|
-
if (status && topLevel) gitStatusMap[topLevel] = status;
|
|
128
|
-
}
|
|
129
|
-
} catch {
|
|
130
|
-
// not a git repo or git not available
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const enrichedItems = items.map(item => ({
|
|
134
|
-
...item,
|
|
135
|
-
gitStatus: gitStatusMap[item.name] || null
|
|
136
|
-
}));
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
success: true,
|
|
140
|
-
path: expandedPath,
|
|
141
|
-
items: enrichedItems
|
|
142
|
-
};
|
|
143
|
-
} catch (err) {
|
|
144
|
-
return {
|
|
145
|
-
success: false,
|
|
146
|
-
error: createError(ErrorCodes.PATH_ACCESS_DENIED, err.message)
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
module.exports = {
|
|
152
|
-
changePath,
|
|
153
|
-
createFolder,
|
|
154
|
-
listDirectory
|
|
155
|
-
};
|