mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
From gekh's review of #768, each bug with a test that fails before: - P2: a failed sequence load clears the previous sequence's cones, resets the selection and can be retried; a second click while loading starts no second lookup. sequences.js had no tests: sequences.test.mjs now covers failure and retry, a superseded late answer, the cache and clearSelection. - P2: closing a photo stops MapillaryJS sequence playback, which kept the hidden viewer stepping and downloading images. - openNearest without the panel reports it instead of loading forever, and stops if the layer (or its provider) went off during the lookup; the mesh sampler's misses are bounded and forgotten with its samples; the sequence count is the union of ids across tiles; a provider withdraws only its own error, so a sequence error cannot mask KEY REJECTED; hover stays on (throttled) under an orbiting camera and re-picks when it stops; "since N days" follows the clock in a long-open tab. - A prewarm waits for one in flight and then builds if it gave up. - New coverage for the layer core (credits, KEY REJECTED vs REQUIRED, switching a provider off), the refresh wiring of the tilted-view fix and the draped-to-cast swap completion. - Cleanup: loading is derived from pending; freshStreet(); tile ranking computes distances once and shares one path for the date line; shared metresBetween / whenIdle / redrawTile; one NEAREST_RADIUS_M and one source method list; unused contract fields, exports, getImage and the decoder's image branch removed; PROVIDER_COLORS lists only Mapillary. Co-Authored-By: Claude Opus 5.5 <[email protected]>
155 lines
4.4 KiB
JavaScript
155 lines
4.4 KiB
JavaScript
import {
|
|
IMAGE_FIELDS,
|
|
MAPILLARY_GRAPH_HOST,
|
|
NEAREST_LIMIT,
|
|
NEAREST_RADIUS_M,
|
|
SEQUENCE_IMAGE_FIELDS,
|
|
SEQUENCE_IMAGES_LIMIT,
|
|
} from './policy.js';
|
|
|
|
/**
|
|
* Error carrying the HTTP status and any server-provided payload: no token
|
|
* (`keyRequired`), a token Mapillary rejected (`keyRejected`), or a rate
|
|
* limit with the seconds to wait (`retryAfterSec`).
|
|
*/
|
|
export class MapillarySourceError extends Error {
|
|
constructor(message, { status = 0, payload = null, retryAfterSec } = {}) {
|
|
super(message);
|
|
this.name = 'MapillarySourceError';
|
|
this.status = status;
|
|
this.payload = payload;
|
|
this.keyRequired = payload?.keyRequired ?? payload?.error === 'no_key';
|
|
this.keyRejected = payload?.keyRejected === true;
|
|
const wait = Number(retryAfterSec ?? payload?.retryAfter);
|
|
this.retryAfterSec =
|
|
status === 429 ? (Number.isFinite(wait) && wait > 0 ? wait : 60) : null;
|
|
}
|
|
}
|
|
|
|
async function readJsonOrThrow(response, label) {
|
|
let payload = null;
|
|
try {
|
|
payload = await response.json();
|
|
} catch {
|
|
/* status is authoritative */
|
|
}
|
|
if (!response.ok)
|
|
throw new MapillarySourceError(
|
|
payload?.error?.message ||
|
|
payload?.error ||
|
|
`${label} HTTP ${response.status}`,
|
|
{ status: response.status, payload },
|
|
);
|
|
return payload;
|
|
}
|
|
|
|
/**
|
|
* Every network path the Mapillary layer uses. Coverage tiles go through the
|
|
* local dev-server proxy (which adds the token and caches them); image and
|
|
* sequence lookups go straight to graph.mapillary.com with the client token,
|
|
* exactly as the embedded MapillaryJS viewer does.
|
|
* @param {{token?: string, fetchImpl?: Function, endpoints?: object}} options
|
|
*/
|
|
export function createMapillarySource({
|
|
token = '',
|
|
fetchImpl = (...args) => globalThis.fetch(...args),
|
|
endpoints = {},
|
|
} = {}) {
|
|
const urls = {
|
|
status: '/api/mapillary/status',
|
|
tiles: '/api/mapillary/tiles',
|
|
graph: MAPILLARY_GRAPH_HOST,
|
|
...endpoints,
|
|
};
|
|
|
|
async function graph(path, params = {}, { signal } = {}) {
|
|
if (!token)
|
|
throw new MapillarySourceError('Mapillary token not configured', {
|
|
status: 503,
|
|
payload: { keyRequired: true },
|
|
});
|
|
const search = new URLSearchParams({ access_token: token, ...params });
|
|
const response = await fetchImpl(`${urls.graph}/${path}?${search}`, {
|
|
signal,
|
|
});
|
|
return readJsonOrThrow(response, 'Mapillary graph');
|
|
}
|
|
|
|
return {
|
|
get token() {
|
|
return token;
|
|
},
|
|
hasToken: () => Boolean(token),
|
|
|
|
async getStatus({ signal } = {}) {
|
|
const response = await fetchImpl(urls.status, {
|
|
signal,
|
|
cache: 'no-store',
|
|
});
|
|
return readJsonOrThrow(response, 'Mapillary status');
|
|
},
|
|
|
|
/** Raw protobuf bytes for one tile; an empty array means no data. */
|
|
async getTile(layer, z, x, y, { signal } = {}) {
|
|
const response = await fetchImpl(
|
|
`${urls.tiles}/${layer}/${z}/${x}/${y}`,
|
|
{
|
|
signal,
|
|
},
|
|
);
|
|
if (response.status === 204) return new Uint8Array(0);
|
|
if (!response.ok) {
|
|
let payload = null;
|
|
try {
|
|
payload = await response.json();
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
throw new MapillarySourceError(
|
|
payload?.error || `Tile HTTP ${response.status}`,
|
|
{
|
|
status: response.status,
|
|
payload,
|
|
retryAfterSec: response.headers?.get?.('retry-after') ?? undefined,
|
|
},
|
|
);
|
|
}
|
|
return new Uint8Array(await response.arrayBuffer());
|
|
},
|
|
|
|
async getSequenceImages(
|
|
sequenceId,
|
|
{ signal, limit = SEQUENCE_IMAGES_LIMIT } = {},
|
|
) {
|
|
const payload = await graph(
|
|
'images',
|
|
{
|
|
sequence_ids: String(sequenceId),
|
|
fields: SEQUENCE_IMAGE_FIELDS,
|
|
limit: String(limit),
|
|
},
|
|
{ signal },
|
|
);
|
|
return Array.isArray(payload?.data) ? payload.data : [];
|
|
},
|
|
|
|
async nearestImages(
|
|
{ lat, lon, radius = NEAREST_RADIUS_M, limit = NEAREST_LIMIT },
|
|
{ signal } = {},
|
|
) {
|
|
const payload = await graph(
|
|
'images',
|
|
{
|
|
lat: String(lat),
|
|
lng: String(lon),
|
|
radius: String(Math.min(50, Math.max(1, radius))),
|
|
limit: String(Math.min(100, Math.max(1, limit))),
|
|
fields: IMAGE_FIELDS,
|
|
},
|
|
{ signal },
|
|
);
|
|
return Array.isArray(payload?.data) ? payload.data : [];
|
|
},
|
|
};
|
|
}
|