@midscene/shared 0.12.7 → 0.12.8-beta-20250317112356.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.
@@ -361,27 +361,8 @@ function getNodeAttributes(node, currentWindow) {
361
361
  });
362
362
  return Object.fromEntries(attributesList);
363
363
  }
364
- var nodeHashCacheList = [];
365
- if (typeof window !== "undefined") {
366
- window.midsceneNodeHashCacheList = window.midsceneNodeHashCacheList || [];
367
- nodeHashCacheList = window.midsceneNodeHashCacheList;
368
- }
369
- function resetNodeHashCacheList() {
370
- if (typeof window !== "undefined") {
371
- nodeHashCacheList = window.midsceneNodeHashCacheList || [];
372
- window.midsceneNodeHashCacheList = [];
373
- } else {
374
- nodeHashCacheList = [];
375
- }
376
- }
377
364
  function midsceneGenerateHash(node, content, rect) {
378
- if (node && nodeHashCacheList.find((item) => item.node === node)) {
379
- return nodeHashCacheList.find((item) => item.node === node)?.id || "";
380
- }
381
365
  const slicedHash = generateHashId(rect, content);
382
- if (node && typeof window !== "undefined") {
383
- window.midsceneNodeHashCacheList.push({ node, id: slicedHash });
384
- }
385
366
  return slicedHash;
386
367
  }
387
368
  function setMidsceneVisibleRectOnWindow() {
@@ -600,7 +581,6 @@ function extractTextWithPosition(initNode, debugMode2 = false) {
600
581
  }
601
582
  function extractTreeNode(initNode, debugMode2 = false) {
602
583
  setDebugMode(debugMode2);
603
- resetNodeHashCacheList();
604
584
  indexId = 0;
605
585
  const topDocument = getTopDocument();
606
586
  const startNode = initNode || topDocument;
@@ -482,27 +482,8 @@ function getNodeAttributes(node, currentWindow) {
482
482
  });
483
483
  return Object.fromEntries(attributesList);
484
484
  }
485
- var nodeHashCacheList = [];
486
- if (typeof window !== "undefined") {
487
- window.midsceneNodeHashCacheList = window.midsceneNodeHashCacheList || [];
488
- nodeHashCacheList = window.midsceneNodeHashCacheList;
489
- }
490
- function resetNodeHashCacheList() {
491
- if (typeof window !== "undefined") {
492
- nodeHashCacheList = window.midsceneNodeHashCacheList || [];
493
- window.midsceneNodeHashCacheList = [];
494
- } else {
495
- nodeHashCacheList = [];
496
- }
497
- }
498
485
  function midsceneGenerateHash(node, content, rect) {
499
- if (node && nodeHashCacheList.find((item) => item.node === node)) {
500
- return nodeHashCacheList.find((item) => item.node === node)?.id || "";
501
- }
502
486
  const slicedHash = generateHashId(rect, content);
503
- if (node && typeof window !== "undefined") {
504
- window.midsceneNodeHashCacheList.push({ node, id: slicedHash });
505
- }
506
487
  return slicedHash;
507
488
  }
