-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
144 lines (120 loc) · 4.59 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
const express = require("express");
const compression = require("compression");
const helmet = require("helmet");
const fs = require("fs");
let isProduction = process.env.NODE_ENV === "production";
let PORT = isProduction ? "/tmp/nginx.socket" : 8080;
let linkBase = isProduction
? "https://unblocker.vercel.app"
: `http://127.0.0.1:${PORT}`;
let callbackFn = () => {
if (isProduction) {
fs.closeSync(fs.openSync("/tmp/app-initialized", "w"));
}
console.log(`Listening on ${PORT}`);
};
const RENDER_CACHE = require("./modules/cacheEngine")(isProduction);
const render = require("./modules/render");
const utils = require("./modules/utils");
const app = express();
app.enable("trust proxy", 1);
app.use(helmet());
app.use(compression());
app.get("/", async (req, res) => {
try {
let targetUrl = new URL(
Buffer.from(req.query.url, "base64").toString("ascii")
);
let shouldDisplay = /^true$/.test(req.query.display);
let shouldForceRender = /^true$/.test(req.query.force);
let keyExists = await RENDER_CACHE.hasKey(targetUrl.href);
if (keyExists) {
if (shouldForceRender) {
RENDER_CACHE.deleteKey(targetUrl.href);
}
else {
let { entryStillValid, entry } = await new Promise(
async resolve => {
let entry = await RENDER_CACHE.getKey(targetUrl.href);
let { isOk } = await utils.checkAvailability(entry);
if (!isOk) {
RENDER_CACHE.deleteKey(targetUrl.href);
return resolve({ entryStillValid: false, entry: null });
}
return resolve({ entryStillValid: true, entry: entry });
}
);
if (entryStillValid) {
if (!shouldDisplay) {
return res.status(200).json({ success: true, pdfLocation: entry, fromCache: true });
}
res.status(200).set(
{
"Content-Type": "application/pdf",
"Content-Disposition": 'inline; filename="render.pdf"'
}
);
return utils.streamFile(
{
writableStream: res,
file: entry
}
);
}
}
}
let { isOk, headers } = await utils.checkAvailability(targetUrl.href);
if (!isOk) {
return res.status(400).json({ success: false, reason: "Non200StatusCode" });
}
let contentTypeHeaderExists = headers.hasOwnProperty("content-type");
if (!contentTypeHeaderExists) {
return res.status(400).json({ success: false, reason: "NoValidHeaders" });
}
let contentType = headers["content-type"];
if (!contentType.includes("text/html")) {
return res.status(400).json({ success: false, reason: "NothingToRender" });
}
let pdfDestination = await render(
{
url: targetUrl.href,
linkBase: linkBase
}
);
if (!pdfDestination) {
return res.status(500).json({ success: false, reason: "PageLoadFailed" });
}
let uploadResult = await utils.uploadFile(pdfDestination);
if (!uploadResult || !uploadResult.success) {
if (!shouldDisplay) {
return res.status(503).json({ success: false, reason: "UploadFailed" });
}
}
else {
RENDER_CACHE.setKey(targetUrl.href, uploadResult.url, uploadResult.ttl);
if (!shouldDisplay) {
return res.status(200).json({ success: true, pdfLocation: uploadResult.url, fromCache: false });
}
}
res.status(200).set(
{
"Content-Type": "application/pdf",
"Content-Disposition": 'inline; filename="render.pdf"'
}
);
return utils.streamFile(
{
writableStream: res,
file: fs.createReadStream(pdfDestination)
}
);
}
catch (e) {
if (e instanceof TypeError) {
return res.status(400).json({ success: false, reason: "InvalidURL" });
}
console.log(e);
return res.status(500).json({ success: false, reason: "EpicFail" });
}
});
app.listen(PORT, callbackFn);