jsgotty 1.4.0 → 1.4.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/gotty.js CHANGED
@@ -16,6 +16,7 @@ const { TerminalModeTracker } = require("./terminal-modes.js");
16
16
  const { WebSocketServer } = WebSocket;
17
17
  const { ClipboardHost, handleOSC52 } = require("./clipboard.js");
18
18
  const clipboardHost = new ClipboardHost();
19
+ const { KittyFrameFlow } = require("./kitty-flow.js");
19
20
 
20
21
  const MSG_INPUT = "1";
21
22
  const MSG_PING = "2";
@@ -1856,7 +1857,8 @@ class PtySession {
1856
1857
  remoteAddr,
1857
1858
  encoding: "null",
1858
1859
  readOnly,
1859
- sendQueue: Promise.resolve()
1860
+ sendQueue: Promise.resolve(),
1861
+ kittyFlow: new KittyFrameFlow(graphic => this.queueSend(ws, MSG_KITTY_GRAPHICS + JSON.stringify(graphic)))
1860
1862
  });
1861
1863
  if (this.permitWrite && !readOnly && !this.writerWs) {
1862
1864
  this.writerWs = ws;
@@ -1919,6 +1921,7 @@ class PtySession {
1919
1921
  const writable = this.isWriter(ws);
1920
1922
  this.send(MSG_SET_ROLE, JSON.stringify({
1921
1923
  role: writable ? "writer" : "viewer",
1924
+ kittyFrameFlow: 1,
1922
1925
  writable
1923
1926
  }), ws);
1924
1927
  }
@@ -2452,6 +2455,9 @@ class PtySession {
2452
2455
  }
2453
2456
 
2454
2457
  switch (type) {
2458
+ case "G":
2459
+ try { client.kittyFlow.control(JSON.parse(payload)); } catch {}
2460
+ break;
2455
2461
  case "C":
2456
2462
  handleOSC52(this, ws, payload, clipboardHost);
2457
2463
  break;
@@ -2580,6 +2586,13 @@ class PtySession {
2580
2586
  return;
2581
2587
  }
2582
2588
 
2589
+ // Negotiate browser-only image credits; never throttle terminal byte output.
2590
+ if (!binary && client.kittyFlow.enabled && payload[0] === MSG_KITTY_GRAPHICS) {
2591
+ const graphic = JSON.parse(payload.slice(1));
2592
+ if (!graphic.frameSequence && client.kittyFlow.graphic(graphic)) return;
2593
+ }
2594
+ if (!binary && payload[0] === MSG_OUTPUT) client.kittyFlow.output(payload.slice(1));
2595
+
2583
2596
  const sendOne = async () => {
2584
2597
  await new Promise((resolve, reject) => {
2585
2598
  try {
package/kitty-flow.js ADDED
@@ -0,0 +1,40 @@
1
+ // Per browser connection. Only replaceable C=1 placements use frame credits;
2
+ // ordinary terminal output, scrolling placements and CLI clients stay ordered.
3
+ class KittyFrameFlow {
4
+ constructor(send) { this.send=send;this.enabled=false;this.visible=true;this.sequence=0;this.inFlight=new Set();this.pending=new Map();this.tail=''; }
5
+ control(message) {
6
+ if(message?.version!==1)return;
7
+ this.enabled=true;
8
+ if(typeof message.visible==='boolean')this.visible=message.visible;
9
+ if(Number.isSafeInteger(message.ack))this.inFlight.delete(message.ack);
10
+ this.flush();
11
+ }
12
+ graphic(graphic) {
13
+ if(!this.enabled)return false;
14
+ if(graphic.kind==='delete'){
15
+ const d=graphic.delete||{};
16
+ for(const [key,g] of this.pending)if(d.scope==='a'||d.scope==='A'||String(g.image.id)===String(d.imageId))this.pending.delete(key);
17
+ return false;
18
+ }
19
+ const c=graphic.control||{},id=c.i||c.I||graphic.image?.id;
20
+ if(graphic.kind!=='placement'||!graphic.image||!id)return false;
21
+ if(String(c.C)!=='1'){this.pending.delete(`${id}:${c.p||'0'}`);return false;}
22
+ const key=`${id}:${c.p||'0'}`;
23
+ // Preserve ordering among different placements when replacing an update.
24
+ this.pending.delete(key);this.pending.set(key,graphic);this.flush();return true;
25
+ }
26
+ output(payload) {
27
+ if(!this.enabled)return;
28
+ const text=this.tail+Buffer.from(payload,'base64').toString('utf8');
29
+ if(text.includes('\x1bc')||text.includes('\f')||/\x1b\[\?(?:1049|1047|47)[hl]/.test(text))this.pending.clear();
30
+ this.tail=text.slice(-32);
31
+ }
32
+ flush() {
33
+ while(this.enabled&&this.visible&&this.inFlight.size<2&&this.pending.size){
34
+ const [key,graphic]=this.pending.entries().next().value;this.pending.delete(key);
35
+ const sequence=++this.sequence;this.inFlight.add(sequence);
36
+ this.send({...graphic,frameSequence:sequence});
37
+ }
38
+ }
39
+ }
40
+ module.exports={KittyFrameFlow};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsgotty",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "Share your terminal as a web application: A JavaScript rewrite of GoTTY",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -1079,7 +1079,31 @@
1079
1079
  state.outputTail = text.slice(-64);
1080
1080
  }
1081
1081
 
1082
- function handleKittyMessage(payload) {
1082
+ // Only a server advertising this capability receives the private control.
1083
+ const frameFlowSockets = new WeakSet();
1084
+ function sendFrameControl(socket, fields) {
1085
+ if (socket?.readyState === 1 && frameFlowSockets.has(socket))
1086
+ socket.send("G" + JSON.stringify({version: 1, ...fields}));
1087
+ }
1088
+ function inspectFrameCapability(socket, data) {
1089
+ if (typeof data !== "string" || data[0] !== "a") return;
1090
+ try {
1091
+ if (JSON.parse(data.slice(1)).kittyFrameFlow !== 1 || frameFlowSockets.has(socket)) return;
1092
+ frameFlowSockets.add(socket);
1093
+ sendFrameControl(socket, {visible: !document.hidden});
1094
+ } catch {}
1095
+ }
1096
+ document.addEventListener("visibilitychange", () => {
1097
+ sendFrameControl(state.socket, {visible: !document.hidden});
1098
+ });
1099
+ function acknowledgeFrame(socket, sequence) {
1100
+ if (!Number.isSafeInteger(sequence)) return;
1101
+ // rAF stops while hidden and also bounds delivery if WebView is suspended
1102
+ // without a visibility event. Credits belong to their original connection.
1103
+ requestAnimationFrame(() => sendFrameControl(socket, {ack: sequence}));
1104
+ }
1105
+
1106
+ function handleKittyMessage(payload, socket) {
1083
1107
  const message = JSON.parse(payload);
1084
1108
  sendBrowserDebug("kitty-message", {
1085
1109
  kind: message.kind,
@@ -1091,7 +1115,9 @@
1091
1115
  // The server records this cursor exactly where the Kitty APC occurred.
1092
1116
  // Do not replace it with xterm's later cursor, which may already have
1093
1117
  // moved because of redraw or scrolling output queued after the APC.
1094
- placeImage(message);
1118
+ Promise.resolve(placeImage(message)).catch(error => {
1119
+ console.warn("Kitty image decode failed", error);
1120
+ }).finally(() => acknowledgeFrame(socket, message.frameSequence));
1095
1121
  return true;
1096
1122
  }
1097
1123
  if (message.kind === "delete") {
@@ -1119,9 +1145,10 @@
1119
1145
  const wrapped = function (event) {
1120
1146
  state.socket = this;
1121
1147
  watchTouchSocket(this);
1148
+ inspectFrameCapability(this, event.data);
1122
1149
  inspectTerminalOutput(event.data);
1123
1150
  if (typeof event.data === "string" && event.data[0] === MSG_KITTY) {
1124
- if (handleKittyMessage(event.data.slice(1))) {
1151
+ if (handleKittyMessage(event.data.slice(1), this)) {
1125
1152
  return;
1126
1153
  }
1127
1154
  }
@@ -1166,9 +1193,10 @@
1166
1193
  const wrapped = function (event) {
1167
1194
  state.socket = this;
1168
1195
  watchTouchSocket(this);
1196
+ inspectFrameCapability(this, event.data);
1169
1197
  inspectTerminalOutput(event.data);
1170
1198
  if (typeof event.data === "string" && event.data[0] === MSG_KITTY) {
1171
- if (handleKittyMessage(event.data.slice(1))) {
1199
+ if (handleKittyMessage(event.data.slice(1), this)) {
1172
1200
  return;
1173
1201
  }
1174
1202
  }
@@ -0,0 +1,43 @@
1
+ // CHROMIUM_HEADLESS=/usr/local/bin/chromium-headless-shell bun test tests/kitty-flow-browser.test.js
2
+ const test=require('node:test'),assert=require('node:assert/strict');
3
+ const fs=require('node:fs'),path=require('node:path'),http=require('node:http');
4
+ const {spawn}=require('node:child_process'),{once}=require('node:events');
5
+ const WebSocket=require('ws'),{KittyFrameFlow}=require('../kitty-flow.js');
6
+ test('real browser resumes latest image after visibility loss or suspended animation callbacks',{skip:!process.env.CHROMIUM_HEADLESS,timeout:30000},async()=>{
7
+ const source=fs.readFileSync(path.join(__dirname,'../static/js/kitty.js'));
8
+ const server=http.createServer((req,res)=>{
9
+ if(req.url==='/kitty.js'){res.setHeader('Content-Type','text/javascript');res.end(source);return;}
10
+ res.end(`<div id="terminal" style="width:800px;height:600px"><div class="xterm-screen"></div></div><div id="kitty-overlay"></div>
11
+ <script>window.hidden=false;Object.defineProperty(document,'hidden',{get:()=>window.hidden});window.held=[];window.suspended=false;const raf=window.requestAnimationFrame.bind(window);window.requestAnimationFrame=fn=>suspended?(held.push(fn),0):raf(fn);</script>
12
+ <script src="/kitty.js"></script><script>window.messages=[];window.socket=new WebSocket('ws://'+location.host);socket.onmessage=e=>messages.push(e.data);</script>`);
13
+ });server.listen(0,'127.0.0.1');await once(server,'listening');
14
+ const wss=new WebSocket.WebSocketServer({server});let flow,socket,count=0,last=null;
15
+ wss.on('connection',ws=>{socket=ws;flow=new KittyFrameFlow(g=>{count++;last=g;ws.send('7'+JSON.stringify(g));});ws.on('message',m=>{if(String(m)[0]==='G')flow.control(JSON.parse(String(m).slice(1)))});ws.send('a{"kittyFrameFlow":1}');});
16
+ const child=spawn(process.env.CHROMIUM_HEADLESS,['--no-sandbox','--disable-gpu','--remote-debugging-port=0','http://127.0.0.1:'+server.address().port],{stdio:['ignore','ignore','pipe']});let cdp;
17
+ try{
18
+ const endpoint=await new Promise((resolve,reject)=>{let log='';child.on('error',reject);child.stderr.on('data',b=>{log+=b;const m=log.match(/DevTools listening on (ws:\/\/[^\s]+)/);if(m)resolve(m[1]);});});
19
+ let tab;for(let i=0;i<100;i++){tab=(await(await fetch('http://'+new URL(endpoint).host+'/json/list')).json()).find(x=>x.type==='page');if(tab)break;await new Promise(r=>setTimeout(r,20));}
20
+ cdp=new WebSocket(tab.webSocketDebuggerUrl);await once(cdp,'open');let id=0;const pending=new Map();cdp.on('message',data=>{const m=JSON.parse(data),p=pending.get(m.id);if(p){pending.delete(m.id);m.error?p.reject(m.error):p.resolve(m.result)}});
21
+ const call=(method,params)=>new Promise((resolve,reject)=>{pending.set(++id,{resolve,reject});cdp.send(JSON.stringify({id,method,params}));});
22
+ const evaluate=async expression=>{const r=await call('Runtime.evaluate',{expression,returnByValue:true,awaitPromise:true});if(r.exceptionDetails)throw Error(JSON.stringify(r.exceptionDetails));return r.result.value;};
23
+ const until=async check=>{for(let i=0;i<100;i++){if(await check())return;await new Promise(r=>setTimeout(r,20));}throw Error('fixture timed out')};
24
+ await until(()=>flow?.enabled);
25
+ const png=await evaluate(`(()=>{const c=document.createElement('canvas');c.width=c.height=1;c.getContext('2d').fillRect(0,0,1,1);return c.toDataURL().split(',')[1]})()`);
26
+ const frame=n=>({kind:'placement',control:{i:'1',C:'1',c:'2',r:'2'},cursor:{row:1,col:1},image:{id:'1',format:100,data:png},testFrame:n});
27
+ flow.graphic(frame(0));await until(()=>flow.inFlight.size===0);
28
+ await evaluate('window.hidden=true;document.dispatchEvent(new Event("visibilitychange"))');await until(()=>flow.visible===false);
29
+ const before=count;for(let n=1;n<=1800;n++)flow.graphic(frame(n));assert.equal(count,before);assert.equal(flow.pending.size,1);
30
+ socket.send('1'+Buffer.from('TEXT_WHILE_HIDDEN').toString('base64'));
31
+ await until(async()=>await evaluate('messages.some(x=>x.startsWith("1"))'));
32
+ await evaluate('window.hidden=false;document.dispatchEvent(new Event("visibilitychange"))');await until(()=>flow.inFlight.size===0&&last.testFrame===1800);
33
+ assert.equal(count,before+1);
34
+ // A renderer can freeze without delivering visibilitychange. Missing rAF
35
+ // acknowledgements still bound the server to two outstanding frames.
36
+ await evaluate('window.suspended=true');const baseline=count;
37
+ for(let n=1801;n<=3600;n++)flow.graphic(frame(n));assert.equal(count,baseline+2);assert.equal(flow.pending.size,1);
38
+ await until(async()=>await evaluate('held.length>=2'));
39
+ await evaluate('window.suspended=false;for(const fn of held.splice(0))requestAnimationFrame(fn)');
40
+ await until(()=>flow.inFlight.size===0&&last.testFrame===3600);assert.equal(count,baseline+3);
41
+ await until(async()=>await evaluate('document.querySelector("img.kitty-image__content").naturalWidth===1'));
42
+ }finally{cdp?.close();for(const ws of wss.clients)ws.terminate();wss.close();server.close();child.kill();}
43
+ });
@@ -0,0 +1,55 @@
1
+ const {test}=require('node:test');
2
+ const assert=require('node:assert/strict');
3
+ const {KittyFrameFlow}=require('../kitty-flow.js');
4
+ const graphic=(n,id='1')=>({kind:'placement',control:{i:id,C:'1'},cursor:{row:2,col:1},image:{id,data:String(n)}});
5
+ test('browser credits bound suspension to two frames and latest replacement',()=>{
6
+ const sent=[],flow=new KittyFrameFlow(g=>sent.push(g));
7
+ assert.equal(flow.graphic(graphic(0)),false); // CLI and old WebViews unchanged
8
+ flow.control({version:1,visible:true});
9
+ for(let n=0;n<1800;n++)assert.equal(flow.graphic(graphic(n)),true);
10
+ assert.equal(sent.length,2);assert.equal(flow.pending.size,1);
11
+ flow.control({version:1,ack:9999});assert.equal(sent.length,2);
12
+ flow.control({version:1,ack:sent[0].frameSequence});
13
+ assert.equal(sent.length,3);assert.equal(sent[2].image.data,'1799');
14
+ flow.control({version:1,ack:sent[0].frameSequence});assert.equal(sent.length,3);
15
+ });
16
+ test('hidden viewers keep only latest per placement and resume without another update',()=>{
17
+ const sent=[],flow=new KittyFrameFlow(g=>sent.push(g));flow.control({version:1,visible:false});
18
+ for(let n=0;n<100;n++){flow.graphic(graphic(n,'1'));flow.graphic(graphic(n,'2'));}
19
+ assert.equal(sent.length,0);assert.equal(flow.pending.size,2);
20
+ flow.control({version:1,visible:true});assert.deepEqual(sent.map(g=>g.image.data),['99','99']);
21
+ });
22
+ test('delete, terminal reset and alternate screen do not resurrect pending images',()=>{
23
+ const sent=[],flow=new KittyFrameFlow(g=>sent.push(g));flow.control({version:1,visible:false});
24
+ flow.graphic(graphic(1));flow.graphic(graphic(2,'2'));
25
+ assert.equal(flow.graphic({kind:'delete',delete:{imageId:'1'}}),false);
26
+ assert.equal(flow.pending.size,1);
27
+ flow.output(Buffer.from('\x1b[?104').toString('base64'));
28
+ flow.output(Buffer.from('9l').toString('base64'));assert.equal(flow.pending.size,0);
29
+ flow.graphic(graphic(3));flow.graphic({kind:'delete',delete:{scope:'a'}});
30
+ flow.control({version:1,visible:true});assert.equal(sent.length,0);
31
+ });
32
+ test('connections have independent credits; normal scrolling images are unchanged',()=>{
33
+ const one=[],two=[],a=new KittyFrameFlow(g=>one.push(g)),b=new KittyFrameFlow(g=>two.push(g));
34
+ a.control({version:1,visible:false});b.control({version:1,visible:true});
35
+ a.graphic(graphic(1));b.graphic(graphic(1));assert.equal(one.length,0);assert.equal(two.length,1);
36
+ const scroll=graphic(2);delete scroll.control.C;assert.equal(a.graphic(scroll),false);
37
+ // A later ordinary placement replaces the same deferred position immediately.
38
+ assert.equal(a.pending.size,0);a.graphic(graphic(3));
39
+ a.output(Buffer.from('ordinary text').toString('base64'));assert.equal(a.pending.size,1);
40
+ });
41
+
42
+ test('browser negotiates only on advertisement and acknowledges after animation opportunity',async()=>{
43
+ const fs=require('node:fs'),vm=require('node:vm'),source=fs.readFileSync(require('node:path').join(__dirname,'../static/js/kitty.js'),'utf8');
44
+ const start=source.indexOf(' // Only a server advertising'),end=source.indexOf(' function deleteImages');
45
+ const helpers=source.slice(start,source.indexOf(' function handleKittyMessage',start));
46
+ const frames=[],events={},sent=[],socket={readyState:1,send:m=>sent.push(JSON.parse(m.slice(1)))};
47
+ const document={hidden:true,addEventListener:(k,fn)=>events[k]=fn};
48
+ const api=vm.runInNewContext(`(()=>{${helpers};return {inspectFrameCapability,acknowledgeFrame}})()`,{document,state:{socket},requestAnimationFrame:fn=>frames.push(fn)});
49
+ api.inspectFrameCapability(socket,'a{"role":"viewer"}');assert.equal(sent.length,0);
50
+ api.inspectFrameCapability(socket,'a{"kittyFrameFlow":1}');assert.equal(sent[0].visible,false);
51
+ api.acknowledgeFrame(socket,1);assert.equal(sent.length,1); // suspended rAF
52
+ document.hidden=false;events.visibilitychange();assert.equal(sent[1].visible,true);
53
+ frames.shift()();assert.equal(sent[2].ack,1);
54
+ socket.readyState=3;api.acknowledgeFrame(socket,2);frames.shift()();assert.equal(sent.length,3);
55
+ });
@@ -0,0 +1,18 @@
1
+ // bun tests/manual/fps-collector.mjs [log.jsonl]
2
+ import {appendFileSync} from 'node:fs';
3
+ import {resolve} from 'node:path';
4
+ const log=resolve(process.argv[2]||'jsgotty-fps.jsonl'),token=crypto.randomUUID();
5
+ const source=await Bun.file(new URL('./fps-debug.js',import.meta.url)).text();
6
+ let rows=0;
7
+ const headers={'Access-Control-Allow-Origin':'*','Access-Control-Allow-Methods':'POST, OPTIONS','Access-Control-Allow-Headers':'Content-Type','Access-Control-Allow-Private-Network':'true','Cache-Control':'no-store'};
8
+ const server=Bun.serve({hostname:'127.0.0.1',port:0,maxRequestBodySize:262144,async fetch(req){
9
+ if(new URL(req.url).pathname!=='/'+token)return new Response('',{status:404});
10
+ if(req.method==='OPTIONS')return new Response(null,{status:204,headers});
11
+ if(req.method!=='POST')return new Response('',{status:405,headers});
12
+ if(rows>=1200)return new Response('',{status:429,headers});
13
+ try{const row=await req.json();appendFileSync(log,JSON.stringify({receivedAt:Date.now(),...row})+'\n',{mode:0o600});rows++;return new Response(null,{status:204,headers});}
14
+ catch{return new Response('',{status:400,headers});}
15
+ }});
16
+ const endpoint=`http://127.0.0.1:${server.port}/${token}`;
17
+ const script=log+'.inject.js';await Bun.write(script,source+'\nwindow.startGottyFpsDebug({endpoint:'+JSON.stringify(endpoint)+'});\n');
18
+ console.log(JSON.stringify({endpoint,log,script,pid:process.pid}));
@@ -0,0 +1,31 @@
1
+ // Eval manually; counts completed Kitty <img> loads, not physical display flips.
2
+ window.startGottyFpsDebug = ({endpoint, duration=600000}={}) => {
3
+ window.stopGottyFpsDebug?.(); window.stopKittyFPS?.();
4
+ const box=document.createElement('div');
5
+ box.style.cssText='position:fixed;top:0;right:0;z-index:2147483647;background:#000b;color:cyan;padding:4px 8px;font:16px monospace;pointer-events:none';
6
+ box.textContent='Kitty: 0 FPS';document.body.append(box);
7
+ const session=Date.now().toString(36),ids=new WeakMap();let id=0,previous=0,started=performance.now(),last=started,frames=[],busy=false,dropped=0,sequence=0,stopped=false;
8
+ const records=[];window.__gottyFpsRecords=records;
9
+ const load=e=>{const el=e.target;if(!el.matches?.('img.kitty-image__content'))return;
10
+ const now=performance.now();if(!ids.has(el))ids.set(el,++id);
11
+ if(frames.length<2000)frames.push({t:now,id:ids.get(el),gap:previous?now-previous:null,w:el.naturalWidth,h:el.naturalHeight});else dropped++;
12
+ previous=now;
13
+ };
14
+ document.addEventListener('load',load,true);
15
+ const flush=()=>{
16
+ const now=performance.now(),batch=frames;frames=[];
17
+ const gaps=batch.map(f=>f.gap).filter(g=>g!==null).sort((a,b)=>a-b);
18
+ const row={session,sequence:sequence++,time:Date.now(),timeOrigin:performance.timeOrigin,elapsed:now-last,fps:batch.length*1000/(now-last),hidden:document.hidden,frames:batch,dropped,
19
+ p95:gaps[Math.floor(gaps.length*.95)]??null,p99:gaps[Math.floor(gaps.length*.99)]??null,max:gaps.at(-1)??null};last=now;
20
+ box.textContent=`Kitty: ${row.fps.toFixed(1)} FPS`;
21
+ records.push(row);if(records.length>600)records.shift();window.__gottyFpsLatest=row;
22
+ if(endpoint&&!busy){busy=true;fetch(endpoint,{method:'POST',headers:{'Content-Type':'text/plain'},body:JSON.stringify(row),signal:AbortSignal.timeout(5000)})
23
+ .then(r=>{if(!r.ok)throw Error('HTTP '+r.status);window.__gottyFpsDeliveryError=null;})
24
+ .catch(e=>{window.__gottyFpsDeliveryError=String(e);dropped+=batch.length;}).finally(()=>{busy=false;});}
25
+ else if(endpoint)dropped+=batch.length;
26
+ };
27
+ const timer=setInterval(flush,1000);
28
+ const expiry=setTimeout(()=>window.stopGottyFpsDebug?.(),Math.max(1000,Math.min(duration,600000)));
29
+ window.stopGottyFpsDebug=()=>{if(stopped)return;stopped=true;document.removeEventListener('load',load,true);clearInterval(timer);clearTimeout(expiry);box.remove();};
30
+ return 'Kitty FPS enabled (image loads; auto-stop in at most 10 minutes)';
31
+ };
@@ -0,0 +1,35 @@
1
+ (() => {
2
+ window.stopKittyFPS?.();
3
+
4
+ const box = document.createElement("div");
5
+ box.style.cssText =
6
+ "position:fixed;top:0;right:0;z-index:2147483647;" +
7
+ "background:#000b;color:cyan;padding:4px 8px;" +
8
+ "font:16px monospace;pointer-events:none";
9
+ box.textContent = "Kitty: 0 FPS";
10
+ document.body.append(box);
11
+
12
+ let count = 0, last = performance.now();
13
+ const loaded = e => {
14
+ if (e.target.matches?.("img.kitty-image__content"))
15
+ count++;
16
+ };
17
+ document.addEventListener("load", loaded, true);
18
+
19
+ const timer = setInterval(() => {
20
+ const now = performance.now();
21
+ box.textContent =
22
+ `Kitty: ${(count * 1000 / (now - last)).toFixed(1)}
23
+ FPS`;
24
+ count = 0;
25
+ last = now;
26
+ }, 1000);
27
+
28
+ window.stopKittyFPS = () => {
29
+ clearInterval(timer);
30
+ document.removeEventListener("load", loaded, true);
31
+ box.remove();
32
+ delete window.stopKittyFPS;
33
+ };
34
+ return "Kitty FPS enabled";
35
+ })()
@@ -0,0 +1,24 @@
1
+ // Eval manually. Counts delivery, bitmap decoding, image loads and animation ticks.
2
+ // Observational only: no frame dropping, focus changes or socket reconnects.
3
+ window.startKittyBackgroundDebug = (endpoint) => {
4
+ window.stopKittyBackgroundDebug?.();
5
+ const parse=JSON.parse,bitmap=window.createImageBitmap,history=[];
6
+ let received=0,bytes=0,decoded=0,loads=0,ticks=0,pending=0,maxPending=0,decodeMs=0,last=performance.now(),busy=false,stopped=false,raf;
7
+ const wrappedParse=function(...args){const v=parse.apply(this,args);if(v?.kind==='placement'&&v.image){received++;bytes+=v.image.data?.length||0;}return v;};
8
+ const wrappedBitmap=async function(...args){const start=performance.now();pending++;maxPending=Math.max(maxPending,pending);try{return await bitmap.apply(this,args)}finally{pending--;decoded++;decodeMs+=performance.now()-start;}};
9
+ JSON.parse=wrappedParse;if(bitmap)window.createImageBitmap=wrappedBitmap;
10
+ const load=e=>{if(e.target.matches?.('img.kitty-image__content'))loads++;};
11
+ document.addEventListener('load',load,true);
12
+ const tick=()=>{ticks++;if(!stopped)raf=requestAnimationFrame(tick)};raf=requestAnimationFrame(tick);
13
+ function flush(reason='interval'){
14
+ const now=performance.now(),row={kind:'background-profile',time:Date.now(),elapsed:now-last,reason,hidden:document.hidden,received,bytes,decoded,loads,ticks,pending,maxPending,decodeMs};
15
+ last=now;received=bytes=decoded=loads=ticks=decodeMs=0;maxPending=pending;
16
+ history.push(row);if(history.length>600)history.shift();window.__kittyBackgroundLatest=row;
17
+ if(endpoint&&!busy){busy=true;fetch(endpoint,{method:'POST',headers:{'Content-Type':'text/plain'},body:JSON.stringify(row),signal:AbortSignal.timeout(5000)}).catch(()=>{}).finally(()=>busy=false);}
18
+ }
19
+ const visibility=()=>flush('visibilitychange');document.addEventListener('visibilitychange',visibility);
20
+ const timer=setInterval(flush,1000),expiry=setTimeout(()=>window.stopKittyBackgroundDebug(),600000);
21
+ window.__kittyBackgroundHistory=history;
22
+ window.stopKittyBackgroundDebug=()=>{stopped=true;if(JSON.parse===wrappedParse)JSON.parse=parse;if(window.createImageBitmap===wrappedBitmap)window.createImageBitmap=bitmap;document.removeEventListener('load',load,true);document.removeEventListener('visibilitychange',visibility);cancelAnimationFrame(raf);clearInterval(timer);clearTimeout(expiry);};
23
+ return 'background diagnostic enabled';
24
+ };