jsgotty 1.1.11 → 1.1.13

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
@@ -1173,9 +1173,9 @@ class KittyGraphicsParser {
1173
1173
 
1174
1174
  if (control.m === "1") {
1175
1175
  this.transfers.set(key, existing);
1176
- if (!control.i && !control.I) {
1177
- this.implicitTransferKey = key;
1178
- }
1176
+ // Later chunks of a transfer only need m=; they continue the transfer
1177
+ // in progress whether or not its first chunk named an image id.
1178
+ this.implicitTransferKey = key;
1179
1179
  return null;
1180
1180
  }
1181
1181
 
@@ -3314,6 +3314,7 @@ async function bootstrap() {
3314
3314
 
3315
3315
  module.exports.CursorStateTracker = CursorStateTracker;
3316
3316
  module.exports.KittyGraphicsParser = KittyGraphicsParser;
3317
+ module.exports.BunPtyBackend = BunPtyBackend;
3317
3318
  module.exports.webSocketOriginAllowed = webSocketOriginAllowed;
3318
3319
 
3319
3320
  if (require.main === module) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsgotty",
3
- "version": "1.1.11",
3
+ "version": "1.1.13",
4
4
  "description": "Share your terminal as a web application: A JavaScript rewrite of GoTTY",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -20,9 +20,32 @@
20
20
  touchScroll: null,
21
21
  contextMenu: null,
22
22
  imageContextBound: false,
23
- socket: null
23
+ socket: null,
24
+ // False until the client has sent its first resize. Until then cols/rows
25
+ // above are placeholders, not the PTY's real geometry.
26
+ sizeFromClient: false
24
27
  };
25
28
 
29
+ // The PTY size only reaches us through the resize messages the client
30
+ // sends. A placement that arrives before the first of those would be laid
31
+ // out against 80x24 instead of the terminal the user is looking at, so
32
+ // borrow xterm's own geometry until the real size shows up.
33
+ function syncViewerSize() {
34
+ if (state.sizeFromClient) {
35
+ return;
36
+ }
37
+ const xterm = terminal && terminal.__gottyXterm;
38
+ if (!xterm) {
39
+ return;
40
+ }
41
+ if (Number.isFinite(xterm.cols) && xterm.cols > 0) {
42
+ state.cols = xterm.cols;
43
+ }
44
+ if (Number.isFinite(xterm.rows) && xterm.rows > 0) {
45
+ state.rows = xterm.rows;
46
+ }
47
+ }
48
+
26
49
  function browserTerminalState() {
27
50
  const xterm = terminal && terminal.__gottyXterm;
28
51
  const active = xterm && xterm.buffer && xterm.buffer.active;
@@ -152,6 +175,7 @@
152
175
  }
153
176
 
154
177
  function createPlacementMarker(cursor) {
178
+ syncViewerSize();
155
179
  const xterm = terminal && terminal.__gottyXterm;
156
180
  if (!xterm || typeof xterm.registerMarker !== "function") {
157
181
  return null;
@@ -372,6 +396,7 @@
372
396
  return;
373
397
  }
374
398
  const { control, cursor, intrinsicWidth, intrinsicHeight, sourceRect } = node.__kittyPlacement;
399
+ syncViewerSize();
375
400
  const metrics = cellMetrics();
376
401
  const anchor = anchorOffset();
377
402
  const controlCols = Math.max(0, Number.parseInt(control.c || "0", 10) || 0);
@@ -825,6 +850,17 @@
825
850
  const generation = (node.__kittyGeneration || 0) + 1;
826
851
  node.__kittyGeneration = generation;
827
852
 
853
+ // Bind before the decode, not after it. The server sends the placement
854
+ // ahead of the synthetic newlines that scroll room for the image, so the
855
+ // scroll this layout depends on can land while we are still decoding.
856
+ // Binding afterwards left the very first image of a session with no
857
+ // listener to correct it, which is why it could come out blank while
858
+ // every later image in the same session was fine.
859
+ bindViewportScroll();
860
+ bindTerminalRender();
861
+ bindTouchScrolling();
862
+ bindImageContextMenu();
863
+
828
864
  const asset = await resolveImageAsset(message.image, control);
829
865
  if (!asset || node.__kittyGeneration !== generation) {
830
866
  return;
@@ -839,11 +875,14 @@
839
875
  intrinsicHeight: asset.height,
840
876
  sourceRect: asset.sourceRect,
841
877
  };
842
- node.__kittyImageElement.src = objectUrl;
843
- bindViewportScroll();
844
- bindTerminalRender();
845
- bindTouchScrolling();
846
- bindImageContextMenu();
878
+ const imageElement = node.__kittyImageElement;
879
+ imageElement.onload = () => {
880
+ imageElement.onload = null;
881
+ if (node.__kittyGeneration === generation) {
882
+ layoutNode(node);
883
+ }
884
+ };
885
+ imageElement.src = objectUrl;
847
886
  layoutNode(node);
848
887
  }
849
888
 
@@ -995,7 +1034,14 @@
995
1034
  return nativeRemoveEventListener.call(this, type, listener, options);
996
1035
  };
