Skip to content

Commit

Permalink
Get rid of expandUrl dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ahocevar committed Dec 12, 2024
1 parent d26e41d commit e9608f2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ function setupGeoJSONSource(glSource, styleUrl, options) {
const data = glSource.data;
const sourceOptions = {};
if (typeof data == 'string') {
const geoJsonUrl = normalizeSourceUrl(
const [geoJsonUrl] = normalizeSourceUrl(
data,
options.accessToken,
options.accessTokenParam || 'access_token',
Expand Down
17 changes: 12 additions & 5 deletions src/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,38 @@ export function normalizeStyleUrl(url, token) {
return `${mapboxBaseUrl}/styles/v1/${style}?&access_token=${token}`;
}

const mapboxSubdomains = ['a', 'b', 'c', 'd'];

/**
* Turns mapbox:// source URLs into vector tile URL templates.
* @param {string} url The source URL.
* @param {string} token The access token.
* @param {string} tokenParam The access token key.
* @param {string} styleUrl The style URL.
* @return {string} A vector tile template.
* @return {Array<string>} A vector tile template.
* @private
*/
export function normalizeSourceUrl(url, token, tokenParam, styleUrl) {
const urlObject = new URL(url, styleUrl);
const mapboxPath = getMapboxPath(url);
if (!mapboxPath) {
if (!token) {
return decodeURI(urlObject.href);
return [decodeURI(urlObject.href)];
}
if (!urlObject.searchParams.has(tokenParam)) {
urlObject.searchParams.set(tokenParam, token);
}
return decodeURI(urlObject.href);
return [decodeURI(urlObject.href)];
}

if (mapboxPath === 'mapbox.satellite') {
const sizeFactor = window.devicePixelRatio >= 1.5 ? '@2x' : '';
return `https://api.mapbox.com/v4/${mapboxPath}/{z}/{x}/{y}${sizeFactor}.webp?access_token=${token}`;
return [
`https://api.mapbox.com/v4/${mapboxPath}/{z}/{x}/{y}${sizeFactor}.webp?access_token=${token}`,
];
}
return `https://{a-d}.tiles.mapbox.com/v4/${mapboxPath}/{z}/{x}/{y}.vector.pbf?access_token=${token}`;
return mapboxSubdomains.map(
(sub) =>
`https://${sub}.tiles.mapbox.com/v4/${mapboxPath}/{z}/{x}/{y}.vector.pbf?access_token=${token}`,
);
}
9 changes: 4 additions & 5 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import TileState from 'ol/TileState.js';
import {VectorTile} from 'ol';
import {expandUrl} from 'ol/tileurlfunction.js';
import {getUid} from 'ol/util.js';
import {normalizeSourceUrl, normalizeStyleUrl} from './mapbox.js';
import {toPromise} from 'ol/functions.js';
Expand Down Expand Up @@ -239,15 +238,15 @@ export function getTileJson(glSource, styleUrl, options = {}) {
promise = Promise.resolve({
tileJson: Object.assign({}, glSource, {
url: undefined,
tiles: expandUrl(normalizedSourceUrl),
tiles: normalizedSourceUrl,
}),
tileLoadFunction,
});
} else {
const metadata = {};
promise = fetchResource(
'Source',
normalizedSourceUrl,
normalizedSourceUrl[0],
options,
metadata,
).then(function (tileJson) {
Expand All @@ -260,7 +259,7 @@ export function getTileJson(glSource, styleUrl, options = {}) {
options.accessToken,
options.accessTokenParam || 'access_token',
metadata.request.url,
);
)[0];
});
return Promise.resolve({tileJson, tileLoadFunction});
});
Expand All @@ -276,7 +275,7 @@ export function getTileJson(glSource, styleUrl, options = {}) {
options.accessToken,
options.accessTokenParam || 'access_token',
styleUrl || location.href,
);
)[0];
}),
});
promise = Promise.resolve({
Expand Down
24 changes: 17 additions & 7 deletions test/mapbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,32 @@ describe('Mapbox utilities', function () {
const cases = [
{
url: 'mapbox://mapbox.mapbox-streets-v7',
expected:
'https://{a-d}.tiles.mapbox.com/v4/mapbox.mapbox-streets-v7/{z}/{x}/{y}.vector.pbf?access_token=test-token',
expected: [
'https://a.tiles.mapbox.com/v4/mapbox.mapbox-streets-v7/{z}/{x}/{y}.vector.pbf?access_token=test-token',
'https://b.tiles.mapbox.com/v4/mapbox.mapbox-streets-v7/{z}/{x}/{y}.vector.pbf?access_token=test-token',
'https://c.tiles.mapbox.com/v4/mapbox.mapbox-streets-v7/{z}/{x}/{y}.vector.pbf?access_token=test-token',
'https://d.tiles.mapbox.com/v4/mapbox.mapbox-streets-v7/{z}/{x}/{y}.vector.pbf?access_token=test-token',
],
},
{
url: 'https://example.com/source/{z}/{x}/{y}.pbf',
expected: 'https://example.com/source/{z}/{x}/{y}.pbf?token=test-token',
expected: [
'https://example.com/source/{z}/{x}/{y}.pbf?token=test-token',
],
},
{
url: 'https://example.com/source/{z}/{x}/{y}.pbf?foo=bar',
expected:
expected: [
'https://example.com/source/{z}/{x}/{y}.pbf?foo=bar&token=test-token',
],
},
{
url: 'https://example.com/source/{z}/{x}/{y}.pbf?token=override-token',
expected:
url: [
'https://example.com/source/{z}/{x}/{y}.pbf?token=override-token',
],
expected: [
'https://example.com/source/{z}/{x}/{y}.pbf?token=override-token',
],
},
];

Expand All @@ -119,7 +129,7 @@ describe('Mapbox utilities', function () {
it(`works for ${c.url}`, () => {
should(
normalizeSourceUrl(c.url, token, tokenParam, location.href),
).equal(c.expected);
).deepEqual(c.expected);
});
}
});
Expand Down

0 comments on commit e9608f2

Please sign in to comment.