@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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +444 -0
  3. package/dist/commonjs/app/extend/context.d.ts +110 -0
  4. package/dist/commonjs/app/extend/context.js +279 -0
  5. package/dist/commonjs/app/middleware/multipart.d.ts +4 -0
  6. package/dist/commonjs/app/middleware/multipart.js +20 -0
  7. package/dist/commonjs/app/schedule/clean_tmpdir.d.ts +3 -0
  8. package/dist/commonjs/app/schedule/clean_tmpdir.js +58 -0
  9. package/dist/commonjs/app.d.ts +6 -0
  10. package/dist/commonjs/app.js +21 -0
  11. package/dist/commonjs/config/config.default.d.ts +98 -0
  12. package/dist/commonjs/config/config.default.js +31 -0
  13. package/dist/commonjs/index.d.ts +2 -0
  14. package/dist/commonjs/index.js +5 -0
  15. package/dist/commonjs/lib/LimitError.d.ts +5 -0
  16. package/dist/commonjs/lib/LimitError.js +16 -0
  17. package/dist/commonjs/lib/MultipartFileTooLargeError.d.ts +6 -0
  18. package/dist/commonjs/lib/MultipartFileTooLargeError.js +18 -0
  19. package/dist/commonjs/lib/utils.d.ts +4 -0
  20. package/dist/commonjs/lib/utils.js +93 -0
  21. package/dist/commonjs/package.json +3 -0
  22. package/dist/esm/app/extend/context.d.ts +110 -0
  23. package/dist/esm/app/extend/context.js +273 -0
  24. package/dist/esm/app/middleware/multipart.d.ts +4 -0
  25. package/dist/esm/app/middleware/multipart.js +18 -0
  26. package/dist/esm/app/schedule/clean_tmpdir.d.ts +3 -0
  27. package/dist/esm/app/schedule/clean_tmpdir.js +53 -0
  28. package/dist/esm/app.d.ts +6 -0
  29. package/dist/esm/app.js +18 -0
  30. package/dist/esm/config/config.default.d.ts +98 -0
  31. package/dist/esm/config/config.default.js +26 -0
  32. package/dist/esm/index.d.ts +2 -0
  33. package/dist/esm/index.js +3 -0
  34. package/dist/esm/lib/LimitError.d.ts +5 -0
  35. package/dist/esm/lib/LimitError.js +12 -0
  36. package/dist/esm/lib/MultipartFileTooLargeError.d.ts +6 -0
  37. package/dist/esm/lib/MultipartFileTooLargeError.js +14 -0
  38. package/dist/esm/lib/utils.d.ts +4 -0
  39. package/dist/esm/lib/utils.js +85 -0
  40. package/dist/esm/package.json +3 -0
  41. package/dist/package.json +4 -0
  42. package/package.json +103 -0
  43. package/src/app/extend/context.ts +368 -0
  44. package/src/app/middleware/multipart.ts +20 -0
  45. package/src/app/schedule/clean_tmpdir.ts +55 -0
  46. package/src/app.ts +20 -0
  47. package/src/config/config.default.ts +130 -0
  48. package/src/index.ts +2 -0
  49. package/src/lib/LimitError.ts +12 -0
  50. package/src/lib/MultipartFileTooLargeError.ts +14 -0
  51. package/src/lib/utils.ts +92 -0
  52. package/src/typings/index.d.ts +4 -0
@@ -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
+ }
@@ -0,0 +1,4 @@
1
+ // make sure to import egg typings and let typescript know about it
2
+ // @see https://github.com/whxaxes/blog/issues/11
3
+ // and https://www.typescriptlang.org/docs/handbook/declaration-merging.html
4
+ import 'egg';