mirror of
https://github.com/protomaps/PMTiles.git
synced 2026-02-04 10:51:07 +00:00
detect byte serving support via Content-Length [#11]
This commit is contained in:
18
js/index.mjs
18
js/index.mjs
@@ -37,11 +37,13 @@ const bytesToMap = dataview => {
|
||||
export class PMTiles {
|
||||
constructor(url, options = {}) {
|
||||
this.url = url
|
||||
this.root = fetch(this.url,{method:'HEAD',headers:{Range:'bytes=0-511999'}}).then(resp => {
|
||||
// for servers like Azure, GH Pages which return 200 instead of 206
|
||||
if (resp.status == 206 || (resp.status == 200 && options.allow_200)) {
|
||||
console.log("Check succeeded: server supports byte ranges")
|
||||
return fetch(this.url,{headers:{Range:'bytes=0-511999'}}).then(resp => {
|
||||
const controller = new AbortController()
|
||||
const signal = controller.signal
|
||||
this.root = fetch(this.url,{signal:signal, headers:{Range:'bytes=0-511999'}}).then(resp => {
|
||||
if (resp.headers.get('Content-Length') != 512000) {
|
||||
console.error("Content-Length mismatch indicates byte serving not supported; aborting.")
|
||||
controller.abort()
|
||||
}
|
||||
return resp.arrayBuffer()
|
||||
}).then(buf => {
|
||||
const header = parseHeader(new DataView(buf,0,10))
|
||||
@@ -51,11 +53,7 @@ export class PMTiles {
|
||||
dir:bytesToMap(new DataView(buf,10+header.json_size,17*header.root_entries))
|
||||
}
|
||||
})
|
||||
} else {
|
||||
console.log("Check failed: if you know this server supports byte serving, pass allow_200: true")
|
||||
throw new Error("Invalid response: " + resp.status)
|
||||
}
|
||||
})
|
||||
|
||||
this.step = 0
|
||||
this.leaves = new Map()
|
||||
this.outstanding_requests = new Map()
|
||||
|
||||
Reference in New Issue
Block a user