mirror of
https://github.com/protomaps/PMTiles.git
synced 2026-02-04 10:51:07 +00:00
js: FetchSource takes optional custom headers (#112)
* js: FetchSource takes optional custom headers
This commit is contained in:
@@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
let PMTILES_URL = "https://protomaps.github.io/PMTiles/protomaps(vector)ODbL_firenze.pmtiles";
|
let PMTILES_URL = "https://protomaps.github.io/PMTiles/protomaps(vector)ODbL_firenze.pmtiles";
|
||||||
|
|
||||||
const p = new pmtiles.PMTiles(PMTILES_URL)
|
const p = new pmtiles.PMTiles(PMTILES_URL);
|
||||||
|
|
||||||
// this is so we share one instance across the JS code and the map renderer
|
// this is so we share one instance across the JS code and the map renderer
|
||||||
protocol.add(p);
|
protocol.add(p);
|
||||||
|
|||||||
16
js/index.ts
16
js/index.ts
@@ -282,15 +282,21 @@ export class FileAPISource implements Source {
|
|||||||
|
|
||||||
export class FetchSource implements Source {
|
export class FetchSource implements Source {
|
||||||
url: string;
|
url: string;
|
||||||
|
customHeaders: Headers;
|
||||||
|
|
||||||
constructor(url: string) {
|
constructor(url: string, customHeaders: Headers = new Headers()) {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
|
this.customHeaders = customHeaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
getKey() {
|
getKey() {
|
||||||
return this.url;
|
return this.url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setHeaders(customHeaders: Headers) {
|
||||||
|
this.customHeaders = customHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
async getBytes(
|
async getBytes(
|
||||||
offset: number,
|
offset: number,
|
||||||
length: number,
|
length: number,
|
||||||
@@ -303,9 +309,15 @@ export class FetchSource implements Source {
|
|||||||
signal = controller.signal;
|
signal = controller.signal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const requestHeaders = new Headers(this.customHeaders);
|
||||||
|
requestHeaders.set(
|
||||||
|
"Range",
|
||||||
|
"bytes=" + offset + "-" + (offset + length - 1)
|
||||||
|
);
|
||||||
|
|
||||||
let resp = await fetch(this.url, {
|
let resp = await fetch(this.url, {
|
||||||
signal: signal,
|
signal: signal,
|
||||||
headers: { Range: "bytes=" + offset + "-" + (offset + length - 1) },
|
headers: requestHeaders,
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: can return 416 with offset > 0 if content changed, which will have a blank etag.
|
// TODO: can return 416 with offset > 0 if content changed, which will have a blank etag.
|
||||||
|
|||||||
Reference in New Issue
Block a user