-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticles.js
More file actions
308 lines (255 loc) · 8.67 KB
/
articles.js
File metadata and controls
308 lines (255 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import data from "./articles.json" with { type: "json" };
/**
* @typedef {Object} Article
* @property {string} id
* @property {string} slug
* @property {string} title
* @property {string} date ISO 8601 date string
* @property {string[]} tags
* @property {string[]} authors
*/
/**
* @typedef {Object} ArticlesFile
* @property {string} format
* @property {number} version
* @property {string} lastChanged ISO 8601 date string
* @property {string} range.from ISO 8601 date string
* @property {string} range.to ISO 8601 date string
* @property {Article[]} articles
* @property {{ path: string } | null} [previous]
*/
/**
* @typedef {Object} ArticlesHandler
* @property {(id: string, file: ArticlesFile) => Promise<Article|null>} getById
* @property {(slug: string, file: ArticlesFile) => Promise<Article|null>} getBySlug
* @property {(count?: number, file?: ArticlesFile) => Promise<Article[]>} getLatest
* @property {(from: string, to: string, file?: ArticlesFile) => Promise<Article[]>} getByDate
* @property {(tags: string[], file?: ArticlesFile) => Promise<Article[]>} getByTags
* @property {(authors: string[], file?: ArticlesFile) => Promise<Article[]>} getByAuthors
*/
export async function init() {
await loadCache();
}
/**
* Finds the first Article with the given ID.
*
* @param {string} id
* @param {ArticlesFile} [file=data]
* @returns {Promise<Article | null>}
*/
export async function getById(id, file = data) {
const handler = getHandler(file);
return handler.getById(id, file);
}
/**
* Finds the first Article with the given Slug.
*
* @param {string} slug
* @param {ArticlesFile} [file=data]
* @returns {Promise<Article | null>}
*/
export async function getBySlug(slug, file = data) {
const handler = getHandler(file);
return handler.getBySlug(slug, file);
}
/**
* Finds the latest Articles.
*
* @param {number} [count=1] How many articles should be retrieved
* @param {ArticlesFile} [file=data]
* @returns {Promise<Article[]>}
*/
export async function getLatest(count = 1, file = data) {
const handler = getHandler(file);
return handler.getLatest(count, file);
}
/**
* Finds all articles for the given range.
*
* @param {string} from ISO 8601 date string
* @param {string} to ISO 8601 date string
* @param {ArticlesFile} [file=data]
* @returns {Promise<Article[]>}
*/
export async function getByDate(from, to, file = data) {
const handler = getHandler(file);
return handler.getByDate(from, to, file);
}
/**
* Finds all articles that have all of the given tags.
*
* @param {string[]} tags
* @param {ArticlesFile} [file=data]
* @returns {Promise<Article[]>}
*/
export async function getByTags(tags, file = data) {
const handler = getHandler(file);
return handler.getByTags(tags, file);
}
/**
* Finds all articles that have all of the given authors.
*
* @param {string[]} authors
* @param {ArticlesFile} file
* @returns {Promise<Article[]>}
*/
export async function getByAuthors(authors, file = data) {
const handler = getHandler(file);
return handler.getByAuthors(authors, file);
}
//----- ArticlesHandler -----//
/** @type {ArticlesHandler} */
const jsonHandlerV1 = {
async getById(id, file) {
let result = file.articles.find(a => a.id === id);
if(!result && file.previous?.path) result = await this.getById(id, await getFile(file.previous));
return result;
},
async getBySlug(slug, file) {
let result = file.articles.find(a => a.slug === slug);
if(!result && file.previous?.path) result = await this.getBySlug(slug, await getFile(file.previous));
return result;
},
async getLatest(count = 1, file) {
const extracted = file.articles.slice(0, count);
const remaining = count - extracted.length;
if (remaining > 0 && file.previous?.path) {
const prevArticles = await this.getLatest(remaining, await getFile(file.previous));
extracted.push(...prevArticles);
}
return extracted;
},
async getByDate(from, to, file) {
const start = new Date(from);
const end = new Date(to);
if (Number.isNaN(start.getTime()) || Number.isNaN(end.getTime())) {
throw new TypeError("Invalid ISO date string");
}
if (start > end) {
throw new RangeError("Start date must be before end date");
}
const fileFrom = new Date(file.range.from);
const fileTo = new Date(file.range.to);
const result = [];
if (start <= fileTo && end >= fileFrom) {
for (const a of file.articles) {
const articleDate = new Date(a.date);
if (articleDate >= start && articleDate <= end) {
result.push(a);
}
}
}
if (file.previous?.path && start < fileFrom) {
const prev = await this.getByDate(from, to, await getFile(file.previous));
result.push(...prev);
}
return result;
},
async getByTags(tags, file) {
const result = [];
for(const a of file.articles) {
const allIncluded = tags.every(tag => a.tags.includes(tag));
if(allIncluded) result.push(a);
}
if(file.previous?.path) {
const prev = await this.getByTags(tags, await getFile(file.previous));
result.push(...prev);
}
return result;
},
async getByAuthors(tags, file) {
const result = [];
for(const a of file.articles) {
const allIncluded = tags.every(tag => a.authors.includes(tag));
if(allIncluded) result.push(a);
}
if(file.previous?.path) {
const prev = await this.getByAuthors(tags, await getFile(file.previous));
result.push(...prev);
}
return result;
}
}
//----- Mapping API to ArticlesHandler -----//
const handlers = {
"article-collection@1": jsonHandlerV1
};
function getHandler(file) {
const fileFormat = `${file.format}@${file.version}`;
const handler = handlers[fileFormat];
if(!handler) throw new Error(`Unsupported file format ${fileFormat}`);
return handler;
}
/**
*
* @param {string} formatVersion must match the "format@version" pattern
* @param {ArticlesHandler} handler
*/
export function registerHandler(formatVersion, handler) {
const requiredMethods = ["getById", "getBySlug", "getLatest", "getByDate", "getByTags", "getByAuthors"];
if (typeof formatVersion !== "string" || !formatVersion.includes("@")) {
throw new TypeError(`Invalid formatVersion: ${formatVersion}`);
}
if (!handler) {
throw new TypeError("Handler must be an object implementing ArticlesHandler");
}
for (const method of requiredMethods) {
if (typeof handler[method] !== "function") {
throw new TypeError(`Handler is missing method: ${method}`);
}
}
if(handlers[formatVersion]) console.warn(`Handler for ${formatVersion} is being overwritten`);
handlers[formatVersion] = handler;
}
//----- Internal File Logic -----//
const cache = new Map();
async function getFile({ path, lastChanged }) {
if (!path) return null;
let result = cache.get(path);
if (!result || new Date(lastChanged) > new Date(result.lastChanged)) {
result = await loadFile(path);
}
return result;
}
async function loadFile(url) {
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
cache.set(url, data);
saveCacheEntry(url, data);
return data;
}
//----- IDB Setup for cross-session Caching -----//
function openDB() {
return new Promise((resolve, reject) => {
const req = indexedDB.open("article-cache", 1);
req.onupgradeneeded = () => {
req.result.createObjectStore("files");
};
req.onsuccess = () => resolve(req.result);
req.onerror = () => reject(req.error);
});
}
async function saveCacheEntry(key, value) {
const db = await openDB();
const tx = db.transaction("files", "readwrite");
tx.objectStore("files").put(value, key);
}
async function loadCache() {
const db = await openDB();
const tx = db.transaction("files", "readonly");
const store = tx.objectStore("files");
return new Promise(resolve => {
const req = store.getAllKeys();
req.onsuccess = async () => {
for (const key of req.result) {
const val = await new Promise(r => {
const g = store.get(key);
g.onsuccess = () => r(g.result);
});
cache.set(key, val);
}
resolve();
};
});
}