All files / client/src/types external-api.ts

100% Statements 22/22
100% Branches 4/4
100% Functions 2/2
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244                5x         5x   5x   5x   5x   5x   5x   5x   5x   5x   5x   5x   5x   5x   5x             5x   5x   5x   5x   5x                                                                                                                                                                                                                                                                                                                                                                                          
/**
 * External API types for the postMessage remote control interface.
 *
 * This module defines the protocol for external websites to control
 * the Arduino simulator via window.postMessage.
 */
 
/** API Version for backward compatibility and feature negotiation. */
export const API_VERSION = "1.4.0";
 
/**
 * All actions supported by the simulator's remote control interface (inbound).
 */
export enum SimulatorActionType {
  /** Load code into the editor */
  LOAD_CODE = "LOAD_CODE",
  /** Compile and start the simulation */
  START_SIMULATION = "START_SIMULATION",
  /** Stop the running simulation */
  STOP_SIMULATION = "STOP_SIMULATION",
  /** Pause a running simulation (preserves state) */
  PAUSE_SIMULATION = "PAUSE_SIMULATION",
  /** Resume a paused simulation */
  RESUME_SIMULATION = "RESUME_SIMULATION",
  /** Set the value of a digital or analog pin */
  SET_PIN_STATE = "SET_PIN_STATE",
  /** Request the current value of a pin (triggers a RESPONSE message) */
  GET_PIN_STATE = "GET_PIN_STATE",
  /** Set multiple pins in a single operation (batch) */
  BATCH_SET_PIN_STATE = "BATCH_SET_PIN_STATE",
  /** Send serial input data to the running simulation */
  SERIAL_INPUT = "SERIAL_INPUT",
  /** Change the simulation timeout (in milliseconds) */
  SET_SIMULATION_TIMEOUT = "SET_SIMULATION_TIMEOUT",
  /** Switch the active output tab (compiler, messages, registry, debug) */
  SET_OUTPUT_TAB = "SET_OUTPUT_TAB",
  /** Query the current simulation state (triggers a RESPONSE message) */
  GET_SIMULATION_STATE = "GET_SIMULATION_STATE",
  /** Query the current server/pool status (triggers a RESPONSE message) */
  GET_SERVER_STATUS = "GET_SERVER_STATUS",
}
 
/**
 * All events emitted by the simulator (outbound).
 * These are sent proactively from the simulator to the parent frame.
 */
export enum SimulatorEventType {
  /** A pin's state (digital or analog value) has changed */
  PIN_STATE_CHANGE_EVENT = "PIN_STATE_CHANGE_EVENT",
  /** The simulation's overall state has changed (RUNNING, STOPPED, ERROR) */
  SIMULATION_STATE_EVENT = "SIMULATION_STATE_EVENT",
  /** Data has been output over the serial interface (Serial.print, etc.) */
  SERIAL_OUTPUT_EVENT = "SERIAL_OUTPUT_EVENT",
  /** The server/pool status has changed (runner pool, compile queue, reachability) */
  SERVER_STATUS_EVENT = "SERVER_STATUS_EVENT",
}
 
/**
 * Payload for LOAD_CODE messages.
 */
export interface LoadCodePayload {
  code: string;
}
 
/**
 * Payload for SET_PIN_STATE messages.
 */
export interface SetPinStatePayload {
  pin: number;
  value: number;
}
 
/**
 * Payload for GET_PIN_STATE messages.
 */
export interface GetPinStatePayload {
  pin: number;
}
 
/**
 * Payload for BATCH_SET_PIN_STATE messages.
 * Allows setting multiple pins in a single operation for efficiency.
 */
export interface BatchSetPinStatePayload {
  pins: Array<{ pin: number; value: number }>;
}
 
/**
 * Payload for SERIAL_INPUT messages.
 */
export interface SerialInputPayload {
  data: string;
}
 
/**
 * Payload for SET_SIMULATION_TIMEOUT messages.
 */
export interface SetSimulationTimeoutPayload {
  timeout: number;
}
 
/**
 * Payload for SET_OUTPUT_TAB messages.
 */
export interface SetOutputTabPayload {
  tab: "compiler" | "messages" | "registry" | "debug";
}
 
