feross / simple-get

Simplest way to make http get requests. Supports HTTPS, redirects, gzip/deflate, streams in < 100 lines
MIT License
401 stars 50 forks source link

remove require('url') in favor of URL #55

Open Flet opened 5 years ago

Flet commented 5 years ago

Closes #53

This also updates travis to use node versions 12 and 10 for tests.

node 8.x is at EOL starting December 2019.

feross commented 4 years ago

Could you explain a bit more about the changes, like why you were able to remove the different code paths for relative/absolute URLs, and why you're now passing href in the opts object. Thanks!

LinusU commented 2 years ago

I wanted to land this so rebased on master. Unfortunately, this was harder than I thought!

Since the current API allows passing any different combinations of options that is being feed right into http.request it's very hard to keep the current API whilst moving to using URL. I added two failing test cases that demonstrates two of the shortcomings.

I'm thinking that the best way forward is a new major release which explicitly states which options are valid, and only allows the new WHATWG URL based ones:

Potentially also switching url for href:


For reference, here is the code to turn Node.js style opts into an url string, which we would need in order to pass it to as the base parameter to new URL:

https://github.com/nodejs/node/blob/4712472c0fa1fbd7df74541698659b85313ab576/lib/url.js#L614-L698

Here is a util function that turns an URL into the expected request options, not too complicated:

https://github.com/nodejs/node/blob/30bdee20ee3a24fa12958c7fad51d9b174c765ad/lib/internal/url.js#L1395-L1418


edit: Hmm, I actually manage to get a version with every test passing, but there is a lot of code added in order for this, which I think that we should avoid...

You can see it here: https://github.com/feross/simple-get/compare/url-linusu-wip

The out-commented setOptionsFromUrl is needed in order to support Node.js 10.0.0 - 10.8.0 😅