@awesomeness-js/utils 1.1.20 → 1.1.21
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/package.json +1 -1
- package/src/clean/buffer.js +74 -0
- package/src/thingType.js +13 -0
- package/src/utils/clean.js +3 -2
- package/src/validateSchema.js +1 -1
- package/src/clean/file.js +0 -62
package/package.json
CHANGED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export default function cleanBuffer(fileObj, {
|
|
2
|
+
required = false,
|
|
3
|
+
validTypes = [], // ['image/png', '.jpg']
|
|
4
|
+
minSize = false, // bytes
|
|
5
|
+
maxSize = false, // bytes
|
|
6
|
+
} = {}) {
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
if (
|
|
10
|
+
!fileObj ||
|
|
11
|
+
typeof fileObj !== 'object' ||
|
|
12
|
+
!(fileObj.buffer instanceof Buffer)
|
|
13
|
+
) {
|
|
14
|
+
throw {
|
|
15
|
+
message: 'Invalid buffer object (expected Busboy fileObj)',
|
|
16
|
+
value: fileObj
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const { buffer, filename, mimeType, totalSize } = fileObj;
|
|
21
|
+
const size = totalSize ?? buffer.length;
|
|
22
|
+
|
|
23
|
+
// basic fields check
|
|
24
|
+
if (typeof size !== 'number' || !mimeType) {
|
|
25
|
+
throw {
|
|
26
|
+
message: 'Missing required fields (mimeType, size)',
|
|
27
|
+
value: fileObj
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// type validation
|
|
32
|
+
if (Array.isArray(validTypes) && validTypes.length > 0) {
|
|
33
|
+
const isValidType = validTypes.some(t =>
|
|
34
|
+
mimeType === t || (filename && filename.endsWith(t))
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
if (!isValidType) {
|
|
38
|
+
throw {
|
|
39
|
+
message: `Invalid buffer type: ${mimeType}`,
|
|
40
|
+
allowed: validTypes,
|
|
41
|
+
value: filename || null
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// size validation
|
|
47
|
+
if (minSize !== false && size < minSize) {
|
|
48
|
+
throw {
|
|
49
|
+
message: `Buffer smaller than minimum size (${minSize} bytes)`,
|
|
50
|
+
value: size
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (maxSize !== false && size > maxSize) {
|
|
55
|
+
throw {
|
|
56
|
+
message: `Buffer larger than maximum size (${maxSize} bytes)`,
|
|
57
|
+
value: size
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// normalized safe object
|
|
62
|
+
return {
|
|
63
|
+
name: filename || null,
|
|
64
|
+
type: mimeType,
|
|
65
|
+
size,
|
|
66
|
+
encoding: fileObj.encoding || null,
|
|
67
|
+
createdAt: new Date().toISOString(),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
} catch (e) {
|
|
71
|
+
if (required) throw e;
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
package/src/thingType.js
CHANGED
|
@@ -29,6 +29,19 @@ export default (thing) => {
|
|
|
29
29
|
if (type === 'object' && thing !== null) {
|
|
30
30
|
|
|
31
31
|
type = Array.isArray(thing) ? 'array' : 'object';
|
|
32
|
+
|
|
33
|
+
if(type === 'object'){
|
|
34
|
+
|
|
35
|
+
// is this a file / buffer
|
|
36
|
+
const isBuffer = thing instanceof Buffer || thing instanceof ArrayBuffer || thing instanceof Blob;
|
|
37
|
+
|
|
38
|
+
if(isBuffer){
|
|
39
|
+
|
|
40
|
+
type = 'buffer';
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
}
|
|
32
45
|
|
|
33
46
|
return type;
|
|
34
47
|
|
package/src/utils/clean.js
CHANGED
|
@@ -10,14 +10,15 @@ import cleanTimestamp from '../clean/timestamp.js';
|
|
|
10
10
|
import cleanUUID from '../clean/uuid.js';
|
|
11
11
|
|
|
12
12
|
const knownTypesToClean = [
|
|
13
|
+
'array',
|
|
13
14
|
'boolean',
|
|
15
|
+
'buffer',
|
|
14
16
|
'integer',
|
|
15
17
|
'number',
|
|
18
|
+
'object',
|
|
16
19
|
'string',
|
|
17
20
|
'timestamp',
|
|
18
21
|
'uuid',
|
|
19
|
-
'object',
|
|
20
|
-
'array'
|
|
21
22
|
];
|
|
22
23
|
|
|
23
24
|
function cleanArray(arr, schema = {}, {
|
package/src/validateSchema.js
CHANGED
package/src/clean/file.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
export default function cleanFile(file, {
|
|
2
|
-
required = false,
|
|
3
|
-
validTypes = [],
|
|
4
|
-
minSize = false, // bytes
|
|
5
|
-
maxSize = false, // bytes
|
|
6
|
-
} = {}) {
|
|
7
|
-
|
|
8
|
-
try {
|
|
9
|
-
if (!file || typeof file !== 'object') {
|
|
10
|
-
throw {
|
|
11
|
-
message: 'File must be an object (like req.file or File API object)',
|
|
12
|
-
value: file
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (typeof file.size !== 'number' || typeof file.type !== 'string') {
|
|
17
|
-
throw {
|
|
18
|
-
message: 'File object missing required fields (size, type)',
|
|
19
|
-
value: file
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// type validation
|
|
24
|
-
if (Array.isArray(validTypes) && validTypes.length > 0) {
|
|
25
|
-
const isValidType = validTypes.some(t => file.type === t || file.name?.endsWith(t));
|
|
26
|
-
if (!isValidType) {
|
|
27
|
-
throw {
|
|
28
|
-
message: `Invalid file type: ${file.type}`,
|
|
29
|
-
allowed: validTypes,
|
|
30
|
-
value: file.name || null
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// size validation
|
|
36
|
-
if (minSize !== false && file.size < minSize) {
|
|
37
|
-
throw {
|
|
38
|
-
message: `File is smaller than minimum size (${minSize} bytes)`,
|
|
39
|
-
value: file.size
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (maxSize !== false && file.size > maxSize) {
|
|
44
|
-
throw {
|
|
45
|
-
message: `File is larger than maximum size (${maxSize} bytes)`,
|
|
46
|
-
value: file.size
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// return a normalized safe file info object
|
|
51
|
-
return {
|
|
52
|
-
name: file.name || null,
|
|
53
|
-
type: file.type,
|
|
54
|
-
size: file.size,
|
|
55
|
-
lastModified: file.lastModified ? new Date(file.lastModified).toISOString() : null,
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
} catch (e) {
|
|
59
|
-
if (required) throw e;
|
|
60
|
-
else return null;
|
|
61
|
-
}
|
|
62
|
-
}
|