@midscene/shared 0.7.1 → 0.7.2-beta-20241024094141.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 +73 -0
- package/dist/{types → browser}/img.d.ts +3 -1
- package/dist/browser/img.js +366 -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 +7 -3
- package/dist/es/img.d.ts +131 -0
- package/dist/es/img.js +144 -89
- 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 +8 -4
- package/dist/lib/img.d.ts +131 -0
- package/dist/lib/img.js +142 -87
- 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 +41 -17
- package/src/fs/index.ts +6 -2
- package/src/img/box-select.ts +8 -5
- package/src/img/get-jimp.ts +7 -0
- package/src/img/index.ts +1 -0
- package/src/img/info.ts +11 -4
- package/src/img/jimp.d.ts +4 -0
- package/src/img/transform.ts +6 -1
- package/src/index.ts +1 -3
- package/src/utils.ts +10 -0
- /package/dist/{types → browser}/constants.d.ts +0 -0
package/dist/es/img.d.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { NodeType } from './constants.js';
|
|
3
|
+
|
|
4
|
+
interface Size$1 {
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Retrieves the dimensions of an image asynchronously
|
|
10
|
+
*
|
|
11
|
+
* @param image - The image data, which can be a string path or a buffer
|
|
12
|
+
* @returns A Promise that resolves to an object containing the width and height of the image
|
|
13
|
+
* @throws Error if the image data is invalid
|
|
14
|
+
*/
|
|
15
|
+
declare function imageInfo(image: string | Buffer): Promise<Size$1>;
|
|
16
|
+
/**
|
|
17
|
+
* Retrieves the dimensions of an image from a base64-encoded string
|
|
18
|
+
*
|
|
19
|
+
* @param imageBase64 - The base64-encoded image data
|
|
20
|
+
* @returns A Promise that resolves to an object containing the width and height of the image
|
|
21
|
+
* @throws Error if the image data is invalid
|
|
22
|
+
*/
|
|
23
|
+
declare function imageInfoOfBase64(imageBase64: string): Promise<Size$1>;
|
|
24
|
+
declare function bufferFromBase64(imageBase64: string): Promise<Buffer>;
|
|
25
|
+
/**
|
|
26
|
+
* Encodes an image file to a base64 encoded string
|
|
27
|
+
*
|
|
28
|
+
* @param image The path of the image file
|
|
29
|
+
* @param withHeader Determine whether to return data including the file header information, the default is true
|
|
30
|
+
*
|
|
31
|
+
* @returns The base64 encoded string of the image file, which may or may not include header information depending on the withHeader parameter
|
|
32
|
+
*
|
|
33
|
+
* @throws When the image type is not supported, an error will be thrown
|
|
34
|
+
*/
|
|
35
|
+
declare function base64Encoded(image: string, withHeader?: boolean): string;
|
|
36
|
+
declare function base64ToPngFormat(base64: string): string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
/**
|
|
40
|
+
* Saves a Base64-encoded image to a file
|
|
41
|
+
*
|
|
42
|
+
* @param options - An object containing the Base64-encoded image data and the output file path
|
|
43
|
+
* @param options.base64Data - The Base64-encoded image data
|
|
44
|
+
* @param options.outputPath - The path where the image will be saved
|
|
45
|
+
* @throws Error if there is an error during the saving process
|
|
46
|
+
*/
|
|
47
|
+
declare function saveBase64Image(options: {
|
|
48
|
+
base64Data: string;
|
|
49
|
+
outputPath: string;
|
|
50
|
+
}): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Transforms an image path into a base64-encoded string
|
|
53
|
+
* @param inputPath - The path of the image file to be encoded
|
|
54
|
+
* @returns A Promise that resolves to a base64-encoded string representing the image file
|
|
55
|
+
*/
|
|
56
|
+
declare function transformImgPathToBase64(inputPath: string): Promise<string>;
|
|
57
|
+
/**
|
|
58
|
+
* Resizes an image from a base64-encoded string
|
|
59
|
+
*
|
|
60
|
+
* @param base64Data - A base64-encoded string representing the image
|
|
61
|
+
* @returns A Promise that resolves to a base64-encoded string representing the resized image
|
|
62
|
+
* @throws An error if the width or height cannot be determined from the metadata
|
|
63
|
+
*/
|
|
64
|
+
declare function resizeImg(inputData: string | Buffer, newSize?: {
|
|
65
|
+
width: number;
|
|
66
|
+
height: number;
|
|
67
|
+
}): Promise<string | Buffer>;
|
|
68
|
+
/**
|
|
69
|
+
* Calculates new dimensions for an image while maintaining its aspect ratio.
|
|
70
|
+
*
|
|
71
|
+
* This function is designed to resize an image to fit within a specified maximum width and height
|
|
72
|
+
* while maintaining the original aspect ratio. If the original width or height exceeds the maximum
|
|
73
|
+
* dimensions, the image will be scaled down to fit.
|
|
74
|
+
*
|
|
75
|
+
* @param {number} originalWidth - The original width of the image.
|
|
76
|
+
* @param {number} originalHeight - The original height of the image.
|
|
77
|
+
* @returns {Object} An object containing the new width and height.
|
|
78
|
+
* @throws {Error} Throws an error if the width or height is not a positive number.
|
|
79
|
+
*/
|
|
80
|
+
declare function calculateNewDimensions(originalWidth: number, originalHeight: number): {
|
|
81
|
+
width: number;
|
|
82
|
+
height: number;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Trims an image and returns the trimming information, including the offset from the left and top edges, and the trimmed width and height
|
|
86
|
+
*
|
|
87
|
+
* @param image - The image to be trimmed. This can be a file path or a Buffer object containing the image data
|
|
88
|
+
* @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
|
|
89
|
+
*/
|
|
90
|
+
declare function trimImage(image: string | Buffer): Promise<{
|
|
91
|
+
trimOffsetLeft: number;
|
|
92
|
+
trimOffsetTop: number;
|
|
93
|
+
width: number;
|
|
94
|
+
height: number;
|
|
95
|
+
} | null>;
|
|
96
|
+
|
|
97
|
+
interface Point {
|
|
98
|
+
left: number;
|
|
99
|
+
top: number;
|
|
100
|
+
}
|
|
101
|
+
interface Size {
|
|
102
|
+
width: number;
|
|
103
|
+
height: number;
|
|
104
|
+
}
|
|
105
|
+
type Rect = Point & Size;
|
|
106
|
+
|
|
107
|
+
type ElementType = {
|
|
108
|
+
locator: string;
|
|
109
|
+
rect: Rect;
|
|
110
|
+
center: [number, number];
|
|
111
|
+
id: string;
|
|
112
|
+
indexId: number;
|
|
113
|
+
attributes: {
|
|
114
|
+
nodeType: NodeType;
|
|
115
|
+
[key: string]: string;
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
declare const compositeElementInfoImg: (options: {
|
|
119
|
+
inputImgBase64: string;
|
|
120
|
+
elementsPositionInfo: Array<ElementType>;
|
|
121
|
+
}) => Promise<string>;
|
|
122
|
+
declare const processImageElementInfo: (options: {
|
|
123
|
+
inputImgBase64: string;
|
|
124
|
+
elementsPositionInfo: Array<ElementType>;
|
|
125
|
+
elementsPositionInfoWithoutText: Array<ElementType>;
|
|
126
|
+
}) => Promise<{
|
|
127
|
+
compositeElementInfoImgBase64: string;
|
|
128
|
+
compositeElementInfoImgWithoutTextBase64: string;
|
|
129
|
+
}>;
|
|
130
|
+
|
|
131
|
+
export { base64Encoded, base64ToPngFormat, bufferFromBase64, calculateNewDimensions, compositeElementInfoImg, imageInfo, imageInfoOfBase64, processImageElementInfo, resizeImg, saveBase64Image, transformImgPathToBase64, trimImage };
|
package/dist/es/img.js
CHANGED
|
@@ -1,27 +1,71 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
1
22
|
// src/img/info.ts
|
|
2
23
|
import assert from "assert";
|
|
3
|
-
import { Buffer } from "buffer";
|
|
24
|
+
import { Buffer as Buffer2 } from "buffer";
|
|
4
25
|
import { readFileSync } from "fs";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
const { width, height } = jimpImage.bitmap;
|
|
16
|
-
assert(
|
|
17
|
-
width && height,
|
|
18
|
-
`Invalid image: ${typeof image === "string" ? image : "Buffer"}`
|
|
19
|
-
);
|
|
20
|
-
return { width, height };
|
|
26
|
+
|
|
27
|
+
// src/img/get-jimp.ts
|
|
28
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
29
|
+
function getJimp() {
|
|
30
|
+
return __async(this, null, function* () {
|
|
31
|
+
if (ifInBrowser) {
|
|
32
|
+
return (yield import("jimp/browser/lib/jimp.js")).default;
|
|
33
|
+
}
|
|
34
|
+
return (yield import("jimp")).default;
|
|
35
|
+
});
|
|
21
36
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
37
|
+
|
|
38
|
+
// src/img/info.ts
|
|
39
|
+
function imageInfo(image) {
|
|
40
|
+
return __async(this, null, function* () {
|
|
41
|
+
const Jimp = yield getJimp();
|
|
42
|
+
let jimpImage;
|
|
43
|
+
if (typeof image === "string") {
|
|
44
|
+
jimpImage = yield Jimp.read(image);
|
|
45
|
+
} else if (Buffer2.isBuffer(image)) {
|
|
46
|
+
jimpImage = yield Jimp.read(image);
|
|
47
|
+
} else {
|
|
48
|
+
throw new Error("Invalid image input: must be a string path or a Buffer");
|
|
49
|
+
}
|
|
50
|
+
const { width, height } = jimpImage.bitmap;
|
|
51
|
+
assert(
|
|
52
|
+
width && height,
|
|
53
|
+
`Invalid image: ${typeof image === "string" ? image : "Buffer"}`
|
|
54
|
+
);
|
|
55
|
+
return { width, height };
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function imageInfoOfBase64(imageBase64) {
|
|
59
|
+
return __async(this, null, function* () {
|
|
60
|
+
const buffer = yield bufferFromBase64(imageBase64);
|
|
61
|
+
return imageInfo(buffer);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function bufferFromBase64(imageBase64) {
|
|
65
|
+
return __async(this, null, function* () {
|
|
66
|
+
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, "");
|
|
67
|
+
return Buffer2.from(base64Data, "base64");
|
|
68
|
+
});
|
|
25
69
|
}
|
|
26
70
|
function base64Encoded(image, withHeader = true) {
|
|
27
71
|
const imageBuffer = readFileSync(image);
|
|
@@ -41,32 +85,40 @@ function base64ToPngFormat(base64) {
|
|
|
41
85
|
}
|
|
42
86
|
|
|
43
87
|
// src/img/transform.ts
|
|
44
|
-
import { Buffer as
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
88
|
+
import { Buffer as Buffer3 } from "buffer";
|
|
89
|
+
function saveBase64Image(options) {
|
|
90
|
+
return __async(this, null, function* () {
|
|
91
|
+
const { base64Data, outputPath } = options;
|
|
92
|
+
const base64Image = base64Data.split(";base64,").pop() || base64Data;
|
|
93
|
+
const imageBuffer = Buffer3.from(base64Image, "base64");
|
|
94
|
+
const Jimp = yield getJimp();
|
|
95
|
+
const image = yield Jimp.read(imageBuffer);
|
|
96
|
+
yield image.writeAsync(outputPath);
|
|
97
|
+
});
|
|
52
98
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
99
|
+
function transformImgPathToBase64(inputPath) {
|
|
100
|
+
return __async(this, null, function* () {
|
|
101
|
+
const Jimp = yield getJimp();
|
|
102
|
+
const image = yield Jimp.read(inputPath);
|
|
103
|
+
const buffer = yield image.getBufferAsync(Jimp.MIME_PNG);
|
|
104
|
+
return buffer.toString("base64");
|
|
105
|
+
});
|
|
57
106
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
107
|
+
function resizeImg(inputData, newSize) {
|
|
108
|
+
return __async(this, null, function* () {
|
|
109
|
+
const isBase64 = typeof inputData === "string";
|
|
110
|
+
const imageBuffer = isBase64 ? Buffer3.from(inputData.split(";base64,").pop() || inputData, "base64") : inputData;
|
|
111
|
+
const Jimp = yield getJimp();
|
|
112
|
+
const image = yield Jimp.read(imageBuffer);
|
|
113
|
+
const { width, height } = image.bitmap;
|
|
114
|
+
if (!width || !height) {
|
|
115
|
+
throw Error("Undefined width or height from the input image.");
|
|
116
|
+
}
|
|
117
|
+
const finalNewSize = newSize || calculateNewDimensions(width, height);
|
|
118
|
+
image.resize(finalNewSize.width, finalNewSize.height);
|
|
119
|
+
const resizedBuffer = yield image.getBufferAsync(Jimp.MIME_PNG);
|
|
120
|
+
return isBase64 ? resizedBuffer.toString("base64") : resizedBuffer;
|
|
121
|
+
});
|
|
70
122
|
}
|
|
71
123
|
function calculateNewDimensions(originalWidth, originalHeight) {
|
|
72
124
|
const maxWidth = 2048;
|
|
@@ -87,36 +139,38 @@ function calculateNewDimensions(originalWidth, originalHeight) {
|
|
|
87
139
|
height: Math.round(newHeight)
|
|
88
140
|
};
|
|
89
141
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
142
|
+
function trimImage(image) {
|
|
143
|
+
return __async(this, null, function* () {
|
|
144
|
+
const Jimp = yield getJimp();
|
|
145
|
+
const jimpImage = yield Jimp.read(
|
|
146
|
+
Buffer3.isBuffer(image) ? image : Buffer3.from(image)
|
|
147
|
+
);
|
|
148
|
+
const { width, height } = jimpImage.bitmap;
|
|
149
|
+
if (width <= 3 || height <= 3) {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
const trimmedImage = jimpImage.autocrop();
|
|
153
|
+
const { width: trimmedWidth, height: trimmedHeight } = trimmedImage.bitmap;
|
|
154
|
+
const trimOffsetLeft = (width - trimmedWidth) / 2;
|
|
155
|
+
const trimOffsetTop = (height - trimmedHeight) / 2;
|
|
156
|
+
if (trimOffsetLeft === 0 && trimOffsetTop === 0) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
trimOffsetLeft: -trimOffsetLeft,
|
|
161
|
+
trimOffsetTop: -trimOffsetTop,
|
|
162
|
+
width: trimmedWidth,
|
|
163
|
+
height: trimmedHeight
|
|
164
|
+
};
|
|
165
|
+
});
|
|
111
166
|
}
|
|
112
167
|
|
|
113
168
|
// src/img/box-select.ts
|
|
114
169
|
import assert2 from "assert";
|
|
115
|
-
import { Buffer as Buffer3 } from "buffer";
|
|
116
|
-
import Jimp3 from "jimp";
|
|
117
170
|
var createSvgOverlay = (elements, imageWidth, imageHeight) => {
|
|
118
|
-
const createPngOverlay =
|
|
119
|
-
const
|
|
171
|
+
const createPngOverlay = (elements2, imageWidth2, imageHeight2) => __async(void 0, null, function* () {
|
|
172
|
+
const Jimp = yield getJimp();
|
|
173
|
+
const image = new Jimp(imageWidth2, imageHeight2, 0);
|
|
120
174
|
const colors = [
|
|
121
175
|
{ rect: 4278190335, text: 4294967295 }
|
|
122
176
|
// red, white
|
|
@@ -197,59 +251,59 @@ var createSvgOverlay = (elements, imageWidth, imageHeight) => {
|
|
|
197
251
|
this.bitmap.data[idx + 2] = color.rect >> 8 & 255;
|
|
198
252
|
this.bitmap.data[idx + 3] = color.rect & 255;
|
|
199
253
|
});
|
|
200
|
-
const font =
|
|
254
|
+
const font = yield Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
|
|
201
255
|
image.print(
|
|
202
256
|
font,
|
|
203
257
|
rectX,
|
|
204
258
|
rectY,
|
|
205
259
|
{
|
|
206
260
|
text: element.indexId.toString(),
|
|
207
|
-
alignmentX:
|
|
208
|
-
alignmentY:
|
|
261
|
+
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
|
|
262
|
+
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
|
|
209
263
|
},
|
|
210
264
|
rectWidth,
|
|
211
265
|
rectHeight
|
|
212
266
|
);
|
|
213
267
|
}
|
|
214
|
-
return image.getBufferAsync(
|
|
215
|
-
};
|
|
268
|
+
return image.getBufferAsync(Jimp.MIME_PNG);
|
|
269
|
+
});
|
|
216
270
|
return createPngOverlay(elements, imageWidth, imageHeight);
|
|
217
271
|
};
|
|
218
|
-
var compositeElementInfoImg =
|
|
272
|
+
var compositeElementInfoImg = (options) => __async(void 0, null, function* () {
|
|
273
|
+
const Jimp = yield getJimp();
|
|
219
274
|
const { inputImgBase64, elementsPositionInfo } = options;
|
|
220
|
-
const imageBuffer =
|
|
221
|
-
const
|
|
222
|
-
const { width, height } = image.bitmap;
|
|
275
|
+
const imageBuffer = yield bufferFromBase64(inputImgBase64);
|
|
276
|
+
const { width, height } = yield imageInfo(imageBuffer);
|
|
223
277
|
if (!width || !height) {
|
|
224
278
|
throw Error("Image processing failed because width or height is undefined");
|
|
225
279
|
}
|
|
226
|
-
const svgOverlay =
|
|
280
|
+
const svgOverlay = yield createSvgOverlay(
|
|
227
281
|
elementsPositionInfo,
|
|
228
282
|
width,
|
|
229
283
|
height
|
|
230
284
|
);
|
|
231
|
-
return
|
|
232
|
-
const svgImage =
|
|
233
|
-
return
|
|
234
|
-
mode:
|
|
285
|
+
return yield Jimp.read(imageBuffer).then((image) => __async(void 0, null, function* () {
|
|
286
|
+
const svgImage = yield Jimp.read(svgOverlay);
|
|
287
|
+
return image.composite(svgImage, 0, 0, {
|
|
288
|
+
mode: Jimp.BLEND_SOURCE_OVER,
|
|
235
289
|
opacitySource: 1,
|
|
236
290
|
opacityDest: 1
|
|
237
291
|
});
|
|
238
|
-
}).then((compositeImage) => {
|
|
239
|
-
return compositeImage.getBufferAsync(
|
|
292
|
+
})).then((compositeImage) => {
|
|
293
|
+
return compositeImage.getBufferAsync(Jimp.MIME_PNG);
|
|
240
294
|
}).then((buffer) => {
|
|
241
295
|
return buffer.toString("base64");
|
|
242
296
|
}).catch((error) => {
|
|
243
297
|
throw error;
|
|
244
298
|
});
|
|
245
|
-
};
|
|
246
|
-
var processImageElementInfo =
|
|
299
|
+
});
|
|
300
|
+
var processImageElementInfo = (options) => __async(void 0, null, function* () {
|
|
247
301
|
const base64Image = options.inputImgBase64.split(";base64,").pop();
|
|
248
302
|
assert2(base64Image, "base64Image is undefined");
|
|
249
303
|
const [
|
|
250
304
|
compositeElementInfoImgBase64,
|
|
251
305
|
compositeElementInfoImgWithoutTextBase64
|
|
252
|
-
] =
|
|
306
|
+
] = yield Promise.all([
|
|
253
307
|
compositeElementInfoImg({
|
|
254
308
|
inputImgBase64: options.inputImgBase64,
|
|
255
309
|
elementsPositionInfo: options.elementsPositionInfo
|
|
@@ -263,10 +317,11 @@ var processImageElementInfo = async (options) => {
|
|
|
263
317
|
compositeElementInfoImgBase64,
|
|
264
318
|
compositeElementInfoImgWithoutTextBase64
|
|
265
319
|
};
|
|
266
|
-
};
|
|
320
|
+
});
|
|
267
321
|
export {
|
|
268
322
|
base64Encoded,
|
|
269
323
|
base64ToPngFormat,
|
|
324
|
+
bufferFromBase64,
|
|
270
325
|
calculateNewDimensions,
|
|
271
326
|
compositeElementInfoImg,
|
|
272
327
|
imageInfo,
|
package/dist/es/index.js
CHANGED
package/dist/es/utils.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/utils.ts
|
|
2
|
+
import { randomUUID } from "crypto";
|
|
3
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
4
|
+
function uuid() {
|
|
5
|
+
if (ifInBrowser) {
|
|
6
|
+
return Math.random().toString(36).substring(2, 15);
|
|
7
|
+
}
|
|
8
|
+
return randomUUID();
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
ifInBrowser,
|
|
12
|
+
uuid
|
|
13
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare const TEXT_SIZE_THRESHOLD = 9;
|
|
2
|
+
declare const TEXT_MAX_SIZE = 40;
|
|
3
|
+
declare const CONTAINER_MINI_HEIGHT = 3;
|
|
4
|
+
declare const CONTAINER_MINI_WIDTH = 3;
|
|
5
|
+
declare enum NodeType {
|
|
6
|
+
CONTAINER = "CONTAINER Node",
|
|
7
|
+
FORM_ITEM = "FORM_ITEM Node",
|
|
8
|
+
BUTTON = "BUTTON Node",
|
|
9
|
+
IMG = "IMG Node",
|
|
10
|
+
TEXT = "TEXT Node"
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { CONTAINER_MINI_HEIGHT, CONTAINER_MINI_WIDTH, NodeType, TEXT_MAX_SIZE, TEXT_SIZE_THRESHOLD };
|
package/dist/lib/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/lib/fs.js
CHANGED
|
@@ -31,14 +31,18 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var fs_exports = {};
|
|
32
32
|
__export(fs_exports, {
|
|
33
33
|
findNearestPackageJson: () => findNearestPackageJson,
|
|
34
|
-
|
|
34
|
+
getRunningPkgInfo: () => getRunningPkgInfo
|
|
35
35
|
});
|
|
36
36
|
module.exports = __toCommonJS(fs_exports);
|
|
37
37
|
var import_node_assert = __toESM(require("assert"));
|
|
38
38
|
var import_node_fs = require("fs");
|
|
39
39
|
var import_node_path = require("path");
|
|
40
40
|
var pkg;
|
|
41
|
-
|
|
41
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
42
|
+
function getRunningPkgInfo(dir) {
|
|
43
|
+
if (ifInBrowser) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
42
46
|
if (pkg) {
|
|
43
47
|
return pkg;
|
|
44
48
|
}
|
|
@@ -51,7 +55,7 @@ function getMidscenePkgInfo(dir) {
|
|
|
51
55
|
return pkg;
|
|
52
56
|
}
|
|
53
57
|
return {
|
|
54
|
-
name: "midscene-unknown-
|
|
58
|
+
name: "midscene-unknown-package-name",
|
|
55
59
|
version: "0.0.0",
|
|
56
60
|
dir: pkgDir
|
|
57
61
|
};
|
|
@@ -70,5 +74,5 @@ function findNearestPackageJson(dir) {
|
|
|
70
74
|
// Annotate the CommonJS export names for ESM import in node:
|
|
71
75
|
0 && (module.exports = {
|
|
72
76
|
findNearestPackageJson,
|
|
73
|
-
|
|
77
|
+
getRunningPkgInfo
|
|
74
78
|
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { NodeType } from './constants.js';
|
|
3
|
+
|
|
4
|
+
interface Size$1 {
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Retrieves the dimensions of an image asynchronously
|
|
10
|
+
*
|
|
11
|
+
* @param image - The image data, which can be a string path or a buffer
|
|
12
|
+
* @returns A Promise that resolves to an object containing the width and height of the image
|
|
13
|
+
* @throws Error if the image data is invalid
|
|
14
|
+
*/
|
|
15
|
+
declare function imageInfo(image: string | Buffer): Promise<Size$1>;
|
|
16
|
+
/**
|
|
17
|
+
* Retrieves the dimensions of an image from a base64-encoded string
|
|
18
|
+
*
|
|
19
|
+
* @param imageBase64 - The base64-encoded image data
|
|
20
|
+
* @returns A Promise that resolves to an object containing the width and height of the image
|
|
21
|
+
* @throws Error if the image data is invalid
|
|
22
|
+
*/
|
|
23
|
+
declare function imageInfoOfBase64(imageBase64: string): Promise<Size$1>;
|
|
24
|
+
declare function bufferFromBase64(imageBase64: string): Promise<Buffer>;
|
|
25
|
+
/**
|
|
26
|
+
* Encodes an image file to a base64 encoded string
|
|
27
|
+
*
|
|
28
|
+
* @param image The path of the image file
|
|
29
|
+
* @param withHeader Determine whether to return data including the file header information, the default is true
|
|
30
|
+
*
|
|
31
|
+
* @returns The base64 encoded string of the image file, which may or may not include header information depending on the withHeader parameter
|
|
32
|
+
*
|
|
33
|
+
* @throws When the image type is not supported, an error will be thrown
|
|
34
|
+
*/
|
|
35
|
+
declare function base64Encoded(image: string, withHeader?: boolean): string;
|
|
36
|
+
declare function base64ToPngFormat(base64: string): string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
/**
|
|
40
|
+
* Saves a Base64-encoded image to a file
|
|
41
|
+
*
|
|
42
|
+
* @param options - An object containing the Base64-encoded image data and the output file path
|
|
43
|
+
* @param options.base64Data - The Base64-encoded image data
|
|
44
|
+
* @param options.outputPath - The path where the image will be saved
|
|
45
|
+
* @throws Error if there is an error during the saving process
|
|
46
|
+
*/
|
|
47
|
+
declare function saveBase64Image(options: {
|
|
48
|
+
base64Data: string;
|
|
49
|
+
outputPath: string;
|
|
50
|
+
}): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Transforms an image path into a base64-encoded string
|
|
53
|
+
* @param inputPath - The path of the image file to be encoded
|
|
54
|
+
* @returns A Promise that resolves to a base64-encoded string representing the image file
|
|
55
|
+
*/
|
|
56
|
+
declare function transformImgPathToBase64(inputPath: string): Promise<string>;
|
|
57
|
+
/**
|
|
58
|
+
* Resizes an image from a base64-encoded string
|
|
59
|
+
*
|
|
60
|
+
* @param base64Data - A base64-encoded string representing the image
|
|
61
|
+
* @returns A Promise that resolves to a base64-encoded string representing the resized image
|
|
62
|
+
* @throws An error if the width or height cannot be determined from the metadata
|
|
63
|
+
*/
|
|
64
|
+
declare function resizeImg(inputData: string | Buffer, newSize?: {
|
|
65
|
+
width: number;
|
|
66
|
+
height: number;
|
|
67
|
+
}): Promise<string | Buffer>;
|
|
68
|
+
/**
|
|
69
|
+
* Calculates new dimensions for an image while maintaining its aspect ratio.
|
|
70
|
+
*
|
|
71
|
+
* This function is designed to resize an image to fit within a specified maximum width and height
|
|
72
|
+
* while maintaining the original aspect ratio. If the original width or height exceeds the maximum
|
|
73
|
+
* dimensions, the image will be scaled down to fit.
|
|
74
|
+
*
|
|
75
|
+
* @param {number} originalWidth - The original width of the image.
|
|
76
|
+
* @param {number} originalHeight - The original height of the image.
|
|
77
|
+
* @returns {Object} An object containing the new width and height.
|
|
78
|
+
* @throws {Error} Throws an error if the width or height is not a positive number.
|
|
79
|
+
*/
|
|
80
|
+
declare function calculateNewDimensions(originalWidth: number, originalHeight: number): {
|
|
81
|
+
width: number;
|
|
82
|
+
height: number;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Trims an image and returns the trimming information, including the offset from the left and top edges, and the trimmed width and height
|
|
86
|
+
*
|
|
87
|
+
* @param image - The image to be trimmed. This can be a file path or a Buffer object containing the image data
|
|
88
|
+
* @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
|
|
89
|
+
*/
|
|
90
|
+
declare function trimImage(image: string | Buffer): Promise<{
|
|
91
|
+
trimOffsetLeft: number;
|
|
92
|
+
trimOffsetTop: number;
|
|
93
|
+
width: number;
|
|
94
|
+
height: number;
|
|
95
|
+
} | null>;
|
|
96
|
+
|
|
97
|
+
interface Point {
|
|
98
|
+
left: number;
|
|
99
|
+
top: number;
|
|
100
|
+
}
|
|
101
|
+
interface Size {
|
|
102
|
+
width: number;
|
|
103
|
+
height: number;
|
|
104
|
+
}
|
|
105
|
+
type Rect = Point & Size;
|
|
106
|
+
|
|
107
|
+
type ElementType = {
|
|
108
|
+
locator: string;
|
|
109
|
+
rect: Rect;
|
|
110
|
+
center: [number, number];
|
|
111
|
+
id: string;
|
|
112
|
+
indexId: number;
|
|
113
|
+
attributes: {
|
|
114
|
+
nodeType: NodeType;
|
|
115
|
+
[key: string]: string;
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
declare const compositeElementInfoImg: (options: {
|
|
119
|
+
inputImgBase64: string;
|
|
120
|
+
elementsPositionInfo: Array<ElementType>;
|
|
121
|
+
}) => Promise<string>;
|
|
122
|
+
declare const processImageElementInfo: (options: {
|
|
123
|
+
inputImgBase64: string;
|
|
124
|
+
elementsPositionInfo: Array<ElementType>;
|
|
125
|
+
elementsPositionInfoWithoutText: Array<ElementType>;
|
|
126
|
+
}) => Promise<{
|
|
127
|
+
compositeElementInfoImgBase64: string;
|
|
128
|
+
compositeElementInfoImgWithoutTextBase64: string;
|
|
129
|
+
}>;
|
|
130
|
+
|
|
131
|
+
export { base64Encoded, base64ToPngFormat, bufferFromBase64, calculateNewDimensions, compositeElementInfoImg, imageInfo, imageInfoOfBase64, processImageElementInfo, resizeImg, saveBase64Image, transformImgPathToBase64, trimImage };
|