Conversation
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
|
TODO: integrate implementation from https://github.com/digidem/mapbox-style-transformer instead of the one done here |
@gmaclennan Haven't done this yet because the module needs to a new version released to be usable. |
src/routes/maps.ts
Outdated
| // Can use `?mapbox_access_token=...` to override the access token used | ||
| // for upstream mapbox style request | ||
| if (defaultOnlineStyleUrl.toString().startsWith(BASE_MAPBOX_API_URL)) { | ||
| const accessTokenFromRequest = Array.isArray( | ||
| request.query['mapbox_access_token'], | ||
| ) | ||
| ? request.query['mapbox_access_token'][0] | ||
| : request.query['mapbox_access_token'] | ||
|
|
||
| // Prioritize the access token from the request | ||
| if (accessTokenFromRequest) { | ||
| defaultOnlineStyleUrl.searchParams.set( | ||
| 'access_token', | ||
| accessTokenFromRequest, | ||
| ) | ||
| } | ||
| } |
There was a problem hiding this comment.
I thought this could be a useful feature to support. Still undecided on that and whether the access token in the request should be scoped to the specific use case i.e. mapbox_access_token vs access_token.
There was a problem hiding this comment.
I don't see the use-case for this and I think this is potentially confusing. This is only used for the defaultOnlineStyleUrl (a static option passed to the constructor) which generally must include an ?access_token= search param if it is a mapbox URL. Having this possibly overridable and therefore also optional I think confuses the API with not a lot of benefit. We won't be using this in CoMapeo, and this is a temporary measure anyway until we move away from mapbox as the default online style, so I think we should just remove this.
There was a problem hiding this comment.
the situation this accounts for is when the access token you pass to the static option no longer works (because of expiry, deletion, etc) which has happened to us before 😄 being able to override it at the request level allows for workarounds in those situations without requiring new releases of the app, although that assumes that there's some client-side exposure for configuring your own access token.
but yeah, probably not worth trying to account for in this case. just trauma-based failure mode handling
There was a problem hiding this comment.
I think we can address this risk a different way down the line.
src/routes/maps.ts
Outdated
| }) | ||
| } | ||
| } else { | ||
| response?.body?.cancel() // Close the connection |
There was a problem hiding this comment.
is this still necessary, or even placed in the right location?
There was a problem hiding this comment.
This is only necessary if you return a 302 response without consuming the upstream response body. The code as-is now always consumes the body, so this is not necessary, however if you make the suggested changes, you will still need to do this if you do not consume the body (because it will be left hanging and the connection will remain open).
src/routes/maps.ts
Outdated
| if (madeChanges) { | ||
| return new Response(JSON.stringify(body), { | ||
| status: 200, | ||
| headers: baseHeaders, | ||
| }) | ||
| } else { | ||
| return new Response(null, { | ||
| status: 302, | ||
| headers: { | ||
| ...baseHeaders, | ||
| location: url.toString(), | ||
| }, | ||
| }) | ||
| } |
There was a problem hiding this comment.
I thought this could make sense but maybe it's overcomplicating things. On the bright side, means I don't have to fix the existing tests 😄
There was a problem hiding this comment.
Yes, I think overcomplicating things.
gmaclennan
left a comment
There was a problem hiding this comment.
I think this over-engineers things a bit. This is only a temporary transformation of the defaultOnlineStyleUrl until we switch to using a non-mapbox style, and it currently adds quite a few extra code paths to the code that need testing and maintaining, and a new feature of making the access token optional on the defaultOnlineStyleUrl and possibly overridable from the API. This seems to be moving towards a broader long-term support of mapbox styles in this package, which I don't think is needed at this stage, and we may not ever do.
src/routes/maps.ts
Outdated
| // Can use `?mapbox_access_token=...` to override the access token used | ||
| // for upstream mapbox style request | ||
| if (defaultOnlineStyleUrl.toString().startsWith(BASE_MAPBOX_API_URL)) { | ||
| const accessTokenFromRequest = Array.isArray( | ||
| request.query['mapbox_access_token'], | ||
| ) | ||
| ? request.query['mapbox_access_token'][0] | ||
| : request.query['mapbox_access_token'] | ||
|
|
||
| // Prioritize the access token from the request | ||
| if (accessTokenFromRequest) { | ||
| defaultOnlineStyleUrl.searchParams.set( | ||
| 'access_token', | ||
| accessTokenFromRequest, | ||
| ) | ||
| } | ||
| } |
There was a problem hiding this comment.
I don't see the use-case for this and I think this is potentially confusing. This is only used for the defaultOnlineStyleUrl (a static option passed to the constructor) which generally must include an ?access_token= search param if it is a mapbox URL. Having this possibly overridable and therefore also optional I think confuses the API with not a lot of benefit. We won't be using this in CoMapeo, and this is a temporary measure anyway until we move away from mapbox as the default online style, so I think we should just remove this.
src/routes/maps.ts
Outdated
| const body = await response.json() | ||
|
|
||
| const madeChanges = transformUrls(body, { | ||
| accessToken: | ||
| defaultOnlineStyleUrl.searchParams.get('access_token') || undefined, | ||
| }) |
There was a problem hiding this comment.
This unnecessarily loads the body of all style requests, when we only need to do it for the default online style URL. Also I don't think we need to load the body for non-mapbox styles. Currently the "is it a mapbox style" question is done by checking whether the transormUrls() funciton returns true. I think the detection used above (does the defaultOnlineStyleUrl start with BASE_MAPBOX_API_URL) is better, and that way we don't need to load the body of requests for non-mapbox styles
There was a problem hiding this comment.
make sense. based on that, then the more desirable behavior is the following?
-
for non-mapbox styles, there's no need to load the body and we keep the existing redirect behavior
-
for mapbox styles, we load the body, attempt to tranform, and provide a 200 response with the maybe-transformed body? basically, remove the conditional response type based on whether any transforms were actually made
There was a problem hiding this comment.
I think the detection used above (does the defaultOnlineStyleUrl start with BASE_MAPBOX_API_URL) is better
Just noting that doing this wouldn't account for online style urls whose payloads include fields that reference mapbox using their URIs. definitely an edge case but just highlighting that.
There was a problem hiding this comment.
Yes I can't think of a situation where someone would choose to host a style that references MapBox resources somewhere other than MapBox, and in any case we are only really handling the case of our current default map tiles
There was a problem hiding this comment.
make sense. based on that, then the more desirable behavior is the following?
for non-mapbox styles, there's no need to load the body and we keep the existing redirect behavior
for mapbox styles, we load the body, attempt to tranform, and provide a 200 response with the maybe-transformed body? basically, remove the conditional response type based on whether any transforms were actually made
Yes
src/routes/maps.ts
Outdated
| if (madeChanges) { | ||
| return new Response(JSON.stringify(body), { | ||
| status: 200, | ||
| headers: baseHeaders, | ||
| }) | ||
| } else { | ||
| return new Response(null, { | ||
| status: 302, | ||
| headers: { | ||
| ...baseHeaders, | ||
| location: url.toString(), | ||
| }, | ||
| }) | ||
| } |
There was a problem hiding this comment.
Yes, I think overcomplicating things.
src/routes/maps.ts
Outdated
| }) | ||
| } | ||
| } else { | ||
| response?.body?.cancel() // Close the connection |
There was a problem hiding this comment.
This is only necessary if you return a 302 response without consuming the upstream response body. The code as-is now always consumes the body, so this is not necessary, however if you make the suggested changes, you will still need to do this if you do not consume the body (because it will be left hanging and the connection will remain open).
src/routes/maps.ts
Outdated
| // Can use `?mapbox_access_token=...` to override the access token used | ||
| // for upstream mapbox style request | ||
| if (defaultOnlineStyleUrl.toString().startsWith(BASE_MAPBOX_API_URL)) { | ||
| const accessTokenFromRequest = Array.isArray( | ||
| request.query['mapbox_access_token'], | ||
| ) | ||
| ? request.query['mapbox_access_token'][0] | ||
| : request.query['mapbox_access_token'] | ||
|
|
||
| // Prioritize the access token from the request | ||
| if (accessTokenFromRequest) { | ||
| defaultOnlineStyleUrl.searchParams.set( | ||
| 'access_token', | ||
| accessTokenFromRequest, | ||
| ) | ||
| } | ||
| } |
There was a problem hiding this comment.
guess this file wasn't updated to be formatted according to the prettier config?
Fixes #29
Mapbox style fixture is taken from here. Might consider just creating much smaller fixture that covers the properties of interest.