R/runtime-versions.R

Defines functions warn_if_electron_unsupported electron_support_problem electron_latest_version resolve_runtime_version

Documented in electron_latest_version electron_support_problem resolve_runtime_version warn_if_electron_unsupported

#' Resolve the runtime version to use for a build
#'
#' An explicit `dependencies.<runtime>.version` in config wins. For R, unset
#' and the literal `"latest"` both give the newest portable R for the
#' platform, from [r_portable_latest_version()], which falls back to the
#' version in `SHINYELECTRON_DEFAULTS$runtime_versions` when the lookup fails.
#' For Python and Electron, `"latest"` calls the live resolver and unset gives
#' the maintained pin in `SHINYELECTRON_DEFAULTS$runtime_versions`.
#'
#' @param runtime One of `"r"`, `"python"`, `"electron"`.
#' @param config Full app configuration list.
#' @param platform Character string or `NULL`. Target platform of the R
#'   lookup, `NULL` for the build machine. Python and Electron ignore it.
#' @return Character version string.
#' @keywords internal
resolve_runtime_version <- function(runtime, config, platform = NULL) {
  runtime <- match.arg(runtime, c("r", "python", "electron"))
  pins <- SHINYELECTRON_DEFAULTS$runtime_versions
  configured <- config$dependencies[[runtime]]$version

  if (!is.null(configured) && !identical(configured, "latest")) {
    return(configured)
  }
  if (runtime == "r") {
    return(r_portable_latest_version(platform))
  }
  if (identical(configured, "latest")) {
    return(switch(runtime,
      python   = python_resolve_pbs("latest")$version,
      electron = electron_latest_version()
    ))
  }
  switch(runtime,
    python   = pins$python$version,
    electron = pins$electron
  )
}

#' Fetch the latest published Electron version from the npm registry
#'
#' Queries `https://registry.npmjs.org/electron/latest` and returns the
#' `version` field as a character string. Used when
#' `dependencies$electron$version` is set to `"latest"`.
#'
#' @return Character version string (e.g. `"45.5.0"`).
#' @keywords internal
electron_latest_version <- function() {
  url <- "https://registry.npmjs.org/electron/latest"
  tmp <- tempfile(fileext = ".json")
  on.exit(unlink(tmp), add = TRUE)
  status <- tryCatch(
    utils::download.file(url, tmp, quiet = TRUE),
    error = function(e) {
      cli::cli_abort(c(
        "Failed to fetch latest Electron version from npm registry",
        "x" = "{conditionMessage(e)}",
        "i" = "Check your internet connection or set {.field dependencies$electron$version} to a specific version"
      ))
    }
  )
  if (!identical(status, 0L)) {
    cli::cli_abort(c(
      "Failed to fetch latest Electron version from npm registry",
      "x" = "HTTP request returned status {status}",
      "i" = "Check your internet connection or set {.field dependencies$electron$version} to a specific version"
    ))
  }
  meta <- jsonlite::fromJSON(tmp)
  as.character(meta$version)
}

#' Describe why the Electron version an export uses is out of support
#'
#' Electron supports only its newest `ELECTRON_SUPPORTED_MAJORS` major
#' versions. The newest is looked up with [electron_latest_version()] under a
#' five-second timeout, which is restored on exit. The check is advisory, so it
#' returns `NULL` when the version is `"latest"`, cannot be read as a version
#' number, or the lookup fails.
#'
#' @param config Full app configuration list.
#' @return A single sentence, or `NULL` when there is no problem to report.
#' @keywords internal
electron_support_problem <- function(config) {
  configured <- config$dependencies$electron$version
  if (identical(configured, "latest")) {
    return(NULL)
  }
  version <- resolve_runtime_version("electron", config)
  in_use <- suppressWarnings(as.integer(sub("^v?([0-9]+)\\..*$", "\\1", version)))
  if (length(in_use) != 1L || is.na(in_use) || !grepl("^v?[0-9]+\\.", version)) {
    return(NULL)
  }

  old <- options(timeout = 5)
  on.exit(options(old), add = TRUE)
  newest <- tryCatch(
    suppressWarnings(electron_latest_version()),
    error = function(e) NULL
  )
  newest <- suppressWarnings(as.integer(sub("^v?([0-9]+)\\..*$", "\\1", newest)))
  if (length(newest) != 1L || is.na(newest)) {
    return(NULL)
  }

  oldest_supported <- newest - ELECTRON_SUPPORTED_MAJORS + 1L
  if (in_use >= oldest_supported) {
    return(NULL)
  }
  paste0(
    "Electron ", version, " is out of support. Electron fixes security problems ",
    "only in its newest ", ELECTRON_SUPPORTED_MAJORS, " major versions, currently ",
    oldest_supported, " to ", newest, "."
  )
}

#' Warn when an export uses an Electron that is out of support
#'
#' @param config Full app configuration list.
#' @return `NULL`, invisibly.
#' @keywords internal
warn_if_electron_unsupported <- function(config) {
  problem <- electron_support_problem(config)
  if (!is.null(problem)) {
    cli::cli_warn(
      c(
        "{problem}",
        "i" = "Set {.field dependencies.electron.version} to a supported version.",
        "i" = "Or update shinyelectron, which moves its pinned version forward."
      ),
      class = "shinyelectron_electron_unsupported"
    )
  }
  invisible(NULL)
}

Try the shinyelectron package in your browser

Any scripts or data that you put into this service are public.

shinyelectron documentation built on Dec. 3, 2026, 9:07 a.m.