Path: blob/master/src/packages/conat/service/terminal.ts
1452 views
/*1Service for controlling a terminal served from a project/compute server.2*/34import {5createServiceClient,6createServiceHandler,7type ConatService,8} from "./typed";910export type { ConatService };1112export const SIZE_TIMEOUT_MS = 45000;1314// API that runs under Node.js in linux:1516interface TerminalApi {17create: (opts: {18env?: { [key: string]: string };19command?: string;20args?: string[];21cwd?: string;22ephemeral?: boolean;23}) => Promise<{ success: "ok"; note?: string; ephemeral?: boolean }>;2425write: (data: string) => Promise<void>;2627restart: () => Promise<void>;2829cwd: () => Promise<string | undefined>;3031kill: () => Promise<void>;3233size: (opts: {34rows: number;35cols: number;36browser_id: string;37kick?: boolean;38}) => Promise<void>;3940// sent from browser to project when this client is leaving.41close: (browser_id: string) => Promise<void>;42}4344export function createTerminalClient({ project_id, termPath }) {45return createServiceClient<TerminalApi>({46project_id,47path: termPath,48service: "terminal-server",49timeout: 3000,50});51}5253export type TerminalServiceApi = ReturnType<typeof createTerminalClient>;5455export function createTerminalServer({56project_id,57termPath,58impl,59}: {60project_id: string;61termPath: string;62impl;63}): ConatService {64return createServiceHandler<TerminalApi>({65project_id,66path: termPath,67service: "terminal-server",68description: "Terminal service.",69impl,70});71}7273// API that runs in the browser:7475export interface TerminalBrowserApi {76// command is used for things like "open foo.txt" in the terminal.77command: (mesg) => Promise<void>;7879// used for kicking all but the specified user out:80kick: (sender_browser_id: string) => Promise<void>;8182// tell browser to change its size83size: (opts: { rows: number; cols: number }) => Promise<void>;84}8586export function createBrowserClient({ project_id, termPath }) {87return createServiceClient<TerminalBrowserApi>({88project_id,89path: termPath,90service: "terminal-browser",91});92}9394export function createBrowserService({95project_id,96termPath,97impl,98}: {99project_id: string;100termPath: string;101impl: TerminalBrowserApi;102}): ConatService {103return createServiceHandler<TerminalBrowserApi>({104project_id,105path: termPath,106service: "terminal-browser",107description: "Browser Terminal service.",108all: true,109impl,110});111}112113114