-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(vim.net): vim.net.download #29327
base: master
Are you sure you want to change the base?
Conversation
Huh, it looks like Ubuntu 22 has curl version 7.81 |
runtime/doc/lua.txt
Outdated
-- Download a file to a path | ||
-- The file will be saved in `/tmp/somefile` | ||
vim.net.download("https://httpbingo.org/anything", { | ||
download_location = "tmp/somefile", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe as
or to
or target
would be shorter? People will specify this virtually always.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this also support specifying a target directory? I think this would be useful, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this also support specifying a target directory? I think this would be useful, too.
Currently, no. A workaround may be to let users pass a cwd
to execute curl
there (and hence download the file on that directory)
Regarding curl, I think we have to take what we get: check version (and features! distros can and do compile the same version differently) and fail gracefully if an requested option is not supported. In my opinion, this is the most important feature a Regarding flags and options, I would start simple and add things on request. From painful experience with nvim-treesitter, proxies are a very common request, so that should work out-of-the-box. (We also need to follow redirects because of Github moving cheese frequently (repo renaming etc.)) One thing we could add as part of this PR is use it for downloading spellfiles (mostly as a way for free testing). |
I think we need to also discuss global configuration, as things like proxy settings are user-specific, while the caller of Something like |
I was adding checks for curls versions and I had a thought (regarding the returned metadata mostly). I fell like this may be the wrong approach (?). If a plugin (or even neovim core itself) relies in feature A that's not available in some user's curl installation, The main examples I can think of right now are:
For some features, the download could continue with a warning (metadata), but for others (avoid overriding files) it may be dangerous to do so. |
Let's not overcomplicate this. Pick a minimum version that includes all features we may want, and then abort with error if that minimum version isn't available. |
Is there some requirement for picking a minimal |
Another question. My initial implementation of edit: #3027 seems to simply |
Whatever version is needed to allow vim.net.download to do what it needs to do. If you can't come up with a minimum version for now don't force it, it should become apparent anyway when some operation doesn't work.
Doesn't matter. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
We don't need both, they do behave very similarly. The last commit on #29104 only uses |
@justinmk is there something else you would like to be removed from the interface? |
runtime/doc/lua.txt
Outdated
Lua module: vim.net *vim.net* | ||
|
||
vim.net.download({url}, {opts}) *vim.net.download()* | ||
Asynchronously download a file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A typical pattern is that passing a callback makes it async, and omitting a callback makes it synchronous.
Something to ask is, why is the as
(aka file
) option necessary? Is it awkward for the caller to write the result to a file using Nvim's stdlib? If so, why, and can we fix that? I would expect it to be as simple as:
-- Asyndc
vim.net.request('https://...', {}, function(r)
vim.fn.writefile(r.data, '/my/file')
end)
-- Sync
local r = vim.net.request('https://...', {})
Also: we are currently figuring out a long-term story for promises/async. But until then, we should try to use common patterns. vim.system()
returns an object that provides :wait()
. Should we mimic that pattern here? I think I prefer the "optional callback" pattern of libuv/luv, but @lewis6991 may be able to comment on whether the :wait()
pattern has advantages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something to ask is, why is the as (aka file) option necessary?
I haven't thought this thoroughly. The first reason I can think of is that it may not always be desirable to need to pass the downloaded file through Neovim to write it. Downloading a big file may cause memory issues (?) (but then, this may be an edge case(?))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Advantages of wait()
is that it:
-
makes it slightly easier to type annotate as the object always returns the same type.
-
generally more versatile. I.e. you might want to submit multiple tasks and then wait on all of them. It's possible to do this with the other method of providing a dummy callback, but is ugly.
local a = foo(function() end) local b = foo(function() end) local c = foo(function() end) a:wait() b:wait() c:cancel()
I think there is merit in the other pattern, and I don't think we need to restrict ourselves to either one.
I think for vim.net
, the wait()
pattern might pan out better longer term. For low level uv functions, the other pattern works well.
As stated in #29104 (comment) this PR aims to simply provide
vim.net.download
.Notes:
curl
should be supported? I annotated the options that were introduced in a specific version (at least the ones thatman curl
mentions) in the code for now (e.g.(Added in 7.88.0)
)Are the current defaults ok? I copied thecurl
defaults (i.e.curl
does not follow redirects by default, so neither doesvim.net.download
).curl
options shouldn't be supported? I introduced options likecompressed
that may be overkill (?). For example, it seems like thecurl
shipped with Windows did not supported this option (at least when it was first introduced. The shippedcurl
in my windows machine does not complain when I use--compressed
)