small refactoring of fetch

This commit is contained in:
Brandon Liu
2021-02-25 16:10:29 +08:00
parent 8b0b193855
commit 8eb3a15549

View File

@@ -40,12 +40,13 @@
} }
class PMTiles { class PMTiles {
constructor(url,ready) { constructor(url,headers={},ready) {
headers.Range = 'bytes=0-511999'
this.url = url this.url = url
this.rootdir = fetch(this.url,{method:'HEAD',headers:{'Range':'bytes=0-511999'}}).then(resp => { this.rootdir = fetch(this.url,{method:'HEAD',headers:headers}).then(resp => {
if (resp.status == 206) { // this does not work on Azure, it returns 200 instead of 206 if (resp.status == 206) { // this does not work on Azure, it returns 200 instead of 206
console.log("Check succeeded: server supports byte ranges") console.log("Check succeeded: server supports byte ranges")
return fetch(this.url,{headers:{'Range':'bytes=0-511999'}}).then(resp => { return fetch(this.url,{headers:headers}).then(resp => {
return resp.arrayBuffer() return resp.arrayBuffer()
}).then(buf => { }).then(buf => {
const header = parseHeader(new DataView(buf,0,10)) const header = parseHeader(new DataView(buf,0,10))
@@ -55,6 +56,8 @@
} }
return bytesToMap(new DataView(buf,10+header.json_size,17*header.root_entries)) return bytesToMap(new DataView(buf,10+header.json_size,17*header.root_entries))
}) })
} else {
throw new Error("Invalid response: " + resp.status)
} }
}) })
this.step = 0 this.step = 0