@blocklet/images 1.16.33-beta-20241107-065332-b20666d2

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 ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2018-2020 ArcBlock
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,6 @@
1
+ export declare const getImageSize: (path: string) => Promise<{
2
+ width: number | undefined;
3
+ height: number | undefined;
4
+ orientation?: number;
5
+ type?: string;
6
+ }>;
@@ -0,0 +1,25 @@
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;
package/lib/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { validateLogo } from './logo-validate';
2
+ import { validateScreenshots } from './screenshot-validate';
3
+ import { ordinalSuffix } from './ordinal-suffix';
4
+ export { validateLogo, validateScreenshots, ordinalSuffix };
package/lib/index.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ordinalSuffix = exports.validateScreenshots = exports.validateLogo = void 0;
4
+ const logo_validate_1 = require("./logo-validate");
5
+ Object.defineProperty(exports, "validateLogo", { enumerable: true, get: function () { return logo_validate_1.validateLogo; } });
6
+ const screenshot_validate_1 = require("./screenshot-validate");
7
+ Object.defineProperty(exports, "validateScreenshots", { enumerable: true, get: function () { return screenshot_validate_1.validateScreenshots; } });
8
+ const ordinal_suffix_1 = require("./ordinal-suffix");
9
+ Object.defineProperty(exports, "ordinalSuffix", { enumerable: true, get: function () { return ordinal_suffix_1.ordinalSuffix; } });
@@ -0,0 +1,9 @@
1
+ type ISizeKB = number;
2
+ interface ILogoValidateOptions {
3
+ maxSize?: ISizeKB;
4
+ width?: number;
5
+ logoType?: string;
6
+ extractedFilepath: string;
7
+ }
8
+ export declare const validateLogo: (logoName: string, options: ILogoValidateOptions) => Promise<any[]>;
9
+ export {};
@@ -0,0 +1,37 @@
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.validateLogo = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const get_image_size_1 = require("./get-image-size");
10
+ const validateLogo = async (logoName, options) => {
11
+ const { extractedFilepath, maxSize = 1024, width = 256, logoType = '.png,.jpg,.jpeg,.webp' } = options;
12
+ const logoPath = path_1.default.join(extractedFilepath, logoName);
13
+ const preLogoMessage = `The logo(${logoName})`;
14
+ const errorMessages = [];
15
+ if (!logoName) {
16
+ errorMessages.push(`The logo field is required in blocklet.yml.`);
17
+ return errorMessages;
18
+ }
19
+ if (!fs_1.default.existsSync(logoPath)) {
20
+ errorMessages.push(`${preLogoMessage} file not found.`);
21
+ return errorMessages;
22
+ }
23
+ const logoStats = fs_1.default.statSync(logoPath);
24
+ if (maxSize && logoStats.size > maxSize * 1024) {
25
+ errorMessages.push(`${preLogoMessage} size exceeds ${maxSize} KB, but got ${(logoStats.size / 1024).toFixed(2)} KB.`);
26
+ }
27
+ await (0, get_image_size_1.getImageSize)(logoPath).then((imageMeta) => {
28
+ if (!logoType.split(',').includes(path_1.default.extname(logoPath).toLowerCase())) {
29
+ errorMessages.push(`${preLogoMessage} format is not supported, expected: [${logoType}].`);
30
+ }
31
+ if (imageMeta.width !== imageMeta.height || imageMeta.width !== width) {
32
+ errorMessages.push(`${preLogoMessage} just supports ${width}x${width}, but got ${imageMeta.width}x${imageMeta.height}.`);
33
+ }
34
+ });
35
+ return errorMessages;
36
+ };
37
+ exports.validateLogo = validateLogo;
@@ -0,0 +1 @@
1
+ export declare function ordinalSuffix(index: number): string;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ordinalSuffix = ordinalSuffix;
4
+ function ordinalSuffix(index) {
5
+ const j = index % 10;
6
+ const k = index % 100;
7
+ if (j === 1 && k !== 11)
8
+ return `${index}st`;
9
+ if (j === 2 && k !== 12)
10
+ return `${index}nd`;
11
+ if (j === 3 && k !== 13)
12
+ return `${index}rd`;
13
+ return `${index}th`;
14
+ }
@@ -0,0 +1,11 @@
1
+ type ISizeMB = number;
2
+ interface IScreenshotValidateOptions {
3
+ extractedFilepath: string;
4
+ minCount?: number;
5
+ maxCount?: number;
6
+ maxSize?: ISizeMB;
7
+ minWidth?: number;
8
+ minHeight?: number;
9
+ }
10
+ export declare const validateScreenshots: (screenshots: string[], options: IScreenshotValidateOptions) => Promise<any[]>;
11
+ export {};
@@ -0,0 +1,67 @@
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.validateScreenshots = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const get_image_size_1 = require("./get-image-size");
10
+ const ordinal_suffix_1 = require("./ordinal-suffix");
11
+ const ASPECT_RATIO_16_10 = 16 / 10;
12
+ const ASPECT_RATIO_THRESHOLD = 0.01;
13
+ const validateScreenshots = async (screenshots, options) => {
14
+ const { extractedFilepath, minCount, maxCount, maxSize, minWidth, minHeight } = options;
15
+ const errorMessages = [];
16
+ const screenshotList = [];
17
+ screenshots.forEach((screenshot, index) => {
18
+ if (screenshotList.includes(screenshot)) {
19
+ errorMessages.push(`The ${(0, ordinal_suffix_1.ordinalSuffix)(index + 1)} screenshot(${screenshot}) is duplicated with the ${(0, ordinal_suffix_1.ordinalSuffix)(screenshotList.indexOf(screenshot) + 1)} screenshot.`);
20
+ }
21
+ else {
22
+ screenshotList.push(screenshot);
23
+ }
24
+ });
25
+ if (minCount && screenshotList.length < minCount) {
26
+ errorMessages.push(`At least ${minCount} screenshots are required, but just got ${screenshotList.length}.`);
27
+ }
28
+ if (maxCount && screenshotList.length > maxCount) {
29
+ errorMessages.push(`At most ${maxCount} screenshots are supported, but just got ${screenshotList.length}.`);
30
+ }
31
+ const asyncErrors = {};
32
+ await Promise.all(screenshotList.map((screenshot, index) => {
33
+ const screenshotsPath = path_1.default.join(extractedFilepath, 'screenshots', screenshot);
34
+ const preMessage = `The ${(0, ordinal_suffix_1.ordinalSuffix)(index + 1)}[${screenshot}] screenshot`;
35
+ asyncErrors[screenshot] = [];
36
+ if (!fs_1.default.existsSync(screenshotsPath)) {
37
+ asyncErrors[screenshot].push(`${preMessage} file not found.`);
38
+ return Promise.resolve('');
39
+ }
40
+ const screenshotsStats = fs_1.default.statSync(screenshotsPath);
41
+ if (maxSize && screenshotsStats.size > maxSize * 1024 * 1024) {
42
+ asyncErrors[screenshot].push(`${preMessage} size exceeds ${maxSize} MB, but got ${(screenshotsStats.size / 1024 / 1024).toFixed(2)} MB.`);
43
+ }
44
+ const screenshotsImageType = ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp', '.svg'];
45
+ if (!screenshotsImageType.includes(path_1.default.extname(screenshotsPath).toLowerCase())) {
46
+ asyncErrors[screenshot].push(`${preMessage} format is not supported, expected: [${screenshotsImageType.join(', ')}].`);
47
+ }
48
+ return (0, get_image_size_1.getImageSize)(screenshotsPath).then((imageMeta) => {
49
+ const aspectRatio = imageMeta.width / imageMeta.height;
50
+ const width = minWidth || (minHeight && minHeight * ASPECT_RATIO_16_10);
51
+ const height = minHeight || (width && width / ASPECT_RATIO_16_10);
52
+ if (imageMeta.width < width || imageMeta.height < height) {
53
+ asyncErrors[screenshot].push(`${preMessage} minimum size must be ${width}x${height}, but got ${imageMeta.width}x${imageMeta.height}.`);
54
+ }
55
+ if (Math.abs(aspectRatio - ASPECT_RATIO_16_10) > ASPECT_RATIO_THRESHOLD) {
56
+ asyncErrors[screenshot].push(`${preMessage} aspect ratio must be approximately 16:10, but got 16:${parseFloat((imageMeta.height / (imageMeta.width / 16)).toFixed(2))} (${imageMeta.width}x${imageMeta.height}).`);
57
+ }
58
+ });
59
+ }));
60
+ screenshotList.forEach((screenshot) => {
61
+ if (asyncErrors[screenshot].length) {
62
+ errorMessages.push(...asyncErrors[screenshot]);
63
+ }
64
+ });
65
+ return errorMessages;
66
+ };
67
+ exports.validateScreenshots = validateScreenshots;
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@blocklet/images",
3
+ "version": "1.16.33-beta-20241107-065332-b20666d2",
4
+ "main": "lib/index.js",
5
+ "types": "./lib/index.d.ts",
6
+ "license": "MIT",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "files": [
11
+ "lib"
12
+ ],
13
+ "bugs": {
14
+ "url": "https://github.com/ArcBlock/blocklet-server/issues"
15
+ },
16
+ "description": "support functions for blocklet image validation",
17
+ "keywords": [
18
+ "blocklet",
19
+ "image",
20
+ "validation"
21
+ ],
22
+ "author": "blocklet<blocklet@arcblock.io>",
23
+ "homepage": "https://github.com/ArcBlock/blocklet-server#readme",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/ArcBlock/blocklet-server.git"
27
+ },
28
+ "scripts": {
29
+ "build:blocklet-images": "tsc",
30
+ "build": "tsc",
31
+ "test": "jest",
32
+ "watch": "nodemon -w src -e ts -x 'npm run build'"
33
+ },
34
+ "dependencies": {
35
+ "image-size": "^1.0.2"
36
+ },
37
+ "devDependencies": {
38
+ "@arcblock/eslint-config-ts": "^0.3.3",
39
+ "@types/jest": "^29.5.14",
40
+ "eslint": "^8.27.0",
41
+ "jest": "^29.7.0",
42
+ "prettier": "^3.3.2",
43
+ "ts-jest": "^29.2.5",
44
+ "ts-node": "^10.9.1",
45
+ "typescript": "^5.6.3"
46
+ },
47
+ "gitHead": "cfc4d5837f83a0bfd507567915695894dda2c0ec"
48
+ }