@midscene/shared 0.7.2 → 0.7.3-beta-20241104100519.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.
- package/dist/browser/constants.js +41 -0
- package/dist/{types → browser}/fs.d.ts +2 -2
- package/dist/browser/fs.js +62 -0
- package/dist/{types → browser}/img.d.ts +23 -9
- package/dist/browser/img.js +395 -0
- package/dist/browser/index.d.ts +3 -0
- package/dist/browser/index.js +26 -0
- package/dist/browser/utils.d.ts +4 -0
- package/dist/browser/utils.js +34 -0
- package/dist/es/constants.d.ts +13 -0
- package/dist/es/fs.d.ts +14 -0
- package/dist/es/fs.js +17 -14
- package/dist/es/img.d.ts +143 -0
- package/dist/es/img.js +271 -187
- package/dist/es/index.d.ts +3 -0
- package/dist/es/index.js +1 -3
- package/dist/es/utils.d.ts +4 -0
- package/dist/es/utils.js +13 -0
- package/dist/lib/constants.d.ts +13 -0
- package/dist/lib/fs.d.ts +14 -0
- package/dist/lib/fs.js +18 -25
- package/dist/lib/img.d.ts +143 -0
- package/dist/lib/img.js +270 -185
- package/dist/lib/index.d.ts +3 -0
- package/dist/lib/index.js +1 -3
- package/dist/lib/utils.d.ts +4 -0
- package/dist/lib/utils.js +39 -0
- package/dist/types/index.d.ts +2 -2
- package/package.json +37 -20
- package/src/fs/index.ts +16 -12
- package/src/img/box-select.ts +185 -170
- package/src/img/get-jimp.ts +11 -0
- package/src/img/index.ts +2 -0
- package/src/img/info.ts +24 -6
- package/src/img/jimp.d.ts +4 -0
- package/src/img/transform.ts +31 -9
- package/src/index.ts +1 -3
- package/src/utils.ts +10 -0
- /package/dist/{types → browser}/constants.d.ts +0 -0
package/dist/es/fs.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface PkgInfo {
|
|
2
|
+
name: string;
|
|
3
|
+
version: string;
|
|
4
|
+
dir: string;
|
|
5
|
+
}
|
|
6
|
+
declare function getRunningPkgInfo(dir?: string): PkgInfo | null;
|
|
7
|
+
/**
|
|
8
|
+
* Find the nearest package.json file recursively
|
|
9
|
+
* @param {string} dir - Home directory
|
|
10
|
+
* @returns {string|null} - The most recent package.json file path or null
|
|
11
|
+
*/
|
|
12
|
+
declare function findNearestPackageJson(dir: string): string | null;
|
|
13
|
+
|
|
14
|
+
export { findNearestPackageJson, getRunningPkgInfo };
|
package/dist/es/fs.js
CHANGED
|
@@ -1,24 +1,27 @@
|
|
|
1
1
|
// src/fs/index.ts
|
|
2
|
-
import assert from "assert";
|
|
3
2
|
import { existsSync, readFileSync } from "fs";
|
|
4
3
|
import { dirname, join } from "path";
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
var pkgCacheMap = {};
|
|
5
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
6
|
+
function getRunningPkgInfo(dir) {
|
|
7
|
+
if (ifInBrowser) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
const dirToCheck = dir || process.cwd();
|
|
11
|
+
if (pkgCacheMap[dirToCheck]) {
|
|
12
|
+
return pkgCacheMap[dirToCheck];
|
|
9
13
|
}
|
|
10
|
-
const pkgDir = findNearestPackageJson(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (pkgJsonFile) {
|
|
14
|
+
const pkgDir = findNearestPackageJson(dirToCheck);
|
|
15
|
+
const pkgJsonFile = pkgDir ? join(pkgDir, "package.json") : null;
|
|
16
|
+
if (pkgDir && pkgJsonFile) {
|
|
14
17
|
const { name, version } = JSON.parse(readFileSync(pkgJsonFile, "utf-8"));
|
|
15
|
-
|
|
16
|
-
return
|
|
18
|
+
pkgCacheMap[dirToCheck] = { name, version, dir: pkgDir };
|
|
19
|
+
return pkgCacheMap[dirToCheck];
|
|
17
20
|
}
|
|
18
21
|
return {
|
|
19
|
-
name: "midscene-unknown-
|
|
22
|
+
name: "midscene-unknown-package-name",
|
|
20
23
|
version: "0.0.0",
|
|
21
|
-
dir:
|
|
24
|
+
dir: dirToCheck
|
|
22
25
|
};
|
|
23
26
|
}
|
|
24
27
|
function findNearestPackageJson(dir) {
|
|
@@ -34,5 +37,5 @@ function findNearestPackageJson(dir) {
|
|
|
34
37
|
}
|
|
35
38
|
export {
|
|
36
39
|
findNearestPackageJson,
|
|
37
|
-
|
|
40
|
+
getRunningPkgInfo
|
|
38
41
|
};
|
package/dist/es/img.d.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import Jimp from 'jimp';
|
|
3
|
+
import { NodeType } from './constants.js';
|
|
4
|
+
|
|
5
|
+
interface Size$1 {
|
|
6
|
+
width: number;
|
|
7
|
+
height: number;
|
|
8
|
+
}
|
|
9
|
+
interface ImageInfo extends Size$1 {
|
|
10
|
+
jimpImage: Jimp;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Retrieves the dimensions of an image asynchronously
|
|
14
|
+
*
|
|
15
|
+
* @param image - The image data, which can be a string path or a buffer
|
|
16
|
+
* @returns A Promise that resolves to an object containing the width and height of the image
|
|
17
|
+
* @throws Error if the image data is invalid
|
|
18
|
+
*/
|
|
19
|
+
declare function imageInfo(image: string | Buffer | Jimp): Promise<ImageInfo>;
|
|
20
|
+
/**
|
|
21
|
+
* Retrieves the dimensions of an image from a base64-encoded string
|
|
22
|
+
*
|
|
23
|
+
* @param imageBase64 - The base64-encoded image data
|
|
24
|
+
* @returns A Promise that resolves to an object containing the width and height of the image
|
|
25
|
+
* @throws Error if the image data is invalid
|
|
26
|
+
*/
|
|
27
|
+
declare function imageInfoOfBase64(imageBase64: string): Promise<ImageInfo>;
|
|
28
|
+
declare function bufferFromBase64(imageBase64: string): Promise<Buffer>;
|
|
29
|
+
/**
|
|
30
|
+
* Encodes an image file to a base64 encoded string
|
|
31
|
+
*
|
|
32
|
+
* @param image The path of the image file
|
|
33
|
+
* @param withHeader Determine whether to return data including the file header information, the default is true
|
|
34
|
+
*
|
|
35
|
+
* @returns The base64 encoded string of the image file, which may or may not include header information depending on the withHeader parameter
|
|
36
|
+
*
|
|
37
|
+
* @throws When the image type is not supported, an error will be thrown
|
|
38
|
+
*/
|
|
39
|
+
declare function base64Encoded(image: string, withHeader?: boolean): string;
|
|
40
|
+
declare function base64ToPngFormat(base64: string): string;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
/**
|
|
44
|
+
* Saves a Base64-encoded image to a file
|
|
45
|
+
*
|
|
46
|
+
* @param options - An object containing the Base64-encoded image data and the output file path
|
|
47
|
+
* @param options.base64Data - The Base64-encoded image data
|
|
48
|
+
* @param options.outputPath - The path where the image will be saved
|
|
49
|
+
* @throws Error if there is an error during the saving process
|
|
50
|
+
*/
|
|
51
|
+
declare function saveBase64Image(options: {
|
|
52
|
+
base64Data: string;
|
|
53
|
+
outputPath: string;
|
|
54
|
+
}): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Transforms an image path into a base64-encoded string
|
|
57
|
+
* @param inputPath - The path of the image file to be encoded
|
|
58
|
+
* @returns A Promise that resolves to a base64-encoded string representing the image file
|
|
59
|
+
*/
|
|
60
|
+
declare function transformImgPathToBase64(inputPath: string): Promise<string>;
|
|
61
|
+
/**
|
|
62
|
+
* Resizes an image from a base64-encoded string
|
|
63
|
+
*
|
|
64
|
+
* @param base64Data - A base64-encoded string representing the image
|
|
65
|
+
* @returns A Promise that resolves to a base64-encoded string representing the resized image
|
|
66
|
+
* @throws An error if the width or height cannot be determined from the metadata
|
|
67
|
+
*/
|
|
68
|
+
declare function resizeImg(inputData: Buffer, newSize?: {
|
|
69
|
+
width: number;
|
|
70
|
+
height: number;
|
|
71
|
+
}): Promise<Buffer>;
|
|
72
|
+
declare function resizeImgBase64(inputData: string, newSize?: {
|
|
73
|
+
width: number;
|
|
74
|
+
height: number;
|
|
75
|
+
}): Promise<string>;
|
|
76
|
+
/**
|
|
77
|
+
* Calculates new dimensions for an image while maintaining its aspect ratio.
|
|
78
|
+
*
|
|
79
|
+
* This function is designed to resize an image to fit within a specified maximum width and height
|
|
80
|
+
* while maintaining the original aspect ratio. If the original width or height exceeds the maximum
|
|
81
|
+
* dimensions, the image will be scaled down to fit.
|
|
82
|
+
*
|
|
83
|
+
* @param {number} originalWidth - The original width of the image.
|
|
84
|
+
* @param {number} originalHeight - The original height of the image.
|
|
85
|
+
* @returns {Object} An object containing the new width and height.
|
|
86
|
+
* @throws {Error} Throws an error if the width or height is not a positive number.
|
|
87
|
+
*/
|
|
88
|
+
declare function calculateNewDimensions(originalWidth: number, originalHeight: number): {
|
|
89
|
+
width: number;
|
|
90
|
+
height: number;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Trims an image and returns the trimming information, including the offset from the left and top edges, and the trimmed width and height
|
|
94
|
+
*
|
|
95
|
+
* @param image - The image to be trimmed. This can be a file path or a Buffer object containing the image data
|
|
96
|
+
* @returns A Promise that resolves to an object containing the trimming information. If the image does not need to be trimmed, this object will be null
|
|
97
|
+
*/
|
|
98
|
+
declare function trimImage(image: string | Buffer): Promise<{
|
|
99
|
+
trimOffsetLeft: number;
|
|
100
|
+
trimOffsetTop: number;
|
|
101
|
+
width: number;
|
|
102
|
+
height: number;
|
|
103
|
+
} | null>;
|
|
104
|
+
|
|
105
|
+
interface Point {
|
|
106
|
+
left: number;
|
|
107
|
+
top: number;
|
|
108
|
+
}
|
|
109
|
+
interface Size {
|
|
110
|
+
width: number;
|
|
111
|
+
height: number;
|
|
112
|
+
}
|
|
113
|
+
type Rect = Point & Size;
|
|
114
|
+
|
|
115
|
+
type ElementType = {
|
|
116
|
+
locator?: string;
|
|
117
|
+
rect: Rect;
|
|
118
|
+
center?: [number, number];
|
|
119
|
+
id?: string;
|
|
120
|
+
indexId: number;
|
|
121
|
+
attributes?: {
|
|
122
|
+
nodeType: NodeType;
|
|
123
|
+
[key: string]: string;
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
declare const compositeElementInfoImg: (options: {
|
|
127
|
+
inputImgBase64: string;
|
|
128
|
+
elementsPositionInfo: Array<ElementType>;
|
|
129
|
+
size?: {
|
|
130
|
+
width: number;
|
|
131
|
+
height: number;
|
|
132
|
+
};
|
|
133
|
+
}) => Promise<string>;
|
|
134
|
+
declare const processImageElementInfo: (options: {
|
|
135
|
+
inputImgBase64: string;
|
|
136
|
+
elementsPositionInfo: Array<ElementType>;
|
|
137
|
+
elementsPositionInfoWithoutText: Array<ElementType>;
|
|
138
|
+
}) => Promise<{
|
|
139
|
+
compositeElementInfoImgBase64: string;
|
|
140
|
+
compositeElementInfoImgWithoutTextBase64: string;
|
|
141
|
+
}>;
|
|
142
|
+
|
|
143
|
+
export { base64Encoded, base64ToPngFormat, bufferFromBase64, calculateNewDimensions, compositeElementInfoImg, imageInfo, imageInfoOfBase64, processImageElementInfo, resizeImg, resizeImgBase64, saveBase64Image, transformImgPathToBase64, trimImage };
|