@open-wa/screencaster 5.0.0 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -3
- package/src/client.ts +402 -0
- package/src/server.ts +861 -0
- package/src/types.ts +206 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// ────────────────────────────────────────────────────────────
|
|
2
|
+
// @open-wa/screencaster — Shared Protocol Types
|
|
3
|
+
//
|
|
4
|
+
// Consumed by both the server (Node.js) and client (browser).
|
|
5
|
+
// This file has ZERO runtime deps — pure type definitions + enums.
|
|
6
|
+
// ────────────────────────────────────────────────────────────
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Options for CDP Page.startScreencast.
|
|
10
|
+
*/
|
|
11
|
+
export interface ScreencastOptions {
|
|
12
|
+
format?: 'jpeg' | 'png';
|
|
13
|
+
quality?: number;
|
|
14
|
+
maxWidth?: number;
|
|
15
|
+
maxHeight?: number;
|
|
16
|
+
everyNthFrame?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Default screencast options.
|
|
21
|
+
*/
|
|
22
|
+
export const DEFAULT_SCREENCAST_OPTIONS: Required<ScreencastOptions> = {
|
|
23
|
+
format: 'jpeg',
|
|
24
|
+
quality: 80,
|
|
25
|
+
maxWidth: 1280,
|
|
26
|
+
maxHeight: 720,
|
|
27
|
+
everyNthFrame: 1,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// ────── Server → Client Messages ──────
|
|
31
|
+
|
|
32
|
+
export interface FrameMessage {
|
|
33
|
+
type: 'frame';
|
|
34
|
+
/** base64-encoded image data */
|
|
35
|
+
data: string;
|
|
36
|
+
/** CDP session ID — must be ACK'd back to keep frames flowing */
|
|
37
|
+
sessionId: number;
|
|
38
|
+
metadata: {
|
|
39
|
+
width: number;
|
|
40
|
+
height: number;
|
|
41
|
+
timestamp: number;
|
|
42
|
+
offsetTop: number;
|
|
43
|
+
pageScaleFactor: number;
|
|
44
|
+
deviceWidth: number;
|
|
45
|
+
deviceHeight: number;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface NavStateMessage {
|
|
50
|
+
type: 'nav-state';
|
|
51
|
+
url: string;
|
|
52
|
+
canGoBack: boolean;
|
|
53
|
+
canGoForward: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface ErrorMessage {
|
|
57
|
+
type: 'error';
|
|
58
|
+
message: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface StoppedMessage {
|
|
62
|
+
type: 'stopped';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface ReadyMessage {
|
|
66
|
+
type: 'ready';
|
|
67
|
+
/** Whether a page driver is currently bound */
|
|
68
|
+
bound: boolean;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type ServerMessage =
|
|
72
|
+
| FrameMessage
|
|
73
|
+
| NavStateMessage
|
|
74
|
+
| ErrorMessage
|
|
75
|
+
| StoppedMessage
|
|
76
|
+
| ReadyMessage;
|
|
77
|
+
|
|
78
|
+
// ────── Client → Server Messages ──────
|
|
79
|
+
|
|
80
|
+
export interface StartMessage {
|
|
81
|
+
type: 'start';
|
|
82
|
+
options?: ScreencastOptions;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface StopMessage {
|
|
86
|
+
type: 'stop';
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface AckMessage {
|
|
90
|
+
type: 'ack';
|
|
91
|
+
sessionId: number;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface MouseMessage {
|
|
95
|
+
type: 'mouse';
|
|
96
|
+
action: 'move' | 'down' | 'up';
|
|
97
|
+
/** Normalized X coordinate (0-1) */
|
|
98
|
+
x: number;
|
|
99
|
+
/** Normalized Y coordinate (0-1) */
|
|
100
|
+
y: number;
|
|
101
|
+
button?: 'left' | 'right' | 'middle';
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface KeyMessage {
|
|
105
|
+
type: 'key';
|
|
106
|
+
action: 'down' | 'up';
|
|
107
|
+
key: string;
|
|
108
|
+
code: string;
|
|
109
|
+
modifiers?: number;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface ScrollMessage {
|
|
113
|
+
type: 'scroll';
|
|
114
|
+
deltaX: number;
|
|
115
|
+
deltaY: number;
|
|
116
|
+
/** Normalized X coordinate (0-1) */
|
|
117
|
+
x: number;
|
|
118
|
+
/** Normalized Y coordinate (0-1) */
|
|
119
|
+
y: number;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export interface NavigateMessage {
|
|
123
|
+
type: 'navigate';
|
|
124
|
+
url: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface GoBackMessage {
|
|
128
|
+
type: 'go-back';
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface GoForwardMessage {
|
|
132
|
+
type: 'go-forward';
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface ResizeMessage {
|
|
136
|
+
type: 'resize';
|
|
137
|
+
width: number;
|
|
138
|
+
height: number;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export type ClientMessage =
|
|
142
|
+
| StartMessage
|
|
143
|
+
| StopMessage
|
|
144
|
+
| AckMessage
|
|
145
|
+
| MouseMessage
|
|
146
|
+
| KeyMessage
|
|
147
|
+
| ScrollMessage
|
|
148
|
+
| NavigateMessage
|
|
149
|
+
| GoBackMessage
|
|
150
|
+
| GoForwardMessage
|
|
151
|
+
| ResizeMessage;
|
|
152
|
+
|
|
153
|
+
// ────── Minimal Page Interface ──────
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* A minimal CDP-capable page interface.
|
|
157
|
+
* This is a subset of IPage from @open-wa/driver-interface
|
|
158
|
+
* that captures exactly what the screencaster needs.
|
|
159
|
+
*
|
|
160
|
+
* Consumers pass in an IPage from driver-interface; the factory
|
|
161
|
+
* narrows it to this interface, keeping the screencaster decoupled
|
|
162
|
+
* from any specific driver package.
|
|
163
|
+
*/
|
|
164
|
+
export interface IScreencastPage {
|
|
165
|
+
/** Get a CDP session for sending raw CDP commands */
|
|
166
|
+
cdp(): IScreencastCDPSession | Promise<IScreencastCDPSession>;
|
|
167
|
+
|
|
168
|
+
/** Navigate to a URL */
|
|
169
|
+
goto(url: string, options?: { waitUntil?: string }): Promise<void>;
|
|
170
|
+
|
|
171
|
+
/** Reload the current page */
|
|
172
|
+
reload(): Promise<void>;
|
|
173
|
+
|
|
174
|
+
/** Check if the page has been closed */
|
|
175
|
+
isClosed(): boolean;
|
|
176
|
+
|
|
177
|
+
/** Get underlying driver for mouse/keyboard (opaque) */
|
|
178
|
+
unwrap(): unknown;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export interface IScreencastCDPSession {
|
|
182
|
+
send<T = unknown>(method: string, params?: Record<string, unknown>): Promise<T>;
|
|
183
|
+
detach(): Promise<void>;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* The CDP modifier key bitmask values.
|
|
188
|
+
* Used to convert from the modifier bitmask to individual key states.
|
|
189
|
+
*/
|
|
190
|
+
export const Modifiers = {
|
|
191
|
+
Alt: 1,
|
|
192
|
+
Ctrl: 2,
|
|
193
|
+
Meta: 4,
|
|
194
|
+
Shift: 8,
|
|
195
|
+
} as const;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Special keys that should be dispatched as raw key events
|
|
199
|
+
* rather than typed as text.
|
|
200
|
+
*/
|
|
201
|
+
export const SPECIAL_KEYS = new Set([
|
|
202
|
+
'Tab', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight',
|
|
203
|
+
'Backspace', 'Enter', 'Escape', 'Delete', 'Home', 'End',
|
|
204
|
+
'PageUp', 'PageDown', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6',
|
|
205
|
+
'F7', 'F8', 'F9', 'F10', 'F11', 'F12',
|
|
206
|
+
]);
|