diff options
| author | William Boman <william@redwill.se> | 2023-06-22 00:03:07 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-22 00:03:07 +0200 |
| commit | 82cae550c87466b1163b216bdb9c71cb71dd8f67 (patch) | |
| tree | af0571ec1fd59e11c780494d9ef64f189442d7e5 | |
| parent | feat(fetch): include mason.nvim version in User-Agent (#1362) (diff) | |
| download | mason-82cae550c87466b1163b216bdb9c71cb71dd8f67.tar mason-82cae550c87466b1163b216bdb9c71cb71dd8f67.tar.gz mason-82cae550c87466b1163b216bdb9c71cb71dd8f67.tar.bz2 mason-82cae550c87466b1163b216bdb9c71cb71dd8f67.tar.lz mason-82cae550c87466b1163b216bdb9c71cb71dd8f67.tar.xz mason-82cae550c87466b1163b216bdb9c71cb71dd8f67.tar.zst mason-82cae550c87466b1163b216bdb9c71cb71dd8f67.zip | |
feat(fetch): add explicit default timeout to requests (#1364)
| -rw-r--r-- | lua/mason-core/fetch.lua | 10 | ||||
| -rw-r--r-- | tests/mason-core/fetch_spec.lua | 4 |
2 files changed, 12 insertions, 2 deletions
diff --git a/lua/mason-core/fetch.lua b/lua/mason-core/fetch.lua index 545c8440..c8a8591c 100644 --- a/lua/mason-core/fetch.lua +++ b/lua/mason-core/fetch.lua @@ -10,6 +10,8 @@ local version = require "mason.version" local USER_AGENT = ("mason.nvim %s (+https://github.com/williamboman/mason.nvim)"):format(version.VERSION) +local TIMEOUT_SECONDS = 30 + ---@alias FetchMethod ---| '"GET"' ---| '"POST"' @@ -47,8 +49,9 @@ local function fetch(url, opts) if opts.out_file then platform_specific = function() return powershell.command( - ([[iwr %s -UseBasicParsing -Method %q -Uri %q %s -OutFile %q;]]):format( + ([[iwr %s -TimeoutSec %s -UseBasicParsing -Method %q -Uri %q %s -OutFile %q;]]):format( headers, + TIMEOUT_SECONDS, opts.method, url, opts.data and ("-Body %s"):format(opts.data) or "", @@ -59,8 +62,9 @@ local function fetch(url, opts) else platform_specific = function() return powershell.command( - ([[Write-Output (iwr %s -Method %q -UseBasicParsing %s -Uri %q).Content;]]):format( + ([[Write-Output (iwr %s -TimeoutSec %s -Method %q -UseBasicParsing %s -Uri %q).Content;]]):format( headers, + TIMEOUT_SECONDS, opts.method, opts.data and ("-Body %s"):format(opts.data) or "", url @@ -80,6 +84,7 @@ local function fetch(url, opts) "/dev/null", "-O", opts.out_file or "-", + ("--timeout=%s"):format(TIMEOUT_SECONDS), ("--method=%s"):format(opts.method), opts.data and { ("--body-data=%s"):format(opts.data) or vim.NIL, @@ -107,6 +112,7 @@ local function fetch(url, opts) }, opts.data and { "-d", "@-" } or vim.NIL, opts.out_file and { "-o", opts.out_file } or vim.NIL, + { "--connect-timeout", TIMEOUT_SECONDS }, url, on_spawn = a.scope(function(_, stdio) local stdin = stdio[1] diff --git a/tests/mason-core/fetch_spec.lua b/tests/mason-core/fetch_spec.lua index 39351abe..107b6417 100644 --- a/tests/mason-core/fetch_spec.lua +++ b/tests/mason-core/fetch_spec.lua @@ -32,6 +32,7 @@ describe("fetch", function() "/dev/null", "-O", "-", + "--timeout=30", "--method=GET", vim.NIL, -- body-data "https://api.github.com", @@ -54,6 +55,7 @@ describe("fetch", function() match.same { "-X", "GET" }, vim.NIL, -- data vim.NIL, -- out file + match.same { "--connect-timeout", 30 }, "https://api.github.com", on_spawn = match.is_function(), }) @@ -93,6 +95,7 @@ describe("fetch", function() "/dev/null", "-O", "/test.json", + "--timeout=30", "--method=GET", vim.NIL, -- body-data "https://api.github.com/data", @@ -111,6 +114,7 @@ describe("fetch", function() match.same { "-X", "GET" }, vim.NIL, -- data match.same { "-o", "/test.json" }, + match.same { "--connect-timeout", 30 }, "https://api.github.com/data", on_spawn = match.is_function(), }) |
