Static site hosting via tangled

Handle binary

Changed files
+41 -19
src
+40 -18
src/pages-service.js
···
}
}
+
class KnotClient {
+
constructor({ domain, ownerDid, repoName, branch }) {
+
this.domain = domain;
+
this.ownerDid = ownerDid;
+
this.repoName = repoName;
+
this.branch = branch;
+
}
+
+
async getBlob(filename) {
+
const url = `https://${this.domain}/${this.ownerDid}/${
+
this.repoName
+
}/blob/${this.branch}/${trimLeadingSlash(filename)}`;
+
const res = await fetch(url);
+
return await res.json();
+
}
+
+
async getRaw(filename) {
+
const url = `https://${this.domain}/${this.ownerDid}/${this.repoName}/raw/${
+
this.branch
+
}/${trimLeadingSlash(filename)}`;
+
const res = await fetch(url, {
+
responseType: "arraybuffer",
+
});
+
const arrayBuffer = await res.arrayBuffer();
+
return Buffer.from(arrayBuffer);
+
}
+
}
+
class PagesService {
constructor({
domain,
···
repoName,
branch,
configFilepath = "pages_config.yaml",
-
verbose = false,
fileCacheExpirationSeconds = 10,
}) {
this.domain = domain;
···
this.repoName = repoName;
this.branch = branch;
this.configFilepath = configFilepath;
-
this.verbose = verbose;
this.fileCache = new FileCache({
expirationSeconds: fileCacheExpirationSeconds,
+
});
+
this.client = new KnotClient({
+
domain: domain,
+
ownerDid: ownerDid,
+
repoName: repoName,
+
branch: branch,
});
}
···
async getFileContent(filename) {
const cachedContent = this.fileCache.get(filename);
if (cachedContent) {
-
if (this.verbose) {
-
console.log(`Cache hit for ${filename}`);
-
}
return cachedContent;
}
-
// todo error handling?
-
const url = `https://${this.domain}/${this.ownerDid}/${
-
this.repoName
-
}/blob/${this.branch}/${trimLeadingSlash(filename)}`;
-
if (this.verbose) {
-
console.log(`Fetching ${url}`);
+
let content = null;
+
const blob = await this.client.getBlob(filename);
+
if (blob.is_binary) {
+
content = await this.client.getRaw(filename);
+
} else {
+
content = blob.contents;
}
-
const res = await fetch(url, {
-
headers: {
-
Accept: "application/json",
-
},
-
});
-
const blob = await res.json();
-
const content = blob.contents;
this.fileCache.set(filename, content);
return content;
}
+1 -1
src/worker.js
···
let pagesService = null;
-
// idk how long cloudflare will keep this around.
+
// idk how long cloudflare keeps this around.
// it would be better to save the config in a KV store
// but this is good enough for now
function getPagesService(env) {