-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
101 lines (84 loc) · 3.03 KB
/
index.js
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
"use strict";
import config from './config.js'
import Fuse from 'fuse.js'
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Parse the GET parameters and trigger the appropriate function.
*/
async function handleRequest(request) {
const url = new URL(request.url);
const query = url.searchParams.get("q");
if (url.pathname.endsWith("refresh")) {
if (isAuthenticated(request)) {
return refreshContent();
} else {
return new Response("403 Forbidden", { status: 403 });
}
} else if (url.pathname.endsWith("api")) {
return search(query);
} else if (url.searchParams.get("q")) {
let response = await fetch(url.href)
response = new Response(response.body, response)
response.headers.set("Link", "<" + config.baseURL + "/api?q=" + query + ">; rel='preload'; as='fetch'")
return response
} else {
return fetch(url.href)
}
}
/**
* Checks if the request is authenticated by looking for a "Authentication"
* header with the value specified as AUTH_KEY.
*/
function isAuthenticated(request) {
return request.headers.get("Authentication") === config.authKey;
}
/**
* Update the site content stored in Workers KV.
*/
async function refreshContent() {
const response = await fetch(config.contentURL + Date.now(), {
headers: { "Content-Type": "application/json;charset=UTF-8" }
});
if (!response.ok) {
console.error("Failed to get content JSON file. Status code was " + response.status);
return new Response("500 Internal Server Error", { status: 500 });
}
let json;
try {
json = await response.json();
} catch (error) {
console.error(error);
return new Response("500 Internal Server Error", { status: 500 });
}
let jsonString = JSON.stringify(json);
CONTENT.put("content", jsonString);
let index = Fuse.createIndex(config.fuseOptions.keys, json);
let indexString = JSON.stringify(index);
CONTENT.put("index", indexString);
return new Response("200 OK", { status: 200 });
}
/**
* Perform a search for the given query.
*/
async function search(query) {
if (!query) return new Response("400 Bad Request", { status: 400 });
let content = await CONTENT.get("content");
content = JSON.parse(content);
let index = await CONTENT.get("index")
index = JSON.parse(index)
index = Fuse.parseIndex(index)
const fuse = new Fuse(content, config.fuseOptions, index);
const results = fuse.search(query);
if (results.length > 0)
for (const result of results)
for (const key of config.keysToRemove)
delete result["item"][key]
return new Response(JSON.stringify(results), {
headers: {
"Content-Type": "application/json;charset=UTF-8",
"Cache-Control": "private, max-age=1800" // cache result in browser only, for 30 mins
}
})
}