@reldens/server-utils 0.20.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/app-server-factory.js +22 -0
- package/lib/file-handler.js +27 -0
- package/package.json +2 -1
|
@@ -15,6 +15,7 @@ const https = require('https');
|
|
|
15
15
|
const express = require('express');
|
|
16
16
|
const bodyParser = require('body-parser');
|
|
17
17
|
const session = require('express-session');
|
|
18
|
+
const compression = require('compression');
|
|
18
19
|
|
|
19
20
|
class AppServerFactory
|
|
20
21
|
{
|
|
@@ -24,6 +25,7 @@ class AppServerFactory
|
|
|
24
25
|
this.applicationFramework = express;
|
|
25
26
|
this.bodyParser = bodyParser;
|
|
26
27
|
this.session = session;
|
|
28
|
+
this.compression = compression;
|
|
27
29
|
this.appServer = false;
|
|
28
30
|
this.app = express();
|
|
29
31
|
this.useCors = true;
|
|
@@ -88,6 +90,17 @@ class AppServerFactory
|
|
|
88
90
|
this.securityConfigurer = new SecurityConfigurer();
|
|
89
91
|
this.corsConfigurer = new CorsConfigurer();
|
|
90
92
|
this.rateLimitConfigurer = new RateLimitConfigurer();
|
|
93
|
+
this.useCompression = true;
|
|
94
|
+
this.compressionOptions = {
|
|
95
|
+
level: 6,
|
|
96
|
+
threshold: 1024,
|
|
97
|
+
filter: function(req, res){
|
|
98
|
+
if(req.headers['x-no-compression']){
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return compression.filter(req, res);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
91
104
|
}
|
|
92
105
|
|
|
93
106
|
createAppServer(appServerConfig)
|
|
@@ -99,6 +112,7 @@ class AppServerFactory
|
|
|
99
112
|
this.setupDevelopmentConfiguration();
|
|
100
113
|
this.setupProtocolEnforcement();
|
|
101
114
|
this.setupSecurity();
|
|
115
|
+
this.setupCompression();
|
|
102
116
|
this.setupVirtualHosts();
|
|
103
117
|
this.setupCors();
|
|
104
118
|
this.setupRateLimiting();
|
|
@@ -162,6 +176,14 @@ class AppServerFactory
|
|
|
162
176
|
});
|
|
163
177
|
}
|
|
164
178
|
|
|
179
|
+
setupCompression()
|
|
180
|
+
{
|
|
181
|
+
if(!this.useCompression){
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
this.app.use(this.compression(this.compressionOptions));
|
|
185
|
+
}
|
|
186
|
+
|
|
165
187
|
setupCors()
|
|
166
188
|
{
|
|
167
189
|
this.corsConfigurer.setup(this.app, {
|
package/lib/file-handler.js
CHANGED
|
@@ -79,6 +79,33 @@ class FileHandler
|
|
|
79
79
|
return randomStr + ext;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
getFileStats(filePath)
|
|
83
|
+
{
|
|
84
|
+
if(!this.isValidPath(filePath)){
|
|
85
|
+
this.error = {message: 'Invalid file path.', filePath};
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
if(!this.exists(filePath)){
|
|
89
|
+
this.error = {message: 'File does not exist.', filePath};
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
return fs.statSync(filePath);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
this.error = {message: 'Failed to get file stats.', error, filePath};
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
getFileModificationTime(filePath)
|
|
101
|
+
{
|
|
102
|
+
let stats = this.getFileStats(filePath);
|
|
103
|
+
if(!stats){
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
return stats.mtime;
|
|
107
|
+
}
|
|
108
|
+
|
|
82
109
|
remove(fullPath)
|
|
83
110
|
{
|
|
84
111
|
let deletePath = Array.isArray(fullPath) ? this.joinPaths(...fullPath) : fullPath;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reldens/server-utils",
|
|
3
3
|
"scope": "@reldens",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.22.0",
|
|
5
5
|
"description": "Reldens - Server Utils",
|
|
6
6
|
"author": "Damian A. Pastorini",
|
|
7
7
|
"license": "MIT",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"body-parser": "2.2.0",
|
|
39
|
+
"compression": "1.8.0",
|
|
39
40
|
"cors": "2.8.5",
|
|
40
41
|
"express": "4.21.2",
|
|
41
42
|
"express-rate-limit": "7.5.1",
|