Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/app/src/ansi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[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?'<span style="'+css+'">'+escape(text)+'</span>':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<codes.length;index++){const code=codes[index];if(code===0)style={};else if(code===1)style.bold=true;else if(code===2)style.dim=true;else if(code===3)style.italic=true;else if(code===4)style.underline=true;else if(code===7)style.inverse=true;else if(code===8)style.hidden=true;else if(code===9)style.strike=true;else if(code===22){style.bold=false;style.dim=false}else if(code===23)style.italic=false;else if(code===24)style.underline=false;else if(code===27)style.inverse=false;else if(code===28)style.hidden=false;else if(code===29)style.strike=false;else if(code>=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;){if(value[index]!=='\u001b'){text+=value[index++];continue}flush();if(value[index+1]==='['){let end=index+2;while(end<value.length&&!/[\x40-\x7e]/.test(value[end]))end++;if(end>=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.length&&value[end]!=='\u0007'&&!(value[end]==='\u001b'&&value[end+1]==='\\'))end++;index=end<value.length?end+(value[end]==='\u001b'?2:1):value.length}else index+=Math.min(2,value.length-index)}flush();return html
};`;
4 changes: 2 additions & 2 deletions packages/app/src/ui.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** Framework-light pages; Tailwind is compiled locally into public/styles.css. */
import { triggerContextCatalog } from "./trigger-context";
import { ansiToHtml } from "./ansi";
import { ansiToHtmlBrowserSource } from "./ansi";
type Viewer = { email: string } | null;
const escapeHtml = (value: string) => value.replace(/[&<>"']/g, (char) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[char]!);

Expand Down Expand Up @@ -48,7 +48,7 @@ const dashboardHead = (title: string, description: string, action = "") => `<div
const card = "rounded-2xl border border-stone-200 bg-white shadow-sm dark:border-slate-800 dark:bg-slate-900";
const uiHelpers = `
const $=s=>document.querySelector(s),esc=v=>String(v??'').replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[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)]}));
Expand Down
9 changes: 8 additions & 1 deletion packages/app/test/ansi.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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 &amp; 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;3m<strong>styled</strong>\u001b[0m';
expect(ansiToHtmlBrowserSource).not.toContain("__name");
expect(browserAnsiToHtml(sample)).toBe(ansiToHtml(sample));
});
});
1 change: 1 addition & 0 deletions packages/app/test/ui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down