@eggjs/multipart 4.0.0 → 5.0.0-beta.18
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/README.md +21 -32
- package/dist/app/extend/context.d.ts +101 -0
- package/dist/app/extend/context.js +199 -0
- package/dist/app/middleware/multipart.d.ts +7 -0
- package/dist/app/middleware/multipart.js +15 -0
- package/dist/app/schedule/clean_tmpdir.d.ts +29 -0
- package/dist/app/schedule/clean_tmpdir.js +57 -0
- package/dist/app.d.ts +10 -0
- package/dist/app.js +21 -0
- package/{src/config/config.default.ts → dist/config/config.default.d.ts} +11 -49
- package/dist/config/config.default.js +28 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/lib/LimitError.d.ts +8 -0
- package/dist/lib/LimitError.js +15 -0
- package/dist/lib/MultipartFileTooLargeError.d.ts +9 -0
- package/dist/lib/MultipartFileTooLargeError.js +17 -0
- package/dist/lib/utils.d.ts +8 -0
- package/dist/lib/utils.js +71 -0
- package/dist/types.d.ts +24 -0
- package/dist/types.js +1 -0
- package/package.json +55 -74
- package/dist/commonjs/app/extend/context.d.ts +0 -110
- package/dist/commonjs/app/extend/context.js +0 -279
- package/dist/commonjs/app/middleware/multipart.d.ts +0 -4
- package/dist/commonjs/app/middleware/multipart.js +0 -20
- package/dist/commonjs/app/schedule/clean_tmpdir.d.ts +0 -3
- package/dist/commonjs/app/schedule/clean_tmpdir.js +0 -58
- package/dist/commonjs/app.d.ts +0 -6
- package/dist/commonjs/app.js +0 -21
- package/dist/commonjs/config/config.default.d.ts +0 -98
- package/dist/commonjs/config/config.default.js +0 -31
- package/dist/commonjs/index.d.ts +0 -2
- package/dist/commonjs/index.js +0 -5
- package/dist/commonjs/lib/LimitError.d.ts +0 -5
- package/dist/commonjs/lib/LimitError.js +0 -16
- package/dist/commonjs/lib/MultipartFileTooLargeError.d.ts +0 -6
- package/dist/commonjs/lib/MultipartFileTooLargeError.js +0 -18
- package/dist/commonjs/lib/utils.d.ts +0 -4
- package/dist/commonjs/lib/utils.js +0 -93
- package/dist/commonjs/package.json +0 -3
- package/dist/esm/app/extend/context.d.ts +0 -110
- package/dist/esm/app/extend/context.js +0 -273
- package/dist/esm/app/middleware/multipart.d.ts +0 -4
- package/dist/esm/app/middleware/multipart.js +0 -18
- package/dist/esm/app/schedule/clean_tmpdir.d.ts +0 -3
- package/dist/esm/app/schedule/clean_tmpdir.js +0 -53
- package/dist/esm/app.d.ts +0 -6
- package/dist/esm/app.js +0 -18
- package/dist/esm/config/config.default.d.ts +0 -98
- package/dist/esm/config/config.default.js +0 -26
- package/dist/esm/index.d.ts +0 -2
- package/dist/esm/index.js +0 -3
- package/dist/esm/lib/LimitError.d.ts +0 -5
- package/dist/esm/lib/LimitError.js +0 -12
- package/dist/esm/lib/MultipartFileTooLargeError.d.ts +0 -6
- package/dist/esm/lib/MultipartFileTooLargeError.js +0 -14
- package/dist/esm/lib/utils.d.ts +0 -4
- package/dist/esm/lib/utils.js +0 -85
- package/dist/esm/package.json +0 -3
- package/dist/package.json +0 -4
- package/src/app/extend/context.ts +0 -368
- package/src/app/middleware/multipart.ts +0 -20
- package/src/app/schedule/clean_tmpdir.ts +0 -55
- package/src/app.ts +0 -20
- package/src/index.ts +0 -2
- package/src/lib/LimitError.ts +0 -12
- package/src/lib/MultipartFileTooLargeError.ts +0 -14
- package/src/lib/utils.ts +0 -92
- package/src/typings/index.d.ts +0 -4
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import type { Context, EggAppInfo } from '@eggjs/core';
|
|
2
|
-
import type { PathMatchingPattern } from 'egg-path-matching';
|
|
3
|
-
export type MatchItem = string | RegExp | ((ctx: Context) => boolean);
|
|
4
|
-
/**
|
|
5
|
-
* multipart parser options
|
|
6
|
-
* @member Config#multipart
|
|
7
|
-
*/
|
|
8
|
-
export interface MultipartConfig {
|
|
9
|
-
/**
|
|
10
|
-
* which mode to handle multipart request, default is `stream`, the hard way.
|
|
11
|
-
* If set mode to `file`, it's the easy way to handle multipart request and save it to local files.
|
|
12
|
-
* If you don't know the Node.js Stream work, maybe you should use the `file` mode to get started.
|
|
13
|
-
*/
|
|
14
|
-
mode: 'stream' | 'file';
|
|
15
|
-
/**
|
|
16
|
-
* special url to use file mode when global `mode` is `stream`.
|
|
17
|
-
*/
|
|
18
|
-
fileModeMatch?: PathMatchingPattern;
|
|
19
|
-
/**
|
|
20
|
-
* Auto set fields to parts, default is `false`.
|
|
21
|
-
* Only work on `stream` mode.
|
|
22
|
-
* If set true,all fields will be auto handle and can access by `parts.fields`
|
|
23
|
-
*/
|
|
24
|
-
autoFields: boolean;
|
|
25
|
-
/**
|
|
26
|
-
* default charset encoding, don't change it before you real know about it
|
|
27
|
-
* Default is `utf8`
|
|
28
|
-
*/
|
|
29
|
-
defaultCharset: string;
|
|
30
|
-
/**
|
|
31
|
-
* For multipart forms, the default character set to use for values of part header parameters (e.g. filename)
|
|
32
|
-
* that are not extended parameters (that contain an explicit charset), don't change it before you real know about it
|
|
33
|
-
* Default is `utf8`
|
|
34
|
-
*/
|
|
35
|
-
defaultParamCharset: string;
|
|
36
|
-
/**
|
|
37
|
-
* Max field name size (in bytes), default is `100`
|
|
38
|
-
*/
|
|
39
|
-
fieldNameSize: number;
|
|
40
|
-
/**
|
|
41
|
-
* Max field value size (in bytes), default is `100kb`
|
|
42
|
-
*/
|
|
43
|
-
fieldSize: string | number;
|
|
44
|
-
/**
|
|
45
|
-
* Max number of non-file fields, default is `10`
|
|
46
|
-
*/
|
|
47
|
-
fields: number;
|
|
48
|
-
/**
|
|
49
|
-
* Max file size (in bytes), default is `10mb`
|
|
50
|
-
*/
|
|
51
|
-
fileSize: string | number;
|
|
52
|
-
/**
|
|
53
|
-
* Max number of file fields, default is `10`
|
|
54
|
-
*/
|
|
55
|
-
files: number;
|
|
56
|
-
/**
|
|
57
|
-
* Add more ext file names to the `whitelist`, default is `[]`, only valid when `whitelist` is `null`
|
|
58
|
-
*/
|
|
59
|
-
fileExtensions: string[];
|
|
60
|
-
/**
|
|
61
|
-
* The white ext file names, default is `null`
|
|
62
|
-
*/
|
|
63
|
-
whitelist: string[] | ((filename: string) => boolean) | null;
|
|
64
|
-
/**
|
|
65
|
-
* Allow array field, default is `false`
|
|
66
|
-
*/
|
|
67
|
-
allowArrayField: boolean;
|
|
68
|
-
/**
|
|
69
|
-
* The directory for temporary files. Only work on `file` mode.
|
|
70
|
-
* Default is `os.tmpdir()/egg-multipart-tmp/${appInfo.name}`
|
|
71
|
-
*/
|
|
72
|
-
tmpdir: string;
|
|
73
|
-
/**
|
|
74
|
-
* The schedule for cleaning temporary files. Only work on `file` mode.
|
|
75
|
-
*/
|
|
76
|
-
cleanSchedule: {
|
|
77
|
-
/**
|
|
78
|
-
* The cron expression for the schedule.
|
|
79
|
-
* Default is `0 30 4 * * *`
|
|
80
|
-
* @see https://github.com/eggjs/egg-schedule#cron-style-scheduling
|
|
81
|
-
*/
|
|
82
|
-
cron: string;
|
|
83
|
-
/**
|
|
84
|
-
* Default is `false`
|
|
85
|
-
*/
|
|
86
|
-
disable: boolean;
|
|
87
|
-
};
|
|
88
|
-
checkFile?(fieldname: string, file: any, filename: string, encoding: string, mimetype: string): void | Error;
|
|
89
|
-
}
|
|
90
|
-
declare const _default: (appInfo: EggAppInfo) => {
|
|
91
|
-
multipart: MultipartConfig;
|
|
92
|
-
};
|
|
93
|
-
export default _default;
|
|
94
|
-
declare module '@eggjs/core' {
|
|
95
|
-
interface EggAppConfig {
|
|
96
|
-
multipart: MultipartConfig;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import os from 'node:os';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
export default (appInfo) => {
|
|
4
|
-
return {
|
|
5
|
-
multipart: {
|
|
6
|
-
mode: 'stream',
|
|
7
|
-
autoFields: false,
|
|
8
|
-
defaultCharset: 'utf8',
|
|
9
|
-
defaultParamCharset: 'utf8',
|
|
10
|
-
fieldNameSize: 100,
|
|
11
|
-
fieldSize: '100kb',
|
|
12
|
-
fields: 10,
|
|
13
|
-
fileSize: '10mb',
|
|
14
|
-
files: 10,
|
|
15
|
-
fileExtensions: [],
|
|
16
|
-
whitelist: null,
|
|
17
|
-
allowArrayField: false,
|
|
18
|
-
tmpdir: path.join(os.tmpdir(), 'egg-multipart-tmp', appInfo.name),
|
|
19
|
-
cleanSchedule: {
|
|
20
|
-
cron: '0 30 4 * * *',
|
|
21
|
-
disable: false,
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29uZmlnLmRlZmF1bHQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvY29uZmlnL2NvbmZpZy5kZWZhdWx0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxNQUFNLFNBQVMsQ0FBQztBQUN6QixPQUFPLElBQUksTUFBTSxXQUFXLENBQUM7QUFtRzdCLGVBQWUsQ0FBQyxPQUFtQixFQUFFLEVBQUU7SUFDckMsT0FBTztRQUNMLFNBQVMsRUFBRTtZQUNULElBQUksRUFBRSxRQUFRO1lBQ2QsVUFBVSxFQUFFLEtBQUs7WUFDakIsY0FBYyxFQUFFLE1BQU07WUFDdEIsbUJBQW1CLEVBQUUsTUFBTTtZQUMzQixhQUFhLEVBQUUsR0FBRztZQUNsQixTQUFTLEVBQUUsT0FBTztZQUNsQixNQUFNLEVBQUUsRUFBRTtZQUNWLFFBQVEsRUFBRSxNQUFNO1lBQ2hCLEtBQUssRUFBRSxFQUFFO1lBQ1QsY0FBYyxFQUFFLEVBQUU7WUFDbEIsU0FBUyxFQUFFLElBQUk7WUFDZixlQUFlLEVBQUUsS0FBSztZQUN0QixNQUFNLEVBQUUsSUFBSSxDQUFDLElBQUksQ0FBQyxFQUFFLENBQUMsTUFBTSxFQUFFLEVBQUUsbUJBQW1CLEVBQUUsT0FBTyxDQUFDLElBQUksQ0FBQztZQUNqRSxhQUFhLEVBQUU7Z0JBQ2IsSUFBSSxFQUFFLGNBQWM7Z0JBQ3BCLE9BQU8sRUFBRSxLQUFLO2FBQ2Y7U0FDaUI7S0FDckIsQ0FBQztBQUNKLENBQUMsQ0FBQyJ9
|
package/dist/esm/index.d.ts
DELETED
package/dist/esm/index.js
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import './config/config.default.js';
|
|
2
|
-
import './app/extend/context.js';
|
|
3
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyw0QkFBNEIsQ0FBQztBQUNwQyxPQUFPLHlCQUF5QixDQUFDIn0=
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export class LimitError extends Error {
|
|
2
|
-
code;
|
|
3
|
-
status;
|
|
4
|
-
constructor(code, message) {
|
|
5
|
-
super(message);
|
|
6
|
-
this.code = code;
|
|
7
|
-
this.status = 413;
|
|
8
|
-
this.name = this.constructor.name;
|
|
9
|
-
Error.captureStackTrace(this, this.constructor);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiTGltaXRFcnJvci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9saWIvTGltaXRFcnJvci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxNQUFNLE9BQU8sVUFBVyxTQUFRLEtBQUs7SUFDbkMsSUFBSSxDQUFTO0lBQ2IsTUFBTSxDQUFTO0lBRWYsWUFBWSxJQUFZLEVBQUUsT0FBZTtRQUN2QyxLQUFLLENBQUMsT0FBTyxDQUFDLENBQUM7UUFDZixJQUFJLENBQUMsSUFBSSxHQUFHLElBQUksQ0FBQztRQUNqQixJQUFJLENBQUMsTUFBTSxHQUFHLEdBQUcsQ0FBQztRQUNsQixJQUFJLENBQUMsSUFBSSxHQUFHLElBQUksQ0FBQyxXQUFXLENBQUMsSUFBSSxDQUFDO1FBQ2xDLEtBQUssQ0FBQyxpQkFBaUIsQ0FBQyxJQUFJLEVBQUUsSUFBSSxDQUFDLFdBQVcsQ0FBQyxDQUFDO0lBQ2xELENBQUM7Q0FDRiJ9
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export class MultipartFileTooLargeError extends Error {
|
|
2
|
-
status;
|
|
3
|
-
fields;
|
|
4
|
-
filename;
|
|
5
|
-
constructor(message, fields, filename) {
|
|
6
|
-
super(message);
|
|
7
|
-
this.name = this.constructor.name;
|
|
8
|
-
this.status = 413;
|
|
9
|
-
this.fields = fields;
|
|
10
|
-
this.filename = filename;
|
|
11
|
-
Error.captureStackTrace(this, this.constructor);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiTXVsdGlwYXJ0RmlsZVRvb0xhcmdlRXJyb3IuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbGliL011bHRpcGFydEZpbGVUb29MYXJnZUVycm9yLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE1BQU0sT0FBTywwQkFBMkIsU0FBUSxLQUFLO0lBQ25ELE1BQU0sQ0FBUztJQUNmLE1BQU0sQ0FBc0I7SUFDNUIsUUFBUSxDQUFTO0lBRWpCLFlBQVksT0FBZSxFQUFFLE1BQTJCLEVBQUUsUUFBZ0I7UUFDeEUsS0FBSyxDQUFDLE9BQU8sQ0FBQyxDQUFDO1FBQ2YsSUFBSSxDQUFDLElBQUksR0FBRyxJQUFJLENBQUMsV0FBVyxDQUFDLElBQUksQ0FBQztRQUNsQyxJQUFJLENBQUMsTUFBTSxHQUFHLEdBQUcsQ0FBQztRQUNsQixJQUFJLENBQUMsTUFBTSxHQUFHLE1BQU0sQ0FBQztRQUNyQixJQUFJLENBQUMsUUFBUSxHQUFHLFFBQVEsQ0FBQztRQUN6QixLQUFLLENBQUMsaUJBQWlCLENBQUMsSUFBSSxFQUFFLElBQUksQ0FBQyxXQUFXLENBQUMsQ0FBQztJQUNsRCxDQUFDO0NBQ0YifQ==
|
package/dist/esm/lib/utils.d.ts
DELETED
package/dist/esm/lib/utils.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import assert from 'node:assert';
|
|
3
|
-
import bytes from 'bytes';
|
|
4
|
-
export const whitelist = [
|
|
5
|
-
// images
|
|
6
|
-
'.jpg', '.jpeg', // image/jpeg
|
|
7
|
-
'.png', // image/png, image/x-png
|
|
8
|
-
'.gif', // image/gif
|
|
9
|
-
'.bmp', // image/bmp
|
|
10
|
-
'.wbmp', // image/vnd.wap.wbmp
|
|
11
|
-
'.webp',
|
|
12
|
-
'.tif',
|
|
13
|
-
'.psd',
|
|
14
|
-
// text
|
|
15
|
-
'.svg',
|
|
16
|
-
'.js', '.jsx',
|
|
17
|
-
'.json',
|
|
18
|
-
'.css', '.less',
|
|
19
|
-
'.html', '.htm',
|
|
20
|
-
'.xml',
|
|
21
|
-
// tar
|
|
22
|
-
'.zip',
|
|
23
|
-
'.gz', '.tgz', '.gzip',
|
|
24
|
-
// video
|
|
25
|
-
'.mp3',
|
|
26
|
-
'.mp4',
|
|
27
|
-
'.avi',
|
|
28
|
-
];
|
|
29
|
-
export function humanizeBytes(size) {
|
|
30
|
-
if (typeof size === 'number') {
|
|
31
|
-
return size;
|
|
32
|
-
}
|
|
33
|
-
return bytes(size);
|
|
34
|
-
}
|
|
35
|
-
export function normalizeOptions(options) {
|
|
36
|
-
// make sure to cast the value of config **Size to number
|
|
37
|
-
options.fileSize = humanizeBytes(options.fileSize);
|
|
38
|
-
options.fieldSize = humanizeBytes(options.fieldSize);
|
|
39
|
-
options.fieldNameSize = humanizeBytes(options.fieldNameSize);
|
|
40
|
-
// validate mode
|
|
41
|
-
options.mode = options.mode || 'stream';
|
|
42
|
-
assert(['stream', 'file'].includes(options.mode), `Expect mode to be 'stream' or 'file', but got '${options.mode}'`);
|
|
43
|
-
if (options.mode === 'file') {
|
|
44
|
-
assert(!options.fileModeMatch, '`fileModeMatch` options only work on stream mode, please remove it');
|
|
45
|
-
}
|
|
46
|
-
// normalize whitelist
|
|
47
|
-
if (Array.isArray(options.whitelist)) {
|
|
48
|
-
options.whitelist = options.whitelist.map(extname => extname.toLowerCase());
|
|
49
|
-
}
|
|
50
|
-
// normalize fileExtensions
|
|
51
|
-
if (Array.isArray(options.fileExtensions)) {
|
|
52
|
-
options.fileExtensions = options.fileExtensions.map(extname => {
|
|
53
|
-
return (extname.startsWith('.') || extname === '') ? extname.toLowerCase() : `.${extname.toLowerCase()}`;
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
function checkExt(fileName) {
|
|
57
|
-
if (typeof options.whitelist === 'function') {
|
|
58
|
-
return options.whitelist(fileName);
|
|
59
|
-
}
|
|
60
|
-
const extname = path.extname(fileName).toLowerCase();
|
|
61
|
-
if (Array.isArray(options.whitelist)) {
|
|
62
|
-
return options.whitelist.includes(extname);
|
|
63
|
-
}
|
|
64
|
-
// only if user don't provide whitelist, we will use default whitelist + fileExtensions
|
|
65
|
-
return whitelist.includes(extname) || options.fileExtensions.includes(extname);
|
|
66
|
-
}
|
|
67
|
-
options.checkFile = (_fieldName, fileStream, fileName) => {
|
|
68
|
-
// just ignore, if no file
|
|
69
|
-
if (!fileStream || !fileName)
|
|
70
|
-
return;
|
|
71
|
-
try {
|
|
72
|
-
if (!checkExt(fileName)) {
|
|
73
|
-
const err = new Error('Invalid filename: ' + fileName);
|
|
74
|
-
Reflect.set(err, 'status', 400);
|
|
75
|
-
return err;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
catch (err) {
|
|
79
|
-
err.status = 400;
|
|
80
|
-
return err;
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
return options;
|
|
84
|
-
}
|
|
85
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvbGliL3V0aWxzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sSUFBSSxNQUFNLFdBQVcsQ0FBQztBQUM3QixPQUFPLE1BQU0sTUFBTSxhQUFhLENBQUM7QUFDakMsT0FBTyxLQUFLLE1BQU0sT0FBTyxDQUFDO0FBRzFCLE1BQU0sQ0FBQyxNQUFNLFNBQVMsR0FBRztJQUN2QixTQUFTO0lBQ1QsTUFBTSxFQUFFLE9BQU8sRUFBRSxhQUFhO0lBQzlCLE1BQU0sRUFBRSx5QkFBeUI7SUFDakMsTUFBTSxFQUFFLFlBQVk7SUFDcEIsTUFBTSxFQUFFLFlBQVk7SUFDcEIsT0FBTyxFQUFFLHFCQUFxQjtJQUM5QixPQUFPO0lBQ1AsTUFBTTtJQUNOLE1BQU07SUFDTixPQUFPO0lBQ1AsTUFBTTtJQUNOLEtBQUssRUFBRSxNQUFNO0lBQ2IsT0FBTztJQUNQLE1BQU0sRUFBRSxPQUFPO0lBQ2YsT0FBTyxFQUFFLE1BQU07SUFDZixNQUFNO0lBQ04sTUFBTTtJQUNOLE1BQU07SUFDTixLQUFLLEVBQUUsTUFBTSxFQUFFLE9BQU87SUFDdEIsUUFBUTtJQUNSLE1BQU07SUFDTixNQUFNO0lBQ04sTUFBTTtDQUNQLENBQUM7QUFFRixNQUFNLFVBQVUsYUFBYSxDQUFDLElBQXFCO0lBQ2pELElBQUksT0FBTyxJQUFJLEtBQUssUUFBUSxFQUFFLENBQUM7UUFDN0IsT0FBTyxJQUFJLENBQUM7SUFDZCxDQUFDO0lBQ0QsT0FBTyxLQUFLLENBQUMsSUFBSSxDQUFXLENBQUM7QUFDL0IsQ0FBQztBQUVELE1BQU0sVUFBVSxnQkFBZ0IsQ0FBQyxPQUF3QjtJQUN2RCx5REFBeUQ7SUFDekQsT0FBTyxDQUFDLFFBQVEsR0FBRyxhQUFhLENBQUMsT0FBTyxDQUFDLFFBQVEsQ0FBQyxDQUFDO0lBQ25ELE9BQU8sQ0FBQyxTQUFTLEdBQUcsYUFBYSxDQUFDLE9BQU8sQ0FBQyxTQUFT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
|
package/dist/esm/package.json
DELETED
package/dist/package.json
DELETED
|
@@ -1,368 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { randomUUID } from 'node:crypto';
|
|
4
|
-
import fs from 'node:fs/promises';
|
|
5
|
-
import { createWriteStream } from 'node:fs';
|
|
6
|
-
import { Readable, PassThrough } from 'node:stream';
|
|
7
|
-
import { pipeline } from 'node:stream/promises';
|
|
8
|
-
// @ts-expect-error no types
|
|
9
|
-
import parse from 'co-busboy';
|
|
10
|
-
import dayjs from 'dayjs';
|
|
11
|
-
import { Context } from '@eggjs/core';
|
|
12
|
-
import { humanizeBytes } from '../../lib/utils.js';
|
|
13
|
-
import { LimitError } from '../../lib/LimitError.js';
|
|
14
|
-
import { MultipartFileTooLargeError } from '../../lib/MultipartFileTooLargeError.js';
|
|
15
|
-
|
|
16
|
-
const HAS_CONSUMED = Symbol('Context#multipartHasConsumed');
|
|
17
|
-
|
|
18
|
-
export interface EggFile {
|
|
19
|
-
field: string;
|
|
20
|
-
filename: string;
|
|
21
|
-
encoding: string;
|
|
22
|
-
mime: string;
|
|
23
|
-
filepath: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface MultipartFileStream extends Readable {
|
|
27
|
-
fields: Record<string, any>;
|
|
28
|
-
filename: string;
|
|
29
|
-
fieldname: string;
|
|
30
|
-
mime: string;
|
|
31
|
-
mimeType: string;
|
|
32
|
-
transferEncoding: string;
|
|
33
|
-
encoding: string;
|
|
34
|
-
truncated: boolean;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface MultipartOptions {
|
|
38
|
-
autoFields?: boolean;
|
|
39
|
-
/**
|
|
40
|
-
* required file submit, default is true
|
|
41
|
-
*/
|
|
42
|
-
requireFile?: boolean;
|
|
43
|
-
/**
|
|
44
|
-
* default charset encoding
|
|
45
|
-
*/
|
|
46
|
-
defaultCharset?: string;
|
|
47
|
-
/**
|
|
48
|
-
* compatible with defaultCharset
|
|
49
|
-
* @deprecated use `defaultCharset` instead
|
|
50
|
-
*/
|
|
51
|
-
defCharset?: string;
|
|
52
|
-
defaultParamCharset?: string;
|
|
53
|
-
/**
|
|
54
|
-
* compatible with defaultParamCharset
|
|
55
|
-
* @deprecated use `defaultParamCharset` instead
|
|
56
|
-
*/
|
|
57
|
-
defParamCharset?: string;
|
|
58
|
-
limits?: {
|
|
59
|
-
fieldNameSize?: number;
|
|
60
|
-
fieldSize?: number;
|
|
61
|
-
fields?: number;
|
|
62
|
-
fileSize?: number;
|
|
63
|
-
files?: number;
|
|
64
|
-
parts?: number;
|
|
65
|
-
headerPairs?: number;
|
|
66
|
-
};
|
|
67
|
-
checkFile?(
|
|
68
|
-
fieldname: string,
|
|
69
|
-
file: any,
|
|
70
|
-
filename: string,
|
|
71
|
-
encoding: string,
|
|
72
|
-
mimetype: string
|
|
73
|
-
): void | Error;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export default class MultipartContext extends Context {
|
|
77
|
-
/**
|
|
78
|
-
* create multipart.parts instance, to get separated files.
|
|
79
|
-
* @function Context#multipart
|
|
80
|
-
* @param {Object} [options] - override default multipart configurations
|
|
81
|
-
* - {Boolean} options.autoFields
|
|
82
|
-
* - {String} options.defaultCharset
|
|
83
|
-
* - {String} options.defaultParamCharset
|
|
84
|
-
* - {Object} options.limits
|
|
85
|
-
* - {Function} options.checkFile
|
|
86
|
-
* @return {Yieldable | AsyncIterable<Yieldable>} parts
|
|
87
|
-
*/
|
|
88
|
-
multipart(options: MultipartOptions = {}): AsyncIterable<MultipartFileStream> {
|
|
89
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
90
|
-
const ctx = this;
|
|
91
|
-
// multipart/form-data
|
|
92
|
-
if (!ctx.is('multipart')) ctx.throw(400, 'Content-Type must be multipart/*');
|
|
93
|
-
|
|
94
|
-
assert(!ctx[HAS_CONSUMED], 'the multipart request can\'t be consumed twice');
|
|
95
|
-
ctx[HAS_CONSUMED] = true;
|
|
96
|
-
|
|
97
|
-
const { autoFields, defaultCharset, defaultParamCharset, checkFile } = ctx.app.config.multipart;
|
|
98
|
-
const { fieldNameSize, fieldSize, fields, fileSize, files } = ctx.app.config.multipart;
|
|
99
|
-
options = extractOptions(options);
|
|
100
|
-
|
|
101
|
-
const parseOptions = Object.assign({
|
|
102
|
-
autoFields,
|
|
103
|
-
defCharset: defaultCharset,
|
|
104
|
-
defParamCharset: defaultParamCharset,
|
|
105
|
-
checkFile,
|
|
106
|
-
}, options);
|
|
107
|
-
|
|
108
|
-
// https://github.com/mscdex/busboy#busboy-methods
|
|
109
|
-
// merge limits
|
|
110
|
-
parseOptions.limits = Object.assign({
|
|
111
|
-
fieldNameSize,
|
|
112
|
-
fieldSize,
|
|
113
|
-
fields,
|
|
114
|
-
fileSize,
|
|
115
|
-
files,
|
|
116
|
-
}, options.limits);
|
|
117
|
-
|
|
118
|
-
// mount asyncIterator, so we can use `for await` to get parts
|
|
119
|
-
const parts = parse(this, parseOptions);
|
|
120
|
-
parts[Symbol.asyncIterator] = async function* () {
|
|
121
|
-
let part: MultipartFileStream | undefined;
|
|
122
|
-
do {
|
|
123
|
-
part = await parts();
|
|
124
|
-
|
|
125
|
-
if (!part) continue;
|
|
126
|
-
|
|
127
|
-
if (Array.isArray(part)) {
|
|
128
|
-
if (part[3]) throw new LimitError('Request_fieldSize_limit', 'Reach fieldSize limit');
|
|
129
|
-
// TODO: still not support at busboy 1.x (only support at urlencoded)
|
|
130
|
-
// https://github.com/mscdex/busboy/blob/v0.3.1/lib/types/multipart.js#L5
|
|
131
|
-
// https://github.com/mscdex/busboy/blob/master/lib/types/multipart.js#L251
|
|
132
|
-
// if (part[2]) throw new LimitError('Request_fieldNameSize_limit', 'Reach fieldNameSize limit');
|
|
133
|
-
} else {
|
|
134
|
-
// user click `upload` before choose a file, `part` will be file stream, but `part.filename` is empty must handler this, such as log error.
|
|
135
|
-
if (!part.filename) {
|
|
136
|
-
ctx.coreLogger.debug('[egg-multipart] file field `%s` is upload without file stream, will drop it.', part.fieldname);
|
|
137
|
-
await pipeline(part, new PassThrough());
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
// TODO: check whether filename is malicious input
|
|
141
|
-
|
|
142
|
-
// busboy only set truncated when consume the stream
|
|
143
|
-
if (part.truncated) {
|
|
144
|
-
// in case of emit 'limit' too fast
|
|
145
|
-
throw new LimitError('Request_fileSize_limit', 'Reach fileSize limit');
|
|
146
|
-
} else {
|
|
147
|
-
part.once('limit', function(this: MultipartFileStream) {
|
|
148
|
-
this.emit('error', new LimitError('Request_fileSize_limit', 'Reach fileSize limit'));
|
|
149
|
-
this.resume();
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// dispatch part to outter logic such as for-await-of
|
|
155
|
-
yield part;
|
|
156
|
-
|
|
157
|
-
} while (part !== undefined);
|
|
158
|
-
};
|
|
159
|
-
return parts;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* save request multipart data and files to `ctx.request`
|
|
164
|
-
* @function Context#saveRequestFiles
|
|
165
|
-
* @param {Object} options - { limits, checkFile, ... }
|
|
166
|
-
*/
|
|
167
|
-
async saveRequestFiles(options: MultipartOptions = {}) {
|
|
168
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
169
|
-
const ctx = this;
|
|
170
|
-
|
|
171
|
-
const allowArrayField = ctx.app.config.multipart.allowArrayField;
|
|
172
|
-
|
|
173
|
-
let storeDir: string | undefined;
|
|
174
|
-
|
|
175
|
-
const requestBody: Record<string, any> = {};
|
|
176
|
-
const requestFiles: EggFile[] = [];
|
|
177
|
-
|
|
178
|
-
options.autoFields = false;
|
|
179
|
-
const parts = ctx.multipart(options);
|
|
180
|
-
|
|
181
|
-
try {
|
|
182
|
-
for await (const part of parts) {
|
|
183
|
-
if (Array.isArray(part)) {
|
|
184
|
-
// fields
|
|
185
|
-
const [ fieldName, fieldValue ] = part;
|
|
186
|
-
if (!allowArrayField) {
|
|
187
|
-
requestBody[fieldName] = fieldValue;
|
|
188
|
-
} else {
|
|
189
|
-
if (!requestBody[fieldName]) {
|
|
190
|
-
requestBody[fieldName] = fieldValue;
|
|
191
|
-
} else if (!Array.isArray(requestBody[fieldName])) {
|
|
192
|
-
requestBody[fieldName] = [ requestBody[fieldName], fieldValue ];
|
|
193
|
-
} else {
|
|
194
|
-
requestBody[fieldName].push(fieldValue);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
} else {
|
|
198
|
-
// stream
|
|
199
|
-
const { filename, fieldname, encoding, mime } = part;
|
|
200
|
-
|
|
201
|
-
if (!storeDir) {
|
|
202
|
-
// ${tmpdir}/YYYY/MM/DD/HH
|
|
203
|
-
storeDir = path.join(ctx.app.config.multipart.tmpdir, dayjs().format('YYYY/MM/DD/HH'));
|
|
204
|
-
await fs.mkdir(storeDir, { recursive: true });
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// write to tmp file
|
|
208
|
-
const filepath = path.join(storeDir, randomUUID() + path.extname(filename));
|
|
209
|
-
const target = createWriteStream(filepath);
|
|
210
|
-
await pipeline(part, target);
|
|
211
|
-
|
|
212
|
-
const meta = {
|
|
213
|
-
filepath,
|
|
214
|
-
field: fieldname,
|
|
215
|
-
filename,
|
|
216
|
-
encoding,
|
|
217
|
-
mime,
|
|
218
|
-
// keep same property name as file stream, https://github.com/cojs/busboy/blob/master/index.js#L114
|
|
219
|
-
fieldname,
|
|
220
|
-
transferEncoding: encoding,
|
|
221
|
-
mimeType: mime,
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
requestFiles.push(meta);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
} catch (err) {
|
|
228
|
-
await ctx.cleanupRequestFiles(requestFiles);
|
|
229
|
-
throw err;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
ctx.request.body = requestBody;
|
|
233
|
-
ctx.request.files = requestFiles;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* get upload file stream
|
|
238
|
-
* @example
|
|
239
|
-
* ```js
|
|
240
|
-
* const stream = await ctx.getFileStream();
|
|
241
|
-
* // get other fields
|
|
242
|
-
* console.log(stream.fields);
|
|
243
|
-
* ```
|
|
244
|
-
* @function Context#getFileStream
|
|
245
|
-
* @param {Object} options
|
|
246
|
-
* - {Boolean} options.requireFile - required file submit, default is true
|
|
247
|
-
* - {String} options.defaultCharset
|
|
248
|
-
* - {String} options.defaultParamCharset
|
|
249
|
-
* - {Object} options.limits
|
|
250
|
-
* - {Function} options.checkFile
|
|
251
|
-
* @return {ReadStream} stream
|
|
252
|
-
* @since 1.0.0
|
|
253
|
-
* @deprecated Not safe enough, use `ctx.multipart()` instead
|
|
254
|
-
*/
|
|
255
|
-
async getFileStream(options: MultipartOptions = {}): Promise<MultipartFileStream> {
|
|
256
|
-
options.autoFields = true;
|
|
257
|
-
const parts: any = this.multipart(options);
|
|
258
|
-
let stream: MultipartFileStream = await parts();
|
|
259
|
-
|
|
260
|
-
if (options.requireFile !== false) {
|
|
261
|
-
// stream not exists, treat as an exception
|
|
262
|
-
if (!stream || !stream.filename) {
|
|
263
|
-
this.throw(400, 'Can\'t found upload file');
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
if (!stream) {
|
|
268
|
-
stream = Readable.from([]) as MultipartFileStream;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (stream.truncated) {
|
|
272
|
-
throw new LimitError('Request_fileSize_limit', 'Request file too large, please check multipart config');
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
stream.fields = parts.field;
|
|
276
|
-
stream.once('limit', () => {
|
|
277
|
-
const err = new MultipartFileTooLargeError(
|
|
278
|
-
'Request file too large, please check multipart config', stream.fields, stream.filename);
|
|
279
|
-
if (stream.listenerCount('error') > 0) {
|
|
280
|
-
stream.emit('error', err);
|
|
281
|
-
this.coreLogger.warn(err);
|
|
282
|
-
} else {
|
|
283
|
-
this.coreLogger.error(err);
|
|
284
|
-
// ignore next error event
|
|
285
|
-
stream.on('error', () => {});
|
|
286
|
-
}
|
|
287
|
-
// ignore all data
|
|
288
|
-
stream.resume();
|
|
289
|
-
});
|
|
290
|
-
return stream;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* clean up request tmp files helper
|
|
295
|
-
* @function Context#cleanupRequestFiles
|
|
296
|
-
* @param {Array<String>} [files] - file paths need to cleanup, default is `ctx.request.files`.
|
|
297
|
-
*/
|
|
298
|
-
async cleanupRequestFiles(files?: EggFile[]) {
|
|
299
|
-
if (!files || !files.length) {
|
|
300
|
-
files = this.request.files;
|
|
301
|
-
}
|
|
302
|
-
if (Array.isArray(files)) {
|
|
303
|
-
for (const file of files) {
|
|
304
|
-
try {
|
|
305
|
-
await fs.rm(file.filepath, { force: true, recursive: true });
|
|
306
|
-
} catch (err) {
|
|
307
|
-
// warning log
|
|
308
|
-
this.coreLogger.warn('[egg-multipart-cleanupRequestFiles-error] file: %j, error: %s', file, err);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
function extractOptions(options: MultipartOptions = {}) {
|
|
316
|
-
const opts: MultipartOptions = {};
|
|
317
|
-
if (typeof options.autoFields === 'boolean') {
|
|
318
|
-
opts.autoFields = options.autoFields;
|
|
319
|
-
}
|
|
320
|
-
if (options.limits) {
|
|
321
|
-
opts.limits = options.limits;
|
|
322
|
-
}
|
|
323
|
-
if (options.checkFile) {
|
|
324
|
-
opts.checkFile = options.checkFile;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
if (options.defCharset) {
|
|
328
|
-
opts.defCharset = options.defCharset;
|
|
329
|
-
}
|
|
330
|
-
if (options.defParamCharset) {
|
|
331
|
-
opts.defParamCharset = options.defParamCharset;
|
|
332
|
-
}
|
|
333
|
-
// compatible with config names
|
|
334
|
-
if (options.defaultCharset) {
|
|
335
|
-
opts.defCharset = options.defaultCharset;
|
|
336
|
-
}
|
|
337
|
-
if (options.defaultParamCharset) {
|
|
338
|
-
opts.defParamCharset = options.defaultParamCharset;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
// limits
|
|
342
|
-
if (options.limits) {
|
|
343
|
-
const limits: Record<string, number | undefined> = opts.limits = { ...options.limits };
|
|
344
|
-
for (const key in limits) {
|
|
345
|
-
if (key.endsWith('Size') && limits[key]) {
|
|
346
|
-
limits[key] = humanizeBytes(limits[key]);
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
return opts;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
declare module '@eggjs/core' {
|
|
355
|
-
interface Request {
|
|
356
|
-
/**
|
|
357
|
-
* Files Object Array
|
|
358
|
-
*/
|
|
359
|
-
files?: EggFile[];
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
interface Context {
|
|
363
|
-
saveRequestFiles(options?: MultipartOptions): Promise<void>;
|
|
364
|
-
getFileStream(options?: MultipartOptions): Promise<MultipartFileStream>;
|
|
365
|
-
cleanupRequestFiles(files?: EggFile[]): Promise<void>;
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { pathMatching } from 'egg-path-matching';
|
|
2
|
-
import type { Context, Next, EggCore } from '@eggjs/core';
|
|
3
|
-
import type { MultipartConfig } from '../../config/config.default.js';
|
|
4
|
-
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
6
|
-
export default (options: MultipartConfig, _app: EggCore) => {
|
|
7
|
-
// normalize options
|
|
8
|
-
const matchFn = options.fileModeMatch && pathMatching({
|
|
9
|
-
match: options.fileModeMatch,
|
|
10
|
-
// pathToRegexpModule: app.options.pathToRegexpModule,
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
return async function multipart(ctx: Context, next: Next) {
|
|
14
|
-
if (!ctx.is('multipart')) return next();
|
|
15
|
-
if (matchFn && !matchFn(ctx)) return next();
|
|
16
|
-
|
|
17
|
-
await ctx.saveRequestFiles();
|
|
18
|
-
return next();
|
|
19
|
-
};
|
|
20
|
-
};
|