@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/lib/fs.js CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,43 +15,38 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/fs/index.ts
31
21
  var fs_exports = {};
32
22
  __export(fs_exports, {
33
23
  findNearestPackageJson: () => findNearestPackageJson,
34
- getMidscenePkgInfo: () => getMidscenePkgInfo
24
+ getRunningPkgInfo: () => getRunningPkgInfo
35
25
  });
36
26
  module.exports = __toCommonJS(fs_exports);
37
- var import_node_assert = __toESM(require("assert"));
38
27
  var import_node_fs = require("fs");
39
28
  var import_node_path = require("path");
40
- var pkg;
41
- function getMidscenePkgInfo(dir) {
42
- if (pkg) {
43
- return pkg;
29
+ var pkgCacheMap = {};
30
+ var ifInBrowser = typeof window !== "undefined";
31
+ function getRunningPkgInfo(dir) {
32
+ if (ifInBrowser) {
33
+ return null;
34
+ }
35
+ const dirToCheck = dir || process.cwd();
36
+ if (pkgCacheMap[dirToCheck]) {
37
+ return pkgCacheMap[dirToCheck];
44
38
  }
45
- const pkgDir = findNearestPackageJson(dir || process.cwd());
46
- (0, import_node_assert.default)(pkgDir, "package.json not found");
47
- const pkgJsonFile = (0, import_node_path.join)(pkgDir, "package.json");
48
- if (pkgJsonFile) {
39
+ const pkgDir = findNearestPackageJson(dirToCheck);
40
+ const pkgJsonFile = pkgDir ? (0, import_node_path.join)(pkgDir, "package.json") : null;
41
+ if (pkgDir && pkgJsonFile) {
49
42
  const { name, version } = JSON.parse((0, import_node_fs.readFileSync)(pkgJsonFile, "utf-8"));
50
- pkg = { name, version, dir: pkgDir };
51
- return pkg;
43
+ pkgCacheMap[dirToCheck] = { name, version, dir: pkgDir };
44
+ return pkgCacheMap[dirToCheck];
52
45
  }
53
46
  return {
54
- name: "midscene-unknown-page-name",
47
+ name: "midscene-unknown-package-name",
55
48
  version: "0.0.0",
56
- dir: pkgDir
49
+ dir: dirToCheck
57
50
  };
58
51
  }
59
52
  function findNearestPackageJson(dir) {
@@ -70,5 +63,5 @@ function findNearestPackageJson(dir) {
70
63
  // Annotate the CommonJS export names for ESM import in node:
71
64
  0 && (module.exports = {
72
65
  findNearestPackageJson,
73
- getMidscenePkgInfo
66
+ getRunningPkgInfo
74
67
  });
@@ -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 };