@rr0/cms
Version:
RR0 Content Management System (CMS)
27 lines (26 loc) • 1.3 kB
JavaScript
import { MimeType } from "./HttpSource.js";
import { FetchHttpFetcher } from "./FetchHttpFetcher.js";
export class ArchiveHttpFetcher {
constructor() {
this.fetcher = new FetchHttpFetcher();
}
async fetch(url, init, resOut, error) {
const archiveUrl = new URL("http://archive.org/wayback/available?url=" + url);
const archiveInit = { headers: { "content-type": MimeType.json } };
const archiveJson = await this.fetcher.fetch(archiveUrl, archiveInit, resOut);
const closest = archiveJson.archived_snapshots["closest"];
if (!closest) {
throw error;
}
const archivedUrl = new URL(closest.url);
const result = await this.fetcher.fetch(archivedUrl, init, resOut);
const timestamp = closest.timestamp;
const archiveDate = new Date(timestamp.substring(0, 4) + "-" + timestamp.substring(4, 6) + "-" + timestamp.substring(6, 8) + " "
+ timestamp.substring(8, 10) + ":" + timestamp.substring(10, 12) + ":" + timestamp.substring(12, 14));
const headers = new Headers(resOut.headers);
headers.set("last-modified", archiveDate.toString());
headers.set("host", url.hostname);
Object.assign(resOut, { url: archivedUrl, headers });
return result;
}
}