997
1036
 
998
- const descriptor = Object.getOwnPropertyDescriptor(WebSocket.prototype, "onmessage");
1037
+ // Walk the prototype chain to find the native onmessage descriptor.
1038
+ // Safari/WebKit defines it on a parent prototype (e.g. EventTarget),
1039
+ // not directly on WebSocket.prototype, so a single-level lookup fails.
1040
+ let descriptor = null;
1041
+ for (let proto = WebSocket.prototype; proto; proto = Object.getPrototypeOf(proto)) {
1042
+ descriptor = Object.getOwnPropertyDescriptor(proto, "onmessage");
1043
+ if (descriptor) break;
1044
+ }
999
1045
  Object.defineProperty(WebSocket.prototype, "onmessage", {
1000
1046
  configurable: true,
1001
1047
  enumerable: descriptor ? descriptor.enumerable : true,
@@ -1004,14 +1050,13 @@
1004
1050
  },
1005
1051
  set(handler) {
1006
1052
  this.__kittyOnMessage = handler;
1007
- if (!descriptor || !descriptor.set) {
1008
- return;
1009
- }
1010
1053
  if (typeof handler !== "function") {
1011
- descriptor.set.call(this, handler);
1054
+ if (descriptor && descriptor.set) {
1055
+ descriptor.set.call(this, handler);
1056
+ }
1012
1057
  return;
1013
1058
  }
1014
- descriptor.set.call(this, function (event) {
1059
+ const wrapped = function (event) {
1015
1060
  state.socket = this;
1016
1061
  inspectTerminalOutput(event.data);
1017
1062
  if (typeof event.data === "string" && event.data[0] === MSG_KITTY) {
@@ -1020,7 +1065,13 @@
1020
1065
  }
1021
1066
  }
1022
1067
  return handler.call(this, event);
1023
- });
1068
+ };
1069
+ if (descriptor && descriptor.set) {
1070
+ descriptor.set.call(this, wrapped);
1071
+ } else {
1072
+ // Fallback: use addEventListener when no native setter is available.
1073
+ this.addEventListener("message", wrapped);
1074
+ }
1024
1075
  }
1025
1076
  });
1026
1077
 
@@ -1034,9 +1085,11 @@
1034
1085
  const resize = JSON.parse(data.slice(1));
1035
1086
  if (resize.columns) {
1036
1087
  state.cols = resize.columns;
1088
+ state.sizeFromClient = true;
1037
1089
  }
1038
1090
  if (resize.rows) {
1039
1091
  state.rows = resize.rows;
1092
+ state.sizeFromClient = true;
1040
1093
  }
1041
1094
  } catch (error) {
1042
1095
  // ignore
package/viu.mjs CHANGED
@@ -27,6 +27,34 @@ function usage(exitCode = 0) {
27
27
  process.exitCode = exitCode;
28
28
  }
29
29
 
30
+ // A browser terminal only reports its size to the PTY once the page has laid
31
+ // out, so a window that was just opened, restored, resized or rotated answers
32
+ // with a couple of columns for a few hundred milliseconds. An image sized
33
+ // against that is emitted as a one-cell speck — indistinguishable from the
34
+ // command having printed nothing — while the next run in the same session,
35
+ // by which time the real size has arrived, comes out correct. Give the size a
36
+ // short chance to settle when what we read back is too small to be a terminal
37
+ // anyone could be looking at.
38
+ const MIN_SANE_COLS = 20;
39
+ const MIN_SANE_ROWS = 5;
40
+ const SIZE_SETTLE_TIMEOUT_MS = 400;
41
+ const SIZE_POLL_INTERVAL_MS = 25;
42
+
43
+ function terminalSizeLooksUnset() {
44
+ return (process.stdout.columns || 0) < MIN_SANE_COLS
45
+ || (process.stdout.rows || 0) < MIN_SANE_ROWS;
46
+ }
47
+
48
+ async function waitForStableTerminalSize() {
49
+ if (!process.stdout.isTTY) {
50
+ return;
51
+ }
52
+ const deadline = Date.now() + SIZE_SETTLE_TIMEOUT_MS;
53
+ while (terminalSizeLooksUnset() && Date.now() < deadline) {
54
+ await new Promise((resolve) => setTimeout(resolve, SIZE_POLL_INTERVAL_MS));
55
+ }
56
+ }
57
+
30
58
  function fitImageCells(width, height) {
31
59
  const terminalCols = Math.max(1, process.stdout.columns || 80);
32
60
  const maxCols = Math.max(1, terminalCols - 1);
@@ -384,6 +412,7 @@ async function main() {
384
412
  const fullPath = path.resolve(inputPath);
385
413
  const buffer = fs.readFileSync(fullPath);
386
414
  const image = sniffImage(buffer, fullPath, { compat });
415
+ await waitForStableTerminalSize();
387
416
  writeKittyImage(buffer, image.mime, image.width, image.height, compat);
388
417
  }
389
418