cdk-local 0.78.1 → 0.79.0

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"local-studio-CSW1raG0.js","names":["nodeSpawn"],"sources":["../src/local/studio-events.ts","../src/local/studio-ui.ts","../src/local/studio-server.ts","../src/local/studio-dispatch.ts","../src/cli/commands/local-studio.ts"],"sourcesContent":["import { EventEmitter } from 'node:events';\n\n/**\n * The kind of runnable target an invocation ran against. Mirrors the\n * categories `cdkl list` / the studio target list expose.\n */\nexport type StudioTargetKind = 'lambda' | 'api' | 'alb' | 'ecs' | 'agentcore';\n\n/**\n * One request observed by `cdkl studio` — a single-shot invoke or one\n * request to a served target. Emitted twice per request: once at start\n * (status/durationMs/response absent) and once at end (filled in). The\n * UI threads the two by {@link StudioInvocationEvent.id}.\n *\n * Slice A defines the shape; the dispatch layer that emits populated\n * events lands with the invoke / serve slices.\n */\nexport interface StudioInvocationEvent {\n /** Correlation id, unique per request. Stable across the start/end pair. */\n id: string;\n /** Wall-clock epoch ms when the request started. */\n ts: number;\n /** Target id the request ran against (stack-qualified or display path). */\n target: string;\n /** The target's kind, for the UI's per-kind affordances. */\n kind: StudioTargetKind;\n /** Short request label for the timeline row (e.g. `GET /orders`). */\n label: string;\n /** The request payload (Lambda event JSON or the HTTP request shape). */\n request?: unknown;\n /** Response payload, set on the end event. */\n response?: unknown;\n /** HTTP status / invoke outcome code, set on the end event. */\n status?: number;\n /** Wall-clock duration in ms, set on the end event. */\n durationMs?: number;\n /**\n * When this invocation is a re-run of an earlier one (Phase 3), the id\n * of the source invocation. The UI links the new row to its origin.\n */\n reinvokeOf?: string;\n}\n\n/**\n * One container stdout/stderr line, tagged with its emitting container\n * so the detail panel can scope logs at CloudWatch granularity (Lambda\n * per-invocation, ECS per-container).\n */\nexport interface StudioLogEvent {\n /** Wall-clock epoch ms when the line was observed. */\n ts: number;\n /** Container id that emitted the line. */\n containerId: string;\n /** Target id the container backs, for the UI grouping. */\n target: string;\n /** The raw log line (without trailing newline). */\n line: string;\n /** Parsed stream, when known. */\n stream?: 'stdout' | 'stderr';\n}\n\n/**\n * Map of event name -> listener argument, for the typed wrapper below.\n */\ninterface StudioEventMap {\n invocation: [StudioInvocationEvent];\n log: [StudioLogEvent];\n}\n\n/**\n * In-process event bus that every studio observation flows through. The\n * studio HTTP server subscribes and forwards events to the browser over\n * SSE; the dispatch / log-streaming layers emit onto it.\n *\n * A thin typed wrapper over {@link EventEmitter} so producers and the\n * server agree on the event shapes without `any`. Re-exported from\n * `cdk-local/internal` so a host CLI embedding studio can subscribe.\n */\nexport class StudioEventBus {\n private readonly emitter = new EventEmitter();\n\n constructor() {\n // The bus can accumulate many SSE subscribers across a long session;\n // raise the cap so Node does not warn about a suspected leak.\n this.emitter.setMaxListeners(0);\n }\n\n emit<E extends keyof StudioEventMap>(event: E, ...args: StudioEventMap[E]): void {\n this.emitter.emit(event, ...args);\n }\n\n on<E extends keyof StudioEventMap>(\n event: E,\n listener: (...args: StudioEventMap[E]) => void\n ): this {\n this.emitter.on(event, listener as (...args: unknown[]) => void);\n return this;\n }\n\n off<E extends keyof StudioEventMap>(\n event: E,\n listener: (...args: StudioEventMap[E]) => void\n ): this {\n this.emitter.off(event, listener as (...args: unknown[]) => void);\n return this;\n }\n\n /**\n * Number of listeners currently subscribed to `event`. Exposed so the\n * SSE server's subscribe / unsubscribe symmetry can be asserted (a\n * dropped client must not leak a listener).\n */\n listenerCount<E extends keyof StudioEventMap>(event: E): number {\n return this.emitter.listenerCount(event);\n }\n}\n","/**\n * The studio web UI, embedded as a string so it ships inside the\n * `cdk-local` npm package (decision D9) with no asset-copy build step —\n * `tsdown` bundles this module like any other source file. Served by\n * the studio HTTP server (`startStudioServer`) at `GET /`.\n *\n * 3-pane shell (decision D6), framework-free vanilla JS (decision D7):\n * - left = target list (from `GET /api/targets`); each runnable\n * Lambda has an [Invoke] button and a selected-highlight.\n * - center = the WORKSPACE for the selected target: an event composer\n * (textarea + Invoke button) with the latest run's Request /\n * Response / Logs shown BELOW it, so you can edit and re-invoke\n * repeatedly without losing the composer.\n * - right = the timeline (history) of every invocation; clicking a\n * row loads it back into the workspace.\n *\n * The center workspace is deliberately adjacent to the left target list\n * (short eye-travel: pick a target -> compose right next to it), and is\n * the primary surface — the timeline is secondary history.\n */\n\nconst STUDIO_CSS = `\n * { box-sizing: border-box; }\n body {\n margin: 0; font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;\n color: #e6e6e6; background: #1a1a1a; height: 100vh; overflow: hidden;\n }\n header {\n padding: 8px 14px; background: #111; border-bottom: 1px solid #333;\n display: flex; align-items: center; gap: 10px;\n }\n header .brand { font-weight: 700; color: #fff; }\n header .meta { color: #888; font-size: 12px; }\n main {\n display: grid; grid-template-columns: 280px 5px 1fr 5px 320px;\n height: calc(100vh - 38px);\n }\n .pane { overflow: auto; }\n .splitter { background: #2a2a2a; cursor: col-resize; }\n .splitter:hover, .splitter.dragging { background: #4ec97a; }\n .pane h2 {\n margin: 0; padding: 8px 12px; font-size: 11px; text-transform: uppercase;\n letter-spacing: 0.5px; color: #888; background: #151515;\n position: sticky; top: 0; border-bottom: 1px solid #2a2a2a; z-index: 1;\n }\n .group-title { padding: 8px 12px 2px; color: #6aa9ff; font-size: 11px; }\n .target {\n padding: 6px 12px; border-bottom: 1px solid #222; display: flex;\n align-items: center; gap: 8px;\n }\n .target.runnable { cursor: pointer; }\n .target.runnable:hover { background: #202020; }\n .target.sel { background: #2a3550; }\n .target .name { color: #ddd; flex: 1; overflow: hidden; text-overflow: ellipsis; }\n .target .kind { color: #777; font-size: 11px; }\n .target .invoke-btn {\n padding: 2px 10px; font: 11px ui-monospace, Menlo, monospace; font-weight: 700;\n color: #0d1f12; background: #4ec97a; border: 0; border-radius: 3px; cursor: pointer;\n }\n .target .invoke-btn:hover { background: #6fe097; }\n .target.sel .invoke-btn { background: #6fe097; }\n .empty { padding: 16px 12px; color: #666; }\n .row {\n padding: 5px 12px; border-bottom: 1px solid #222; cursor: pointer;\n display: flex; gap: 8px; white-space: nowrap;\n }\n .row:hover { background: #222; }\n .row.sel { background: #2a3550; }\n .row .ts { color: #777; }\n .row .label { color: #ddd; flex: 1; overflow: hidden; text-overflow: ellipsis; }\n .row .status { color: #7bd88f; }\n .row .status.err { color: #e0707a; }\n #workspace { padding: 0 0 24px; }\n .composer { padding: 10px 12px; border-bottom: 1px solid #2a2a2a; }\n .composer .target-name { color: #fff; font-weight: 700; margin-bottom: 6px; }\n .composer textarea {\n width: 100%; min-height: 130px; resize: vertical; background: #111; color: #ddd;\n border: 1px solid #333; border-radius: 3px; padding: 6px;\n font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;\n }\n .composer button {\n margin-top: 8px; padding: 6px 18px; background: #2a7d46; color: #fff;\n border: 0; border-radius: 3px; cursor: pointer; font: inherit; font-weight: 700;\n }\n .composer button:hover { background: #339152; }\n .composer button:disabled { background: #333; color: #888; cursor: default; }\n .composer .err { color: #e0707a; margin-top: 6px; min-height: 18px; }\n .section { padding: 8px 12px; border-bottom: 1px solid #222; }\n .section h3 { margin: 0 0 6px; font-size: 11px; color: #888; text-transform: uppercase; }\n .section h3 .ok { color: #7bd88f; }\n .section h3 .bad { color: #e0707a; }\n .section pre { margin: 0; white-space: pre-wrap; word-break: break-word; color: #cfcfcf; }\n #conn { font-size: 11px; }\n #conn.up { color: #7bd88f; }\n #conn.down { color: #e0707a; }\n`;\n\nconst STUDIO_SCRIPT = `\n const KIND_LABEL = { lambda: 'Lambda', api: 'API', alb: 'ALB', ecs: 'ECS', agentcore: 'AgentCore' };\n const rowsById = new Map(); // invocationId -> timeline row element\n const invById = new Map(); // invocationId -> latest invocation event\n const logsById = new Map(); // invocationId -> [log lines]\n const targetEls = new Map(); // targetId -> left-pane element\n let active = null; // { id, kind, ta, btn, msg, result }\n let shownInvId = null; // invocation whose result is in the workspace\n\n function el(tag, cls, text) {\n const e = document.createElement(tag);\n if (cls) e.className = cls;\n if (text != null) e.textContent = text;\n return e;\n }\n\n async function loadTargets() {\n const pane = document.getElementById('targets');\n try {\n const res = await fetch('/api/targets');\n const data = await res.json();\n pane.querySelectorAll('.group-title,.target,.empty').forEach((n) => n.remove());\n let total = 0;\n for (const group of data.groups) {\n if (!group.entries.length) continue;\n pane.appendChild(el('div', 'group-title', group.title));\n for (const entry of group.entries) {\n total += 1;\n // Slice B: only Lambda targets are runnable from the UI (single-shot\n // invoke). Other kinds list but are not yet selectable.\n const runnable = group.kind === 'lambda';\n const t = el('div', runnable ? 'target runnable' : 'target');\n const name = el('span', 'name', entry.id);\n name.title = entry.id; // full path on hover even when truncated\n t.appendChild(name);\n t.appendChild(el('span', 'kind', '(' + (KIND_LABEL[group.kind] || group.kind) + ')'));\n if (runnable) {\n const btn = el('button', 'invoke-btn', 'Invoke');\n btn.onclick = (e) => { e.stopPropagation(); selectTarget(entry.id, group.kind); };\n t.appendChild(btn);\n t.onclick = () => selectTarget(entry.id, group.kind);\n targetEls.set(entry.id, t);\n }\n pane.appendChild(t);\n }\n }\n if (!total) pane.appendChild(el('div', 'empty', 'No runnable targets found.'));\n } catch (err) {\n pane.appendChild(el('div', 'empty', 'Failed to load targets: ' + err));\n }\n }\n\n function highlightTarget(id) {\n document.querySelectorAll('.target.sel').forEach((n) => n.classList.remove('sel'));\n const t = targetEls.get(id);\n if (t) t.classList.add('sel');\n }\n\n function selectTarget(id, kind) {\n highlightTarget(id);\n renderComposer(id, kind, '{}');\n }\n\n function renderComposer(id, kind, eventText) {\n const ws = document.getElementById('workspace');\n ws.innerHTML = '';\n\n const composer = el('div', 'composer');\n composer.appendChild(el('div', 'target-name', 'Invoke ' + id));\n const ta = el('textarea');\n ta.value = eventText;\n ta.spellcheck = false;\n composer.appendChild(ta);\n composer.appendChild(document.createElement('br'));\n const btn = el('button', null, 'Invoke');\n const msg = el('div', 'err');\n composer.appendChild(btn);\n composer.appendChild(msg);\n\n const result = el('div', 'result');\n\n ws.appendChild(composer);\n ws.appendChild(result);\n\n active = { id, kind, ta, btn, msg, result };\n btn.onclick = () => runInvoke();\n shownInvId = null;\n ta.focus();\n }\n\n async function runInvoke() {\n if (!active) return;\n const { id, kind, ta, btn, msg, result } = active;\n let event;\n try {\n event = ta.value.trim() === '' ? {} : JSON.parse(ta.value);\n } catch (err) {\n msg.textContent = 'Invalid JSON: ' + err.message;\n return;\n }\n msg.textContent = '';\n btn.disabled = true;\n btn.textContent = 'Invoking...';\n result.innerHTML = '';\n try {\n const res = await fetch('/api/run', {\n method: 'POST',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify({ targetId: id, kind, event }),\n });\n const data = await res.json();\n if (data.invocationId) {\n shownInvId = data.invocationId;\n renderResult(shownInvId);\n }\n if (!res.ok || data.ok === false) {\n msg.textContent = 'Invoke failed: ' + (data.error || ('HTTP ' + res.status));\n }\n } catch (err) {\n msg.textContent = 'Request failed: ' + err;\n } finally {\n btn.disabled = false;\n btn.textContent = 'Invoke';\n }\n }\n\n function fmt(body) {\n return typeof body === 'string' ? body : JSON.stringify(body, null, 2);\n }\n\n function renderResult(invId) {\n if (!active) return;\n const result = active.result;\n result.innerHTML = '';\n const ev = invById.get(invId);\n if (!ev) return;\n\n const reqSec = el('div', 'section');\n reqSec.appendChild(el('h3', null, 'Request'));\n reqSec.appendChild(el('pre', null, ev.request != null ? fmt(ev.request) : '(none)'));\n result.appendChild(reqSec);\n\n const respSec = el('div', 'section');\n const h = el('h3', null, 'Response');\n if (ev.status != null) {\n const cls = ev.status >= 200 && ev.status < 300 ? 'ok' : 'bad';\n const meta = el('span', cls, ' ' + ev.status + (ev.durationMs != null ? ' · ' + ev.durationMs + 'ms' : ''));\n h.appendChild(meta);\n }\n respSec.appendChild(h);\n respSec.appendChild(el('pre', null, ev.response != null ? fmt(ev.response) : '(pending…)'));\n result.appendChild(respSec);\n\n const logs = logsById.get(invId) || [];\n const logSec = el('div', 'section');\n logSec.appendChild(el('h3', null, 'Logs'));\n logSec.appendChild(el('pre', null, logs.length ? logs.join('\\\\n') : '(none)'));\n result.appendChild(logSec);\n }\n\n function addInvocation(ev) {\n invById.set(ev.id, Object.assign(invById.get(ev.id) || {}, ev));\n const timeline = document.getElementById('timeline');\n const placeholder = timeline.querySelector('.empty');\n if (placeholder) placeholder.remove();\n\n let row = rowsById.get(ev.id);\n if (!row) {\n row = el('div', 'row');\n row.appendChild(el('span', 'ts'));\n row.appendChild(el('span', 'label'));\n row.appendChild(el('span', 'status'));\n row.onclick = () => loadInvocation(ev.id);\n rowsById.set(ev.id, row);\n timeline.insertBefore(row, timeline.querySelector('.row')); // newest on top\n }\n const merged = invById.get(ev.id);\n const d = new Date(merged.ts);\n row.querySelector('.ts').textContent = d.toLocaleTimeString();\n row.querySelector('.label').textContent = (merged.target || '') + ' ' + (merged.label || '');\n const statusEl = row.querySelector('.status');\n statusEl.textContent =\n merged.status != null\n ? merged.status + (merged.durationMs != null ? ' ' + merged.durationMs + 'ms' : '')\n : '…';\n statusEl.className = 'status' + (merged.status != null && (merged.status < 200 || merged.status >= 300) ? ' err' : '');\n\n // Live-refresh the workspace result if it is showing this invocation.\n if (shownInvId === ev.id) renderResult(ev.id);\n }\n\n function loadInvocation(id) {\n const ev = invById.get(id);\n if (!ev) return;\n document.querySelectorAll('.row.sel').forEach((n) => n.classList.remove('sel'));\n const row = rowsById.get(id);\n if (row) row.classList.add('sel');\n highlightTarget(ev.target);\n renderComposer(ev.target, ev.kind, ev.request != null ? fmt(ev.request) : '{}');\n shownInvId = id;\n renderResult(id);\n }\n\n function connect() {\n const conn = document.getElementById('conn');\n const es = new EventSource('/api/events');\n es.addEventListener('open', () => { conn.textContent = '● live'; conn.className = 'up'; });\n es.addEventListener('error', () => { conn.textContent = '● disconnected'; conn.className = 'down'; });\n es.addEventListener('invocation', (e) => addInvocation(JSON.parse(e.data)));\n es.addEventListener('log', (e) => {\n const ev = JSON.parse(e.data);\n const arr = logsById.get(ev.containerId) || [];\n arr.push(ev.line);\n logsById.set(ev.containerId, arr);\n if (shownInvId === ev.containerId) renderResult(ev.containerId);\n });\n }\n\n function initSplitters() {\n const main = document.querySelector('main');\n let left = 280, right = 320;\n const apply = () => {\n main.style.gridTemplateColumns = left + 'px 5px 1fr 5px ' + right + 'px';\n };\n const wire = (splitterId, onMove) => {\n const s = document.getElementById(splitterId);\n s.addEventListener('mousedown', (e) => {\n e.preventDefault();\n const startX = e.clientX;\n const l0 = left, r0 = right;\n s.classList.add('dragging');\n document.body.style.userSelect = 'none';\n const move = (ev) => { onMove(ev.clientX - startX, l0, r0); apply(); };\n const up = () => {\n s.classList.remove('dragging');\n document.body.style.userSelect = '';\n document.removeEventListener('mousemove', move);\n document.removeEventListener('mouseup', up);\n };\n document.addEventListener('mousemove', move);\n document.addEventListener('mouseup', up);\n });\n };\n const clamp = (v) => Math.max(160, Math.min(760, v));\n wire('split-left', (dx, l0) => { left = clamp(l0 + dx); });\n wire('split-right', (dx, l0, r0) => { right = clamp(r0 - dx); });\n }\n\n loadTargets();\n connect();\n initSplitters();\n`;\n\n/**\n * Render the full studio HTML document. `appLabel` is shown in the\n * header (the CDK app / stack context); `cliName` brands the title for\n * host CLIs that rebrand `cdkl`.\n */\nexport function renderStudioHtml(appLabel: string, cliName: string): string {\n const safeApp = escapeHtml(appLabel);\n const safeCli = escapeHtml(cliName);\n return `<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\" />\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n<title>${safeCli} studio</title>\n<style>${STUDIO_CSS}</style>\n</head>\n<body>\n<header>\n <span class=\"brand\">${safeCli} studio</span>\n <span class=\"meta\">${safeApp}</span>\n <span id=\"conn\" class=\"down\">● connecting</span>\n</header>\n<main>\n <section class=\"pane\" id=\"targets\"><h2>Targets</h2></section>\n <div class=\"splitter\" id=\"split-left\"></div>\n <section class=\"pane\" id=\"workspace\"><div class=\"empty\">Pick a Lambda on the left to invoke it.</div></section>\n <div class=\"splitter\" id=\"split-right\"></div>\n <section class=\"pane\" id=\"timeline\"><h2>Timeline</h2><div class=\"empty\">No requests yet.</div></section>\n</main>\n<script>${STUDIO_SCRIPT}</script>\n</body>\n</html>`;\n}\n\n/** Minimal HTML-escape for the few interpolated text values. */\nfunction escapeHtml(s: string): string {\n return s\n .replace(/&/g, '&amp;')\n .replace(/</g, '&lt;')\n .replace(/>/g, '&gt;')\n .replace(/\"/g, '&quot;');\n}\n","import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';\nimport type { AddressInfo } from 'node:net';\nimport {\n StudioEventBus,\n type StudioInvocationEvent,\n type StudioLogEvent,\n} from './studio-events.js';\nimport { renderStudioHtml } from './studio-ui.js';\nimport type { TargetListing } from './target-lister.js';\n\n/** One target as the studio UI consumes it (`GET /api/targets`). */\nexport interface StudioTarget {\n /** Stable target id — the display path when available, else qualified id. */\n id: string;\n /** Stack-qualified `<Stack>:<LogicalId>` for disambiguation. */\n qualifiedId: string;\n /** API surface kind (REST v1 / HTTP v2 / ...), only for `api` entries. */\n surface?: string;\n}\n\n/** A category of targets, grouped by the studio kind that runs them. */\nexport interface StudioTargetGroup {\n /** Studio kind discriminator shared with {@link StudioInvocationEvent}. */\n kind: 'lambda' | 'api' | 'alb' | 'ecs' | 'agentcore';\n /** Human-readable group heading. */\n title: string;\n entries: StudioTarget[];\n}\n\n/**\n * Project a {@link TargetListing} (the same enumeration `cdkl list`\n * prints) into the grouped shape the studio UI renders. ECS services and\n * task definitions are folded into one `ecs` group; everything else maps\n * one category to one group. Exported so a unit test can assert the\n * projection without booting the server.\n */\nexport function toStudioTargetGroups(listing: TargetListing): StudioTargetGroup[] {\n const map = (entries: TargetListing['lambdas']): StudioTarget[] =>\n entries.map((e) => {\n const t: StudioTarget = { id: e.displayPath ?? e.qualifiedId, qualifiedId: e.qualifiedId };\n if (e.kind) t.surface = e.kind;\n return t;\n });\n return [\n { kind: 'lambda', title: 'Lambda Functions', entries: map(listing.lambdas) },\n { kind: 'api', title: 'APIs', entries: map(listing.apis) },\n {\n kind: 'ecs',\n title: 'ECS Services / Tasks',\n entries: [...map(listing.ecsServices), ...map(listing.ecsTaskDefinitions)],\n },\n { kind: 'agentcore', title: 'AgentCore Runtimes', entries: map(listing.agentCoreRuntimes) },\n { kind: 'alb', title: 'Load Balancers', entries: map(listing.loadBalancers) },\n ];\n}\n\n/** Inputs to {@link startStudioServer}. */\nexport interface StudioServerOptions {\n /** Preferred listen port; bumps on collision (decision: collision-safe). */\n port: number;\n /** Listen host. Defaults to `127.0.0.1` (localhost-only). */\n host?: string;\n /** The shared event bus the SSE stream forwards. */\n bus: StudioEventBus;\n /** Target groups to serve at `GET /api/targets`. */\n targetGroups: StudioTargetGroup[];\n /** Header label for the running app / stack context. */\n appLabel: string;\n /** CLI brand name (`cdkl`, or a host rebrand). */\n cliName: string;\n /**\n * Max consecutive ports to try on `EADDRINUSE` before giving up.\n * Defaults to 20.\n */\n maxPortBump?: number;\n /**\n * Handler for `POST /api/run` — runs a target (slice B: a single-shot\n * Lambda invoke) and returns the result. When omitted, `/api/run`\n * answers 501 (the observe-only shell). The body is the parsed JSON\n * request; the handler emits its own invocation / log events onto the\n * bus, so the UI's timeline updates over SSE independently of the\n * response.\n */\n onRun?: (body: unknown) => Promise<unknown>;\n}\n\n/** A running studio server. */\nexport interface RunningStudioServer {\n /** The URL the UI is served at, e.g. `http://127.0.0.1:9999`. */\n url: string;\n /** The actually-bound port (may differ from the requested one). */\n port: number;\n /** Stop the server and release the port. */\n close: () => Promise<void>;\n}\n\nconst SSE_HEARTBEAT_MS = 15_000;\n\n/**\n * Boot the studio HTTP server: serves the embedded UI at `/`, the target\n * list at `/api/targets`, and a Server-Sent-Events stream of the bus's\n * `invocation` / `log` events at `/api/events`. Localhost-only by\n * default. Resolves once the socket is listening.\n */\nexport async function startStudioServer(\n options: StudioServerOptions\n): Promise<RunningStudioServer> {\n const host = options.host ?? '127.0.0.1';\n const maxBump = options.maxPortBump ?? 20;\n const html = renderStudioHtml(options.appLabel, options.cliName);\n const targetsJson = JSON.stringify({ groups: options.targetGroups });\n\n const server = createServer((req, res) =>\n handleRequest(req, res, options.bus, html, targetsJson, options.onRun)\n );\n\n const boundPort = await listenWithBump(server, host, options.port, maxBump);\n\n return {\n url: `http://${host}:${boundPort}`,\n port: boundPort,\n close: () =>\n new Promise<void>((resolveClose, reject) => {\n server.close((err) => (err ? reject(err) : resolveClose()));\n // closeAllConnections exists on Node 18.2+; SSE keeps sockets open\n // so without this `close()` would hang on live EventSource clients.\n server.closeAllConnections?.();\n }),\n };\n}\n\nfunction handleRequest(\n req: IncomingMessage,\n res: ServerResponse,\n bus: StudioEventBus,\n html: string,\n targetsJson: string,\n onRun?: (body: unknown) => Promise<unknown>\n): void {\n const url = req.url ?? '/';\n const path = url.split('?')[0];\n\n if (req.method === 'GET' && (path === '/' || path === '/index.html')) {\n res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });\n res.end(html);\n return;\n }\n if (req.method === 'GET' && path === '/api/targets') {\n res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' });\n res.end(targetsJson);\n return;\n }\n if (req.method === 'GET' && path === '/api/events') {\n serveSse(req, res, bus);\n return;\n }\n if (req.method === 'POST' && path === '/api/run') {\n void handleRun(req, res, onRun);\n return;\n }\n res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });\n res.end('Not found');\n}\n\nconst MAX_RUN_BODY_BYTES = 5 * 1024 * 1024;\n\n/** Reply to `POST /api/run`: parse the JSON body, dispatch via `onRun`. */\nasync function handleRun(\n req: IncomingMessage,\n res: ServerResponse,\n onRun?: (body: unknown) => Promise<unknown>\n): Promise<void> {\n const sendJson = (statusCode: number, payload: unknown): void => {\n res.writeHead(statusCode, { 'content-type': 'application/json; charset=utf-8' });\n res.end(JSON.stringify(payload));\n };\n\n if (!onRun) {\n // The observe-only shell (no dispatcher wired) cannot run targets.\n sendJson(501, { error: 'Running targets is not supported by this studio server.' });\n return;\n }\n\n let body: unknown;\n try {\n body = await readJsonBody(req);\n } catch (err) {\n sendJson(400, { error: err instanceof Error ? err.message : String(err) });\n return;\n }\n\n try {\n const result = await onRun(body);\n sendJson(200, result);\n } catch (err) {\n sendJson(500, { error: err instanceof Error ? err.message : String(err) });\n }\n}\n\n/** Read + JSON-parse a request body, bounded to {@link MAX_RUN_BODY_BYTES}. */\nfunction readJsonBody(req: IncomingMessage): Promise<unknown> {\n return new Promise<unknown>((resolveBody, reject) => {\n let raw = '';\n let bytes = 0;\n // Single-flight: once the promise settles (over-limit / end / error) the\n // handlers short-circuit so a late chunk or the destroy-triggered `error`\n // cannot re-settle it.\n let done = false;\n const settle = (fn: () => void): void => {\n if (done) return;\n done = true;\n fn();\n };\n req.setEncoding('utf8');\n req.on('data', (chunk: string) => {\n if (done) return;\n bytes += Buffer.byteLength(chunk);\n if (bytes > MAX_RUN_BODY_BYTES) {\n settle(() => reject(new Error('Request body too large.')));\n req.destroy();\n return;\n }\n raw += chunk;\n });\n req.on('end', () => {\n settle(() => {\n if (raw.trim() === '') {\n resolveBody(undefined);\n return;\n }\n try {\n resolveBody(JSON.parse(raw));\n } catch {\n reject(new Error('Invalid JSON body.'));\n }\n });\n });\n req.on('error', (err) => settle(() => reject(err)));\n });\n}\n\nfunction serveSse(req: IncomingMessage, res: ServerResponse, bus: StudioEventBus): void {\n res.writeHead(200, {\n 'content-type': 'text/event-stream; charset=utf-8',\n 'cache-control': 'no-cache, no-transform',\n connection: 'keep-alive',\n });\n\n let closed = false;\n const heartbeat = setInterval(() => safeWrite(':hb\\n\\n'), SSE_HEARTBEAT_MS);\n heartbeat.unref?.();\n\n const onInvocation = (ev: StudioInvocationEvent): void => {\n safeWrite(`event: invocation\\ndata: ${JSON.stringify(ev)}\\n\\n`);\n };\n const onLog = (ev: StudioLogEvent): void => {\n safeWrite(`event: log\\ndata: ${JSON.stringify(ev)}\\n\\n`);\n };\n\n // Idempotent teardown: unsubscribe from the bus + stop the heartbeat so\n // a dropped EventSource client never leaks a listener.\n function cleanup(): void {\n if (closed) return;\n closed = true;\n clearInterval(heartbeat);\n bus.off('invocation', onInvocation);\n bus.off('log', onLog);\n }\n\n // Guard every write: on shutdown `close()` destroys live SSE sockets\n // (closeAllConnections), and a bus emit / heartbeat tick can race that\n // destroy. Writing to a destroyed socket emits an unhandled `error` on\n // the response — which, with no top-level uncaughtException handler on\n // the studio path, would crash the process on Ctrl-C. Drop the write\n // (and tear down) instead.\n function safeWrite(chunk: string): void {\n if (closed || res.writableEnded || res.destroyed) {\n cleanup();\n return;\n }\n res.write(chunk);\n }\n\n bus.on('invocation', onInvocation);\n bus.on('log', onLog);\n\n // `close` fires on client disconnect AND on server-side socket destroy;\n // `error` fires if a write loses the race with the destroy. All three\n // route through the idempotent cleanup so the bus listeners + heartbeat\n // never leak and a write error never propagates as uncaught.\n req.on('close', cleanup);\n res.on('close', cleanup);\n res.on('error', cleanup);\n\n // Open the stream so EventSource fires `open` immediately.\n safeWrite(':ok\\n\\n');\n}\n\n/**\n * Listen on `port`, retrying `port+1`, `port+2`, ... on `EADDRINUSE` up\n * to `maxBump` extra attempts. Resolves with the bound port.\n */\nfunction listenWithBump(\n server: Server,\n host: string,\n port: number,\n maxBump: number\n): Promise<number> {\n return new Promise<number>((resolveListen, reject) => {\n let attempt = 0;\n const tryListen = (p: number): void => {\n const onError = (err: NodeJS.ErrnoException): void => {\n if (err.code === 'EADDRINUSE' && attempt < maxBump) {\n attempt += 1;\n server.removeListener('error', onError);\n tryListen(p + 1);\n return;\n }\n reject(err);\n };\n server.once('error', onError);\n server.listen(p, host, () => {\n server.removeListener('error', onError);\n // Resolve with the ACTUAL bound port from the socket — when the\n // requested port is 0 the OS assigns a free one, which `p` does\n // not reflect.\n resolveListen((server.address() as AddressInfo).port);\n });\n };\n tryListen(port);\n });\n}\n","import { spawn as nodeSpawn, type ChildProcessWithoutNullStreams } from 'node:child_process';\nimport { mkdtempSync, rmSync, writeFileSync } from 'node:fs';\nimport { tmpdir } from 'node:os';\nimport { join } from 'node:path';\nimport { StudioEventBus, type StudioTargetKind } from './studio-events.js';\n\n/** A request to run a target, as the studio UI posts it to `/api/run`. */\nexport interface StudioRunRequest {\n /** Target id the user picked (display path or stack-qualified id). */\n targetId: string;\n /** Target kind. Slice B handles `'lambda'` (single-shot invoke) only. */\n kind: StudioTargetKind;\n /** The event payload to invoke with. */\n event: unknown;\n}\n\n/** The outcome of a single-shot run, returned from `/api/run`. */\nexport interface StudioRunResult {\n /** Correlation id shared with the emitted invocation events. */\n invocationId: string;\n /** Whether the underlying `cdkl invoke` exited 0. */\n ok: boolean;\n /**\n * Synthetic status for the timeline: 200 when the invoke succeeded,\n * 500 when it failed. A direct Lambda invoke has no HTTP status; this\n * is the UI's success/failure signal only.\n */\n status: number;\n /** Parsed Lambda response (JSON when parseable, else the raw string). */\n response?: unknown;\n /** Raw stdout payload from `cdkl invoke`. */\n raw?: string;\n /** Error summary when the invoke failed. */\n error?: string;\n /** Wall-clock duration in ms. */\n durationMs: number;\n}\n\n/** Config for {@link createStudioDispatcher}. */\nexport interface StudioDispatchConfig {\n /** Path to the `cdkl` CLI entry (`dist/cli.js`) — usually `process.argv[1]`. */\n cliEntry: string;\n /** The shared event bus; invocation + log events are emitted onto it. */\n bus: StudioEventBus;\n /** Working directory for the child invoke (defaults to `process.cwd()`). */\n cwd?: string;\n /** `--app` value to thread into the child invoke, if studio was given one. */\n app?: string;\n /** `-c key=value` context overrides to thread through. */\n context?: Record<string, string>;\n /** `--profile` to thread through. */\n profile?: string;\n /** `--region` to thread through. */\n region?: string;\n /** Node binary to spawn (defaults to `process.execPath`; injectable for tests). */\n nodeBin?: string;\n /** Spawn implementation (injectable for tests). */\n spawnFn?: typeof nodeSpawn;\n /** Clock (injectable for tests; defaults to `Date.now`). */\n clock?: () => number;\n /** Invocation-id factory (injectable for tests). */\n idFactory?: () => string;\n}\n\n/** A bound dispatcher exposing the `/api/run` handler. */\nexport interface StudioDispatcher {\n run: (req: StudioRunRequest) => Promise<StudioRunResult>;\n}\n\nlet idCounter = 0;\n\n/**\n * Build the studio run dispatcher. Slice B drives a single-shot Lambda\n * invoke by spawning the SAME `cdkl invoke` the headless command runs —\n * studio is a control plane over the CLI, so re-using the whole command\n * (rather than re-wiring its internals) guarantees byte-for-byte parity\n * and keeps all of `cdkl invoke`'s process-global behavior\n * (`process.exit`, env mutation, stdin) isolated in a child process.\n *\n * The child's stdout is the Lambda response payload; its stderr (status\n * + container logs) is streamed line-by-line onto the bus as `log`\n * events. An `invocation` start event is emitted before spawn and an end\n * event (with response + status + duration) after exit, both keyed by\n * the same correlation id so the UI threads them into one timeline row.\n */\nexport function createStudioDispatcher(config: StudioDispatchConfig): StudioDispatcher {\n const spawnFn = config.spawnFn ?? nodeSpawn;\n const nodeBin = config.nodeBin ?? process.execPath;\n const clock = config.clock ?? Date.now;\n const idFactory =\n config.idFactory ??\n (() => {\n idCounter += 1;\n return `inv-${clock()}-${idCounter}`;\n });\n\n async function run(req: StudioRunRequest): Promise<StudioRunResult> {\n const invocationId = idFactory();\n const startedAt = clock();\n\n if (req.kind !== 'lambda') {\n // Serve targets (API / ALB / ECS) arrive in slice C; reject clearly\n // rather than silently mis-dispatching.\n const error = `Running '${req.kind}' targets from studio is not supported yet (Lambda only).`;\n config.bus.emit('invocation', {\n id: invocationId,\n ts: startedAt,\n target: req.targetId,\n kind: req.kind,\n label: 'invoke',\n request: req.event,\n response: error,\n status: 501,\n durationMs: clock() - startedAt,\n });\n return { invocationId, ok: false, status: 501, error, durationMs: clock() - startedAt };\n }\n\n config.bus.emit('invocation', {\n id: invocationId,\n ts: startedAt,\n target: req.targetId,\n kind: req.kind,\n label: 'invoke',\n request: req.event,\n });\n\n // `dir` is created inside the try so a `writeFileSync` / `JSON.stringify`\n // throw still hits the finally that cleans it up.\n let dir: string | undefined;\n try {\n dir = mkdtempSync(join(tmpdir(), 'cdkl-studio-run-'));\n const eventFile = join(dir, 'event.json');\n writeFileSync(eventFile, JSON.stringify(req.event ?? {}));\n\n const args = ['invoke', req.targetId, '--event', eventFile];\n if (config.app) args.push('--app', config.app);\n if (config.profile) args.push('--profile', config.profile);\n if (config.region) args.push('--region', config.region);\n for (const [k, v] of Object.entries(config.context ?? {})) {\n args.push('-c', `${k}=${v}`);\n }\n\n const { code, stdout, stderr } = await runChild(\n spawnFn,\n nodeBin,\n [config.cliEntry, ...args],\n config.cwd ?? process.cwd(),\n invocationId,\n req.targetId,\n config.bus,\n clock\n );\n\n const durationMs = clock() - startedAt;\n const ok = code === 0;\n const failure = stderr.trim() || `cdkl invoke exited ${code}`;\n\n // `cdkl invoke` interleaves synth progress + streamed container logs on\n // stdout and writes the Lambda response there too. The RIE response is\n // always a single line of valid JSON (the serialized handler return\n // value or error object), whereas progress / container lines\n // (`Synthesizing…`, `Starting container…`, `START/END/REPORT`) are NOT\n // JSON — so the response is the LAST JSON-parseable stdout line, which is\n // robust against a container log line flushing AFTER the response. Every\n // other stdout line is surfaced as a log event. Falls back to the last\n // line when nothing parses (an unusual non-JSON / empty response).\n const stdoutLines = stdout\n .split('\\n')\n .map((l) => l.replace(/\\r$/, '').trimEnd())\n .filter((l) => l.trim().length > 0);\n\n let responseIdx = -1;\n let response: unknown;\n if (ok) {\n for (let i = stdoutLines.length - 1; i >= 0; i -= 1) {\n const parsed = tryParseJson(stdoutLines[i] ?? '');\n if (parsed.ok) {\n responseIdx = i;\n response = parsed.value;\n break;\n }\n }\n if (responseIdx === -1 && stdoutLines.length > 0) {\n responseIdx = stdoutLines.length - 1;\n response = stdoutLines[responseIdx];\n }\n } else {\n response = failure;\n }\n\n // Every stdout line that is not the response line is a log line.\n stdoutLines.forEach((line, i) => {\n if (i === responseIdx) return;\n config.bus.emit('log', {\n ts: clock(),\n containerId: invocationId,\n target: req.targetId,\n line,\n stream: 'stdout',\n });\n });\n\n const raw = responseIdx >= 0 ? (stdoutLines[responseIdx] ?? '') : '';\n const status = ok ? 200 : 500;\n\n config.bus.emit('invocation', {\n id: invocationId,\n ts: startedAt,\n target: req.targetId,\n kind: req.kind,\n label: 'invoke',\n request: req.event,\n response,\n status,\n durationMs,\n });\n\n const result: StudioRunResult = { invocationId, ok, status, durationMs };\n if (raw) result.raw = raw;\n if (response !== undefined) result.response = response;\n if (!ok) result.error = failure;\n return result;\n } finally {\n if (dir) rmSync(dir, { recursive: true, force: true });\n }\n }\n\n return { run };\n}\n\ninterface ChildOutcome {\n code: number | null;\n stdout: string;\n stderr: string;\n}\n\n/**\n * Spawn the child invoke, accumulate stdout, and stream stderr to the\n * bus line-by-line as `log` events. Resolves on process close.\n */\nfunction runChild(\n spawnFn: typeof nodeSpawn,\n nodeBin: string,\n argv: string[],\n cwd: string,\n invocationId: string,\n target: string,\n bus: StudioEventBus,\n clock: () => number\n): Promise<ChildOutcome> {\n return new Promise<ChildOutcome>((resolve, reject) => {\n let child: ChildProcessWithoutNullStreams;\n try {\n child = spawnFn(nodeBin, argv, { cwd });\n } catch (err) {\n reject(err instanceof Error ? err : new Error(String(err)));\n return;\n }\n\n let stdout = '';\n let stderr = '';\n let lineBuf = '';\n\n child.stdout.setEncoding('utf8');\n child.stderr.setEncoding('utf8');\n\n child.stdout.on('data', (chunk: string) => {\n stdout += chunk;\n });\n child.stderr.on('data', (chunk: string) => {\n stderr += chunk;\n lineBuf += chunk;\n let nl = lineBuf.indexOf('\\n');\n while (nl !== -1) {\n const line = lineBuf.slice(0, nl);\n lineBuf = lineBuf.slice(nl + 1);\n if (line.length > 0) {\n bus.emit('log', {\n ts: clock(),\n containerId: invocationId,\n target,\n line,\n stream: 'stderr',\n });\n }\n nl = lineBuf.indexOf('\\n');\n }\n });\n\n child.on('error', (err) => reject(err));\n child.on('close', (code) => {\n if (lineBuf.length > 0) {\n bus.emit('log', {\n ts: clock(),\n containerId: invocationId,\n target,\n line: lineBuf,\n stream: 'stderr',\n });\n }\n resolve({ code, stdout, stderr });\n });\n });\n}\n\n/** Try to JSON-parse `raw`; `ok` distinguishes a parsed value from a failure. */\nfunction tryParseJson(raw: string): { ok: boolean; value: unknown } {\n if (raw === '') return { ok: false, value: undefined };\n try {\n return { ok: true, value: JSON.parse(raw) };\n } catch {\n return { ok: false, value: undefined };\n }\n}\n","import { spawn } from 'node:child_process';\nimport { Command, Option } from 'commander';\nimport {\n appOptions,\n commonOptions,\n contextOptions,\n regionOption,\n parseContextOptions,\n} from '../options.js';\nimport { getLogger } from '../../utils/logger.js';\nimport { applyRoleArnIfSet } from '../../utils/role-arn.js';\nimport { withErrorHandling } from '../../utils/error-handler.js';\nimport { Synthesizer, type SynthesisOptions } from '../../synthesis/synthesizer.js';\nimport { resolveApp } from '../config-loader.js';\nimport {\n getEmbedConfig,\n setEmbedConfig,\n type CdkLocalEmbedConfig,\n} from '../../local/embed-config.js';\nimport { listTargets } from '../../local/target-lister.js';\nimport { StudioEventBus, type StudioTargetKind } from '../../local/studio-events.js';\nimport {\n startStudioServer,\n toStudioTargetGroups,\n type RunningStudioServer,\n} from '../../local/studio-server.js';\nimport { createStudioDispatcher, type StudioRunRequest } from '../../local/studio-dispatch.js';\n\nconst STUDIO_TARGET_KINDS: readonly StudioTargetKind[] = [\n 'lambda',\n 'api',\n 'alb',\n 'ecs',\n 'agentcore',\n];\n\n/**\n * Validate + narrow the untyped `POST /api/run` body into a\n * {@link StudioRunRequest}. Throws (→ 400 from the server) on a malformed\n * body so a bad UI / curl payload fails loudly rather than spawning an\n * `invoke` for an empty target.\n */\nexport function coerceRunRequest(body: unknown): StudioRunRequest {\n if (typeof body !== 'object' || body === null) {\n throw new Error('Request body must be a JSON object.');\n }\n const { targetId, kind, event } = body as Record<string, unknown>;\n if (typeof targetId !== 'string' || targetId.trim() === '') {\n throw new Error('Request body must include a non-empty \"targetId\" string.');\n }\n if (typeof kind !== 'string' || !STUDIO_TARGET_KINDS.includes(kind as StudioTargetKind)) {\n throw new Error(`Request body \"kind\" must be one of: ${STUDIO_TARGET_KINDS.join(', ')}.`);\n }\n return { targetId, kind: kind as StudioTargetKind, event };\n}\n\nconst DEFAULT_STUDIO_PORT = 9999;\n\n/**\n * Parse + validate the `--studio-port` value. Accepts `0` (OS-assigned)\n * through `65535`. Exported so a unit test can assert the bounds without\n * driving the full command. Throws on anything out of range / non-numeric.\n */\nexport function parseStudioPort(raw: string): number {\n // `Number('')` / `Number(' ')` coerce to 0, which would pass the range\n // check; reject blank input explicitly.\n const port = raw.trim() === '' ? NaN : Number(raw);\n if (!Number.isInteger(port) || port < 0 || port > 65535) {\n throw new Error(`--studio-port must be 0..65535 (got ${raw}).`);\n }\n return port;\n}\n\ninterface LocalStudioOptions {\n app?: string;\n output: string;\n verbose: boolean;\n profile?: string;\n roleArn?: string;\n context?: string[];\n region?: string;\n /** `--studio-port`: preferred listen port (bumps on collision). */\n studioPort: string;\n /** `--no-open`: suppress auto-opening the browser (Commander sets `open`). */\n open: boolean;\n}\n\n/**\n * Factory options for {@link createLocalStudioCommand}.\n */\nexport interface CreateLocalStudioCommandOptions {\n /** Embed-time branding overrides for a host wrapping this factory. */\n embedConfig?: CdkLocalEmbedConfig;\n}\n\nasync function localStudioCommand(options: LocalStudioOptions): Promise<void> {\n const logger = getLogger();\n if (options.verbose) logger.setLevel('debug');\n\n const port = parseStudioPort(options.studioPort);\n\n await applyRoleArnIfSet({\n roleArn: options.roleArn,\n region: undefined,\n profile: options.profile,\n });\n\n const appCmd = resolveApp(options.app);\n if (!appCmd) {\n throw new Error(\n `No CDK app specified. Pass --app, set ${getEmbedConfig().envPrefix}_APP, or add \"app\" to cdk.json.`\n );\n }\n\n logger.info('Synthesizing CDK app...');\n const synthesizer = new Synthesizer();\n const context = parseContextOptions(options.context);\n const synthOpts: SynthesisOptions = {\n app: appCmd,\n output: options.output,\n ...(options.profile && { profile: options.profile }),\n ...(Object.keys(context).length > 0 && { context }),\n };\n const { stacks } = await synthesizer.synthesize(synthOpts);\n\n const listing = listTargets(stacks);\n const targetGroups = toStudioTargetGroups(listing);\n const appLabel = stacks.map((s) => s.stackName).join(', ') || appCmd;\n\n const bus = new StudioEventBus();\n const dispatcher = createStudioDispatcher({\n // `process.argv[1]` is the running CLI entry (`dist/cli.js` / the `cdkl`\n // bin); the dispatcher spawns it again as `cdkl invoke <target>`.\n cliEntry: process.argv[1] ?? '',\n bus,\n cwd: process.cwd(),\n ...(appCmd ? { app: appCmd } : {}),\n ...(options.profile ? { profile: options.profile } : {}),\n ...(options.region ? { region: options.region } : {}),\n ...(Object.keys(context).length > 0 ? { context } : {}),\n });\n\n const server = await startStudioServer({\n port,\n bus,\n targetGroups,\n appLabel,\n cliName: getEmbedConfig().cliName,\n onRun: (body) => dispatcher.run(coerceRunRequest(body)),\n });\n\n const cliName = getEmbedConfig().cliName;\n logger.info(`${cliName} studio is running at ${server.url}`);\n logger.info('Press Ctrl-C to stop.');\n\n // Auto-open the browser only in an interactive terminal (never in CI /\n // piped / integ runs) and unless --no-open was passed.\n if (options.open && process.stdout.isTTY) {\n openBrowser(server.url);\n }\n\n await blockUntilShutdown(server, cliName);\n}\n\n/** Best-effort cross-platform browser open. Failures are non-fatal. */\nfunction openBrowser(url: string): void {\n const cmd =\n process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';\n try {\n const child = spawn(cmd, [url], {\n stdio: 'ignore',\n detached: true,\n shell: process.platform === 'win32',\n });\n child.on('error', () => undefined);\n child.unref();\n } catch {\n // Opening the browser is a convenience; ignore any failure.\n }\n}\n\n/**\n * Block until SIGINT / SIGTERM, then close the studio server and resolve.\n * Mirrors the long-running serve commands' graceful-shutdown contract.\n */\nfunction blockUntilShutdown(server: RunningStudioServer, cliName: string): Promise<void> {\n return new Promise<void>((resolveShutdown) => {\n let shuttingDown = false;\n const shutdown = (signal: string): void => {\n if (shuttingDown) return;\n shuttingDown = true;\n getLogger().info(`Received ${signal}; stopping ${cliName} studio...`);\n void server\n .close()\n .catch((err: unknown) => getLogger().warn(`Error stopping studio server: ${String(err)}`))\n .finally(() => resolveShutdown());\n };\n process.on('SIGINT', () => shutdown('SIGINT'));\n process.on('SIGTERM', () => shutdown('SIGTERM'));\n });\n}\n\nexport function createLocalStudioCommand(opts: CreateLocalStudioCommandOptions = {}): Command {\n setEmbedConfig(opts.embedConfig);\n const cmd = new Command('studio')\n .description(\n \"Open the local studio: a web console that lists the synthesized CDK app's runnable \" +\n 'targets and lets you invoke / serve them from the browser while watching all activity ' +\n 'in one timeline. The interactive counterpart to the headless invoke / start-* commands.'\n )\n .action(\n withErrorHandling(async (options: LocalStudioOptions) => {\n await localStudioCommand(options);\n })\n );\n\n addStudioSpecificOptions(cmd);\n [...commonOptions(), ...appOptions(), ...contextOptions].forEach((opt) => cmd.addOption(opt));\n cmd.addOption(regionOption);\n return cmd;\n}\n\n/**\n * Register the option block `cdkl studio` adds on top of the shared\n * common / app / context option helpers. Kept in a named helper (not\n * inline in {@link createLocalStudioCommand}) so a host CLI embedding\n * this factory inherits new studio flags without a duplicate\n * `.addOption(...)` block, matching every other `add<Cmd>SpecificOptions`\n * extraction. Chainable: returns `cmd`.\n */\nexport function addStudioSpecificOptions(cmd: Command): Command {\n cmd.addOption(\n new Option(\n '--studio-port <port>',\n 'Preferred port for the studio web server (bumps to the next free port on collision)'\n ).default(String(DEFAULT_STUDIO_PORT))\n );\n cmd.addOption(\n new Option('--no-open', 'Do not auto-open the browser when studio starts (TTY only)')\n );\n return cmd;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA8EA,IAAa,iBAAb,MAA4B;CAC1B,AAAiB,UAAU,IAAI,cAAc;CAE7C,cAAc;EAGZ,KAAK,QAAQ,gBAAgB,EAAE;;CAGjC,KAAqC,OAAU,GAAG,MAA+B;EAC/E,KAAK,QAAQ,KAAK,OAAO,GAAG,KAAK;;CAGnC,GACE,OACA,UACM;EACN,KAAK,QAAQ,GAAG,OAAO,SAAyC;EAChE,OAAO;;CAGT,IACE,OACA,UACM;EACN,KAAK,QAAQ,IAAI,OAAO,SAAyC;EACjE,OAAO;;;;;;;CAQT,cAA8C,OAAkB;EAC9D,OAAO,KAAK,QAAQ,cAAc,MAAM;;;;;;;;;;;;;;;;;;;;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AAuB;CAC9D,IAAI,UACF,IAAI,OACF,wBACA,sFACD,CAAC,QAAQ,OAAO,oBAAoB,CAAC,CACvC;CACD,IAAI,UACF,IAAI,OAAO,aAAa,6DAA6D,CACtF;CACD,OAAO"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cdk-local",
3
- "version": "0.78.1",
3
+ "version": "0.79.0",
4
4
  "description": "Run your CDK-built app locally — standalone, or against the real AWS resources and data it depends on. A CDK-native alternative to sam local.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,484 +0,0 @@
1
- import { c as getEmbedConfig, s as getLogger, u as setEmbedConfig } from "./docker-cmd-Dqx2YENO.js";
2
- import { Hn as listTargets, Ln as resolveApp, cr as withErrorHandling, fr as appOptions, gr as regionOption, hr as parseContextOptions, lr as applyRoleArnIfSet, mr as contextOptions, pr as commonOptions, zn as Synthesizer } from "./local-list-DBlBRfA-.js";
3
- import { Command, Option } from "commander";
4
- import { spawn } from "node:child_process";
5
- import { createServer } from "node:http";
6
- import { EventEmitter } from "node:events";
7
-
8
- //#region src/local/studio-events.ts
9
- /**
10
- * In-process event bus that every studio observation flows through. The
11
- * studio HTTP server subscribes and forwards events to the browser over
12
- * SSE; the dispatch / log-streaming layers emit onto it.
13
- *
14
- * A thin typed wrapper over {@link EventEmitter} so producers and the
15
- * server agree on the event shapes without `any`. Re-exported from
16
- * `cdk-local/internal` so a host CLI embedding studio can subscribe.
17
- */
18
- var StudioEventBus = class {
19
- emitter = new EventEmitter();
20
- constructor() {
21
- this.emitter.setMaxListeners(0);
22
- }
23
- emit(event, ...args) {
24
- this.emitter.emit(event, ...args);
25
- }
26
- on(event, listener) {
27
- this.emitter.on(event, listener);
28
- return this;
29
- }
30
- off(event, listener) {
31
- this.emitter.off(event, listener);
32
- return this;
33
- }
34
- /**
35
- * Number of listeners currently subscribed to `event`. Exposed so the
36
- * SSE server's subscribe / unsubscribe symmetry can be asserted (a
37
- * dropped client must not leak a listener).
38
- */
39
- listenerCount(event) {
40
- return this.emitter.listenerCount(event);
41
- }
42
- };
43
-
44
- //#endregion
45
- //#region src/local/studio-ui.ts
46
- /**
47
- * The studio web UI, embedded as a string so it ships inside the
48
- * `cdk-local` npm package (decision D9) with no asset-copy build step —
49
- * `tsdown` bundles this module like any other source file. Served by
50
- * the studio HTTP server (`startStudioServer`) at `GET /`.
51
- *
52
- * Slice A renders the 3-pane shell (decision D6): left = target list
53
- * (from `GET /api/targets`), center = live timeline (from the
54
- * `GET /api/events` SSE stream — empty until the invoke / serve slices
55
- * emit invocations), right = detail panel (filled on row click). It is
56
- * intentionally framework-free vanilla JS (decision D7).
57
- */
58
- const STUDIO_CSS = `
59
- * { box-sizing: border-box; }
60
- body {
61
- margin: 0; font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
62
- color: #e6e6e6; background: #1a1a1a; height: 100vh; overflow: hidden;
63
- }
64
- header {
65
- padding: 8px 14px; background: #111; border-bottom: 1px solid #333;
66
- display: flex; align-items: center; gap: 10px;
67
- }
68
- header .brand { font-weight: 700; color: #fff; }
69
- header .meta { color: #888; font-size: 12px; }
70
- main { display: grid; grid-template-columns: 280px 1fr 360px; height: calc(100vh - 38px); }
71
- .pane { overflow: auto; border-right: 1px solid #333; }
72
- .pane:last-child { border-right: 0; }
73
- .pane h2 {
74
- margin: 0; padding: 8px 12px; font-size: 11px; text-transform: uppercase;
75
- letter-spacing: 0.5px; color: #888; background: #151515;
76
- position: sticky; top: 0; border-bottom: 1px solid #2a2a2a;
77
- }
78
- .group-title { padding: 8px 12px 2px; color: #6aa9ff; font-size: 11px; }
79
- .target { padding: 5px 12px; cursor: default; border-bottom: 1px solid #222; }
80
- .target .name { color: #ddd; }
81
- .target .kind { color: #777; font-size: 11px; }
82
- .empty { padding: 16px 12px; color: #666; }
83
- .row {
84
- padding: 5px 12px; border-bottom: 1px solid #222; cursor: pointer;
85
- display: flex; gap: 8px; white-space: nowrap;
86
- }
87
- .row:hover { background: #222; }
88
- .row.sel { background: #2a3550; }
89
- .row .ts { color: #777; }
90
- .row .label { color: #ddd; flex: 1; overflow: hidden; text-overflow: ellipsis; }
91
- .row .status { color: #7bd88f; }
92
- #detail .section { padding: 8px 12px; border-bottom: 1px solid #222; }
93
- #detail .section h3 { margin: 0 0 6px; font-size: 11px; color: #888; text-transform: uppercase; }
94
- #detail pre { margin: 0; white-space: pre-wrap; word-break: break-word; color: #cfcfcf; }
95
- #conn { font-size: 11px; }
96
- #conn.up { color: #7bd88f; }
97
- #conn.down { color: #e0707a; }
98
- `;
99
- const STUDIO_SCRIPT = `
100
- const KIND_LABEL = { lambda: 'Lambda', api: 'API', alb: 'ALB', ecs: 'ECS', agentcore: 'AgentCore' };
101
- const rowsById = new Map();
102
-
103
- function el(tag, cls, text) {
104
- const e = document.createElement(tag);
105
- if (cls) e.className = cls;
106
- if (text != null) e.textContent = text;
107
- return e;
108
- }
109
-
110
- async function loadTargets() {
111
- const pane = document.getElementById('targets');
112
- try {
113
- const res = await fetch('/api/targets');
114
- const data = await res.json();
115
- pane.querySelectorAll('.group-title,.target,.empty').forEach((n) => n.remove());
116
- let total = 0;
117
- for (const group of data.groups) {
118
- if (!group.entries.length) continue;
119
- pane.appendChild(el('div', 'group-title', group.title));
120
- for (const entry of group.entries) {
121
- total += 1;
122
- const t = el('div', 'target');
123
- t.appendChild(el('span', 'name', entry.id));
124
- t.appendChild(document.createTextNode(' '));
125
- t.appendChild(el('span', 'kind', '(' + (KIND_LABEL[group.kind] || group.kind) + ')'));
126
- pane.appendChild(t);
127
- }
128
- }
129
- if (!total) pane.appendChild(el('div', 'empty', 'No runnable targets found.'));
130
- } catch (err) {
131
- pane.appendChild(el('div', 'empty', 'Failed to load targets: ' + err));
132
- }
133
- }
134
-
135
- function addInvocation(ev) {
136
- const timeline = document.getElementById('timeline');
137
- const placeholder = timeline.querySelector('.empty');
138
- if (placeholder) placeholder.remove();
139
- let row = rowsById.get(ev.id);
140
- if (!row) {
141
- row = el('div', 'row');
142
- row.appendChild(el('span', 'ts'));
143
- row.appendChild(el('span', 'label'));
144
- row.appendChild(el('span', 'status'));
145
- row.onclick = () => selectRow(ev.id);
146
- rowsById.set(ev.id, row);
147
- timeline.appendChild(row);
148
- }
149
- row._ev = Object.assign(row._ev || {}, ev);
150
- const d = new Date(ev.ts);
151
- row.querySelector('.ts').textContent = d.toLocaleTimeString();
152
- row.querySelector('.label').textContent = (ev.kind ? ev.target + ' ' : '') + (ev.label || '');
153
- row.querySelector('.status').textContent =
154
- ev.status != null ? ev.status + (ev.durationMs != null ? ' ' + ev.durationMs + 'ms' : '') : '...';
155
- }
156
-
157
- function selectRow(id) {
158
- document.querySelectorAll('.row.sel').forEach((n) => n.classList.remove('sel'));
159
- const row = rowsById.get(id);
160
- if (row) row.classList.add('sel');
161
- const ev = row && row._ev;
162
- const detail = document.getElementById('detail');
163
- detail.innerHTML = '';
164
- if (!ev) return;
165
- const sec = (title, body) => {
166
- const s = el('div', 'section');
167
- s.appendChild(el('h3', null, title));
168
- const pre = el('pre');
169
- pre.textContent = typeof body === 'string' ? body : JSON.stringify(body, null, 2);
170
- s.appendChild(pre);
171
- detail.appendChild(s);
172
- };
173
- sec('Request', ev.request != null ? ev.request : '(none)');
174
- sec('Response', ev.response != null ? ev.response : '(pending)');
175
- }
176
-
177
- function connect() {
178
- const conn = document.getElementById('conn');
179
- const es = new EventSource('/api/events');
180
- es.addEventListener('open', () => { conn.textContent = '● live'; conn.className = 'up'; });
181
- es.addEventListener('error', () => { conn.textContent = '● disconnected'; conn.className = 'down'; });
182
- es.addEventListener('invocation', (e) => addInvocation(JSON.parse(e.data)));
183
- }
184
-
185
- loadTargets();
186
- connect();
187
- `;
188
- /**
189
- * Render the full studio HTML document. `appLabel` is shown in the
190
- * header (the CDK app / stack context); `cliName` brands the title for
191
- * host CLIs that rebrand `cdkl`.
192
- */
193
- function renderStudioHtml(appLabel, cliName) {
194
- const safeApp = escapeHtml(appLabel);
195
- const safeCli = escapeHtml(cliName);
196
- return `<!doctype html>
197
- <html lang="en">
198
- <head>
199
- <meta charset="utf-8" />
200
- <meta name="viewport" content="width=device-width, initial-scale=1" />
201
- <title>${safeCli} studio</title>
202
- <style>${STUDIO_CSS}</style>
203
- </head>
204
- <body>
205
- <header>
206
- <span class="brand">${safeCli} studio</span>
207
- <span class="meta">${safeApp}</span>
208
- <span id="conn" class="down">● connecting</span>
209
- </header>
210
- <main>
211
- <section class="pane" id="targets"><h2>Targets</h2></section>
212
- <section class="pane" id="timeline"><h2>Timeline</h2><div class="empty">No requests yet. Invoke or serve a target to see activity.</div></section>
213
- <section class="pane" id="detail"><h2>Detail</h2><div class="empty">Select a request.</div></section>
214
- </main>
215
- <script>${STUDIO_SCRIPT}<\/script>
216
- </body>
217
- </html>`;
218
- }
219
- /** Minimal HTML-escape for the few interpolated text values. */
220
- function escapeHtml(s) {
221
- return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
222
- }
223
-
224
- //#endregion
225
- //#region src/local/studio-server.ts
226
- /**
227
- * Project a {@link TargetListing} (the same enumeration `cdkl list`
228
- * prints) into the grouped shape the studio UI renders. ECS services and
229
- * task definitions are folded into one `ecs` group; everything else maps
230
- * one category to one group. Exported so a unit test can assert the
231
- * projection without booting the server.
232
- */
233
- function toStudioTargetGroups(listing) {
234
- const map = (entries) => entries.map((e) => {
235
- const t = {
236
- id: e.displayPath ?? e.qualifiedId,
237
- qualifiedId: e.qualifiedId
238
- };
239
- if (e.kind) t.surface = e.kind;
240
- return t;
241
- });
242
- return [
243
- {
244
- kind: "lambda",
245
- title: "Lambda Functions",
246
- entries: map(listing.lambdas)
247
- },
248
- {
249
- kind: "api",
250
- title: "APIs",
251
- entries: map(listing.apis)
252
- },
253
- {
254
- kind: "ecs",
255
- title: "ECS Services / Tasks",
256
- entries: [...map(listing.ecsServices), ...map(listing.ecsTaskDefinitions)]
257
- },
258
- {
259
- kind: "agentcore",
260
- title: "AgentCore Runtimes",
261
- entries: map(listing.agentCoreRuntimes)
262
- },
263
- {
264
- kind: "alb",
265
- title: "Load Balancers",
266
- entries: map(listing.loadBalancers)
267
- }
268
- ];
269
- }
270
- const SSE_HEARTBEAT_MS = 15e3;
271
- /**
272
- * Boot the studio HTTP server: serves the embedded UI at `/`, the target
273
- * list at `/api/targets`, and a Server-Sent-Events stream of the bus's
274
- * `invocation` / `log` events at `/api/events`. Localhost-only by
275
- * default. Resolves once the socket is listening.
276
- */
277
- async function startStudioServer(options) {
278
- const host = options.host ?? "127.0.0.1";
279
- const maxBump = options.maxPortBump ?? 20;
280
- const html = renderStudioHtml(options.appLabel, options.cliName);
281
- const targetsJson = JSON.stringify({ groups: options.targetGroups });
282
- const server = createServer((req, res) => handleRequest(req, res, options.bus, html, targetsJson));
283
- const boundPort = await listenWithBump(server, host, options.port, maxBump);
284
- return {
285
- url: `http://${host}:${boundPort}`,
286
- port: boundPort,
287
- close: () => new Promise((resolveClose, reject) => {
288
- server.close((err) => err ? reject(err) : resolveClose());
289
- server.closeAllConnections?.();
290
- })
291
- };
292
- }
293
- function handleRequest(req, res, bus, html, targetsJson) {
294
- const path = (req.url ?? "/").split("?")[0];
295
- if (req.method === "GET" && (path === "/" || path === "/index.html")) {
296
- res.writeHead(200, { "content-type": "text/html; charset=utf-8" });
297
- res.end(html);
298
- return;
299
- }
300
- if (req.method === "GET" && path === "/api/targets") {
301
- res.writeHead(200, { "content-type": "application/json; charset=utf-8" });
302
- res.end(targetsJson);
303
- return;
304
- }
305
- if (req.method === "GET" && path === "/api/events") {
306
- serveSse(req, res, bus);
307
- return;
308
- }
309
- res.writeHead(404, { "content-type": "text/plain; charset=utf-8" });
310
- res.end("Not found");
311
- }
312
- function serveSse(req, res, bus) {
313
- res.writeHead(200, {
314
- "content-type": "text/event-stream; charset=utf-8",
315
- "cache-control": "no-cache, no-transform",
316
- connection: "keep-alive"
317
- });
318
- let closed = false;
319
- const heartbeat = setInterval(() => safeWrite(":hb\n\n"), SSE_HEARTBEAT_MS);
320
- heartbeat.unref?.();
321
- const onInvocation = (ev) => {
322
- safeWrite(`event: invocation\ndata: ${JSON.stringify(ev)}\n\n`);
323
- };
324
- const onLog = (ev) => {
325
- safeWrite(`event: log\ndata: ${JSON.stringify(ev)}\n\n`);
326
- };
327
- function cleanup() {
328
- if (closed) return;
329
- closed = true;
330
- clearInterval(heartbeat);
331
- bus.off("invocation", onInvocation);
332
- bus.off("log", onLog);
333
- }
334
- function safeWrite(chunk) {
335
- if (closed || res.writableEnded || res.destroyed) {
336
- cleanup();
337
- return;
338
- }
339
- res.write(chunk);
340
- }
341
- bus.on("invocation", onInvocation);
342
- bus.on("log", onLog);
343
- req.on("close", cleanup);
344
- res.on("close", cleanup);
345
- res.on("error", cleanup);
346
- safeWrite(":ok\n\n");
347
- }
348
- /**
349
- * Listen on `port`, retrying `port+1`, `port+2`, ... on `EADDRINUSE` up
350
- * to `maxBump` extra attempts. Resolves with the bound port.
351
- */
352
- function listenWithBump(server, host, port, maxBump) {
353
- return new Promise((resolveListen, reject) => {
354
- let attempt = 0;
355
- const tryListen = (p) => {
356
- const onError = (err) => {
357
- if (err.code === "EADDRINUSE" && attempt < maxBump) {
358
- attempt += 1;
359
- server.removeListener("error", onError);
360
- tryListen(p + 1);
361
- return;
362
- }
363
- reject(err);
364
- };
365
- server.once("error", onError);
366
- server.listen(p, host, () => {
367
- server.removeListener("error", onError);
368
- resolveListen(server.address().port);
369
- });
370
- };
371
- tryListen(port);
372
- });
373
- }
374
-
375
- //#endregion
376
- //#region src/cli/commands/local-studio.ts
377
- const DEFAULT_STUDIO_PORT = 9999;
378
- /**
379
- * Parse + validate the `--studio-port` value. Accepts `0` (OS-assigned)
380
- * through `65535`. Exported so a unit test can assert the bounds without
381
- * driving the full command. Throws on anything out of range / non-numeric.
382
- */
383
- function parseStudioPort(raw) {
384
- const port = raw.trim() === "" ? NaN : Number(raw);
385
- if (!Number.isInteger(port) || port < 0 || port > 65535) throw new Error(`--studio-port must be 0..65535 (got ${raw}).`);
386
- return port;
387
- }
388
- async function localStudioCommand(options) {
389
- const logger = getLogger();
390
- if (options.verbose) logger.setLevel("debug");
391
- const port = parseStudioPort(options.studioPort);
392
- await applyRoleArnIfSet({
393
- roleArn: options.roleArn,
394
- region: void 0,
395
- profile: options.profile
396
- });
397
- const appCmd = resolveApp(options.app);
398
- if (!appCmd) throw new Error(`No CDK app specified. Pass --app, set ${getEmbedConfig().envPrefix}_APP, or add "app" to cdk.json.`);
399
- logger.info("Synthesizing CDK app...");
400
- const synthesizer = new Synthesizer();
401
- const context = parseContextOptions(options.context);
402
- const synthOpts = {
403
- app: appCmd,
404
- output: options.output,
405
- ...options.profile && { profile: options.profile },
406
- ...Object.keys(context).length > 0 && { context }
407
- };
408
- const { stacks } = await synthesizer.synthesize(synthOpts);
409
- const targetGroups = toStudioTargetGroups(listTargets(stacks));
410
- const appLabel = stacks.map((s) => s.stackName).join(", ") || appCmd;
411
- const server = await startStudioServer({
412
- port,
413
- bus: new StudioEventBus(),
414
- targetGroups,
415
- appLabel,
416
- cliName: getEmbedConfig().cliName
417
- });
418
- const cliName = getEmbedConfig().cliName;
419
- logger.info(`${cliName} studio is running at ${server.url}`);
420
- logger.info("Press Ctrl-C to stop.");
421
- if (options.open && process.stdout.isTTY) openBrowser(server.url);
422
- await blockUntilShutdown(server, cliName);
423
- }
424
- /** Best-effort cross-platform browser open. Failures are non-fatal. */
425
- function openBrowser(url) {
426
- const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
427
- try {
428
- const child = spawn(cmd, [url], {
429
- stdio: "ignore",
430
- detached: true,
431
- shell: process.platform === "win32"
432
- });
433
- child.on("error", () => void 0);
434
- child.unref();
435
- } catch {}
436
- }
437
- /**
438
- * Block until SIGINT / SIGTERM, then close the studio server and resolve.
439
- * Mirrors the long-running serve commands' graceful-shutdown contract.
440
- */
441
- function blockUntilShutdown(server, cliName) {
442
- return new Promise((resolveShutdown) => {
443
- let shuttingDown = false;
444
- const shutdown = (signal) => {
445
- if (shuttingDown) return;
446
- shuttingDown = true;
447
- getLogger().info(`Received ${signal}; stopping ${cliName} studio...`);
448
- server.close().catch((err) => getLogger().warn(`Error stopping studio server: ${String(err)}`)).finally(() => resolveShutdown());
449
- };
450
- process.on("SIGINT", () => shutdown("SIGINT"));
451
- process.on("SIGTERM", () => shutdown("SIGTERM"));
452
- });
453
- }
454
- function createLocalStudioCommand(opts = {}) {
455
- setEmbedConfig(opts.embedConfig);
456
- const cmd = new Command("studio").description("Open the local studio: a web console that lists the synthesized CDK app's runnable targets and lets you invoke / serve them from the browser while watching all activity in one timeline. The interactive counterpart to the headless invoke / start-* commands.").action(withErrorHandling(async (options) => {
457
- await localStudioCommand(options);
458
- }));
459
- addStudioSpecificOptions(cmd);
460
- [
461
- ...commonOptions(),
462
- ...appOptions(),
463
- ...contextOptions
464
- ].forEach((opt) => cmd.addOption(opt));
465
- cmd.addOption(regionOption);
466
- return cmd;
467
- }
468
- /**
469
- * Register the option block `cdkl studio` adds on top of the shared
470
- * common / app / context option helpers. Kept in a named helper (not
471
- * inline in {@link createLocalStudioCommand}) so a host CLI embedding
472
- * this factory inherits new studio flags without a duplicate
473
- * `.addOption(...)` block, matching every other `add<Cmd>SpecificOptions`
474
- * extraction. Chainable: returns `cmd`.
475
- */
476
- function addStudioSpecificOptions(cmd) {
477
- cmd.addOption(new Option("--studio-port <port>", "Preferred port for the studio web server (bumps to the next free port on collision)").default(String(DEFAULT_STUDIO_PORT)));
478
- cmd.addOption(new Option("--no-open", "Do not auto-open the browser when studio starts (TTY only)"));
479
- return cmd;
480
- }
481
-
482
- //#endregion
483
- export { renderStudioHtml as a, toStudioTargetGroups as i, createLocalStudioCommand as n, StudioEventBus as o, startStudioServer as r, addStudioSpecificOptions as t };
484
- //# sourceMappingURL=local-studio-BuU6kMYP.js.map