@rpascene/shared 0.30.8
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.
- package/README.md +9 -0
- package/dist/es/baseDB.mjs +109 -0
- package/dist/es/build/copy-static.mjs +29 -0
- package/dist/es/common.mjs +37 -0
- package/dist/es/constants/example-code.mjs +202 -0
- package/dist/es/constants/index.mjs +74 -0
- package/dist/es/env/basic.mjs +6 -0
- package/dist/es/env/constants.mjs +97 -0
- package/dist/es/env/decide-model-config.mjs +172 -0
- package/dist/es/env/global-config-manager.mjs +82 -0
- package/dist/es/env/helper.mjs +45 -0
- package/dist/es/env/index.mjs +5 -0
- package/dist/es/env/init-debug.mjs +18 -0
- package/dist/es/env/model-config-manager.mjs +99 -0
- package/dist/es/env/parse.mjs +69 -0
- package/dist/es/env/types.mjs +265 -0
- package/dist/es/env/utils.mjs +18 -0
- package/dist/es/extractor/constants.mjs +2 -0
- package/dist/es/extractor/cs_postmessage.mjs +61 -0
- package/dist/es/extractor/customLocator.mjs +646 -0
- package/dist/es/extractor/debug.mjs +6 -0
- package/dist/es/extractor/dom-util.mjs +92 -0
- package/dist/es/extractor/index.mjs +7 -0
- package/dist/es/extractor/locator.mjs +95 -0
- package/dist/es/extractor/tree.mjs +81 -0
- package/dist/es/extractor/util.mjs +244 -0
- package/dist/es/extractor/web-extractor.mjs +361 -0
- package/dist/es/img/box-select.mjs +184 -0
- package/dist/es/img/draw-box.mjs +42 -0
- package/dist/es/img/get-jimp.mjs +10 -0
- package/dist/es/img/get-photon.mjs +19 -0
- package/dist/es/img/get-sharp.mjs +11 -0
- package/dist/es/img/index.mjs +5 -0
- package/dist/es/img/info.mjs +32 -0
- package/dist/es/img/transform.mjs +192 -0
- package/dist/es/index.mjs +3 -0
- package/dist/es/logger.mjs +61 -0
- package/dist/es/node/fs.mjs +44 -0
- package/dist/es/node/index.mjs +1 -0
- package/dist/es/polyfills/async-hooks.mjs +2 -0
- package/dist/es/polyfills/index.mjs +1 -0
- package/dist/es/types/index.mjs +3 -0
- package/dist/es/us-keyboard-layout.mjs +1414 -0
- package/dist/es/us-keyboard-layout.mjs.LICENSE.txt +5 -0
- package/dist/es/utils.mjs +66 -0
- package/dist/lib/baseDB.js +149 -0
- package/dist/lib/build/copy-static.js +77 -0
- package/dist/lib/common.js +93 -0
- package/dist/lib/constants/example-code.js +239 -0
- package/dist/lib/constants/index.js +153 -0
- package/dist/lib/env/basic.js +40 -0
- package/dist/lib/env/constants.js +143 -0
- package/dist/lib/env/decide-model-config.js +212 -0
- package/dist/lib/env/global-config-manager.js +116 -0
- package/dist/lib/env/helper.js +85 -0
- package/dist/lib/env/index.js +94 -0
- package/dist/lib/env/init-debug.js +52 -0
- package/dist/lib/env/model-config-manager.js +133 -0
- package/dist/lib/env/parse.js +106 -0
- package/dist/lib/env/types.js +650 -0
- package/dist/lib/env/utils.js +61 -0
- package/dist/lib/extractor/constants.js +42 -0
- package/dist/lib/extractor/cs_postmessage.js +98 -0
- package/dist/lib/extractor/customLocator.js +698 -0
- package/dist/lib/extractor/debug.js +12 -0
- package/dist/lib/extractor/dom-util.js +150 -0
- package/dist/lib/extractor/index.js +153 -0
- package/dist/lib/extractor/locator.js +141 -0
- package/dist/lib/extractor/tree.js +127 -0
- package/dist/lib/extractor/util.js +335 -0
- package/dist/lib/extractor/web-extractor.js +407 -0
- package/dist/lib/img/box-select.js +232 -0
- package/dist/lib/img/draw-box.js +89 -0
- package/dist/lib/img/get-jimp.js +72 -0
- package/dist/lib/img/get-photon.js +76 -0
- package/dist/lib/img/get-sharp.js +63 -0
- package/dist/lib/img/index.js +102 -0
- package/dist/lib/img/info.js +86 -0
- package/dist/lib/img/transform.js +279 -0
- package/dist/lib/index.js +43 -0
- package/dist/lib/logger.js +114 -0
- package/dist/lib/node/fs.js +97 -0
- package/dist/lib/node/index.js +60 -0
- package/dist/lib/polyfills/async-hooks.js +36 -0
- package/dist/lib/polyfills/index.js +60 -0
- package/dist/lib/types/index.js +37 -0
- package/dist/lib/us-keyboard-layout.js +1457 -0
- package/dist/lib/us-keyboard-layout.js.LICENSE.txt +5 -0
- package/dist/lib/utils.js +136 -0
- package/dist/types/baseDB.d.ts +25 -0
- package/dist/types/build/copy-static.d.ts +31 -0
- package/dist/types/common.d.ts +12 -0
- package/dist/types/constants/example-code.d.ts +2 -0
- package/dist/types/constants/index.d.ts +23 -0
- package/dist/types/env/basic.d.ts +6 -0
- package/dist/types/env/constants.d.ts +40 -0
- package/dist/types/env/decide-model-config.d.ts +14 -0
- package/dist/types/env/global-config-manager.d.ts +32 -0
- package/dist/types/env/helper.d.ts +6 -0
- package/dist/types/env/index.d.ts +4 -0
- package/dist/types/env/init-debug.d.ts +1 -0
- package/dist/types/env/model-config-manager.d.ts +24 -0
- package/dist/types/env/parse.d.ts +12 -0
- package/dist/types/env/types.d.ts +295 -0
- package/dist/types/env/utils.d.ts +7 -0
- package/dist/types/extractor/constants.d.ts +1 -0
- package/dist/types/extractor/cs_postmessage.d.ts +2 -0
- package/dist/types/extractor/customLocator.d.ts +69 -0
- package/dist/types/extractor/debug.d.ts +1 -0
- package/dist/types/extractor/dom-util.d.ts +26 -0
- package/dist/types/extractor/index.d.ts +36 -0
- package/dist/types/extractor/locator.d.ts +7 -0
- package/dist/types/extractor/tree.d.ts +9 -0
- package/dist/types/extractor/util.d.ts +43 -0
- package/dist/types/extractor/web-extractor.d.ts +19 -0
- package/dist/types/img/box-select.d.ts +25 -0
- package/dist/types/img/draw-box.d.ts +15 -0
- package/dist/types/img/get-jimp.d.ts +2 -0
- package/dist/types/img/get-photon.d.ts +8 -0
- package/dist/types/img/get-sharp.d.ts +3 -0
- package/dist/types/img/index.d.ts +4 -0
- package/dist/types/img/info.d.ts +29 -0
- package/dist/types/img/transform.d.ts +88 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/logger.d.ts +4 -0
- package/dist/types/node/fs.d.ts +15 -0
- package/dist/types/node/index.d.ts +1 -0
- package/dist/types/polyfills/async-hooks.d.ts +6 -0
- package/dist/types/polyfills/index.d.ts +4 -0
- package/dist/types/types/index.d.ts +37 -0
- package/dist/types/us-keyboard-layout.d.ts +32 -0
- package/dist/types/utils.d.ts +22 -0
- package/package.json +102 -0
- package/src/baseDB.ts +158 -0
- package/src/build/copy-static.ts +62 -0
- package/src/common.ts +67 -0
- package/src/constants/example-code.ts +202 -0
- package/src/constants/index.ts +81 -0
- package/src/env/basic.ts +12 -0
- package/src/env/constants.ts +291 -0
- package/src/env/decide-model-config.ts +319 -0
- package/src/env/global-config-manager.ts +174 -0
- package/src/env/helper.ts +80 -0
- package/src/env/index.ts +4 -0
- package/src/env/init-debug.ts +29 -0
- package/src/env/model-config-manager.ts +145 -0
- package/src/env/parse.ts +131 -0
- package/src/env/types.ts +573 -0
- package/src/env/utils.ts +39 -0
- package/src/extractor/constants.ts +5 -0
- package/src/extractor/cs_postmessage.ts +101 -0
- package/src/extractor/customLocator.ts +1138 -0
- package/src/extractor/debug.ts +10 -0
- package/src/extractor/dom-util.ts +141 -0
- package/src/extractor/index.ts +54 -0
- package/src/extractor/locator.ts +179 -0
- package/src/extractor/tree.ts +179 -0
- package/src/extractor/util.ts +468 -0
- package/src/extractor/web-extractor.ts +559 -0
- package/src/img/box-select.ts +346 -0
- package/src/img/draw-box.ts +60 -0
- package/src/img/get-jimp.ts +12 -0
- package/src/img/get-photon.ts +48 -0
- package/src/img/get-sharp.ts +18 -0
- package/src/img/index.ts +24 -0
- package/src/img/info.ts +79 -0
- package/src/img/jimp.d.ts +4 -0
- package/src/img/transform.ts +396 -0
- package/src/index.ts +6 -0
- package/src/logger.ts +93 -0
- package/src/node/fs.ts +84 -0
- package/src/node/index.ts +1 -0
- package/src/polyfills/async-hooks.ts +6 -0
- package/src/polyfills/index.ts +4 -0
- package/src/types/index.ts +53 -0
- package/src/us-keyboard-layout.ts +723 -0
- package/src/utils.ts +127 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import type Jimp from 'jimp';
|
|
3
|
+
import type { Rect } from '../types';
|
|
4
|
+
/**
|
|
5
|
+
/**
|
|
6
|
+
* Saves a Base64-encoded image to a file
|
|
7
|
+
*
|
|
8
|
+
* @param options - An object containing the Base64-encoded image data and the output file path
|
|
9
|
+
* @param options.base64Data - The Base64-encoded image data
|
|
10
|
+
* @param options.outputPath - The path where the image will be saved
|
|
11
|
+
* @throws Error if there is an error during the saving process
|
|
12
|
+
*/
|
|
13
|
+
export declare function saveBase64Image(options: {
|
|
14
|
+
base64Data: string;
|
|
15
|
+
outputPath: string;
|
|
16
|
+
}): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Resizes an image from Buffer, maybe return a new format
|
|
19
|
+
* - If the image is Resized, the returned format will be jpg.
|
|
20
|
+
* - If the image is not Resized, it will return to its original format.
|
|
21
|
+
* @returns { buffer: resized buffer, format: the new format}
|
|
22
|
+
*/
|
|
23
|
+
export declare function resizeAndConvertImgBuffer(inputFormat: string, inputData: Buffer, newSize: {
|
|
24
|
+
width: number;
|
|
25
|
+
height: number;
|
|
26
|
+
}): Promise<{
|
|
27
|
+
buffer: Buffer;
|
|
28
|
+
format: string;
|
|
29
|
+
}>;
|
|
30
|
+
export declare const createImgBase64ByFormat: (format: string, body: string) => string;
|
|
31
|
+
export declare function resizeImgBase64(inputBase64: string, newSize: {
|
|
32
|
+
width: number;
|
|
33
|
+
height: number;
|
|
34
|
+
}): Promise<string>;
|
|
35
|
+
/**
|
|
36
|
+
* Calculates new dimensions for an image while maintaining its aspect ratio.
|
|
37
|
+
*
|
|
38
|
+
* This function is designed to resize an image to fit within a specified maximum width and height
|
|
39
|
+
* while maintaining the original aspect ratio. If the original width or height exceeds the maximum
|
|
40
|
+
* dimensions, the image will be scaled down to fit.
|
|
41
|
+
*
|
|
42
|
+
* @param {number} originalWidth - The original width of the image.
|
|
43
|
+
* @param {number} originalHeight - The original height of the image.
|
|
44
|
+
* @returns {Object} An object containing the new width and height.
|
|
45
|
+
* @throws {Error} Throws an error if the width or height is not a positive number.
|
|
46
|
+
*/
|
|
47
|
+
export declare function zoomForGPT4o(originalWidth: number, originalHeight: number): {
|
|
48
|
+
width: number;
|
|
49
|
+
height: number;
|
|
50
|
+
};
|
|
51
|
+
export declare function jimpFromBase64(base64: string): Promise<Jimp>;
|
|
52
|
+
export declare function paddingToMatchBlock(image: Jimp, blockSize?: number): Promise<{
|
|
53
|
+
width: number;
|
|
54
|
+
height: number;
|
|
55
|
+
image: Jimp;
|
|
56
|
+
}>;
|
|
57
|
+
export declare function paddingToMatchBlockByBase64(imageBase64: string, blockSize?: number): Promise<{
|
|
58
|
+
width: number;
|
|
59
|
+
height: number;
|
|
60
|
+
imageBase64: string;
|
|
61
|
+
}>;
|
|
62
|
+
export declare function cropByRect(imageBase64: string, rect: Rect, paddingImage: boolean): Promise<{
|
|
63
|
+
width: number;
|
|
64
|
+
height: number;
|
|
65
|
+
imageBase64: string;
|
|
66
|
+
}>;
|
|
67
|
+
export declare function jimpToBase64(image: Jimp): Promise<string>;
|
|
68
|
+
export declare const httpImg2Base64: (url: string) => Promise<string>;
|
|
69
|
+
/**
|
|
70
|
+
* Convert image file to base64 string
|
|
71
|
+
* Because this method is synchronous, the npm package `sharp` cannot be used to detect the file type.
|
|
72
|
+
* TODO: convert to webp to reduce base64 size.
|
|
73
|
+
*/
|
|
74
|
+
export declare const localImg2Base64: (imgPath: string, withoutHeader?: boolean) => string;
|
|
75
|
+
/**
|
|
76
|
+
* PreProcess image url to ensure image is accessible to LLM.
|
|
77
|
+
* @param url - The url of the image, it can be a http url or a base64 string or a file path
|
|
78
|
+
* @param convertHttpImage2Base64 - Whether to convert http image to base64, if true, the http image will be converted to base64, otherwise, the http image will be returned as is
|
|
79
|
+
* @returns The base64 string of the image (when convertHttpImage2Base64 is true or url is a file path) or the http image url
|
|
80
|
+
*/
|
|
81
|
+
export declare const preProcessImageUrl: (url: string, convertHttpImage2Base64: boolean) => Promise<string>;
|
|
82
|
+
/**
|
|
83
|
+
* parse base64 string to get mimeType and body
|
|
84
|
+
*/
|
|
85
|
+
export declare const parseBase64: (fullBase64String: string) => {
|
|
86
|
+
mimeType: string;
|
|
87
|
+
body: string;
|
|
88
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
interface PkgInfo {
|
|
2
|
+
name: string;
|
|
3
|
+
version: string;
|
|
4
|
+
dir: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function getRunningPkgInfo(dir?: string): PkgInfo | null;
|
|
7
|
+
/**
|
|
8
|
+
* Find the nearest package.json file recursively
|
|
9
|
+
* @param {string} dir - Home directory
|
|
10
|
+
* @returns {string|null} - The most recent package.json file path or null
|
|
11
|
+
*/
|
|
12
|
+
export declare function findNearestPackageJson(dir: string): string | null;
|
|
13
|
+
export declare function getElementInfosScriptContent(): string;
|
|
14
|
+
export declare function getExtraReturnLogic(tree?: boolean): Promise<";rpascene_element_inspector.setNodeHashCacheListOnWindow();;rpascene_element_inspector.webExtractNodeTree()" | ";rpascene_element_inspector.setNodeHashCacheListOnWindow();;rpascene_element_inspector.webExtractTextWithPosition()" | null>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './fs';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { NodeType } from '../constants';
|
|
2
|
+
import type { ElementInfo } from '../extractor';
|
|
3
|
+
export interface Point {
|
|
4
|
+
left: number;
|
|
5
|
+
top: number;
|
|
6
|
+
}
|
|
7
|
+
export interface Size {
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
dpr?: number;
|
|
11
|
+
}
|
|
12
|
+
export type Rect = Point & Size & {
|
|
13
|
+
zoom?: number;
|
|
14
|
+
};
|
|
15
|
+
export declare abstract class BaseElement {
|
|
16
|
+
abstract id: string;
|
|
17
|
+
abstract indexId?: number;
|
|
18
|
+
abstract attributes: {
|
|
19
|
+
nodeType: NodeType;
|
|
20
|
+
[key: string]: string;
|
|
21
|
+
};
|
|
22
|
+
abstract content: string;
|
|
23
|
+
abstract rect: Rect;
|
|
24
|
+
abstract center: [number, number];
|
|
25
|
+
abstract xpaths?: string[];
|
|
26
|
+
abstract isVisible: boolean;
|
|
27
|
+
abstract allPaths?: any[];
|
|
28
|
+
abstract containerPaths?: any[];
|
|
29
|
+
}
|
|
30
|
+
export interface ElementTreeNode<ElementType extends BaseElement = BaseElement> {
|
|
31
|
+
node: ElementType | null;
|
|
32
|
+
children: ElementTreeNode<ElementType>[];
|
|
33
|
+
}
|
|
34
|
+
export interface WebElementInfo extends ElementInfo {
|
|
35
|
+
zoom: number;
|
|
36
|
+
locator?: string;
|
|
37
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2017 Google Inc.
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export interface KeyDefinition {
|
|
10
|
+
keyCode?: number;
|
|
11
|
+
shiftKeyCode?: number;
|
|
12
|
+
key?: string;
|
|
13
|
+
shiftKey?: string;
|
|
14
|
+
code?: string;
|
|
15
|
+
text?: string;
|
|
16
|
+
shiftText?: string;
|
|
17
|
+
location?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* All the valid keys that can be passed to functions that take user input, such
|
|
21
|
+
* as {@link Keyboard.press | keyboard.press }
|
|
22
|
+
*
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export type KeyInput = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'Power' | 'Eject' | 'Abort' | 'Help' | 'Backspace' | 'Tab' | 'Numpad5' | 'NumpadEnter' | 'Enter' | '\r' | '\n' | 'ShiftLeft' | 'ShiftRight' | 'ControlLeft' | 'ControlRight' | 'AltLeft' | 'AltRight' | 'Pause' | 'CapsLock' | 'Escape' | 'Convert' | 'NonConvert' | 'Space' | 'Numpad9' | 'PageUp' | 'Numpad3' | 'PageDown' | 'End' | 'Numpad1' | 'Home' | 'Numpad7' | 'ArrowLeft' | 'Numpad4' | 'Numpad8' | 'ArrowUp' | 'ArrowRight' | 'Numpad6' | 'Numpad2' | 'ArrowDown' | 'Select' | 'Open' | 'PrintScreen' | 'Insert' | 'Numpad0' | 'Delete' | 'NumpadDecimal' | 'Digit0' | 'Digit1' | 'Digit2' | 'Digit3' | 'Digit4' | 'Digit5' | 'Digit6' | 'Digit7' | 'Digit8' | 'Digit9' | 'KeyA' | 'KeyB' | 'KeyC' | 'KeyD' | 'KeyE' | 'KeyF' | 'KeyG' | 'KeyH' | 'KeyI' | 'KeyJ' | 'KeyK' | 'KeyL' | 'KeyM' | 'KeyN' | 'KeyO' | 'KeyP' | 'KeyQ' | 'KeyR' | 'KeyS' | 'KeyT' | 'KeyU' | 'KeyV' | 'KeyW' | 'KeyX' | 'KeyY' | 'KeyZ' | 'MetaLeft' | 'MetaRight' | 'ContextMenu' | 'NumpadMultiply' | 'NumpadAdd' | 'NumpadSubtract' | 'NumpadDivide' | 'F1' | 'F2' | 'F3' | 'F4' | 'F5' | 'F6' | 'F7' | 'F8' | 'F9' | 'F10' | 'F11' | 'F12' | 'F13' | 'F14' | 'F15' | 'F16' | 'F17' | 'F18' | 'F19' | 'F20' | 'F21' | 'F22' | 'F23' | 'F24' | 'NumLock' | 'ScrollLock' | 'AudioVolumeMute' | 'AudioVolumeDown' | 'AudioVolumeUp' | 'MediaTrackNext' | 'MediaTrackPrevious' | 'MediaStop' | 'MediaPlayPause' | 'Semicolon' | 'Equal' | 'NumpadEqual' | 'Comma' | 'Minus' | 'Period' | 'Slash' | 'Backquote' | 'BracketLeft' | 'Backslash' | 'BracketRight' | 'Quote' | 'AltGraph' | 'Props' | 'Cancel' | 'Clear' | 'Shift' | 'Control' | 'Alt' | 'Accept' | 'ModeChange' | ' ' | 'Print' | 'Execute' | '\u0000' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z' | 'Meta' | '*' | '+' | '-' | '/' | ';' | '=' | ',' | '.' | '`' | '[' | '\\' | ']' | "'" | 'Attn' | 'CrSel' | 'ExSel' | 'EraseEof' | 'Play' | 'ZoomOut' | ')' | '!' | '@' | '#' | '$' | '%' | '^' | '&' | '(' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z' | ':' | '<' | '_' | '>' | '?' | '~' | '{' | '|' | '}' | '"' | 'SoftLeft' | 'SoftRight' | 'Camera' | 'Call' | 'EndCall' | 'VolumeDown' | 'VolumeUp';
|
|
26
|
+
/**
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export declare const _keyDefinitions: Readonly<Record<KeyInput, KeyDefinition>>;
|
|
30
|
+
export declare const getKeyDefinition: (key: string) => KeyInput;
|
|
31
|
+
export declare const isMac: boolean;
|
|
32
|
+
export declare function transformHotkeyInput(keyInput: string): string[];
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare const ifInBrowser: boolean;
|
|
2
|
+
export declare const ifInWorker: boolean;
|
|
3
|
+
export declare const ifInNode: string | false;
|
|
4
|
+
export declare function uuid(): string;
|
|
5
|
+
export declare function generateHashId(rect: any, content?: string): string;
|
|
6
|
+
/**
|
|
7
|
+
* A utility function that asserts a condition and throws an error with a message if the condition is false.
|
|
8
|
+
*
|
|
9
|
+
* @param condition - The condition to assert
|
|
10
|
+
* @param message - The error message to throw if the condition is false
|
|
11
|
+
* @throws Error with the provided message if the condition is false
|
|
12
|
+
*/
|
|
13
|
+
export declare function assert(condition: any, message?: string): asserts condition;
|
|
14
|
+
type GlobalScope = typeof window | typeof globalThis | typeof self | undefined;
|
|
15
|
+
export declare function getGlobalScope(): GlobalScope;
|
|
16
|
+
export declare function setIsMcp(value: boolean): void;
|
|
17
|
+
export declare function logMsg(...message: Parameters<typeof console.log>): void;
|
|
18
|
+
export declare function repeat(times: number, fn: (index: number) => Promise<void>): Promise<void>;
|
|
19
|
+
export declare const escapeScriptTag: (html: string) => string;
|
|
20
|
+
export declare const antiEscapeScriptTag: (html: string) => string;
|
|
21
|
+
export declare function replaceIllegalPathCharsAndSpace(str: string): string;
|
|
22
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rpascene/shared",
|
|
3
|
+
"version": "0.30.8",
|
|
4
|
+
"repository": "",
|
|
5
|
+
"homepage": "",
|
|
6
|
+
"types": "./dist/types/index.d.ts",
|
|
7
|
+
"main": "./dist/lib/index.js",
|
|
8
|
+
"module": "./dist/es/index.mjs",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/types/index.d.ts",
|
|
12
|
+
"import": "./dist/es/index.mjs",
|
|
13
|
+
"require": "./dist/lib/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./constants": {
|
|
16
|
+
"types": "./dist/types/constants/index.d.ts",
|
|
17
|
+
"import": "./dist/es/constants/index.mjs",
|
|
18
|
+
"require": "./dist/lib/constants/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./extractor": {
|
|
21
|
+
"types": "./dist/types/extractor/index.d.ts",
|
|
22
|
+
"import": "./dist/es/extractor/index.mjs",
|
|
23
|
+
"require": "./dist/lib/extractor/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./img": {
|
|
26
|
+
"types": "./dist/types/img/index.d.ts",
|
|
27
|
+
"import": "./dist/es/img/index.mjs",
|
|
28
|
+
"require": "./dist/lib/img/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./node": {
|
|
31
|
+
"types": "./dist/types/node/index.d.ts",
|
|
32
|
+
"import": "./dist/es/node/index.mjs",
|
|
33
|
+
"require": "./dist/lib/node/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./types": {
|
|
36
|
+
"types": "./dist/types/types/index.d.ts",
|
|
37
|
+
"import": "./dist/es/types/index.mjs",
|
|
38
|
+
"require": "./dist/lib/types/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./env": {
|
|
41
|
+
"types": "./dist/types/env/index.d.ts",
|
|
42
|
+
"import": "./dist/es/env/index.mjs",
|
|
43
|
+
"require": "./dist/lib/env/index.js"
|
|
44
|
+
},
|
|
45
|
+
"./polyfills": {
|
|
46
|
+
"types": "./dist/types/polyfills/index.d.ts",
|
|
47
|
+
"import": "./dist/es/polyfills/index.mjs",
|
|
48
|
+
"require": "./dist/lib/polyfills/index.js"
|
|
49
|
+
},
|
|
50
|
+
"./utils": {
|
|
51
|
+
"types": "./dist/types/utils.d.ts",
|
|
52
|
+
"import": "./dist/es/utils.mjs",
|
|
53
|
+
"require": "./dist/lib/utils.js"
|
|
54
|
+
},
|
|
55
|
+
"./common": {
|
|
56
|
+
"types": "./dist/types/common.d.ts",
|
|
57
|
+
"import": "./dist/es/common.mjs",
|
|
58
|
+
"require": "./dist/lib/common.js"
|
|
59
|
+
},
|
|
60
|
+
"./*": {
|
|
61
|
+
"types": "./dist/types/*.d.ts",
|
|
62
|
+
"import": "./dist/es/*.mjs",
|
|
63
|
+
"require": "./dist/lib/*.js"
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"files": ["dist", "src", "README.md"],
|
|
67
|
+
"scripts": {
|
|
68
|
+
"dev": "npm run build:watch",
|
|
69
|
+
"build": "npm run build:script && npm run build:pkg",
|
|
70
|
+
"build:pkg": "rslib build",
|
|
71
|
+
"build:script": "rslib build -c ./rslib.inspect.config.ts",
|
|
72
|
+
"build:watch": "npm run build:script && rslib build --watch",
|
|
73
|
+
"reset": "rimraf ./**/node_modules",
|
|
74
|
+
"test": "vitest --run",
|
|
75
|
+
"test:u": "vitest --run -u"
|
|
76
|
+
},
|
|
77
|
+
"dependencies": {
|
|
78
|
+
"@silvia-odwyer/photon": "0.3.3",
|
|
79
|
+
"@silvia-odwyer/photon-node": "0.3.3",
|
|
80
|
+
"debug": "4.4.0",
|
|
81
|
+
"jimp": "0.22.12",
|
|
82
|
+
"js-sha256": "0.11.0",
|
|
83
|
+
"sharp": "^0.34.3",
|
|
84
|
+
"uuid": "11.1.0"
|
|
85
|
+
},
|
|
86
|
+
"devDependencies": {
|
|
87
|
+
"@rslib/core": "^0.11.2",
|
|
88
|
+
"@types/debug": "4.1.12",
|
|
89
|
+
"@types/node": "^18.0.0",
|
|
90
|
+
"@ui-tars/shared": "1.2.0",
|
|
91
|
+
"dotenv": "^16.4.5",
|
|
92
|
+
"rimraf": "~3.0.2",
|
|
93
|
+
"typescript": "^5.8.3",
|
|
94
|
+
"vitest": "3.0.5"
|
|
95
|
+
},
|
|
96
|
+
"sideEffects": [],
|
|
97
|
+
"publishConfig": {
|
|
98
|
+
"access": "public",
|
|
99
|
+
"registry": "https://registry.npmjs.org/"
|
|
100
|
+
},
|
|
101
|
+
"license": "MIT"
|
|
102
|
+
}
|
package/src/baseDB.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// Generic database operations class
|
|
2
|
+
export class IndexedDBManager {
|
|
3
|
+
private dbPromise: Promise<IDBDatabase>;
|
|
4
|
+
private dbName: string;
|
|
5
|
+
private version: number;
|
|
6
|
+
private storeConfigs: Array<{ name: string; keyPath: string }>;
|
|
7
|
+
|
|
8
|
+
constructor(
|
|
9
|
+
dbName: string,
|
|
10
|
+
version: number,
|
|
11
|
+
storeConfigs: Array<{ name: string; keyPath: string }>,
|
|
12
|
+
) {
|
|
13
|
+
this.dbName = dbName;
|
|
14
|
+
this.version = version;
|
|
15
|
+
this.storeConfigs = storeConfigs;
|
|
16
|
+
this.dbPromise = this.initDB();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
private initDB(): Promise<IDBDatabase> {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const request = indexedDB.open(this.dbName, this.version);
|
|
22
|
+
|
|
23
|
+
request.onerror = () => reject(request.error);
|
|
24
|
+
request.onsuccess = () => resolve(request.result);
|
|
25
|
+
|
|
26
|
+
request.onupgradeneeded = (event) => {
|
|
27
|
+
const db = (event.target as IDBOpenDBRequest).result;
|
|
28
|
+
|
|
29
|
+
// Create stores if they don't exist
|
|
30
|
+
this.storeConfigs.forEach(({ name, keyPath }) => {
|
|
31
|
+
if (!db.objectStoreNames.contains(name)) {
|
|
32
|
+
const store = db.createObjectStore(name, { keyPath });
|
|
33
|
+
store.createIndex('timestamp', 'timestamp', { unique: false });
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private async withTransaction<T>(
|
|
41
|
+
storeNames: string | string[],
|
|
42
|
+
mode: IDBTransactionMode,
|
|
43
|
+
operation: (stores: IDBObjectStore | IDBObjectStore[]) => Promise<T>,
|
|
44
|
+
): Promise<T> {
|
|
45
|
+
const db = await this.dbPromise;
|
|
46
|
+
const transaction = db.transaction(storeNames, mode);
|
|
47
|
+
|
|
48
|
+
const stores = Array.isArray(storeNames)
|
|
49
|
+
? storeNames.map((name) => transaction.objectStore(name))
|
|
50
|
+
: transaction.objectStore(storeNames);
|
|
51
|
+
|
|
52
|
+
return operation(stores);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private promisifyRequest<T>(request: IDBRequest<T>): Promise<T> {
|
|
56
|
+
return new Promise((resolve, reject) => {
|
|
57
|
+
request.onsuccess = () => resolve(request.result);
|
|
58
|
+
request.onerror = () => reject(request.error);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async put<T>(storeName: string, data: T): Promise<void> {
|
|
63
|
+
await this.withTransaction(storeName, 'readwrite', async (store) => {
|
|
64
|
+
await this.promisifyRequest((store as IDBObjectStore).put(data));
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async get<T>(storeName: string, key: string): Promise<T | undefined> {
|
|
69
|
+
return this.withTransaction(storeName, 'readonly', async (store) => {
|
|
70
|
+
return this.promisifyRequest((store as IDBObjectStore).get(key));
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async getAll<T>(storeName: string, sortByTimestamp = true): Promise<T[]> {
|
|
75
|
+
return this.withTransaction(storeName, 'readonly', async (store) => {
|
|
76
|
+
const objectStore = store as IDBObjectStore;
|
|
77
|
+
const results = sortByTimestamp
|
|
78
|
+
? await this.promisifyRequest(objectStore.index('timestamp').getAll())
|
|
79
|
+
: await this.promisifyRequest(objectStore.getAll());
|
|
80
|
+
|
|
81
|
+
return sortByTimestamp
|
|
82
|
+
? results.sort((a: any, b: any) => a.timestamp - b.timestamp)
|
|
83
|
+
: results;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async clear(storeName: string): Promise<void> {
|
|
88
|
+
await this.withTransaction(storeName, 'readwrite', async (store) => {
|
|
89
|
+
await this.promisifyRequest((store as IDBObjectStore).clear());
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async delete(storeName: string, key: string): Promise<void> {
|
|
94
|
+
await this.withTransaction(storeName, 'readwrite', async (store) => {
|
|
95
|
+
await this.promisifyRequest((store as IDBObjectStore).delete(key));
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async count(storeName: string): Promise<number> {
|
|
100
|
+
return this.withTransaction(storeName, 'readonly', async (store) => {
|
|
101
|
+
return this.promisifyRequest((store as IDBObjectStore).count());
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
getDBPromise(): Promise<IDBDatabase> {
|
|
106
|
+
return this.dbPromise;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Generic error handler wrapper
|
|
111
|
+
export const withErrorHandling = async <T>(
|
|
112
|
+
operation: () => Promise<T>,
|
|
113
|
+
errorMessage: string,
|
|
114
|
+
defaultValue?: T,
|
|
115
|
+
onQuotaExceeded?: () => Promise<void>,
|
|
116
|
+
): Promise<T | undefined> => {
|
|
117
|
+
try {
|
|
118
|
+
return await operation();
|
|
119
|
+
} catch (e) {
|
|
120
|
+
console.error(errorMessage, e);
|
|
121
|
+
if (
|
|
122
|
+
e instanceof Error &&
|
|
123
|
+
e.name === 'QuotaExceededError' &&
|
|
124
|
+
onQuotaExceeded
|
|
125
|
+
) {
|
|
126
|
+
console.log('Storage quota exceeded, running cleanup...');
|
|
127
|
+
await onQuotaExceeded();
|
|
128
|
+
}
|
|
129
|
+
return defaultValue;
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// Base cleanup function for managing storage space
|
|
134
|
+
export const createCleanupFunction = <
|
|
135
|
+
T extends { id: string; timestamp: number },
|
|
136
|
+
>(
|
|
137
|
+
dbManager: IndexedDBManager,
|
|
138
|
+
storeName: string,
|
|
139
|
+
maxItems: number,
|
|
140
|
+
) => {
|
|
141
|
+
return async (): Promise<void> => {
|
|
142
|
+
try {
|
|
143
|
+
const results = await dbManager.getAll<T>(storeName);
|
|
144
|
+
|
|
145
|
+
if (results.length > maxItems) {
|
|
146
|
+
const toDelete = results
|
|
147
|
+
.sort((a, b) => a.timestamp - b.timestamp)
|
|
148
|
+
.slice(0, results.length - maxItems);
|
|
149
|
+
|
|
150
|
+
await Promise.all(
|
|
151
|
+
toDelete.map((item) => dbManager.delete(storeName, item.id)),
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
} catch (e) {
|
|
155
|
+
console.error(`Failed to cleanup ${storeName}:`, e);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
export interface CopyStaticOptions {
|
|
5
|
+
/** Source directory to copy from */
|
|
6
|
+
srcDir: string;
|
|
7
|
+
/** Destination directory to copy to */
|
|
8
|
+
destDir: string;
|
|
9
|
+
/** Optional favicon source path (relative to directory containing srcDir) */
|
|
10
|
+
faviconPath?: string;
|
|
11
|
+
/** Name for the rsbuild plugin */
|
|
12
|
+
pluginName?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Creates an rsbuild plugin that copies static files after build
|
|
17
|
+
* @param options Configuration options for copying static files
|
|
18
|
+
* @returns Rsbuild plugin object
|
|
19
|
+
*/
|
|
20
|
+
export const createCopyStaticPlugin = (options: CopyStaticOptions) => ({
|
|
21
|
+
name: options.pluginName || 'copy-static',
|
|
22
|
+
setup(api: any) {
|
|
23
|
+
api.onAfterBuild(async () => {
|
|
24
|
+
const { srcDir, destDir, faviconPath } = options;
|
|
25
|
+
|
|
26
|
+
await fs.promises.mkdir(destDir, { recursive: true });
|
|
27
|
+
|
|
28
|
+
// Copy directory contents recursively
|
|
29
|
+
await fs.promises.cp(srcDir, destDir, { recursive: true });
|
|
30
|
+
console.log(`Copied build artifacts from ${srcDir} to ${destDir}`);
|
|
31
|
+
|
|
32
|
+
// Copy favicon if specified
|
|
33
|
+
if (faviconPath) {
|
|
34
|
+
const faviconDest = path.join(destDir, 'favicon.ico');
|
|
35
|
+
await fs.promises.copyFile(faviconPath, faviconDest);
|
|
36
|
+
console.log(`Copied favicon from ${faviconPath} to ${faviconDest}`);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Helper function to create a copy static plugin for playground builds
|
|
44
|
+
* @param srcDir Source directory (usually dist directory)
|
|
45
|
+
* @param destDir Destination directory
|
|
46
|
+
* @param pluginName Optional plugin name
|
|
47
|
+
* @param faviconSrc Optional favicon source path
|
|
48
|
+
* @returns Rsbuild plugin
|
|
49
|
+
*/
|
|
50
|
+
export const createPlaygroundCopyPlugin = (
|
|
51
|
+
srcDir: string,
|
|
52
|
+
destDir: string,
|
|
53
|
+
pluginName?: string,
|
|
54
|
+
faviconSrc?: string,
|
|
55
|
+
) => {
|
|
56
|
+
return createCopyStaticPlugin({
|
|
57
|
+
srcDir,
|
|
58
|
+
destDir,
|
|
59
|
+
faviconPath: faviconSrc,
|
|
60
|
+
pluginName,
|
|
61
|
+
});
|
|
62
|
+
};
|
package/src/common.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { existsSync, mkdirSync } from 'node:fs';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
// do not import getBasicEnvValue and RPASCENE_RUN_DIR directly from ./env,
|
|
5
|
+
// because it will cause circular dependency
|
|
6
|
+
import { getBasicEnvValue } from './env/basic';
|
|
7
|
+
import { RPASCENE_RUN_DIR } from './env/types';
|
|
8
|
+
import { ifInNode } from './utils';
|
|
9
|
+
|
|
10
|
+
export const defaultRunDirName = 'rpascene_run';
|
|
11
|
+
// Define locally for now to avoid import issues
|
|
12
|
+
|
|
13
|
+
export const getRpasceneRunDir = () => {
|
|
14
|
+
if (!ifInNode) {
|
|
15
|
+
return '';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return getBasicEnvValue(RPASCENE_RUN_DIR) || defaultRunDirName;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const getRpasceneRunBaseDir = () => {
|
|
22
|
+
if (!ifInNode) {
|
|
23
|
+
return '';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let basePath = path.resolve(process.cwd(), getRpasceneRunDir());
|
|
27
|
+
|
|
28
|
+
// Create a base directory
|
|
29
|
+
if (!existsSync(basePath)) {
|
|
30
|
+
try {
|
|
31
|
+
mkdirSync(basePath, { recursive: true });
|
|
32
|
+
} catch (error) {
|
|
33
|
+
// console.error(`Failed to create ${runDirName} directory: ${error}`);
|
|
34
|
+
basePath = path.join(tmpdir(), defaultRunDirName);
|
|
35
|
+
mkdirSync(basePath, { recursive: true });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return basePath;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Get the path to the rpascene_run directory or a subdirectory within it.
|
|
44
|
+
* Creates the directory if it doesn't exist.
|
|
45
|
+
*
|
|
46
|
+
* @param subdir - Optional subdirectory name (e.g., 'log', 'report')
|
|
47
|
+
* @returns The absolute path to the requested directory
|
|
48
|
+
*/
|
|
49
|
+
export const getRpasceneRunSubDir = (
|
|
50
|
+
subdir: 'dump' | 'cache' | 'report' | 'tmp' | 'log' | 'output',
|
|
51
|
+
): string => {
|
|
52
|
+
if (!ifInNode) {
|
|
53
|
+
return '';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Create a log directory
|
|
57
|
+
const basePath = getRpasceneRunBaseDir();
|
|
58
|
+
const logPath = path.join(basePath, subdir);
|
|
59
|
+
if (!existsSync(logPath)) {
|
|
60
|
+
mkdirSync(logPath, { recursive: true });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return logPath;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const ERROR_CODE_NOT_IMPLEMENTED_AS_DESIGNED =
|
|
67
|
+
'NOT_IMPLEMENTED_AS_DESIGNED';
|