From 8eb3a155498caa1857e978864685890476fc178e Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Thu, 25 Feb 2021 16:10:29 +0800 Subject: [PATCH] small refactoring of fetch --- js/pmtiles.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/js/pmtiles.js b/js/pmtiles.js index a20f7d1..f26de6a 100644 --- a/js/pmtiles.js +++ b/js/pmtiles.js @@ -40,12 +40,13 @@ } class PMTiles { - constructor(url,ready) { + constructor(url,headers={},ready) { + headers.Range = 'bytes=0-511999' 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 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() }).then(buf => { 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)) }) + } else { + throw new Error("Invalid response: " + resp.status) } }) this.step = 0