mirror of
https://github.com/protomaps/PMTiles.git
synced 2026-02-04 10:51:07 +00:00
cloudflare worker supports PMTILES_PATH
This commit is contained in:
@@ -1,5 +1,17 @@
|
||||
import { test } from "zora";
|
||||
import { pmtiles_path } from "./worker";
|
||||
|
||||
test("stub test", (assertion) => {
|
||||
assertion.ok(true);
|
||||
test("pmtiles path", (assertion) => {
|
||||
let result = pmtiles_path(undefined, "foo");
|
||||
assertion.equal(result, "foo.pmtiles");
|
||||
});
|
||||
|
||||
test("pmtiles path", (assertion) => {
|
||||
let result = pmtiles_path("folder/{name}/file.pmtiles", "foo");
|
||||
assertion.equal(result, "folder/foo/file.pmtiles");
|
||||
});
|
||||
|
||||
test("pmtiles path with slash", (assertion) => {
|
||||
let result = pmtiles_path("folder/{name}/file.pmtiles", "foo/bar");
|
||||
assertion.equal(result, "folder/foo/bar/file.pmtiles");
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@ import { PMTiles, Source } from "../../js";
|
||||
|
||||
interface Env {
|
||||
BUCKET: R2Bucket;
|
||||
CACHE_TTL: string;
|
||||
PMTILES_PATH: string | undefined;
|
||||
}
|
||||
|
||||
interface CacheEntry {
|
||||
@@ -60,6 +60,13 @@ export class LRUCache {
|
||||
|
||||
let worker_cache = new LRUCache();
|
||||
|
||||
export const pmtiles_path = (p: string | undefined, name: string): string => {
|
||||
if (p) {
|
||||
return p.replace("{name}", name);
|
||||
}
|
||||
return name + ".pmtiles";
|
||||
};
|
||||
|
||||
const TILE = new RegExp(
|
||||
/^\/([0-9a-zA-Z\/!\-_\.\*\'\(\)]+)\/(\d+)\/(\d+)\/(\d+).pbf$/
|
||||
);
|
||||
@@ -72,11 +79,15 @@ export default {
|
||||
): Promise<Response> {
|
||||
let url = new URL(request.url);
|
||||
|
||||
let match = url.pathname.match(TILE);
|
||||
let match = url.pathname.match(TILE)!;
|
||||
|
||||
let subrequests = 1;
|
||||
|
||||
if (match) {
|
||||
let name = match[1];
|
||||
let z = +match[2];
|
||||
let x = +match[3];
|
||||
let y = +match[4];
|
||||
class TempSource {
|
||||
getKey() {
|
||||
return "";
|
||||
@@ -85,7 +96,7 @@ export default {
|
||||
async getBytes(offset: number, length: number) {
|
||||
let result = await worker_cache.get(
|
||||
env.BUCKET,
|
||||
match![1] + ".pmtiles",
|
||||
pmtiles_path(env.PMTILES_PATH, name),
|
||||
offset,
|
||||
length
|
||||
);
|
||||
@@ -100,9 +111,9 @@ export default {
|
||||
|
||||
let p = new PMTiles(source);
|
||||
let metadata = await p.metadata();
|
||||
let entry = await p.getZxy(+match[2], +match[3], +match[4]);
|
||||
let entry = await p.getZxy(z, x, y);
|
||||
if (entry) {
|
||||
let tile = await env.BUCKET.get(match![1] + ".pmtiles", {
|
||||
let tile = await env.BUCKET.get(pmtiles_path(env.PMTILES_PATH, name), {
|
||||
range: { offset: entry.offset, length: entry.length },
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user