@adaptivestone/framework 4.9.2 → 4.10.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/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
### 4.10.0
|
|
2
|
+
|
|
3
|
+
[UPDATE] deps update
|
|
4
|
+
[NEW] Static file middleware
|
|
5
|
+
[BREAKING] This is a potencial breaking change and we switched from express.static to internal middleware that provide less features but faster. From API nothing was changed
|
|
6
|
+
|
|
1
7
|
### 4.9.2
|
|
2
8
|
|
|
3
9
|
[UPDATE] deps update
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adaptivestone/framework",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.10.0",
|
|
4
4
|
"description": "Adaptive stone node js framework",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"engines": {
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"i18next-chained-backend": "^4.0.0",
|
|
39
39
|
"i18next-fs-backend": "^2.0.0",
|
|
40
40
|
"juice": "^9.0.0",
|
|
41
|
+
"mime": "^3.0.0",
|
|
41
42
|
"minimist": "^1.2.5",
|
|
42
43
|
"mongoose": "^7.0.0",
|
|
43
44
|
"nodemailer": "^6.6.3",
|
|
@@ -58,7 +59,7 @@
|
|
|
58
59
|
"eslint-plugin-prettier": "^5.0.0",
|
|
59
60
|
"eslint-plugin-vitest": "^0.3.1",
|
|
60
61
|
"husky": "^8.0.0",
|
|
61
|
-
"lint-staged": "^
|
|
62
|
+
"lint-staged": "^15.0.0",
|
|
62
63
|
"mongodb-memory-server": "^9.0.0",
|
|
63
64
|
"nodemon": "^3.0.1",
|
|
64
65
|
"prettier": "^3.0.0",
|
|
@@ -7,6 +7,7 @@ const RequestLoggerMiddleware = require('./middleware/RequestLogger');
|
|
|
7
7
|
const I18nMiddleware = require('./middleware/I18n');
|
|
8
8
|
const PrepareAppInfoMiddleware = require('./middleware/PrepareAppInfo');
|
|
9
9
|
const RequestParserMiddleware = require('./middleware/RequestParser');
|
|
10
|
+
const StaticFilesMiddleware = require('./middleware/StaticFiles');
|
|
10
11
|
|
|
11
12
|
const Base = require('../../modules/Base');
|
|
12
13
|
|
|
@@ -33,9 +34,16 @@ class HttpServer extends Base {
|
|
|
33
34
|
cors({
|
|
34
35
|
origin: httpConfig.corsDomains,
|
|
35
36
|
}),
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
this.express.use(
|
|
37
|
+
);
|
|
38
|
+
// todo whitelist
|
|
39
|
+
this.express.use(
|
|
40
|
+
new StaticFilesMiddleware(this.app, {
|
|
41
|
+
folders: [
|
|
42
|
+
this.app.foldersConfig.public,
|
|
43
|
+
path.join(__dirname, '../../public/files'),
|
|
44
|
+
],
|
|
45
|
+
}).getMiddleware(),
|
|
46
|
+
);
|
|
39
47
|
|
|
40
48
|
this.express.use(new RequestParserMiddleware(this.app).getMiddleware());
|
|
41
49
|
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const fsPromises = require('node:fs/promises');
|
|
2
|
+
const fs = require('node:fs');
|
|
3
|
+
const path = require('node:path');
|
|
4
|
+
const mime = require('mime');
|
|
5
|
+
|
|
6
|
+
const AbstractMiddleware = require('./AbstractMiddleware');
|
|
7
|
+
/**
|
|
8
|
+
* Middleware for static files
|
|
9
|
+
*/
|
|
10
|
+
class StaticFiles extends AbstractMiddleware {
|
|
11
|
+
constructor(app, params) {
|
|
12
|
+
super(app);
|
|
13
|
+
this.params = params;
|
|
14
|
+
if (!params || !params.folders || !params.folders.length) {
|
|
15
|
+
throw new Error('StaticFiles inited without folders config');
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static get description() {
|
|
20
|
+
return 'Static file server middleware. Host you static files from public foolder. Mostly for dev.';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async middleware(req, res, next) {
|
|
24
|
+
if (req.method !== 'GET') {
|
|
25
|
+
// only get supported
|
|
26
|
+
return next();
|
|
27
|
+
}
|
|
28
|
+
const { folders } = this.params;
|
|
29
|
+
|
|
30
|
+
const promises = [];
|
|
31
|
+
|
|
32
|
+
for (const f of folders) {
|
|
33
|
+
const filePath = path.join(f, req.url);
|
|
34
|
+
promises.push(
|
|
35
|
+
fsPromises
|
|
36
|
+
.stat(filePath)
|
|
37
|
+
.catch(() => {
|
|
38
|
+
// nothing there, file just not exists
|
|
39
|
+
})
|
|
40
|
+
.then((stats) => ({ stats, file: filePath })),
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const fileStats = await Promise.all(promises);
|
|
45
|
+
|
|
46
|
+
for (const fileStat of fileStats) {
|
|
47
|
+
if (fileStat.stats && fileStat.stats.isFile()) {
|
|
48
|
+
const contentType = mime.getType(fileStat.file);
|
|
49
|
+
const fileStream = fs.createReadStream(fileStat.file);
|
|
50
|
+
res.set('Content-Type', contentType);
|
|
51
|
+
fileStream.pipe(res);
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return next();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
module.exports = StaticFiles;
|