@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,414 @@
|
|
|
1
|
+
import { createViewer, mapHostPointToViewport } from '../../src/renderer/index.js';
|
|
2
|
+
import { createWebSocketTransport } from '../../src/transport/websocket.js';
|
|
3
|
+
import { CONTROL, STREAM, REMOTE_CONTROL, REMOTE_CONTROL_STATE } from '../../src/protocol/index.js';
|
|
4
|
+
|
|
5
|
+
var params = new URLSearchParams(window.location.search);
|
|
6
|
+
var room = params.get('room') || '';
|
|
7
|
+
var roomPrefix = room ? room.slice(0, 8) : 'unknown';
|
|
8
|
+
var wsUrl = buildViewerWsUrl(params);
|
|
9
|
+
|
|
10
|
+
var lifecycleBadge = document.getElementById('lifecycle-badge');
|
|
11
|
+
var lifecycleLabel = document.getElementById('lifecycle-label');
|
|
12
|
+
var viewerLifecycle = document.getElementById('viewer-lifecycle');
|
|
13
|
+
var viewerRoom = document.getElementById('viewer-room');
|
|
14
|
+
var viewerRelay = document.getElementById('viewer-relay');
|
|
15
|
+
var adapterState = document.getElementById('adapter-state');
|
|
16
|
+
var viewerLastFrame = document.getElementById('viewer-last-frame');
|
|
17
|
+
var navigationCount = document.getElementById('navigation-count');
|
|
18
|
+
var stage = document.getElementById('mirror-stage');
|
|
19
|
+
var overlay = document.getElementById('control-overlay');
|
|
20
|
+
var requestButton = document.getElementById('request-control');
|
|
21
|
+
var stopButton = document.getElementById('stop-control');
|
|
22
|
+
var controlBadge = document.getElementById('control-badge');
|
|
23
|
+
var controlLabel = document.getElementById('control-label');
|
|
24
|
+
var controlLastAction = document.getElementById('control-last-action');
|
|
25
|
+
var denialMessage = document.getElementById('denial-message');
|
|
26
|
+
var actionLog = document.getElementById('action-log');
|
|
27
|
+
|
|
28
|
+
var healthFrames = document.getElementById('health-frames');
|
|
29
|
+
var healthSnapshots = document.getElementById('health-snapshots');
|
|
30
|
+
var healthMutations = document.getElementById('health-mutations');
|
|
31
|
+
var healthMisses = document.getElementById('health-misses');
|
|
32
|
+
var healthApplyFailures = document.getElementById('health-apply-failures');
|
|
33
|
+
var healthDrops = document.getElementById('health-drops');
|
|
34
|
+
var healthErrors = document.getElementById('health-errors');
|
|
35
|
+
var healthControlRequests = document.getElementById('health-control-requests');
|
|
36
|
+
var healthDenied = document.getElementById('health-denied');
|
|
37
|
+
var healthDispatched = document.getElementById('health-dispatched');
|
|
38
|
+
var healthNavs = document.getElementById('health-navs');
|
|
39
|
+
|
|
40
|
+
var lifecycleState = 'connecting';
|
|
41
|
+
var controlState = REMOTE_CONTROL_STATE.LOCKED;
|
|
42
|
+
var requestSequence = 0;
|
|
43
|
+
var controlCounters = {
|
|
44
|
+
requests: 0,
|
|
45
|
+
denied: 0,
|
|
46
|
+
dispatched: 0
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
viewerRoom.textContent = roomPrefix;
|
|
50
|
+
|
|
51
|
+
var logger = {
|
|
52
|
+
info: function () {},
|
|
53
|
+
warn: function () {},
|
|
54
|
+
error: function () {
|
|
55
|
+
logLine('Mirror could not render — open the browser console for details.');
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
var transport = createWebSocketTransport({
|
|
60
|
+
url: wsUrl,
|
|
61
|
+
role: 'viewer',
|
|
62
|
+
logger: logger
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
var viewer = createViewer({
|
|
66
|
+
container: stage,
|
|
67
|
+
transport: transport,
|
|
68
|
+
disconnectDelayMs: 4000,
|
|
69
|
+
logger: logger
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
function buildViewerWsUrl(query) {
|
|
73
|
+
var raw = query.get('ws') || '';
|
|
74
|
+
var url;
|
|
75
|
+
if (raw) {
|
|
76
|
+
url = new URL(raw, window.location.href);
|
|
77
|
+
} else {
|
|
78
|
+
url = new URL('/ws', window.location.href);
|
|
79
|
+
url.protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
80
|
+
}
|
|
81
|
+
if (room) url.searchParams.set('room', room);
|
|
82
|
+
url.searchParams.set('role', 'viewer');
|
|
83
|
+
return url.toString();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function logLine(text) {
|
|
87
|
+
var line = document.createElement('div');
|
|
88
|
+
line.textContent = new Date().toLocaleTimeString() + ' ' + text;
|
|
89
|
+
actionLog.appendChild(line);
|
|
90
|
+
while (actionLog.children.length > 8) {
|
|
91
|
+
actionLog.removeChild(actionLog.firstElementChild);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function labelForLifecycle(state) {
|
|
96
|
+
if (state === 'live') return 'Live';
|
|
97
|
+
if (state === 'stale') return 'Stale';
|
|
98
|
+
if (state === 'disconnected') return 'Disconnected';
|
|
99
|
+
return 'Connecting';
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function setLifecycle(state) {
|
|
103
|
+
lifecycleState = normalizeLifecycle(state);
|
|
104
|
+
var label = labelForLifecycle(lifecycleState);
|
|
105
|
+
lifecycleBadge.classList.remove('state-connecting', 'state-live', 'state-stale', 'state-disconnected');
|
|
106
|
+
lifecycleBadge.classList.add('state-' + lifecycleState);
|
|
107
|
+
lifecycleLabel.textContent = label;
|
|
108
|
+
viewerLifecycle.textContent = label;
|
|
109
|
+
if (lifecycleState === 'disconnected') {
|
|
110
|
+
logLine('Relay disconnected — restart the Playwright demo and refresh this page.');
|
|
111
|
+
}
|
|
112
|
+
updateControlButtons();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function normalizeLifecycle(state) {
|
|
116
|
+
if (state === 'live' || state === 'stale' || state === 'disconnected') return state;
|
|
117
|
+
return 'connecting';
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function normalizeControlState(state) {
|
|
121
|
+
if (state === REMOTE_CONTROL_STATE.REQUESTING ||
|
|
122
|
+
state === REMOTE_CONTROL_STATE.ACTIVE ||
|
|
123
|
+
state === REMOTE_CONTROL_STATE.DENIED ||
|
|
124
|
+
state === REMOTE_CONTROL_STATE.STOPPED) {
|
|
125
|
+
return state;
|
|
126
|
+
}
|
|
127
|
+
return REMOTE_CONTROL_STATE.LOCKED;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function setControlState(state, source) {
|
|
131
|
+
controlState = normalizeControlState(state);
|
|
132
|
+
adapterState.textContent = controlState;
|
|
133
|
+
controlBadge.classList.remove(
|
|
134
|
+
'state-locked',
|
|
135
|
+
'state-requesting',
|
|
136
|
+
'state-active',
|
|
137
|
+
'state-denied',
|
|
138
|
+
'state-stopped'
|
|
139
|
+
);
|
|
140
|
+
controlBadge.classList.add('state-' + controlState);
|
|
141
|
+
controlLabel.textContent = controlState;
|
|
142
|
+
stage.classList.toggle('control-active', controlState === REMOTE_CONTROL_STATE.ACTIVE);
|
|
143
|
+
overlay.style.pointerEvents = controlState === REMOTE_CONTROL_STATE.ACTIVE ? 'auto' : 'none';
|
|
144
|
+
denialMessage.hidden = controlState !== REMOTE_CONTROL_STATE.DENIED;
|
|
145
|
+
|
|
146
|
+
if (controlState === REMOTE_CONTROL_STATE.ACTIVE) {
|
|
147
|
+
controlLastAction.textContent = 'Control active';
|
|
148
|
+
stage.focus({ preventScroll: true });
|
|
149
|
+
} else if (controlState === REMOTE_CONTROL_STATE.DENIED) {
|
|
150
|
+
controlLastAction.textContent = 'Action blocked: control locked';
|
|
151
|
+
if (source !== 'local') logLine('Remote control denied');
|
|
152
|
+
} else if (controlState === REMOTE_CONTROL_STATE.STOPPED) {
|
|
153
|
+
controlLastAction.textContent = 'Action blocked: control locked';
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
updateControlButtons();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function updateControlButtons() {
|
|
160
|
+
var disabled = lifecycleState !== 'live' || controlState === REMOTE_CONTROL_STATE.REQUESTING;
|
|
161
|
+
requestButton.disabled = disabled;
|
|
162
|
+
requestButton.setAttribute('aria-disabled', String(disabled));
|
|
163
|
+
requestButton.textContent = controlState === REMOTE_CONTROL_STATE.REQUESTING
|
|
164
|
+
? 'Requesting control'
|
|
165
|
+
: 'Request control';
|
|
166
|
+
stopButton.hidden = controlState !== REMOTE_CONTROL_STATE.ACTIVE;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function formatTime(ts) {
|
|
170
|
+
return ts ? new Date(ts).toLocaleTimeString() : 'never';
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function count(map, type) {
|
|
174
|
+
return map && map[type] ? map[type] : 0;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function sumCounts(map) {
|
|
178
|
+
var total = 0;
|
|
179
|
+
map = map || {};
|
|
180
|
+
Object.keys(map).forEach(function (key) {
|
|
181
|
+
total += map[key] || 0;
|
|
182
|
+
});
|
|
183
|
+
return total;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function syncControlCounts(counts) {
|
|
187
|
+
if (!counts || Object(counts) !== counts) return;
|
|
188
|
+
if (Number.isFinite(counts.requested)) controlCounters.requests = counts.requested;
|
|
189
|
+
if (Number.isFinite(counts.denied)) controlCounters.denied = counts.denied;
|
|
190
|
+
if (Number.isFinite(counts.dispatches)) controlCounters.dispatched = counts.dispatches;
|
|
191
|
+
renderControlCounts();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function renderControlCounts() {
|
|
195
|
+
healthControlRequests.textContent = String(controlCounters.requests);
|
|
196
|
+
healthDenied.textContent = String(controlCounters.denied);
|
|
197
|
+
healthDispatched.textContent = String(controlCounters.dispatched);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function getAuthorizationMode() {
|
|
201
|
+
var selected = document.querySelector('input[name="authorization-mode"]:checked');
|
|
202
|
+
return selected && selected.value === 'approve' ? 'approve' : 'deny';
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function sendControlFrame(type, payload) {
|
|
206
|
+
transport.send(type, payload || {});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function requestControl() {
|
|
210
|
+
if (lifecycleState !== 'live' || controlState === REMOTE_CONTROL_STATE.REQUESTING) {
|
|
211
|
+
blockAction('Action blocked: control locked');
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
requestSequence += 1;
|
|
215
|
+
controlCounters.requests += 1;
|
|
216
|
+
renderControlCounts();
|
|
217
|
+
setControlState(REMOTE_CONTROL_STATE.REQUESTING, 'local');
|
|
218
|
+
sendControlFrame(REMOTE_CONTROL.REQUEST, {
|
|
219
|
+
requestId: 'viewer-' + Date.now().toString(36) + '-' + requestSequence,
|
|
220
|
+
authorizationMode: getAuthorizationMode()
|
|
221
|
+
});
|
|
222
|
+
logLine('Control requested');
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function stopControl(reason) {
|
|
226
|
+
sendControlFrame(REMOTE_CONTROL.STOP, { reason: reason || 'viewer-stop' });
|
|
227
|
+
setControlState(REMOTE_CONTROL_STATE.STOPPED, 'local');
|
|
228
|
+
logLine('Control stopped');
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function blockAction(message) {
|
|
232
|
+
controlLastAction.textContent = message;
|
|
233
|
+
logLine(message);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function mapEventToViewport(event) {
|
|
237
|
+
var rect = stage.getBoundingClientRect();
|
|
238
|
+
var mapping = viewer.getViewportMapping();
|
|
239
|
+
return mapHostPointToViewport({
|
|
240
|
+
x: event.clientX - rect.left,
|
|
241
|
+
y: event.clientY - rect.top
|
|
242
|
+
}, mapping.scale);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function sendMappedAction(event, type, payloadForPoint, feedback) {
|
|
246
|
+
if (controlState !== REMOTE_CONTROL_STATE.ACTIVE) {
|
|
247
|
+
blockAction('Action blocked: control locked');
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
var mapped = mapEventToViewport(event);
|
|
251
|
+
if (!mapped.inside) {
|
|
252
|
+
blockAction('Action blocked: outside mirror');
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
var payload = payloadForPoint(mapped);
|
|
256
|
+
sendControlFrame(type, payload);
|
|
257
|
+
controlCounters.dispatched += 1;
|
|
258
|
+
renderControlCounts();
|
|
259
|
+
feedback(mapped, event);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function showClickFeedback(event) {
|
|
263
|
+
var rect = stage.getBoundingClientRect();
|
|
264
|
+
var ring = document.createElement('div');
|
|
265
|
+
ring.className = 'feedback-ring';
|
|
266
|
+
ring.style.left = Math.round(event.clientX - rect.left) + 'px';
|
|
267
|
+
ring.style.top = Math.round(event.clientY - rect.top) + 'px';
|
|
268
|
+
stage.appendChild(ring);
|
|
269
|
+
setTimeout(function () {
|
|
270
|
+
if (ring.parentNode) ring.parentNode.removeChild(ring);
|
|
271
|
+
}, 450);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function showTypeFeedback() {
|
|
275
|
+
var chip = document.createElement('div');
|
|
276
|
+
chip.className = 'feedback-chip';
|
|
277
|
+
chip.textContent = 'Type sent';
|
|
278
|
+
stage.appendChild(chip);
|
|
279
|
+
setTimeout(function () {
|
|
280
|
+
if (chip.parentNode) chip.parentNode.removeChild(chip);
|
|
281
|
+
}, 900);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function showScrollFeedback(deltaY) {
|
|
285
|
+
var tick = document.createElement('div');
|
|
286
|
+
tick.className = deltaY < 0 ? 'feedback-scroll is-up' : 'feedback-scroll';
|
|
287
|
+
stage.appendChild(tick);
|
|
288
|
+
setTimeout(function () {
|
|
289
|
+
if (tick.parentNode) tick.parentNode.removeChild(tick);
|
|
290
|
+
}, 450);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function isPrintableKey(event) {
|
|
294
|
+
return event.key && event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
overlay.addEventListener('click', function (event) {
|
|
298
|
+
event.preventDefault();
|
|
299
|
+
sendMappedAction(event, REMOTE_CONTROL.CLICK, function (point) {
|
|
300
|
+
return { x: point.x, y: point.y, button: 'left' };
|
|
301
|
+
}, function (point, clickEvent) {
|
|
302
|
+
controlLastAction.textContent = 'Click sent: x ' + point.x + ', y ' + point.y;
|
|
303
|
+
logLine('Click sent: x ' + point.x + ', y ' + point.y);
|
|
304
|
+
showClickFeedback(clickEvent);
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
overlay.addEventListener('wheel', function (event) {
|
|
309
|
+
event.preventDefault();
|
|
310
|
+
sendMappedAction(event, REMOTE_CONTROL.SCROLL, function (point) {
|
|
311
|
+
return {
|
|
312
|
+
x: point.x,
|
|
313
|
+
y: point.y,
|
|
314
|
+
deltaX: Math.round(event.deltaX),
|
|
315
|
+
deltaY: Math.round(event.deltaY)
|
|
316
|
+
};
|
|
317
|
+
}, function (point) {
|
|
318
|
+
var dx = Math.round(event.deltaX);
|
|
319
|
+
var dy = Math.round(event.deltaY);
|
|
320
|
+
controlLastAction.textContent = 'Scroll sent: dx ' + dx + ', dy ' + dy;
|
|
321
|
+
logLine('Scroll sent: dx ' + dx + ', dy ' + dy);
|
|
322
|
+
showScrollFeedback(dy);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
stage.addEventListener('keydown', function (event) {
|
|
327
|
+
if (controlState !== REMOTE_CONTROL_STATE.ACTIVE) return;
|
|
328
|
+
if (event.key === 'Escape') {
|
|
329
|
+
event.preventDefault();
|
|
330
|
+
stopControl('viewer-escape');
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
if (isPrintableKey(event)) {
|
|
334
|
+
event.preventDefault();
|
|
335
|
+
var text = event.key;
|
|
336
|
+
sendControlFrame(REMOTE_CONTROL.TEXT, { text: text });
|
|
337
|
+
controlCounters.dispatched += 1;
|
|
338
|
+
renderControlCounts();
|
|
339
|
+
controlLastAction.textContent = 'Type sent: ' + text.length + ' chars';
|
|
340
|
+
logLine('Type sent: ' + text.length + ' chars');
|
|
341
|
+
showTypeFeedback();
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
event.preventDefault();
|
|
345
|
+
sendControlFrame(REMOTE_CONTROL.KEY, { key: event.key, event: 'down' });
|
|
346
|
+
controlCounters.dispatched += 1;
|
|
347
|
+
renderControlCounts();
|
|
348
|
+
controlLastAction.textContent = 'Key sent: ' + event.key;
|
|
349
|
+
logLine('Key sent: ' + event.key);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
stage.addEventListener('keyup', function (event) {
|
|
353
|
+
if (controlState !== REMOTE_CONTROL_STATE.ACTIVE || isPrintableKey(event)) return;
|
|
354
|
+
if (event.key === 'Escape') return;
|
|
355
|
+
event.preventDefault();
|
|
356
|
+
sendControlFrame(REMOTE_CONTROL.KEY, { key: event.key, event: 'up' });
|
|
357
|
+
controlCounters.dispatched += 1;
|
|
358
|
+
renderControlCounts();
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
requestButton.addEventListener('click', requestControl);
|
|
362
|
+
stopButton.addEventListener('click', function () {
|
|
363
|
+
stopControl('viewer-stop');
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
viewer.on('state', function (event) {
|
|
367
|
+
setLifecycle(event.state);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
viewer.on('health', function (health) {
|
|
371
|
+
var received = health.receivedByType || {};
|
|
372
|
+
var transportHealth = health.transport || {};
|
|
373
|
+
var snapshots = count(received, STREAM.SNAPSHOT);
|
|
374
|
+
var mutations = count(received, STREAM.MUTATIONS);
|
|
375
|
+
var navs = Math.max(0, snapshots - 1);
|
|
376
|
+
|
|
377
|
+
if (health.lastFrameAt) stage.classList.add('has-frame');
|
|
378
|
+
viewerLastFrame.textContent = formatTime(health.lastFrameAt);
|
|
379
|
+
healthFrames.textContent = String(sumCounts(received));
|
|
380
|
+
healthSnapshots.textContent = String(snapshots);
|
|
381
|
+
healthMutations.textContent = String(mutations);
|
|
382
|
+
healthMisses.textContent = String(health.staleMisses || 0);
|
|
383
|
+
healthApplyFailures.textContent = String(health.applyFailures || 0);
|
|
384
|
+
healthDrops.textContent = String(transportHealth.drops || 0);
|
|
385
|
+
healthErrors.textContent = String((transportHealth.errors || []).length);
|
|
386
|
+
navigationCount.textContent = String(navs);
|
|
387
|
+
healthNavs.textContent = String(navs);
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
transport.onStatus(function (status) {
|
|
391
|
+
viewerRelay.textContent = status.state || 'unknown';
|
|
392
|
+
if (status.state === 'open') {
|
|
393
|
+
sendControlFrame(CONTROL.START, {
|
|
394
|
+
trigger: 'viewer-open',
|
|
395
|
+
roomPrefix: roomPrefix
|
|
396
|
+
});
|
|
397
|
+
logLine('control: ' + CONTROL.START);
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
transport.onMessage(function (type, payload) {
|
|
402
|
+
if (type !== REMOTE_CONTROL.STATE) return;
|
|
403
|
+
var p = payload || {};
|
|
404
|
+
setControlState(p.state, 'adapter');
|
|
405
|
+
syncControlCounts(p.counts);
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
setControlState(REMOTE_CONTROL_STATE.LOCKED, 'local');
|
|
409
|
+
renderControlCounts();
|
|
410
|
+
|
|
411
|
+
window.__phantomstreamPlaywrightViewer = {
|
|
412
|
+
viewer: viewer,
|
|
413
|
+
transport: transport
|
|
414
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// examples/serve.js -- dependency-free static server for the loopback demo.
|
|
2
|
+
//
|
|
3
|
+
// Design source: 02-RESEARCH.md Pattern 7, plus the plan 02-05 security
|
|
4
|
+
// deltas. Serves the REPO ROOT (not just examples/) so the demo page's
|
|
5
|
+
// native ESM imports (../src/renderer/index.js, ../src/capture/index.js)
|
|
6
|
+
// resolve over http. Module scripts are subject to strict MIME checking,
|
|
7
|
+
// so .js/.mjs MUST be served as text/javascript (Pitfall 6).
|
|
8
|
+
//
|
|
9
|
+
// Security posture (dev demo; threat register T-02-16 / T-02-17):
|
|
10
|
+
// - binds 127.0.0.1 only -- never reachable from the network
|
|
11
|
+
// - the raw request path is decoded (decodeURIComponent) BEFORE resolving,
|
|
12
|
+
// so percent-encoded dot-dot cannot bypass the ROOT-prefix guard;
|
|
13
|
+
// malformed encodings get 400
|
|
14
|
+
// - the raw path is used as-is, deliberately NOT parsed through WHATWG
|
|
15
|
+
// `new URL(...)`: URL parsing normalizes "/../x" to "/x", which would
|
|
16
|
+
// silently rewrite above-root traversal probes into in-root 200s
|
|
17
|
+
// instead of rejecting them
|
|
18
|
+
// - the resolved path must stay under ROOT (403 otherwise); directory
|
|
19
|
+
// requests resolve only to an explicit index.html (404 otherwise --
|
|
20
|
+
// no directory listings)
|
|
21
|
+
|
|
22
|
+
import { createServer } from 'node:http';
|
|
23
|
+
import { createReadStream } from 'node:fs';
|
|
24
|
+
import { stat } from 'node:fs/promises';
|
|
25
|
+
import { extname, resolve, sep } from 'node:path';
|
|
26
|
+
import { fileURLToPath } from 'node:url';
|
|
27
|
+
|
|
28
|
+
// Repo root: one level up from examples/. fileURLToPath (not URL.pathname)
|
|
29
|
+
// so percent-encoded filesystem paths decode correctly on every platform.
|
|
30
|
+
const ROOT = resolve(fileURLToPath(new URL('..', import.meta.url)));
|
|
31
|
+
const HOST = '127.0.0.1'; // localhost only (T-02-17)
|
|
32
|
+
const PORT = 8642;
|
|
33
|
+
|
|
34
|
+
const MIME = {
|
|
35
|
+
'.html': 'text/html; charset=utf-8',
|
|
36
|
+
'.js': 'text/javascript; charset=utf-8', // required for ESM module scripts
|
|
37
|
+
'.mjs': 'text/javascript; charset=utf-8',
|
|
38
|
+
'.css': 'text/css; charset=utf-8',
|
|
39
|
+
'.json': 'application/json',
|
|
40
|
+
'.svg': 'image/svg+xml',
|
|
41
|
+
'.png': 'image/png',
|
|
42
|
+
'.ico': 'image/x-icon',
|
|
43
|
+
'.map': 'application/json'
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const server = createServer(async (req, res) => {
|
|
47
|
+
// Strip the query string, then decode BEFORE resolving (T-02-16): a
|
|
48
|
+
// malformed encoding is a client error, and decoding after the prefix
|
|
49
|
+
// check would let %2e%2e sneak past it.
|
|
50
|
+
let pathname;
|
|
51
|
+
try {
|
|
52
|
+
pathname = decodeURIComponent(req.url.split('?')[0]);
|
|
53
|
+
} catch {
|
|
54
|
+
res.writeHead(400, { 'content-type': 'text/plain; charset=utf-8' });
|
|
55
|
+
res.end('bad request');
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let filePath = resolve(ROOT, '.' + pathname);
|
|
60
|
+
if (filePath !== ROOT && !filePath.startsWith(ROOT + sep)) {
|
|
61
|
+
res.writeHead(403, { 'content-type': 'text/plain; charset=utf-8' });
|
|
62
|
+
res.end('forbidden');
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
let stats = await stat(filePath);
|
|
68
|
+
if (stats.isDirectory()) {
|
|
69
|
+
// Directories resolve only to an explicit index.html -- no listings.
|
|
70
|
+
filePath = resolve(filePath, 'index.html');
|
|
71
|
+
stats = await stat(filePath);
|
|
72
|
+
}
|
|
73
|
+
if (!stats.isFile()) {
|
|
74
|
+
// FIFOs/sockets/devices in the tree would hang the response forever.
|
|
75
|
+
res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });
|
|
76
|
+
res.end('not found');
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
res.writeHead(200, {
|
|
80
|
+
'content-type': MIME[extname(filePath)] || 'application/octet-stream'
|
|
81
|
+
});
|
|
82
|
+
const stream = createReadStream(filePath);
|
|
83
|
+
// pipe() does NOT forward source errors: without this handler one
|
|
84
|
+
// unreadable file (EACCES, deleted between stat and open) is an
|
|
85
|
+
// unhandled 'error' event that kills the whole process (review WR-01).
|
|
86
|
+
// Headers are already sent, so abort the socket instead of a 500 body.
|
|
87
|
+
stream.on('error', () => res.destroy());
|
|
88
|
+
stream.pipe(res);
|
|
89
|
+
} catch {
|
|
90
|
+
res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });
|
|
91
|
+
res.end('not found');
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
server.listen(PORT, HOST, () => {
|
|
96
|
+
console.log(`loopback demo: http://localhost:${PORT}/examples/loopback-mirror.html`);
|
|
97
|
+
});
|