@eggjs/multipart 4.0.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/LICENSE +21 -0
- package/README.md +444 -0
- package/dist/commonjs/app/extend/context.d.ts +110 -0
- package/dist/commonjs/app/extend/context.js +279 -0
- package/dist/commonjs/app/middleware/multipart.d.ts +4 -0
- package/dist/commonjs/app/middleware/multipart.js +20 -0
- package/dist/commonjs/app/schedule/clean_tmpdir.d.ts +3 -0
- package/dist/commonjs/app/schedule/clean_tmpdir.js +58 -0
- package/dist/commonjs/app.d.ts +6 -0
- package/dist/commonjs/app.js +21 -0
- package/dist/commonjs/config/config.default.d.ts +98 -0
- package/dist/commonjs/config/config.default.js +31 -0
- package/dist/commonjs/index.d.ts +2 -0
- package/dist/commonjs/index.js +5 -0
- package/dist/commonjs/lib/LimitError.d.ts +5 -0
- package/dist/commonjs/lib/LimitError.js +16 -0
- package/dist/commonjs/lib/MultipartFileTooLargeError.d.ts +6 -0
- package/dist/commonjs/lib/MultipartFileTooLargeError.js +18 -0
- package/dist/commonjs/lib/utils.d.ts +4 -0
- package/dist/commonjs/lib/utils.js +93 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/esm/app/extend/context.d.ts +110 -0
- package/dist/esm/app/extend/context.js +273 -0
- package/dist/esm/app/middleware/multipart.d.ts +4 -0
- package/dist/esm/app/middleware/multipart.js +18 -0
- package/dist/esm/app/schedule/clean_tmpdir.d.ts +3 -0
- package/dist/esm/app/schedule/clean_tmpdir.js +53 -0
- package/dist/esm/app.d.ts +6 -0
- package/dist/esm/app.js +18 -0
- package/dist/esm/config/config.default.d.ts +98 -0
- package/dist/esm/config/config.default.js +26 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/lib/LimitError.d.ts +5 -0
- package/dist/esm/lib/LimitError.js +12 -0
- package/dist/esm/lib/MultipartFileTooLargeError.d.ts +6 -0
- package/dist/esm/lib/MultipartFileTooLargeError.js +14 -0
- package/dist/esm/lib/utils.d.ts +4 -0
- package/dist/esm/lib/utils.js +85 -0
- package/dist/esm/package.json +3 -0
- package/dist/package.json +4 -0
- package/package.json +103 -0
- package/src/app/extend/context.ts +368 -0
- package/src/app/middleware/multipart.ts +20 -0
- package/src/app/schedule/clean_tmpdir.ts +55 -0
- package/src/app.ts +20 -0
- package/src/config/config.default.ts +130 -0
- package/src/index.ts +2 -0
- package/src/lib/LimitError.ts +12 -0
- package/src/lib/MultipartFileTooLargeError.ts +14 -0
- package/src/lib/utils.ts +92 -0
- package/src/typings/index.d.ts +4 -0
package/src/lib/utils.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import bytes from 'bytes';
|
|
4
|
+
import { MultipartConfig } from '../config/config.default.js';
|
|
5
|
+
|
|
6
|
+
export const whitelist = [
|
|
7
|
+
// images
|
|
8
|
+
'.jpg', '.jpeg', // image/jpeg
|
|
9
|
+
'.png', // image/png, image/x-png
|
|
10
|
+
'.gif', // image/gif
|
|
11
|
+
'.bmp', // image/bmp
|
|
12
|
+
'.wbmp', // image/vnd.wap.wbmp
|
|
13
|
+
'.webp',
|
|
14
|
+
'.tif',
|
|
15
|
+
'.psd',
|
|
16
|
+
// text
|
|
17
|
+
'.svg',
|
|
18
|
+
'.js', '.jsx',
|
|
19
|
+
'.json',
|
|
20
|
+
'.css', '.less',
|
|
21
|
+
'.html', '.htm',
|
|
22
|
+
'.xml',
|
|
23
|
+
// tar
|
|
24
|
+
'.zip',
|
|
25
|
+
'.gz', '.tgz', '.gzip',
|
|
26
|
+
// video
|
|
27
|
+
'.mp3',
|
|
28
|
+
'.mp4',
|
|
29
|
+
'.avi',
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
export function humanizeBytes(size: number | string) {
|
|
33
|
+
if (typeof size === 'number') {
|
|
34
|
+
return size;
|
|
35
|
+
}
|
|
36
|
+
return bytes(size) as number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function normalizeOptions(options: MultipartConfig) {
|
|
40
|
+
// make sure to cast the value of config **Size to number
|
|
41
|
+
options.fileSize = humanizeBytes(options.fileSize);
|
|
42
|
+
options.fieldSize = humanizeBytes(options.fieldSize);
|
|
43
|
+
options.fieldNameSize = humanizeBytes(options.fieldNameSize);
|
|
44
|
+
|
|
45
|
+
// validate mode
|
|
46
|
+
options.mode = options.mode || 'stream';
|
|
47
|
+
assert([ 'stream', 'file' ].includes(options.mode), `Expect mode to be 'stream' or 'file', but got '${options.mode}'`);
|
|
48
|
+
if (options.mode === 'file') {
|
|
49
|
+
assert(!options.fileModeMatch, '`fileModeMatch` options only work on stream mode, please remove it');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// normalize whitelist
|
|
53
|
+
if (Array.isArray(options.whitelist)) {
|
|
54
|
+
options.whitelist = options.whitelist.map(extname => extname.toLowerCase());
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// normalize fileExtensions
|
|
58
|
+
if (Array.isArray(options.fileExtensions)) {
|
|
59
|
+
options.fileExtensions = options.fileExtensions.map(extname => {
|
|
60
|
+
return (extname.startsWith('.') || extname === '') ? extname.toLowerCase() : `.${extname.toLowerCase()}`;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function checkExt(fileName: string) {
|
|
65
|
+
if (typeof options.whitelist === 'function') {
|
|
66
|
+
return options.whitelist(fileName);
|
|
67
|
+
}
|
|
68
|
+
const extname = path.extname(fileName).toLowerCase();
|
|
69
|
+
if (Array.isArray(options.whitelist)) {
|
|
70
|
+
return options.whitelist.includes(extname);
|
|
71
|
+
}
|
|
72
|
+
// only if user don't provide whitelist, we will use default whitelist + fileExtensions
|
|
73
|
+
return whitelist.includes(extname) || options.fileExtensions.includes(extname);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
options.checkFile = (_fieldName: string, fileStream: any, fileName: string): void | Error => {
|
|
77
|
+
// just ignore, if no file
|
|
78
|
+
if (!fileStream || !fileName) return;
|
|
79
|
+
try {
|
|
80
|
+
if (!checkExt(fileName)) {
|
|
81
|
+
const err = new Error('Invalid filename: ' + fileName);
|
|
82
|
+
Reflect.set(err, 'status', 400);
|
|
83
|
+
return err;
|
|
84
|
+
}
|
|
85
|
+
} catch (err: any) {
|
|
86
|
+
err.status = 400;
|
|
87
|
+
return err;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
return options;
|
|
92
|
+
}
|