@turns/dev 0.0.7 → 0.0.8
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/README.md +10 -4
- package/dist/daemon.js +123 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -39,14 +39,19 @@ never prunes saved upload cursors, so it cannot make the sessions it filtered
|
|
|
39
39
|
out replay from their first byte the next time the daemon starts.
|
|
40
40
|
|
|
41
41
|
The server is selected with `--server URL` or `TURNS_ORIGIN`. Its normalized
|
|
42
|
-
origin is saved
|
|
43
|
-
|
|
42
|
+
origin is saved and reused by later invocations and daemon starts. On Linux,
|
|
43
|
+
Turns uses the XDG config/state directories and installs this user service:
|
|
44
44
|
|
|
45
45
|
- `~/.config/turns/machine-id`
|
|
46
46
|
- `~/.local/state/turns/native-upload-state.json`
|
|
47
47
|
- `~/.local/state/turns/letta-sessions/*.json` (Letta conversation-to-project associations)
|
|
48
48
|
- `~/.config/systemd/user/turns.service`
|
|
49
49
|
|
|
50
|
+
On macOS, credentials and daemon state live under
|
|
51
|
+
`~/Library/Application Support/turns/`, and `turns install` creates the per-user
|
|
52
|
+
LaunchAgent `~/Library/LaunchAgents/dev.turns.cli.plist`. Its output is written
|
|
53
|
+
to `~/Library/Application Support/turns/daemon.log`.
|
|
54
|
+
|
|
50
55
|
The credential file contains a seven-day access token and a signed refresh
|
|
51
56
|
token. The CLI refreshes the access token at least once every 24 hours. A new
|
|
52
57
|
machine opens a one-time turns.dev URL, waits on an authentication WebSocket,
|
|
@@ -72,8 +77,9 @@ An explicit file browse may upload up to 199 MB to the same private R2 prefix.
|
|
|
72
77
|
The daemon streams the file from disk and the Worker hands that request stream
|
|
73
78
|
straight to R2, so neither process retains the complete file in memory.
|
|
74
79
|
|
|
75
|
-
The systemd unit uses `KillMode=process
|
|
76
|
-
|
|
80
|
+
The systemd unit uses `KillMode=process`, and the macOS LaunchAgent uses
|
|
81
|
+
`AbandonProcessGroup`: restarting the uploader leaves its detached agent-anchor
|
|
82
|
+
processes running so in-flight harness turns continue.
|
|
77
83
|
For a foreground daemon restricted to one or more directory trees, repeat
|
|
78
84
|
`--directory`. Both transcript discovery and agent launches enforce the
|
|
79
85
|
canonical prefix, including protection against symlinks that escape it:
|
package/dist/daemon.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
2
3
|
import path from 'node:path';
|
|
3
4
|
import process from 'node:process';
|
|
4
5
|
import { spawnSync } from 'node:child_process';
|
|
@@ -36,6 +37,7 @@ export function canonicalDaemonCliEntry(cliDirectory = HERE) {
|
|
|
36
37
|
}
|
|
37
38
|
export const SYSTEMD_SERVICE_NAME = 'turns.service';
|
|
38
39
|
export const LEGACY_SYSTEMD_SERVICE_NAME = 'turns-native-uploader.service';
|
|
40
|
+
export const MAC_LAUNCH_AGENT_LABEL = 'dev.turns.cli';
|
|
39
41
|
function systemdQuote(value) {
|
|
40
42
|
return `"${String(value).replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll('%', '%%')}"`;
|
|
41
43
|
}
|
|
@@ -59,6 +61,62 @@ Environment=NODE_NO_WARNINGS=1
|
|
|
59
61
|
WantedBy=default.target
|
|
60
62
|
`;
|
|
61
63
|
}
|
|
64
|
+
function xmlEscape(value) {
|
|
65
|
+
return String(value)
|
|
66
|
+
.replaceAll('&', '&')
|
|
67
|
+
.replaceAll('<', '<')
|
|
68
|
+
.replaceAll('>', '>')
|
|
69
|
+
.replaceAll('"', '"')
|
|
70
|
+
.replaceAll("'", ''');
|
|
71
|
+
}
|
|
72
|
+
export function macLaunchAgentPlist({ cliEntry = canonicalDaemonCliEntry(), execPath = process.execPath, workingDirectory = os.homedir(), logPath = applicationPaths().daemonLog, pathEnvironment = process.env.PATH || '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin', } = {}) {
|
|
73
|
+
const arguments_ = [
|
|
74
|
+
execPath,
|
|
75
|
+
'--max-old-space-size=256',
|
|
76
|
+
'--max-semi-space-size=8',
|
|
77
|
+
'--experimental-strip-types',
|
|
78
|
+
cliEntry,
|
|
79
|
+
'daemon',
|
|
80
|
+
'run',
|
|
81
|
+
];
|
|
82
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
83
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
84
|
+
<plist version="1.0">
|
|
85
|
+
<dict>
|
|
86
|
+
<key>Label</key>
|
|
87
|
+
<string>${MAC_LAUNCH_AGENT_LABEL}</string>
|
|
88
|
+
<key>ProgramArguments</key>
|
|
89
|
+
<array>
|
|
90
|
+
${arguments_.map((value) => ` <string>${xmlEscape(value)}</string>`).join('\n')}
|
|
91
|
+
</array>
|
|
92
|
+
<key>WorkingDirectory</key>
|
|
93
|
+
<string>${xmlEscape(workingDirectory)}</string>
|
|
94
|
+
<key>RunAtLoad</key>
|
|
95
|
+
<true/>
|
|
96
|
+
<key>KeepAlive</key>
|
|
97
|
+
<dict>
|
|
98
|
+
<key>SuccessfulExit</key>
|
|
99
|
+
<false/>
|
|
100
|
+
</dict>
|
|
101
|
+
<key>ThrottleInterval</key>
|
|
102
|
+
<integer>5</integer>
|
|
103
|
+
<key>AbandonProcessGroup</key>
|
|
104
|
+
<true/>
|
|
105
|
+
<key>EnvironmentVariables</key>
|
|
106
|
+
<dict>
|
|
107
|
+
<key>NODE_NO_WARNINGS</key>
|
|
108
|
+
<string>1</string>
|
|
109
|
+
<key>PATH</key>
|
|
110
|
+
<string>${xmlEscape(pathEnvironment)}</string>
|
|
111
|
+
</dict>
|
|
112
|
+
<key>StandardOutPath</key>
|
|
113
|
+
<string>${xmlEscape(logPath)}</string>
|
|
114
|
+
<key>StandardErrorPath</key>
|
|
115
|
+
<string>${xmlEscape(logPath)}</string>
|
|
116
|
+
</dict>
|
|
117
|
+
</plist>
|
|
118
|
+
`;
|
|
119
|
+
}
|
|
62
120
|
function systemctl(arguments_, { spawnSyncImpl = spawnSync, allowFailure = false } = {}) {
|
|
63
121
|
const result = spawnSyncImpl('systemctl', ['--user', ...arguments_], { encoding: 'utf8', stdio: 'pipe' });
|
|
64
122
|
if (!allowFailure && result.status !== 0) {
|
|
@@ -66,6 +124,16 @@ function systemctl(arguments_, { spawnSyncImpl = spawnSync, allowFailure = false
|
|
|
66
124
|
}
|
|
67
125
|
return result;
|
|
68
126
|
}
|
|
127
|
+
function launchctl(arguments_, { spawnSyncImpl = spawnSync, allowFailure = false } = {}) {
|
|
128
|
+
const result = spawnSyncImpl('/bin/launchctl', arguments_, { encoding: 'utf8', stdio: 'pipe' });
|
|
129
|
+
if (!allowFailure && result.status !== 0) {
|
|
130
|
+
throw new Error(String(result.stderr || result.stdout || `launchctl ${arguments_.join(' ')} failed`).trim());
|
|
131
|
+
}
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
function macLaunchTarget(userId) {
|
|
135
|
+
return `gui/${userId}/${MAC_LAUNCH_AGENT_LABEL}`;
|
|
136
|
+
}
|
|
69
137
|
export function installLinuxDaemon({ paths = applicationPaths(), spawnSyncImpl = spawnSync, } = {}) {
|
|
70
138
|
fs.mkdirSync(path.dirname(paths.linuxService), { recursive: true, mode: 0o700 });
|
|
71
139
|
const unit = linuxServiceUnit();
|
|
@@ -115,20 +183,63 @@ export function linuxDaemonStatus({ spawnSyncImpl = spawnSync } = {}) {
|
|
|
115
183
|
status: String(result.stdout || result.stderr || 'unknown').trim(),
|
|
116
184
|
};
|
|
117
185
|
}
|
|
118
|
-
export function
|
|
119
|
-
if (
|
|
120
|
-
throw new Error('
|
|
121
|
-
}
|
|
122
|
-
|
|
186
|
+
export function installMacDaemon({ paths = applicationPaths(), spawnSyncImpl = spawnSync, userId = process.getuid?.(), } = {}) {
|
|
187
|
+
if (userId === undefined)
|
|
188
|
+
throw new Error('Could not determine the macOS user ID for launchd.');
|
|
189
|
+
fs.mkdirSync(path.dirname(paths.macLaunchAgent), { recursive: true, mode: 0o700 });
|
|
190
|
+
fs.mkdirSync(path.dirname(paths.daemonLog), { recursive: true, mode: 0o700 });
|
|
191
|
+
const target = macLaunchTarget(userId);
|
|
192
|
+
const temporary = `${paths.macLaunchAgent}.${process.pid}.tmp`;
|
|
193
|
+
fs.writeFileSync(temporary, macLaunchAgentPlist({ workingDirectory: os.homedir(), logPath: paths.daemonLog }), { mode: 0o644 });
|
|
194
|
+
fs.renameSync(temporary, paths.macLaunchAgent);
|
|
195
|
+
// A loaded job retains its previous definition. Boot it out after the atomic
|
|
196
|
+
// replacement so a reinstall always starts the command from the current CLI.
|
|
197
|
+
launchctl(['bootout', target], { spawnSyncImpl, allowFailure: true });
|
|
198
|
+
launchctl(['enable', target], { spawnSyncImpl });
|
|
199
|
+
launchctl(['bootstrap', `gui/${userId}`, paths.macLaunchAgent], { spawnSyncImpl });
|
|
200
|
+
launchctl(['kickstart', '-k', target], { spawnSyncImpl });
|
|
201
|
+
return paths.macLaunchAgent;
|
|
123
202
|
}
|
|
124
|
-
export function
|
|
125
|
-
if (
|
|
126
|
-
throw new Error('
|
|
203
|
+
export function uninstallMacDaemon({ paths = applicationPaths(), spawnSyncImpl = spawnSync, userId = process.getuid?.(), } = {}) {
|
|
204
|
+
if (userId === undefined)
|
|
205
|
+
throw new Error('Could not determine the macOS user ID for launchd.');
|
|
206
|
+
launchctl(['bootout', macLaunchTarget(userId)], { spawnSyncImpl, allowFailure: true });
|
|
207
|
+
try {
|
|
208
|
+
fs.unlinkSync(paths.macLaunchAgent);
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
if (error?.code !== 'ENOENT')
|
|
212
|
+
throw error;
|
|
127
213
|
}
|
|
128
|
-
return uninstallLinuxDaemon(options);
|
|
129
214
|
}
|
|
130
|
-
export function
|
|
131
|
-
if (
|
|
215
|
+
export function macDaemonStatus({ spawnSyncImpl = spawnSync, userId = process.getuid?.(), } = {}) {
|
|
216
|
+
if (userId === undefined)
|
|
132
217
|
return { active: false, status: 'unsupported' };
|
|
133
|
-
|
|
218
|
+
const result = launchctl(['print', macLaunchTarget(userId)], { spawnSyncImpl, allowFailure: true });
|
|
219
|
+
if (result.status !== 0)
|
|
220
|
+
return { active: false, status: 'not running' };
|
|
221
|
+
const output = String(result.stdout || result.stderr || '');
|
|
222
|
+
const state = /^\s*state = (.+?)\s*$/mu.exec(output)?.[1] || 'loaded';
|
|
223
|
+
return { active: state === 'running', status: state };
|
|
224
|
+
}
|
|
225
|
+
export function installDaemon({ platform = process.platform, ...options } = {}) {
|
|
226
|
+
if (platform === 'linux')
|
|
227
|
+
return installLinuxDaemon(options);
|
|
228
|
+
if (platform === 'darwin')
|
|
229
|
+
return installMacDaemon(options);
|
|
230
|
+
throw new Error('Automatic daemon installation is supported on Linux with systemd and macOS with launchd.');
|
|
231
|
+
}
|
|
232
|
+
export function uninstallDaemon({ platform = process.platform, ...options } = {}) {
|
|
233
|
+
if (platform === 'linux')
|
|
234
|
+
return uninstallLinuxDaemon(options);
|
|
235
|
+
if (platform === 'darwin')
|
|
236
|
+
return uninstallMacDaemon(options);
|
|
237
|
+
throw new Error('Automatic daemon removal is supported on Linux with systemd and macOS with launchd.');
|
|
238
|
+
}
|
|
239
|
+
export function daemonStatus({ platform = process.platform, ...options } = {}) {
|
|
240
|
+
if (platform === 'linux')
|
|
241
|
+
return linuxDaemonStatus(options);
|
|
242
|
+
if (platform === 'darwin')
|
|
243
|
+
return macDaemonStatus(options);
|
|
244
|
+
return { active: false, status: 'unsupported' };
|
|
134
245
|
}
|