/** Union of all payload types keyed by action type */
type PayloadMap = {
  [SimulatorActionType.LOAD_CODE]: LoadCodePayload;
  [SimulatorActionType.START_SIMULATION]: undefined;
  [SimulatorActionType.STOP_SIMULATION]: undefined;
  [SimulatorActionType.PAUSE_SIMULATION]: undefined;
  [SimulatorActionType.RESUME_SIMULATION]: undefined;
  [SimulatorActionType.SET_PIN_STATE]: SetPinStatePayload;
  [SimulatorActionType.GET_PIN_STATE]: GetPinStatePayload;
  [SimulatorActionType.BATCH_SET_PIN_STATE]: BatchSetPinStatePayload;
  [SimulatorActionType.SERIAL_INPUT]: SerialInputPayload;
  [SimulatorActionType.SET_SIMULATION_TIMEOUT]: SetSimulationTimeoutPayload;
  [SimulatorActionType.SET_OUTPUT_TAB]: SetOutputTabPayload;
  [SimulatorActionType.GET_SIMULATION_STATE]: undefined;
  [SimulatorActionType.GET_SERVER_STATUS]: undefined;
};
 
/**
 * Payload for PIN_STATE_CHANGE_EVENT events.
 */
export interface PinStateChangeEventData {
  pin: number;
  value: number;
}
 
/**
 * Data for SIMULATION_STATE_EVENT events.
 *
 * States follow the full client lifecycle:
 *   IDLE → QUEUED_FOR_COMPILING → COMPILING → QUEUED_FOR_SIMULATION → RUNNING ⇄ PAUSED
 *
 * Legacy values STOPPED and QUEUED are accepted for backward compatibility
 * but will no longer be emitted starting with API v1.4.0.
 */
export interface SimulationStateEventData {
  state:
    | "IDLE"
    | "QUEUED_FOR_COMPILING"
    | "COMPILING"
    | "QUEUED_FOR_SIMULATION"
    | "RUNNING"
    | "PAUSED"
    | "ERROR";
  message?: string;
}
 
/**
 * Data for SERVER_STATUS_EVENT events.
 * Reports server reachability, sandbox runner stats, and compile slot stats.
 *
 * Canonical terms (used across UI, API, banner, and docs):
 * - sandboxRunners → "Sandbox Runners" (container pool for docker-sandbox mode)
 * - compileSlots → "Compile Slots" (max concurrent compilation processes)
 * 
 * Legacy aliases (pool, compile) are deprecated but still supported for backward compatibility.
 */
export interface ServerStatusEventData {
  /** True when the server HTTP endpoint is reachable */
  serverReachable: boolean;
  /** Sandbox Runners — container pool stats (docker-sandbox mode) */
  sandboxRunners: {
    /** Total sandbox runner slots allocated */
    total: number;
    /** Sandbox runner slots currently idle */
    available: number;
    /** Sandbox runner slots actively running a simulation */
    inUse: number;
    /** Requests waiting for a free sandbox runner */
    queued: number;
    /** Maximum sandbox runner slots allowed */
    max: number;
  };
  /** Compile Slots — concurrent compilation stats */
  compileSlots: {
    /** Compile jobs currently in progress */
    active: number;
    /** Compile jobs waiting for a free slot */
    queued: number;
    /** Maximum concurrent compile slots allowed */
    maxConcurrent: number;
  };
  /** @deprecated Use sandboxRunners instead. Will be removed in next major release. */
  pool?: ServerStatusEventData["sandboxRunners"];
  /** @deprecated Use compileSlots instead. Will be removed in next major release. */
  compile?: ServerStatusEventData["compileSlots"];
}
 
 
/**
 * Inbound message from an external website.
 *
 * @example
 * window.postMessage({ type: "LOAD_CODE", payload: { code: "void setup() {}" } }, "*");
 */
export type SimulatorMessage<T extends SimulatorActionType = SimulatorActionType> = {
  type: T;
  payload: PayloadMap[T];
};
 
/**
 * Response message sent back to the parent frame via postMessage.
 * Includes version for backward compatibility negotiation.
 */
export interface SimulatorResponse {
  /** API version (semantic versioning: major.minor.patch) */
  version: string;
  /** The action this response corresponds to */
  type: string;
  /** Whether the action was processed successfully */
  success: boolean;
  /** Optional data payload (e.g. pin value for GET_PIN_STATE) */
  data?: unknown;
  /** Error message when success is false */
  error?: string;
}
 
/**
 * Event message emitted proactively by the simulator.
 * These are sent without request and inform the parent of state changes.
 * The `data` field matches the format documented in EXTERNAL_API.md.
 */
export type SimulatorEventMessage<T extends SimulatorEventType = SimulatorEventType> = {
  version: string;
  type: T;
  success: true; // Events always report success
  data: T extends SimulatorEventType.PIN_STATE_CHANGE_EVENT
    ? PinStateChangeEventData
    : T extends SimulatorEventType.SIMULATION_STATE_EVENT
      ? SimulationStateEventData
      : T extends SimulatorEventType.SERIAL_OUTPUT_EVENT
        ? string
        : T extends SimulatorEventType.SERVER_STATUS_EVENT
          ? ServerStatusEventData
          : never;
}