+40
-18
src/pages-service.js
+40
-18
src/pages-service.js
···
64
64
}
65
65
}
66
66
67
+
class KnotClient {
68
+
constructor({ domain, ownerDid, repoName, branch }) {
69
+
this.domain = domain;
70
+
this.ownerDid = ownerDid;
71
+
this.repoName = repoName;
72
+
this.branch = branch;
73
+
}
74
+
75
+
async getBlob(filename) {
76
+
const url = `https://${this.domain}/${this.ownerDid}/${
77
+
this.repoName
78
+
}/blob/${this.branch}/${trimLeadingSlash(filename)}`;
79
+
const res = await fetch(url);
80
+
return await res.json();
81
+
}
82
+
83
+
async getRaw(filename) {
84
+
const url = `https://${this.domain}/${this.ownerDid}/${this.repoName}/raw/${
85
+
this.branch
86
+
}/${trimLeadingSlash(filename)}`;
87
+
const res = await fetch(url, {
88
+
responseType: "arraybuffer",
89
+
});
90
+
const arrayBuffer = await res.arrayBuffer();
91
+
return Buffer.from(arrayBuffer);
92
+
}
93
+
}
94
+
67
95
class PagesService {
68
96
constructor({
69
97
domain,
···
71
99
repoName,
72
100
branch,
73
101
configFilepath = "pages_config.yaml",
74
-
verbose = false,
75
102
fileCacheExpirationSeconds = 10,
76
103
}) {
77
104
this.domain = domain;
···
79
106
this.repoName = repoName;
80
107
this.branch = branch;
81
108
this.configFilepath = configFilepath;
82
-
this.verbose = verbose;
83
109
this.fileCache = new FileCache({
84
110
expirationSeconds: fileCacheExpirationSeconds,
111
+
});
112
+
this.client = new KnotClient({
113
+
domain: domain,
114
+
ownerDid: ownerDid,
115
+
repoName: repoName,
116
+
branch: branch,
85
117
});
86
118
}
87
119
···
110
142
async getFileContent(filename) {
111
143
const cachedContent = this.fileCache.get(filename);
112
144
if (cachedContent) {
113
-
if (this.verbose) {
114
-
console.log(`Cache hit for ${filename}`);
115
-
}
116
145
return cachedContent;
117
146
}
118
-
// todo error handling?
119
-
const url = `https://${this.domain}/${this.ownerDid}/${
120
-
this.repoName
121
-
}/blob/${this.branch}/${trimLeadingSlash(filename)}`;
122
-
if (this.verbose) {
123
-
console.log(`Fetching ${url}`);
147
+
let content = null;
148
+
const blob = await this.client.getBlob(filename);
149
+
if (blob.is_binary) {
150
+
content = await this.client.getRaw(filename);
151
+
} else {
152
+
content = blob.contents;
124
153
}
125
-
const res = await fetch(url, {
126
-
headers: {
127
-
Accept: "application/json",
128
-
},
129
-
});
130
-
const blob = await res.json();
131
-
const content = blob.contents;
132
154
this.fileCache.set(filename, content);
133
155
return content;
134
156
}