@xiuchang-midscene/shared 2.0.2

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.
Files changed (382) hide show
  1. package/README.md +9 -0
  2. package/dist/es/baseDB.mjs +109 -0
  3. package/dist/es/baseDB.mjs.bak +109 -0
  4. package/dist/es/build/copy-static.mjs +31 -0
  5. package/dist/es/build/copy-static.mjs.bak +31 -0
  6. package/dist/es/build/rspack-config.mjs +4 -0
  7. package/dist/es/build/rspack-config.mjs.bak +4 -0
  8. package/dist/es/cli/cli-runner.mjs +140 -0
  9. package/dist/es/cli/cli-runner.mjs.bak +140 -0
  10. package/dist/es/cli/index.mjs +2 -0
  11. package/dist/es/cli/index.mjs.bak +2 -0
  12. package/dist/es/common.mjs +37 -0
  13. package/dist/es/common.mjs.bak +37 -0
  14. package/dist/es/constants/example-code.mjs +223 -0
  15. package/dist/es/constants/example-code.mjs.bak +223 -0
  16. package/dist/es/constants/index.mjs +23 -0
  17. package/dist/es/constants/index.mjs.bak +23 -0
  18. package/dist/es/env/basic.mjs +6 -0
  19. package/dist/es/env/basic.mjs.bak +6 -0
  20. package/dist/es/env/constants.mjs +70 -0
  21. package/dist/es/env/constants.mjs.bak +70 -0
  22. package/dist/es/env/global-config-manager.mjs +94 -0
  23. package/dist/es/env/global-config-manager.mjs.bak +94 -0
  24. package/dist/es/env/helper.mjs +43 -0
  25. package/dist/es/env/helper.mjs.bak +43 -0
  26. package/dist/es/env/index.mjs +5 -0
  27. package/dist/es/env/index.mjs.bak +5 -0
  28. package/dist/es/env/init-debug.mjs +18 -0
  29. package/dist/es/env/init-debug.mjs.bak +18 -0
  30. package/dist/es/env/model-config-manager.mjs +79 -0
  31. package/dist/es/env/model-config-manager.mjs.bak +79 -0
  32. package/dist/es/env/parse-model-config.mjs +132 -0
  33. package/dist/es/env/parse-model-config.mjs.bak +132 -0
  34. package/dist/es/env/types.mjs +220 -0
  35. package/dist/es/env/types.mjs.bak +220 -0
  36. package/dist/es/env/utils.mjs +26 -0
  37. package/dist/es/env/utils.mjs.bak +26 -0
  38. package/dist/es/extractor/constants.mjs +2 -0
  39. package/dist/es/extractor/constants.mjs.bak +2 -0
  40. package/dist/es/extractor/debug.mjs +6 -0
  41. package/dist/es/extractor/debug.mjs.bak +6 -0
  42. package/dist/es/extractor/dom-util.mjs +92 -0
  43. package/dist/es/extractor/dom-util.mjs.bak +92 -0
  44. package/dist/es/extractor/index.mjs +5 -0
  45. package/dist/es/extractor/index.mjs.bak +5 -0
  46. package/dist/es/extractor/locator.mjs +250 -0
  47. package/dist/es/extractor/locator.mjs.bak +250 -0
  48. package/dist/es/extractor/tree.mjs +78 -0
  49. package/dist/es/extractor/tree.mjs.bak +78 -0
  50. package/dist/es/extractor/util.mjs +245 -0
  51. package/dist/es/extractor/util.mjs.bak +245 -0
  52. package/dist/es/extractor/web-extractor.mjs +303 -0
  53. package/dist/es/extractor/web-extractor.mjs.bak +303 -0
  54. package/dist/es/img/box-select.mjs +824 -0
  55. package/dist/es/img/box-select.mjs.bak +824 -0
  56. package/dist/es/img/canvas-fallback.mjs +238 -0
  57. package/dist/es/img/canvas-fallback.mjs.bak +238 -0
  58. package/dist/es/img/get-photon.mjs +45 -0
  59. package/dist/es/img/get-photon.mjs.bak +45 -0
  60. package/dist/es/img/get-sharp.mjs +11 -0
  61. package/dist/es/img/get-sharp.mjs.bak +11 -0
  62. package/dist/es/img/index.mjs +4 -0
  63. package/dist/es/img/index.mjs.bak +4 -0
  64. package/dist/es/img/info.mjs +29 -0
  65. package/dist/es/img/info.mjs.bak +29 -0
  66. package/dist/es/img/transform.mjs +295 -0
  67. package/dist/es/img/transform.mjs.bak +295 -0
  68. package/dist/es/index.mjs +4 -0
  69. package/dist/es/index.mjs.bak +4 -0
  70. package/dist/es/logger.mjs +64 -0
  71. package/dist/es/logger.mjs.bak +64 -0
  72. package/dist/es/mcp/base-server.mjs +281 -0
  73. package/dist/es/mcp/base-server.mjs.bak +281 -0
  74. package/dist/es/mcp/base-tools.mjs +91 -0
  75. package/dist/es/mcp/base-tools.mjs.bak +91 -0
  76. package/dist/es/mcp/chrome-path.mjs +35 -0
  77. package/dist/es/mcp/chrome-path.mjs.bak +35 -0
  78. package/dist/es/mcp/index.mjs +7 -0
  79. package/dist/es/mcp/index.mjs.bak +7 -0
  80. package/dist/es/mcp/inject-report-html-plugin.mjs +53 -0
  81. package/dist/es/mcp/inject-report-html-plugin.mjs.bak +53 -0
  82. package/dist/es/mcp/launcher-helper.mjs +52 -0
  83. package/dist/es/mcp/launcher-helper.mjs.bak +52 -0
  84. package/dist/es/mcp/tool-generator.mjs +297 -0
  85. package/dist/es/mcp/tool-generator.mjs.bak +297 -0
  86. package/dist/es/mcp/types.mjs +3 -0
  87. package/dist/es/mcp/types.mjs.bak +3 -0
  88. package/dist/es/node/fs.mjs +44 -0
  89. package/dist/es/node/fs.mjs.bak +44 -0
  90. package/dist/es/node/index.mjs +2 -0
  91. package/dist/es/node/index.mjs.bak +2 -0
  92. package/dist/es/node/port.mjs +24 -0
  93. package/dist/es/node/port.mjs.bak +24 -0
  94. package/dist/es/oss/demo.mjs +30 -0
  95. package/dist/es/oss/demo.mjs.bak +30 -0
  96. package/dist/es/oss/index.mjs +90 -0
  97. package/dist/es/oss/index.mjs.bak +90 -0
  98. package/dist/es/polyfills/async-hooks.mjs +2 -0
  99. package/dist/es/polyfills/async-hooks.mjs.bak +2 -0
  100. package/dist/es/polyfills/index.mjs +1 -0
  101. package/dist/es/polyfills/index.mjs.bak +1 -0
  102. package/dist/es/types/index.mjs +3 -0
  103. package/dist/es/types/index.mjs.bak +3 -0
  104. package/dist/es/us-keyboard-layout.mjs +1414 -0
  105. package/dist/es/us-keyboard-layout.mjs.LICENSE.txt +5 -0
  106. package/dist/es/us-keyboard-layout.mjs.bak +1414 -0
  107. package/dist/es/utils.mjs +72 -0
  108. package/dist/es/utils.mjs.bak +72 -0
  109. package/dist/es/zod-schema-utils.mjs +54 -0
  110. package/dist/es/zod-schema-utils.mjs.bak +54 -0
  111. package/dist/lib/baseDB.js +149 -0
  112. package/dist/lib/baseDB.js.bak +149 -0
  113. package/dist/lib/build/copy-static.js +79 -0
  114. package/dist/lib/build/copy-static.js.bak +79 -0
  115. package/dist/lib/build/rspack-config.js +38 -0
  116. package/dist/lib/build/rspack-config.js.bak +38 -0
  117. package/dist/lib/cli/cli-runner.js +196 -0
  118. package/dist/lib/cli/cli-runner.js.bak +196 -0
  119. package/dist/lib/cli/index.js +48 -0
  120. package/dist/lib/cli/index.js.bak +48 -0
  121. package/dist/lib/common.js +93 -0
  122. package/dist/lib/common.js.bak +93 -0
  123. package/dist/lib/constants/example-code.js +260 -0
  124. package/dist/lib/constants/example-code.js.bak +260 -0
  125. package/dist/lib/constants/index.js +96 -0
  126. package/dist/lib/constants/index.js.bak +96 -0
  127. package/dist/lib/env/basic.js +40 -0
  128. package/dist/lib/env/basic.js.bak +40 -0
  129. package/dist/lib/env/constants.js +113 -0
  130. package/dist/lib/env/constants.js.bak +113 -0
  131. package/dist/lib/env/global-config-manager.js +128 -0
  132. package/dist/lib/env/global-config-manager.js.bak +128 -0
  133. package/dist/lib/env/helper.js +80 -0
  134. package/dist/lib/env/helper.js.bak +80 -0
  135. package/dist/lib/env/index.js +90 -0
  136. package/dist/lib/env/index.js.bak +90 -0
  137. package/dist/lib/env/init-debug.js +52 -0
  138. package/dist/lib/env/init-debug.js.bak +52 -0
  139. package/dist/lib/env/model-config-manager.js +113 -0
  140. package/dist/lib/env/model-config-manager.js.bak +113 -0
  141. package/dist/lib/env/parse-model-config.js +178 -0
  142. package/dist/lib/env/parse-model-config.js.bak +178 -0
  143. package/dist/lib/env/types.js +554 -0
  144. package/dist/lib/env/types.js.bak +554 -0
  145. package/dist/lib/env/utils.js +72 -0
  146. package/dist/lib/env/utils.js.bak +72 -0
  147. package/dist/lib/extractor/constants.js +42 -0
  148. package/dist/lib/extractor/constants.js.bak +42 -0
  149. package/dist/lib/extractor/debug.js +12 -0
  150. package/dist/lib/extractor/debug.js.bak +12 -0
  151. package/dist/lib/extractor/dom-util.js +153 -0
  152. package/dist/lib/extractor/dom-util.js.bak +153 -0
  153. package/dist/lib/extractor/index.js +81 -0
  154. package/dist/lib/extractor/index.js.bak +81 -0
  155. package/dist/lib/extractor/locator.js +296 -0
  156. package/dist/lib/extractor/locator.js.bak +296 -0
  157. package/dist/lib/extractor/tree.js +124 -0
  158. package/dist/lib/extractor/tree.js.bak +124 -0
  159. package/dist/lib/extractor/util.js +336 -0
  160. package/dist/lib/extractor/util.js.bak +336 -0
  161. package/dist/lib/extractor/web-extractor.js +349 -0
  162. package/dist/lib/extractor/web-extractor.js.bak +349 -0
  163. package/dist/lib/img/box-select.js +875 -0
  164. package/dist/lib/img/box-select.js.bak +875 -0
  165. package/dist/lib/img/canvas-fallback.js +305 -0
  166. package/dist/lib/img/canvas-fallback.js.bak +305 -0
  167. package/dist/lib/img/get-photon.js +82 -0
  168. package/dist/lib/img/get-photon.js.bak +82 -0
  169. package/dist/lib/img/get-sharp.js +45 -0
  170. package/dist/lib/img/get-sharp.js.bak +45 -0
  171. package/dist/lib/img/index.js +95 -0
  172. package/dist/lib/img/index.js.bak +95 -0
  173. package/dist/lib/img/info.js +83 -0
  174. package/dist/lib/img/info.js.bak +83 -0
  175. package/dist/lib/img/transform.js +387 -0
  176. package/dist/lib/img/transform.js.bak +387 -0
  177. package/dist/lib/index.js +47 -0
  178. package/dist/lib/index.js.bak +47 -0
  179. package/dist/lib/logger.js +114 -0
  180. package/dist/lib/logger.js.bak +114 -0
  181. package/dist/lib/mcp/base-server.js +331 -0
  182. package/dist/lib/mcp/base-server.js.bak +331 -0
  183. package/dist/lib/mcp/base-tools.js +125 -0
  184. package/dist/lib/mcp/base-tools.js.bak +125 -0
  185. package/dist/lib/mcp/chrome-path.js +72 -0
  186. package/dist/lib/mcp/chrome-path.js.bak +72 -0
  187. package/dist/lib/mcp/index.js +100 -0
  188. package/dist/lib/mcp/index.js.bak +100 -0
  189. package/dist/lib/mcp/inject-report-html-plugin.js +98 -0
  190. package/dist/lib/mcp/inject-report-html-plugin.js.bak +98 -0
  191. package/dist/lib/mcp/launcher-helper.js +86 -0
  192. package/dist/lib/mcp/launcher-helper.js.bak +86 -0
  193. package/dist/lib/mcp/tool-generator.js +334 -0
  194. package/dist/lib/mcp/tool-generator.js.bak +334 -0
  195. package/dist/lib/mcp/types.js +40 -0
  196. package/dist/lib/mcp/types.js.bak +40 -0
  197. package/dist/lib/node/fs.js +97 -0
  198. package/dist/lib/node/fs.js.bak +97 -0
  199. package/dist/lib/node/index.js +65 -0
  200. package/dist/lib/node/index.js.bak +65 -0
  201. package/dist/lib/node/port.js +61 -0
  202. package/dist/lib/node/port.js.bak +61 -0
  203. package/dist/lib/oss/demo.js +36 -0
  204. package/dist/lib/oss/demo.js.bak +36 -0
  205. package/dist/lib/oss/index.js +138 -0
  206. package/dist/lib/oss/index.js.bak +138 -0
  207. package/dist/lib/polyfills/async-hooks.js +36 -0
  208. package/dist/lib/polyfills/async-hooks.js.bak +36 -0
  209. package/dist/lib/polyfills/index.js +58 -0
  210. package/dist/lib/polyfills/index.js.bak +58 -0
  211. package/dist/lib/types/index.js +37 -0
  212. package/dist/lib/types/index.js.bak +37 -0
  213. package/dist/lib/us-keyboard-layout.js +1457 -0
  214. package/dist/lib/us-keyboard-layout.js.LICENSE.txt +5 -0
  215. package/dist/lib/us-keyboard-layout.js.bak +1457 -0
  216. package/dist/lib/utils.js +148 -0
  217. package/dist/lib/utils.js.bak +148 -0
  218. package/dist/lib/zod-schema-utils.js +97 -0
  219. package/dist/lib/zod-schema-utils.js.bak +97 -0
  220. package/dist/types/baseDB.d.ts +25 -0
  221. package/dist/types/baseDB.d.ts.bak +25 -0
  222. package/dist/types/build/copy-static.d.ts +31 -0
  223. package/dist/types/build/copy-static.d.ts.bak +31 -0
  224. package/dist/types/build/rspack-config.d.ts +8 -0
  225. package/dist/types/build/rspack-config.d.ts.bak +8 -0
  226. package/dist/types/cli/cli-runner.d.ts +14 -0
  227. package/dist/types/cli/cli-runner.d.ts.bak +14 -0
  228. package/dist/types/cli/index.d.ts +2 -0
  229. package/dist/types/cli/index.d.ts.bak +2 -0
  230. package/dist/types/common.d.ts +12 -0
  231. package/dist/types/common.d.ts.bak +12 -0
  232. package/dist/types/constants/example-code.d.ts +2 -0
  233. package/dist/types/constants/example-code.d.ts.bak +2 -0
  234. package/dist/types/constants/index.d.ts +21 -0
  235. package/dist/types/constants/index.d.ts.bak +21 -0
  236. package/dist/types/env/basic.d.ts +6 -0
  237. package/dist/types/env/basic.d.ts.bak +6 -0
  238. package/dist/types/env/constants.d.ts +40 -0
  239. package/dist/types/env/constants.d.ts.bak +40 -0
  240. package/dist/types/env/global-config-manager.d.ts +32 -0
  241. package/dist/types/env/global-config-manager.d.ts.bak +32 -0
  242. package/dist/types/env/helper.d.ts +4 -0
  243. package/dist/types/env/helper.d.ts.bak +4 -0
  244. package/dist/types/env/index.d.ts +4 -0
  245. package/dist/types/env/index.d.ts.bak +4 -0
  246. package/dist/types/env/init-debug.d.ts +1 -0
  247. package/dist/types/env/init-debug.d.ts.bak +1 -0
  248. package/dist/types/env/model-config-manager.d.ts +25 -0
  249. package/dist/types/env/model-config-manager.d.ts.bak +25 -0
  250. package/dist/types/env/parse-model-config.d.ts +31 -0
  251. package/dist/types/env/parse-model-config.d.ts.bak +31 -0
  252. package/dist/types/env/types.d.ts +318 -0
  253. package/dist/types/env/types.d.ts.bak +318 -0
  254. package/dist/types/env/utils.d.ts +38 -0
  255. package/dist/types/env/utils.d.ts.bak +38 -0
  256. package/dist/types/extractor/constants.d.ts +1 -0
  257. package/dist/types/extractor/constants.d.ts.bak +1 -0
  258. package/dist/types/extractor/debug.d.ts +1 -0
  259. package/dist/types/extractor/debug.d.ts.bak +1 -0
  260. package/dist/types/extractor/dom-util.d.ts +56 -0
  261. package/dist/types/extractor/dom-util.d.ts.bak +56 -0
  262. package/dist/types/extractor/index.d.ts +32 -0
  263. package/dist/types/extractor/index.d.ts.bak +32 -0
  264. package/dist/types/extractor/locator.d.ts +9 -0
  265. package/dist/types/extractor/locator.d.ts.bak +9 -0
  266. package/dist/types/extractor/tree.d.ts +6 -0
  267. package/dist/types/extractor/tree.d.ts.bak +6 -0
  268. package/dist/types/extractor/util.d.ts +47 -0
  269. package/dist/types/extractor/util.d.ts.bak +47 -0
  270. package/dist/types/extractor/web-extractor.d.ts +19 -0
  271. package/dist/types/extractor/web-extractor.d.ts.bak +19 -0
  272. package/dist/types/img/box-select.d.ts +26 -0
  273. package/dist/types/img/box-select.d.ts.bak +26 -0
  274. package/dist/types/img/canvas-fallback.d.ts +105 -0
  275. package/dist/types/img/canvas-fallback.d.ts.bak +105 -0
  276. package/dist/types/img/get-photon.d.ts +19 -0
  277. package/dist/types/img/get-photon.d.ts.bak +19 -0
  278. package/dist/types/img/get-sharp.d.ts +3 -0
  279. package/dist/types/img/get-sharp.d.ts.bak +3 -0
  280. package/dist/types/img/index.d.ts +3 -0
  281. package/dist/types/img/index.d.ts.bak +3 -0
  282. package/dist/types/img/info.d.ts +29 -0
  283. package/dist/types/img/info.d.ts.bak +29 -0
  284. package/dist/types/img/transform.d.ts +107 -0
  285. package/dist/types/img/transform.d.ts.bak +107 -0
  286. package/dist/types/index.d.ts +4 -0
  287. package/dist/types/index.d.ts.bak +4 -0
  288. package/dist/types/logger.d.ts +5 -0
  289. package/dist/types/logger.d.ts.bak +5 -0
  290. package/dist/types/mcp/base-server.d.ts +93 -0
  291. package/dist/types/mcp/base-server.d.ts.bak +93 -0
  292. package/dist/types/mcp/base-tools.d.ts +79 -0
  293. package/dist/types/mcp/base-tools.d.ts.bak +79 -0
  294. package/dist/types/mcp/chrome-path.d.ts +2 -0
  295. package/dist/types/mcp/chrome-path.d.ts.bak +2 -0
  296. package/dist/types/mcp/index.d.ts +7 -0
  297. package/dist/types/mcp/index.d.ts.bak +7 -0
  298. package/dist/types/mcp/inject-report-html-plugin.d.ts +18 -0
  299. package/dist/types/mcp/inject-report-html-plugin.d.ts.bak +18 -0
  300. package/dist/types/mcp/launcher-helper.d.ts +94 -0
  301. package/dist/types/mcp/launcher-helper.d.ts.bak +94 -0
  302. package/dist/types/mcp/tool-generator.d.ts +10 -0
  303. package/dist/types/mcp/tool-generator.d.ts.bak +10 -0
  304. package/dist/types/mcp/types.d.ts +103 -0
  305. package/dist/types/mcp/types.d.ts.bak +103 -0
  306. package/dist/types/node/fs.d.ts +15 -0
  307. package/dist/types/node/fs.d.ts.bak +15 -0
  308. package/dist/types/node/index.d.ts +2 -0
  309. package/dist/types/node/index.d.ts.bak +2 -0
  310. package/dist/types/node/port.d.ts +8 -0
  311. package/dist/types/node/port.d.ts.bak +8 -0
  312. package/dist/types/oss/demo.d.ts +1 -0
  313. package/dist/types/oss/demo.d.ts.bak +1 -0
  314. package/dist/types/oss/index.d.ts +34 -0
  315. package/dist/types/oss/index.d.ts.bak +34 -0
  316. package/dist/types/polyfills/async-hooks.d.ts +6 -0
  317. package/dist/types/polyfills/async-hooks.d.ts.bak +6 -0
  318. package/dist/types/polyfills/index.d.ts +4 -0
  319. package/dist/types/polyfills/index.d.ts.bak +4 -0
  320. package/dist/types/types/index.d.ts +34 -0
  321. package/dist/types/types/index.d.ts.bak +34 -0
  322. package/dist/types/us-keyboard-layout.d.ts +32 -0
  323. package/dist/types/us-keyboard-layout.d.ts.bak +32 -0
  324. package/dist/types/utils.d.ts +34 -0
  325. package/dist/types/utils.d.ts.bak +34 -0
  326. package/dist/types/zod-schema-utils.d.ts +23 -0
  327. package/dist/types/zod-schema-utils.d.ts.bak +23 -0
  328. package/package.json +132 -0
  329. package/src/baseDB.ts +158 -0
  330. package/src/build/copy-static.ts +68 -0
  331. package/src/build/rspack-config.ts +12 -0
  332. package/src/cli/cli-runner.ts +224 -0
  333. package/src/cli/index.ts +8 -0
  334. package/src/common.ts +67 -0
  335. package/src/constants/example-code.ts +223 -0
  336. package/src/constants/index.ts +29 -0
  337. package/src/env/basic.ts +12 -0
  338. package/src/env/constants.ts +234 -0
  339. package/src/env/global-config-manager.ts +191 -0
  340. package/src/env/helper.ts +58 -0
  341. package/src/env/index.ts +4 -0
  342. package/src/env/init-debug.ts +34 -0
  343. package/src/env/model-config-manager.ts +149 -0
  344. package/src/env/parse-model-config.ts +294 -0
  345. package/src/env/types.ts +547 -0
  346. package/src/env/utils.ts +89 -0
  347. package/src/extractor/constants.ts +5 -0
  348. package/src/extractor/debug.ts +10 -0
  349. package/src/extractor/dom-util.ts +226 -0
  350. package/src/extractor/index.ts +48 -0
  351. package/src/extractor/locator.ts +469 -0
  352. package/src/extractor/tree.ts +179 -0
  353. package/src/extractor/util.ts +482 -0
  354. package/src/extractor/web-extractor.ts +481 -0
  355. package/src/img/box-select.ts +588 -0
  356. package/src/img/canvas-fallback.ts +393 -0
  357. package/src/img/get-photon.ts +108 -0
  358. package/src/img/get-sharp.ts +18 -0
  359. package/src/img/index.ts +26 -0
  360. package/src/img/info.ts +75 -0
  361. package/src/img/transform.ts +594 -0
  362. package/src/index.ts +8 -0
  363. package/src/logger.ts +96 -0
  364. package/src/mcp/base-server.ts +502 -0
  365. package/src/mcp/base-tools.ts +185 -0
  366. package/src/mcp/chrome-path.ts +48 -0
  367. package/src/mcp/index.ts +7 -0
  368. package/src/mcp/inject-report-html-plugin.ts +119 -0
  369. package/src/mcp/launcher-helper.ts +200 -0
  370. package/src/mcp/tool-generator.ts +429 -0
  371. package/src/mcp/types.ts +112 -0
  372. package/src/node/fs.ts +84 -0
  373. package/src/node/index.ts +2 -0
  374. package/src/node/port.ts +37 -0
  375. package/src/oss/demo.ts +61 -0
  376. package/src/oss/index.ts +187 -0
  377. package/src/polyfills/async-hooks.ts +6 -0
  378. package/src/polyfills/index.ts +4 -0
  379. package/src/types/index.ts +52 -0
  380. package/src/us-keyboard-layout.ts +723 -0
  381. package/src/utils.ts +149 -0
  382. package/src/zod-schema-utils.ts +133 -0
