Skip to content
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: add geolocation data to Netlify Dev #4566

Merged
merged 9 commits into from
Apr 27, 2022
Merged

Conversation

eduardoboucas
Copy link
Member

Summary

Adds geolocation data to Netlify Dev when using Edge Functions. It retrieves the geolocation data from https://netlifind.netlify.app/ and caches it locally for 24h, to avoid making a request every time the app starts.

When the request fails, or when CLI is used with the --offline flag, we use a mock location (San Francisco).

A new --geo flag is added to the dev command with the following values:

  • cache (default): Uses the geolocation cached locally if it's not older than 24h. Otherwise, it retrieves it from the API.
  • update: Retrieves geolocation data from the API, even if there is a cached version.
  • mock: Does not make any requests to the API and uses the mock location.

@eduardoboucas eduardoboucas added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Apr 26, 2022
@github-actions
Copy link

github-actions bot commented Apr 26, 2022

📊 Benchmark results

Comparing with 2c58896

Package size: 273 MB

(no change)

^  380 MB  380 MB  380 MB  380 MB  380 MB  380 MB  380 MB  380 MB  380 MB  380 MB  380 MB  380 MB         
│   ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐          
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |          
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |          
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |          
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |          
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |   273 MB 
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    ┌──┐  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-12    T-11    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

@ascorbic
Copy link
Contributor

It might be good to add a check for user-agent in the API, to stop somebody deciding to use it as a free, unlimited geoip service

@eduardoboucas
Copy link
Member Author

It might be good to add a check for user-agent in the API, to stop somebody deciding to use it as a free, unlimited geoip service

The service will return geolocation data only for the requesting IP, as opposed to accepting an IP address as input, so it will be less useful as a service? Also, I'm not sure that checking the UA will help, since it can be easily spoofed.

@eduardoboucas eduardoboucas requested a review from a team as a code owner April 26, 2022 16:19
@ascorbic
Copy link
Contributor

The scenario I'm thinking about is somebody using it in their site to get the visitor's IP. It wouldn;t be spoofable then as it's the user's browser.

@mraerino
Copy link
Contributor

mraerino commented Apr 26, 2022

long-term we should require an api token from the user. We can't easily build that in right now because the api tokens can only be verified by bitballoon and not as stateless JWTs.
if it should become a problem soon, we should build the endpoint into bitballoon instead.

technically anybody could build such an api with edge functions though. it would just be easier for us to kick them off our service.

@mraerino
Copy link
Contributor

mraerino commented Apr 26, 2022

you don't need to verify the user-agent though since CORS prevents usage from other websites through browsers

@ascorbic
Copy link
Contributor

I do forget that CORS exists for purposes other than wasting mornings on broken header configurations.

mraerino
mraerino previously approved these changes Apr 27, 2022
Copy link
Contributor

@mraerino mraerino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

still slightly terrified of reviewing non-typescript code 🙈

@eduardoboucas
Copy link
Member Author

still slightly terrified of reviewing non-typescript code 🙈

And I'm terrified of writing it! 🙊

@ascorbic
Copy link
Contributor

It's not quite the same, but // @ts-check with JSDoc types are better than nothing.

@eduardoboucas
Copy link
Member Author

It's not quite the same, but // @ts-check with JSDoc types are better than nothing.

Totally. We've been doing this in every new file we add to the CLI, and it was missing from this PR. Added in 2f6c520.

I've also fixed some tests.

Could I get another ✅ , please?

@eduardoboucas eduardoboucas requested a review from mraerino April 27, 2022 09:56
@eduardoboucas eduardoboucas merged commit f35b14c into main Apr 27, 2022
@eduardoboucas eduardoboucas deleted the feat/local-geo branch April 27, 2022 10:32
This was referenced May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants