-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
286 lines (258 loc) · 7.5 KB
/
server.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
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
// DeXo CDN
require("dotenv").config();
const fs = require("fs");
const path = require("path");
const url = require("url");
const express = require("express");
const cache = require("memory-cache");
const compression = require("compression");
const UglifyJS = require("uglify-js");
const sharp = require("sharp");
const cors = require("cors");
const helmet = require("helmet");
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const app = express();
const port = process.env.PORT || 3000;
const useMongoDB = process.env.USE_MOGO_DB || false;
// Mongoose connection
if (useMongoDB === "true") {
mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => {
console.log("Connected to MongoDB");
}).catch((err) => {
console.log("Error connecting to MongoDB");
console.log(err);
return process.exit(1);
});
}
const ImageSchema = new Schema({
url: String,
time: Number,
filename: String,
loaded: String,
expires: Number,
userip: String
});
const JsSchema = new Schema({
url: String,
time: Number,
filename: String,
loaded: String,
expires: Number,
userip: String
});
// Middleware
app.use(cors());
app.use(helmet({
crossOriginResourcePolicy: false,
}));
app.use(compression());
// Start server
app.use((req, res, next) => {
res.removeHeader("Connection");
res.removeHeader("Content-Length");
res.removeHeader("Transfer-Encoding");
res.removeHeader("X-Powered-By");
res.setHeader("Cache-Control", "public, max-age=31536000");
res.setHeader("Expires", new Date(Date.now() + 31536000000).toUTCString());
res.setHeader("X-Content-Type-Options", "nosniff");
res.setHeader("X-Frame-Options", "SAMEORIGIN");
res.setHeader("X-XSS-Protection", "1; mode=block");
res.setHeader("Referrer-Policy", "no-referrer-when-downgrade");
res.setHeader("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload");
if (req.url.includes("/js/")) {
res.setHeader("Content-Type", "application/javascript");
}
next();
});
// Cache middleware
const cacheMiddleware = (duration) => {
return (req, res, next) => {
const key = "__express__" + req.originalUrl || req.url;
const cachedBody = cache.get(key);
if (cachedBody) {
res.send(cachedBody);
return;
}
res.sendResponse = res.send;
res.send = (body) => {
cache.put(key, body, duration * 1000);
res.sendResponse(body);
};
next();
};
};
// Unit converter
function units(bytes) {
const units = ['ms', 's', 'm', 'h', 'd', 'y'];
let unit = 0;
while (bytes > 1024) {
bytes /= 1024;
unit++;
}
return `${Math.round(bytes)}${units[unit]}`;
}
// Image route - optimize image
app.get("/images/:filename", cacheMiddleware(86400), (req, res) => {
const starttime = new Date().getTime();
const file = url.parse(req.url).pathname;
const filePath = path.resolve("./public" + file);
const cachedFile = cache.get(req.url);
optimizeImageMiddleware(req, res, filePath, cachedFile, starttime);
if (!fs.existsSync(filePath)) {
return res.status(404).send({ error: "Not found" });
}
if (cachedFile) {
return res.sendFile(cachedFile);
}
if (useMongoDB === "true") {
const endtime = new Date().getTime();
const Image = mongoose.model("Image", ImageSchema);
const loadtime = endtime - starttime;
const userip = getRealIp(req);
const image = new Image({
url: req.url,
time: starttime,
filename: path.basename(filePath),
loaded: units(loadtime),
expires: endtime + 31536000000,
userip: userip
});
image.save();
}
const image = sharp(filePath);
image.metadata().then((metadata) => {
if (metadata.format === "jpeg") {
image
.jpeg({
quality: 85,
chromaSubsampling: "4:4:4",
force: false,
})
.toBuffer()
.then((data) => {
cache.put(req.url, data, 86400000);
return res.send(data);
})
.catch((err) => {
console.log(err);
return res.status(500).send({ error: "Internal server error" });
});
} else if (metadata.format === "png") {
image
.png({
quality: 85,
force: false,
})
.toBuffer()
.then((data) => {
cache.put(req.url, data, 86400000);
return res.send(data);
})
.catch((err) => {
console.log(err);
return res.status(500).send({ error: "Internal server error" });
});
} else if (metadata.format === "webp") {
image
.webp({
quality: 85,
force: false,
})
.toBuffer()
.then((data) => {
cache.put(req.url, data, 86400000);
return res.send(data);
})
.catch((err) => {
console.log(err);
return res.status(500).send({ error: "Internal server error" });
});
} else {
return res.status(500).send({ error: "Internal server error" });
}
});
image.on("error", (err) => {
console.log(err);
return res.status(500).send({ error: "Internal server error" });
});
image.on("info", (info) => {
console.log(info);
});
image.on("warn", (err) => {
console.log(err);
});
image.on("complete", (info) => {
return res.send(info);
});
});
// Js route - minify js
app.get("/js/:file", cacheMiddleware(86400), async (req, res) => {
res.setHeader("Content-Type", "application/javascript");
const starttime = new Date().getTime();
const file = url.parse(req.url).pathname;
const filePath = path.resolve("./public" + file);
const readFile = fs.readFileSync(filePath, "utf8");
if (!fs.existsSync(filePath)) {
return res.status(404).send("Cannot GET " + file);
}
const minifiedBody = UglifyJS.minify(readFile);
if (useMongoDB === "true") {
const endtime = new Date().getTime();
const Js = mongoose.model("Js", JsSchema);
const loadtime = endtime - starttime;
const userip = getRealIp(req);
const js = new Js({
url: req.url,
time: starttime,
filename: path.basename(filePath),
loaded: units(loadtime),
expires: endtime + 31536000000,
userip: userip
});
js.save();
}
return res.send(minifiedBody.code);
});
// Status - for monitoring
app.get("/status", async (req, res) => {
if (useMongoDB === "true") {
const Image = mongoose.model("Image", ImageSchema);
const Js = mongoose.model("Js", JsSchema);
const images = await Image.find({}).sort({ time: -1 }).lean();
const js = await Js.find({}).sort({ time: -1 }).lean();
res.send({ images, js });
} else {
res.send({ error: "MongoDB not enabled" });
}
});
// Optimize image middleware
const optimizeImageMiddleware = () => {
return (req, res, next) => {
const imagePath = path.join(__dirname, req.url);
sharp(imagePath).toBuffer((err, buffer) => {
if (err) return next();
res.setHeader("Content-Type", "image/jpeg");
res.send(buffer);
});
};
};
// Start server - if u try to start this and not work, u need to use a proxy like nginx
app.listen(port, () => {
console.log(`Loading config from .env file`);
setTimeout(() => {
console.log(`Server listening on port ${process.env.PORT}`);
}, 1000);
});
// Get real ip
function getRealIp(req) {
return req.headers["x-forwarded-for"] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
}
// Error handler
process.on("uncaughtException", (err) => {
console.log(err);
process.exit(1);
});