jsgotty 1.1.1 → 1.1.4

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/gotty.js CHANGED
@@ -33,10 +33,36 @@ const MSG_CONTROL_RESULT = "b";
33
33
  const MSG_ZMODEM_MODE = "z";
34
34
  const MSG_ZMODEM_OUTPUT = "y";
35
35
  const MSG_ZMODEM_ACK = "Y";
36
- const KITTY_TRACE = process.env.KITTY_TRACE === "1";
36
+ const MSG_KITTY_DEBUG = "K";
37
+ const KITTY_TRACE = /^(1|true|yes|on)$/i.test(String(process.env.JSGOTTY_KITTY_DEBUG || ""));
37
38
  const WINDOWS_BRIDGE_TRACE = process.env.WINDOWS_BRIDGE_TRACE === "1";
38
39
  const WINDOWS_BRIDGE_PREFIX = Buffer.from("@@GOTTYCTL:", "ascii");
39
40
  const WINDOWS_BRIDGE_SUFFIX = Buffer.from("@@", "ascii");
41
+ const KITTY_PLACEMENT_LOG = path.resolve(__dirname, "kitty-placement.log");
42
+
43
+ function kittyPlacementLog(stage, details = {}) {
44
+ if (!KITTY_TRACE) {
45
+ return;
46
+ }
47
+ try {
48
+ fs.appendFileSync(KITTY_PLACEMENT_LOG, JSON.stringify({
49
+ time: new Date().toISOString(),
50
+ pid: process.pid,
51
+ stage,
52
+ ...details,
53
+ }) + "\n");
54
+ } catch (error) {
55
+ // Placement diagnostics must never affect terminal output.
56
+ }
57
+ }
58
+
59
+ function cursorControls(buffer) {
60
+ const text = Buffer.from(buffer).toString("utf8");
61
+ return Array.from(text.matchAll(/\u001b\[([0-9;?]*)([A-Za-z])/g), (match) => ({
62
+ params: match[1],
63
+ final: match[2],
64
+ })).filter((entry) => "ABCDEFGHfJKhl".includes(entry.final));
65
+ }
40
66
 
41
67
  const DEFAULTS = {
42
68
  address: "0.0.0.0",
@@ -168,8 +194,8 @@ Options:
168
194
  --title-format <value> Title format
169
195
  (default: {{ .command }}@{{ .hostname }} )
170
196
 
171
- --reconnect Enable reconnecting to the original TTY
172
- Useful when the connection is temporarily down
197
+ --reconnect, --session Enable session mode
198
+ Keep the PTY session resumable after the browser disconnects
173
199
  (default: disabled)
174
200
  --reconnect-time <value> Time to preserve original TTY (seconds)
175
201
  (default: -1 (forever) )
@@ -539,6 +565,7 @@ function parseArgs(argv) {
539
565
  options.titleFormat = next();
540
566
  break;
541
567
  case "--reconnect":
568
+ case "--session":
542
569
  options.reconnect = true;
543
570
  break;
544
571
  case "--reconnect-time":
@@ -973,11 +1000,10 @@ class KittyGraphicsParser {
973
1000
  ));
974
1001
  }
975
1002
 
976
- consume(raw, cursor) {
1003
+ consume(raw) {
977
1004
  const data = this.pending.length > 0 ? Buffer.concat([this.pending, raw]) : raw;
978
1005
  this.pending = Buffer.alloc(0);
979
- const plain = [];
980
- const graphics = [];
1006
+ const events = [];
981
1007
  let offset = 0;
982
1008
  let plainStart = 0;
983
1009
 
@@ -985,19 +1011,19 @@ class KittyGraphicsParser {
985
1011
  if (data[offset] === 0x1b && offset + 2 < data.length && data[offset + 1] === 0x5f && data[offset + 2] === 0x47) {
986
1012
  const commandEnd = this._findApcEnd(data, offset + 3);
987
1013
  if (commandEnd === -1) {
1014
+ if (plainStart < offset) {
1015
+ events.push({ kind: "plain", data: data.subarray(plainStart, offset) });
1016
+ }
988
1017
  this.pending = data.subarray(offset);
989
1018
  break;
990
1019
  }
991
1020
 
992
1021
  if (plainStart < offset) {
993
- plain.push(data.subarray(plainStart, offset));
1022
+ events.push({ kind: "plain", data: data.subarray(plainStart, offset) });
994
1023
  }
995
1024
 
996
1025
  const packet = data.subarray(offset + 3, commandEnd);
997
- const parsed = this._parsePacket(packet, cursor);
998
- if (parsed) {
999
- graphics.push(parsed);
1000
- }
1026
+ events.push({ kind: "kitty", packet });
1001
1027
 
1002
1028
  offset = commandEnd + 2;
1003
1029
  plainStart = offset;
@@ -1007,10 +1033,14 @@ class KittyGraphicsParser {
1007
1033
  }
1008
1034
 
1009
1035
  if (plainStart < data.length && this.pending.length === 0) {
1010
- plain.push(data.subarray(plainStart));
1036
+ events.push({ kind: "plain", data: data.subarray(plainStart) });
1011
1037
  }
1012
1038
 
1013
- return { plain, graphics };
1039
+ return { events };
1040
+ }
1041
+
1042
+ parsePacket(packet, cursor) {
1043
+ return this._parsePacket(packet, cursor);
1014
1044
  }
1015
1045
 
1016
1046
  acknowledge(graphic) {
@@ -1093,11 +1123,18 @@ class KittyGraphicsParser {
1093
1123
  }
1094
1124
 
1095
1125
  if (action === "d") {
1126
+ const deleteScope = control.d || "a";
1127
+ const deleteImageId = control.i || control.I || null;
1128
+ if (deleteScope === "A") {
1129
+ this.images.clear();
1130
+ } else if (deleteScope === "I" && deleteImageId) {
1131
+ this.images.delete(deleteImageId);
1132
+ }
1096
1133
  return {
1097
1134
  kind: "delete",
1098
1135
  delete: {
1099
- scope: control.d || "a",
1100
- imageId: control.i || control.I || null,
1136
+ scope: deleteScope,
1137
+ imageId: deleteImageId,
1101
1138
  placementId: control.p || null,
1102
1139
  cursor
1103
1140
  }
@@ -1169,6 +1206,7 @@ class KittyGraphicsParser {
1169
1206
  format: Number.parseInt(existing.control.f || "32", 10),
1170
1207
  width: Number.parseInt(existing.control.s || "0", 10),
1171
1208
  height: Number.parseInt(existing.control.v || "0", 10),
1209
+ mime: existing.control.U || existing.control.mime || null,
1172
1210
  data: binary.toString("base64")
1173
1211
  };
1174
1212
 
@@ -1642,49 +1680,84 @@ class PtySession {
1642
1680
  this.sendChunk(MSG_OUTPUT, raw);
1643
1681
  return;
1644
1682
  }
1645
- const cursorBefore = this.cursorTracker.snapshot();
1646
- const parsed = this.kittyParser.consume(raw, cursorBefore);
1647
- if (parsed.graphics.length > 0) {
1648
- traceLog("graphics", parsed.graphics.map((graphic) => ({
1649
- kind: graphic.kind,
1650
- control: graphic.control || null,
1651
- imageId: graphic.image ? graphic.image.id : null
1652
- })));
1653
- }
1654
- for (const chunk of parsed.plain) {
1683
+ const parsed = this.kittyParser.consume(raw);
1684
+ for (const event of parsed.events) {
1685
+ if (event.kind === "kitty") {
1686
+ const cursorAtApc = this.cursorTracker.snapshot();
1687
+ const graphic = this.kittyParser.parsePacket(event.packet, cursorAtApc);
1688
+ if (!graphic) {
1689
+ continue;
1690
+ }
1691
+ kittyPlacementLog("server-kitty-event", {
1692
+ cursorAtApc,
1693
+ kind: graphic.kind,
1694
+ control: graphic.control || null,
1695
+ delete: graphic.delete || null,
1696
+ imageId: graphic.image?.id || null,
1697
+ });
1698
+ traceLog("graphics", JSON.stringify({
1699
+ kind: graphic.kind,
1700
+ control: graphic.control || null,
1701
+ imageId: graphic.image ? graphic.image.id : null
1702
+ }));
1703
+ if (graphic.kind === "placement") {
1704
+ this.cursorTracker.applyKittyPlacement(graphic.control);
1705
+ }
1706
+ this.send(MSG_KITTY_GRAPHICS, JSON.stringify(graphic));
1707
+ const syntheticCursorMotion = this.buildKittyCursorMotion(graphic);
1708
+ if (syntheticCursorMotion) {
1709
+ this.sendChunk(MSG_OUTPUT, Buffer.from(syntheticCursorMotion, "utf8"));
1710
+ }
1711
+ const ack = this.kittyParser.acknowledge(graphic);
1712
+ if (ack) {
1713
+ traceLog("graphics-ack", JSON.stringify(graphic.control || {}), JSON.stringify(ack));
1714
+ this.backend.write(Buffer.from(ack, "utf8"));
1715
+ }
1716
+ continue;
1717
+ }
1718
+
1719
+ const chunk = event.data;
1655
1720
  const filteredChunk = this.filterEchoedTerminalReplies(chunk);
1656
1721
  if (filteredChunk.length === 0) {
1657
1722
  continue;
1658
1723
  }
1659
1724
  if (!this.supportsWindowsBridge) {
1725
+ const cursorBefore = this.cursorTracker.snapshot();
1660
1726
  this.cursorTracker.consume(filteredChunk);
1727
+ const cursorAfter = this.cursorTracker.snapshot();
1728
+ const controls = cursorControls(filteredChunk);
1729
+ if (controls.length > 0) {
1730
+ kittyPlacementLog("server-plain-cursor-controls", {
1731
+ bytes: filteredChunk.length,
1732
+ cursorBefore,
1733
+ cursorAfter,
1734
+ controls,
1735
+ });
1736
+ }
1661
1737
  this.sendChunk(MSG_OUTPUT, filteredChunk);
1662
1738
  continue;
1663
1739
  }
1664
1740
  const bridged = this.windowsBridgeParser.consume(filteredChunk);
1665
1741
  if (bridged.plain.length > 0) {
1742
+ const cursorBefore = this.cursorTracker.snapshot();
1666
1743
  this.cursorTracker.consume(bridged.plain);
1744
+ const cursorAfter = this.cursorTracker.snapshot();
1745
+ const controls = cursorControls(bridged.plain);
1746
+ if (controls.length > 0) {
1747
+ kittyPlacementLog("server-plain-cursor-controls", {
1748
+ bytes: bridged.plain.length,
1749
+ cursorBefore,
1750
+ cursorAfter,
1751
+ controls,
1752
+ windowsBridge: true,
1753
+ });
1754
+ }
1667
1755
  this.sendChunk(MSG_OUTPUT, bridged.plain);
1668
1756
  }
1669
1757
  for (const control of bridged.controls) {
1670
1758
  this.handleWindowsBridgeControl(control);
1671
1759
  }
1672
1760
  }
1673
- for (const graphic of parsed.graphics) {
1674
- if (graphic.kind === "placement") {
1675
- this.cursorTracker.applyKittyPlacement(graphic.control);
1676
- }
1677
- this.send(MSG_KITTY_GRAPHICS, JSON.stringify(graphic));
1678
- const syntheticCursorMotion = this.buildKittyCursorMotion(graphic);
1679
- if (syntheticCursorMotion) {
1680
- this.sendChunk(MSG_OUTPUT, Buffer.from(syntheticCursorMotion, "utf8"));
1681
- }
1682
- const ack = this.kittyParser.acknowledge(graphic);
1683
- if (ack) {
1684
- traceLog("graphics-ack", JSON.stringify(graphic.control || {}), JSON.stringify(ack));
1685
- this.backend.write(Buffer.from(ack, "utf8"));
1686
- }
1687
- }
1688
1761
  });
1689
1762
 
1690
1763
  this.backend.onExit(() => {
@@ -2353,6 +2426,16 @@ class PtySession {
2353
2426
  case MSG_PING:
2354
2427
  this.send(MSG_PONG, "", ws);
2355
2428
  break;
2429
+ case MSG_KITTY_DEBUG:
2430
+ if (!KITTY_TRACE) {
2431
+ break;
2432
+ }
2433
+ try {
2434
+ kittyPlacementLog("browser", JSON.parse(payload));
2435
+ } catch (error) {
2436
+ kittyPlacementLog("browser-malformed", { payload });
2437
+ }
2438
+ break;
2356
2439
  case MSG_WINDOWS_BRIDGE: {
2357
2440
  if (!this.supportsWindowsBridge || !this.isWriter(ws) || payload.length === 0) {
2358
2441
  return;
@@ -3226,7 +3309,17 @@ async function bootstrap() {
3226
3309
  }
3227
3310
  }
3228
3311
 
3229
- bootstrap().catch((error) => {
3230
- console.error(String(error && error.stack ? error.stack : error));
3231
- process.exitCode = 1;
3232
- });
3312
+ module.exports.CursorStateTracker = CursorStateTracker;
3313
+ module.exports.KittyGraphicsParser = KittyGraphicsParser;
3314
+
3315
+ if (require.main === module) {
3316
+ if (KITTY_TRACE) {
3317
+ try {
3318
+ fs.writeFileSync(KITTY_PLACEMENT_LOG, "");
3319
+ } catch {}
3320
+ }
3321
+ bootstrap().catch((error) => {
3322
+ console.error(String(error && error.stack ? error.stack : error));
3323
+ process.exitCode = 1;
3324
+ });
3325
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsgotty",
3
- "version": "1.1.1",
3
+ "version": "1.1.4",
4
4
  "description": "Share your terminal as a web application: A JavaScript rewrite of GoTTY",
5
5
  "bin": {
6
6
  "jsgotty": "./gotty.js"
@@ -11,6 +11,13 @@
11
11
  }
12
12
 
13
13
  #kitty-overlay .kitty-image {
14
+ position: absolute;
15
+ display: block;
16
+ overflow: hidden;
17
+ pointer-events: none;
18
+ }
19
+
20
+ #kitty-overlay .kitty-image__content {
14
21
  position: absolute;
15
22
  display: block;
16
23
  max-width: none;
package/static/help.md CHANGED
@@ -89,7 +89,7 @@ Useful methods/properties:
89
89
 
90
90
  - `window.getTerminalText`: helper exposed by `GoTTYXterm` for debugging/copying the full visible buffer as plain text.
91
91
  - `window.Zmodem`: ZMODEM helper namespace exposed by `zmodem.js`. Useful methods are used by the file-transfer addon, especially `Browser.send_files(...)` and `Browser.save_to_disk(...)`.
92
- - `window.gotty_kitty_trace`: boolean trace flag consumed by `kitty.js`. When true, kitty-specific debug logging is enabled.
92
+ - `window.gotty_kitty_trace`: boolean trace flag consumed by `kitty.js`. It is enabled by starting jsgotty with `JSGOTTY_KITTY_DEBUG=1`.
93
93
  - `window.touchScrollThreshold`: mobile touch-scroll threshold in pixels used by `kitty.js` to convert finger movement into synthetic wheel ticks. Default: 24 px. Lower means faster scrolling.
94
94
  - `alert_advanced(text, asHtml, closeOnClick)`: page-local modal helper used by custom UI scripts.
95
95
  - `noWinOpenUseFetch()`: intentional monkey patch for link handling.