taxonium-component
Version:
React component for exploring large phylogenetic trees in the browser
93 lines (92 loc) • 3.01 kB
JavaScript
import { g as d } from "./index-CoM8QAjP.js";
import { aF as u } from "./JBrowsePanel-BNE3gNW1.js";
var w = u();
const $ = /* @__PURE__ */ d(w);
function h(i) {
return (typeof i == "object" && i !== null && "message" in i ? i.message : `${i}`).replace(/\.$/, "");
}
class y {
constructor(e, t = {}) {
this.baseOverrides = {}, this.url = e;
const s = t.fetch || globalThis.fetch.bind(globalThis);
t.overrides && (this.baseOverrides = t.overrides), this.fetchImplementation = s;
}
async fetch(e, t) {
let s;
try {
s = await this.fetchImplementation(e, t);
} catch (a) {
if (`${a}`.includes("Failed to fetch")) {
console.warn(`generic-filehandle: refetching ${e} to attempt to work around chrome CORS header caching bug`);
try {
s = await this.fetchImplementation(e, {
...t,
cache: "reload"
});
} catch (n) {
throw new Error(`${h(n)} fetching ${e}`, { cause: n });
}
} else
throw new Error(`${h(a)} fetching ${e}`, { cause: a });
}
return s;
}
async read(e, t, s = {}) {
const { headers: a = {}, signal: n, overrides: c = {} } = s;
e < 1 / 0 ? a.range = `bytes=${t}-${t + e}` : e === 1 / 0 && t !== 0 && (a.range = `bytes=${t}-`);
const r = await this.fetch(this.url, {
...this.baseOverrides,
...c,
headers: {
...a,
...c.headers,
...this.baseOverrides.headers
},
method: "GET",
redirect: "follow",
mode: "cors",
signal: n
});
if (!r.ok)
throw new Error(`HTTP ${r.status} fetching ${this.url}`);
if (r.status === 200 && t === 0 || r.status === 206) {
const f = await r.arrayBuffer(), l = r.headers.get("content-range"), o = /\/(\d+)$/.exec(l || "");
return o != null && o[1] && (this._stat = {
size: parseInt(o[1], 10)
}), new Uint8Array(f.slice(0, e));
}
throw r.status === 200 ? new Error(`${this.url} fetch returned status 200, expected 206`) : new Error(`HTTP ${r.status} fetching ${this.url}`);
}
async readFile(e = {}) {
let t, s;
typeof e == "string" ? (t = e, s = {}) : (t = e.encoding, s = e, delete s.encoding);
const { headers: a = {}, signal: n, overrides: c = {} } = s, r = await this.fetch(this.url, {
headers: a,
method: "GET",
redirect: "follow",
mode: "cors",
signal: n,
...this.baseOverrides,
...c
});
if (r.status !== 200)
throw new Error(`HTTP ${r.status} fetching ${this.url}`);
if (t === "utf8")
return r.text();
if (t)
throw new Error(`unsupported encoding: ${t}`);
return new Uint8Array(await r.arrayBuffer());
}
async stat() {
if (!this._stat && (await this.read(10, 0), !this._stat))
throw new Error(`unable to determine size of file at ${this.url}`);
return this._stat;
}
async close() {
}
}
export {
$ as Q,
y as R
};
//# sourceMappingURL=remoteFile-H_6BTCFF.js.map