@midscene/shared 0.13.1 → 0.13.2-beta-20250401015137.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.
@@ -72,7 +72,6 @@ function includeBaseElement(node) {
72
72
 
73
73
  // src/utils.ts
74
74
  import { sha256 } from "js-sha256";
75
- import debug from "debug";
76
75
  var hashMap = {};
77
76
  function generateHashId(rect, content = "") {
78
77
  const combined = JSON.stringify({
@@ -193,7 +193,6 @@ function includeBaseElement(node) {
193
193
 
194
194
  // src/utils.ts
195
195
  import { sha256 } from "js-sha256";
196
- import debug from "debug";
197
196
  var hashMap = {};
198
197
  function generateHashId(rect, content = "") {
199
198
  const combined = JSON.stringify({
package/dist/es/fs.js CHANGED
@@ -5,7 +5,6 @@ import path from "path";
5
5
 
6
6
  // src/utils.ts
7
7
  import { sha256 } from "js-sha256";
8
- import debug from "debug";
9
8
  var ifInBrowser = typeof window !== "undefined";
10
9
  function assert(condition, message) {
11
10
  if (!condition) {
@@ -0,0 +1,6 @@
1
+ type DebugFunction = (...args: unknown[]) => void;
2
+ declare function getDebug(topic: string): DebugFunction;
3
+ declare function enableDebug(topic: string): void;
4
+ declare function cleanupLogStreams(): void;
5
+
6
+ export { type DebugFunction, cleanupLogStreams, enableDebug, getDebug };
@@ -0,0 +1,72 @@
1
+ // src/logger.ts
2
+ import fs2 from "fs";
3
+ import path2 from "path";
4
+ import debug from "debug";
5
+
6
+ // src/common.ts
7
+ import fs from "fs";
8
+ import path from "path";
9
+ var isNodeEnv = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
10
+ var getMidsceneRunLogPath = () => {
11
+ const basePath = path.join(process.cwd(), "midscene_run");
12
+ if (!fs.existsSync(basePath)) {
13
+ fs.mkdirSync(basePath, { recursive: true });
14
+ }
15
+ const logPath = path.join(basePath, "log");
16
+ if (!fs.existsSync(logPath)) {
17
+ fs.mkdirSync(logPath, { recursive: true });
18
+ }
19
+ return logPath;
20
+ };
21
+ var logDir = isNodeEnv ? getMidsceneRunLogPath() : "";
22
+
23
+ // src/logger.ts
24
+ var topicPrefix = "midscene";
25
+ var logStreams = /* @__PURE__ */ new Map();
26
+ function getLogStream(topic) {
27
+ if (!logStreams.has(topic)) {
28
+ const logFile = path2.join(logDir, `${topic}.log`);
29
+ const stream = fs2.createWriteStream(logFile, { flags: "a" });
30
+ logStreams.set(topic, stream);
31
+ }
32
+ return logStreams.get(topic);
33
+ }
34
+ function writeLogToFile(topic, message) {
35
+ if (!isNodeEnv)
36
+ return;
37
+ const stream = getLogStream(topic);
38
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
39
+ stream.write(`[${timestamp}] ${message}
40
+ `);
41
+ }
42
+ function getDebug(topic) {
43
+ return (...args) => {
44
+ const message = args.map(
45
+ (arg) => typeof arg === "object" ? JSON.stringify(arg) : String(arg)
46
+ ).join(" ");
47
+ if (isNodeEnv) {
48
+ writeLogToFile(topic, message);
49
+ }
50
+ const debugFn = debug(`${topicPrefix}:${topic}`);
51
+ debugFn(...args);
52
+ };
53
+ }
54
+ function enableDebug(topic) {
55
+ if (isNodeEnv) {
56
+ return;
57
+ }
58
+ debug.enable(`${topicPrefix}:${topic}`);
59
+ }
60
+ function cleanupLogStreams() {
61
+ if (!isNodeEnv)
62
+ return;
63
+ for (const stream of logStreams.values()) {
64
+ stream.end();
65
+ }
66
+ logStreams.clear();
67
+ }
68
+ export {
69
+ cleanupLogStreams,
70
+ enableDebug,
71
+ getDebug
72
+ };
@@ -1,9 +1,5 @@
1
- import debug from 'debug';
2
-
3
1
  declare const ifInBrowser: boolean;
4
2
  declare function uuid(): string;
5
- declare function getDebug(topic: string): debug.Debugger;
6
- declare function enableDebug(topic: string): void;
7
3
  declare function generateHashId(rect: any, content?: string): string;
8
4
  /**
9
5
  * A utility function that asserts a condition and throws an error with a message if the condition is false.
@@ -13,6 +9,7 @@ declare function generateHashId(rect: any, content?: string): string;
13
9
  * @throws Error with the provided message if the condition is false
14
10
  */
15
11
  declare function assert(condition: any, message?: string): asserts condition;
16
- declare function getGlobalScope(): typeof globalThis | undefined;
12
+ type GlobalScope = typeof window | typeof globalThis | typeof self | undefined;
13
+ declare function getGlobalScope(): GlobalScope;
17
14
 
18
- export { assert, enableDebug, generateHashId, getDebug, getGlobalScope, ifInBrowser, uuid };
15
+ export { assert, generateHashId, getGlobalScope, ifInBrowser, uuid };
package/dist/es/utils.js CHANGED
@@ -1,18 +1,10 @@
1
1
  // src/utils.ts
2
2
  import { sha256 } from "js-sha256";
3
- import debug from "debug";
4
3
  var ifInBrowser = typeof window !== "undefined";
5
4
  function uuid() {
6
5
  return Math.random().toString(36).substring(2, 15);
7
6
  }
8
7
  var hashMap = {};
9
- var topicPrefix = "midscene";
10
- function getDebug(topic) {
11
- return debug(`${topicPrefix}:${topic}`);
12
- }
13
- function enableDebug(topic) {
14
- debug.enable(`${topicPrefix}:${topic}`);
15
- }
16
8
  function generateHashId(rect, content = "") {
17
9
  const combined = JSON.stringify({
18
10
  content,
@@ -48,8 +40,8 @@ function getGlobalScope() {
48
40
  if (typeof window !== "undefined") {
49
41
  return window;
50
42
  }
51
- if (typeof global !== "undefined") {
52
- return global;
43
+ if (typeof globalThis !== "undefined") {
44
+ return globalThis;
53
45
  }
54
46
  if (typeof self !== "undefined") {
55
47
  return self;
@@ -58,9 +50,7 @@ function getGlobalScope() {
58
50
  }
59
51
  export {
60
52
  assert,
61
- enableDebug,
62
53
  generateHashId,
63
- getDebug,
64
54
  getGlobalScope,
65
55
  ifInBrowser,
66
56
  uuid
@@ -1,26 +1,4 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (let key of __getOwnPropNames(from))
11
- if (!__hasOwnProp.call(to, key) && key !== except)
12
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
- }
14
- return to;
15
- };
16
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
- // If the importer is in node compatibility mode or this is not an ESM
18
- // file that has been converted to a CommonJS file using a Babel-
19
- // compatible transform (i.e. "__esModule" has not been set), then set
20
- // "default" to the CommonJS "module.exports" for node compatibility.
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
- mod
23
- ));
24
2
 
25
3
  // src/constants/index.ts
26
4
  var CONTAINER_MINI_HEIGHT = 3;
@@ -96,7 +74,6 @@ function includeBaseElement(node) {
96
74
 
97
75
  // src/utils.ts
98
76
  var import_js_sha256 = require("js-sha256");
99
- var import_debug = __toESM(require("debug"));
100
77
  var hashMap = {};
101
78
  function generateHashId(rect, content = "") {
102
79
  const combined = JSON.stringify({
@@ -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,14 +15,6 @@ 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/extractor/index.ts
@@ -235,7 +225,6 @@ function includeBaseElement(node) {
235
225
 
236
226
  // src/utils.ts
237
227
  var import_js_sha256 = require("js-sha256");
238
- var import_debug = __toESM(require("debug"));
239
228
  var hashMap = {};
240
229
  function generateHashId(rect, content = "") {
241
230
  const combined = JSON.stringify({
package/dist/lib/fs.js CHANGED
@@ -41,7 +41,6 @@ var import_node_path2 = __toESM(require("path"));
41
41
 
42
42
  // src/utils.ts
43
43
  var import_js_sha256 = require("js-sha256");
44
- var import_debug = __toESM(require("debug"));
45
44
  var ifInBrowser = typeof window !== "undefined";
46
45
  function assert(condition, message) {
47
46
  if (!condition) {
@@ -0,0 +1,6 @@
1
+ type DebugFunction = (...args: unknown[]) => void;
2
+ declare function getDebug(topic: string): DebugFunction;
3
+ declare function enableDebug(topic: string): void;
4
+ declare function cleanupLogStreams(): void;
5
+
6
+ export { type DebugFunction, cleanupLogStreams, enableDebug, getDebug };
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
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
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/logger.ts
31
+ var logger_exports = {};
32
+ __export(logger_exports, {
33
+ cleanupLogStreams: () => cleanupLogStreams,
34
+ enableDebug: () => enableDebug,
35
+ getDebug: () => getDebug
36
+ });
37
+ module.exports = __toCommonJS(logger_exports);
38
+ var import_node_fs2 = __toESM(require("fs"));
39
+ var import_node_path2 = __toESM(require("path"));
40
+ var import_debug = __toESM(require("debug"));
41
+
42
+ // src/common.ts
43
+ var import_node_fs = __toESM(require("fs"));
44
+ var import_node_path = __toESM(require("path"));
45
+ var isNodeEnv = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
46
+ var getMidsceneRunLogPath = () => {
47
+ const basePath = import_node_path.default.join(process.cwd(), "midscene_run");
48
+ if (!import_node_fs.default.existsSync(basePath)) {
49
+ import_node_fs.default.mkdirSync(basePath, { recursive: true });
50
+ }
51
+ const logPath = import_node_path.default.join(basePath, "log");
52
+ if (!import_node_fs.default.existsSync(logPath)) {
53
+ import_node_fs.default.mkdirSync(logPath, { recursive: true });
54
+ }
55
+ return logPath;
56
+ };
57
+ var logDir = isNodeEnv ? getMidsceneRunLogPath() : "";
58
+
59
+ // src/logger.ts
60
+ var topicPrefix = "midscene";
61
+ var logStreams = /* @__PURE__ */ new Map();
62
+ function getLogStream(topic) {
63
+ if (!logStreams.has(topic)) {
64
+ const logFile = import_node_path2.default.join(logDir, `${topic}.log`);
65
+ const stream = import_node_fs2.default.createWriteStream(logFile, { flags: "a" });
66
+ logStreams.set(topic, stream);
67
+ }
68
+ return logStreams.get(topic);
69
+ }
70
+ function writeLogToFile(topic, message) {
71
+ if (!isNodeEnv)
72
+ return;
73
+ const stream = getLogStream(topic);
74
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
75
+ stream.write(`[${timestamp}] ${message}
76
+ `);
77
+ }
78
+ function getDebug(topic) {
79
+ return (...args) => {
80
+ const message = args.map(
81
+ (arg) => typeof arg === "object" ? JSON.stringify(arg) : String(arg)
82
+ ).join(" ");
83
+ if (isNodeEnv) {
84
+ writeLogToFile(topic, message);
85
+ }
86
+ const debugFn = (0, import_debug.default)(`${topicPrefix}:${topic}`);
87
+ debugFn(...args);
88
+ };
89
+ }
90
+ function enableDebug(topic) {
91
+ if (isNodeEnv) {
92
+ return;
93
+ }
94
+ import_debug.default.enable(`${topicPrefix}:${topic}`);
95
+ }
96
+ function cleanupLogStreams() {
97
+ if (!isNodeEnv)
98
+ return;
99
+ for (const stream of logStreams.values()) {
100
+ stream.end();
101
+ }
102
+ logStreams.clear();
103
+ }
104
+ // Annotate the CommonJS export names for ESM import in node:
105
+ 0 && (module.exports = {
106
+ cleanupLogStreams,
107
+ enableDebug,
108
+ getDebug
109
+ });
@@ -1,9 +1,5 @@
1
- import debug from 'debug';
2
-
3
1
  declare const ifInBrowser: boolean;
4
2
  declare function uuid(): string;
5
- declare function getDebug(topic: string): debug.Debugger;
6
- declare function enableDebug(topic: string): void;
7
3
  declare function generateHashId(rect: any, content?: string): string;
8
4
  /**
9
5
  * A utility function that asserts a condition and throws an error with a message if the condition is false.
@@ -13,6 +9,7 @@ declare function generateHashId(rect: any, content?: string): string;
13
9
  * @throws Error with the provided message if the condition is false
14
10
  */
15
11
  declare function assert(condition: any, message?: string): asserts condition;
16
- declare function getGlobalScope(): typeof globalThis | undefined;
12
+ type GlobalScope = typeof window | typeof globalThis | typeof self | undefined;
13
+ declare function getGlobalScope(): GlobalScope;
17
14
 
18
- export { assert, enableDebug, generateHashId, getDebug, getGlobalScope, ifInBrowser, uuid };
15
+ export { assert, generateHashId, getGlobalScope, ifInBrowser, uuid };
package/dist/lib/utils.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,42 +15,24 @@ 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/utils.ts
31
21
  var utils_exports = {};
32
22
  __export(utils_exports, {
33
23
  assert: () => assert,
34
- enableDebug: () => enableDebug,
35
24
  generateHashId: () => generateHashId,
36
- getDebug: () => getDebug,
37
25
  getGlobalScope: () => getGlobalScope,
38
26
  ifInBrowser: () => ifInBrowser,
39
27
  uuid: () => uuid
40
28
  });
41
29
  module.exports = __toCommonJS(utils_exports);
42
30
  var import_js_sha256 = require("js-sha256");
43
- var import_debug = __toESM(require("debug"));
44
31
  var ifInBrowser = typeof window !== "undefined";
45
32
  function uuid() {
46
33
  return Math.random().toString(36).substring(2, 15);
47
34
  }
48
35
  var hashMap = {};
49
- var topicPrefix = "midscene";
50
- function getDebug(topic) {
51
- return (0, import_debug.default)(`${topicPrefix}:${topic}`);
52
- }
53
- function enableDebug(topic) {
54
- import_debug.default.enable(`${topicPrefix}:${topic}`);
55
- }
56
36
  function generateHashId(rect, content = "") {
57
37
  const combined = JSON.stringify({
58
38
  content,
@@ -88,8 +68,8 @@ function getGlobalScope() {
88
68
  if (typeof window !== "undefined") {
89
69
  return window;
90
70
  }
91
- if (typeof global !== "undefined") {
92
- return global;
71
+ if (typeof globalThis !== "undefined") {
72
+ return globalThis;
93
73
  }
94
74
  if (typeof self !== "undefined") {
95
75
  return self;
@@ -99,9 +79,7 @@ function getGlobalScope() {
99
79
  // Annotate the CommonJS export names for ESM import in node:
100
80
  0 && (module.exports = {
101
81
  assert,
102
- enableDebug,
103
82
  generateHashId,
104
- getDebug,
105
83
  getGlobalScope,
106
84
  ifInBrowser,
107
85
  uuid