vendor: 6,892 bytes, lines 1-301
1"use strict"; 2var pmtiles = (() => { 3 var __defProp = Object.defineProperty; 4 var __getOwnPropDesc = Object.getOwnPropertyDescriptor; 5 var __getOwnPropNames = Object.getOwnPropertyNames; 6 var __hasOwnProp = Object.prototype.hasOwnProperty; 7 var __pow = Math.pow; 8 var __export = (target, all) => { 9 for (var name in all) 10 __defProp(target, name, { get: all[name], enumerable: true }); 11 }; 12 var __copyProps = (to, from, except, desc) => { 13 if (from && typeof from === "object" || typeof from === "function") { 14 for (let key of __getOwnPropNames(from)) 15 if (!__hasOwnProp.call(to, key) && key !== except) 16 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); 17 } 18 return to; 19 }; 20 var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); 21 var __async = (__this, __arguments, generator) => { 22 return new Promise((resolve, reject) => { 23 var fulfilled = (value) => { 24 try { 25 step(generator.next(value)); 26 } catch (e) { 27 reject(e); 28 } 29 }; 30 var rejected = (value) => { 31 try { 32 step(generator.throw(value)); 33 } catch (e) { 34 reject(e); 35 } 36 }; 37 var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); 38 step((generator = generator.apply(__this, __arguments)).next()); 39 }); 40 }; 41 42 // index.ts 43 var js_exports = {}; 44 __export(js_exports, { 45 Compression: () => Compression, 46 EtagMismatch: () => EtagMismatch, 47 FetchSource: () => FetchSource, 48 FileSource: () => FileSource, 49 PMTiles: () => PMTiles, 50 Protocol: () => Protocol, 51 ResolvedValueCache: () => ResolvedValueCache, 52 SharedPromiseCache: () => SharedPromiseCache, 53 TileType: () => TileType, 54 bytesToHeader: () => bytesToHeader, 55 findTile: () => findTile, 56 getUint64: () => getUint64, 57 leafletRasterLayer: () => leafletRasterLayer, 58 readVarint: () => readVarint, 59 tileIdToZxy: () => tileIdToZxy, 60 tileTypeExt: () => tileTypeExt, 61 zxyToTileId: () => zxyToTileId 62 }); 63 64 // node_modules/fflate/esm/browser.js 65 var u8 = Uint8Array; 66 var u16 = Uint16Array; 67 var i32 = Int32Array; 68 var fleb = new u8([ 69 0, 70 0, 71 0, 72 0, 73 0, 74 0, 75 0, 76 0, 77 1, 78 1, 79 1, 80 1, 81 2, 82 2, 83 2, 84 2, 85 3, 86 3, 87 3, 88 3, 89 4, 90 4, 91 4, 92 4, 93 5, 94 5, 95 5, 96 5, 97 0, 98 /* unused */ 99 0, 100 0, 101 /* impossible */ 102 0 103 ]); 104 var fdeb = new u8([ 105 0, 106 0, 107 0, 108 0, 109 1, 110 1, 111 2, 112 2, 113 3, 114 3, 115 4, 116 4, 117 5, 118 5, 119 6, 120 6, 121 7, 122 7, 123 8, 124 8, 125 9, 126 9, 127 10, 128 10, 129 11, 130 11, 131 12, 132 12, 133 13, 134 13, 135 /* unused */ 136 0, 137 0 138 ]); 139 var clim = new u8([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]); 140 var freb = function(eb, start) { 141 var b = new u16(31); 142 for (var i = 0; i < 31; ++i) { 143 b[i] = start += 1 << eb[i - 1]; 144 } 145 var r = new i32(b[30]); 146 for (var i = 1; i < 30; ++i) { 147 for (var j = b[i]; j < b[i + 1]; ++j) { 148 r[j] = j - b[i] << 5 | i; 149 } 150 } 151 return { b, r }; 152 }; 153 var _a = freb(fleb, 2); 154 var fl = _a.b; 155 var revfl = _a.r; 156 fl[28] = 258, revfl[258] = 28; 157 var _b = freb(fdeb, 0); 158 var fd = _b.b; 159 var revfd = _b.r; 160 var rev = new u16(32768); 161 for (i = 0; i < 32768; ++i) { 162 x = (i & 43690) >> 1 | (i & 21845) << 1; 163 x = (x & 52428) >> 2 | (x & 13107) << 2; 164 x = (x & 61680) >> 4 | (x & 3855) << 4; 165 rev[i] = ((x & 65280) >> 8 | (x & 255) << 8) >> 1; 166 } 167 var x; 168 var i; 169 var hMap = function(cd, mb, r) { 170 var s = cd.length; 171 var i = 0; 172 var l = new u16(mb); 173 for (; i < s; ++i) { 174 if (cd[i]) 175 ++l[cd[i] - 1]; 176 } 177 var le = new u16(mb); 178 for (i = 1; i < mb; ++i) { 179 le[i] = le[i - 1] + l[i - 1] << 1; 180 } 181 var co; 182 if (r) { 183 co = new u16(1 << mb); 184 var rvb = 15 - mb; 185 for (i = 0; i < s; ++i) { 186 if (cd[i]) { 187 var sv = i << 4 | cd[i]; 188 var r_1 = mb - cd[i]; 189 var v = le[cd[i] - 1]++ << r_1; 190 for (var m = v | (1 << r_1) - 1; v <= m; ++v) { 191 co[rev[v] >> rvb] = sv; 192 } 193 } 194 } 195 } else { 196 co = new u16(s); 197 for (i = 0; i < s; ++i) { 198 if (cd[i]) { 199 co[i] = rev[le[cd[i] - 1]++] >> 15 - cd[i]; 200 } 201 } 202 } 203 return co; 204 }; 205 var flt = new u8(288); 206 for (i = 0; i < 144; ++i) 207 flt[i] = 8; 208 var i; 209 for (i = 144; i < 256; ++i) 210 flt[i] = 9; 211 var i; 212 for (i = 256; i < 280; ++i) 213 flt[i] = 7; 214 var i; 215 for (i = 280; i < 288; ++i) 216 flt[i] = 8; 217 var i; 218 var fdt = new u8(32); 219 for (i = 0; i < 32; ++i) 220 fdt[i] = 5; 221 var i; 222 var flrm = /* @__PURE__ */ hMap(flt, 9, 1); 223 var fdrm = /* @__PURE__ */ hMap(fdt, 5, 1); 224 var max = function(a) { 225 var m = a[0]; 226 for (var i = 1; i < a.length; ++i) { 227 if (a[i] > m) 228 m = a[i]; 229 } 230 return m; 231 }; 232 var bits = function(d, p, m) { 233 var o = p / 8 | 0; 234 return (d[o] | d[o + 1] << 8) >> (p & 7) & m; 235 }; 236 var bits16 = function(d, p) { 237 var o = p / 8 | 0; 238 return (d[o] | d[o + 1] << 8 | d[o + 2] << 16) >> (p & 7); 239 }; 240 var shft = function(p) { 241 return (p + 7) / 8 | 0; 242 }; 243 var slc = function(v, s, e) { 244 if (s == null || s < 0) 245 s = 0; 246 if (e == null || e > v.length) 247 e = v.length; 248 var n = new u8(e - s); 249 n.set(v.subarray(s, e)); 250 return n; 251 }; 252 var ec = [ 253 "unexpected EOF", 254 "invalid block type", 255 "invalid length/literal", 256 "invalid distance", 257 "stream finished", 258 "no stream handler", 259 , 260 "no callback", 261 "invalid UTF-8 data", 262 "extra field too long", 263 "date not in range 1980-2099", 264 "filename too long", 265 "stream finishing", 266 "invalid zip data" 267 // determined by unknown compression method 268 ]; 269 var err = function(ind, msg, nt) { 270 var e = new Error(msg || ec[ind]); 271 e.code = ind; 272 if (Error.captureStackTrace) 273 Error.captureStackTrace(e, err); 274 if (!nt) 275 throw e; 276 return e; 277 }; 278 var inflt = function(dat, st, buf, dict) { 279 var sl = dat.length, dl = dict ? dict.length : 0; 280 if (!sl || st.f && !st.l) 281 return buf || new u8(0); 282 var noBuf = !buf || st.i != 2; 283 var noSt = st.i; 284 if (!buf) 285 buf = new u8(sl * 3); 286 var cbuf = function(l2) { 287 var bl = buf.length; 288 if (l2 > bl) { 289 var nbuf = new u8(Math.max(bl * 2, l2)); 290 nbuf.set(buf); 291 buf = nbuf; 292 } 293 }; 294 var final = st.f || 0, pos = st.p || 0, bt = st.b || 0, lm = st.l, dm = st.d, lbt = st.m, dbt = st.n; 295 var tbts = sl * 8; 296 do { 297 if (!lm) { 298 final = bits(dat, pos, 1); 299 var type = bits(dat, pos + 1, 3); 300 pos += 3; 301 if (!type) {
vendor: 4,762 bytes, lines 302-453
302 var s = shft(pos) + 4, l = dat[s - 4] | dat[s - 3] << 8, t = s + l; 303 if (t > sl) { 304 if (noSt) 305 err(0); 306 break; 307 } 308 if (noBuf) 309 cbuf(bt + l); 310 buf.set(dat.subarray(s, t), bt); 311 st.b = bt += l, st.p = pos = t * 8, st.f = final; 312 continue; 313 } else if (type == 1) 314 lm = flrm, dm = fdrm, lbt = 9, dbt = 5; 315 else if (type == 2) { 316 var hLit = bits(dat, pos, 31) + 257, hcLen = bits(dat, pos + 10, 15) + 4; 317 var tl = hLit + bits(dat, pos + 5, 31) + 1; 318 pos += 14; 319 var ldt = new u8(tl); 320 var clt = new u8(19); 321 for (var i = 0; i < hcLen; ++i) { 322 clt[clim[i]] = bits(dat, pos + i * 3, 7); 323 } 324 pos += hcLen * 3; 325 var clb = max(clt), clbmsk = (1 << clb) - 1; 326 var clm = hMap(clt, clb, 1); 327 for (var i = 0; i < tl; ) { 328 var r = clm[bits(dat, pos, clbmsk)]; 329 pos += r & 15; 330 var s = r >> 4; 331 if (s < 16) { 332 ldt[i++] = s; 333 } else { 334 var c = 0, n = 0; 335 if (s == 16) 336 n = 3 + bits(dat, pos, 3), pos += 2, c = ldt[i - 1]; 337 else if (s == 17) 338 n = 3 + bits(dat, pos, 7), pos += 3; 339 else if (s == 18) 340 n = 11 + bits(dat, pos, 127), pos += 7; 341 while (n--) 342 ldt[i++] = c; 343 } 344 } 345 var lt = ldt.subarray(0, hLit), dt = ldt.subarray(hLit); 346 lbt = max(lt); 347 dbt = max(dt); 348 lm = hMap(lt, lbt, 1); 349 dm = hMap(dt, dbt, 1); 350 } else 351 err(1); 352 if (pos > tbts) { 353 if (noSt) 354 err(0); 355 break; 356 } 357 } 358 if (noBuf) 359 cbuf(bt + 131072); 360 var lms = (1 << lbt) - 1, dms = (1 << dbt) - 1; 361 var lpos = pos; 362 for (; ; lpos = pos) { 363 var c = lm[bits16(dat, pos) & lms], sym = c >> 4; 364 pos += c & 15; 365 if (pos > tbts) { 366 if (noSt) 367 err(0); 368 break; 369 } 370 if (!c) 371 err(2); 372 if (sym < 256) 373 buf[bt++] = sym; 374 else if (sym == 256) { 375 lpos = pos, lm = null; 376 break; 377 } else { 378 var add = sym - 254; 379 if (sym > 264) { 380 var i = sym - 257, b = fleb[i]; 381 add = bits(dat, pos, (1 << b) - 1) + fl[i]; 382 pos += b; 383 } 384 var d = dm[bits16(dat, pos) & dms], dsym = d >> 4; 385 if (!d) 386 err(3); 387 pos += d & 15; 388 var dt = fd[dsym]; 389 if (dsym > 3) { 390 var b = fdeb[dsym]; 391 dt += bits16(dat, pos) & (1 << b) - 1, pos += b; 392 } 393 if (pos > tbts) { 394 if (noSt) 395 err(0); 396 break; 397 } 398 if (noBuf) 399 cbuf(bt + 131072); 400 var end = bt + add; 401 if (bt < dt) { 402 var shift2 = dl - dt, dend = Math.min(dt, end); 403 if (shift2 + bt < 0) 404 err(3); 405 for (; bt < dend; ++bt) 406 buf[bt] = dict[shift2 + bt]; 407 } 408 for (; bt < end; bt += 4) { 409 buf[bt] = buf[bt - dt]; 410 buf[bt + 1] = buf[bt + 1 - dt]; 411 buf[bt + 2] = buf[bt + 2 - dt]; 412 buf[bt + 3] = buf[bt + 3 - dt]; 413 } 414 bt = end; 415 } 416 } 417 st.l = lm, st.p = lpos, st.b = bt, st.f = final; 418 if (lm) 419 final = 1, st.m = lbt, st.d = dm, st.n = dbt; 420 } while (!final); 421 return bt == buf.length ? buf : slc(buf, 0, bt); 422 }; 423 var et = /* @__PURE__ */ new u8(0); 424 var gzs = function(d) { 425 if (d[0] != 31 || d[1] != 139 || d[2] != 8) 426 err(6, "invalid gzip data"); 427 var flg = d[3]; 428 var st = 10; 429 if (flg & 4) 430 st += (d[10] | d[11] << 8) + 2; 431 for (var zs = (flg >> 3 & 1) + (flg >> 4 & 1); zs > 0; zs -= !d[st++]) 432 ; 433 return st + (flg & 2); 434 }; 435 var gzl = function(d) { 436 var l = d.length; 437 return (d[l - 4] | d[l - 3] << 8 | d[l - 2] << 16 | d[l - 1] << 24) >>> 0; 438 }; 439 var zls = function(d, dict) { 440 if ((d[0] & 15) != 8 || d[0] >> 4 > 7 || (d[0] << 8 | d[1]) % 31) 441 err(6, "invalid zlib data"); 442 if ((d[1] >> 5 & 1) == +!dict) 443 err(6, "invalid zlib data: " + (d[1] & 32 ? "need" : "unexpected") + " dictionary"); 444 return (d[1] >> 3 & 4) + 2; 445 }; 446 function inflateSync(data, opts) { 447 return inflt(data, { i: 2 }, opts && opts.out, opts && opts.dictionary); 448 } 449 function gunzipSync(data, opts) { 450 var st = gzs(data); 451 if (st + 8 > data.length) 452 err(6, "invalid gzip data"); 453 return inflt(data.subarray(st, -8), { i: 2 }
453, opts && opts.out || new u8(gzl(data)), opts && opts.dictionary); 454 } 455 function unzlibSync(data, opts) { 456 return inflt(data.subarray(zls(data, opts && opts.dictionary), -4), { i: 2 }, opts && opts.out, opts && opts.dictionary); 457 } 458 function decompressSync(data, opts) { 459 return data[0] == 31 && data[1] == 139 && data[2] == 8 ? gunzipSync(data, opts) : (data[0] & 15) != 8 || data[0] >> 4 > 7 || (data[0] << 8 | data[1]) % 31 ? inflateSync(data, opts) : unzlibSync(data, opts); 460 } 461 var td = typeof TextDecoder != "undefined" && /* @__PURE__ */ new TextDecoder(); 462 var tds = 0; 463 try { 464 td.decode(et, { stream: true }); 465 tds = 1; 466 } catch (e) { 467 } 468 469 // v2.ts 470 var shift = (n, shift2) => { 471 return n * __pow(2, shift2); 472 }; 473 var unshift = (n, shift2) => { 474 return Math.floor(n / __pow(2, shift2)); 475 }; 476 var getUint24 = (view, pos) => { 477 return shift(view.getUint16(pos + 1, true), 8) + view.getUint8(pos); 478 }; 479 var getUint48 = (view, pos) => { 480 return shift(view.getUint32(pos + 2, true), 16) + view.getUint16(pos, true); 481 }; 482 var compare = (tz, tx, ty, view, i) => { 483 if (tz !== view.getUint8(i)) 484 return tz - view.getUint8(i); 485 const x = getUint24(view, i + 1); 486 if (tx !== x) 487 return tx - x; 488 const y = getUint24(view, i + 4); 489 if (ty !== y) 490 return ty - y; 491 return 0; 492 }; 493 var queryLeafdir = (view, z, x, y) => { 494 const offsetLen = queryView(view, z | 128, x, y); 495 if (offsetLen) { 496 return { 497 z, 498 x, 499 y, 500 offset: offsetLen[0], 501 length: offsetLen[1], 502 isDir: true 503 }; 504 } 505 return null; 506 }; 507 var queryTile = (view, z, x, y) => { 508 const offsetLen = queryView(view, z, x, y); 509 if (offsetLen) { 510 return { 511 z, 512 x, 513 y, 514 offset: offsetLen[0], 515 length: offsetLen[1], 516 isDir: false 517 }; 518 } 519 return null; 520 }; 521 var queryView = (view, z, x, y) => { 522 let m = 0; 523 let n = view.byteLength / 17 - 1; 524 while (m <= n) { 525 const k = n + m >> 1; 526 const cmp = compare(z, x, y, view, k * 17); 527 if (cmp > 0) { 528 m = k + 1; 529 } else if (cmp < 0) { 530 n = k - 1; 531 } else { 532 return [getUint48(view, k * 17 + 7), view.getUint32(k * 17 + 13, true)]; 533 } 534 } 535 return null; 536 }; 537 var entrySort = (a, b) => { 538 if (a.isDir && !b.isDir) { 539 return 1; 540 } 541 if (!a.isDir && b.isDir) { 542 return -1; 543 } 544 if (a.z !== b.z) { 545 return a.z - b.z; 546 } 547 if (a.x !== b.x) { 548 return a.x - b.x; 549 } 550 return a.y - b.y; 551 }; 552 var parseEntry = (dataview, i) => { 553 const zRaw = dataview.getUint8(i * 17); 554 const z = zRaw & 127; 555 return { 556 z,
557 x: getUint24(dataview, i * 17 + 1), 558 y: getUint24(dataview, i * 17 + 4), 559 offset: getUint48(dataview, i * 17 + 7), 560 length: dataview.getUint32(i * 17 + 13, true), 561 isDir: zRaw >> 7 === 1 562 }; 563 }; 564 var sortDir = (a) => { 565 const entries = []; 566 const view = new DataView(a); 567 for (let i = 0; i < view.byteLength / 17; i++) { 568 entries.push(parseEntry(view, i)); 569 } 570 return createDirectory(entries); 571 }; 572 var createDirectory = (entries) => { 573 entries.sort(entrySort); 574 const buffer = new ArrayBuffer(17 * entries.length); 575 const arr = new Uint8Array(buffer); 576 for (let i = 0; i < entries.length; i++) { 577 const entry = entries[i]; 578 let z = entry.z; 579 if (entry.isDir) 580 z = z | 128; 581 arr[i * 17] = z; 582 arr[i * 17 + 1] = entry.x & 255; 583 arr[i * 17 + 2] = entry.x >> 8 & 255; 584 arr[i * 17 + 3] = entry.x >> 16 & 255; 585 arr[i * 17 + 4] = entry.y & 255; 586 arr[i * 17 + 5] = entry.y >> 8 & 255; 587 arr[i * 17 + 6] = entry.y >> 16 & 255; 588 arr[i * 17 + 7] = entry.offset & 255; 589 arr[i * 17 + 8] = unshift(entry.offset, 8) & 255; 590 arr[i * 17 + 9] = unshift(entry.offset, 16) & 255; 591 arr[i * 17 + 10] = unshift(entry.offset, 24) & 255; 592 arr[i * 17 + 11] = unshift(entry.offset, 32) & 255; 593 arr[i * 17 + 12] = unshift(entry.offset, 48) & 255; 594 arr[i * 17 + 13] = entry.length & 255; 595 arr[i * 17 + 14] = entry.length >> 8 & 255; 596 arr[i * 17 + 15] = entry.length >> 16 & 255; 597 arr[i * 17 + 16] = entry.length >> 24 & 255; 598 } 599 return buffer; 600 }; 601 var deriveLeaf = (view, tile) => { 602 if (view.byteLength < 17) 603 return null; 604 const numEntries = view.byteLength / 17; 605 const entry = parseEntry(view, numEntries - 1); 606 if (entry.isDir) { 607 const leafLevel = entry.z; 608 const levelDiff = tile.z - leafLevel; 609 const leafX = Math.trunc(tile.x / (1 << levelDiff)); 610 const leafY = Math.trunc(tile.y / (1 << levelDiff)); 611 return { z: leafLevel, x: leafX, y: leafY }; 612 } 613 return null; 614 }; 615 function getHeader(source) { 616 return __async(this, null, function* () { 617 const resp = yield source.getBytes(0, 512e3); 618 const dataview = new DataView(resp.data); 619 const jsonSize = dataview.getUint32(4, true); 620 const rootEntries = dataview.getUint16(8, true); 621 const dec = new TextDecoder("utf-8"); 622 const jsonMetadata = JSON.parse( 623 dec.decode(new DataView(resp.data, 10, jsonSize)) 624 ); 625 let tileCompression = 0 /* Unknown */; 626 if (jsonMetadata.compression === "gzip") { 627 tileCompression = 2 /* Gzip */; 628 } 629 let minzoom = 0; 630 if ("minzoom" in jsonMetadata) { 631 minzoom = +jsonMetadata.minzoom; 632 } 633 let maxzoom = 0; 634 if ("maxzoom" in jsonMetadata) { 635 maxzoom = +jsonMetadata.maxzoom; 636 } 637 let centerLon = 0; 638 let centerLat = 0; 639 let centerZoom = 0; 640 let minLon = -180; 641 let minLat = -85; 642 let maxLon = 180; 643 let maxLat = 85; 644 if (jsonMetadata.bounds) { 645 const split = jsonMetadata.bounds.split(","); 646 minLon = +split[0]; 647 minLat = +split[1]; 648 maxLon = +split[2]; 649 maxLat = +split[3]; 650 } 651 if (jsonMetadata.center) { 652 const split = jsonMetadata.center.split(","); 653 centerLon = +split[0]; 654 centerLat = +split[1]; 655 centerZoom = +split[2]; 656 } 657 const header = { 658 specVersion: dataview.getUint16(2, true), 659 rootDirectoryOffset: 10 + jsonSize, 660 rootDirectoryLength: rootEntries * 17, 661 jsonMetadataOffset: 10, 662 jsonMetadataLength: jsonSize, 663 leafDirectoryOffset: 0, 664 leafDirectoryLength: void 0, 665 tileDataOffset: 0, 666 tileDataLength: void 0, 667 numAddressedTiles: 0, 668 numTileEntries: 0, 669 numTileContents: 0, 670 clustered: false, 671 internalCompression: 1 /* None */, 672 tileCompression, 673 tileType: 1 /* Mvt */, 674 minZoom: minzoom, 675 maxZoom: maxzoom, 676 minLon, 677 minLat, 678 maxLon, 679 maxLat, 680 centerZoom, 681 centerLon, 682 centerLat, 683 etag: resp.etag 684 }; 685 return header; 686 }); 687 } 688 function getZxy(header, source, cache, z, x, y, signal) { 689 return __async(this, null, function* () {
690 let rootDir = yield cache.getArrayBuffer( 691 source, 692 header.rootDirectoryOffset, 693 header.rootDirectoryLength, 694 header 695 ); 696 if (header.specVersion === 1) { 697 rootDir = sortDir(rootDir); 698 } 699 const entry = queryTile(new DataView(rootDir), z, x, y); 700 if (entry) { 701 const resp = yield source.getBytes(entry.offset, entry.length, signal); 702 let tileData = resp.data; 703 const view = new DataView(tileData); 704 if (view.getUint8(0) === 31 && view.getUint8(1) === 139) { 705 tileData = decompressSync(new Uint8Array(tileData)); 706 } 707 return { 708 data: tileData 709 }; 710 } 711 const leafcoords = deriveLeaf(new DataView(rootDir), { z, x, y }); 712 if (leafcoords) { 713 const leafdirEntry = queryLeafdir( 714 new DataView(rootDir), 715 leafcoords.z, 716 leafcoords.x, 717 leafcoords.y 718 ); 719 if (leafdirEntry) { 720 let leafDir = yield cache.getArrayBuffer( 721 source, 722 leafdirEntry.offset, 723 leafdirEntry.length, 724 header 725 ); 726 if (header.specVersion === 1) { 727 leafDir = sortDir(leafDir); 728 } 729 const tileEntry = queryTile(new DataView(leafDir), z, x, y); 730 if (tileEntry) { 731 const resp = yield source.getBytes( 732 tileEntry.offset, 733 tileEntry.length, 734 signal 735 ); 736 let tileData = resp.data; 737 const view = new DataView(tileData); 738 if (view.getUint8(0) === 31 && view.getUint8(1) === 139) { 739 tileData = decompressSync(new Uint8Array(tileData)); 740 } 741 return { 742 data: tileData 743 }; 744 } 745 } 746 } 747 return void 0; 748 }); 749 } 750 var v2_default = { 751 getHeader, 752 getZxy 753 }; 754 755 // adapters.ts 756 var leafletRasterLayer = (source, options) => { 757 let loaded = false; 758 let mimeType = ""; 759 const cls = L.GridLayer.extend({ 760 createTile: (coord, done) => { 761 const el = document.createElement("img"); 762 const controller = new AbortController(); 763 const signal = controller.signal; 764 el.cancel = () => { 765 controller.abort(); 766 }; 767 if (!loaded) { 768 source.getHeader().then((header) => { 769 if (header.tileType === 1 /* Mvt */) { 770 console.error( 771 "Error: archive contains MVT vector tiles, but leafletRasterLayer is for displaying raster tiles. See https://github.com/protomaps/PMTiles/tree/main/js for details." 772 ); 773 } else if (header.tileType === 2) { 774 mimeType = "image/png"; 775 } else if (header.tileType === 3) { 776 mimeType = "image/jpeg"; 777 } else if (header.tileType === 4) { 778 mimeType = "image/webp"; 779 } else if (header.tileType === 5) { 780 mimeType = "image/avif"; 781 } 782 }); 783 loaded = true; 784 } 785 source.getZxy(coord.z, coord.x, coord.y, signal).then((arr) => { 786 if (arr) { 787 const blob = new Blob([arr.data], { type: mimeType }); 788 const imageUrl = window.URL.createObjectURL(blob); 789 el.src = imageUrl; 790 el.cancel = void 0; 791 done(void 0, el); 792 } 793 }).catch((e) => { 794 if (e.name !== "AbortError") { 795 throw e; 796 } 797 }); 798 return el; 799 }, 800 _removeTile: function(key) { 801 const tile = this._tiles[key]; 802 if (!tile) { 803 return; 804 } 805 if (tile.el.cancel) 806 tile.el.cancel(); 807 tile.el.width = 0; 808 tile.el.height = 0; 809 tile.el.deleted = true; 810 L.DomUtil.remove(tile.el); 811 delete this._tiles[key]; 812 this.fire("tileunload", { 813 tile: tile.el, 814 coords: this._keyToTileCoords(key) 815 }); 816 } 817 }); 818 return new cls(options); 819 }; 820 var v3compat = (v4) => (requestParameters, arg2) => { 821 if (arg2 instanceof AbortController) { 822 return v4(requestParameters, arg2); 823 } 824 const abortController = new AbortController(); 825 v4(requestParameters, abortController).then( 826 (result) => { 827 return arg2( 828 void 0, 829 result.data, 830 result.cacheControl || "", 831 result.expires || "" 832 ); 833 }, 834 (err2) => { 835 return arg2(err2); 836 } 837 ).catch((e) => { 838 return arg2(e); 839 }); 840 return { cancel: () => abortController.abort() }; 841 }; 842 var Protocol = class { 843 /** 844 * Initialize the MapLibre PMTiles protocol. 845 * 846 * * metadata: also load the metadata section of the PMTiles. required for some "inspect" functionality 847 * and to automatically populate the map attribution. Requires an extra HTTP request. 848 */ 849 constructor(options) { 850 /** @hidden */ 851 this.tilev4 = (params, abortController) => __async(this, null, function* () { 852 if (params.type === "json") {
853 const pmtilesUrl2 = params.url.substr(10); 854 let instance2 = this.tiles.get(pmtilesUrl2); 855 if (!instance2) { 856 instance2 = new PMTiles(pmtilesUrl2); 857 this.tiles.set(pmtilesUrl2, instance2); 858 } 859 if (this.metadata) { 860 return { 861 data: yield instance2.getTileJson(params.url) 862 }; 863 } 864 const h = yield instance2.getHeader(); 865 return { 866 data: { 867 tiles: [`${params.url}/{z}/{x}/{y}`], 868 minzoom: h.minZoom, 869 maxzoom: h.maxZoom, 870 bounds: [h.minLon, h.minLat, h.maxLon, h.maxLat] 871 } 872 }; 873 } 874 const re = new RegExp(/pmtiles:\/\/(.+)\/(\d+)\/(\d+)\/(\d+)/); 875 const result = params.url.match(re); 876 if (!result) { 877 throw new Error("Invalid PMTiles protocol URL"); 878 } 879 const pmtilesUrl = result[1]; 880 let instance = this.tiles.get(pmtilesUrl); 881 if (!instance) { 882 instance = new PMTiles(pmtilesUrl); 883 this.tiles.set(pmtilesUrl, instance); 884 } 885 const z = result[2]; 886 const x = result[3]; 887 const y = result[4]; 888 const header = yield instance.getHeader(); 889 const resp = yield instance == null ? void 0 : instance.getZxy(+z, +x, +y, abortController.signal); 890 if (resp) { 891 return { 892 data: new Uint8Array(resp.data), 893 cacheControl: resp.cacheControl, 894 expires: resp.expires 895 }; 896 } 897 if (header.tileType === 1 /* Mvt */) { 898 return { data: new Uint8Array() }; 899 } 900 return { data: null }; 901 }); 902 this.tile = v3compat(this.tilev4); 903 this.tiles = /* @__PURE__ */ new Map(); 904 this.metadata = (options == null ? void 0 : options.metadata) || false; 905 } 906 /** 907 * Add a {@link PMTiles} instance to the global protocol instance. 908 * 909 * For remote fetch sources, references in MapLibre styles like pmtiles://http://... 910 * will resolve to the same instance if the URLs match. 911 */ 912 add(p) { 913 this.tiles.set(p.source.getKey(), p); 914 } 915 /** 916 * Fetch a {@link PMTiles} instance by URL, for remote PMTiles instances. 917 */ 918 get(url) { 919 return this.tiles.get(url); 920 } 921 }; 922 923 // index.ts 924 function toNum(low, high) { 925 return (high >>> 0) * 4294967296 + (low >>> 0); 926 } 927 function readVarintRemainder(l, p) { 928 const buf = p.buf; 929 let b = buf[p.pos++]; 930 let h = (b & 112) >> 4; 931 if (b < 128) 932 return toNum(l, h); 933 b = buf[p.pos++]; 934 h |= (b & 127) << 3; 935 if (b < 128) 936 return toNum(l, h); 937 b = buf[p.pos++]; 938 h |= (b & 127) << 10; 939 if (b < 128) 940 return toNum(l, h); 941 b = buf[p.pos++]; 942 h |= (b & 127) << 17; 943 if (b < 128) 944 return toNum(l, h); 945 b = buf[p.pos++]; 946 h |= (b & 127) << 24; 947 if (b < 128) 948 return toNum(l, h); 949 b = buf[p.pos++]; 950 h |= (b & 1) << 31; 951 if (b < 128) 952 return toNum(l, h); 953 throw new Error("Expected varint not more than 10 bytes"); 954 } 955 function readVarint(p) { 956 const buf = p.buf; 957 let b = buf[p.pos++]; 958 let val = b & 127; 959 if (b < 128) 960 return val; 961 b = buf[p.pos++]; 962 val |= (b & 127) << 7; 963 if (b < 128) 964 return val; 965 b = buf[p.pos++]; 966 val |= (b & 127) << 14; 967 if (b < 128) 968 return val; 969 b = buf[p.pos++]; 970 val |= (b & 127) << 21; 971 if (b < 128) 972 return val; 973 b = buf[p.pos]; 974 val |= (b & 15) << 28; 975 return readVarintRemainder(val, p); 976 } 977 function rotate(n, xy, rx, ry) { 978 if (ry === 0) { 979 if (rx === 1) { 980 xy[0] = n - 1 - xy[0]; 981 xy[1] = n - 1 - xy[1]; 982 } 983 const t = xy[0]; 984 xy[0] = xy[1]; 985 xy[1] = t; 986 } 987 } 988 function idOnLevel(z, pos) { 989 const n = __pow(2, z); 990 let rx = pos; 991 let ry = pos; 992 let t = pos; 993 const xy = [0, 0]; 994 let s = 1; 995 while (s < n) { 996 rx = 1 & t / 2; 997 ry = 1 & (t ^ rx); 998 rotate(s, xy, rx, ry); 999 xy[0] += s * rx; 1000 xy[1] += s * ry; 1001 t = t / 4; 1002 s *= 2; 1003 } 1004 return [z, xy[0], xy[1]]; 1005 } 1006 var tzValues = [ 1007 0, 1008 1, 1009 5, 1010 21, 1011 85, 1012 341, 1013 1365, 1014 5461, 1015 21845, 1016 87381, 1017 349525, 1018 1398101, 1019 5592405, 1020 22369621, 1021 89478485, 1022 357913941, 1023 1431655765, 1024 5726623061, 1025 22906492245, 1026 91625968981, 1027 366503875925, 1028 1466015503701, 1029 5864062014805, 1030 23456248059221, 1031 93824992236885, 1032 375299968947541, 1033 1501199875790165 1034 ]; 1035 function zxyToTileId(z, x, y) { 1036 if (z > 26) { 1037 throw Error("Tile zoom level exceeds max safe number limit (26)"); 1038 } 1039 if (x > __pow(2, z) - 1 || y > __pow(2, z) - 1) { 1040 throw Error("tile x/y outside zoom level bounds"); 1041 } 1042 const acc = tzValues[z]; 1043 const n = __pow(2, z); 1044 let rx = 0; 1045 let ry = 0; 1046 let d = 0; 1047 const xy = [x, y]; 1048 let s = n / 2; 1049 while (s > 0) { 1050 rx = (xy[0] & s) > 0 ? 1 : 0; 1051 ry = (xy[1] & s) > 0 ? 1 : 0; 1052 d += s * s * (3 * rx ^ ry); 1053 rotate(s, xy, rx, ry); 1054 s = s / 2; 1055 } 1056 return acc + d; 1057 } 1058 function tileIdToZxy(i) { 1059 let acc = 0; 1060 const z = 0; 1061 for (let z2 = 0; z2 < 27; z2++) { 1062 const numTiles = (1 << z2) * (1 << z2); 1063 if (acc + numTiles > i) { 1064 return idOnLevel(z2, i - acc); 1065 } 1066 acc += numTiles; 1067 } 1068 throw Error("Tile zoom level exceeds max safe number limit (26)"); 1069 } 1070 var Compression = /* @__PURE__ */ ((Compression2) => { 1071 Compression2[Compression2["Unknown"] = 0] = "Unknown"; 1072 Compression2[Compression2["None"] = 1] = "None"; 1073 Compression2[Compression2["Gzip"] = 2] = "Gzip"; 1074 Compression2[Compression2["Brotli"] = 3] = "Brotli"; 1075 Compression2[Compression2["Zstd"] = 4] = "Zstd"; 1076 return Compression2; 1077 })(Compression || {}); 1078 function defaultDecompress(buf, compression) { 1079 return __async(this, null, function* () { 1080 if (compression === 1 /* None */ || compression === 0 /* Unknown */) { 1081 return buf; 1082 } 1083 if (compression === 2 /* Gzip */) { 1084 if (typeof globalThis.DecompressionStream === "undefined") { 1085 return decompressSync(new Uint8Array(buf)); 1086 } 1087 const stream = new Response(buf).body; 1088 if (!stream) { 1089 throw Error("Failed to read response stream"); 1090 } 1091 const result = stream.pipeThrough( 1092 // biome-ignore lint: needed to detect DecompressionStream in browser+node+cloudflare workers 1093 new globalThis.DecompressionStream("gzip") 1094 ); 1095 return new Response(result).arrayBuffer(); 1096 } 1097 throw Error("Compression method not supported"); 1098 }); 1099 } 1100 var TileType = /* @__PURE__ */ ((TileType2) => { 1101 TileType2[TileType2["Unknown"] = 0] = "Unknown"; 1102 TileType2[TileType2["Mvt"] = 1] = "Mvt"; 1103 TileType2[TileType2["Png"] = 2] = "Png"; 1104 TileType2[TileType2["Jpeg"] = 3] = "Jpeg"; 1105 TileType2[TileType2["Webp"] = 4] = "Webp"; 1106 TileType2[TileType2["Avif"] = 5] = "Avif"; 1107 return TileType2; 1108 })(TileType || {}); 1109 function tileTypeExt(t) { 1110 if (t === 1 /* Mvt */) 1111 return ".mvt"; 1112 if (t === 2 /* Png */) 1113 return ".png"; 1114 if (t === 3 /* Jpeg */) 1115 return ".jpg"; 1116 if (t === 4 /* Webp */) 1117 return ".webp"; 1118 if (t === 5 /* Avif */) 1119 return ".avif";
1120 return ""; 1121 } 1122 var HEADER_SIZE_BYTES = 127; 1123 function findTile(entries, tileId) { 1124 let m = 0; 1125 let n = entries.length - 1; 1126 while (m <= n) { 1127 const k = n + m >> 1; 1128 const cmp = tileId - entries[k].tileId; 1129 if (cmp > 0) { 1130 m = k + 1; 1131 } else if (cmp < 0) { 1132 n = k - 1; 1133 } else { 1134 return entries[k]; 1135 } 1136 } 1137 if (n >= 0) { 1138 if (entries[n].runLength === 0) { 1139 return entries[n]; 1140 } 1141 if (tileId - entries[n].tileId < entries[n].runLength) { 1142 return entries[n]; 1143 } 1144 } 1145 return null; 1146 } 1147 var FileSource = class { 1148 constructor(file) { 1149 this.file = file; 1150 } 1151 getKey() { 1152 return this.file.name; 1153 } 1154 getBytes(offset, length) { 1155 return __async(this, null, function* () { 1156 const blob = this.file.slice(offset, offset + length); 1157 const a = yield blob.arrayBuffer(); 1158 return { data: a }; 1159 }); 1160 } 1161 }; 1162 var FetchSource = class { 1163 constructor(url, customHeaders = new Headers()) { 1164 this.url = url; 1165 this.customHeaders = customHeaders; 1166 this.mustReload = false; 1167 let userAgent = ""; 1168 if ("navigator" in globalThis) { 1169 userAgent = globalThis.navigator.userAgent || ""; 1170 } 1171 const isWindows = userAgent.indexOf("Windows") > -1; 1172 const isChromiumBased = /Chrome|Chromium|Edg|OPR|Brave/.test(userAgent); 1173 this.chromeWindowsNoCache = false; 1174 if (isWindows && isChromiumBased) { 1175 this.chromeWindowsNoCache = true; 1176 } 1177 } 1178 getKey() { 1179 return this.url; 1180 } 1181 /** 1182 * Mutate the custom [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) set for all requests to the remote archive. 1183 */ 1184 setHeaders(customHeaders) { 1185 this.customHeaders = customHeaders; 1186 } 1187 getBytes(offset, length, passedSignal, etag) { 1188 return __async(this, null, function* () { 1189 let controller; 1190 let signal; 1191 if (passedSignal) { 1192 signal = passedSignal; 1193 } else { 1194 controller = new AbortController(); 1195 signal = controller.signal; 1196 } 1197 const requestHeaders = new Headers(this.customHeaders); 1198 requestHeaders.set("range", `bytes=${offset}-${offset + length - 1}`); 1199 let cache; 1200 if (this.mustReload) { 1201 cache = "reload"; 1202 } else if (this.chromeWindowsNoCache) { 1203 cache = "no-store"; 1204 } 1205 let resp = yield fetch(this.url, { 1206 signal, 1207 cache, 1208 headers: requestHeaders 1209 //biome-ignore lint: "cache" is incompatible between cloudflare workers and browser 1210 }); 1211 if (offset === 0 && resp.status === 416) { 1212 const contentRange = resp.headers.get("Content-Range"); 1213 if (!contentRange || !contentRange.startsWith("bytes */")) { 1214 throw Error("Missing content-length on 416 response"); 1215 } 1216 const actualLength = +contentRange.substr(8); 1217 resp = yield fetch(this.url, { 1218 signal, 1219 cache: "reload", 1220 headers: { range: `bytes=0-${actualLength - 1}` } 1221 //biome-ignore lint: "cache" is incompatible between cloudflare workers and browser 1222 }); 1223 } 1224 let newEtag = resp.headers.get("Etag"); 1225 if (newEtag == null ? void 0 : newEtag.startsWith("W/")) { 1226 newEtag = null; 1227 } 1228 if (resp.status === 416 || etag && newEtag && newEtag !== etag) { 1229 this.mustReload = true; 1230 throw new EtagMismatch( 1231 `Server returned non-matching ETag ${etag} after one retry. Check browser extensions and servers for issues that may affect correct ETag headers.` 1232 ); 1233 } 1234 if (resp.status >= 300) { 1235 throw Error(`Bad response code: ${resp.status}`); 1236 } 1237 const contentLength = resp.headers.get("Content-Length"); 1238 if (resp.status === 200 && (!contentLength || +contentLength > length)) { 1239 if (controller) 1240 controller.abort(); 1241 throw Error( 1242 "Server returned no content-length header or content-length exceeding request. Check that your storage backend supports HTTP Byte Serving." 1243 ); 1244 } 1245 const a = yield resp.arrayBuffer(); 1246 return { 1247 data: a, 1248 etag: newEtag || void 0, 1249 cacheControl: resp.headers.get("Cache-Control") || void 0, 1250 expires: resp.headers.get("Expires") || void 0 1251 }; 1252 }); 1253 } 1254 }; 1255 function getUint64(v, offset) { 1256 const wh = v.getUint32(offset + 4, true); 1257 const wl = v.getUint32(offset + 0, true); 1258 return wh * __pow(2, 32) + wl; 1259 } 1260 function bytesToHeader(bytes, etag) { 1261 const v = new DataView(bytes); 1262 const specVersion = v.getUint8(7); 1263 if (specVersion > 3) { 1264 throw Error( 1265 `Archive is spec version ${specVersion} but this library supports up to spec version 3` 1266 ); 1267 } 1268 return { 1269 specVersion, 1270 rootDirectoryOffset: getUint64(v, 8), 1271 rootDirectoryLength: getUint64(v, 16), 1272 jsonMetadataOffset: getUint64(v, 24), 1273 jsonMetadataLength: getUint64(v, 32), 1274 leafDirectoryOffset: getUint64(v, 40), 1275 leafDirectoryLength: getUint64(v, 48), 1276 tileDataOffset: getUint64(v, 56), 1277 tileDataLength: getUint64(v, 64), 1278 numAddressedTiles: getUint64(v, 72), 1279 numTileEntries: getUint64(v, 80), 1280 numTileContents: getUint64(v, 88),
1281 clustered: v.getUint8(96) === 1, 1282 internalCompression: v.getUint8(97), 1283 tileCompression: v.getUint8(98), 1284 tileType: v.getUint8(99), 1285 minZoom: v.getUint8(100), 1286 maxZoom: v.getUint8(101), 1287 minLon: v.getInt32(102, true) / 1e7, 1288 minLat: v.getInt32(106, true) / 1e7, 1289 maxLon: v.getInt32(110, true) / 1e7, 1290 maxLat: v.getInt32(114, true) / 1e7, 1291 centerZoom: v.getUint8(118), 1292 centerLon: v.getInt32(119, true) / 1e7, 1293 centerLat: v.getInt32(123, true) / 1e7, 1294 etag 1295 }; 1296 } 1297 function deserializeIndex(buffer) { 1298 const p = { buf: new Uint8Array(buffer), pos: 0 }; 1299 const numEntries = readVarint(p); 1300 const entries = []; 1301 let lastId = 0; 1302 for (let i = 0; i < numEntries; i++) { 1303 const v = readVarint(p); 1304 entries.push({ tileId: lastId + v, offset: 0, length: 0, runLength: 1 }); 1305 lastId += v; 1306 } 1307 for (let i = 0; i < numEntries; i++) { 1308 entries[i].runLength = readVarint(p); 1309 } 1310 for (let i = 0; i < numEntries; i++) { 1311 entries[i].length = readVarint(p); 1312 } 1313 for (let i = 0; i < numEntries; i++) { 1314 const v = readVarint(p); 1315 if (v === 0 && i > 0) { 1316 entries[i].offset = entries[i - 1].offset + entries[i - 1].length; 1317 } else { 1318 entries[i].offset = v - 1; 1319 } 1320 } 1321 return entries; 1322 } 1323 function detectVersion(a) { 1324 const v = new DataView(a); 1325 if (v.getUint16(2, true) === 2) { 1326 console.warn( 1327 "PMTiles spec version 2 has been deprecated; please see github.com/protomaps/PMTiles for tools to upgrade" 1328 ); 1329 return 2; 1330 } 1331 if (v.getUint16(2, true) === 1) { 1332 console.warn( 1333 "PMTiles spec version 1 has been deprecated; please see github.com/protomaps/PMTiles for tools to upgrade" 1334 ); 1335 return 1; 1336 } 1337 return 3; 1338 } 1339 var EtagMismatch = class extends Error { 1340 }; 1341 function getHeaderAndRoot(source, decompress) { 1342 return __async(this, null, function* () { 1343 const resp = yield source.getBytes(0, 16384); 1344 const v = new DataView(resp.data); 1345 if (v.getUint16(0, true) !== 19792) { 1346 throw new Error("Wrong magic number for PMTiles archive"); 1347 } 1348 if (detectVersion(resp.data) < 3) { 1349 return [yield v2_default.getHeader(source)]; 1350 } 1351 const headerData = resp.data.slice(0, HEADER_SIZE_BYTES); 1352 const header = bytesToHeader(headerData, resp.etag); 1353 const rootDirData = resp.data.slice( 1354 header.rootDirectoryOffset, 1355 header.rootDirectoryOffset + header.rootDirectoryLength 1356 ); 1357 const dirKey = `${source.getKey()}|${header.etag || ""}|${header.rootDirectoryOffset}|${header.rootDirectoryLength}`; 1358 const rootDir = deserializeIndex( 1359 yield decompress(rootDirData, header.internalCompression) 1360 ); 1361 return [header, [dirKey, rootDir.length, rootDir]]; 1362 }); 1363 } 1364 function getDirectory(source, decompress, offset, length, header) { 1365 return __async(this, null, function* () { 1366 const resp = yield source.getBytes(offset, length, void 0, header.etag); 1367 const data = yield decompress(resp.data, header.internalCompression); 1368 const directory = deserializeIndex(data); 1369 if (directory.length === 0) {
1370 throw new Error("Empty directory is invalid"); 1371 } 1372 return directory; 1373 }); 1374 } 1375 var ResolvedValueCache = class { 1376 constructor(maxCacheEntries = 100, prefetch = true, decompress = defaultDecompress) { 1377 this.cache = /* @__PURE__ */ new Map(); 1378 this.maxCacheEntries = maxCacheEntries; 1379 this.counter = 1; 1380 this.decompress = decompress; 1381 } 1382 getHeader(source) { 1383 return __async(this, null, function* () { 1384 const cacheKey = source.getKey(); 1385 const cacheValue = this.cache.get(cacheKey); 1386 if (cacheValue) { 1387 cacheValue.lastUsed = this.counter++; 1388 const data = cacheValue.data; 1389 return data; 1390 } 1391 const res = yield getHeaderAndRoot(source, this.decompress); 1392 if (res[1]) { 1393 this.cache.set(res[1][0], { 1394 lastUsed: this.counter++, 1395 data: res[1][2] 1396 }); 1397 } 1398 this.cache.set(cacheKey, { 1399 lastUsed: this.counter++, 1400 data: res[0] 1401 }); 1402 this.prune(); 1403 return res[0]; 1404 }); 1405 } 1406 getDirectory(source, offset, length, header) { 1407 return __async(this, null, function* () { 1408 const cacheKey = `${source.getKey()}|${header.etag || ""}|${offset}|${length}`; 1409 const cacheValue = this.cache.get(cacheKey); 1410 if (cacheValue) { 1411 cacheValue.lastUsed = this.counter++; 1412 const data = cacheValue.data; 1413 return data; 1414 } 1415 const directory = yield getDirectory( 1416 source, 1417 this.decompress, 1418 offset, 1419 length, 1420 header 1421 ); 1422 this.cache.set(cacheKey, { 1423 lastUsed: this.counter++, 1424 data: directory 1425 }); 1426 this.prune(); 1427 return directory; 1428 }); 1429 } 1430 // for v2 backwards compatibility 1431 getArrayBuffer(source, offset, length, header) { 1432 return __async(this, null, function* () { 1433 const cacheKey = `${source.getKey()}|${header.etag || ""}|${offset}|${length}`; 1434 const cacheValue = this.cache.get(cacheKey); 1435 if (cacheValue) { 1436 cacheValue.lastUsed = this.counter++; 1437 const data = yield cacheValue.data; 1438 return data; 1439 } 1440 const resp = yield source.getBytes(offset, length, void 0, header.etag); 1441 this.cache.set(cacheKey, { 1442 lastUsed: this.counter++, 1443 data: resp.data 1444 }); 1445 this.prune(); 1446 return resp.data; 1447 }); 1448 } 1449 prune() { 1450 if (this.cache.size > this.maxCacheEntries) { 1451 let minUsed = Infinity; 1452 let minKey = void 0; 1453 this.cache.forEach((cacheValue, key) => { 1454 if (cacheValue.lastUsed < minUsed) { 1455 minUsed = cacheValue.lastUsed; 1456 minKey = key; 1457 } 1458 }); 1459 if (minKey) { 1460 this.cache.delete(minKey); 1461 } 1462 } 1463 } 1464 invalidate(source) { 1465 return __async(this, null, function* () { 1466 this.cache.delete(source.getKey()); 1467 }); 1468 } 1469 }; 1470 var SharedPromiseCache = class { 1471 constructor(maxCacheEntries = 100, prefetch = true, decompress = defaultDecompress) { 1472 this.cache = /* @__PURE__ */ new Map(); 1473 this.invalidations = /* @__PURE__ */ new Map(); 1474 this.maxCacheEntries = maxCacheEntries; 1475 this.counter = 1; 1476 this.decompress = decompress; 1477 } 1478 getHeader(source) { 1479 return __async(this, null, function* () { 1480 const cacheKey = source.getKey(); 1481 const cacheValue = this.cache.get(cacheKey); 1482 if (cacheValue) { 1483 cacheValue.lastUsed = this.counter++; 1484 const data = yield cacheValue.data; 1485 return data; 1486 } 1487 const p = new Promise((resolve, reject) => { 1488 getHeaderAndRoot(source, this.decompress).then((res) => { 1489 if (res[1]) { 1490 this.cache.set(res[1][0], { 1491 lastUsed: this.counter++, 1492 data: Promise.resolve(res[1][2]) 1493 }); 1494 } 1495 resolve(res[0]); 1496 this.prune(); 1497 }).catch((e) => { 1498 reject(e); 1499 }); 1500 }); 1501 this.cache.set(cacheKey, { lastUsed: this.counter++, data: p }); 1502 return p; 1503 }); 1504 } 1505 getDirectory(source, offset, length, header) { 1506 return __async(this, null, function* () { 1507 const cacheKey = `${source.getKey()}|${header.etag || ""}|${offset}|${length}`; 1508 const cacheValue = this.cache.get(cacheKey); 1509 if (cacheValue) { 1510 cacheValue.lastUsed = this.counter++; 1511 const data = yield cacheValue.data; 1512 return data; 1513 } 1514 const p = new Promise((resolve, reject) => { 1515 getDirectory(source, this.decompress, offset, length, header).then((directory) => { 1516 resolve(directory); 1517 this.prune(); 1518 }).catch((e) => { 1519 reject(e); 1520 }); 1521 }); 1522 this.cache.set(cacheKey, { lastUsed: this.counter++, data: p }); 1523 return p; 1524 }); 1525 } 1526 // for v2 backwards compatibility 1527 getArrayBuffer(source, offset, length, header) { 1528 return __async(this, null, function* () { 1529 const cacheKey = `${source.getKey()}|${header.etag || ""}|${offset}|${length}`; 1530 const cacheValue = this.cache.get(cacheKey); 1531 if (cacheValue) { 1532 cacheValue.lastUsed = this.counter++; 1533 const data = yield cacheValue.data; 1534 return data; 1535 } 1536 const p = new Promise((resolve, reject) => { 1537 source.getBytes(offset, length, void 0, header.etag).then((resp) => { 1538 resolve(resp.data); 1539 if (this.cache.has(cacheKey)) { 1540 } 1541 this.prune(); 1542 }).catch((e) => { 1543 reject(e); 1544 }); 1545 }); 1546 this.cache.set(cacheKey, { lastUsed: this.counter++, data: p }); 1547 return p; 1548 }); 1549 } 1550 prune() { 1551 if (this.cache.size >= this.maxCacheEntries) { 1552 let minUsed = Infinity; 1553 let minKey = void 0; 1554 this.cache.forEach((cacheValue, key) => { 1555 if (cacheValue.lastUsed < minUsed) {
1556 minUsed = cacheValue.lastUsed; 1557 minKey = key; 1558 } 1559 }); 1560 if (minKey) { 1561 this.cache.delete(minKey); 1562 } 1563 } 1564 } 1565 invalidate(source) { 1566 return __async(this, null, function* () { 1567 const key = source.getKey(); 1568 if (this.invalidations.get(key)) { 1569 return yield this.invalidations.get(key); 1570 } 1571 this.cache.delete(source.getKey()); 1572 const p = new Promise((resolve, reject) => { 1573 this.getHeader(source).then((h) => { 1574 resolve(); 1575 this.invalidations.delete(key); 1576 }).catch((e) => { 1577 reject(e); 1578 }); 1579 }); 1580 this.invalidations.set(key, p); 1581 }); 1582 } 1583 }; 1584 var PMTiles = class { 1585 constructor(source, cache, decompress) { 1586 if (typeof source === "string") { 1587 this.source = new FetchSource(source); 1588 } else { 1589 this.source = source; 1590 } 1591 if (decompress) { 1592 this.decompress = decompress; 1593 } else { 1594 this.decompress = defaultDecompress; 1595 } 1596 if (cache) { 1597 this.cache = cache; 1598 } else { 1599 this.cache = new SharedPromiseCache(); 1600 } 1601 } 1602 /** 1603 * Return the header of the archive, 1604 * including information such as tile type, min/max zoom, bounds, and summary statistics. 1605 */ 1606 getHeader() { 1607 return __async(this, null, function* () { 1608 return yield this.cache.getHeader(this.source); 1609 }); 1610 } 1611 /** @hidden */ 1612 getZxyAttempt(z, x, y, signal) { 1613 return __async(this, null, function* () { 1614 const tileId = zxyToTileId(z, x, y); 1615 const header = yield this.cache.getHeader(this.source); 1616 if (header.specVersion < 3) { 1617 return v2_default.getZxy(header, this.source, this.cache, z, x, y, signal); 1618 } 1619 if (z < header.minZoom || z > header.maxZoom) { 1620 return void 0; 1621 } 1622 let dO = header.rootDirectoryOffset; 1623 let dL = header.rootDirectoryLength; 1624 for (let depth = 0; depth <= 3; depth++) { 1625 const directory = yield this.cache.getDirectory( 1626 this.source, 1627 dO, 1628 dL, 1629 header 1630 ); 1631 const entry = findTile(directory, tileId); 1632 if (entry) { 1633 if (entry.runLength > 0) { 1634 const resp = yield this.source.getBytes( 1635 header.tileDataOffset + entry.offset, 1636 entry.length, 1637 signal, 1638 header.etag 1639 ); 1640 return { 1641 data: yield this.decompress(resp.data, header.tileCompression), 1642 cacheControl: resp.cacheControl, 1643 expires: resp.expires 1644 }; 1645 } 1646 dO = header.leafDirectoryOffset + entry.offset; 1647 dL = entry.length; 1648 } else { 1649 return void 0; 1650 } 1651 } 1652 throw Error("Maximum directory depth exceeded"); 1653 }); 1654 } 1655 /** 1656 * Primary method to get a single tile's bytes from an archive. 1657 * 1658 * Returns undefined if the tile does not exist in the archive. 1659 */ 1660 getZxy(z, x, y, signal) { 1661 return __async(this, null, function* () { 1662 try { 1663 return yield this.getZxyAttempt(z, x, y, signal); 1664 } catch (e) { 1665 if (e instanceof EtagMismatch) { 1666 this.cache.invalidate(this.source); 1667 return yield this.getZxyAttempt(z, x, y, signal); 1668 } 1669 throw e; 1670 } 1671 }); 1672 } 1673 /** @hidden */ 1674 getMetadataAttempt() { 1675 return __async(this, null, function* () { 1676 const header = yield this.cache.getHeader(this.source); 1677 const resp = yield this.source.getBytes( 1678 header.jsonMetadataOffset, 1679 header.jsonMetadataLength, 1680 void 0, 1681 header.etag 1682 ); 1683 const decompressed = yield this.decompress( 1684 resp.data, 1685 header.internalCompression 1686 ); 1687 const dec = new TextDecoder("utf-8"); 1688 return JSON.parse(dec.decode(decompressed)); 1689 }); 1690 } 1691 /** 1692 * Return the arbitrary JSON metadata of the archive. 1693 */ 1694 getMetadata() { 1695 return __async(this, null, function* () { 1696 try { 1697 return yield this.getMetadataAttempt(); 1698 } catch (e) { 1699 if (e instanceof EtagMismatch) { 1700 this.cache.invalidate(this.source); 1701 return yield this.getMetadataAttempt(); 1702 } 1703 throw e; 1704 } 1705 }); 1706 } 1707 /** 1708 * Construct a [TileJSON](https://github.com/mapbox/tilejson-spec) object. 1709 * 1710 * baseTilesUrl is the desired tiles URL, excluding the suffix `/{z}/{x}/{y}.{ext}`. 1711 * For example, if the desired URL is `http://example.com/tileset/{z}/{x}/{y}.mvt`, 1712 * the baseTilesUrl should be `https://example.com/tileset`. 1713 */ 1714 getTileJson(baseTilesUrl) { 1715 return __async(this, null, function* () { 1716 const header = yield this.getHeader(); 1717 const metadata = yield this.getMetadata(); 1718 const ext = tileTypeExt(header.tileType); 1719 return { 1720 tilejson: "3.0.0", 1721 scheme: "xyz", 1722 tiles: [`${baseTilesUrl}/{z}/{x}/{y}${ext}`], 1723 // biome-ignore lint: TileJSON spec 1724 vector_layers: metadata.vector_layers, 1725 attribution: metadata.attribution, 1726 description: metadata.description, 1727 name: metadata.name, 1728 version: metadata.version, 1729 bounds: [header.minLon, header.minLat, header.maxLon, header.maxLat], 1730 center: [header.centerLon, header.centerLat, header.centerZoom], 1731 minzoom: header.minZoom, 1732 maxzoom: header.maxZoom 1733 }; 1734 }); 1735 } 1736 }; 1737 return __toCommonJS(js_exports); 1738})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.