@xiuchang-midscene/shared 2.0.2 → 2.0.3

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 (166) hide show
  1. package/dist/es/oss/index.mjs +1 -1
  2. package/dist/lib/oss/index.js +1 -1
  3. package/package.json +1 -1
  4. package/src/oss/index.ts +1 -1
  5. package/dist/es/baseDB.mjs.bak +0 -109
  6. package/dist/es/build/copy-static.mjs.bak +0 -31
  7. package/dist/es/build/rspack-config.mjs.bak +0 -4
  8. package/dist/es/cli/cli-runner.mjs.bak +0 -140
  9. package/dist/es/cli/index.mjs.bak +0 -2
  10. package/dist/es/common.mjs.bak +0 -37
  11. package/dist/es/constants/example-code.mjs.bak +0 -223
  12. package/dist/es/constants/index.mjs.bak +0 -23
  13. package/dist/es/env/basic.mjs.bak +0 -6
  14. package/dist/es/env/constants.mjs.bak +0 -70
  15. package/dist/es/env/global-config-manager.mjs.bak +0 -94
  16. package/dist/es/env/helper.mjs.bak +0 -43
  17. package/dist/es/env/index.mjs.bak +0 -5
  18. package/dist/es/env/init-debug.mjs.bak +0 -18
  19. package/dist/es/env/model-config-manager.mjs.bak +0 -79
  20. package/dist/es/env/parse-model-config.mjs.bak +0 -132
  21. package/dist/es/env/types.mjs.bak +0 -220
  22. package/dist/es/env/utils.mjs.bak +0 -26
  23. package/dist/es/extractor/constants.mjs.bak +0 -2
  24. package/dist/es/extractor/debug.mjs.bak +0 -6
  25. package/dist/es/extractor/dom-util.mjs.bak +0 -92
  26. package/dist/es/extractor/index.mjs.bak +0 -5
  27. package/dist/es/extractor/locator.mjs.bak +0 -250
  28. package/dist/es/extractor/tree.mjs.bak +0 -78
  29. package/dist/es/extractor/util.mjs.bak +0 -245
  30. package/dist/es/extractor/web-extractor.mjs.bak +0 -303
  31. package/dist/es/img/box-select.mjs.bak +0 -824
  32. package/dist/es/img/canvas-fallback.mjs.bak +0 -238
  33. package/dist/es/img/get-photon.mjs.bak +0 -45
  34. package/dist/es/img/get-sharp.mjs.bak +0 -11
  35. package/dist/es/img/index.mjs.bak +0 -4
  36. package/dist/es/img/info.mjs.bak +0 -29
  37. package/dist/es/img/transform.mjs.bak +0 -295
  38. package/dist/es/index.mjs.bak +0 -4
  39. package/dist/es/logger.mjs.bak +0 -64
  40. package/dist/es/mcp/base-server.mjs.bak +0 -281
  41. package/dist/es/mcp/base-tools.mjs.bak +0 -91
  42. package/dist/es/mcp/chrome-path.mjs.bak +0 -35
  43. package/dist/es/mcp/index.mjs.bak +0 -7
  44. package/dist/es/mcp/inject-report-html-plugin.mjs.bak +0 -53
  45. package/dist/es/mcp/launcher-helper.mjs.bak +0 -52
  46. package/dist/es/mcp/tool-generator.mjs.bak +0 -297
  47. package/dist/es/mcp/types.mjs.bak +0 -3
  48. package/dist/es/node/fs.mjs.bak +0 -44
  49. package/dist/es/node/index.mjs.bak +0 -2
  50. package/dist/es/node/port.mjs.bak +0 -24
  51. package/dist/es/oss/demo.mjs.bak +0 -30
  52. package/dist/es/oss/index.mjs.bak +0 -90
  53. package/dist/es/polyfills/async-hooks.mjs.bak +0 -2
  54. package/dist/es/polyfills/index.mjs.bak +0 -1
  55. package/dist/es/types/index.mjs.bak +0 -3
  56. package/dist/es/us-keyboard-layout.mjs.bak +0 -1414
  57. package/dist/es/utils.mjs.bak +0 -72
  58. package/dist/es/zod-schema-utils.mjs.bak +0 -54
  59. package/dist/lib/baseDB.js.bak +0 -149
  60. package/dist/lib/build/copy-static.js.bak +0 -79
  61. package/dist/lib/build/rspack-config.js.bak +0 -38
  62. package/dist/lib/cli/cli-runner.js.bak +0 -196
  63. package/dist/lib/cli/index.js.bak +0 -48
  64. package/dist/lib/common.js.bak +0 -93
  65. package/dist/lib/constants/example-code.js.bak +0 -260
  66. package/dist/lib/constants/index.js.bak +0 -96
  67. package/dist/lib/env/basic.js.bak +0 -40
  68. package/dist/lib/env/constants.js.bak +0 -113
  69. package/dist/lib/env/global-config-manager.js.bak +0 -128
  70. package/dist/lib/env/helper.js.bak +0 -80
  71. package/dist/lib/env/index.js.bak +0 -90
  72. package/dist/lib/env/init-debug.js.bak +0 -52
  73. package/dist/lib/env/model-config-manager.js.bak +0 -113
  74. package/dist/lib/env/parse-model-config.js.bak +0 -178
  75. package/dist/lib/env/types.js.bak +0 -554
  76. package/dist/lib/env/utils.js.bak +0 -72
  77. package/dist/lib/extractor/constants.js.bak +0 -42
  78. package/dist/lib/extractor/debug.js.bak +0 -12
  79. package/dist/lib/extractor/dom-util.js.bak +0 -153
  80. package/dist/lib/extractor/index.js.bak +0 -81
  81. package/dist/lib/extractor/locator.js.bak +0 -296
  82. package/dist/lib/extractor/tree.js.bak +0 -124
  83. package/dist/lib/extractor/util.js.bak +0 -336
  84. package/dist/lib/extractor/web-extractor.js.bak +0 -349
  85. package/dist/lib/img/box-select.js.bak +0 -875
  86. package/dist/lib/img/canvas-fallback.js.bak +0 -305
  87. package/dist/lib/img/get-photon.js.bak +0 -82
  88. package/dist/lib/img/get-sharp.js.bak +0 -45
  89. package/dist/lib/img/index.js.bak +0 -95
  90. package/dist/lib/img/info.js.bak +0 -83
  91. package/dist/lib/img/transform.js.bak +0 -387
  92. package/dist/lib/index.js.bak +0 -47
  93. package/dist/lib/logger.js.bak +0 -114
  94. package/dist/lib/mcp/base-server.js.bak +0 -331
  95. package/dist/lib/mcp/base-tools.js.bak +0 -125
  96. package/dist/lib/mcp/chrome-path.js.bak +0 -72
  97. package/dist/lib/mcp/index.js.bak +0 -100
  98. package/dist/lib/mcp/inject-report-html-plugin.js.bak +0 -98
  99. package/dist/lib/mcp/launcher-helper.js.bak +0 -86
  100. package/dist/lib/mcp/tool-generator.js.bak +0 -334
  101. package/dist/lib/mcp/types.js.bak +0 -40
  102. package/dist/lib/node/fs.js.bak +0 -97
  103. package/dist/lib/node/index.js.bak +0 -65
  104. package/dist/lib/node/port.js.bak +0 -61
  105. package/dist/lib/oss/demo.js.bak +0 -36
  106. package/dist/lib/oss/index.js.bak +0 -138
  107. package/dist/lib/polyfills/async-hooks.js.bak +0 -36
  108. package/dist/lib/polyfills/index.js.bak +0 -58
  109. package/dist/lib/types/index.js.bak +0 -37
  110. package/dist/lib/us-keyboard-layout.js.bak +0 -1457
  111. package/dist/lib/utils.js.bak +0 -148
  112. package/dist/lib/zod-schema-utils.js.bak +0 -97
  113. package/dist/types/baseDB.d.ts.bak +0 -25
  114. package/dist/types/build/copy-static.d.ts.bak +0 -31
  115. package/dist/types/build/rspack-config.d.ts.bak +0 -8
  116. package/dist/types/cli/cli-runner.d.ts.bak +0 -14
  117. package/dist/types/cli/index.d.ts.bak +0 -2
  118. package/dist/types/common.d.ts.bak +0 -12
  119. package/dist/types/constants/example-code.d.ts.bak +0 -2
  120. package/dist/types/constants/index.d.ts.bak +0 -21
  121. package/dist/types/env/basic.d.ts.bak +0 -6
  122. package/dist/types/env/constants.d.ts.bak +0 -40
  123. package/dist/types/env/global-config-manager.d.ts.bak +0 -32
  124. package/dist/types/env/helper.d.ts.bak +0 -4
  125. package/dist/types/env/index.d.ts.bak +0 -4
  126. package/dist/types/env/init-debug.d.ts.bak +0 -1
  127. package/dist/types/env/model-config-manager.d.ts.bak +0 -25
  128. package/dist/types/env/parse-model-config.d.ts.bak +0 -31
  129. package/dist/types/env/types.d.ts.bak +0 -318
  130. package/dist/types/env/utils.d.ts.bak +0 -38
  131. package/dist/types/extractor/constants.d.ts.bak +0 -1
  132. package/dist/types/extractor/debug.d.ts.bak +0 -1
  133. package/dist/types/extractor/dom-util.d.ts.bak +0 -56
  134. package/dist/types/extractor/index.d.ts.bak +0 -32
  135. package/dist/types/extractor/locator.d.ts.bak +0 -9
  136. package/dist/types/extractor/tree.d.ts.bak +0 -6
  137. package/dist/types/extractor/util.d.ts.bak +0 -47
  138. package/dist/types/extractor/web-extractor.d.ts.bak +0 -19
  139. package/dist/types/img/box-select.d.ts.bak +0 -26
  140. package/dist/types/img/canvas-fallback.d.ts.bak +0 -105
  141. package/dist/types/img/get-photon.d.ts.bak +0 -19
  142. package/dist/types/img/get-sharp.d.ts.bak +0 -3
  143. package/dist/types/img/index.d.ts.bak +0 -3
  144. package/dist/types/img/info.d.ts.bak +0 -29
  145. package/dist/types/img/transform.d.ts.bak +0 -107
  146. package/dist/types/index.d.ts.bak +0 -4
  147. package/dist/types/logger.d.ts.bak +0 -5
  148. package/dist/types/mcp/base-server.d.ts.bak +0 -93
  149. package/dist/types/mcp/base-tools.d.ts.bak +0 -79
  150. package/dist/types/mcp/chrome-path.d.ts.bak +0 -2
  151. package/dist/types/mcp/index.d.ts.bak +0 -7
  152. package/dist/types/mcp/inject-report-html-plugin.d.ts.bak +0 -18
  153. package/dist/types/mcp/launcher-helper.d.ts.bak +0 -94
  154. package/dist/types/mcp/tool-generator.d.ts.bak +0 -10
  155. package/dist/types/mcp/types.d.ts.bak +0 -103
  156. package/dist/types/node/fs.d.ts.bak +0 -15
  157. package/dist/types/node/index.d.ts.bak +0 -2
  158. package/dist/types/node/port.d.ts.bak +0 -8
  159. package/dist/types/oss/demo.d.ts.bak +0 -1
  160. package/dist/types/oss/index.d.ts.bak +0 -34
  161. package/dist/types/polyfills/async-hooks.d.ts.bak +0 -6
  162. package/dist/types/polyfills/index.d.ts.bak +0 -4
  163. package/dist/types/types/index.d.ts.bak +0 -34
  164. package/dist/types/us-keyboard-layout.d.ts.bak +0 -32
  165. package/dist/types/utils.d.ts.bak +0 -34
  166. package/dist/types/zod-schema-utils.d.ts.bak +0 -23
