UNPKG

histoire

Version:

A fast story book powered by Vite

93 lines (92 loc) • 4.08 kB
import { TerminalError } from '@histoire/cli/protocol/core'; import { isBoundedFrame, isRuntimeCommand } from '@histoire/dev-proxy/protocol'; import { getRuntimeCatalogForContext } from '../catalog/attachment.js'; import { createExecutionOwner } from '../execution-owner.js'; import { terminalHandlers } from './handlers/index.js'; import { createCommandRegistry } from './registry.js'; /** * Creates the child-side terminal adapter. It attaches through the existing * generation lifecycle callbacks (`onGeneration`/`onBeforeRelease`), validates * `runtime-command` frames, and dispatches through the handler registry only. * It never creates a second server, collector, queue or listener. * @param options Wiring supplied by `dev-child`. */ export function createTerminalAdapter(options) { const registry = createCommandRegistry(options.handlers ?? terminalHandlers); let scope; let closed = false; /** Emits an observation; oversized or post-close events are dropped (returns false). */ function emit(event, data) { const message = { type: 'runtime-event', generation: options.generation, event, data }; if (closed || !isBoundedFrame(message)) return false; return options.send(message); } /** Answers one command; late answers of a released generation are suppressed. */ async function run(command) { const base = { type: 'runtime-reply', generation: options.generation, id: command.id }; const current = scope; if (!current || closed) { options.send({ ...base, ok: false, error: { code: 'unavailable', message: 'Runtime generation is not ready' } }); return; } const context = { generation: options.generation, handle: current.handle, catalog: getRuntimeCatalogForContext(current.handle.context)?.catalog, execution: current.owner, emit: emit, signal: current.abort.signal, }; try { const result = await registry.dispatch(command.operation, command.args, context); if (scope !== current || closed) return; if (!isBoundedFrame({ ...base, ok: true, result })) throw new TerminalError('failed', 'Result exceeds the frame bound'); options.send({ ...base, ok: true, result }); } catch (error) { if (scope !== current || closed) return; const known = error instanceof TerminalError; options.send({ ...base, ok: false, error: { code: known ? error.code : 'failed', message: String(error instanceof Error ? error.message : error).split('\n')[0].slice(0, 1024) } }); } } return { /** Operation names currently served. */ operations: registry.operations, /** Pass as (part of) `onGeneration`; opens the generation scope. */ onGeneration(handle) { scope = { handle, owner: createExecutionOwner(options.execution()), abort: new AbortController() }; }, /** * Pass as (part of) `onBeforeRelease`: stop admission first, then join * confirmed cleanup of terminal-owned work. */ async onBeforeRelease(handle) { const current = scope; if (!current || current.handle !== handle) return; scope = undefined; current.abort.abort(); await current.owner.close(); }, /** * Handles one raw IPC message. * @returns Whether it was a command for this generation (and was taken). */ receive(raw) { if (!isRuntimeCommand(raw) || raw.generation !== options.generation) return false; void run(raw); return true; }, emit, /** Stops all replies/events; hooks already installed elsewhere are restored by their owners. */ close() { closed = true; scope?.abort.abort(); }, }; }