tring-chat 0.1.7 → 0.1.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 +34 -0
- package/dist/fix-spawn-helper.mjs +55 -0
- package/dist/tring.js +294 -38
- package/dist/web/assets/{index-9gbsSKfr.css → index-BD2SqiUu.css} +1 -1
- package/dist/web/assets/{index-Duc9mz6Q.js → index-Vc7NVEbx.js} +8 -8
- package/dist/web/index.html +2 -2
- package/package.json +3 -1
- package/scripts/postinstall.mjs +18 -0
package/README.md
CHANGED
|
@@ -50,6 +50,21 @@ sudo dnf install -y gcc-c++ make python3 # Fedora/RHEL
|
|
|
50
50
|
Without those, `npm i -g tring-chat` fails while building node-pty. It takes a few seconds
|
|
51
51
|
once they are present.
|
|
52
52
|
|
|
53
|
+
**macOS needs no compiler, but node-pty's prebuilds carry one defect.** It
|
|
54
|
+
publishes `prebuilds/darwin-*/spawn-helper` without the execute bit
|
|
55
|
+
([node-pty#850](https://github.com/microsoft/node-pty/issues/850)), and every
|
|
56
|
+
PTY on macOS is forked through that binary — so an untreated install answers
|
|
57
|
+
the first tile with `posix_spawnp failed` and starts no shell. tring puts the
|
|
58
|
+
bit back, at install time and again before the first spawn, so this should
|
|
59
|
+
never be visible. If you installed with `--ignore-scripts` into a prefix you
|
|
60
|
+
do not own, the one-liner is:
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
sudo chmod +x "$(npm root -g)/tring-chat/node_modules/node-pty/prebuilds/darwin-arm64/spawn-helper"
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
All of this goes away when node-pty publishes a stable release carrying the fix.
|
|
67
|
+
|
|
53
68
|
`tring` starts the daemon and opens a chromeless browser window — no address bar, no
|
|
54
69
|
tab strip, its own taskbar entry. In that window the browser stops reserving
|
|
55
70
|
`Ctrl+1`–`Ctrl+8` for tab switching, so slots 11–16 get their natural keys.
|
|
@@ -392,6 +407,25 @@ Two things are deliberate:
|
|
|
392
407
|
Auto-executing whatever was there last time is how four dev servers end up fighting
|
|
393
408
|
over a port.
|
|
394
409
|
|
|
410
|
+
## Dropping images on a terminal
|
|
411
|
+
|
|
412
|
+
Drag a screenshot onto the centre terminal and its path is typed into the prompt,
|
|
413
|
+
the way dropping a file on any other terminal types one. Claude Code reads the
|
|
414
|
+
path from there.
|
|
415
|
+
|
|
416
|
+
It cannot work the way a native terminal's does. A browser hands a dropped file
|
|
417
|
+
over as bytes and withholds its location by design, and that location would be
|
|
418
|
+
the wrong machine's anyway whenever you have the deck open on a phone or a laptop
|
|
419
|
+
across the room. So the bytes go to the daemon, which writes them down on the
|
|
420
|
+
machine the shell is actually running on and answers with the path it chose.
|
|
421
|
+
|
|
422
|
+
- PNG, JPEG, GIF and WebP, up to 10MB. The type is read from the file's first
|
|
423
|
+
bytes, not from what the browser called it.
|
|
424
|
+
- The daemon names every file itself, under `~/.config/tring/uploads`. Nothing
|
|
425
|
+
a client sends reaches a filesystem path.
|
|
426
|
+
- The last 20 are kept. The directory is emptied when the daemon stops.
|
|
427
|
+
- The upload goes through the same token as the rest of the API.
|
|
428
|
+
|
|
395
429
|
## Claude Code
|
|
396
430
|
|
|
397
431
|
Optional. Add a `Stop` hook to `~/.claude/settings.json` so a tile turns green the
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// src/spawn-helper.ts
|
|
2
|
+
import {
|
|
3
|
+
closeSync,
|
|
4
|
+
constants,
|
|
5
|
+
fchmodSync,
|
|
6
|
+
fstatSync,
|
|
7
|
+
openSync,
|
|
8
|
+
readdirSync
|
|
9
|
+
} from "node:fs";
|
|
10
|
+
import { createRequire } from "node:module";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
var EXEC = 73;
|
|
13
|
+
function nodePtyRoot() {
|
|
14
|
+
try {
|
|
15
|
+
return path.dirname(createRequire(import.meta.url).resolve("node-pty/package.json"));
|
|
16
|
+
} catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function spawnHelperPaths(root) {
|
|
21
|
+
const found = [path.join(root, "build", "Release", "spawn-helper")];
|
|
22
|
+
let prebuilds;
|
|
23
|
+
try {
|
|
24
|
+
prebuilds = readdirSync(path.join(root, "prebuilds"), { withFileTypes: true });
|
|
25
|
+
} catch {
|
|
26
|
+
return found;
|
|
27
|
+
}
|
|
28
|
+
for (const entry of prebuilds) {
|
|
29
|
+
if (entry.isDirectory()) found.push(path.join(root, "prebuilds", entry.name, "spawn-helper"));
|
|
30
|
+
}
|
|
31
|
+
return found;
|
|
32
|
+
}
|
|
33
|
+
function fixSpawnHelper(root = nodePtyRoot()) {
|
|
34
|
+
if (process.platform === "win32" || root === null) return [];
|
|
35
|
+
const fixed = [];
|
|
36
|
+
for (const file of spawnHelperPaths(root)) {
|
|
37
|
+
let fd = null;
|
|
38
|
+
try {
|
|
39
|
+
fd = openSync(file, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
40
|
+
const info = fstatSync(fd);
|
|
41
|
+
if (!info.isFile()) continue;
|
|
42
|
+
const mode = info.mode & 511;
|
|
43
|
+
if (mode & EXEC) continue;
|
|
44
|
+
fchmodSync(fd, mode | EXEC);
|
|
45
|
+
fixed.push(file);
|
|
46
|
+
} catch {
|
|
47
|
+
} finally {
|
|
48
|
+
if (fd !== null) closeSync(fd);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return fixed;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// <stdin>
|
|
55
|
+
for (const file of fixSpawnHelper()) console.log(`tring: made ${file} executable`);
|
package/dist/tring.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { existsSync as existsSync2, readFileSync as readFileSync3 } from "node:fs";
|
|
5
5
|
import { createServer } from "node:http";
|
|
6
6
|
import { createServer as createSecureServer } from "node:https";
|
|
7
|
-
import
|
|
7
|
+
import path8 from "node:path";
|
|
8
8
|
import { fileURLToPath } from "node:url";
|
|
9
9
|
import { WebSocketServer } from "ws";
|
|
10
10
|
|
|
@@ -25,6 +25,7 @@ var SUSTAINED_MS = 1500;
|
|
|
25
25
|
var SUSTAINED_BYTES = 2048;
|
|
26
26
|
var BURST_GAP_MS = SUSTAINED_MS;
|
|
27
27
|
var DEFAULT_IDLE_MS = 3e3;
|
|
28
|
+
var ECHO_MS = 300;
|
|
28
29
|
var NOTABLE_BUSY_MS = 1e4;
|
|
29
30
|
var ActivityTracker = class {
|
|
30
31
|
status = "idle";
|
|
@@ -41,6 +42,7 @@ var ActivityTracker = class {
|
|
|
41
42
|
burstStart = null;
|
|
42
43
|
burstBytes = 0;
|
|
43
44
|
lastOutput = null;
|
|
45
|
+
lastNavigation = null;
|
|
44
46
|
constructor(now, idleMs = DEFAULT_IDLE_MS) {
|
|
45
47
|
this.since = now;
|
|
46
48
|
this.idleMs = idleMs;
|
|
@@ -53,15 +55,23 @@ var ActivityTracker = class {
|
|
|
53
55
|
}
|
|
54
56
|
this.lastOutput = now;
|
|
55
57
|
if (this.status !== "idle") return;
|
|
58
|
+
if (this.lastNavigation !== null && now - this.lastNavigation <= ECHO_MS) {
|
|
59
|
+
this.resetBurst();
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
56
62
|
if (this.burstStart === null) this.burstStart = now;
|
|
57
63
|
this.burstBytes += bytes;
|
|
58
64
|
if (now - this.burstStart >= SUSTAINED_MS || this.burstBytes >= SUSTAINED_BYTES) {
|
|
59
65
|
this.transition("busy", now);
|
|
60
66
|
}
|
|
61
67
|
}
|
|
62
|
-
/**
|
|
63
|
-
|
|
68
|
+
/**
|
|
69
|
+
* The user drove this session: a keystroke, a paste, a mouse report from a
|
|
70
|
+
* wheel. Only the focused session receives input.
|
|
71
|
+
*/
|
|
72
|
+
input(now, kind = "command") {
|
|
64
73
|
this.resetBurst();
|
|
74
|
+
this.lastNavigation = kind === "navigation" ? now : null;
|
|
65
75
|
if (this.status === "done") this.transition("idle", now);
|
|
66
76
|
}
|
|
67
77
|
/** OSC 133;C — a command started. */
|
|
@@ -390,6 +400,34 @@ import { readlinkSync } from "node:fs";
|
|
|
390
400
|
import { createRequire as createRequire2 } from "node:module";
|
|
391
401
|
import { spawn } from "node-pty";
|
|
392
402
|
|
|
403
|
+
// ../shared/src/input.ts
|
|
404
|
+
var NAVIGATION = new RegExp("^(?:" + [
|
|
405
|
+
"\\x1b\\[<\\d+;\\d+;\\d+[Mm]",
|
|
406
|
+
// SGR, SGR-pixels
|
|
407
|
+
"\\x1b\\[\\d+;\\d+;\\d+M",
|
|
408
|
+
// urxvt
|
|
409
|
+
"\\x1b\\[M[\\s\\S]{3}",
|
|
410
|
+
// X10, UTF-8
|
|
411
|
+
"\\x1b\\[[\\d;]*[ABCDHF]",
|
|
412
|
+
// arrows, home, end — with or without modifiers
|
|
413
|
+
"\\x1bO[ABCDHF]",
|
|
414
|
+
// the same in application cursor mode
|
|
415
|
+
"\\x1b\\[[145678]~",
|
|
416
|
+
// home, end, page up, page down
|
|
417
|
+
"\\x1b\\[[IO]"
|
|
418
|
+
// focus in, focus out — the terminal answering, not the user
|
|
419
|
+
].join("|") + ")");
|
|
420
|
+
function inputKind(data) {
|
|
421
|
+
if (!data) return "command";
|
|
422
|
+
let rest = data;
|
|
423
|
+
while (rest.length > 0) {
|
|
424
|
+
const match = NAVIGATION.exec(rest);
|
|
425
|
+
if (!match) return "command";
|
|
426
|
+
rest = rest.slice(match[0].length);
|
|
427
|
+
}
|
|
428
|
+
return "navigation";
|
|
429
|
+
}
|
|
430
|
+
|
|
393
431
|
// src/snapshot.ts
|
|
394
432
|
function snapshot(term) {
|
|
395
433
|
const buf = term.buffer.active;
|
|
@@ -579,7 +617,7 @@ var Session = class {
|
|
|
579
617
|
write(data) {
|
|
580
618
|
if (this.disposed) return;
|
|
581
619
|
this.hasInput = true;
|
|
582
|
-
this.signal((t, now) => t.input(now));
|
|
620
|
+
this.signal((t, now) => t.input(now, inputKind(data)));
|
|
583
621
|
this.pty.write(data);
|
|
584
622
|
}
|
|
585
623
|
resize(cols, rows) {
|
|
@@ -699,6 +737,8 @@ var SessionManager = class {
|
|
|
699
737
|
}
|
|
700
738
|
bySlot = /* @__PURE__ */ new Map();
|
|
701
739
|
byId = /* @__PURE__ */ new Map();
|
|
740
|
+
/** Set while a compound change runs, so listeners are told once, at the end. */
|
|
741
|
+
quiet = false;
|
|
702
742
|
onSessionData = null;
|
|
703
743
|
onSessionStatus = null;
|
|
704
744
|
onSessionExit = null;
|
|
@@ -730,10 +770,10 @@ var SessionManager = class {
|
|
|
730
770
|
session.onData = (data) => this.onSessionData?.(session, data);
|
|
731
771
|
session.onStatusChange = () => this.onSessionStatus?.(session);
|
|
732
772
|
session.onExit = (code) => this.onSessionExit?.(session, code);
|
|
733
|
-
session.onCwdChange = () => this.
|
|
773
|
+
session.onCwdChange = () => this.structureChanged();
|
|
734
774
|
this.bySlot.set(slot, session);
|
|
735
775
|
this.byId.set(session.id, session);
|
|
736
|
-
this.
|
|
776
|
+
this.structureChanged();
|
|
737
777
|
return session;
|
|
738
778
|
}
|
|
739
779
|
get(id) {
|
|
@@ -749,7 +789,7 @@ var SessionManager = class {
|
|
|
749
789
|
const s = this.byId.get(id);
|
|
750
790
|
if (!s) return;
|
|
751
791
|
s.name = name;
|
|
752
|
-
this.
|
|
792
|
+
this.structureChanged();
|
|
753
793
|
}
|
|
754
794
|
/**
|
|
755
795
|
* The tint ends up in a CSS custom property in the browser, and arrives from
|
|
@@ -760,7 +800,7 @@ var SessionManager = class {
|
|
|
760
800
|
const s = this.byId.get(id);
|
|
761
801
|
if (!s) return;
|
|
762
802
|
s.color = color !== null && HEX.test(color) ? color : null;
|
|
763
|
-
this.
|
|
803
|
+
this.structureChanged();
|
|
764
804
|
}
|
|
765
805
|
kill(id) {
|
|
766
806
|
const s = this.byId.get(id);
|
|
@@ -768,7 +808,7 @@ var SessionManager = class {
|
|
|
768
808
|
s.dispose();
|
|
769
809
|
this.bySlot.delete(s.slot);
|
|
770
810
|
this.byId.delete(id);
|
|
771
|
-
this.
|
|
811
|
+
this.structureChanged();
|
|
772
812
|
}
|
|
773
813
|
/**
|
|
774
814
|
* Explicit user-initiated respawn of a dead tile. Unlike daemon restore this
|
|
@@ -785,7 +825,12 @@ var SessionManager = class {
|
|
|
785
825
|
color: old.color,
|
|
786
826
|
autorun: true
|
|
787
827
|
};
|
|
788
|
-
this.
|
|
828
|
+
this.quiet = true;
|
|
829
|
+
try {
|
|
830
|
+
this.kill(id);
|
|
831
|
+
} finally {
|
|
832
|
+
this.quiet = false;
|
|
833
|
+
}
|
|
789
834
|
return this.create(spec);
|
|
790
835
|
}
|
|
791
836
|
tick(now) {
|
|
@@ -796,6 +841,9 @@ var SessionManager = class {
|
|
|
796
841
|
this.bySlot.clear();
|
|
797
842
|
this.byId.clear();
|
|
798
843
|
}
|
|
844
|
+
structureChanged() {
|
|
845
|
+
if (!this.quiet) this.onStructureChange?.();
|
|
846
|
+
}
|
|
799
847
|
firstEmptySlot() {
|
|
800
848
|
for (let slot = 1; slot <= SLOT_COUNT; slot++) if (!this.bySlot.has(slot)) return slot;
|
|
801
849
|
return null;
|
|
@@ -991,9 +1039,9 @@ var ProjectManager = class _ProjectManager {
|
|
|
991
1039
|
|
|
992
1040
|
// src/http.ts
|
|
993
1041
|
import { createReadStream } from "node:fs";
|
|
994
|
-
import { readdir as
|
|
1042
|
+
import { readdir as readdir3, realpath, stat as stat3 } from "node:fs/promises";
|
|
995
1043
|
import os4 from "node:os";
|
|
996
|
-
import
|
|
1044
|
+
import path6 from "node:path";
|
|
997
1045
|
|
|
998
1046
|
// src/security.ts
|
|
999
1047
|
import { createHash, timingSafeEqual } from "node:crypto";
|
|
@@ -1267,6 +1315,64 @@ function describe(err) {
|
|
|
1267
1315
|
return err.message || "could not run `claude -p /usage`";
|
|
1268
1316
|
}
|
|
1269
1317
|
|
|
1318
|
+
// src/uploads.ts
|
|
1319
|
+
import { randomBytes as randomBytes2 } from "node:crypto";
|
|
1320
|
+
import { mkdir as mkdir3, readdir as readdir2, rm, stat as stat2, writeFile as writeFile3 } from "node:fs/promises";
|
|
1321
|
+
import path5 from "node:path";
|
|
1322
|
+
var MAX_UPLOAD_BYTES = 10 * 1024 * 1024;
|
|
1323
|
+
var KEEP = 20;
|
|
1324
|
+
function sniffImage(bytes) {
|
|
1325
|
+
const at = (i, sig) => sig.every((b, n) => bytes[i + n] === b);
|
|
1326
|
+
if (at(0, [137, 80, 78, 71, 13, 10, 26, 10])) return ".png";
|
|
1327
|
+
if (at(0, [255, 216, 255])) return ".jpg";
|
|
1328
|
+
if (at(0, [71, 73, 70, 56])) return ".gif";
|
|
1329
|
+
if (at(0, [82, 73, 70, 70]) && at(8, [87, 69, 66, 80])) return ".webp";
|
|
1330
|
+
return null;
|
|
1331
|
+
}
|
|
1332
|
+
var NotAnImage = class extends Error {
|
|
1333
|
+
constructor() {
|
|
1334
|
+
super("only PNG, JPEG, GIF and WebP images can be dropped on a terminal");
|
|
1335
|
+
}
|
|
1336
|
+
};
|
|
1337
|
+
var UploadStore = class {
|
|
1338
|
+
constructor(dir, keep = KEEP) {
|
|
1339
|
+
this.dir = dir;
|
|
1340
|
+
this.keep = keep;
|
|
1341
|
+
}
|
|
1342
|
+
/** Writes the bytes and returns the absolute path to type into the prompt. */
|
|
1343
|
+
async save(bytes) {
|
|
1344
|
+
const ext = sniffImage(bytes);
|
|
1345
|
+
if (!ext) throw new NotAnImage();
|
|
1346
|
+
await mkdir3(this.dir, { recursive: true, mode: 448 });
|
|
1347
|
+
const file = path5.join(this.dir, randomBytes2(8).toString("hex") + ext);
|
|
1348
|
+
await writeFile3(file, bytes, { mode: 384 });
|
|
1349
|
+
await this.prune();
|
|
1350
|
+
return file;
|
|
1351
|
+
}
|
|
1352
|
+
/** Drops are scratch: a daemon that stops has nothing left to point at. */
|
|
1353
|
+
async dispose() {
|
|
1354
|
+
await rm(this.dir, { recursive: true, force: true }).catch(() => {
|
|
1355
|
+
});
|
|
1356
|
+
}
|
|
1357
|
+
/**
|
|
1358
|
+
* Oldest first, newest kept. A deck left open for weeks would otherwise
|
|
1359
|
+
* accumulate every screenshot ever dropped into it.
|
|
1360
|
+
*/
|
|
1361
|
+
async prune() {
|
|
1362
|
+
try {
|
|
1363
|
+
const names = await readdir2(this.dir);
|
|
1364
|
+
if (names.length <= this.keep) return;
|
|
1365
|
+
const dated = await Promise.all(names.map(async (name) => {
|
|
1366
|
+
const full = path5.join(this.dir, name);
|
|
1367
|
+
return { full, at: (await stat2(full)).mtimeMs };
|
|
1368
|
+
}));
|
|
1369
|
+
dated.sort((a, b) => b.at - a.at);
|
|
1370
|
+
await Promise.all(dated.slice(this.keep).map((f) => rm(f.full, { force: true })));
|
|
1371
|
+
} catch {
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
};
|
|
1375
|
+
|
|
1270
1376
|
// src/http.ts
|
|
1271
1377
|
var MIME = {
|
|
1272
1378
|
".html": "text/html; charset=utf-8",
|
|
@@ -1292,14 +1398,46 @@ var text = (res, code, body) => {
|
|
|
1292
1398
|
head(res, code, "text/plain; charset=utf-8");
|
|
1293
1399
|
res.end(body);
|
|
1294
1400
|
};
|
|
1295
|
-
var
|
|
1296
|
-
|
|
1401
|
+
var DRAIN_GRACE_MS = 2e3;
|
|
1402
|
+
var tooLarge = (req, res, message) => {
|
|
1403
|
+
res.writeHead(413, {
|
|
1404
|
+
...SECURITY_HEADERS,
|
|
1405
|
+
"content-type": "application/json; charset=utf-8",
|
|
1406
|
+
connection: "close"
|
|
1407
|
+
});
|
|
1408
|
+
res.end(JSON.stringify({ error: message }));
|
|
1409
|
+
const cut = setTimeout(() => req.destroy(), DRAIN_GRACE_MS);
|
|
1410
|
+
cut.unref();
|
|
1411
|
+
req.on("end", () => clearTimeout(cut));
|
|
1412
|
+
req.on("close", () => clearTimeout(cut));
|
|
1413
|
+
req.resume();
|
|
1414
|
+
};
|
|
1415
|
+
var MAX_BODY_BYTES = 1e6;
|
|
1416
|
+
var readBytes = (req, limit) => new Promise((resolve) => {
|
|
1417
|
+
let chunks = [];
|
|
1418
|
+
let size = 0;
|
|
1419
|
+
const settle = (body) => {
|
|
1420
|
+
if (chunks === null) return;
|
|
1421
|
+
chunks = null;
|
|
1422
|
+
resolve(body);
|
|
1423
|
+
};
|
|
1297
1424
|
req.on("data", (c) => {
|
|
1298
|
-
|
|
1299
|
-
|
|
1425
|
+
if (chunks === null) return;
|
|
1426
|
+
size += c.length;
|
|
1427
|
+
if (size > limit) {
|
|
1428
|
+
settle(null);
|
|
1429
|
+
return;
|
|
1430
|
+
}
|
|
1431
|
+
chunks.push(c);
|
|
1300
1432
|
});
|
|
1301
|
-
req.on("end", () =>
|
|
1433
|
+
req.on("end", () => settle(chunks ? Buffer.concat(chunks) : null));
|
|
1434
|
+
req.on("error", () => settle(null));
|
|
1435
|
+
req.on("close", () => settle(null));
|
|
1302
1436
|
});
|
|
1437
|
+
var readBody = async (req) => {
|
|
1438
|
+
const bytes = await readBytes(req, MAX_BODY_BYTES);
|
|
1439
|
+
return bytes === null ? null : bytes.toString("utf8");
|
|
1440
|
+
};
|
|
1303
1441
|
var USAGE_CACHE_MS = 3e4;
|
|
1304
1442
|
function createHandler(opts) {
|
|
1305
1443
|
const { pm, webRoot, token } = opts;
|
|
@@ -1307,14 +1445,14 @@ function createHandler(opts) {
|
|
|
1307
1445
|
let usage = null;
|
|
1308
1446
|
const homeDir = () => process.env["HOME"] ?? os4.homedir();
|
|
1309
1447
|
const within = (root, dir) => {
|
|
1310
|
-
const rel =
|
|
1311
|
-
return rel === "" || !rel.startsWith("..") && !
|
|
1448
|
+
const rel = path6.relative(path6.resolve(root), dir);
|
|
1449
|
+
return rel === "" || !rel.startsWith("..") && !path6.isAbsolute(rel);
|
|
1312
1450
|
};
|
|
1313
1451
|
const real = async (p) => {
|
|
1314
1452
|
try {
|
|
1315
1453
|
return await realpath(p);
|
|
1316
1454
|
} catch {
|
|
1317
|
-
return
|
|
1455
|
+
return path6.resolve(p);
|
|
1318
1456
|
}
|
|
1319
1457
|
};
|
|
1320
1458
|
const browsable = async (dir) => {
|
|
@@ -1345,9 +1483,11 @@ function createHandler(opts) {
|
|
|
1345
1483
|
if (status && req.method === "POST") {
|
|
1346
1484
|
const s = pm.findSession(decodeURIComponent(status[1]));
|
|
1347
1485
|
if (!s) return json(res, 404, { error: "no such session" });
|
|
1486
|
+
const raw = await readBody(req);
|
|
1487
|
+
if (raw === null) return tooLarge(req, res, "body too large");
|
|
1348
1488
|
let body;
|
|
1349
1489
|
try {
|
|
1350
|
-
body = JSON.parse(
|
|
1490
|
+
body = JSON.parse(raw || "{}");
|
|
1351
1491
|
} catch {
|
|
1352
1492
|
return json(res, 400, { error: "malformed body" });
|
|
1353
1493
|
}
|
|
@@ -1358,12 +1498,12 @@ function createHandler(opts) {
|
|
|
1358
1498
|
}
|
|
1359
1499
|
if (url.pathname === "/api/fs" && req.method === "GET") {
|
|
1360
1500
|
const raw = url.searchParams.get("path")?.trim();
|
|
1361
|
-
const dir = raw ?
|
|
1501
|
+
const dir = raw ? path6.resolve(raw) : homeDir();
|
|
1362
1502
|
if (!await browsable(dir)) return json(res, 403, { error: "outside the browsable roots" });
|
|
1363
1503
|
try {
|
|
1364
|
-
const found = await
|
|
1365
|
-
const entries = found.filter((e) => (e.isDirectory() || e.isSymbolicLink()) && !e.name.startsWith(".")).map((e) => ({ name: e.name, path:
|
|
1366
|
-
const parent =
|
|
1504
|
+
const found = await readdir3(dir, { withFileTypes: true });
|
|
1505
|
+
const entries = found.filter((e) => (e.isDirectory() || e.isSymbolicLink()) && !e.name.startsWith(".")).map((e) => ({ name: e.name, path: path6.join(dir, e.name) })).sort((a, b) => a.name.localeCompare(b.name));
|
|
1506
|
+
const parent = path6.dirname(dir);
|
|
1367
1507
|
const up = parent !== dir && await browsable(parent) ? parent : null;
|
|
1368
1508
|
return json(res, 200, { path: dir, parent: up, entries });
|
|
1369
1509
|
} catch {
|
|
@@ -1382,6 +1522,25 @@ function createHandler(opts) {
|
|
|
1382
1522
|
return json(res, 500, { error: "cannot read Claude Code transcripts" });
|
|
1383
1523
|
}
|
|
1384
1524
|
}
|
|
1525
|
+
if (url.pathname === "/api/upload" && req.method === "POST") {
|
|
1526
|
+
const store = opts.uploads;
|
|
1527
|
+
if (!store) return json(res, 503, { error: "uploads are not configured" });
|
|
1528
|
+
const declared = Number(req.headers["content-length"] ?? "0");
|
|
1529
|
+
if (declared > MAX_UPLOAD_BYTES) {
|
|
1530
|
+
return json(res, 413, { error: `images are limited to ${MAX_UPLOAD_BYTES >> 20}MB` });
|
|
1531
|
+
}
|
|
1532
|
+
const bytes = await readBytes(req, MAX_UPLOAD_BYTES);
|
|
1533
|
+
if (bytes === null) {
|
|
1534
|
+
return tooLarge(req, res, `images are limited to ${MAX_UPLOAD_BYTES >> 20}MB`);
|
|
1535
|
+
}
|
|
1536
|
+
if (bytes.length === 0) return json(res, 400, { error: "empty upload" });
|
|
1537
|
+
try {
|
|
1538
|
+
return json(res, 200, { path: await store.save(bytes) });
|
|
1539
|
+
} catch (err) {
|
|
1540
|
+
if (err instanceof NotAnImage) return json(res, 415, { error: err.message });
|
|
1541
|
+
return json(res, 500, { error: "cannot write the dropped image" });
|
|
1542
|
+
}
|
|
1543
|
+
}
|
|
1385
1544
|
if (url.pathname === "/api/sessions" && req.method === "GET") {
|
|
1386
1545
|
const sessions = pm.list().flatMap(
|
|
1387
1546
|
(p) => p.sessions.map((s) => ({ ...s, project: p.name }))
|
|
@@ -1392,15 +1551,15 @@ function createHandler(opts) {
|
|
|
1392
1551
|
}
|
|
1393
1552
|
async function serveStatic(res, pathname) {
|
|
1394
1553
|
const rel = pathname === "/" ? "index.html" : pathname.replace(/^\/+/, "");
|
|
1395
|
-
const file =
|
|
1396
|
-
if (file !== webRoot && !file.startsWith(webRoot +
|
|
1554
|
+
const file = path6.resolve(webRoot, rel);
|
|
1555
|
+
if (file !== webRoot && !file.startsWith(webRoot + path6.sep)) {
|
|
1397
1556
|
text(res, 403, "forbidden");
|
|
1398
1557
|
return;
|
|
1399
1558
|
}
|
|
1400
1559
|
try {
|
|
1401
|
-
const info = await
|
|
1560
|
+
const info = await stat3(file);
|
|
1402
1561
|
if (!info.isFile()) throw new Error("not a file");
|
|
1403
|
-
head(res, 200, MIME[
|
|
1562
|
+
head(res, 200, MIME[path6.extname(file)] ?? "application/octet-stream");
|
|
1404
1563
|
createReadStream(file).pipe(res);
|
|
1405
1564
|
} catch {
|
|
1406
1565
|
if (rel === "index.html") {
|
|
@@ -1617,6 +1776,7 @@ var Hub = class {
|
|
|
1617
1776
|
import { spawn as spawn3 } from "node:child_process";
|
|
1618
1777
|
import { existsSync, readFileSync as readFileSync2 } from "node:fs";
|
|
1619
1778
|
import os5 from "node:os";
|
|
1779
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
1620
1780
|
function isWsl() {
|
|
1621
1781
|
if (process.env["WSL_DISTRO_NAME"]) return true;
|
|
1622
1782
|
try {
|
|
@@ -1640,7 +1800,44 @@ function firstExisting(paths) {
|
|
|
1640
1800
|
for (const p of paths) if (existsSync(p)) return p;
|
|
1641
1801
|
return null;
|
|
1642
1802
|
}
|
|
1643
|
-
|
|
1803
|
+
var WINDOWS_CURL = "/mnt/c/Windows/System32/curl.exe";
|
|
1804
|
+
var RELAY_DEADLINE_MS = 8e3;
|
|
1805
|
+
var RELAY_GAP_MS = 150;
|
|
1806
|
+
function reachableFromWindows(origin) {
|
|
1807
|
+
return new Promise((resolve) => {
|
|
1808
|
+
const probe = spawn3(WINDOWS_CURL, [
|
|
1809
|
+
"-s",
|
|
1810
|
+
"-k",
|
|
1811
|
+
"-o",
|
|
1812
|
+
"NUL",
|
|
1813
|
+
"--max-time",
|
|
1814
|
+
"2",
|
|
1815
|
+
origin
|
|
1816
|
+
], { stdio: "ignore" });
|
|
1817
|
+
probe.on("close", (code) => resolve(code === 0 ? "reachable" : "refused"));
|
|
1818
|
+
probe.on("error", () => resolve("unusable"));
|
|
1819
|
+
});
|
|
1820
|
+
}
|
|
1821
|
+
async function awaitRelay(probe, opts = {}) {
|
|
1822
|
+
const deadline = Date.now() + (opts.deadlineMs ?? RELAY_DEADLINE_MS);
|
|
1823
|
+
for (; ; ) {
|
|
1824
|
+
const answer = await probe();
|
|
1825
|
+
if (answer !== "refused") return answer;
|
|
1826
|
+
if (Date.now() >= deadline) return "refused";
|
|
1827
|
+
await delay(opts.gapMs ?? RELAY_GAP_MS);
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
async function waitForWindowsRelay(url) {
|
|
1831
|
+
if (!existsSync(WINDOWS_CURL)) return;
|
|
1832
|
+
let origin;
|
|
1833
|
+
try {
|
|
1834
|
+
origin = new URL(url).origin;
|
|
1835
|
+
} catch {
|
|
1836
|
+
return;
|
|
1837
|
+
}
|
|
1838
|
+
await awaitRelay(() => reachableFromWindows(origin));
|
|
1839
|
+
}
|
|
1840
|
+
async function openWindow(url) {
|
|
1644
1841
|
if (process.env["TRING_NO_OPEN"]) return false;
|
|
1645
1842
|
const appFlag = `--app=${url}`;
|
|
1646
1843
|
let command;
|
|
@@ -1677,6 +1874,7 @@ function openWindow(url) {
|
|
|
1677
1874
|
command = exe;
|
|
1678
1875
|
args = [appFlag];
|
|
1679
1876
|
}
|
|
1877
|
+
if (isWsl()) await waitForWindowsRelay(url);
|
|
1680
1878
|
try {
|
|
1681
1879
|
const child = spawn3(command, args, { detached: true, stdio: "ignore" });
|
|
1682
1880
|
child.on("error", () => {
|
|
@@ -1691,6 +1889,59 @@ function describeFallback(url) {
|
|
|
1691
1889
|
return isWsl() || os5.platform() === "linux" ? `open ${url} in your browser` : `open ${url}`;
|
|
1692
1890
|
}
|
|
1693
1891
|
|
|
1892
|
+
// src/spawn-helper.ts
|
|
1893
|
+
import {
|
|
1894
|
+
closeSync,
|
|
1895
|
+
constants,
|
|
1896
|
+
fchmodSync,
|
|
1897
|
+
fstatSync,
|
|
1898
|
+
openSync,
|
|
1899
|
+
readdirSync
|
|
1900
|
+
} from "node:fs";
|
|
1901
|
+
import { createRequire as createRequire3 } from "node:module";
|
|
1902
|
+
import path7 from "node:path";
|
|
1903
|
+
var EXEC = 73;
|
|
1904
|
+
function nodePtyRoot() {
|
|
1905
|
+
try {
|
|
1906
|
+
return path7.dirname(createRequire3(import.meta.url).resolve("node-pty/package.json"));
|
|
1907
|
+
} catch {
|
|
1908
|
+
return null;
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
function spawnHelperPaths(root) {
|
|
1912
|
+
const found = [path7.join(root, "build", "Release", "spawn-helper")];
|
|
1913
|
+
let prebuilds;
|
|
1914
|
+
try {
|
|
1915
|
+
prebuilds = readdirSync(path7.join(root, "prebuilds"), { withFileTypes: true });
|
|
1916
|
+
} catch {
|
|
1917
|
+
return found;
|
|
1918
|
+
}
|
|
1919
|
+
for (const entry of prebuilds) {
|
|
1920
|
+
if (entry.isDirectory()) found.push(path7.join(root, "prebuilds", entry.name, "spawn-helper"));
|
|
1921
|
+
}
|
|
1922
|
+
return found;
|
|
1923
|
+
}
|
|
1924
|
+
function fixSpawnHelper(root = nodePtyRoot()) {
|
|
1925
|
+
if (process.platform === "win32" || root === null) return [];
|
|
1926
|
+
const fixed = [];
|
|
1927
|
+
for (const file of spawnHelperPaths(root)) {
|
|
1928
|
+
let fd = null;
|
|
1929
|
+
try {
|
|
1930
|
+
fd = openSync(file, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
1931
|
+
const info = fstatSync(fd);
|
|
1932
|
+
if (!info.isFile()) continue;
|
|
1933
|
+
const mode = info.mode & 511;
|
|
1934
|
+
if (mode & EXEC) continue;
|
|
1935
|
+
fchmodSync(fd, mode | EXEC);
|
|
1936
|
+
fixed.push(file);
|
|
1937
|
+
} catch {
|
|
1938
|
+
} finally {
|
|
1939
|
+
if (fd !== null) closeSync(fd);
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1942
|
+
return fixed;
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1694
1945
|
// src/index.ts
|
|
1695
1946
|
function resolveAuth(args) {
|
|
1696
1947
|
if (args.insecureNoToken) return { token: null, persisted: false };
|
|
@@ -1729,11 +1980,12 @@ async function main() {
|
|
|
1729
1980
|
const tls = args.tlsCert && args.tlsKey ? loadTls(args.tlsCert, args.tlsKey) : null;
|
|
1730
1981
|
const url = `${tls ? "https" : "http"}://${args.host}:${args.port}`;
|
|
1731
1982
|
const cleartextOffBox = !tls && !isLoopbackBind(args.host);
|
|
1732
|
-
const here =
|
|
1983
|
+
const here = path8.dirname(fileURLToPath(import.meta.url));
|
|
1733
1984
|
const webRoot = [
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
].find((p) => existsSync2(p)) ??
|
|
1985
|
+
path8.resolve(here, "web"),
|
|
1986
|
+
path8.resolve(here, "../../web/dist")
|
|
1987
|
+
].find((p) => existsSync2(p)) ?? path8.resolve(here, "web");
|
|
1988
|
+
for (const file of fixSpawnHelper()) console.log(`made ${file} executable`);
|
|
1737
1989
|
const pm = await ProjectManager.open({
|
|
1738
1990
|
url,
|
|
1739
1991
|
// Reaches each session as $TRING_TOKEN, so the documented Stop hook can
|
|
@@ -1744,12 +1996,14 @@ async function main() {
|
|
|
1744
1996
|
...args.shell ? { shell: args.shell } : {}
|
|
1745
1997
|
});
|
|
1746
1998
|
const sameOrigin = createOriginCheck({ host: args.host, allow: args.allowOrigin });
|
|
1999
|
+
const uploads = new UploadStore(path8.join(path8.dirname(pm.statePath), "uploads"));
|
|
1747
2000
|
const handle = createHandler({
|
|
1748
2001
|
pm,
|
|
1749
2002
|
webRoot,
|
|
1750
2003
|
token,
|
|
1751
2004
|
sameOrigin,
|
|
1752
|
-
fsRoots: args.fsRoot
|
|
2005
|
+
fsRoots: args.fsRoot,
|
|
2006
|
+
uploads
|
|
1753
2007
|
});
|
|
1754
2008
|
const listener = (req, res) => {
|
|
1755
2009
|
void handle(req, res).catch(() => {
|
|
@@ -1771,21 +2025,23 @@ async function main() {
|
|
|
1771
2025
|
console.warn(`warning: ${url} is plain http \u2014 the token, every keystroke and all terminal output cross the network in clear. Use an encrypted overlay (Tailscale, WireGuard) or pass --tls-cert/--tls-key.`);
|
|
1772
2026
|
}
|
|
1773
2027
|
if (args.updateCheck) {
|
|
1774
|
-
void checkForUpdate(
|
|
2028
|
+
void checkForUpdate(path8.join(path8.dirname(pm.statePath), "update-check.json")).then((latest) => {
|
|
1775
2029
|
if (!latest) return;
|
|
1776
2030
|
hub.setUpdate({ current: currentVersion(), latest });
|
|
1777
2031
|
console.log(`update available: ${currentVersion()} -> ${latest} (npm i -g tring-chat)`);
|
|
1778
2032
|
});
|
|
1779
2033
|
}
|
|
1780
2034
|
if (args.open) {
|
|
1781
|
-
|
|
1782
|
-
|
|
2035
|
+
void openWindow(authUrl).then((opened) => {
|
|
2036
|
+
if (!opened) console.log(`no browser found \u2014 ${describeFallback(authUrl)}`);
|
|
2037
|
+
});
|
|
1783
2038
|
} else if (token) {
|
|
1784
2039
|
console.log(`open ${authUrl}`);
|
|
1785
2040
|
}
|
|
1786
2041
|
});
|
|
1787
2042
|
const shutdown = async () => {
|
|
1788
2043
|
hub.dispose();
|
|
2044
|
+
await uploads.dispose();
|
|
1789
2045
|
await pm.dispose();
|
|
1790
2046
|
server.close(() => process.exit(0));
|
|
1791
2047
|
setTimeout(() => process.exit(0), 1e3).unref();
|