@@ -1,238 +0,0 @@
1
- import { getDebug } from "../logger.mjs";
2
- function _define_property(obj, key, value) {
3
- if (key in obj) Object.defineProperty(obj, key, {
4
- value: value,
5
- enumerable: true,
6
- configurable: true,
7
- writable: true
8
- });
9
- else obj[key] = value;
10
- return obj;
11
- }
12
- const debug = getDebug('img:canvas-fallback');
13
- class CanvasImage {
14
- get_width() {
15
- return this._width;
16
- }
17
- get_height() {
18
- return this._height;
19
- }
20
- get_raw_pixels() {
21
- const imageData = this.ctx.getImageData(0, 0, this._width, this._height);
22
- return new Uint8Array(imageData.data.buffer);
23
- }
24
- get_bytes_jpeg(quality) {
25
- const dataUrl = this.canvas.toDataURL('image/jpeg', quality / 100);
26
- const base64 = dataUrl.split(',')[1];
27
- const binary = atob(base64);
28
- const bytes = new Uint8Array(binary.length);
29
- for(let i = 0; i < binary.length; i++)bytes[i] = binary.charCodeAt(i);
30
- return bytes;
31
- }
32
- free() {}
33
- _getCanvas() {
34
- return this.canvas;
35
- }
36
- _getContext() {
37
- return this.ctx;
38
- }
39
- static async new_from_base64(base64Body) {
40
- return new Promise((resolve, reject)=>{
41
- const img = new Image();
42
- img.onload = ()=>{
43
- const canvas = document.createElement('canvas');
44
- canvas.width = img.width;
45
- canvas.height = img.height;
46
- const ctx = canvas.getContext('2d');
47
- if (!ctx) return void reject(new Error('Failed to get 2d context'));
48
- ctx.drawImage(img, 0, 0);
49
- resolve(new CanvasImage(canvas));
50
- };
51
- img.onerror = ()=>{
52
- reject(new Error('Failed to load image'));
53
- };
54
- if (base64Body.startsWith('data:')) img.src = base64Body;
55
- else img.src = `data:image/png;base64,${base64Body}`;
56
- });
57
- }
58
- static async new_from_byteslice(bytes) {
59
- return new Promise((resolve, reject)=>{
60
- const blob = new Blob([
61
- bytes
62
- ], {
63
- type: 'image/png'
64
- });
65
- const url = URL.createObjectURL(blob);
66
- const img = new Image();
67
- img.onload = ()=>{
68
- const canvas = document.createElement('canvas');
69
- canvas.width = img.width;
70
- canvas.height = img.height;
71
- const ctx = canvas.getContext('2d');
72
- if (!ctx) {
73
- URL.revokeObjectURL(url);
74
- reject(new Error('Failed to get 2d context'));
75
- return;
76
- }
77
- ctx.drawImage(img, 0, 0);
78
- URL.revokeObjectURL(url);
79
- resolve(new CanvasImage(canvas));
80
- };
81
- img.onerror = ()=>{
82
- URL.revokeObjectURL(url);
83
- reject(new Error('Failed to load image from bytes'));
84
- };
85
- img.src = url;
86
- });
87
- }
88
- constructor(canvas){
89
- _define_property(this, "canvas", void 0);
90
- _define_property(this, "ctx", void 0);
91
- _define_property(this, "_width", void 0);
92
- _define_property(this, "_height", void 0);
93
- this.canvas = canvas;
94
- const ctx = canvas.getContext('2d');
95
- if (!ctx) throw new Error('Failed to get 2d context');
96
- this.ctx = ctx;
97
- this._width = canvas.width;
98
- this._height = canvas.height;
99
- }
100
- }
101
- const CanvasSamplingFilter = {
102
- Nearest: 'nearest',
103
- Triangle: 'triangle',
104
- CatmullRom: 'catmullrom',
105
- Gaussian: 'gaussian',
106
- Lanczos3: 'lanczos3'
107
- };
108
- class CanvasRgba {
109
- constructor(r, g, b, a){
110
- _define_property(this, "r", void 0);
111
- _define_property(this, "g", void 0);
112
- _define_property(this, "b", void 0);
113
- _define_property(this, "a", void 0);
114
- this.r = r;
115
- this.g = g;
116
- this.b = b;
117
- this.a = a;
118
- }
119
- }
120
- function canvasResize(image, newWidth, newHeight, _filter) {
121
- const canvas = document.createElement('canvas');
122
- canvas.width = newWidth;
123
- canvas.height = newHeight;
124
- const ctx = canvas.getContext('2d');
125
- if (!ctx) throw new Error('Failed to get 2d context');
126
- ctx.imageSmoothingEnabled = true;
127
- ctx.imageSmoothingQuality = 'high';
128
- ctx.drawImage(image._getCanvas(), 0, 0, newWidth, newHeight);
129
- return new CanvasImage(canvas);
130
- }
131
- function canvasCrop(image, x1, y1, x2, y2) {
132
- const width = x2 - x1;
133
- const height = y2 - y1;
134
- const canvas = document.createElement('canvas');
135
- canvas.width = width;
136
- canvas.height = height;
137
- const ctx = canvas.getContext('2d');
138
- if (!ctx) throw new Error('Failed to get 2d context');
139
- ctx.drawImage(image._getCanvas(), x1, y1, width, height, 0, 0, width, height);
140
- return new CanvasImage(canvas);
141
- }
142
- function canvasPaddingRight(image, padding, color) {
143
- const newWidth = image.get_width() + padding;
144
- const height = image.get_height();
145
- const canvas = document.createElement('canvas');
146
- canvas.width = newWidth;
147
- canvas.height = height;
148
- const ctx = canvas.getContext('2d');
149
- if (!ctx) throw new Error('Failed to get 2d context');
150
- ctx.fillStyle = `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a / 255})`;
151
- ctx.fillRect(0, 0, newWidth, height);
152
- ctx.drawImage(image._getCanvas(), 0, 0);
153
- return new CanvasImage(canvas);
154
- }
155
- function canvasPaddingBottom(image, padding, color) {
156
- const width = image.get_width();
157
- const newHeight = image.get_height() + padding;
158
- const canvas = document.createElement('canvas');
159
- canvas.width = width;
160
- canvas.height = newHeight;
161
- const ctx = canvas.getContext('2d');
162
- if (!ctx) throw new Error('Failed to get 2d context');
163
- ctx.fillStyle = `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a / 255})`;
164
- ctx.fillRect(0, 0, width, newHeight);
165
- ctx.drawImage(image._getCanvas(), 0, 0);
166
- return new CanvasImage(canvas);
167
- }
168
- function canvasPaddingUniform(image, padding, color) {
169
- const newWidth = image.get_width() + 2 * padding;
170
- const newHeight = image.get_height() + 2 * padding;
171
- const canvas = document.createElement('canvas');
172
- canvas.width = newWidth;
173
- canvas.height = newHeight;
174
- const ctx = canvas.getContext('2d');
175
- if (!ctx) throw new Error('Failed to get 2d context');
176
- ctx.fillStyle = `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a / 255})`;
177
- ctx.fillRect(0, 0, newWidth, newHeight);
178
- ctx.drawImage(image._getCanvas(), padding, padding);
179
- return new CanvasImage(canvas);
180
- }
181
- function canvasPaddingLeft(image, padding, color) {
182
- const newWidth = image.get_width() + padding;
183
- const height = image.get_height();
184
- const canvas = document.createElement('canvas');
185
- canvas.width = newWidth;
186
- canvas.height = height;
187
- const ctx = canvas.getContext('2d');
188
- if (!ctx) throw new Error('Failed to get 2d context');
189
- ctx.fillStyle = `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a / 255})`;
190
- ctx.fillRect(0, 0, newWidth, height);
191
- ctx.drawImage(image._getCanvas(), padding, 0);
192
- return new CanvasImage(canvas);
193
- }
194
- function canvasPaddingTop(image, padding, color) {
195
- const width = image.get_width();
196
- const newHeight = image.get_height() + padding;
197
- const canvas = document.createElement('canvas');
198
- canvas.width = width;
199
- canvas.height = newHeight;
200
- const ctx = canvas.getContext('2d');
201
- if (!ctx) throw new Error('Failed to get 2d context');
202
- ctx.fillStyle = `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a / 255})`;
203
- ctx.fillRect(0, 0, width, newHeight);
204
- ctx.drawImage(image._getCanvas(), 0, padding);
205
- return new CanvasImage(canvas);
206
- }
207
- function canvasWatermark(base, overlay, x, y) {
208
- const canvas = document.createElement('canvas');
209
- canvas.width = base.get_width();
210
- canvas.height = base.get_height();
211
- const ctx = canvas.getContext('2d');
212
- if (!ctx) throw new Error('Failed to get 2d context');
213
- ctx.drawImage(base._getCanvas(), 0, 0);
214
- ctx.drawImage(overlay._getCanvas(), x, y);
215
- return new CanvasImage(canvas);
216
- }
217
- function createCanvasFallbackModule() {
218
- debug('Creating Canvas fallback module');
219
- console.log('[midscene:img] Using Canvas fallback (Photon WASM not available)');
220
- return {
221
- PhotonImage: CanvasImage,
222
- SamplingFilter: CanvasSamplingFilter,
223
- resize: canvasResize,
224
- crop: canvasCrop,
225
- open_image: ()=>{
226
- throw new Error('open_image not supported in Canvas fallback');
227
- },
228
- base64_to_image: CanvasImage.new_from_base64,
229
- padding_uniform: canvasPaddingUniform,
230
- padding_left: canvasPaddingLeft,
231
- padding_right: canvasPaddingRight,
232
- padding_top: canvasPaddingTop,
233
- padding_bottom: canvasPaddingBottom,
234
- watermark: canvasWatermark,
235
- Rgba: CanvasRgba
236
- };
237
- }
238
- export { CanvasImage, CanvasRgba, CanvasSamplingFilter, canvasCrop, canvasPaddingBottom, canvasPaddingLeft, canvasPaddingRight, canvasPaddingTop, canvasPaddingUniform, canvasResize, canvasWatermark, createCanvasFallbackModule };
@@ -1,45 +0,0 @@
1
- import { getDebug } from "../logger.mjs";
2
- import { ifInBrowser, ifInNode, ifInWorker } from "../utils.mjs";
3
- const debug = getDebug('img');
4
- let photonModule = null;
5
- let isInitialized = false;
6
- let usingCanvasFallback = false;
7
- async function getPhoton() {
8
- if (photonModule && isInitialized) return photonModule;
9
- const env = ifInBrowser ? 'browser' : ifInWorker ? 'worker' : ifInNode ? 'node' : 'unknown';
10
- debug(`Loading photon module in ${env} environment`);
11
- try {
12
- if (ifInBrowser || ifInWorker) {
13
- const photon = await import("@silvia-odwyer/photon");
14
- if ('function' == typeof photon.default) await photon.default();
15
- debug('Photon loaded: @silvia-odwyer/photon (browser/worker)');
16
- photonModule = photon;
17
- } else if (ifInNode) {
18
- photonModule = await import("@silvia-odwyer/photon-node");
19
- debug('Photon loaded: @silvia-odwyer/photon-node (node)');
20
- }
21
- if (!photonModule?.PhotonImage) throw new Error('PhotonImage is not available');
22
- if (!photonModule.PhotonImage.new_from_byteslice && !photonModule.PhotonImage.new_from_base64) throw new Error('PhotonImage.new_from_byteslice or new_from_base64 is not available');
23
- isInitialized = true;
24
- return photonModule;
25
- } catch (error) {
26
- debug(`Photon load failed: ${error instanceof Error ? error.message : String(error)}`);
27
- if (ifInBrowser) {
28
- console.warn(`[midscene:img] Photon WASM failed to load, falling back to Canvas API. Error: ${error instanceof Error ? error.message : String(error)}`);
29
- try {
30
- const { createCanvasFallbackModule } = await import("./canvas-fallback.mjs");
31
- photonModule = createCanvasFallbackModule();
32
- usingCanvasFallback = true;
33
- isInitialized = true;
34
- return photonModule;
35
- } catch (fallbackError) {
36
- debug(`Canvas fallback also failed: ${fallbackError instanceof Error ? fallbackError.message : String(fallbackError)}`);
37
- }
38
- }
39
- throw new Error(`Failed to load photon module: ${error instanceof Error ? error.message : String(error)}`);
40
- }
41
- }
42
- function isUsingCanvasFallback() {
43
- return usingCanvasFallback;
44
- }
45
- export { getPhoton as default, isUsingCanvasFallback };
@@ -1,11 +0,0 @@
1
- import { ifInNode } from "../utils.mjs";
2
- async function getSharp() {
3
- if (!ifInNode) throw new Error('Sharp is only available in Node.js environment');
4
- try {
5
- const sharp = await import("sharp");
6
- return sharp.default;
7
- } catch (error) {
8
- throw new Error(`Failed to load sharp module: ${error instanceof Error ? error.message : String(error)}`);
9
- }
10
- }
11
- export { getSharp as default };
@@ -1,4 +0,0 @@
1
- import { imageInfoOfBase64, isValidImageBuffer, isValidJPEGImageBuffer, isValidPNGImageBuffer } from "./info.mjs";
2
- import { convertToJpegBase64, createImgBase64ByFormat, cropByRect, httpImg2Base64, localImg2Base64, paddingToMatchBlockByBase64, parseBase64, preProcessImageUrl, resizeAndConvertImgBuffer, resizeImgBase64, saveBase64Image, scaleImage, zoomForGPT4o } from "./transform.mjs";
3
- import { annotateRects, compositeElementInfoImg, processImageElementInfo } from "./box-select.mjs";
4
- export { annotateRects, compositeElementInfoImg, convertToJpegBase64, createImgBase64ByFormat, cropByRect, httpImg2Base64, imageInfoOfBase64, isValidImageBuffer, isValidJPEGImageBuffer, isValidPNGImageBuffer, localImg2Base64, paddingToMatchBlockByBase64, parseBase64, preProcessImageUrl, processImageElementInfo, resizeAndConvertImgBuffer, resizeImgBase64, saveBase64Image, scaleImage, zoomForGPT4o };
@@ -1,29 +0,0 @@
1
- import node_assert from "node:assert";
2
- import get_photon from "./get-photon.mjs";
3
- async function imageInfoOfBase64(imageBase64) {
4
- const { PhotonImage } = await get_photon();
5
- const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, '');
6
- const result = PhotonImage.new_from_base64(base64Data);
7
- const image = result instanceof Promise ? await result : result;
8
- const width = image.get_width();
9
- const height = image.get_height();
10
- image.free();
11
- node_assert(width && height, 'Invalid image: cannot get width or height');
12
- return {
13
- width,
14
- height
15
- };
16
- }
17
- function isValidPNGImageBuffer(buffer) {
18
- if (!buffer || buffer.length < 8) return false;
19
- const isPNG = 0x89 === buffer[0] && 0x50 === buffer[1] && 0x4e === buffer[2] && 0x47 === buffer[3] && 0x0d === buffer[4] && 0x0a === buffer[5] && 0x1a === buffer[6] && 0x0a === buffer[7];
20
- return isPNG;
21
- }
22
- function isValidJPEGImageBuffer(buffer) {
23
- if (!buffer || buffer.length < 3) return false;
24
- return 0xff === buffer[0] && 0xd8 === buffer[1] && 0xff === buffer[2];
25
- }
26
- function isValidImageBuffer(buffer) {
27
- return isValidPNGImageBuffer(buffer) || isValidJPEGImageBuffer(buffer);
28
- }
29
- export { imageInfoOfBase64, isValidImageBuffer, isValidJPEGImageBuffer, isValidPNGImageBuffer };
@@ -1,295 +0,0 @@
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 };
@@ -1,4 +0,0 @@
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 };