@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.
@@ -1,6 +1,7 @@
1
1
  import { Buffer } from 'node:buffer';
2
- import Jimp from 'jimp';
2
+ import getJimp from './get-jimp';
3
3
 
4
+ /**
4
5
  /**
5
6
  * Saves a Base64-encoded image to a file
6
7
  *
@@ -21,6 +22,7 @@ export async function saveBase64Image(options: {
21
22
  const imageBuffer = Buffer.from(base64Image, 'base64');
22
23
 
23
24
  // Use Jimp to process the image and save it to the specified location
25
+ const Jimp = await getJimp();
24
26
  const image = await Jimp.read(imageBuffer);
25
27
  await image.writeAsync(outputPath);
26
28
  }
@@ -32,6 +34,7 @@ export async function saveBase64Image(options: {
32
34
  */
33
35
  export async function transformImgPathToBase64(inputPath: string) {
34
36
  // Use Jimp to process images and generate base64 data
37
+ const Jimp = await getJimp();
35
38
  const image = await Jimp.read(inputPath);
36
39
  const buffer = await image.getBufferAsync(Jimp.MIME_PNG);
37
40
  return buffer.toString('base64');
@@ -45,18 +48,17 @@ export async function transformImgPathToBase64(inputPath: string) {
45
48
  * @throws An error if the width or height cannot be determined from the metadata
46
49
  */
47
50
  export async function resizeImg(
48
- inputData: string | Buffer,
51
+ inputData: Buffer,
49
52
  newSize?: {
50
53
  width: number;
51
54
  height: number;
52
55
  },
53
- ): Promise<string | Buffer> {
54
- const isBase64 = typeof inputData === 'string';
55
- const imageBuffer = isBase64
56
- ? Buffer.from(inputData.split(';base64,').pop() || inputData, 'base64')
57
- : inputData;
56
+ ): Promise<Buffer> {
57
+ if (typeof inputData === 'string')
58
+ throw Error('inputData is base64, use resizeImgBase64 instead');
58
59
 
59
- const image = await Jimp.read(imageBuffer);
60
+ const Jimp = await getJimp();
61
+ const image = await Jimp.read(inputData);
60
62
  const { width, height } = image.bitmap;
61
63
 
62
64
  if (!width || !height) {
@@ -68,7 +70,26 @@ export async function resizeImg(
68
70
  image.resize(finalNewSize.width, finalNewSize.height);
69
71
  const resizedBuffer = await image.getBufferAsync(Jimp.MIME_PNG);
70
72
 
71
- return isBase64 ? resizedBuffer.toString('base64') : resizedBuffer;
73
+ return resizedBuffer;
74
+ }
75
+
76
+ export async function resizeImgBase64(
77
+ inputData: string,
78
+ newSize?: {
79
+ width: number;
80
+ height: number;
81
+ },
82
+ ): Promise<string> {
83
+ const splitFlag = ';base64,';
84
+ const dataSplitted = inputData.split(splitFlag);
85
+ if (dataSplitted.length !== 2) {
86
+ throw Error('Invalid base64 data');
87
+ }
88
+
89
+ const imageBuffer = Buffer.from(dataSplitted[1], 'base64');
90
+ const buffer = await resizeImg(imageBuffer, newSize);
91
+ const content = buffer.toString('base64');
92
+ return `${dataSplitted[0]}${splitFlag}${content}`;
72
93
  }
73
94
 
74
95
  /**
@@ -127,6 +148,7 @@ export async function trimImage(image: string | Buffer): Promise<{
127
148
  width: number;
128
149
  height: number;
129
150
  } | null> {
151
+ const Jimp = await getJimp();
130
152
  const jimpImage = await Jimp.read(
131
153
  Buffer.isBuffer(image) ? image : Buffer.from(image),
132
154
  );
package/src/index.ts CHANGED
@@ -1,3 +1 @@
1
- export default function () {
2
- return 'hello world';
3
- }
1
+ export default {};
package/src/utils.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { randomUUID } from 'node:crypto';
2
+
3
+ export const ifInBrowser = typeof window !== 'undefined';
4
+
5
+ export function uuid() {
6
+ if (ifInBrowser) {
7
+ return Math.random().toString(36).substring(2, 15);
8
+ }
9
+ return randomUUID();
10
+ }
File without changes