MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / toAsciicast

Function toAsciicast

e2e/src/surfaces/cli.ts:45–74  ·  view source on GitHub ↗
(recording: Uint8Array)

Source from the content-addressed store, hash-verified

43
44/** terminal-control's JSONL recording → asciicast v2 (what asciinema plays). */
45const toAsciicast = (recording: Uint8Array): string => {
46 const events = new TextDecoder()
47 .decode(recording)
48 .split("\n")
49 .filter(Boolean)
50 .flatMap((line): AsciicastEvent[] => {
51 // The PTY can be killed mid-write on teardown (e.g. a vitest timeout
52 // interrupts the fiber), leaving a truncated final JSONL line. A line
53 // that doesn't parse is one dropped frame, never a failed test: skip it
54 // instead of throwing.
55 // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: lenient parse of a best-effort recording artifact
56 try {
57 return [JSON.parse(line) as AsciicastEvent];
58 } catch {
59 return [];
60 }
61 });
62 const header = events.find((event) => event.type === "header");
63 const lines = [
64 JSON.stringify({ version: 2, width: header?.cols ?? 80, height: header?.rows ?? 24 }),
65 ];
66 // One streaming decoder so multi-byte UTF-8 split across events survives.
67 const decoder = new TextDecoder();
68 for (const event of events) {
69 if (event.type !== "output") continue;
70 const text = decoder.decode(Uint8Array.from(event.bytes ?? []), { stream: true });
71 if (text) lines.push(JSON.stringify([(event.at_ms ?? 0) / 1000, "o", text]));
72 }
73 return `${lines.join("\n")}\n`;
74};
75
76// acquireUseRelease so a vitest timeout (fiber interruption) still tears the
77// PTY down instead of leaking the child process.

Callers 1

makeCliSurfaceFunction · 0.85

Calls 2

decodeMethod · 0.80
pushMethod · 0.80

Tested by

no test coverage detected