Files
PMTiles/js/adapters.ts
Brandon Liu 6638d040a5 linting and typing improvements [#287] (#337)
* Typing improvements [#287]

* rename FileApiSource to FileSource
* In a few cases we need to use any and biome-ignore. Deferring any restructuring here to js v4.
* replace prettier with biome
2024-01-31 12:22:25 +08:00

208 lines
5.4 KiB
TypeScript

// biome-ignore lint: needed for Leaflet + IIFE to work
declare const L: any;
// biome-ignore lint: needed for window.URL to disambiguate from cloudflare workers
declare const window: any;
declare const document: DocumentLike;
import type { Coords } from "leaflet";
import { PMTiles, TileType } from "./index";
interface DocumentLike {
// biome-ignore lint: we don't want to bring in the entire document type
createElement: (s: string) => any;
}
// biome-ignore lint: we don't want to bring in the entire document type
type DoneCallback = (error?: Error, tile?: any) => void;
export const leafletRasterLayer = (source: PMTiles, options: unknown) => {
let loaded = false;
let mimeType = "";
const cls = L.GridLayer.extend({
createTile: (coord: Coords, done: DoneCallback) => {
const el = document.createElement("img");
const controller = new AbortController();
const signal = controller.signal;
el.cancel = () => {
controller.abort();
};
if (!loaded) {
source.getHeader().then((header) => {
if (header.tileType === TileType.Mvt) {
console.error(
"Error: archive contains MVT vector tiles, but leafletRasterLayer is for displaying raster tiles. See https://github.com/protomaps/PMTiles/tree/main/js for details."
);
} else if (header.tileType === 2) {
mimeType = "image/png";
} else if (header.tileType === 3) {
mimeType = "image/jpeg";
} else if (header.tileType === 4) {
mimeType = "image/webp";
} else if (header.tileType === 5) {
mimeType = "image/avif";
}
});
loaded = true;
}
source
.getZxy(coord.z, coord.x, coord.y, signal)
.then((arr) => {
if (arr) {
const blob = new Blob([arr.data], { type: mimeType });
const imageUrl = window.URL.createObjectURL(blob);
el.src = imageUrl;
el.cancel = undefined;
done(undefined, el);
}
})
.catch((e) => {
if (e.name !== "AbortError") {
throw e;
}
});
return el;
},
_removeTile: function (key: string) {
const tile = this._tiles[key];
if (!tile) {
return;
}
if (tile.el.cancel) tile.el.cancel();
tile.el.width = 0;
tile.el.height = 0;
tile.el.deleted = true;
L.DomUtil.remove(tile.el);
delete this._tiles[key];
this.fire("tileunload", {
tile: tile.el,
coords: this._keyToTileCoords(key),
});
},
});
return new cls(options);
};
// copied from MapLibre /util/ajax.ts
type RequestParameters = {
url: string;
headers?: unknown;
method?: "GET" | "POST" | "PUT";
body?: string;
type?: "string" | "json" | "arrayBuffer" | "image";
credentials?: "same-origin" | "include";
collectResourceTiming?: boolean;
};
type ResponseCallback = (
error?: Error | null,
data?: unknown | null,
cacheControl?: string | null,
expires?: string | null
) => void;
type Cancelable = {
cancel: () => void;
};
export class Protocol {
tiles: Map<string, PMTiles>;
constructor() {
this.tiles = new Map<string, PMTiles>();
}
add(p: PMTiles) {
this.tiles.set(p.source.getKey(), p);
}
get(url: string) {
return this.tiles.get(url);
}
tile = (
params: RequestParameters,
callback: ResponseCallback
): Cancelable => {
if (params.type === "json") {
const pmtilesUrl = params.url.substr(10);
let instance = this.tiles.get(pmtilesUrl);
if (!instance) {
instance = new PMTiles(pmtilesUrl);
this.tiles.set(pmtilesUrl, instance);
}
instance
.getHeader()
.then((h) => {
const tilejson = {
tiles: [`${params.url}/{z}/{x}/{y}`],
minzoom: h.minZoom,
maxzoom: h.maxZoom,
bounds: [h.minLon, h.minLat, h.maxLon, h.maxLat],
};
callback(null, tilejson, null, null);
})
.catch((e) => {
callback(e, null, null, null);
});
return {
cancel: () => {},
};
}
const re = new RegExp(/pmtiles:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)/);
const result = params.url.match(re);
if (!result) {
throw new Error("Invalid PMTiles protocol URL");
}
const pmtilesUrl = result[1];
let instance = this.tiles.get(pmtilesUrl);
if (!instance) {
instance = new PMTiles(pmtilesUrl);
this.tiles.set(pmtilesUrl, instance);
}
const z = result[2];
const x = result[3];
const y = result[4];
const controller = new AbortController();
const signal = controller.signal;
const cancel = () => {
controller.abort();
};
instance.getHeader().then((header) => {
instance
?.getZxy(+z, +x, +y, signal)
.then((resp) => {
if (resp) {
callback(
null,
new Uint8Array(resp.data),
resp.cacheControl,
resp.expires
);
} else {
if (header.tileType === TileType.Mvt) {
callback(null, new Uint8Array(), null, null);
} else {
callback(null, null, null, null);
}
}
})
.catch((e) => {
if ((e as Error).name !== "AbortError") {
callback(e, null, null, null);
}
});
});
return {
cancel: cancel,
};
};
}