508
489
  function getTopDocument() {
@@ -715,7 +696,6 @@ function extractTreeNodeAsString(initNode, debugMode2 = false) {
715
696
  }
716
697
  function extractTreeNode(initNode, debugMode2 = false) {
717
698
  setDebugMode(debugMode2);
718
- resetNodeHashCacheList();
719
699
  indexId = 0;
720
700
  const topDocument = getTopDocument();
721
701
  const startNode = initNode || topDocument;
package/dist/es/fs.js CHANGED
@@ -6,6 +6,7 @@ import path from "path";
6
6
  // src/utils.ts
7
7
  import { sha256 } from "js-sha256";
8
8
  import debug from "debug";
9
+ var ifInBrowser = typeof window !== "undefined";
9
10
  function assert(condition, message) {
10
11
  if (!condition) {
11
12
  throw new Error(message || "Assertion failed");
@@ -14,7 +15,6 @@ function assert(condition, message) {
14
15
 
15
16
  // src/node/fs.ts
16
17
  var pkgCacheMap = {};
17
- var ifInBrowser = typeof window !== "undefined";
18
18
  function getRunningPkgInfo(dir) {
19
19
  if (ifInBrowser) {
20
20
  return null;
package/dist/es/img.js CHANGED
@@ -58,19 +58,26 @@ function base64Encoded(image, withHeader = true) {
58
58
  // src/img/transform.ts
59
59
  import assert2 from "assert";
60
60
  import { Buffer as Buffer2 } from "buffer";
61
+ import getDebug from "debug";
62
+ var debugImg = getDebug("img");
61
63
  async function saveBase64Image(options) {
64
+ debugImg(`saveBase64Image start: ${options.outputPath}`);
62
65
  const { base64Data, outputPath } = options;
63
66
  const base64Image = base64Data.split(";base64,").pop() || base64Data;
64
67
  const imageBuffer = Buffer2.from(base64Image, "base64");
65
68
  const Jimp = await getJimp();
66
69
  const image = await Jimp.read(imageBuffer);
67
70
  await image.writeAsync(outputPath);
71
+ debugImg(`saveBase64Image done: ${options.outputPath}`);
68
72
  }
69
73
  async function transformImgPathToBase64(inputPath) {
74
+ debugImg(`transformImgPathToBase64 start: ${inputPath}`);
70
75
  const Jimp = await getJimp();
71
76
  const image = await Jimp.read(inputPath);
72
77
  const buffer = await image.getBufferAsync(Jimp.MIME_JPEG);
73
- return buffer.toString("base64");
78
+ const res = buffer.toString("base64");
79
+ debugImg(`transformImgPathToBase64 done: ${inputPath}`);
80
+ return res;
74
81
  }
75
82
  async function resizeImg(inputData, newSize) {
76
83
  if (typeof inputData === "string")
@@ -79,6 +86,7 @@ async function resizeImg(inputData, newSize) {
79
86
  newSize && newSize.width > 0 && newSize.height > 0,
80
87
  "newSize must be positive"
81
88
  );
89
+ debugImg(`resizeImg start, target size: ${newSize.width}x${newSize.height}`);
82
90
  const Jimp = await getJimp();
83
91
  const image = await Jimp.read(inputData);
84
92
  const { width, height } = image.bitmap;
@@ -91,6 +99,7 @@ async function resizeImg(inputData, newSize) {
91
99
  image.resize(newSize.width, newSize.height, Jimp.RESIZE_NEAREST_NEIGHBOR);
92
100
  image.quality(90);
93
101
  const resizedBuffer = await image.getBufferAsync(Jimp.MIME_JPEG);
102
+ debugImg(`resizeImg done, target size: ${newSize.width}x${newSize.height}`);
94
103
  return resizedBuffer;
95
104
  }
96
105
  async function bufferFromBase642(base64) {
@@ -99,9 +108,13 @@ async function bufferFromBase642(base64) {
99
108
  if (dataSplitted.length !== 2) {
100
109
  throw Error("Invalid base64 data");
101
110
  }
102
- return Buffer2.from(dataSplitted[1], "base64");
111
+ debugImg(`bufferFromBase64 start: ${base64}`);
112
+ const res = Buffer2.from(dataSplitted[1], "base64");
113
+ debugImg(`bufferFromBase64 done: ${base64}`);
114
+ return res;
103
115
  }
104
116
  async function resizeImgBase64(inputBase64, newSize) {
117
+ debugImg(`resizeImgBase64 start: ${inputBase64}`);
105
118
  const splitFlag = ";base64,";
106
119
  const dataSplitted = inputBase64.split(splitFlag);
107
120
  if (dataSplitted.length !== 2) {
@@ -110,7 +123,9 @@ async function resizeImgBase64(inputBase64, newSize) {
110
123
  const imageBuffer = Buffer2.from(dataSplitted[1], "base64");
111
124
  const buffer = await resizeImg(imageBuffer, newSize);
112
125
  const content = buffer.toString("base64");
113
- return `${dataSplitted[0]}${splitFlag}${content}`;
126
+ const res = `${dataSplitted[0]}${splitFlag}${content}`;
127
+ debugImg(`resizeImgBase64 done: ${inputBase64}`);
128
+ return res;
114
129
  }
115
130
  function zoomForGPT4o(originalWidth, originalHeight) {
116
131
  const maxWidth = 2048;
@@ -155,6 +170,7 @@ async function trimImage(image) {
155
170
  };
156
171
  }
157
172
  async function paddingToMatchBlock(imageBase64, blockSize = 28) {
173
+ debugImg("paddingToMatchBlock start");
158
174
  const Jimp = await getJimp();
159
175
  const imageBuffer = await bufferFromBase642(imageBase64);
160
176
  const image = await Jimp.read(imageBuffer);
@@ -167,6 +183,7 @@ async function paddingToMatchBlock(imageBase64, blockSize = 28) {
167
183
  const paddedImage = new Jimp(targetWidth, targetHeight, 4294967295);
168
184
  paddedImage.composite(image, 0, 0);
169
185
  const base64 = await paddedImage.getBase64Async(Jimp.MIME_JPEG);
186
+ debugImg("paddingToMatchBlock done");
170
187
  return base64;
171
188
  }
172
189
 
@@ -385,27 +385,8 @@ function getNodeAttributes(node, currentWindow) {
385
385
  });
386
386
  return Object.fromEntries(attributesList);
387
387
  }
388
- var nodeHashCacheList = [];
389
- if (typeof window !== "undefined") {
390
- window.midsceneNodeHashCacheList = window.midsceneNodeHashCacheList || [];
391
- nodeHashCacheList = window.midsceneNodeHashCacheList;
392
- }
393
- function resetNodeHashCacheList() {
394
- if (typeof window !== "undefined") {
395
- nodeHashCacheList = window.midsceneNodeHashCacheList || [];
396
- window.midsceneNodeHashCacheList = [];
397
- } else {
398
- nodeHashCacheList = [];
399
- }
400
- }
401
388
  function midsceneGenerateHash(node, content, rect) {
402
- if (node && nodeHashCacheList.find((item) => item.node === node)) {
403
- return nodeHashCacheList.find((item) => item.node === node)?.id || "";
404
- }
405
389
  const slicedHash = generateHashId(rect, content);
406
- if (node && typeof window !== "undefined") {
407
- window.midsceneNodeHashCacheList.push({ node, id: slicedHash });
408
- }
409
390
  return slicedHash;
410
391
  }
411
392
  function setMidsceneVisibleRectOnWindow() {
@@ -624,7 +605,6 @@ function extractTextWithPosition(initNode, debugMode2 = false) {
624
605
  }
625
606
  function extractTreeNode(initNode, debugMode2 = false) {
626
607
  setDebugMode(debugMode2);
627
- resetNodeHashCacheList();
628
608
  indexId = 0;
629
609
  const topDocument = getTopDocument();
630
610
  const startNode = initNode || topDocument;
@@ -524,27 +524,8 @@ function getNodeAttributes(node, currentWindow) {
524
524
  });
525
525
  return Object.fromEntries(attributesList);
526
526
  }
527
- var nodeHashCacheList = [];
528
- if (typeof window !== "undefined") {
529
- window.midsceneNodeHashCacheList = window.midsceneNodeHashCacheList || [];
530
- nodeHashCacheList = window.midsceneNodeHashCacheList;
531
- }
532
- function resetNodeHashCacheList() {
533
- if (typeof window !== "undefined") {
534
- nodeHashCacheList = window.midsceneNodeHashCacheList || [];
535
- window.midsceneNodeHashCacheList = [];
536
- } else {
537
- nodeHashCacheList = [];
538
- }
539
- }
540
527
  function midsceneGenerateHash(node, content, rect) {
541
- if (node && nodeHashCacheList.find((item) => item.node === node)) {
542
- return nodeHashCacheList.find((item) => item.node === node)?.id || "";
543
- }
544
528
  const slicedHash = generateHashId(rect, content);
545
- if (node && typeof window !== "undefined") {
546
- window.midsceneNodeHashCacheList.push({ node, id: slicedHash });
547
- }
548
529
  return slicedHash;
549
530
  }
550
531
  function getTopDocument() {
@@ -757,7 +738,6 @@ function extractTreeNodeAsString(initNode, debugMode2 = false) {
757
738
  }
758
739
  function extractTreeNode(initNode, debugMode2 = false) {
759
740
  setDebugMode(debugMode2);
760
- resetNodeHashCacheList();
761
741
  indexId = 0;
762
742
  const topDocument = getTopDocument();
763
743
  const startNode = initNode || topDocument;
package/dist/lib/fs.js CHANGED
@@ -42,6 +42,7 @@ var import_node_path2 = __toESM(require("path"));
42
42
  // src/utils.ts
43
43
  var import_js_sha256 = require("js-sha256");
44
44
  var import_debug = __toESM(require("debug"));
45
+ var ifInBrowser = typeof window !== "undefined";
45
46
  function assert(condition, message) {
46
47
  if (!condition) {
47
48
  throw new Error(message || "Assertion failed");
@@ -50,7 +51,6 @@ function assert(condition, message) {
50
51
 
51
52
  // src/node/fs.ts
52
53
  var pkgCacheMap = {};
53
- var ifInBrowser = typeof window !== "undefined";
54
54
  function getRunningPkgInfo(dir) {
55
55
  if (ifInBrowser) {
56
56
  return null;
package/dist/lib/img.js CHANGED
@@ -108,19 +108,26 @@ function base64Encoded(image, withHeader = true) {
108
108
  // src/img/transform.ts
109
109
  var import_node_assert2 = __toESM(require("assert"));
110
110
  var import_node_buffer2 = require("buffer");
111
+ var import_debug = __toESM(require("debug"));
112
+ var debugImg = (0, import_debug.default)("img");
111
113
  async function saveBase64Image(options) {
114
+ debugImg(`saveBase64Image start: ${options.outputPath}`);
112
115
  const { base64Data, outputPath } = options;
113
116
  const base64Image = base64Data.split(";base64,").pop() || base64Data;
114
117
  const imageBuffer = import_node_buffer2.Buffer.from(base64Image, "base64");
115
118
  const Jimp = await getJimp();
116
119
  const image = await Jimp.read(imageBuffer);
117
120
  await image.writeAsync(outputPath);
121
+ debugImg(`saveBase64Image done: ${options.outputPath}`);
118
122
  }
119
123
  async function transformImgPathToBase64(inputPath) {
124
+ debugImg(`transformImgPathToBase64 start: ${inputPath}`);
120
125
  const Jimp = await getJimp();
121
126
  const image = await Jimp.read(inputPath);
122
127
  const buffer = await image.getBufferAsync(Jimp.MIME_JPEG);
123
- return buffer.toString("base64");
128
+ const res = buffer.toString("base64");
129
+ debugImg(`transformImgPathToBase64 done: ${inputPath}`);
130
+ return res;
124
131
  }
125
132
  async function resizeImg(inputData, newSize) {
126
133
  if (typeof inputData === "string")
@@ -129,6 +136,7 @@ async function resizeImg(inputData, newSize) {
129
136
  newSize && newSize.width > 0 && newSize.height > 0,
130
137
  "newSize must be positive"
131
138
  );
139
+ debugImg(`resizeImg start, target size: ${newSize.width}x${newSize.height}`);
132
140
  const Jimp = await getJimp();
133
141
  const image = await Jimp.read(inputData);
134
142
  const { width, height } = image.bitmap;
@@ -141,6 +149,7 @@ async function resizeImg(inputData, newSize) {
141
149
  image.resize(newSize.width, newSize.height, Jimp.RESIZE_NEAREST_NEIGHBOR);
142
150
  image.quality(90);
143
151
  const resizedBuffer = await image.getBufferAsync(Jimp.MIME_JPEG);
152
+ debugImg(`resizeImg done, target size: ${newSize.width}x${newSize.height}`);
144
153
  return resizedBuffer;
145
154
  }
146
155
  async function bufferFromBase642(base64) {
@@ -149,9 +158,13 @@ async function bufferFromBase642(base64) {
149
158
  if (dataSplitted.length !== 2) {
150
159
  throw Error("Invalid base64 data");
151
160
  }
152
- return import_node_buffer2.Buffer.from(dataSplitted[1], "base64");
161
+ debugImg(`bufferFromBase64 start: ${base64}`);
162
+ const res = import_node_buffer2.Buffer.from(dataSplitted[1], "base64");
163
+ debugImg(`bufferFromBase64 done: ${base64}`);
164
+ return res;
153
165
  }
154
166
  async function resizeImgBase64(inputBase64, newSize) {
167
+ debugImg(`resizeImgBase64 start: ${inputBase64}`);
155
168
  const splitFlag = ";base64,";
156
169
  const dataSplitted = inputBase64.split(splitFlag);
157
170
  if (dataSplitted.length !== 2) {
@@ -160,7 +173,9 @@ async function resizeImgBase64(inputBase64, newSize) {
160
173
  const imageBuffer = import_node_buffer2.Buffer.from(dataSplitted[1], "base64");
161
174
  const buffer = await resizeImg(imageBuffer, newSize);
162
175
  const content = buffer.toString("base64");
163
- return `${dataSplitted[0]}${splitFlag}${content}`;
176
+ const res = `${dataSplitted[0]}${splitFlag}${content}`;
177
+ debugImg(`resizeImgBase64 done: ${inputBase64}`);
178
+ return res;
164
179
  }
165
180
  function zoomForGPT4o(originalWidth, originalHeight) {
166
181
  const maxWidth = 2048;
@@ -205,6 +220,7 @@ async function trimImage(image) {
205
220
  };
206
221
  }
207
222
  async function paddingToMatchBlock(imageBase64, blockSize = 28) {
223
+ debugImg("paddingToMatchBlock start");
208
224
  const Jimp = await getJimp();
209
225
  const imageBuffer = await bufferFromBase642(imageBase64);
210
226
  const image = await Jimp.read(imageBuffer);
@@ -217,6 +233,7 @@ async function paddingToMatchBlock(imageBase64, blockSize = 28) {
217
233
  const paddedImage = new Jimp(targetWidth, targetHeight, 4294967295);
218
234
  paddedImage.composite(image, 0, 0);
219
235
  const base64 = await paddedImage.getBase64Async(Jimp.MIME_JPEG);
236
+ debugImg("paddingToMatchBlock done");
220
237
  return base64;
221
238
  }
222
239
 
@@ -1538,28 +1538,8 @@ ${indentStr}${after}`;
1538
1538
  });
1539
1539
  return Object.fromEntries(attributesList);
1540
1540
  }
1541
- var nodeHashCacheList = [];
1542
- if (typeof window !== "undefined") {
1543
- window.midsceneNodeHashCacheList = window.midsceneNodeHashCacheList || [];
1544
- nodeHashCacheList = window.midsceneNodeHashCacheList;
1545
- }
1546
- function resetNodeHashCacheList() {
1547
- if (typeof window !== "undefined") {
1548
- nodeHashCacheList = window.midsceneNodeHashCacheList || [];
1549
- window.midsceneNodeHashCacheList = [];
1550
- } else {
1551
- nodeHashCacheList = [];
1552
- }
1553
- }
1554
1541
  function midsceneGenerateHash(node, content, rect) {
1555
- var _a;
1556
- if (node && nodeHashCacheList.find((item) => item.node === node)) {
1557
- return ((_a = nodeHashCacheList.find((item) => item.node === node)) == null ? void 0 : _a.id) || "";
1558
- }
1559
1542
  const slicedHash = generateHashId(rect, content);
1560
- if (node && typeof window !== "undefined") {
1561
- window.midsceneNodeHashCacheList.push({ node, id: slicedHash });
1562
- }
1563
1543
  return slicedHash;
1564
1544
  }
1565
1545
  function getTopDocument() {
@@ -1767,7 +1747,6 @@ ${indentStr}${after}`;
1767
1747
  }
1768
1748
  function extractTreeNode(initNode, debugMode2 = false) {
1769
1749
  setDebugMode(debugMode2);
1770
- resetNodeHashCacheList();
1771
1750
  indexId = 0;
1772
1751
  const topDocument = getTopDocument();
1773
1752
  const startNode = initNode || topDocument;
@@ -1399,28 +1399,8 @@ var midscene_element_inspector = (() => {
1399
1399
  });
1400
1400
  return Object.fromEntries(attributesList);
1401
1401
  }
1402
- var nodeHashCacheList = [];
1403
- if (typeof window !== "undefined") {
1404
- window.midsceneNodeHashCacheList = window.midsceneNodeHashCacheList || [];
1405
- nodeHashCacheList = window.midsceneNodeHashCacheList;
1406
- }
1407
- function resetNodeHashCacheList() {
1408
- if (typeof window !== "undefined") {
1409
- nodeHashCacheList = window.midsceneNodeHashCacheList || [];
1410
- window.midsceneNodeHashCacheList = [];
1411
- } else {
1412
- nodeHashCacheList = [];
1413
- }
1414
- }
1415
1402
  function midsceneGenerateHash(node, content, rect) {
1416
- var _a;
1417
- if (node && nodeHashCacheList.find((item) => item.node === node)) {
1418
- return ((_a = nodeHashCacheList.find((item) => item.node === node)) == null ? void 0 : _a.id) || "";
1419
- }
1420
1403
  const slicedHash = generateHashId(rect, content);
1421
- if (node && typeof window !== "undefined") {
1422
- window.midsceneNodeHashCacheList.push({ node, id: slicedHash });
1423
- }
1424
1404
  return slicedHash;
1425
1405
  }
1426
1406
  function setMidsceneVisibleRectOnWindow() {
@@ -1634,7 +1614,6 @@ var midscene_element_inspector = (() => {
1634
1614
  }
1635
1615
  function extractTreeNode(initNode, debugMode2 = false) {
1636
1616
  setDebugMode(debugMode2);
1637
- resetNodeHashCacheList();
1638
1617
  indexId = 0;
1639
1618
  const topDocument = getTopDocument();
1640
1619
  const startNode = initNode || topDocument;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midscene/shared",
3
- "version": "0.12.7",
3
+ "version": "0.12.8-beta-20250317112356.0",
4
4
  "repository": "https://github.com/web-infra-dev/midscene",
5
5
  "homepage": "https://midscenejs.com/",
6
6
  "types": "./dist/types/index.d.ts",
@@ -441,36 +441,12 @@ export function getNodeAttributes(
441
441
  return Object.fromEntries(attributesList);
442
442
  }
443
443
 
444
- let nodeHashCacheList: { node: Node; id: string }[] = [];
445
- if (typeof window !== 'undefined') {
446
- (window as any).midsceneNodeHashCacheList =
447
- (window as any).midsceneNodeHashCacheList || [];
448
- nodeHashCacheList = (window as any).midsceneNodeHashCacheList;
449
- }
450
- const hashMap: Record<string, string> = {}; // id - combined
451
-
452
- // for each run, reset the cache list
453
- export function resetNodeHashCacheList() {
454
- if (typeof window !== 'undefined') {
455
- nodeHashCacheList = (window as any).midsceneNodeHashCacheList || [];
456
- (window as any).midsceneNodeHashCacheList = [];
457
- } else {
458
- nodeHashCacheList = [];
459
- }
460
- }
461
-
462
444
  export function midsceneGenerateHash(
463
445
  node: globalThis.Node | null,
464
446
  content: string,
465
447
  rect: any,
466
448
  ): string {
467
- if (node && nodeHashCacheList.find((item) => item.node === node)) {
468
- return nodeHashCacheList.find((item) => item.node === node)?.id || '';
469
- }
470
449
  const slicedHash = generateHashId(rect, content);
471
- if (node && typeof window !== 'undefined') {
472
- (window as any).midsceneNodeHashCacheList.push({ node, id: slicedHash });
473
- }
474
450
 
475
451
  // Returns the first 10 characters as a short hash
476
452
  return slicedHash;
@@ -20,7 +20,6 @@ import {
20
20
  getTopDocument,
21
21
  logger,
22
22
  midsceneGenerateHash,
23
- resetNodeHashCacheList,
24
23
  setDataForNode,
25
24
  setDebugMode,
26
25
  visibleRect,
@@ -292,7 +291,6 @@ export function extractTreeNode(
292
291
  debugMode = false,
293
292
  ): WebElementNode {
294
293
  setDebugMode(debugMode);
295
- resetNodeHashCacheList();
296
294
  indexId = 0;
297
295
 
298
296
  const topDocument = getTopDocument();
@@ -1,7 +1,9 @@
1
1
  import assert from 'node:assert';
2
2
  import { Buffer } from 'node:buffer';
3
+ import getDebug from 'debug';
3
4
  import getJimp from './get-jimp';
4
5
 
6
+ const debugImg = getDebug('img');
5
7
  /**
6
8
  /**
7
9
  * Saves a Base64-encoded image to a file
@@ -15,6 +17,7 @@ export async function saveBase64Image(options: {
15
17
  base64Data: string;
16
18
  outputPath: string;
17
19
  }): Promise<void> {
20
+ debugImg(`saveBase64Image start: ${options.outputPath}`);
18
21
  const { base64Data, outputPath } = options;
19
22
  // Remove the base64 data prefix (if any)
20
23
  const base64Image = base64Data.split(';base64,').pop() || base64Data;
@@ -26,6 +29,7 @@ export async function saveBase64Image(options: {
26
29
  const Jimp = await getJimp();
27
30
  const image = await Jimp.read(imageBuffer);
28
31
  await image.writeAsync(outputPath);
32
+ debugImg(`saveBase64Image done: ${options.outputPath}`);
29
33
  }
30
34
 
31
35
  /**
@@ -35,10 +39,13 @@ export async function saveBase64Image(options: {
35
39
  */
36
40
  export async function transformImgPathToBase64(inputPath: string) {
37
41
  // Use Jimp to process images and generate base64 data
42
+ debugImg(`transformImgPathToBase64 start: ${inputPath}`);
38
43
  const Jimp = await getJimp();
39
44
  const image = await Jimp.read(inputPath);
40
45
  const buffer = await image.getBufferAsync(Jimp.MIME_JPEG);
41
- return buffer.toString('base64');
46
+ const res = buffer.toString('base64');
47
+ debugImg(`transformImgPathToBase64 done: ${inputPath}`);
48
+ return res;
42
49
  }
43
50
 
44
51
  /**
@@ -63,6 +70,7 @@ export async function resizeImg(
63
70
  'newSize must be positive',
64
71
  );
65
72
 
73
+ debugImg(`resizeImg start, target size: ${newSize.width}x${newSize.height}`);
66
74
  const Jimp = await getJimp();
67
75
  const image = await Jimp.read(inputData);
68
76
  const { width, height } = image.bitmap;
@@ -78,6 +86,7 @@ export async function resizeImg(
78
86
  image.resize(newSize.width, newSize.height, Jimp.RESIZE_NEAREST_NEIGHBOR);
79
87
  image.quality(90);
80
88
  const resizedBuffer = await image.getBufferAsync(Jimp.MIME_JPEG);
89
+ debugImg(`resizeImg done, target size: ${newSize.width}x${newSize.height}`);
81
90
 
82
91
  return resizedBuffer;
83
92
  }
@@ -88,7 +97,10 @@ export async function bufferFromBase64(base64: string) {
88
97
  if (dataSplitted.length !== 2) {
89
98
  throw Error('Invalid base64 data');
90
99
  }
91
- return Buffer.from(dataSplitted[1], 'base64');
100
+ debugImg(`bufferFromBase64 start: ${base64}`);
101
+ const res = Buffer.from(dataSplitted[1], 'base64');
102
+ debugImg(`bufferFromBase64 done: ${base64}`);
103
+ return res;
92
104
  }
93
105
 
94
106
  export async function resizeImgBase64(
@@ -98,6 +110,7 @@ export async function resizeImgBase64(
98
110
  height: number;
99
111
  },
100
112
  ): Promise<string> {
113
+ debugImg(`resizeImgBase64 start: ${inputBase64}`);
101
114
  const splitFlag = ';base64,';
102
115
  const dataSplitted = inputBase64.split(splitFlag);
103
116
  if (dataSplitted.length !== 2) {
@@ -107,7 +120,9 @@ export async function resizeImgBase64(
107
120
  const imageBuffer = Buffer.from(dataSplitted[1], 'base64');
108
121
  const buffer = await resizeImg(imageBuffer, newSize);
109
122
  const content = buffer.toString('base64');
110
- return `${dataSplitted[0]}${splitFlag}${content}`;
123
+ const res = `${dataSplitted[0]}${splitFlag}${content}`;
124
+ debugImg(`resizeImgBase64 done: ${inputBase64}`);
125
+ return res;
111
126
  }
112
127
 
113
128
  /**
@@ -196,6 +211,7 @@ export function prependBase64Header(base64: string, mimeType = 'image/png') {
196
211
  }
197
212
 
198
213
  export async function paddingToMatchBlock(imageBase64: string, blockSize = 28) {
214
+ debugImg('paddingToMatchBlock start');
199
215
  const Jimp = await getJimp();
200
216
  const imageBuffer = await bufferFromBase64(imageBase64);
201
217
  const image = await Jimp.read(imageBuffer);
@@ -214,5 +230,6 @@ export async function paddingToMatchBlock(imageBase64: string, blockSize = 28) {
214
230
  paddedImage.composite(image, 0, 0);
215
231
 
216
232
  const base64 = await paddedImage.getBase64Async(Jimp.MIME_JPEG);
233
+ debugImg('paddingToMatchBlock done');
217
234
  return base64;
218
235
  }
package/src/node/fs.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  import { existsSync, readFileSync } from 'node:fs';
2
2
  import { dirname, join } from 'node:path';
3
3
  import path from 'node:path';
4
- import { fileURLToPath } from 'node:url';
5
- import { assert } from '../utils';
4
+ import { assert, ifInBrowser } from '../utils';
6
5
 
7
6
  interface PkgInfo {
8
7
  name: string;
@@ -11,7 +10,6 @@ interface PkgInfo {
11
10
  }
12
11
 
13
12
  const pkgCacheMap: Record<string, PkgInfo> = {};
14
- const ifInBrowser = typeof window !== 'undefined';
15
13
 
16
14
  export function getRunningPkgInfo(dir?: string): PkgInfo | null {
17
15
  if (ifInBrowser) {