Skip to content

Commit

Permalink
baseApiUrl w/ path & normalizeTileURL functionality
Browse files Browse the repository at this point in the history
- adds regex to trim tileURL prefix to /v4/...
- adds filter to makeAPIURL preventing duplication of param 'access_token'
- reorders params on test case -> sku,access_token

closes #8458
  • Loading branch information
Arni Sumarlidason committed Jul 11, 2019
1 parent be7e27f commit 9f8fbc6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/util/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ export class RequestManager {

const urlObject = parseUrl(tileURL);
const imageExtensionRe = /(\.(png|jpg)\d*)(?=$)/;
const tileURLAPIPrefixRe = /^.+\/v4\//;

// The v4 mapbox tile API supports 512x512 image tiles only when @2x
// is appended to the tile URL. If `tileSize: 512` is specified for
// a Mapbox raster source force the @2x suffix even if a non hidpi device.
const suffix = browser.devicePixelRatio >= 2 || tileSize === 512 ? '@2x' : '';
const extension = webpSupported.supported ? '.webp' : '$1';
urlObject.path = urlObject.path.replace(imageExtensionRe, `${suffix}${extension}`);
urlObject.path = urlObject.path.replace(tileURLAPIPrefixRe, '/');
urlObject.path = `/v4${urlObject.path}`;

if (config.REQUIRE_ACCESS_TOKEN && (config.ACCESS_TOKEN || this._customAccessToken) && this._skuToken) {
Expand Down Expand Up @@ -176,6 +178,7 @@ export class RequestManager {
if (accessToken[0] === 's')
throw new Error(`Use a public access token (pk.*) with Mapbox GL, not a secret access token (sk.*). ${help}`);

urlObject.params = urlObject.params.filter((d) => !d.includes('access_token'));
urlObject.params.push(`access_token=${accessToken}`);
return formatUrl(urlObject);
}
Expand Down
9 changes: 9 additions & 0 deletions test/unit/util/mapbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,15 @@ test("mapbox", (t) => {
t.end();
});

t.test('.normalizeTileURL ignores non-mapbox:// sources', (t) => {
// Add a path to the config:
config.API_URL = 'http://localhost:8080/mbx';
const input = `https://localhost:8080/mbx/v4/mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7/10/184/401.vector.pbf?access_token=${config.ACCESS_TOKEN}`;
const expected = `http://localhost:8080/mbx/v4/mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7/10/184/401.vector.pbf?sku=${manager._skuToken}&access_token=${config.ACCESS_TOKEN}`;
t.equal(manager.normalizeTileURL(input, 'mapbox://mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7'), expected);
t.end();
});

t.test('.normalizeTileURL ignores undefined sources', (t) => {
t.equal(manager.normalizeTileURL('http://path.png'), 'http://path.png');
t.end();
Expand Down

0 comments on commit 9f8fbc6

Please sign in to comment.