-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
329 lines (282 loc) · 9.35 KB
/
index.ts
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import express from "express";
import { Readability, isProbablyReaderable } from "@mozilla/readability";
import path from "path";
import { fileURLToPath } from "url";
import "dotenv/config";
import { parseHTML, parseJSON } from "linkedom";
import XHR2 from "xhr2";
const XMLHttpRequest = XHR2.XMLHttpRequest;
import { minify } from "html-minifier";
import {
blazeFunctionality,
blazeUrl,
highlightBlazedLinks,
injectBlazeToPageLinks,
} from "./utils.js";
import etag from "etag";
import compression from "compression";
import fs from "fs";
const app = express();
const port = 8888;
const minifierOptions = {
collapseWhitespace: true,
removeComments: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeTagWhitespace: true,
useShortDoctype: true,
minifyCSS: true,
};
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export type SerpResponse = {
url: string;
title: string;
description: string;
meta_url: {
hostname: string;
};
};
// Middlewares
app.use(compression());
app.use((req, res, next) => {
res.set("Cache-Control", "public, max-age=60000");
res.set("Service-Worker-Allowed", "/");
next();
});
// Routes
app.get("/", async (req, res) => {
const searchEngine = "https://api.search.brave.com/res/v1/web/search";
const query = req.query.q as string;
if (!query) {
return res.sendFile(path.join(__dirname, "/index.html"));
}
const key = process.env.CYCLIC_BRAVE_KEY;
if (!key) {
throw new Error("No brave key found");
}
try {
const xhr = new XMLHttpRequest();
const formattedQuery = encodeURIComponent(query);
xhr.open(
"GET",
`${searchEngine}?q=${formattedQuery}&safesearch=moderate`,
true
);
xhr.setRequestHeader("Accept", "*/*");
xhr.setRequestHeader("X-Subscription-Token", key);
xhr.onreadystatechange = () => {
if (xhr.readyState !== 4) {
return;
}
if (xhr.status !== 200) {
console.error("XHR request failed:", xhr.status, xhr.statusText);
return;
}
const data = JSON.parse(xhr.responseText);
const results = data.web.results.map(
(result: SerpResponse) => `
<article>
<h2><a href="${blazeUrl}/blazed?url=${result.url}">
${result.title}
</a></h2>
<span>${result.meta_url.hostname}</span>
<p>${result.description}</p>
</article>
<hr />
`
);
const html = `
<html>
<head>
<meta charset="UTF-8">
<link rel="icon" type="image/x-icon" href="/favicon.svg" />
<link rel="stylesheet" href="/styles/serp.css" media="print" onload="this.media='all'">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Blaze - ${query}</title>
<style>
body {font-family:sans-serif}
h2 {margin-bottom:0}
span {font-size:.9rem}
</style>
</head>
<body>
<header>
<label>
<a href="/"><strong>BLAZE</strong></a>
<input type="search" value="${query}" />
<button>Blaze it</button>
</label>
</header>
<hr/>
${results.join("")}
<script>
${blazeFunctionality}
${highlightBlazedLinks}
blazeFunctionality('${blazeUrl}')
const links = document.querySelectorAll('a')
highlightBlazedLinks(links)
</script>
</body>
</html>
`;
try {
const minifiedSerp = minify(html, minifierOptions);
res.set("X-Blaze-Etag", etag(minifiedSerp));
res.send(minifiedSerp);
} catch (e) {
console.log("Error during html minifier:", e);
res.sendFile(path.join(__dirname, "/not_blazed.html"));
}
};
xhr.send();
} catch (err) {
console.error(err);
}
});
app.get("/blazed", async (req, res) => {
const pageToBlaze = req.query.url as string;
try {
const xhr = new XMLHttpRequest();
xhr.open("GET", pageToBlaze, true);
xhr.setRequestHeader("Accept", "text/html");
xhr.onreadystatechange = async () => {
if (xhr.readyState !== 4) {
return;
}
if (xhr.status === 404) {
res.sendFile(path.join(__dirname, "/404.html"));
return;
}
if (xhr.status !== 200) {
console.error("XHR request failed:", xhr.status, xhr.statusText);
res.send(
minify(
`
<html>
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/x-icon" href="/favicon.svg" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Blaze - error</title>
</head>
<body>
<div style="text-align: center; display: flex; align-items: center; flex-direction: column; justify-content: center; font-family: sans-serif; width: 100%; height: 100%">
<h1>Blaze could not load the page :(</h1>
<p>Reason: <code>${xhr.status} ${xhr.statusText}</code></p>
<br />
<br />
<p>
If you want (it would be great!) you can report this problem, writing the requested URL and the reason,
at <a href="mailto:[email protected]">[email protected]</a>
</p>
<br />
<a href="#" role="button" onclick="history.back()">Go back</a>
</div>
</body>
</html>
`,
minifierOptions
)
);
return;
}
const response = xhr.responseText;
const { document } = parseHTML(response);
if (!isProbablyReaderable(document)) {
// TODO: still a lot of bugs, must be refined to handle some cases, like
// cookie banners, etc.
document.querySelectorAll("link").forEach((l) => {
l.remove();
});
document.querySelectorAll("style").forEach((s) => {
s.remove();
});
document.querySelectorAll("script").forEach((s) => {
s.remove();
});
document.querySelectorAll("img").forEach((i) => {
i.remove();
});
document.querySelectorAll("iframe").forEach((f) => {
f.remove();
});
const blazeDisclaimer = document.createElement("div");
blazeDisclaimer.style.width = "100dvw";
blazeDisclaimer.style.border = "1px solid red";
blazeDisclaimer.style.padding = "1rem";
blazeDisclaimer.style.textAlign = "center";
blazeDisclaimer.innerHTML = `
<h2>BLAZE INFO</h2>
<p>
The page you are seeing <strong>could not be correctly blazed</strong> due to these webpage characteristics.
<strong>Blaze served anyway</strong> a lightweight version of the page.
Keep in mind that this kind of pages <strong>can be hard or even impossible to use, read or understand</strong>.
</p>
`;
const referenceElement = document.body.firstChild;
document.body.insertBefore(blazeDisclaimer, referenceElement);
const blazedPage = minify(document.toString(), minifierOptions);
return res.send(blazedPage);
}
//TODO: find if there are more performant ways to remove images or evaluate if is the case to remove images
document.querySelectorAll("img").forEach((img) => img.remove());
const reader = new Readability(document);
const article = reader.parse();
if (!article) {
return res.send("Something went wrong");
}
const blazedPage = `<html><head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<style>body {font-family: sans-serif}</style>
</head>
<body>
${article.content}
<script>
${injectBlazeToPageLinks}
const url = "${blazeUrl}"
const currentUrl = "${req.query.url}"
injectBlazeToPageLinks(url, currentUrl)
${highlightBlazedLinks}
const links = document.querySelectorAll('a')
highlightBlazedLinks(links)
</script>
</body></html>
`;
const minifiedBlazedPage = minify(blazedPage, minifierOptions);
res.send(minifiedBlazedPage);
};
xhr.send();
} catch (err) {
console.log(err);
}
});
app.get("/info", (_, res) => {
let Etag;
fs.readFile(path.join(__dirname + "/info.html"), "utf8", (err, data) => {
if (err) {
console.error(err);
return;
}
Etag = etag(data);
res.set("X-Blaze-Etag", Etag);
res.sendFile(path.join(__dirname + "/info.html"));
});
});
app.get("/ooops", (_, res) => {
res.sendFile(path.join(__dirname + "/info_not_blazed.html"));
});
app.get("/favicon.svg", (_, res) => {
res.sendFile(path.join(__dirname + "/favicon.svg"));
});
app.get("/service-worker.js", (_, res) => {
res.sendFile(path.join(__dirname + "/service-worker.js"));
});
app.get("/styles/serp.css", (_, res) => {
res.sendFile(path.join(__dirname + "/styles/serp.css"));
});
app.listen(port, () => {
console.log(`Got request`);
});