diff --git a/DESCRIPTION b/DESCRIPTION index d2576f2b9..50bd204fe 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -70,7 +70,7 @@ Config/needs/dependencies: pkgbuild, r-lib/pkgcache, r-lib/pkgdepends, - pkgsearch, + r-hub/pkgsearch, processx, ps, Config/Needs/website: diff --git a/src/library/pkgsearch/DESCRIPTION b/src/library/pkgsearch/DESCRIPTION index dfd019d70..2b0d9fb50 100644 --- a/src/library/pkgsearch/DESCRIPTION +++ b/src/library/pkgsearch/DESCRIPTION @@ -1,6 +1,6 @@ Package: pkgsearch Title: Search and Query CRAN R Packages -Version: 3.1.3 +Version: 3.1.3.9000 Authors@R: c( person("Gábor", "Csárdi", , "csardi.gabor@gmail.com", role = c("aut", "cre")), person("Maëlle", "Salmon", role = "aut", @@ -22,12 +22,11 @@ Suggests: covr, memoise, mockery, pillar, pingr (>= 2.0.0), rstudioapi, withr Config/testthat/edition: 3 Encoding: UTF-8 -RoxygenNote: 7.2.0.9000 +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.3.1.9000 NeedsCompilation: no -Packaged: 2023-12-10 13:34:02 UTC; gaborcsardi +Packaged: 2024-09-24 07:38:50 UTC; gaborcsardi Author: Gábor Csárdi [aut, cre], Maëlle Salmon [aut] (), R Consortium [fnd] Maintainer: Gábor Csárdi -Repository: CRAN -Date/Publication: 2023-12-10 14:00:03 UTC diff --git a/src/library/pkgsearch/NEWS.md b/src/library/pkgsearch/NEWS.md index 4909db5a3..7fde0821f 100644 --- a/src/library/pkgsearch/NEWS.md +++ b/src/library/pkgsearch/NEWS.md @@ -1,3 +1,8 @@ +# pkgsearch (development version) + +* pkgsearch now uses the `timeout` option to set the limit for the total + time of each HTTP request (#125, @gladkia). + # pkgsearch 3.1.3 * No user visible changes. diff --git a/src/library/pkgsearch/R/advanced_search.R b/src/library/pkgsearch/R/advanced_search.R index 1a9b007d5..9ed2da180 100644 --- a/src/library/pkgsearch/R/advanced_search.R +++ b/src/library/pkgsearch/R/advanced_search.R @@ -92,10 +92,9 @@ advanced_search <- function(..., json = NULL, format = c("short", "long"), qstr <- json } - server <- Sys.getenv("R_PKG_SEARCH_SERVER", "search.r-pkg.org") - port <- as.integer(Sys.getenv("R_PKG_SEARCH_PORT", "80")) + server <- Sys.getenv("R_PKG_SEARCH_SERVER", "https://search.r-pkg.org") - resp <- do_query(qstr, server, port, from, size) + resp <- do_query(qstr, server, from, size) result <- format_result( resp, @@ -104,7 +103,6 @@ advanced_search <- function(..., json = NULL, format = c("short", "long"), from = from, size = size, server = server, - port = port, qstr = qstr ) diff --git a/src/library/pkgsearch/R/api.R b/src/library/pkgsearch/R/api.R index 9d8e7c869..7bc884d5b 100644 --- a/src/library/pkgsearch/R/api.R +++ b/src/library/pkgsearch/R/api.R @@ -68,10 +68,9 @@ pkg_search <- function(query = NULL, format = c("short", "long"), if (is.null(query)) return(pkg_search_again()) format <- match.arg(format) - server <- Sys.getenv("R_PKG_SEARCH_SERVER", "search.r-pkg.org") - port <- as.integer(Sys.getenv("R_PKG_SEARCH_PORT", "80")) + server <- Sys.getenv("R_PKG_SEARCH_SERVER", "https://search.r-pkg.org") - make_pkg_search(query, format, from, size, server, port) + make_pkg_search(query, format, from, size, server) } #' @rdname pkg_search @@ -79,13 +78,12 @@ pkg_search <- function(query = NULL, format = c("short", "long"), ps <- pkg_search -make_pkg_search <- function(query, format, from, size, server, port) { +make_pkg_search <- function(query, format, from, size, server) { qry <- make_query(query = query) - rsp <- do_query(qry, server = server, port = port, from = from, - size = size) + rsp <- do_query(qry, server = server, from = from, size = size) rst <- format_result(rsp, query = query, format = format, from = from, - size = size, server = server, port = port) + size = size, server = server) s_data$prev_q <- list(type = "simple", result = rst) @@ -108,8 +106,7 @@ more <- function(format = NULL, size = NULL) { format = format %||% meta(rst)$format, from = meta(rst)$from + meta(rst)$size, size = size %||% meta(rst)$size, - server = meta(rst)$server, - port = meta(rst)$port + server = meta(rst)$server ) } else if (s_data$prev_q$type == "advanced") { @@ -183,17 +180,20 @@ make_query <- function(query) { ) } -do_query <- function(query, server, port, from, size) { +do_query <- function(query, server, from, size) { check_count(from) check_count(size) - url <- "http://" %+% server %+% ":" %+% as.character(port) %+% - "/package/_search?from=" %+% as.character(from - 1) %+% - "&size=" %+% as.character(size) + # timeout for the curl's connect phase (in seconds) + timeout <- getOption("timeout", 60) + + url <- server %+% "/package/_search?from=" %+% + as.character(from - 1) %+% "&size=" %+% as.character(size) result <- http_post( url, body = query, - headers = c("Content-Type" = "application/json")) + headers = c("Content-Type" = "application/json"), + options = list(timeout = timeout)) chain_error( http_stop_for_status(result), new_query_error(result, "search server failure") @@ -236,8 +236,7 @@ print.pkgsearch_query_error <- function(x, ...) { invisible(x) } -format_result <- function(result, query, format, from, size, server, - port, ...) { +format_result <- function(result, query, format, from, size, server, ...) { result <- jsonlite::fromJSON(result, simplifyVector = FALSE) meta <- list( @@ -246,7 +245,6 @@ format_result <- function(result, query, format, from, size, server, from = from, size = size, server = server, - port = port, total = result$hits$total, max_score = result$hits$max_score, took = result$took, diff --git a/src/library/pkgsearch/R/http.R b/src/library/pkgsearch/R/http.R index 921bec382..59e4b61cd 100644 --- a/src/library/pkgsearch/R/http.R +++ b/src/library/pkgsearch/R/http.R @@ -1,3 +1,21 @@ +get_default_curl_options <- function(options) { + getopt <- function(nm) { + if (!is.null(v <- options[[nm]])) return(v) + anm <- paste0("async_http_", nm) + if (!is.null(v <- getOption(anm))) return(v) + if (!is.na(v <- Sys.getenv(toupper(anm), NA_character_))) return (v) + } + modifyList( + options, + drop_nulls(list( + timeout = as.integer(getopt("timeout") %||% 0), + connecttimeout = as.integer(getopt("connecttimeout") %||% 300), + low_speed_time = as.integer(getopt("low_speed_time") %||% 0), + low_speed_limit = as.integer(getopt("low_speed_limit") %||% 0), + cainfo = getopt("cainfo") + )) + ) +} http_get <- function(url) { curl::curl_fetch_memory(url) diff --git a/src/library/pkgsearch/R/utils.R b/src/library/pkgsearch/R/utils.R index 9749c850a..14019dacf 100644 --- a/src/library/pkgsearch/R/utils.R +++ b/src/library/pkgsearch/R/utils.R @@ -80,9 +80,9 @@ contains <- function(x, y) y %in% x isin <- function(x, y) x %in% y remove_special <- function(list, level = 1) { - + assert_that(is_positive_count(level)) - + if (level == 1) { replace( grepl(pattern = "^_", names(list)), @@ -92,7 +92,7 @@ remove_special <- function(list, level = 1) { } else { lapply(list, remove_special, level = level - 1) } - + } pluck <- function(list, idx) list[[idx]] @@ -104,9 +104,9 @@ needs_packages <- function(pkgs) { if (!all(has)) { not_installed_pkgs <- pkgs[!has] - + if (length(not_installed_pkgs) == 1) { - + throw(new_error( "The ", sQuote(not_installed_pkgs), @@ -114,7 +114,7 @@ needs_packages <- function(pkgs) { call. = FALSE )) } else { - + throw(new_error( "The ", paste(sQuote(not_installed_pkgs), collapse = ", "), @@ -122,7 +122,7 @@ needs_packages <- function(pkgs) { call. = FALSE )) } - + } } @@ -133,3 +133,7 @@ clean_description <- function(txt) { zap_null <- function(x) { x[! map_lgl(x, is.null)] } + +drop_nulls <- function (x) { + x[!map_lgl(x, is.null)] +}