From fe40d5c15df3b34247436d94c2b207577b7324d4 Mon Sep 17 00:00:00 2001 From: Brendan Date: Thu, 17 Sep 2026 00:12:44 +0000 Subject: [PATCH] Fix run detail browser helper --- packages/app/src/ansi.ts | 17 +++++++++++++++++ packages/app/src/ui.ts | 4 ++-- packages/app/test/ansi.test.ts | 9 ++++++++- packages/app/test/ui.test.ts | 1 + 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/app/src/ansi.ts b/packages/app/src/ansi.ts index c1e5fc90..f1bc990d 100644 --- a/packages/app/src/ansi.ts +++ b/packages/app/src/ansi.ts @@ -71,3 +71,20 @@ export function ansiToHtml(input: unknown): string { flush(); return html; } + +/** + * Browser-safe source for the run detail page. + * + * Do not derive this with `ansiToHtml.toString()`: Wrangler/esbuild decorates + * named functions with its private `__name` helper, which is not present in + * the HTML page where the serialized function runs. + */ +export const ansiToHtmlBrowserSource = String.raw`const ansiToHtml=input=>{ + const palette=['#0f172a','#ef4444','#22c55e','#eab308','#3b82f6','#d946ef','#06b6d4','#e2e8f0','#64748b','#f87171','#4ade80','#facc15','#60a5fa','#e879f9','#22d3ee','#f8fafc']; + const escape=value=>value.replace(/[&<>"']/g,char=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[char])); + const color=value=>{if(value<16)return palette[value];if(value<232){const offset=value-16,levels=[0,95,135,175,215,255];return 'rgb('+levels[Math.floor(offset/36)]+','+levels[Math.floor(offset/6)%6]+','+levels[offset%6]+')'}const gray=8+(value-232)*10;return 'rgb('+gray+','+gray+','+gray+')'}; + let style={},text='',html=''; + const flush=()=>{if(!text)return;let fg=style.fg,bg=style.bg;if(style.inverse){const originalFg=fg??'#e2e8f0';fg=bg??'#020617';bg=originalFg}const css=[fg&&'color:'+fg,bg&&'background-color:'+bg,style.bold&&'font-weight:700',style.dim&&'opacity:.65',style.italic&&'font-style:italic',style.underline&&'text-decoration:underline',style.strike&&'text-decoration:line-through',style.hidden&&'visibility:hidden'].filter(Boolean).join(';');html+=css?''+escape(text)+'':escape(text);text=''}; + const apply=parameters=>{const codes=parameters===''?[0]:parameters.split(';').map(value=>/^\d+$/.test(value)?Number(value):-1);for(let index=0;index=30&&code<=37)style.fg=color(code-30);else if(code>=40&&code<=47)style.bg=color(code-40);else if(code>=90&&code<=97)style.fg=color(code-90+8);else if(code>=100&&code<=107)style.bg=color(code-100+8);else if(code===39)delete style.fg;else if(code===49)delete style.bg;else if((code===38||code===48)&&codes[index+1]===5&&codes[index+2]>=0&&codes[index+2]<=255){style[code===38?'fg':'bg']=color(codes[index+2]);index+=2}else if((code===38||code===48)&&codes[index+1]===2&&codes.slice(index+2,index+5).every(value=>value>=0&&value<=255)){style[code===38?'fg':'bg']='rgb('+codes[index+2]+','+codes[index+3]+','+codes[index+4]+')';index+=4}}}; + const value=String(input??'');for(let index=0;index=value.length)break;if(value[end]==='m'&&/^[\d;]*$/.test(value.slice(index+2,end)))apply(value.slice(index+2,end));index=end+1}else if(value[index+1]===']'){let end=index+2;while(end value.replace(/[&<>"']/g, (char) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[char]!); @@ -48,7 +48,7 @@ const dashboardHead = (title: string, description: string, action = "") => `
document.querySelector(s),esc=v=>String(v??'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c])); -${ansiToHtml.toString()} +${ansiToHtmlBrowserSource} const request=async(url,options)=>{const r=await fetch(url,options),body=await r.json().catch(()=>({}));if(r.status===401){location.href='/auth/linear';throw new Error('Unauthorized')}if(!r.ok)throw new Error(body.error?.message||body.error||'Request failed');return body}; const pairs=v=>Object.entries(v||{}).map(([k,x])=>k+'='+x).join('\\n'); const record=v=>Object.fromEntries(String(v||'').split(/\\n/).map(x=>x.trim()).filter(Boolean).map(x=>{const i=x.indexOf('=');if(i<1)throw new Error('Parameters must use name=value, one per line.');return[x.slice(0,i).trim(),x.slice(i+1)]})); diff --git a/packages/app/test/ansi.test.ts b/packages/app/test/ansi.test.ts index 0262cdd9..5edfb82f 100644 --- a/packages/app/test/ansi.test.ts +++ b/packages/app/test/ansi.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { ansiToHtml } from "../src/ansi"; +import { ansiToHtml, ansiToHtmlBrowserSource } from "../src/ansi"; describe("ansiToHtml", () => { it("renders standard, bright, indexed, and true-color SGR colors", () => { @@ -32,4 +32,11 @@ describe("ansiToHtml", () => { it("leaves plain text unchanged apart from required HTML escaping", () => { expect(ansiToHtml("plain\n output & more")).toBe("plain\n output & more"); }); + + it("ships standalone browser source without bundler runtime helpers", () => { + const browserAnsiToHtml = Function(`${ansiToHtmlBrowserSource}; return ansiToHtml`)() as (input: unknown) => string; + const sample = '\u001b[1;38;2;1;2;3mstyled\u001b[0m'; + expect(ansiToHtmlBrowserSource).not.toContain("__name"); + expect(browserAnsiToHtml(sample)).toBe(ansiToHtml(sample)); + }); }); diff --git a/packages/app/test/ui.test.ts b/packages/app/test/ui.test.ts index 9f58c2bf..105b2cad 100644 --- a/packages/app/test/ui.test.ts +++ b/packages/app/test/ui.test.ts @@ -232,6 +232,7 @@ describe("pages", () => { expect(run).toContain('data-run-output'); expect(run).toContain('font-mono text-xs leading-5'); expect(run).toContain("ansiToHtml(r.result"); + expect(run).not.toContain("__name"); expect(settings).toContain("Connect every provider used by your Jobs"); expect(settings).toContain("Linear"); expect(settings).toContain("GitHub App");