Static site hosting via tangled
1import { trimLeadingSlash } from "./helpers.js";
2
3export class KnotClient {
4 constructor({ domain, ownerDid, repoName, branch }) {
5 this.domain = domain;
6 this.ownerDid = ownerDid;
7 this.repoName = repoName;
8 this.branch = branch;
9 }
10
11 async getBlob(filename) {
12 const params = new URLSearchParams({
13 repo: `${this.ownerDid}/${this.repoName}`,
14 path: trimLeadingSlash(filename),
15 ref: this.branch,
16 });
17 const url = `https://${this.domain}/xrpc/sh.tangled.repo.blob?${params}`;
18 console.log(`[KNOT CLIENT]: GET ${url}`);
19 const res = await fetch(url);
20 return await res.json();
21 }
22
23 async getRaw(filename) {
24 const params = new URLSearchParams({
25 repo: `${this.ownerDid}/${this.repoName}`,
26 path: trimLeadingSlash(filename),
27 ref: this.branch,
28 raw: "true",
29 });
30 const url = `https://${this.domain}/xrpc/sh.tangled.repo.blob?${params}`;
31 console.log(`[KNOT CLIENT]: GET ${url}`);
32 const res = await fetch(url, {
33 responseType: "arraybuffer",
34 });
35 const arrayBuffer = await res.arrayBuffer();
36 return Buffer.from(arrayBuffer);
37 }
38}