@reldens/server-utils 0.10.0 → 0.12.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 +21 -7
- package/lib/uploader-factory.js +38 -8
- package/package.json +1 -1
|
@@ -47,7 +47,13 @@ class AppServerFactory
|
|
|
47
47
|
this.corsOrigin = String(process.env.RELDENS_CORS_ORIGIN || '*');
|
|
48
48
|
this.corsMethods = String(process.env.RELDENS_CORS_METHODS || 'GET,POST').split(',');
|
|
49
49
|
this.corsHeaders = String(process.env.RELDENS_CORS_HEADERS || 'Content-Type,Authorization').split(',');
|
|
50
|
-
this.
|
|
50
|
+
this.tooManyRequestsMessage = String(
|
|
51
|
+
process.env.RELDENS_TOO_MANY_REQUESTS_MESSAGE || 'Too many requests, please try again later.'
|
|
52
|
+
);
|
|
53
|
+
this.error = {};
|
|
54
|
+
this.processErrorResponse = function(status, message, req, res) {
|
|
55
|
+
return { status, message, handled: false };
|
|
56
|
+
};
|
|
51
57
|
}
|
|
52
58
|
|
|
53
59
|
createAppServer(appServerConfig)
|
|
@@ -72,7 +78,7 @@ class AppServerFactory
|
|
|
72
78
|
max: this.maxRequests,
|
|
73
79
|
standardHeaders: true,
|
|
74
80
|
legacyHeaders: false,
|
|
75
|
-
message:
|
|
81
|
+
message: this.tooManyRequestsMessage
|
|
76
82
|
};
|
|
77
83
|
if(this.applyKeyGenerator){
|
|
78
84
|
limiterParams.keyGenerator = function(req){
|
|
@@ -168,13 +174,21 @@ class AppServerFactory
|
|
|
168
174
|
app.get('/', async (req, res, next) => {
|
|
169
175
|
if('/' === req._parsedUrl.pathname){
|
|
170
176
|
if('function' !== typeof homePageLoadCallback){
|
|
171
|
-
|
|
177
|
+
let result = this.processErrorResponse(500, 'Homepage contents could not be loaded.', req, res);
|
|
178
|
+
if(result.handled){
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
return res.status(result.status).send(result.message);
|
|
172
182
|
}
|
|
173
183
|
try {
|
|
174
184
|
return res.send(await homePageLoadCallback(req));
|
|
175
185
|
} catch(error){
|
|
176
|
-
this.
|
|
177
|
-
|
|
186
|
+
this.error = {message: 'Error loading homepage.', error};
|
|
187
|
+
let result = this.processErrorResponse(500, 'Error loading homepage.', req, res);
|
|
188
|
+
if(result.handled){
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
return res.status(result.status).send(result.message);
|
|
178
192
|
}
|
|
179
193
|
}
|
|
180
194
|
next();
|
|
@@ -184,7 +198,7 @@ class AppServerFactory
|
|
|
184
198
|
async serveStatics(app, statics)
|
|
185
199
|
{
|
|
186
200
|
if(!FileHandler.isValidPath(statics)){
|
|
187
|
-
this.
|
|
201
|
+
this.error = {message: 'Invalid statics path.'};
|
|
188
202
|
return;
|
|
189
203
|
}
|
|
190
204
|
let staticOptions = {
|
|
@@ -202,7 +216,7 @@ class AppServerFactory
|
|
|
202
216
|
async serveStaticsPath(app, staticsPath, statics)
|
|
203
217
|
{
|
|
204
218
|
if(!FileHandler.isValidPath(staticsPath) || !FileHandler.isValidPath(statics)){
|
|
205
|
-
this.
|
|
219
|
+
this.error = {message: 'Invalid statics path to be served.'};
|
|
206
220
|
return;
|
|
207
221
|
}
|
|
208
222
|
let staticOptions = {
|
package/lib/uploader-factory.js
CHANGED
|
@@ -18,6 +18,9 @@ class UploaderFactory
|
|
|
18
18
|
this.fileLimit = props.fileLimit || 0;
|
|
19
19
|
this.allowedExtensions = props.allowedExtensions;
|
|
20
20
|
this.applySecureFileNames = props.applySecureFileNames;
|
|
21
|
+
this.processErrorResponse = props.processErrorResponse || function(status, message, req, res) {
|
|
22
|
+
return { status, message, handled: false };
|
|
23
|
+
};
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
createUploader(fields, buckets, allowedFileTypes)
|
|
@@ -66,14 +69,30 @@ class UploaderFactory
|
|
|
66
69
|
if(err){
|
|
67
70
|
if(err instanceof multer.MulterError){
|
|
68
71
|
if(err.code === 'LIMIT_FILE_SIZE'){
|
|
69
|
-
|
|
72
|
+
let result = this.processErrorResponse(413, 'File too large.', req, res);
|
|
73
|
+
if(result.handled){
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
return res.status(result.status).send(result.message);
|
|
70
77
|
}
|
|
71
78
|
if(err.code === 'LIMIT_FILE_COUNT'){
|
|
72
|
-
|
|
79
|
+
let result = this.processErrorResponse(413, 'Too many files.', req, res);
|
|
80
|
+
if(result.handled){
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
return res.status(result.status).send(result.message);
|
|
84
|
+
}
|
|
85
|
+
let result = this.processErrorResponse(400, 'File upload error: ' + err.message, req, res);
|
|
86
|
+
if(result.handled){
|
|
87
|
+
return;
|
|
73
88
|
}
|
|
74
|
-
return res.status(
|
|
89
|
+
return res.status(result.status).send(result.message);
|
|
75
90
|
}
|
|
76
|
-
|
|
91
|
+
let result = this.processErrorResponse(500, 'Server error during file upload.', req, res);
|
|
92
|
+
if(result.handled){
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
return res.status(result.status).send(result.message);
|
|
77
96
|
}
|
|
78
97
|
if(!req.files){
|
|
79
98
|
return next();
|
|
@@ -85,7 +104,14 @@ class UploaderFactory
|
|
|
85
104
|
if(FileHandler.exists(file.path)){
|
|
86
105
|
FileHandler.remove(file.path);
|
|
87
106
|
}
|
|
88
|
-
|
|
107
|
+
let result = this.processErrorResponse(
|
|
108
|
+
415,
|
|
109
|
+
'File contents do not match declared type.', req, res
|
|
110
|
+
);
|
|
111
|
+
if(result.handled){
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
return res.status(result.status).send(result.message);
|
|
89
115
|
}
|
|
90
116
|
}
|
|
91
117
|
}
|
|
@@ -93,7 +119,11 @@ class UploaderFactory
|
|
|
93
119
|
} catch(error){
|
|
94
120
|
this.error = {message: 'File validation error.', error};
|
|
95
121
|
this.cleanupFiles(req.files);
|
|
96
|
-
|
|
122
|
+
let result = this.processErrorResponse(500, 'Error processing uploaded files.', req, res);
|
|
123
|
+
if(result.handled){
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
return res.status(result.status).send(result.message);
|
|
97
127
|
}
|
|
98
128
|
});
|
|
99
129
|
};
|
|
@@ -118,11 +148,11 @@ class UploaderFactory
|
|
|
118
148
|
this.error = {message: 'Field name is invalid'};
|
|
119
149
|
return false;
|
|
120
150
|
}
|
|
121
|
-
if(!
|
|
151
|
+
if(!buckets[field.name]){
|
|
122
152
|
this.error = {message: `Missing bucket for field: ${field.name}`};
|
|
123
153
|
return false;
|
|
124
154
|
}
|
|
125
|
-
if(!
|
|
155
|
+
if(!allowedFileTypes[field.name]){
|
|
126
156
|
this.error = {message: `Missing allowedFileType for field: ${field.name}`};
|
|
127
157
|
return false;
|
|
128
158
|
}
|