|
| 1 | +import type { Readable } from 'node:stream'; |
| 2 | + |
| 3 | +export interface SSEMessage { |
| 4 | + event: string; |
| 5 | + data: string; |
| 6 | + id?: string; |
| 7 | + retry?: number; |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * Parse a Readable carrying Server-Sent Events into structured messages. |
| 12 | + * |
| 13 | + * Yields one `SSEMessage` per blank-line-terminated record. Handles split data: lines, |
| 14 | + * CRLF or LF line endings, and arbitrary chunk boundaries — the underlying Node http |
| 15 | + * Readable does not guarantee chunks align with SSE record boundaries. |
| 16 | + */ |
| 17 | +export async function* parseSSE(stream: Readable): AsyncGenerator<SSEMessage> { |
| 18 | + let buffer = ''; |
| 19 | + for await (const chunk of stream) { |
| 20 | + buffer += chunk.toString('utf8'); |
| 21 | + while (true) { |
| 22 | + const recordEnd = buffer.indexOf('\n\n'); |
| 23 | + const crlfEnd = buffer.indexOf('\r\n\r\n'); |
| 24 | + let endIdx = -1; |
| 25 | + let delimLen = 0; |
| 26 | + if (recordEnd !== -1 && (crlfEnd === -1 || recordEnd < crlfEnd)) { |
| 27 | + endIdx = recordEnd; |
| 28 | + delimLen = 2; |
| 29 | + } else if (crlfEnd !== -1) { |
| 30 | + endIdx = crlfEnd; |
| 31 | + delimLen = 4; |
| 32 | + } |
| 33 | + if (endIdx === -1) break; |
| 34 | + const record = buffer.slice(0, endIdx); |
| 35 | + buffer = buffer.slice(endIdx + delimLen); |
| 36 | + const msg = parseRecord(record); |
| 37 | + if (msg) yield msg; |
| 38 | + } |
| 39 | + } |
| 40 | + // Any trailing record without a terminating blank line is treated as a final message, |
| 41 | + // matching the looser behavior browsers exhibit on connection close. |
| 42 | + if (buffer.trim()) { |
| 43 | + const msg = parseRecord(buffer); |
| 44 | + if (msg) yield msg; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function parseRecord(record: string): SSEMessage | null { |
| 49 | + const lines = record.split(/\r?\n/); |
| 50 | + let event = 'message'; |
| 51 | + let id: string | undefined; |
| 52 | + let retry: number | undefined; |
| 53 | + const dataLines: string[] = []; |
| 54 | + for (const line of lines) { |
| 55 | + if (line === '' || line.startsWith(':')) continue; |
| 56 | + const colon = line.indexOf(':'); |
| 57 | + const field = colon === -1 ? line : line.slice(0, colon); |
| 58 | + // Per spec, a leading space after the colon is stripped. |
| 59 | + let value = colon === -1 ? '' : line.slice(colon + 1); |
| 60 | + if (value.startsWith(' ')) value = value.slice(1); |
| 61 | + switch (field) { |
| 62 | + case 'event': |
| 63 | + event = value; |
| 64 | + break; |
| 65 | + case 'data': |
| 66 | + dataLines.push(value); |
| 67 | + break; |
| 68 | + case 'id': |
| 69 | + id = value; |
| 70 | + break; |
| 71 | + case 'retry': { |
| 72 | + const n = Number(value); |
| 73 | + if (Number.isFinite(n)) retry = n; |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + if (dataLines.length === 0 && event === 'message') return null; |
| 79 | + return { event, data: dataLines.join('\n'), id, retry }; |
| 80 | +} |
| 81 | + |
| 82 | +interface RenderState { |
| 83 | + currentPhase?: string; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Render SSE deploy events as terse, line-oriented progress to stderr (so stdout stays |
| 88 | + * reserved for the final JSON/YAML response document). Phase transitions print once. |
| 89 | + */ |
| 90 | +export function renderDeployProgress(message: SSEMessage, state: RenderState, output: NodeJS.WritableStream): void { |
| 91 | + let parsed: unknown; |
| 92 | + try { |
| 93 | + parsed = JSON.parse(message.data); |
| 94 | + } catch { |
| 95 | + parsed = message.data; |
| 96 | + } |
| 97 | + switch (message.event) { |
| 98 | + case 'phase': { |
| 99 | + const p = parsed as { phase?: string; status?: string; rolling?: boolean }; |
| 100 | + const label = p.phase ?? '?'; |
| 101 | + if (p.status === 'start') { |
| 102 | + if (state.currentPhase !== label) { |
| 103 | + output.write(`${label}…\n`); |
| 104 | + state.currentPhase = label; |
| 105 | + } |
| 106 | + } else if (p.status === 'done') { |
| 107 | + output.write(`${label} done\n`); |
| 108 | + } else if (p.status === 'error') { |
| 109 | + const msg = (parsed as { message?: string }).message ?? 'failed'; |
| 110 | + output.write(`${label} ERROR: ${msg}\n`); |
| 111 | + } |
| 112 | + break; |
| 113 | + } |
| 114 | + case 'error': { |
| 115 | + const e = parsed as { message?: string; code?: string | number }; |
| 116 | + output.write(`error: ${e.message ?? message.data}${e.code ? ` (${e.code})` : ''}\n`); |
| 117 | + break; |
| 118 | + } |
| 119 | + case 'done': |
| 120 | + // Caller picks up the final result via the SSE iterator; nothing to render here. |
| 121 | + break; |
| 122 | + } |
| 123 | +} |
0 commit comments