@@ -0,0 +1,295 @@
1
+ import node_assert from "node:assert";
2
+ import { Buffer } from "node:buffer";
3
+ import { readFileSync } from "node:fs";
4
+ import { writeFile } from "node:fs/promises";
5
+ import node_path from "node:path";
6
+ import { getDebug } from "../logger.mjs";
7
+ import { ifInNode } from "../utils.mjs";
8
+ import get_photon from "./get-photon.mjs";
9
+ import get_sharp from "./get-sharp.mjs";
10
+ const imgDebug = getDebug('img');
11
+ async function saveBase64Image(options) {
12
+ const { base64Data, outputPath } = options;
13
+ const { body } = parseBase64(base64Data);
14
+ const imageBuffer = Buffer.from(body, 'base64');
15
+ await writeFile(outputPath, imageBuffer);
16
+ }
17
+ async function resizeAndConvertImgBuffer(inputFormat, inputData, newSize) {
18
+ if ('string' == typeof inputData) throw Error('inputData is base64, use resizeImgBase64 instead');
19
+ node_assert(newSize && newSize.width > 0 && newSize.height > 0, 'newSize must be positive');
20
+ const resizeStartTime = Date.now();
21
+ imgDebug(`resizeImg start, target size: ${newSize.width}x${newSize.height}`);
22
+ if (ifInNode) try {
23
+ const Sharp = await get_sharp();
24
+ const metadata = await Sharp(inputData).metadata();
25
+ const { width: originalWidth, height: originalHeight } = metadata;
26
+ if (!originalWidth || !originalHeight) throw Error('Undefined width or height from the input image.');
27
+ if (newSize.width === originalWidth && newSize.height === originalHeight) return {
28
+ buffer: inputData,
29
+ format: inputFormat
30
+ };
31
+ const resizedBuffer = await Sharp(inputData).resize(newSize.width, newSize.height).jpeg({
32
+ quality: 90
33
+ }).toBuffer();
34
+ const resizeEndTime = Date.now();
35
+ imgDebug(`resizeImg done (Sharp), target size: ${newSize.width}x${newSize.height}, cost: ${resizeEndTime - resizeStartTime}ms`);
36
+ return {
37
+ buffer: resizedBuffer,
38
+ format: 'jpeg'
39
+ };
40
+ } catch (error) {
41
+ imgDebug('Sharp failed, falling back to Photon:', error);
42
+ }
43
+ const { PhotonImage, SamplingFilter, resize } = await get_photon();
44
+ const inputBytes = new Uint8Array(inputData);
45
+ const bytesliceResult = PhotonImage.new_from_byteslice(inputBytes);
46
+ const inputImage = bytesliceResult instanceof Promise ? await bytesliceResult : bytesliceResult;
47
+ const originalWidth = inputImage.get_width();
48
+ const originalHeight = inputImage.get_height();
49
+ if (!originalWidth || !originalHeight) {
50
+ inputImage.free();
51
+ throw Error('Undefined width or height from the input image.');
52
+ }
53
+ if (newSize.width === originalWidth && newSize.height === originalHeight) {
54
+ inputImage.free();
55
+ return {
56
+ buffer: inputData,
57
+ format: inputFormat
58
+ };
59
+ }
60
+ const outputImage = resize(inputImage, newSize.width, newSize.height, SamplingFilter.CatmullRom);
61
+ const outputBytes = outputImage.get_bytes_jpeg(90);
62
+ const resizedBuffer = Buffer.from(outputBytes);
63
+ inputImage.free();
64
+ outputImage.free();
65
+ const resizeEndTime = Date.now();
66
+ imgDebug(`resizeImg done (Photon), target size: ${newSize.width}x${newSize.height}, cost: ${resizeEndTime - resizeStartTime}ms`);
67
+ return {
68
+ buffer: resizedBuffer,
69
+ format: 'jpeg'
70
+ };
71
+ }
72
+ const createImgBase64ByFormat = (format, body)=>`data:image/${format};base64,${body}`;
73
+ async function resizeImgBase64(inputBase64, newSize) {
74
+ const { body, mimeType } = parseBase64(inputBase64);
75
+ const imageBuffer = Buffer.from(body, 'base64');
76
+ const { buffer, format } = await resizeAndConvertImgBuffer(mimeType.split('/')[1], imageBuffer, newSize);
77
+ return createImgBase64ByFormat(format, buffer.toString('base64'));
78
+ }
79
+ function zoomForGPT4o(originalWidth, originalHeight) {
80
+ const maxWidth = 2048;
81
+ const maxHeight = 768;
82
+ let newWidth = originalWidth;
83
+ let newHeight = originalHeight;
84
+ const aspectRatio = originalWidth / originalHeight;
85
+ if (originalWidth > maxWidth) {
86
+ newWidth = maxWidth;
87
+ newHeight = newWidth / aspectRatio;
88
+ }
89
+ if (newHeight > maxHeight) {
90
+ newHeight = maxHeight;
91
+ newWidth = newHeight * aspectRatio;
92
+ }
93
+ return {
94
+ width: Math.round(newWidth),
95
+ height: Math.round(newHeight)
96
+ };
97
+ }
98
+ async function photonFromBase64(base64) {
99
+ const { PhotonImage } = await get_photon();
100
+ const { body } = parseBase64(base64);
101
+ const result = PhotonImage.new_from_base64(body);
102
+ return result instanceof Promise ? await result : result;
103
+ }
104
+ async function paddingToMatchBlock(image, blockSize = 28) {
105
+ const width = image.get_width();
106
+ const height = image.get_height();
107
+ const targetWidth = Math.ceil(width / blockSize) * blockSize;
108
+ const targetHeight = Math.ceil(height / blockSize) * blockSize;
109
+ if (targetWidth === width && targetHeight === height) return {
110
+ width,
111
+ height,
112
+ image
113
+ };
114
+ const { padding_right, padding_bottom, Rgba } = await get_photon();
115
+ const rightPadding = targetWidth - width;
116
+ const bottomPadding = targetHeight - height;
117
+ let result = image;
118
+ if (rightPadding > 0) {
119
+ const white = new Rgba(255, 255, 255, 255);
120
+ result = padding_right(result, rightPadding, white);
121
+ }
122
+ if (bottomPadding > 0) {
123
+ const white = new Rgba(255, 255, 255, 255);
124
+ const previousResult = result;
125
+ result = padding_bottom(previousResult, bottomPadding, white);
126
+ if (previousResult !== image) previousResult.free();
127
+ }
128
+ return {
129
+ width: targetWidth,
130
+ height: targetHeight,
131
+ image: result
132
+ };
133
+ }
134
+ async function paddingToMatchBlockByBase64(imageBase64, blockSize = 28) {
135
+ const photonImage = await photonFromBase64(imageBase64);
136
+ try {
137
+ const paddedResult = await paddingToMatchBlock(photonImage, blockSize);
138
+ const result = {
139
+ width: paddedResult.width,
140
+ height: paddedResult.height,
141
+ imageBase64: await photonToBase64(paddedResult.image)
142
+ };
143
+ if (paddedResult.image !== photonImage) paddedResult.image.free();
144
+ return result;
145
+ } finally{
146
+ photonImage.free();
147
+ }
148
+ }
149
+ async function cropByRect(imageBase64, rect, paddingImage) {
150
+ const { crop } = await get_photon();
151
+ const photonImage = await photonFromBase64(imageBase64);
152
+ const { left, top, width, height } = rect;
153
+ const cropped = crop(photonImage, left, top, left + width, top + height);
154
+ photonImage.free();
155
+ try {
156
+ if (paddingImage) {
157
+ const paddedResult = await paddingToMatchBlock(cropped);
158
+ const result = {
159
+ width: paddedResult.width,
160
+ height: paddedResult.height,
161
+ imageBase64: await photonToBase64(paddedResult.image)
162
+ };
163
+ if (paddedResult.image !== cropped) paddedResult.image.free();
164
+ return result;
165
+ }
166
+ return {
167
+ width: cropped.get_width(),
168
+ height: cropped.get_height(),
169
+ imageBase64: await photonToBase64(cropped)
170
+ };
171
+ } finally{
172
+ cropped.free();
173
+ }
174
+ }
175
+ async function photonToBase64(image, quality = 90) {
176
+ const bytes = image.get_bytes_jpeg(quality);
177
+ const base64Body = Buffer.from(bytes).toString('base64');
178
+ return `data:image/jpeg;base64,${base64Body}`;
179
+ }
180
+ const httpImg2Base64 = async (url)=>{
181
+ const response = await fetch(url);
182
+ if (!response.ok) throw new Error(`Failed to fetch image: ${url}`);
183
+ const contentType = response.headers.get('content-type');
184
+ if (!contentType) throw new Error(`Failed to fetch image: ${url}`);
185
+ node_assert(contentType.startsWith('image/'), `The url ${url} is not a image, because of content-type in header is ${contentType}.`);
186
+ const buffer = Buffer.from(await response.arrayBuffer());
187
+ return `data:${contentType};base64,${buffer.toString('base64')}`;
188
+ };
189
+ const localImg2Base64 = (imgPath, withoutHeader = false)=>{
190
+ const body = readFileSync(imgPath).toString('base64');
191
+ if (withoutHeader) return body;
192
+ const type = node_path.extname(imgPath).slice(1);
193
+ const finalType = 'svg' === type ? 'svg+xml' : type || 'jpg';
194
+ return `data:image/${finalType};base64,${body}`;
195
+ };
196
+ const preProcessImageUrl = async (url, convertHttpImage2Base64)=>{
197
+ if ('string' != typeof url) throw new Error(`url must be a string, but got ${url} with type ${typeof url}`);
198
+ if (url.startsWith('data:')) return url;
199
+ if (!(url.startsWith('http://') || url.startsWith('https://'))) return await localImg2Base64(url);
200
+ if (!convertHttpImage2Base64) return url;
201
+ return await httpImg2Base64(url);
202
+ };
203
+ const parseBase64 = (fullBase64String)=>{
204
+ try {
205
+ const separator = ';base64,';
206
+ const index = fullBase64String.indexOf(separator);
207
+ if (-1 === index) throw new Error('Invalid base64 string');
208
+ return {
209
+ mimeType: fullBase64String.slice(5, index),
210
+ body: fullBase64String.slice(index + separator.length)
211
+ };
212
+ } catch (e) {
213
+ throw new Error(`parseBase64 fail because intput is not a valid base64 string: ${fullBase64String}`, {
214
+ cause: e
215
+ });
216
+ }
217
+ };
218
+ async function convertToJpegBase64(inputBase64, quality) {
219
+ const { body } = parseBase64(inputBase64);
220
+ const buffer = Buffer.from(body, 'base64');
221
+ if (ifInNode) try {
222
+ const Sharp = await get_sharp();
223
+ const jpegBuffer = await Sharp(buffer).jpeg({
224
+ quality
225
+ }).toBuffer();
226
+ return `data:image/jpeg;base64,${jpegBuffer.toString('base64')}`;
227
+ } catch (error) {
228
+ imgDebug('Sharp failed for JPEG conversion, falling back to Photon:', error);
229
+ }
230
+ const { PhotonImage } = await get_photon();
231
+ const inputBytes = new Uint8Array(buffer);
232
+ const bytesliceResult = PhotonImage.new_from_byteslice(inputBytes);
233
+ const inputImage = bytesliceResult instanceof Promise ? await bytesliceResult : bytesliceResult;
234
+ const outputBytes = inputImage.get_bytes_jpeg(quality);
235
+ inputImage.free();
236
+ return `data:image/jpeg;base64,${Buffer.from(outputBytes).toString('base64')}`;
237
+ }
238
+ async function scaleImage(imageBase64, scale) {
239
+ if (scale <= 0) throw new Error('Scale factor must be positive');
240
+ const { body } = parseBase64(imageBase64);
241
+ const buffer = Buffer.from(body, 'base64');
242
+ const scaleStartTime = Date.now();
243
+ imgDebug(`scaleImage start, scale factor: ${scale}`);
244
+ if (ifInNode) try {
245
+ const Sharp = await get_sharp();
246
+ const metadata = await Sharp(buffer).metadata();
247
+ const originalWidth = metadata.width || 0;
248
+ const originalHeight = metadata.height || 0;
249
+ if (0 === originalWidth || 0 === originalHeight) throw new Error('Failed to get image dimensions');
250
+ const newWidth = Math.round(originalWidth * scale);
251
+ const newHeight = Math.round(originalHeight * scale);
252
+ const resizedBuffer = await Sharp(buffer).resize(newWidth, newHeight, {
253
+ kernel: 'lanczos3',
254
+ fit: 'fill'
255
+ }).jpeg({
256
+ quality: 90
257
+ }).toBuffer();
258
+ const scaleEndTime = Date.now();
259
+ imgDebug(`scaleImage done (Sharp): ${originalWidth}x${originalHeight} -> ${newWidth}x${newHeight} (scale=${scale}), cost: ${scaleEndTime - scaleStartTime}ms`);
260
+ const base64 = `data:image/jpeg;base64,${resizedBuffer.toString('base64')}`;
261
+ return {
262
+ width: newWidth,
263
+ height: newHeight,
264
+ imageBase64: base64
265
+ };
266
+ } catch (error) {
267
+ imgDebug('Sharp failed, falling back to Photon:', error);
268
+ }
269
+ const { PhotonImage, SamplingFilter, resize } = await get_photon();
270
+ const inputBytes = new Uint8Array(buffer);
271
+ const bytesliceResult = PhotonImage.new_from_byteslice(inputBytes);
272
+ const inputImage = bytesliceResult instanceof Promise ? await bytesliceResult : bytesliceResult;
273
+ const originalWidth = inputImage.get_width();
274
+ const originalHeight = inputImage.get_height();
275
+ if (!originalWidth || !originalHeight) {
276
+ inputImage.free();
277
+ throw new Error('Failed to get image dimensions');
278
+ }
279
+ const newWidth = Math.round(originalWidth * scale);
280
+ const newHeight = Math.round(originalHeight * scale);
281
+ const outputImage = resize(inputImage, newWidth, newHeight, SamplingFilter.CatmullRom);
282
+ const outputBytes = outputImage.get_bytes_jpeg(90);
283
+ const resizedBuffer = Buffer.from(outputBytes);
284
+ inputImage.free();
285
+ outputImage.free();
286
+ const scaleEndTime = Date.now();
287
+ imgDebug(`scaleImage done (Photon): ${originalWidth}x${originalHeight} -> ${newWidth}x${newHeight} (scale=${scale}), cost: ${scaleEndTime - scaleStartTime}ms`);
288
+ const base64 = `data:image/jpeg;base64,${resizedBuffer.toString('base64')}`;
289
+ return {
290
+ width: newWidth,
291
+ height: newHeight,
292
+ imageBase64: base64
293
+ };
294
+ }
295
+ export { convertToJpegBase64, createImgBase64ByFormat, cropByRect, httpImg2Base64, localImg2Base64, paddingToMatchBlock, paddingToMatchBlockByBase64, parseBase64, photonFromBase64, photonToBase64, preProcessImageUrl, resizeAndConvertImgBuffer, resizeImgBase64, saveBase64Image, scaleImage, zoomForGPT4o };
@@ -0,0 +1,295 @@
1
+ import node_assert from "node:assert";
2
+ import { Buffer } from "node:buffer";
3
+ import { readFileSync } from "node:fs";
4
+ import { writeFile } from "node:fs/promises";
5
+ import node_path from "node:path";
6
+ import { getDebug } from "../logger.mjs";
7
+ import { ifInNode } from "../utils.mjs";
8
+ import get_photon from "./get-photon.mjs";
9
+ import get_sharp from "./get-sharp.mjs";
10
+ const imgDebug = getDebug('img');
11
+ async function saveBase64Image(options) {
12
+ const { base64Data, outputPath } = options;
13
+ const { body } = parseBase64(base64Data);
14
+ const imageBuffer = Buffer.from(body, 'base64');
15
+ await writeFile(outputPath, imageBuffer);
16
+ }
17
+ async function resizeAndConvertImgBuffer(inputFormat, inputData, newSize) {
18
+ if ('string' == typeof inputData) throw Error('inputData is base64, use resizeImgBase64 instead');
19
+ node_assert(newSize && newSize.width > 0 && newSize.height > 0, 'newSize must be positive');
20
+ const resizeStartTime = Date.now();
21
+ imgDebug(`resizeImg start, target size: ${newSize.width}x${newSize.height}`);
22
+ if (ifInNode) try {
23
+ const Sharp = await get_sharp();
24
+ const metadata = await Sharp(inputData).metadata();
25
+ const { width: originalWidth, height: originalHeight } = metadata;
26
+ if (!originalWidth || !originalHeight) throw Error('Undefined width or height from the input image.');
27
+ if (newSize.width === originalWidth && newSize.height === originalHeight) return {
28
+ buffer: inputData,
29
+ format: inputFormat
30
+ };
31
+ const resizedBuffer = await Sharp(inputData).resize(newSize.width, newSize.height).jpeg({
32
+ quality: 90
33
+ }).toBuffer();
34
+ const resizeEndTime = Date.now();
35
+ imgDebug(`resizeImg done (Sharp), target size: ${newSize.width}x${newSize.height}, cost: ${resizeEndTime - resizeStartTime}ms`);
36
+ return {
37
+ buffer: resizedBuffer,
38
+ format: 'jpeg'
39
+ };
40
+ } catch (error) {
41
+ imgDebug('Sharp failed, falling back to Photon:', error);
42
+ }
43
+ const { PhotonImage, SamplingFilter, resize } = await get_photon();
44
+ const inputBytes = new Uint8Array(inputData);
45
+ const bytesliceResult = PhotonImage.new_from_byteslice(inputBytes);
46
+ const inputImage = bytesliceResult instanceof Promise ? await bytesliceResult : bytesliceResult;
47
+ const originalWidth = inputImage.get_width();
48
+ const originalHeight = inputImage.get_height();
49
+ if (!originalWidth || !originalHeight) {
50
+ inputImage.free();
51
+ throw Error('Undefined width or height from the input image.');
52
+ }
53
+ if (newSize.width === originalWidth && newSize.height === originalHeight) {
54
+ inputImage.free();
55
+ return {
56
+ buffer: inputData,
57
+ format: inputFormat
58
+ };
59
+ }
60
+ const outputImage = resize(inputImage, newSize.width, newSize.height, SamplingFilter.CatmullRom);
61
+ const outputBytes = outputImage.get_bytes_jpeg(90);
62
+ const resizedBuffer = Buffer.from(outputBytes);
63
+ inputImage.free();
64
+ outputImage.free();
65
+ const resizeEndTime = Date.now();
66
+ imgDebug(`resizeImg done (Photon), target size: ${newSize.width}x${newSize.height}, cost: ${resizeEndTime - resizeStartTime}ms`);
67
+ return {
68
+ buffer: resizedBuffer,
69
+ format: 'jpeg'
70
+ };
71
+ }
72
+ const createImgBase64ByFormat = (format, body)=>`data:image/${format};base64,${body}`;
73
+ async function resizeImgBase64(inputBase64, newSize) {
74
+ const { body, mimeType } = parseBase64(inputBase64);
75
+ const imageBuffer = Buffer.from(body, 'base64');
76
+ const { buffer, format } = await resizeAndConvertImgBuffer(mimeType.split('/')[1], imageBuffer, newSize);
77
+ return createImgBase64ByFormat(format, buffer.toString('base64'));
78
+ }
79
+ function zoomForGPT4o(originalWidth, originalHeight) {
80
+ const maxWidth = 2048;
81
+ const maxHeight = 768;
82
+ let newWidth = originalWidth;
83
+ let newHeight = originalHeight;
84
+ const aspectRatio = originalWidth / originalHeight;
85
+ if (originalWidth > maxWidth) {
86
+ newWidth = maxWidth;
87
+ newHeight = newWidth / aspectRatio;
88
+ }
89
+ if (newHeight > maxHeight) {
90
+ newHeight = maxHeight;
91
+ newWidth = newHeight * aspectRatio;
92
+ }
93
+ return {
94
+ width: Math.round(newWidth),
95
+ height: Math.round(newHeight)
96
+ };
97
+ }
98
+ async function photonFromBase64(base64) {
99
+ const { PhotonImage } = await get_photon();
100
+ const { body } = parseBase64(base64);
101
+ const result = PhotonImage.new_from_base64(body);
102
+ return result instanceof Promise ? await result : result;
103
+ }
104
+ async function paddingToMatchBlock(image, blockSize = 28) {
105
+ const width = image.get_width();
106
+ const height = image.get_height();
107
+ const targetWidth = Math.ceil(width / blockSize) * blockSize;
108
+ const targetHeight = Math.ceil(height / blockSize) * blockSize;
109
+ if (targetWidth === width && targetHeight === height) return {
110
+ width,
111
+ height,
112
+ image
113
+ };
114
+ const { padding_right, padding_bottom, Rgba } = await get_photon();
115
+ const rightPadding = targetWidth - width;
116
+ const bottomPadding = targetHeight - height;
117
+ let result = image;
118
+ if (rightPadding > 0) {
119
+ const white = new Rgba(255, 255, 255, 255);
120
+ result = padding_right(result, rightPadding, white);
121
+ }
122
+ if (bottomPadding > 0) {
123
+ const white = new Rgba(255, 255, 255, 255);
124
+ const previousResult = result;
125
+ result = padding_bottom(previousResult, bottomPadding, white);
126
+ if (previousResult !== image) previousResult.free();
127
+ }
128
+ return {
129
+ width: targetWidth,
130
+ height: targetHeight,
131
+ image: result
132
+ };
133
+ }
134
+ async function paddingToMatchBlockByBase64(imageBase64, blockSize = 28) {
135
+ const photonImage = await photonFromBase64(imageBase64);
136
+ try {
137
+ const paddedResult = await paddingToMatchBlock(photonImage, blockSize);
138
+ const result = {
139
+ width: paddedResult.width,
140
+ height: paddedResult.height,
141
+ imageBase64: await photonToBase64(paddedResult.image)
142
+ };
143
+ if (paddedResult.image !== photonImage) paddedResult.image.free();
144
+ return result;
145
+ } finally{
146
+ photonImage.free();
147
+ }
148
+ }
149
+ async function cropByRect(imageBase64, rect, paddingImage) {
150
+ const { crop } = await get_photon();
151
+ const photonImage = await photonFromBase64(imageBase64);
152
+ const { left, top, width, height } = rect;
153
+ const cropped = crop(photonImage, left, top, left + width, top + height);
154
+ photonImage.free();
155
+ try {
156
+ if (paddingImage) {
157
+ const paddedResult = await paddingToMatchBlock(cropped);
158
+ const result = {
159
+ width: paddedResult.width,
160
+ height: paddedResult.height,
161
+ imageBase64: await photonToBase64(paddedResult.image)
162
+ };
163
+ if (paddedResult.image !== cropped) paddedResult.image.free();
164
+ return result;
165
+ }
166
+ return {
167
+ width: cropped.get_width(),
168
+ height: cropped.get_height(),
169
+ imageBase64: await photonToBase64(cropped)
170
+ };
171
+ } finally{
172
+ cropped.free();
173
+ }
174
+ }
175
+ async function photonToBase64(image, quality = 90) {
176
+ const bytes = image.get_bytes_jpeg(quality);
177
+ const base64Body = Buffer.from(bytes).toString('base64');
178
+ return `data:image/jpeg;base64,${base64Body}`;
179
+ }
180
+ const httpImg2Base64 = async (url)=>{
181
+ const response = await fetch(url);
182
+ if (!response.ok) throw new Error(`Failed to fetch image: ${url}`);
183
+ const contentType = response.headers.get('content-type');
184
+ if (!contentType) throw new Error(`Failed to fetch image: ${url}`);
185
+ node_assert(contentType.startsWith('image/'), `The url ${url} is not a image, because of content-type in header is ${contentType}.`);
186
+ const buffer = Buffer.from(await response.arrayBuffer());
187
+ return `data:${contentType};base64,${buffer.toString('base64')}`;
188
+ };
189
+ const localImg2Base64 = (imgPath, withoutHeader = false)=>{
190
+ const body = readFileSync(imgPath).toString('base64');
191
+ if (withoutHeader) return body;
192
+ const type = node_path.extname(imgPath).slice(1);
193
+ const finalType = 'svg' === type ? 'svg+xml' : type || 'jpg';
194
+ return `data:image/${finalType};base64,${body}`;
195
+ };
196
+ const preProcessImageUrl = async (url, convertHttpImage2Base64)=>{
197
+ if ('string' != typeof url) throw new Error(`url must be a string, but got ${url} with type ${typeof url}`);
198
+ if (url.startsWith('data:')) return url;
199
+ if (!(url.startsWith('http://') || url.startsWith('https://'))) return await localImg2Base64(url);
200
+ if (!convertHttpImage2Base64) return url;
201
+ return await httpImg2Base64(url);
202
+ };
203
+ const parseBase64 = (fullBase64String)=>{
204
+ try {
205
+ const separator = ';base64,';
206
+ const index = fullBase64String.indexOf(separator);
207
+ if (-1 === index) throw new Error('Invalid base64 string');
208
+ return {
209
+ mimeType: fullBase64String.slice(5, index),
210
+ body: fullBase64String.slice(index + separator.length)
211
+ };
212
+ } catch (e) {
213
+ throw new Error(`parseBase64 fail because intput is not a valid base64 string: ${fullBase64String}`, {
214
+ cause: e
215
+ });
216
+ }
217
+ };
218
+ async function convertToJpegBase64(inputBase64, quality) {
219
+ const { body } = parseBase64(inputBase64);
220
+ const buffer = Buffer.from(body, 'base64');
221
+ if (ifInNode) try {
222
+ const Sharp = await get_sharp();
223
+ const jpegBuffer = await Sharp(buffer).jpeg({
224
+ quality
225
+ }).toBuffer();
226
+ return `data:image/jpeg;base64,${jpegBuffer.toString('base64')}`;
227
+ } catch (error) {
228
+ imgDebug('Sharp failed for JPEG conversion, falling back to Photon:', error);
229
+ }
230
+ const { PhotonImage } = await get_photon();
231
+ const inputBytes = new Uint8Array(buffer);
232
+ const bytesliceResult = PhotonImage.new_from_byteslice(inputBytes);
233
+ const inputImage = bytesliceResult instanceof Promise ? await bytesliceResult : bytesliceResult;
234
+ const outputBytes = inputImage.get_bytes_jpeg(quality);
235
+ inputImage.free();
236
+ return `data:image/jpeg;base64,${Buffer.from(outputBytes).toString('base64')}`;
237
+ }
238
+ async function scaleImage(imageBase64, scale) {
239
+ if (scale <= 0) throw new Error('Scale factor must be positive');
240
+ const { body } = parseBase64(imageBase64);
241
+ const buffer = Buffer.from(body, 'base64');
242
+ const scaleStartTime = Date.now();
243
+ imgDebug(`scaleImage start, scale factor: ${scale}`);
244
+ if (ifInNode) try {
245
+ const Sharp = await get_sharp();
246
+ const metadata = await Sharp(buffer).metadata();
247
+ const originalWidth = metadata.width || 0;
248
+ const originalHeight = metadata.height || 0;
249
+ if (0 === originalWidth || 0 === originalHeight) throw new Error('Failed to get image dimensions');
250
+ const newWidth = Math.round(originalWidth * scale);
251
+ const newHeight = Math.round(originalHeight * scale);
252
+ const resizedBuffer = await Sharp(buffer).resize(newWidth, newHeight, {
253
+ kernel: 'lanczos3',
254
+ fit: 'fill'
255
+ }).jpeg({
256
+ quality: 90
257
+ }).toBuffer();
258
+ const scaleEndTime = Date.now();
259
+ imgDebug(`scaleImage done (Sharp): ${originalWidth}x${originalHeight} -> ${newWidth}x${newHeight} (scale=${scale}), cost: ${scaleEndTime - scaleStartTime}ms`);
260
+ const base64 = `data:image/jpeg;base64,${resizedBuffer.toString('base64')}`;
261
+ return {
262
+ width: newWidth,
263
+ height: newHeight,
264
+ imageBase64: base64
265
+ };
266
+ } catch (error) {
267
+ imgDebug('Sharp failed, falling back to Photon:', error);
268
+ }
269
+ const { PhotonImage, SamplingFilter, resize } = await get_photon();
270
+ const inputBytes = new Uint8Array(buffer);
271
+ const bytesliceResult = PhotonImage.new_from_byteslice(inputBytes);
272
+ const inputImage = bytesliceResult instanceof Promise ? await bytesliceResult : bytesliceResult;
273
+ const originalWidth = inputImage.get_width();
274
+ const originalHeight = inputImage.get_height();
275
+ if (!originalWidth || !originalHeight) {
276
+ inputImage.free();
277
+ throw new Error('Failed to get image dimensions');
278
+ }
279
+ const newWidth = Math.round(originalWidth * scale);
280
+ const newHeight = Math.round(originalHeight * scale);
281
+ const outputImage = resize(inputImage, newWidth, newHeight, SamplingFilter.CatmullRom);
282
+ const outputBytes = outputImage.get_bytes_jpeg(90);
283
+ const resizedBuffer = Buffer.from(outputBytes);
284
+ inputImage.free();
285
+ outputImage.free();
286
+ const scaleEndTime = Date.now();
287
+ imgDebug(`scaleImage done (Photon): ${originalWidth}x${originalHeight} -> ${newWidth}x${newHeight} (scale=${scale}), cost: ${scaleEndTime - scaleStartTime}ms`);
288
+ const base64 = `data:image/jpeg;base64,${resizedBuffer.toString('base64')}`;
289
+ return {
290
+ width: newWidth,
291
+ height: newHeight,
292
+ imageBase64: base64
293
+ };
294
+ }
295
+ export { convertToJpegBase64, createImgBase64ByFormat, cropByRect, httpImg2Base64, localImg2Base64, paddingToMatchBlock, paddingToMatchBlockByBase64, parseBase64, photonFromBase64, photonToBase64, preProcessImageUrl, resizeAndConvertImgBuffer, resizeImgBase64, saveBase64Image, scaleImage, zoomForGPT4o };
@@ -0,0 +1,4 @@
1
+ import { createCopyStaticPlugin, createPlaygroundCopyPlugin } from "./build/copy-static.mjs";
2
+ import { commonIgnoreWarnings } from "./build/rspack-config.mjs";
3
+ const src = {};
4
+ export { commonIgnoreWarnings, createCopyStaticPlugin, createPlaygroundCopyPlugin, src as default };
@@ -0,0 +1,4 @@
1
+ import { createCopyStaticPlugin, createPlaygroundCopyPlugin } from "./build/copy-static.mjs";
2
+ import { commonIgnoreWarnings } from "./build/rspack-config.mjs";
3
+ const src = {};
4
+ export { commonIgnoreWarnings, createCopyStaticPlugin, createPlaygroundCopyPlugin, src as default };
@@ -0,0 +1,64 @@
1
+ import node_fs from "node:fs";
2
+ import node_path from "node:path";
3
+ import node_util from "node:util";
4
+ import debug from "debug";
5
+ import { getMidsceneRunSubDir } from "./common.mjs";
6
+ import { ifInNode } from "./utils.mjs";
7
+ const topicPrefix = 'midscene';
8
+ const logStreams = new Map();
9
+ const debugInstances = new Map();
10
+ function getLogStream(topic) {
11
+ const topicFileName = topic.replace(/:/g, '-');
12
+ if (!logStreams.has(topicFileName)) {
13
+ const logFile = node_path.join(getMidsceneRunSubDir('log'), `${topicFileName}.log`);
14
+ const stream = node_fs.createWriteStream(logFile, {
15
+ flags: 'a'
16
+ });
17
+ logStreams.set(topicFileName, stream);
18
+ }
19
+ return logStreams.get(topicFileName);
20
+ }
21
+ function writeLogToFile(topic, message) {
22
+ if (!ifInNode) return;
23
+ const stream = getLogStream(topic);
24
+ const now = new Date();
25
+ const isoDate = now.toLocaleDateString('sv-SE');
26
+ const isoTime = now.toLocaleTimeString('sv-SE');
27
+ const milliseconds = now.getMilliseconds().toString().padStart(3, '0');
28
+ const timezoneOffsetMinutes = now.getTimezoneOffset();
29
+ const sign = timezoneOffsetMinutes <= 0 ? '+' : '-';
30
+ const hours = Math.floor(Math.abs(timezoneOffsetMinutes) / 60).toString().padStart(2, '0');
31
+ const minutes = (Math.abs(timezoneOffsetMinutes) % 60).toString().padStart(2, '0');
32
+ const timezoneString = `${sign}${hours}:${minutes}`;
33
+ const localISOTime = `${isoDate}T${isoTime}.${milliseconds}${timezoneString}`;
34
+ stream.write(`[${localISOTime}] ${message}\n`);
35
+ }
36
+ function getDebug(topic, options) {
37
+ const fullTopic = `${topicPrefix}:${topic}`;
38
+ const withConsole = options?.console ?? false;
39
+ const cacheKey = withConsole ? `${fullTopic}:withConsole` : fullTopic;
40
+ if (!debugInstances.has(cacheKey)) if (withConsole) {
41
+ const baseFn = getDebug(topic);
42
+ const wrapper = (...args)=>{
43
+ baseFn(...args);
44
+ console.warn('[Midscene]', ...args);
45
+ };
46
+ debugInstances.set(cacheKey, wrapper);
47
+ } else {
48
+ const debugFn = debug(fullTopic);
49
+ const wrapper = (...args)=>{
50
+ if (ifInNode) {
51
+ const message = node_util.format(...args);
52
+ writeLogToFile(topic, message);
53
+ }
54
+ debugFn(...args);
55
+ };
56
+ debugInstances.set(cacheKey, wrapper);
57
+ }
58
+ return debugInstances.get(cacheKey);
59
+ }
60
+ function enableDebug(topic) {
61
+ if (ifInNode) return;
62
+ debug.enable(`${topicPrefix}:${topic}`);
63
+ }
64
+ export { enableDebug, getDebug };