Phoenix.ReactServer.Runtime.Common (Phoenix.ReactServer NG v0.8.4)

Copy Markdown View Source

Common functionality shared between different runtime implementations (Bun, Deno, etc.).

This module provides shared behavior for:

  • Port management and cleanup
  • HTTP client operations
  • Configuration handling
  • Error handling patterns
  • Process monitoring

Summary

Functions

Safely terminates a runtime process with proper cleanup.

Formats error messages consistently across runtimes.

Gets OS PID from port with error handling.

Handles file change events with async processing and timeout. Uses non-blocking approach to prevent GenServer blocking.

Standardized error handling for runtime operations.

Makes HTTP request to runtime server with standardized error handling.

Merges user configuration with defaults, applying validation.

Normalizes exit reasons for consistent process termination.

Creates port options for spawning runtime processes.

Creates environment variables for runtime processes.

Validates configuration values with proper error messages.

Functions

cleanup_runtime_process(runtime_port, reason)

@spec cleanup_runtime_process(port(), term()) :: term()

Safely terminates a runtime process with proper cleanup.

format_error(reason)

@spec format_error(term()) :: String.t()

Formats error messages consistently across runtimes.

get_port_os_pid(runtime_port)

@spec get_port_os_pid(port()) :: integer() | nil

Gets OS PID from port with error handling.

handle_file_change(path, bundle_module, bundle_args, timeout \\ 5000)

@spec handle_file_change(String.t(), module(), keyword(), timeout()) ::
  :ok | {:error, term()}

Handles file change events with async processing and timeout. Uses non-blocking approach to prevent GenServer blocking.

handle_result(result)

@spec handle_result(term()) :: {:ok, term()} | {:error, String.t()}

Standardized error handling for runtime operations.

Returns {:ok, result} or {:error, reason} tuples consistently.

make_http_request(server_port, method, component, props, timeout)

Makes HTTP request to runtime server with standardized error handling.

merge_config(user_config, defaults, runtime_name)

@spec merge_config(keyword(), keyword(), atom()) ::
  {:ok, keyword()} | {:error, String.t()}

Merges user configuration with defaults, applying validation.

normalize_exit_reason(reason)

@spec normalize_exit_reason(term()) :: term()

Normalizes exit reasons for consistent process termination.

port_options(executable, args, opts \\ [])

@spec port_options(String.t(), [String.t()], keyword()) :: keyword()

Creates port options for spawning runtime processes.

runtime_env(port_var, port_value, env_var, env_value)

@spec runtime_env(String.t(), String.t(), String.t(), String.t()) :: [
  {charlist(), charlist()}
]

Creates environment variables for runtime processes.

validate_config(config, runtime_name)

@spec validate_config(
  keyword(),
  atom()
) :: {:ok, keyword()} | {:error, String.t()}

Validates configuration values with proper error messages.