mirror of
https://github.com/protomaps/PMTiles.git
synced 2026-03-21 22:39:39 +00:00
* js: Allow passing credentials option to fetch [#397] * fix passing custom headers in case where remote archive is < 16 kB * clean up `any` usage
This commit is contained in:
@@ -6,21 +6,27 @@ class MockServer {
|
||||
etag?: string;
|
||||
numRequests: number;
|
||||
lastCache?: string;
|
||||
lastRequestHeaders: Headers | null;
|
||||
lastCredentials?: string;
|
||||
|
||||
reset() {
|
||||
this.numRequests = 0;
|
||||
this.etag = undefined;
|
||||
this.lastRequestHeaders = null;
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.numRequests = 0;
|
||||
this.etag = undefined;
|
||||
this.lastRequestHeaders = null;
|
||||
const serverBuffer = fs.readFileSync("test/data/test_fixture_1.pmtiles");
|
||||
const server = setupServer(
|
||||
http.get(
|
||||
"http://localhost:1337/example.pmtiles",
|
||||
({ request, params }) => {
|
||||
this.lastCache = request.cache;
|
||||
this.lastRequestHeaders = request.headers;
|
||||
this.lastCredentials = request.credentials;
|
||||
this.numRequests++;
|
||||
const range = request.headers.get("range")?.substr(6).split("-");
|
||||
if (!range) {
|
||||
@@ -35,7 +41,31 @@ class MockServer {
|
||||
headers: { etag: this.etag } as HeadersInit,
|
||||
});
|
||||
}
|
||||
)
|
||||
),
|
||||
http.get("http://localhost:1337/small.pmtiles", ({ request }) => {
|
||||
this.lastRequestHeaders = request.headers;
|
||||
this.lastCredentials = request.credentials;
|
||||
this.numRequests++;
|
||||
const range = request.headers.get("range")?.substr(6).split("-");
|
||||
if (!range) {
|
||||
throw new Error("invalid range");
|
||||
}
|
||||
const rangeEnd = +range[1];
|
||||
if (rangeEnd >= serverBuffer.length) {
|
||||
return new HttpResponse(null, {
|
||||
status: 416,
|
||||
headers: {
|
||||
"Content-Range": `bytes */${serverBuffer.length}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
const rangeStart = +range[0];
|
||||
const body = serverBuffer.slice(rangeStart, rangeEnd + 1);
|
||||
return new HttpResponse(body, {
|
||||
status: 206,
|
||||
headers: { etag: this.etag } as HeadersInit,
|
||||
});
|
||||
})
|
||||
);
|
||||
server.listen({ onUnhandledRequest: "error" });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user