metadata is promise-based

This commit is contained in:
Brandon Liu
2021-03-01 11:10:58 +08:00
parent 09e58ccde3
commit edaf544dd7

View File

@@ -40,9 +40,9 @@
} }
class PMTiles { class PMTiles {
constructor(url,ready) { constructor(url) {
this.url = url this.url = url
this.rootdir = fetch(this.url,{method:'HEAD',headers:{Range:'bytes=0-511999'}}).then(resp => { this.root = fetch(this.url,{method:'HEAD',headers:{Range:'bytes=0-511999'}}).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:{Range:'bytes=0-511999'}}).then(resp => {
@@ -50,10 +50,10 @@
}).then(buf => { }).then(buf => {
const header = parseHeader(new DataView(buf,0,10)) const header = parseHeader(new DataView(buf,0,10))
var dec = new TextDecoder("utf-8") var dec = new TextDecoder("utf-8")
if (ready) { return {
ready(JSON.parse(dec.decode(new DataView(buf,10,header.json_size)))) metadata: JSON.parse(dec.decode(new DataView(buf,10,header.json_size))),
dir: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 { } else {
throw new Error("Invalid response: " + resp.status) throw new Error("Invalid response: " + resp.status)
@@ -64,6 +64,12 @@
this.outstanding_requests = new Map() this.outstanding_requests = new Map()
} }
metadata = func => {
this.root.then(root => {
func(root.metadata)
})
}
getLeaf = (offset, len) => { getLeaf = (offset, len) => {
return new Promise((resolve,reject) => { return new Promise((resolve,reject) => {
if (this.leaves.has(offset)) { if (this.leaves.has(offset)) {
@@ -100,16 +106,16 @@
getZxy = (z,x,y) => { getZxy = (z,x,y) => {
var strid = z + '_' + x + '_' + y var strid = z + '_' + x + '_' + y
return this.rootdir.then(rootdir => { return this.root.then(root => {
if (rootdir.has(strid) && rootdir.get(strid)[2] == 0) { if (root.dir.has(strid) && root.dir.get(strid)[2] == 0) {
return rootdir.get(strid) return root.dir.get(strid)
} else { } else {
if (z >= 7) { if (z >= 7) {
var z7_tile_diff = (z - 7) var z7_tile_diff = (z - 7)
var z7_tile = [7,Math.trunc(x / (1 << z7_tile_diff)), Math.trunc(y / (1 << z7_tile_diff))] var z7_tile = [7,Math.trunc(x / (1 << z7_tile_diff)), Math.trunc(y / (1 << z7_tile_diff))]
var z7_tile_str = z7_tile[0] + "_" + z7_tile[1] + "_" + z7_tile[2] var z7_tile_str = z7_tile[0] + "_" + z7_tile[1] + "_" + z7_tile[2]
if (rootdir.has(z7_tile_str)) { if (root.dir.has(z7_tile_str)) {
const val = rootdir.get(z7_tile_str) const val = root.dir.get(z7_tile_str)
return this.getLeaf(val[0],val[1]).then(leafdir => { return this.getLeaf(val[0],val[1]).then(leafdir => {
if (leafdir.has(strid)) { if (leafdir.has(strid)) {
return leafdir.get(strid) return leafdir.get(strid)