UNPKG

@expofp/floorplan

Version:

Interactive floor plan library for expos and events

2 lines (1 loc) • 914 B
import{FetchSource as s,PMTiles as u}from"pmtiles";const i=16384;export class RangeTolerantSource{constructor(r){this.url=r,this.ranged=new s(r)}getKey(){return this.url}async getBytes(r,n,t,o){this.archive||(this.archive=this.probe());const a=await this.archive;return a?(c(t),{data:await h(a.slice(r,r+n))}):this.ranged.getBytes(r,n,t,o)}async probe(){let r;try{r=await fetch(this.url,{headers:{Range:`bytes=0-${i-1}`}})}catch{return null}if(r.status!==200)return null;const n=await r.blob();return n.size===i?null:n}}export function registerPmtilesArchive(e,r){e.get(r)||e.add(new u(new RangeTolerantSource(r)))}function c(e){if(e?.aborted)throw e.reason??new DOMException("The operation was aborted.","AbortError")}async function h(e){return typeof e.arrayBuffer=="function"?e.arrayBuffer():new Promise((r,n)=>{const t=new FileReader;t.onload=()=>r(t.result),t.onerror=()=>n(t.error),t.readAsArrayBuffer(e)})}