-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
226 lines (219 loc) · 7.87 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
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
require("dotenv").config();
const fs = require("fs");
const fetch = require("node-fetch");
const express = require("express");
const cheerio = require("cheerio");
const minify = require("html-minifier").minify;
const CleanCSS = require("clean-css");
const Jimp = require("jimp");
const { URL } = require("url");
const app = express();
const ip = process.env.IP;
const port = process.env.PORT || 3000;
const stripCSS = process.env.NO_CSS;
const stripJs = process.env.NO_JS;
const minifyImages = Boolean(process.env.RESIZE_TO);
let friendlies = [];
try {
const input = fs.readFileSync(process.env.ALLOWLIST, { encoding: "utf-8" });
friendlies = input.trim().split("\n");
console.log("allow-list", friendlies);
} catch (error) {
console.error('Failed to open "allowed.txt"! See allowed.txt.example for a starting point.');
friendlies = [];
}
const maxSrcWidth = process.env.RESIZE_TO;
const maxInlineWidth = process.env.SCALE_TO;
const cssMinifyOptions = {
compatibility: {
colors: {
opacity: false, // controls `rgba()` / `hsla()` color support
},
properties: {
backgroundClipMerging: false, // controls background-clip merging into shorthand
backgroundOriginMerging: false, // controls background-origin merging into shorthand
backgroundSizeMerging: false, // controls background-size merging into shorthand
colors: true, // controls color optimizations
ieBangHack: true, // controls keeping IE bang hack
ieFilters: true, // controls keeping IE `filter` / `-ms-filter`
iePrefixHack: true, // controls keeping IE prefix hack
ieSuffixHack: true, // controls keeping IE suffix hack
merging: false, // controls property merging based on understandability
shorterLengthUnits: false, // controls shortening pixel units into `pc`, `pt`, or `in` units
spaceAfterClosingBrace: true, // controls keeping space after closing brace - `url() no-repeat` into `url()no-repeat`
urlQuotes: true, // controls keeping quoting inside `url()`
zeroUnits: true, // controls removal of units `0` value
},
selectors: {
adjacentSpace: false, // controls extra space before `nav` element
ie7Hack: false, // controls removal of IE7 selector hacks, e.g. `*+html...`
mergeLimit: 8191, // controls maximum number of selectors in a single rule (since 4.1.0)
multiplePseudoMerging: true, // controls merging of rules with multiple pseudo classes / elements (since 4.1.0)
},
units: {
ch: false, // controls treating `ch` as a supported unit
in: false, // controls treating `in` as a supported unit
pc: false, // controls treating `pc` as a supported unit
pt: false, // controls treating `pt` as a supported unit
rem: false, // controls treating `rem` as a supported unit
vh: false, // controls treating `vh` as a supported unit
vm: false, // controls treating `vm` as a supported unit
vmax: false, // controls treating `vmax` as a supported unit
vmin: false, // controls treating `vmin` as a supported unit
},
},
};
const minifyOptions = {
collapseBooleanAttributes: true,
collapseWhitespace: true,
processConditionalComments: true,
removeComments: true,
minifyCSS: stripCSS ? false : cssMinifyOptions,
continueOnParseError: true
};
app.get("*", async (req, res, next) => {
const friendly = friendlies.some((f) => req.hostname.endsWith(f));
const url = req.originalUrl;
if (friendly) {
console.log("friendly site:", url);
}
try {
const upstream = await fetch(url);
const contentType = upstream.headers.get("content-type");
//console.log(contentType);
if (contentType.startsWith("text/html")) {
const imageSizes = {};
const text = (await upstream.text()).replace(/https:\/\//g, "http://");
const $ = cheerio.load(text);
if (!friendly && stripJs) {
$("script").remove();
$("noscript").after(function (index) {
$(this).contents();
});
$("noscript").remove();
}
if (!friendly && stripCSS) {
$("style").remove();
$("link").remove();
$("*").removeAttr("class");
$("*").removeAttr("style");
}
if (!friendly) {
const imgs = [];
$("img").each(function () {
const src = new URL($(this).attr("src"), url).href;
//remove SVGs for now
if (src.toLowerCase().endsWith(".svg")) {
$(this).remove();
} else {
imgs.push(this);
}
});
if (maxInlineWidth) {
//set image tag sizes
for (let img of imgs) {
const src = new URL($(img).attr("src"), url).href;
const attrWidth = $(img).attr("width");
const attrHeight = $(img).attr("height");
if (!attrWidth) {
try {
if (!imageSizes[src]) {
const image = await Jimp.read(src);
imageSizes[src] = {
width: image.bitmap.width,
height: image.bitmap.height,
};
}
const width = Math.min(maxInlineWidth, imageSizes[src].width);
const height =
(imageSizes[src].height * width) / imageSizes[src].width;
$(this).attr("width", width);
$(this).attr("height", height);
} catch (error) {
console.error("Unable to resize image: "+error);
}
} else {
const width = Math.min(maxInlineWidth, attrWidth);
const height = (attrHeight * width) / attrWidth;
$(this).attr("width", width);
$(this).attr("height", height);
}
}
}
}
//fix root-relative URLs for Netscape
$("[href^='/']").each(function(index,element) {
const href = $(element).attr('href');
$(this).attr('href',new URL(url).origin+href);
});
res.set("Content-Type", "text/html");
res.status(upstream.status);
if (!friendly) {
const minified = minify(
$.root()
.html()
.replace(/'/g, "'"),
minifyOptions
);
console.log("html minified", contentType, url);
res.send(minified);
} else {
res.send(
$.root()
.html()
.replace(/'/g, "'")
);
}
} else if (contentType.startsWith("text/css")) {
const text = await upstream.text();
res.set("Content-Type", "text/css");
res.status(upstream.status);
res.send(new CleanCSS(cssMinifyOptions).minify(text).styles);
console.log("css minified", contentType, url);
} else if (
!friendly &&
minifyImages &&
contentType.startsWith("image/") &&
!contentType.includes("xml")
) {
const buffer = await upstream.buffer();
const image = await Jimp.read(buffer);
image.resize(Math.min(maxSrcWidth, image.bitmap.width), Jimp.AUTO);
image.quality(50);
const output = await image.getBufferAsync("image/jpeg");
res.set("Content-Type", "image/jpeg");
res.status(upstream.status);
res.send(output);
console.log("image minified", contentType, url);
} else {
res.set("Content-Type", contentType);
res.status(upstream.status);
res.send(await upstream.buffer());
}
} catch (error) {
console.error(error);
res.set("Content-Type", "text/html");
res.status(502);
res.send(
`<html>
<head>
<title>502 - Bad Gateway</title>
</head>
<body>
<h1>502 - Bad Gateway</h1>
<p>An error occurred while retrieving the page. Please check the server log for details.
</body>
</html>`
);
}
});
if (ip != "") {
app.listen(port, ip);
} else {
app.listen(port);
}
console.log(
`Listening on port ${port}, CSS is ${
stripCSS ? "disabled" : "enabled"
}, images are ${minifyImages ? "compressed" : "original quality"}`
);