@blocklet/images 1.16.33-beta-20241107-065332-b20666d2 → 1.16.33-beta-20241111-162739-f5906773
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/lib/logo-validate.d.ts +4 -3
- package/lib/logo-validate.js +24 -9
- package/lib/screenshot-validate.d.ts +1 -1
- package/lib/screenshot-validate.js +28 -20
- package/package.json +10 -3
- package/lib/get-image-size.d.ts +0 -6
- package/lib/get-image-size.js +0 -25
package/lib/logo-validate.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { ImageType } from 'image-type';
|
|
1
2
|
type ISizeKB = number;
|
|
2
3
|
interface ILogoValidateOptions {
|
|
3
4
|
maxSize?: ISizeKB;
|
|
4
|
-
|
|
5
|
-
logoType?:
|
|
5
|
+
minWidth?: number;
|
|
6
|
+
logoType?: ImageType[];
|
|
6
7
|
extractedFilepath: string;
|
|
7
8
|
}
|
|
8
|
-
export declare const validateLogo: (logoName: string, options: ILogoValidateOptions) =>
|
|
9
|
+
export declare const validateLogo: (logoName: string, options: ILogoValidateOptions) => any[];
|
|
9
10
|
export {};
|
package/lib/logo-validate.js
CHANGED
|
@@ -6,14 +6,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.validateLogo = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
|
|
9
|
+
const image_size_1 = __importDefault(require("image-size"));
|
|
10
|
+
const image_type_1 = __importDefault(require("image-type"));
|
|
11
|
+
const is_svg_1 = __importDefault(require("is-svg"));
|
|
12
|
+
const ASPECT_RATIO = 1 / 1;
|
|
13
|
+
const ASPECT_RATIO_THRESHOLD = 0.01;
|
|
14
|
+
const validateLogo = (logoName, options) => {
|
|
15
|
+
const { extractedFilepath, maxSize = 1024, minWidth = 256 } = options;
|
|
16
|
+
const logoType = options.logoType || ['png', 'jpg', 'webp', 'svg'];
|
|
12
17
|
const logoPath = path_1.default.join(extractedFilepath, logoName);
|
|
13
18
|
const preLogoMessage = `The logo(${logoName})`;
|
|
14
19
|
const errorMessages = [];
|
|
15
20
|
if (!logoName) {
|
|
16
|
-
errorMessages.push(
|
|
21
|
+
errorMessages.push('The logo field is required in blocklet.yml.');
|
|
17
22
|
return errorMessages;
|
|
18
23
|
}
|
|
19
24
|
if (!fs_1.default.existsSync(logoPath)) {
|
|
@@ -24,14 +29,24 @@ const validateLogo = async (logoName, options) => {
|
|
|
24
29
|
if (maxSize && logoStats.size > maxSize * 1024) {
|
|
25
30
|
errorMessages.push(`${preLogoMessage} size exceeds ${maxSize} KB, but got ${(logoStats.size / 1024).toFixed(2)} KB.`);
|
|
26
31
|
}
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
const isSvgFile = (0, is_svg_1.default)(fs_1.default.readFileSync(logoPath));
|
|
33
|
+
if (!isSvgFile) {
|
|
34
|
+
const imgType = (0, image_type_1.default)(fs_1.default.readFileSync(logoPath));
|
|
35
|
+
if (!imgType || !logoType.includes(imgType.ext)) {
|
|
29
36
|
errorMessages.push(`${preLogoMessage} format is not supported, expected: [${logoType}].`);
|
|
30
37
|
}
|
|
31
|
-
if (
|
|
32
|
-
errorMessages.push(`${preLogoMessage}
|
|
38
|
+
if (`.${imgType?.ext}` !== path_1.default.extname(logoPath)) {
|
|
39
|
+
errorMessages.push(`${preLogoMessage} extension is not match the real file extension [.${imgType?.ext}].`);
|
|
33
40
|
}
|
|
34
|
-
|
|
41
|
+
const logoMeta = (0, image_size_1.default)(logoPath);
|
|
42
|
+
const aspectRatio = logoMeta.width / logoMeta.height;
|
|
43
|
+
if (logoMeta.width < minWidth || logoMeta.height < minWidth) {
|
|
44
|
+
errorMessages.push(`${preLogoMessage} minimum size must be ${minWidth}x${minWidth}, but got ${logoMeta.width}x${logoMeta.height}.`);
|
|
45
|
+
}
|
|
46
|
+
if (Math.abs(aspectRatio - ASPECT_RATIO) > ASPECT_RATIO_THRESHOLD) {
|
|
47
|
+
errorMessages.push(`${preLogoMessage} aspect ratio must be approximately 1:1, but got 1:${parseFloat((logoMeta.height / logoMeta.width).toFixed(2))} (${logoMeta.width}x${logoMeta.height}).`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
35
50
|
return errorMessages;
|
|
36
51
|
};
|
|
37
52
|
exports.validateLogo = validateLogo;
|
|
@@ -7,5 +7,5 @@ interface IScreenshotValidateOptions {
|
|
|
7
7
|
minWidth?: number;
|
|
8
8
|
minHeight?: number;
|
|
9
9
|
}
|
|
10
|
-
export declare const validateScreenshots: (screenshots: string[], options: IScreenshotValidateOptions) =>
|
|
10
|
+
export declare const validateScreenshots: (screenshots: string[], options: IScreenshotValidateOptions) => any[];
|
|
11
11
|
export {};
|
|
@@ -6,11 +6,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.validateScreenshots = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const
|
|
9
|
+
const image_size_1 = __importDefault(require("image-size"));
|
|
10
|
+
const image_type_1 = __importDefault(require("image-type"));
|
|
11
|
+
const is_svg_1 = __importDefault(require("is-svg"));
|
|
10
12
|
const ordinal_suffix_1 = require("./ordinal-suffix");
|
|
11
|
-
const
|
|
12
|
-
const ASPECT_RATIO_THRESHOLD = 0.
|
|
13
|
-
const validateScreenshots =
|
|
13
|
+
const ASPECT_RATIO = 16 / 9;
|
|
14
|
+
const ASPECT_RATIO_THRESHOLD = 0.05;
|
|
15
|
+
const validateScreenshots = (screenshots, options) => {
|
|
14
16
|
const { extractedFilepath, minCount, maxCount, maxSize, minWidth, minHeight } = options;
|
|
15
17
|
const errorMessages = [];
|
|
16
18
|
const screenshotList = [];
|
|
@@ -23,40 +25,46 @@ const validateScreenshots = async (screenshots, options) => {
|
|
|
23
25
|
}
|
|
24
26
|
});
|
|
25
27
|
if (minCount && screenshotList.length < minCount) {
|
|
26
|
-
errorMessages.
|
|
28
|
+
errorMessages.unshift(`At least ${minCount} screenshots are required, but just got ${screenshotList.length}.`);
|
|
27
29
|
}
|
|
28
|
-
if (maxCount && screenshotList.length > maxCount) {
|
|
29
|
-
errorMessages.
|
|
30
|
+
else if (maxCount && screenshotList.length > maxCount) {
|
|
31
|
+
errorMessages.unshift(`At most ${maxCount} screenshots are supported, but just got ${screenshotList.length}.`);
|
|
30
32
|
}
|
|
31
33
|
const asyncErrors = {};
|
|
32
|
-
|
|
34
|
+
screenshotList.forEach((screenshot, index) => {
|
|
33
35
|
const screenshotsPath = path_1.default.join(extractedFilepath, 'screenshots', screenshot);
|
|
34
36
|
const preMessage = `The ${(0, ordinal_suffix_1.ordinalSuffix)(index + 1)}[${screenshot}] screenshot`;
|
|
35
37
|
asyncErrors[screenshot] = [];
|
|
36
38
|
if (!fs_1.default.existsSync(screenshotsPath)) {
|
|
37
39
|
asyncErrors[screenshot].push(`${preMessage} file not found.`);
|
|
38
|
-
return
|
|
40
|
+
return;
|
|
39
41
|
}
|
|
40
42
|
const screenshotsStats = fs_1.default.statSync(screenshotsPath);
|
|
41
43
|
if (maxSize && screenshotsStats.size > maxSize * 1024 * 1024) {
|
|
42
44
|
asyncErrors[screenshot].push(`${preMessage} size exceeds ${maxSize} MB, but got ${(screenshotsStats.size / 1024 / 1024).toFixed(2)} MB.`);
|
|
43
45
|
}
|
|
44
|
-
const
|
|
45
|
-
if (!
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
const isSvgFile = (0, is_svg_1.default)(fs_1.default.readFileSync(screenshotsPath));
|
|
47
|
+
if (!isSvgFile) {
|
|
48
|
+
const imgType = (0, image_type_1.default)(fs_1.default.readFileSync(screenshotsPath));
|
|
49
|
+
if (!imgType) {
|
|
50
|
+
asyncErrors[screenshot].push(`${preMessage} is not a valid image file.`);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (`.${imgType.ext}` !== path_1.default.extname(screenshotsPath)) {
|
|
54
|
+
asyncErrors[screenshot].push(`${preMessage} extension is not match the real file extension [.${imgType.ext}].`);
|
|
55
|
+
}
|
|
56
|
+
const imageMeta = (0, image_size_1.default)(screenshotsPath);
|
|
49
57
|
const aspectRatio = imageMeta.width / imageMeta.height;
|
|
50
|
-
const width = minWidth || (minHeight && minHeight *
|
|
51
|
-
const height = minHeight || (width && width /
|
|
58
|
+
const width = minWidth || (minHeight && Math.round(minHeight * ASPECT_RATIO));
|
|
59
|
+
const height = minHeight || (width && Math.round(width / ASPECT_RATIO));
|
|
52
60
|
if (imageMeta.width < width || imageMeta.height < height) {
|
|
53
61
|
asyncErrors[screenshot].push(`${preMessage} minimum size must be ${width}x${height}, but got ${imageMeta.width}x${imageMeta.height}.`);
|
|
54
62
|
}
|
|
55
|
-
if (Math.abs(aspectRatio -
|
|
56
|
-
asyncErrors[screenshot].push(`${preMessage} aspect ratio must be approximately 16:
|
|
63
|
+
if (Math.abs(aspectRatio - ASPECT_RATIO) > ASPECT_RATIO_THRESHOLD) {
|
|
64
|
+
asyncErrors[screenshot].push(`${preMessage} aspect ratio must be approximately 16:9, but got 16:${parseFloat((imageMeta.height / (imageMeta.width / 16)).toFixed(2))} (${imageMeta.width}x${imageMeta.height}).`);
|
|
57
65
|
}
|
|
58
|
-
}
|
|
59
|
-
})
|
|
66
|
+
}
|
|
67
|
+
});
|
|
60
68
|
screenshotList.forEach((screenshot) => {
|
|
61
69
|
if (asyncErrors[screenshot].length) {
|
|
62
70
|
errorMessages.push(...asyncErrors[screenshot]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blocklet/images",
|
|
3
|
-
"version": "1.16.33-beta-
|
|
3
|
+
"version": "1.16.33-beta-20241111-162739-f5906773",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "./lib/index.d.ts",
|
|
6
6
|
"license": "MIT",
|
|
@@ -26,13 +26,20 @@
|
|
|
26
26
|
"url": "git+https://github.com/ArcBlock/blocklet-server.git"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
|
+
"verify": "npm run lint:fix && npm run coverage && npm run build",
|
|
30
|
+
"lint": "eslint src tests",
|
|
31
|
+
"lint:fix": "npm run lint -- --fix",
|
|
32
|
+
"coverage": "npm run test -- --coverage",
|
|
33
|
+
"clean": "rm -fr lib",
|
|
29
34
|
"build:blocklet-images": "tsc",
|
|
30
35
|
"build": "tsc",
|
|
31
36
|
"test": "jest",
|
|
32
37
|
"watch": "nodemon -w src -e ts -x 'npm run build'"
|
|
33
38
|
},
|
|
34
39
|
"dependencies": {
|
|
35
|
-
"image-size": "^1.0.2"
|
|
40
|
+
"image-size": "^1.0.2",
|
|
41
|
+
"image-type": "^4.1.0",
|
|
42
|
+
"is-svg": "^4.3.2"
|
|
36
43
|
},
|
|
37
44
|
"devDependencies": {
|
|
38
45
|
"@arcblock/eslint-config-ts": "^0.3.3",
|
|
@@ -44,5 +51,5 @@
|
|
|
44
51
|
"ts-node": "^10.9.1",
|
|
45
52
|
"typescript": "^5.6.3"
|
|
46
53
|
},
|
|
47
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "de1be92a37cd504e82b3d963b5b4d5302ecbb887"
|
|
48
55
|
}
|
package/lib/get-image-size.d.ts
DELETED
package/lib/get-image-size.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getImageSize = void 0;
|
|
7
|
-
const image_size_1 = __importDefault(require("image-size"));
|
|
8
|
-
const getImageSize = async (path) => {
|
|
9
|
-
return new Promise((reslove, reject) => {
|
|
10
|
-
try {
|
|
11
|
-
(0, image_size_1.default)(path, (e, res) => {
|
|
12
|
-
if (e) {
|
|
13
|
-
reject(e);
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
reslove(res);
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
catch (error) {
|
|
21
|
-
reject(error);
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
};
|
|
25
|
-
exports.getImageSize = getImageSize;
|