-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadability_httpserver.js
More file actions
101 lines (90 loc) · 3.09 KB
/
readability_httpserver.js
File metadata and controls
101 lines (90 loc) · 3.09 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
import { parse } from "https://deno.land/std@0.83.0/flags/mod.ts";
import { readFileSync, writeFile } from "node:fs";
import { join } from "https://deno.land/std@0.212.0/path/join.ts";
import { DOMParser } from "https://deno.land/x/deno_dom/deno-dom-wasm.ts"
import { Readability } from "./readability";
const { homedir, listenon, port } = parse(Deno.args);
const nport = parseInt(port);
if (typeof nport !== 'number') {
throw new Error("we need port to represent a valid number");
}
Deno.serve({ port: nport, hostname: listenon }, (_req, _info) => {
const pathname = new URL(_req.url).pathname;
if (containsDotDot(pathname)) {
return new Response("403: double dots in pathname are forbidden to enhance security", {
status: 403,
});
}
const filelocation = join(homedir, pathname);
let source;
try {
source = readFile(filelocation);
} catch (e) {
return new Response("404: file not found: " + e, {
status: 404,
});
}
let rd = Readability(sourceToDoc(source)).parse();
let content = rd.content.replace(`<div id="readability-page-1" class="page"><div>`, `<div id="readability-page-1" class="page"><div>\n<h1>` + rd.title + `</h1>`);
let p = new Promise((resolve, reject) => {
let done = 0;
let sent = false;
const cb = (err) => {
if (sent) {
return;
}
if (err) {
sent = true;
resolve(new Response("501: " + err, {
status: 501,
}));
return;
}
done++;
if (done === 2) {
sent = true;
resolve(new Response({ success: true }, {
status: 200,
headers: { "Content-Type": "application/json" },
}));
return;
}
};
writeFile(filelocation + ".readable.html", content, cb);
writeFile(filelocation + ".readable.txt", rd.title + "\n" + rd.textContent, cb);
});
return p;
});
// Check for .. in the path and respond with an error if it is present
// otherwise users could access any file on the server
function containsDotDot(v) {
if (!v.includes("..")) {
return false;
}
const fields = v.split(/[/\\]/);
for (let i = 0; i < fields.length; i++) {
if (fields[i] === "..") {
return true;
}
}
return false;
}
function readFile(filePath) {
return readFileSync(filePath, { encoding: "utf-8" }).trim();
}
// source is a string of all the html
function sourceToDoc(source) {
var doc = new DOMParser().parseFromString(source, "text/html");
removeCommentNodesRecursively(doc);
return doc;
}
function removeCommentNodesRecursively(node) {
for (var i = node.childNodes.length - 1; i >= 0; i--) {
var child = node.childNodes[i];
if (child.nodeType === child.COMMENT_NODE) {
node.removeChild(child);
} else if (child.nodeType === child.ELEMENT_NODE) {
removeCommentNodesRecursively(child);
}
}
}