@full-self-browsing/phantom-stream 0.1.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/LICENSE +21 -0
- package/README.md +263 -0
- package/bin/phantom-stream.js +193 -0
- package/dist/types/adapters/bookmarklet.d.ts +22 -0
- package/dist/types/adapters/bookmarklet.d.ts.map +1 -0
- package/dist/types/adapters/browser-inject.d.ts +7 -0
- package/dist/types/adapters/browser-inject.d.ts.map +1 -0
- package/dist/types/adapters/extension.d.ts +43 -0
- package/dist/types/adapters/extension.d.ts.map +1 -0
- package/dist/types/adapters/playwright-inject.d.ts +2 -0
- package/dist/types/adapters/playwright-inject.d.ts.map +1 -0
- package/dist/types/adapters/playwright.d.ts +14 -0
- package/dist/types/adapters/playwright.d.ts.map +1 -0
- package/dist/types/capture/index.d.ts +241 -0
- package/dist/types/capture/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/protocol/constants.d.ts +35 -0
- package/dist/types/protocol/constants.d.ts.map +1 -0
- package/dist/types/protocol/envelope.d.ts +41 -0
- package/dist/types/protocol/envelope.d.ts.map +1 -0
- package/dist/types/protocol/index.d.ts +5 -0
- package/dist/types/protocol/index.d.ts.map +1 -0
- package/dist/types/protocol/messages.d.ts +520 -0
- package/dist/types/protocol/messages.d.ts.map +1 -0
- package/dist/types/protocol/remote-control.d.ts +70 -0
- package/dist/types/protocol/remote-control.d.ts.map +1 -0
- package/dist/types/relay/backends/ws.d.ts +39 -0
- package/dist/types/relay/backends/ws.d.ts.map +1 -0
- package/dist/types/relay/index.d.ts +4 -0
- package/dist/types/relay/index.d.ts.map +1 -0
- package/dist/types/relay/limits.d.ts +54 -0
- package/dist/types/relay/limits.d.ts.map +1 -0
- package/dist/types/relay/relay.d.ts +73 -0
- package/dist/types/relay/relay.d.ts.map +1 -0
- package/dist/types/renderer/diff.d.ts +62 -0
- package/dist/types/renderer/diff.d.ts.map +1 -0
- package/dist/types/renderer/index.d.ts +211 -0
- package/dist/types/renderer/index.d.ts.map +1 -0
- package/dist/types/renderer/overlays.d.ts +166 -0
- package/dist/types/renderer/overlays.d.ts.map +1 -0
- package/dist/types/renderer/sanitize.d.ts +73 -0
- package/dist/types/renderer/sanitize.d.ts.map +1 -0
- package/dist/types/renderer/snapshot.d.ts +41 -0
- package/dist/types/renderer/snapshot.d.ts.map +1 -0
- package/dist/types/transport/websocket.d.ts +44 -0
- package/dist/types/transport/websocket.d.ts.map +1 -0
- package/docs/ARCHITECTURE.md +273 -0
- package/docs/DESIGN-HISTORY.md +120 -0
- package/docs/QUICKSTARTS.md +289 -0
- package/docs/RELEASE.md +60 -0
- package/docs/SECURITY.md +156 -0
- package/docs/paper/OUTLINE.md +100 -0
- package/examples/bookmarklet-demo/demo.css +102 -0
- package/examples/bookmarklet-demo/server.js +297 -0
- package/examples/bookmarklet-demo/source.html +52 -0
- package/examples/bookmarklet-demo/viewer.html +48 -0
- package/examples/extension-mv3/demo.css +102 -0
- package/examples/extension-mv3/server.js +350 -0
- package/examples/extension-mv3/source.html +48 -0
- package/examples/extension-mv3/viewer.html +48 -0
- package/examples/loopback-mirror.html +369 -0
- package/examples/loopback-transport.js +65 -0
- package/examples/playwright-demo/demo.css +462 -0
- package/examples/playwright-demo/fixture.html +56 -0
- package/examples/playwright-demo/fixture.js +57 -0
- package/examples/playwright-demo/server.js +517 -0
- package/examples/playwright-demo/viewer.html +112 -0
- package/examples/playwright-demo/viewer.js +414 -0
- package/examples/serve.js +97 -0
- package/examples/two-tab-demo/demo.css +266 -0
- package/examples/two-tab-demo/server.js +233 -0
- package/examples/two-tab-demo/source.html +70 -0
- package/examples/two-tab-demo/source.js +213 -0
- package/examples/two-tab-demo/viewer.html +72 -0
- package/examples/two-tab-demo/viewer.js +121 -0
- package/package.json +107 -0
- package/src/adapters/bookmarklet.js +98 -0
- package/src/adapters/browser-inject.js +17 -0
- package/src/adapters/extension.js +322 -0
- package/src/adapters/playwright-inject.js +4401 -0
- package/src/adapters/playwright.js +540 -0
- package/src/capture/README.md +302 -0
- package/src/capture/index.js +4361 -0
- package/src/index.js +5 -0
- package/src/protocol/constants.js +49 -0
- package/src/protocol/envelope.js +75 -0
- package/src/protocol/index.js +4 -0
- package/src/protocol/messages.js +268 -0
- package/src/protocol/remote-control.js +300 -0
- package/src/relay/README.md +22 -0
- package/src/relay/backends/ws.js +186 -0
- package/src/relay/index.js +3 -0
- package/src/relay/limits.js +105 -0
- package/src/relay/relay.js +397 -0
- package/src/renderer/README.md +443 -0
- package/src/renderer/diff.js +389 -0
- package/src/renderer/index.js +1401 -0
- package/src/renderer/overlays.js +528 -0
- package/src/renderer/sanitize.js +366 -0
- package/src/renderer/snapshot.js +202 -0
- package/src/transport/websocket.js +553 -0
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
// PhantomStream Playwright/CDP adapter.
|
|
2
|
+
//
|
|
3
|
+
// The adapter is the authorization boundary for viewer-originated remote
|
|
4
|
+
// control. It installs the page bridge, forwards main-frame capture messages,
|
|
5
|
+
// and replays approved control frames through Playwright or CDP driver APIs.
|
|
6
|
+
|
|
7
|
+
import { readFileSync } from 'node:fs';
|
|
8
|
+
import { randomBytes } from 'node:crypto';
|
|
9
|
+
import { fileURLToPath } from 'node:url';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
CONTROL,
|
|
13
|
+
REMOTE_CONTROL,
|
|
14
|
+
REMOTE_CONTROL_STATE,
|
|
15
|
+
STREAM,
|
|
16
|
+
createRemoteControlStateEvent,
|
|
17
|
+
isRemoteControlType,
|
|
18
|
+
validateRemoteControlMessage,
|
|
19
|
+
} from '../protocol/index.js';
|
|
20
|
+
|
|
21
|
+
var DEFAULT_BINDING_NAME = '__phantomStreamBridge';
|
|
22
|
+
var INJECT_TOKEN_DECLARATION = 'var PHANTOM_STREAM_BRIDGE_TOKEN = "";';
|
|
23
|
+
var INJECT_CAPTURE_OPTIONS_DECLARATION = 'var PHANTOM_STREAM_CAPTURE_OPTIONS = {};';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Read the checked-in classic-script inject artifact.
|
|
27
|
+
*
|
|
28
|
+
* @returns {string}
|
|
29
|
+
*/
|
|
30
|
+
export function getPlaywrightInjectSource(options) {
|
|
31
|
+
return buildPlaywrightInjectSource(options);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function buildPlaywrightInjectSource(options) {
|
|
35
|
+
var source = readFileSync(
|
|
36
|
+
fileURLToPath(new URL('./playwright-inject.js', import.meta.url)),
|
|
37
|
+
'utf8'
|
|
38
|
+
);
|
|
39
|
+
var opts = options || {};
|
|
40
|
+
var captureOptions = normalizeCaptureOptions(opts);
|
|
41
|
+
var out = source.replace(
|
|
42
|
+
INJECT_CAPTURE_OPTIONS_DECLARATION,
|
|
43
|
+
'var PHANTOM_STREAM_CAPTURE_OPTIONS = ' + JSON.stringify(captureOptions) + ';'
|
|
44
|
+
);
|
|
45
|
+
if (!Object.prototype.hasOwnProperty.call(opts, 'bridgeToken')) return out;
|
|
46
|
+
return out.replace(
|
|
47
|
+
INJECT_TOKEN_DECLARATION,
|
|
48
|
+
'var PHANTOM_STREAM_BRIDGE_TOKEN = ' + JSON.stringify(String(opts.bridgeToken || '')) + ';'
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function normalizeCaptureOptions(options) {
|
|
53
|
+
var opts = options || {};
|
|
54
|
+
var captureOptions = opts.captureOptions || {};
|
|
55
|
+
if (captureOptions && captureOptions.styleMode === 'cssom') {
|
|
56
|
+
return { styleMode: 'cssom' };
|
|
57
|
+
}
|
|
58
|
+
return {};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Create a Playwright/CDP adapter for one driven page.
|
|
63
|
+
*
|
|
64
|
+
* @param {Object} options
|
|
65
|
+
* @returns {Object}
|
|
66
|
+
*/
|
|
67
|
+
export function createPlaywrightAdapter(options) {
|
|
68
|
+
var cfg = options || {};
|
|
69
|
+
var page = cfg.page;
|
|
70
|
+
var transport = cfg.transport;
|
|
71
|
+
if (!page || Object(page) !== page) throw new Error('playwright-page-required');
|
|
72
|
+
if (!transport || typeof transport.send !== 'function') throw new Error('transport-send-required');
|
|
73
|
+
|
|
74
|
+
var bindingName = typeof cfg.bindingName === 'string' && cfg.bindingName
|
|
75
|
+
? cfg.bindingName
|
|
76
|
+
: DEFAULT_BINDING_NAME;
|
|
77
|
+
var bridgeToken = randomBytes(32).toString('base64url');
|
|
78
|
+
var allowedBridgeTypes = new Set(Object.keys(STREAM).map(function(key) {
|
|
79
|
+
return STREAM[key];
|
|
80
|
+
}));
|
|
81
|
+
var authorizeControl = typeof cfg.authorizeControl === 'function'
|
|
82
|
+
? cfg.authorizeControl
|
|
83
|
+
: function () { return false; };
|
|
84
|
+
var logger = cfg.logger || {
|
|
85
|
+
info: function () {},
|
|
86
|
+
warn: function () {},
|
|
87
|
+
error: function () {}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
var installed = false;
|
|
91
|
+
var disposed = false;
|
|
92
|
+
var cdpSession = cfg.cdpSession || null;
|
|
93
|
+
var controlState = REMOTE_CONTROL_STATE.LOCKED;
|
|
94
|
+
var controlReason = 'initial';
|
|
95
|
+
var counts = {
|
|
96
|
+
requested: 0,
|
|
97
|
+
approved: 0,
|
|
98
|
+
denied: 0,
|
|
99
|
+
stopped: 0,
|
|
100
|
+
dispatches: 0,
|
|
101
|
+
blocked: 0,
|
|
102
|
+
invalid: 0,
|
|
103
|
+
};
|
|
104
|
+
var eventHandlers = new Map();
|
|
105
|
+
var pageListeners = [];
|
|
106
|
+
var unsubscribeTransport = null;
|
|
107
|
+
var installPromise = null;
|
|
108
|
+
|
|
109
|
+
function safeLog(level, message, detail) {
|
|
110
|
+
try {
|
|
111
|
+
if (logger && typeof logger[level] === 'function') logger[level](message, detail);
|
|
112
|
+
} catch (e) { /* logging must not affect adapter behavior */ }
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function emit(eventName, event) {
|
|
116
|
+
var handlers = eventHandlers.get(eventName);
|
|
117
|
+
if (!handlers) return;
|
|
118
|
+
handlers.forEach(function (handler) {
|
|
119
|
+
try {
|
|
120
|
+
handler(cloneEvent(event));
|
|
121
|
+
} catch (err) {
|
|
122
|
+
safeLog('error', '[PlaywrightAdapter] event handler failed', { eventName: eventName });
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function on(eventName, handler) {
|
|
128
|
+
if (typeof eventName !== 'string' || typeof handler !== 'function') {
|
|
129
|
+
throw new Error('adapter-event-unsupported');
|
|
130
|
+
}
|
|
131
|
+
var handlers = eventHandlers.get(eventName);
|
|
132
|
+
if (!handlers) {
|
|
133
|
+
handlers = new Set();
|
|
134
|
+
eventHandlers.set(eventName, handlers);
|
|
135
|
+
}
|
|
136
|
+
handlers.add(handler);
|
|
137
|
+
return function unsubscribeAdapterEvent() {
|
|
138
|
+
handlers.delete(handler);
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function cloneEvent(event) {
|
|
143
|
+
if (!event || Object(event) !== event) return event;
|
|
144
|
+
return JSON.parse(JSON.stringify(event));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function stateEvent(state, reason) {
|
|
148
|
+
return createRemoteControlStateEvent(state, reason, { counts: counts });
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function setControlState(state, reason) {
|
|
152
|
+
controlState = state;
|
|
153
|
+
controlReason = reason || '';
|
|
154
|
+
var event = stateEvent(state, controlReason);
|
|
155
|
+
try {
|
|
156
|
+
transport.send(REMOTE_CONTROL.STATE, event);
|
|
157
|
+
} catch (err) {
|
|
158
|
+
safeLog('error', '[PlaywrightAdapter] control state send failed', { reason: 'state-send-failed' });
|
|
159
|
+
}
|
|
160
|
+
emit('controlstate', event);
|
|
161
|
+
return event;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function getControlState() {
|
|
165
|
+
return stateEvent(controlState, controlReason);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function mainFrame() {
|
|
169
|
+
try {
|
|
170
|
+
return typeof page.mainFrame === 'function' ? page.mainFrame() : null;
|
|
171
|
+
} catch (err) {
|
|
172
|
+
safeLog('warn', '[PlaywrightAdapter] mainFrame lookup failed', { reason: 'main-frame-failed' });
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function isMainFrame(frame) {
|
|
178
|
+
if (!frame) return true;
|
|
179
|
+
var main = mainFrame();
|
|
180
|
+
return !main || frame === main;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function bindingCallback(caller, msg) {
|
|
184
|
+
if (disposed) return { ok: false, error: 'adapter-disposed' };
|
|
185
|
+
if (caller && caller.page && caller.page !== page) {
|
|
186
|
+
return { ok: false, error: 'page-ignored' };
|
|
187
|
+
}
|
|
188
|
+
if (caller && caller.frame && !isMainFrame(caller.frame)) {
|
|
189
|
+
return { ok: false, error: 'frame-ignored' };
|
|
190
|
+
}
|
|
191
|
+
if (!msg || Object(msg) !== msg || typeof msg.type !== 'string') {
|
|
192
|
+
return { ok: false, error: 'bridge-message-invalid' };
|
|
193
|
+
}
|
|
194
|
+
if (msg.token !== bridgeToken) {
|
|
195
|
+
return { ok: false, error: 'bridge-token-invalid' };
|
|
196
|
+
}
|
|
197
|
+
if (!allowedBridgeTypes.has(msg.type)) {
|
|
198
|
+
return { ok: false, error: 'bridge-type-invalid' };
|
|
199
|
+
}
|
|
200
|
+
try {
|
|
201
|
+
transport.send(msg.type, msg.payload || {});
|
|
202
|
+
emit('bridge', { type: msg.type });
|
|
203
|
+
return { ok: true };
|
|
204
|
+
} catch (err) {
|
|
205
|
+
safeLog('error', '[PlaywrightAdapter] bridge forward failed', { reason: 'bridge-forward-failed' });
|
|
206
|
+
return { ok: false, error: 'bridge-forward-failed' };
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function ensureCDPSession() {
|
|
211
|
+
if (cdpSession) return cdpSession;
|
|
212
|
+
if (typeof cfg.cdpSessionFactory !== 'function') return null;
|
|
213
|
+
cdpSession = await cfg.cdpSessionFactory(page);
|
|
214
|
+
return cdpSession || null;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function addPageListener(eventName, handler) {
|
|
218
|
+
if (typeof page.on !== 'function') return;
|
|
219
|
+
page.on(eventName, handler);
|
|
220
|
+
pageListeners.push({ eventName: eventName, handler: handler });
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function removePageListeners() {
|
|
224
|
+
if (typeof page.off !== 'function' && typeof page.removeListener !== 'function') {
|
|
225
|
+
pageListeners = [];
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
pageListeners.forEach(function (entry) {
|
|
229
|
+
try {
|
|
230
|
+
if (typeof page.off === 'function') {
|
|
231
|
+
page.off(entry.eventName, entry.handler);
|
|
232
|
+
} else {
|
|
233
|
+
page.removeListener(entry.eventName, entry.handler);
|
|
234
|
+
}
|
|
235
|
+
} catch (err) { /* best-effort cleanup */ }
|
|
236
|
+
});
|
|
237
|
+
pageListeners = [];
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async function startInjectedCapture() {
|
|
241
|
+
if (disposed) return false;
|
|
242
|
+
try {
|
|
243
|
+
return await page.evaluate(function () {
|
|
244
|
+
if (typeof document === 'undefined' || !document.body) return false;
|
|
245
|
+
return !!(window.__phantomStreamStart && window.__phantomStreamStart());
|
|
246
|
+
});
|
|
247
|
+
} catch (err) {
|
|
248
|
+
safeLog('warn', '[PlaywrightAdapter] injected capture restart failed', { reason: 'capture-restart-failed' });
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function cloneSubtreeRequestPayload(payload) {
|
|
254
|
+
var request = {};
|
|
255
|
+
if (!payload || Object(payload) !== payload) return request;
|
|
256
|
+
['requestId', 'nid', 'streamSessionId', 'snapshotId', 'reason'].forEach(function (key) {
|
|
257
|
+
if (Object.prototype.hasOwnProperty.call(payload, key)) request[key] = payload[key];
|
|
258
|
+
});
|
|
259
|
+
return request;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async function forwardSubtreeRequest(payload) {
|
|
263
|
+
if (disposed) return { ok: false, error: 'adapter-disposed' };
|
|
264
|
+
try {
|
|
265
|
+
await page.evaluate(function (controlFrame) {
|
|
266
|
+
if (typeof window === 'undefined') return false;
|
|
267
|
+
var handle = window.__phantomStreamHandleControl;
|
|
268
|
+
if (typeof handle !== 'function') return false;
|
|
269
|
+
return handle(controlFrame.type, controlFrame.payload || {});
|
|
270
|
+
}, {
|
|
271
|
+
type: CONTROL.SUBTREE_REQUEST,
|
|
272
|
+
payload: cloneSubtreeRequestPayload(payload)
|
|
273
|
+
});
|
|
274
|
+
return { ok: true };
|
|
275
|
+
} catch (err) {
|
|
276
|
+
safeLog('warn', '[PlaywrightAdapter] subtree request forward failed', { reason: 'subtree-request-forward-failed' });
|
|
277
|
+
return { ok: false, error: 'subtree-request-forward-failed' };
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function handleNavigation(frame) {
|
|
282
|
+
if (frame && !isMainFrame(frame)) return;
|
|
283
|
+
startInjectedCapture();
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
async function install() {
|
|
287
|
+
if (installed) return installPromise || Promise.resolve(handle);
|
|
288
|
+
installPromise = (async function runInstall() {
|
|
289
|
+
var injectSource = buildPlaywrightInjectSource({
|
|
290
|
+
bridgeToken: bridgeToken,
|
|
291
|
+
captureOptions: cfg.captureOptions || {}
|
|
292
|
+
});
|
|
293
|
+
if (typeof page.exposeBinding !== 'function') throw new Error('page-expose-binding-required');
|
|
294
|
+
if (typeof page.addInitScript !== 'function') throw new Error('page-add-init-script-required');
|
|
295
|
+
|
|
296
|
+
await page.exposeBinding(bindingName, bindingCallback);
|
|
297
|
+
await page.addInitScript({ content: injectSource });
|
|
298
|
+
|
|
299
|
+
var session = await ensureCDPSession();
|
|
300
|
+
if (session && typeof session.send === 'function') {
|
|
301
|
+
await session.send('Page.addScriptToEvaluateOnNewDocument', { source: injectSource });
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
addPageListener('framenavigated', handleNavigation);
|
|
305
|
+
addPageListener('domcontentloaded', function () { handleNavigation(null); });
|
|
306
|
+
addPageListener('load', function () { handleNavigation(null); });
|
|
307
|
+
|
|
308
|
+
if (transport && typeof transport.onMessage === 'function') {
|
|
309
|
+
unsubscribeTransport = transport.onMessage(function (type, payload) {
|
|
310
|
+
if (type === CONTROL.START) {
|
|
311
|
+
startInjectedCapture();
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
if (type === CONTROL.SUBTREE_REQUEST) {
|
|
315
|
+
forwardSubtreeRequest(payload).catch(function () {
|
|
316
|
+
safeLog('warn', '[PlaywrightAdapter] subtree request forward failed', { reason: 'subtree-request-forward-failed' });
|
|
317
|
+
});
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
if (isRemoteControlType(type)) {
|
|
321
|
+
handleControlMessage(type, payload).catch(function () {
|
|
322
|
+
safeLog('error', '[PlaywrightAdapter] control message failed', { reason: 'control-message-failed' });
|
|
323
|
+
setControlState(controlState, 'control-dispatch-failed');
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
installed = true;
|
|
330
|
+
emit('install', { bindingName: bindingName });
|
|
331
|
+
return handle;
|
|
332
|
+
})();
|
|
333
|
+
return installPromise;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function dispose() {
|
|
337
|
+
disposed = true;
|
|
338
|
+
removePageListeners();
|
|
339
|
+
if (typeof unsubscribeTransport === 'function') {
|
|
340
|
+
try {
|
|
341
|
+
unsubscribeTransport();
|
|
342
|
+
} catch (err) { /* best-effort cleanup */ }
|
|
343
|
+
unsubscribeTransport = null;
|
|
344
|
+
}
|
|
345
|
+
eventHandlers.clear();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
async function requestControl(payload) {
|
|
349
|
+
var p = payload && Object(payload) === payload ? payload : {};
|
|
350
|
+
counts.requested += 1;
|
|
351
|
+
setControlState(REMOTE_CONTROL_STATE.REQUESTING, 'control-requested');
|
|
352
|
+
|
|
353
|
+
var approved = false;
|
|
354
|
+
try {
|
|
355
|
+
approved = await authorizeControl({
|
|
356
|
+
requestId: typeof p.requestId === 'string' ? p.requestId : '',
|
|
357
|
+
source: 'viewer',
|
|
358
|
+
authorizationMode: typeof p.authorizationMode === 'string' ? p.authorizationMode : 'host',
|
|
359
|
+
});
|
|
360
|
+
} catch (err) {
|
|
361
|
+
approved = false;
|
|
362
|
+
safeLog('warn', '[PlaywrightAdapter] control authorization failed', { reason: 'authorization-failed' });
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (!approved) {
|
|
366
|
+
counts.denied += 1;
|
|
367
|
+
setControlState(REMOTE_CONTROL_STATE.DENIED, 'authorization-denied');
|
|
368
|
+
return false;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
counts.approved += 1;
|
|
372
|
+
setControlState(REMOTE_CONTROL_STATE.ACTIVE, 'authorization-approved');
|
|
373
|
+
return true;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function stopControl(reason) {
|
|
377
|
+
counts.stopped += 1;
|
|
378
|
+
setControlState(REMOTE_CONTROL_STATE.STOPPED, sanitizeReason(reason) || 'control-stopped');
|
|
379
|
+
return true;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
async function handleControlMessage(type, payload) {
|
|
383
|
+
var result = validateRemoteControlMessage(type, payload || {});
|
|
384
|
+
if (!result.ok) {
|
|
385
|
+
counts.invalid += 1;
|
|
386
|
+
setControlState(controlState, result.error);
|
|
387
|
+
return { ok: false, error: result.error };
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
var action = result.action;
|
|
391
|
+
if (action.kind === 'request') {
|
|
392
|
+
return { ok: await requestControl(payload || {}) };
|
|
393
|
+
}
|
|
394
|
+
if (action.kind === 'stop') {
|
|
395
|
+
stopControl(payload && payload.reason);
|
|
396
|
+
return { ok: true };
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
return dispatchRemoteAction(action);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
async function dispatchRemoteAction(action) {
|
|
403
|
+
if (controlState !== REMOTE_CONTROL_STATE.ACTIVE) {
|
|
404
|
+
counts.blocked += 1;
|
|
405
|
+
setControlState(controlState === REMOTE_CONTROL_STATE.DENIED
|
|
406
|
+
? REMOTE_CONTROL_STATE.DENIED
|
|
407
|
+
: controlState, 'control-inactive');
|
|
408
|
+
return { ok: false, error: 'control-inactive' };
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
var result = validateRemoteControlMessage(action.type, action);
|
|
412
|
+
if (!result.ok) {
|
|
413
|
+
counts.invalid += 1;
|
|
414
|
+
setControlState(controlState, result.error);
|
|
415
|
+
return { ok: false, error: result.error };
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
var normalized = result.action;
|
|
419
|
+
if (normalized.kind === 'request' || normalized.kind === 'stop') {
|
|
420
|
+
return handleControlMessage(normalized.type, normalized);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
if (controlState !== REMOTE_CONTROL_STATE.ACTIVE) {
|
|
424
|
+
counts.blocked += 1;
|
|
425
|
+
setControlState(controlState, 'control-inactive');
|
|
426
|
+
return { ok: false, error: 'control-inactive' };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
var session = await ensureCDPSession();
|
|
430
|
+
if (session && typeof session.send === 'function') {
|
|
431
|
+
await dispatchWithCDP(session, normalized);
|
|
432
|
+
} else {
|
|
433
|
+
await dispatchWithPlaywright(normalized);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
counts.dispatches += 1;
|
|
437
|
+
emit('controlaction', { kind: normalized.kind });
|
|
438
|
+
return { ok: true };
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
async function dispatchWithPlaywright(action) {
|
|
442
|
+
if (action.kind === 'click') {
|
|
443
|
+
await page.mouse.click(action.x, action.y, {
|
|
444
|
+
button: action.button,
|
|
445
|
+
clickCount: action.clickCount,
|
|
446
|
+
});
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (action.kind === 'text') {
|
|
450
|
+
await page.keyboard.insertText(action.text);
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
if (action.kind === 'key') {
|
|
454
|
+
if (action.event === 'down') {
|
|
455
|
+
await page.keyboard.down(action.key);
|
|
456
|
+
} else {
|
|
457
|
+
await page.keyboard.up(action.key);
|
|
458
|
+
}
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
if (action.kind === 'scroll') {
|
|
462
|
+
if (page.mouse && typeof page.mouse.move === 'function') {
|
|
463
|
+
await page.mouse.move(action.x, action.y);
|
|
464
|
+
}
|
|
465
|
+
await page.mouse.wheel(action.deltaX, action.deltaY);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
async function dispatchWithCDP(session, action) {
|
|
470
|
+
if (action.kind === 'click') {
|
|
471
|
+
await session.send('Input.dispatchMouseEvent', {
|
|
472
|
+
type: 'mouseMoved',
|
|
473
|
+
x: action.x,
|
|
474
|
+
y: action.y,
|
|
475
|
+
button: 'none',
|
|
476
|
+
buttons: 0,
|
|
477
|
+
});
|
|
478
|
+
await session.send('Input.dispatchMouseEvent', {
|
|
479
|
+
type: 'mousePressed',
|
|
480
|
+
x: action.x,
|
|
481
|
+
y: action.y,
|
|
482
|
+
button: action.button,
|
|
483
|
+
buttons: buttonMask(action.button),
|
|
484
|
+
clickCount: action.clickCount,
|
|
485
|
+
});
|
|
486
|
+
await session.send('Input.dispatchMouseEvent', {
|
|
487
|
+
type: 'mouseReleased',
|
|
488
|
+
x: action.x,
|
|
489
|
+
y: action.y,
|
|
490
|
+
button: action.button,
|
|
491
|
+
buttons: 0,
|
|
492
|
+
clickCount: action.clickCount,
|
|
493
|
+
});
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
if (action.kind === 'text') {
|
|
497
|
+
await session.send('Input.insertText', { text: action.text });
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
if (action.kind === 'key') {
|
|
501
|
+
await session.send('Input.dispatchKeyEvent', {
|
|
502
|
+
type: action.event === 'down' ? 'keyDown' : 'keyUp',
|
|
503
|
+
key: action.key,
|
|
504
|
+
});
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
if (action.kind === 'scroll') {
|
|
508
|
+
await session.send('Input.dispatchMouseEvent', {
|
|
509
|
+
type: 'mouseWheel',
|
|
510
|
+
x: action.x,
|
|
511
|
+
y: action.y,
|
|
512
|
+
deltaX: action.deltaX,
|
|
513
|
+
deltaY: action.deltaY,
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
function buttonMask(button) {
|
|
519
|
+
if (button === 'right') return 2;
|
|
520
|
+
if (button === 'middle') return 4;
|
|
521
|
+
return 1;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function sanitizeReason(reason) {
|
|
525
|
+
return typeof reason === 'string' && /^[a-z0-9-]+$/.test(reason) ? reason : '';
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
var handle = {
|
|
529
|
+
install: install,
|
|
530
|
+
dispose: dispose,
|
|
531
|
+
requestControl: requestControl,
|
|
532
|
+
stopControl: stopControl,
|
|
533
|
+
handleControlMessage: handleControlMessage,
|
|
534
|
+
dispatchRemoteAction: dispatchRemoteAction,
|
|
535
|
+
on: on,
|
|
536
|
+
getControlState: getControlState,
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
return handle;
|
|
540
|
+
}
|