townmouse 0.2.1 → 0.4.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/CHANGELOG.md +72 -2
- package/DESIGN.md +26 -42
- package/README.md +286 -54
- package/SECURITY.md +3 -0
- package/TOWNMOUSE.md +1 -1
- package/bin/townmouse +2 -2
- package/docs/PACKAGING.md +104 -8
- package/docs/browser-workspace.md +62 -9
- package/docs/panes.md +199 -9
- package/native/TownmouseApp.swift +5 -5
- package/npm-shrinkwrap.json +1282 -0
- package/package.json +11 -7
- package/public/browser-picture.mjs +159 -0
- package/public/features/app-version.mjs +35 -0
- package/public/features/autosave.mjs +5 -1
- package/public/features/multi-window-sync.mjs +3 -1
- package/public/features/osc-bridge.mjs +8 -3
- package/public/features/session-manager.mjs +202 -100
- package/public/features/status-metrics.mjs +23 -0
- package/public/features/tab-reorder.mjs +8 -0
- package/public/features/window-title.mjs +10 -3
- package/public/index.html +617 -78
- package/public/input-guard.mjs +10 -2
- package/public/panes.css +189 -13
- package/public/panes.html +4 -1
- package/public/panes.mjs +667 -109
- package/public/workspace.css +198 -24
- package/public/workspace.mjs +346 -106
- package/scripts/install-service.sh +97 -63
- package/scripts/panes +134 -5
- package/scripts/retire-legacy-service.sh +76 -0
- package/scripts/{t1.sh → townmouse.sh} +60 -21
- package/scripts/uninstall-service.sh +31 -15
- package/src/agent-guide.js +72 -3
- package/src/attachments.js +2 -2
- package/src/browser-dom.js +5 -2
- package/src/browser-workspace.js +549 -62
- package/src/owner-page.js +43 -0
- package/src/request-boundary.js +1 -1
- package/src/server.js +588 -128
- package/src/workspace-panes.js +423 -91
package/src/browser-workspace.js
CHANGED
|
@@ -3,6 +3,7 @@ const crypto = require('crypto');
|
|
|
3
3
|
const puppeteer = require('puppeteer');
|
|
4
4
|
const dom = require('./browser-dom');
|
|
5
5
|
const { resolveChrome, missingBrowserMessage } = require('./chrome-resolve');
|
|
6
|
+
const { ownerPageRefusal } = require('./owner-page');
|
|
6
7
|
|
|
7
8
|
// How many past observations a session keeps. Enough for an agent to compare a
|
|
8
9
|
// few steps back; bounded so a long session cannot grow without limit.
|
|
@@ -11,6 +12,66 @@ const MAX_OBSERVATIONS = 20;
|
|
|
11
12
|
const MAX_REF_RECORDS = 4000;
|
|
12
13
|
const MAX_ACTION_TEXT = 10000;
|
|
13
14
|
|
|
15
|
+
// ── Pictures for someone watching a page ────────────────────────────────────
|
|
16
|
+
//
|
|
17
|
+
// The numbers come from measuring this Chrome on a desktop-class machine: one
|
|
18
|
+
// 1280x800 picture of a page with real content costs about 0.6 ms of Chrome's
|
|
19
|
+
// time to encode, and a picture asked for one by one (Page.captureScreenshot)
|
|
20
|
+
// takes 65 to 85 ms, so twelve to fifteen a second at the very most. Chrome
|
|
21
|
+
// pushing its own pictures (Page.startScreencast) reaches 60 a second if
|
|
22
|
+
// unheld, so the pace is set here rather than left to Chrome.
|
|
23
|
+
const PICTURE_QUALITY = 75;
|
|
24
|
+
const PICTURE_PERIOD_MS = 1000 / 30; // one pane: thirty pictures a second
|
|
25
|
+
const ALL_PICTURES_PERIOD_MS = 1000 / 48; // every pane together: forty-eight
|
|
26
|
+
// Right after an action on the page the next picture is not held back, so the
|
|
27
|
+
// person sees what they did at once. This is the closest two of those may be.
|
|
28
|
+
const HURRY_PERIOD_MS = 1000 / 60;
|
|
29
|
+
// A viewer renews this with every request. When it runs out, nobody is looking
|
|
30
|
+
// and the screencast stops, whether or not the viewer said goodbye.
|
|
31
|
+
const VIEW_LEASE_MS = 1500;
|
|
32
|
+
// Viewer requests waiting for a picture hold a connection each. The same count
|
|
33
|
+
// terminal streams allow, so two of a browser's six stay free for input.
|
|
34
|
+
const WAITING_VIEWERS_MAX = 4;
|
|
35
|
+
const PICTURE_WAIT_MAX_MS = 2000;
|
|
36
|
+
// A screencast that has sent nothing by then is treated as not working, and
|
|
37
|
+
// pictures are taken one by one instead until it is tried again. A stream
|
|
38
|
+
// cannot start while its page is still opening, which is exactly when a new
|
|
39
|
+
// pane gets its first look, so the retry is quick.
|
|
40
|
+
const CAST_GRACE_MS = 600;
|
|
41
|
+
const CAST_RETRY_MS = 500;
|
|
42
|
+
// What Chrome answers a page command with while the page is still opening.
|
|
43
|
+
const OPENING_PAGE = /Not attached to an active page/;
|
|
44
|
+
const OPENING_PAGE_RETRY_MS = 50;
|
|
45
|
+
const OPENING_PAGE_WAIT_MS = 2000;
|
|
46
|
+
// With no usable picture in hand (a first look, just after a resize), this is
|
|
47
|
+
// how long Chrome's own next picture is given before one is taken instead. It
|
|
48
|
+
// normally comes within 30 ms; a capture takes 65 to 85.
|
|
49
|
+
const PICTURE_GRACE_MS = 300;
|
|
50
|
+
// Title and history are read from the page at most this often for a viewer.
|
|
51
|
+
const METADATA_PERIOD_MS = 1000;
|
|
52
|
+
|
|
53
|
+
// The pixel size of a JPEG, read from its own header. A picture is labelled
|
|
54
|
+
// with this and never with what the viewport is believed to be at the time.
|
|
55
|
+
function jpegSize(buffer) {
|
|
56
|
+
let offset = 2;
|
|
57
|
+
while (offset + 9 < buffer.length) {
|
|
58
|
+
if (buffer[offset] !== 0xff) { offset += 1; continue; }
|
|
59
|
+
const marker = buffer[offset + 1];
|
|
60
|
+
if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) {
|
|
61
|
+
return { height: buffer.readUInt16BE(offset + 5), width: buffer.readUInt16BE(offset + 7) };
|
|
62
|
+
}
|
|
63
|
+
offset += 2 + buffer.readUInt16BE(offset + 2);
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
69
|
+
|
|
70
|
+
// A page read that must not hold a response: the page may be mid-navigation.
|
|
71
|
+
async function promptly(promise, ms, fallback) {
|
|
72
|
+
return Promise.race([promise, sleep(ms).then(() => fallback)]);
|
|
73
|
+
}
|
|
74
|
+
|
|
14
75
|
// Action result statuses. These distinguish "the instruction was received" from
|
|
15
76
|
// "the instruction happened", which is the point of the assessment.
|
|
16
77
|
const STATUS = {
|
|
@@ -38,6 +99,10 @@ function installBrowserWorkspace(app) {
|
|
|
38
99
|
let browserPromise;
|
|
39
100
|
let opening = 0;
|
|
40
101
|
let observationCounter = 0;
|
|
102
|
+
// Shared by every pane: when the last picture of any pane was due, and how
|
|
103
|
+
// many viewer requests are holding a connection while they wait.
|
|
104
|
+
let lastPictureAt = 0;
|
|
105
|
+
let waitingViewers = 0;
|
|
41
106
|
|
|
42
107
|
const resolveEngine = () => resolveChrome();
|
|
43
108
|
const engineStatus = () => {
|
|
@@ -69,6 +134,7 @@ function installBrowserWorkspace(app) {
|
|
|
69
134
|
browserPromise = null;
|
|
70
135
|
// Every session belonged to the dead browser. Clearing the map lets
|
|
71
136
|
// the UI reconcile instead of offering unclosable ghost tabs.
|
|
137
|
+
for (const s of sessions.values()) settlePictures(s);
|
|
72
138
|
sessions.clear();
|
|
73
139
|
});
|
|
74
140
|
return browser;
|
|
@@ -83,6 +149,20 @@ function installBrowserWorkspace(app) {
|
|
|
83
149
|
|
|
84
150
|
const human = (req) => req.get('X-Workspace-Key') === humanKey;
|
|
85
151
|
|
|
152
|
+
// No password value ever leaves the server. The page reports what it holds
|
|
153
|
+
// about a control (its value and its type), and this is the one place that
|
|
154
|
+
// decides what may go on: a password field's value is dropped and the field
|
|
155
|
+
// is marked redacted, as the catalog already does inside the page. Every
|
|
156
|
+
// path a control description takes into a result passes through here, so a
|
|
157
|
+
// new action or route cannot forget. Typing into the field is untouched:
|
|
158
|
+
// only what comes back is masked.
|
|
159
|
+
function redactControl(control) {
|
|
160
|
+
if (!control || typeof control !== 'object') return control;
|
|
161
|
+
const type = String(control.type ?? control.kind ?? '').toLowerCase();
|
|
162
|
+
if (type !== 'password') return control;
|
|
163
|
+
return { ...control, value: null, redacted: true };
|
|
164
|
+
}
|
|
165
|
+
|
|
86
166
|
const describe = (s) => ({
|
|
87
167
|
id: s.id,
|
|
88
168
|
kind: s.kind,
|
|
@@ -220,6 +300,8 @@ function installBrowserWorkspace(app) {
|
|
|
220
300
|
} catch (error) {
|
|
221
301
|
deny(`The page dialog could not be answered: ${error.message}`, 409);
|
|
222
302
|
}
|
|
303
|
+
// The page can draw again from here on; a viewer should see it at once.
|
|
304
|
+
hurryCast(s);
|
|
223
305
|
return describe(s);
|
|
224
306
|
}
|
|
225
307
|
|
|
@@ -424,6 +506,22 @@ function installBrowserWorkspace(app) {
|
|
|
424
506
|
fixedWidth: 1280,
|
|
425
507
|
fixedHeight: 800,
|
|
426
508
|
queue: Promise.resolve(),
|
|
509
|
+
// One protocol session for the page, made on first use.
|
|
510
|
+
cdp: null,
|
|
511
|
+
// The newest picture of the page, with the size it really has in pixels.
|
|
512
|
+
frame: null,
|
|
513
|
+
frameSeq: 0,
|
|
514
|
+
// Who is watching: a lease renewed by each viewer request, and Chrome's
|
|
515
|
+
// own picture stream while it holds.
|
|
516
|
+
viewLease: 0,
|
|
517
|
+
viewTimer: null,
|
|
518
|
+
cast: null,
|
|
519
|
+
castFailedAt: 0,
|
|
520
|
+
// Viewer requests waiting for the next picture.
|
|
521
|
+
waiters: new Set(),
|
|
522
|
+
titleAt: 0,
|
|
523
|
+
history: { canGoBack: false, canGoForward: false },
|
|
524
|
+
historyAt: 0,
|
|
427
525
|
...fields
|
|
428
526
|
};
|
|
429
527
|
}
|
|
@@ -482,77 +580,403 @@ function installBrowserWorkspace(app) {
|
|
|
482
580
|
}
|
|
483
581
|
}
|
|
484
582
|
|
|
583
|
+
// One protocol session per page for its whole life. It used to be made and
|
|
584
|
+
// detached around every picture, which was pure overhead.
|
|
585
|
+
function cdpSession(s) {
|
|
586
|
+
if (!s.cdp) {
|
|
587
|
+
const pending = s.page.createCDPSession().then((client) => {
|
|
588
|
+
client.on('disconnected', () => {
|
|
589
|
+
if (s.cdp === pending) s.cdp = null;
|
|
590
|
+
if (s.cast && s.cast.client === client) { clearTimeout(s.cast.timer); s.cast = null; }
|
|
591
|
+
});
|
|
592
|
+
return client;
|
|
593
|
+
});
|
|
594
|
+
pending.catch(() => { if (s.cdp === pending) s.cdp = null; });
|
|
595
|
+
s.cdp = pending;
|
|
596
|
+
}
|
|
597
|
+
return s.cdp;
|
|
598
|
+
}
|
|
599
|
+
|
|
485
600
|
async function historyState(s) {
|
|
486
|
-
let client = null;
|
|
487
601
|
try {
|
|
488
|
-
client = await s
|
|
489
|
-
const history = await client.send('Page.getNavigationHistory');
|
|
602
|
+
const client = await cdpSession(s);
|
|
603
|
+
const history = await client.send('Page.getNavigationHistory', {}, { timeout: 2000 });
|
|
490
604
|
return {
|
|
491
605
|
canGoBack: Number(history.currentIndex) > 0,
|
|
492
606
|
canGoForward: Number(history.currentIndex) < (history.entries || []).length - 1
|
|
493
607
|
};
|
|
494
608
|
} catch (_) {
|
|
495
|
-
return {
|
|
496
|
-
} finally {
|
|
497
|
-
if (client) await client.detach().catch(() => {});
|
|
609
|
+
return { ...s.history };
|
|
498
610
|
}
|
|
499
611
|
}
|
|
500
612
|
|
|
501
613
|
async function capture(s) {
|
|
502
|
-
const client = await s
|
|
614
|
+
const client = await cdpSession(s);
|
|
615
|
+
return client.send('Page.captureScreenshot', { format: 'jpeg', quality: PICTURE_QUALITY, captureBeyondViewport: false }, { timeout: 10000 });
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
async function refreshTitle(s) {
|
|
619
|
+
try { s.title = await promptly(s.page.title(), 1500, s.title); } catch (_) { /* page gone */ }
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// ── The picture stream for someone watching ───────────────────────────────
|
|
623
|
+
//
|
|
624
|
+
// A person's viewer asks with `watch=1` while its pane is on screen. While
|
|
625
|
+
// such a request has arrived within VIEW_LEASE_MS, Chrome itself sends a
|
|
626
|
+
// picture each time the page repaints (Page.startScreencast): a still page
|
|
627
|
+
// costs nothing, and a changing one is shown as it changes. Agents never
|
|
628
|
+
// send `watch`; an observation is its own capture, when it is asked for.
|
|
629
|
+
//
|
|
630
|
+
// Chrome sends its next picture only once the last one is acknowledged, and
|
|
631
|
+
// an acknowledgement is held to one pane period and one all-panes period. So
|
|
632
|
+
// each pane is capped and all the panes together are capped. Measured: about
|
|
633
|
+
// 1.05 pictures for each acknowledgement once a stream is running. (In its
|
|
634
|
+
// first 150 ms a stream sends five or six pictures before that takes hold,
|
|
635
|
+
// and while an acknowledgement is held Chrome may have two more ready; that
|
|
636
|
+
// is Chrome's own doing, and bounded.) The picture itself is handed over the
|
|
637
|
+
// moment it arrives; only Chrome's leave to make the next one waits.
|
|
638
|
+
//
|
|
639
|
+
// A picture Chrome pushes can have been taken at any moment, including
|
|
640
|
+
// while the viewport is being changed. It is accepted only if its own pixel
|
|
641
|
+
// size is the viewport's, so a picture is never labelled with a size that is
|
|
642
|
+
// not its own and click mapping stays exact. Pictures taken on request still
|
|
643
|
+
// run inside the session queue, as before, and so never meet a resize.
|
|
644
|
+
|
|
645
|
+
const viewLeased = (s) => s.viewLease > Date.now();
|
|
646
|
+
|
|
647
|
+
const wakeViewers = (s) => { for (const waiter of [...s.waiters]) waiter.wake(); };
|
|
648
|
+
|
|
649
|
+
async function startCast(s) {
|
|
650
|
+
if (Date.now() - s.castFailedAt < CAST_RETRY_MS) return;
|
|
651
|
+
const client = await cdpSession(s);
|
|
652
|
+
if (s.cast || !viewLeased(s)) return;
|
|
653
|
+
const cast = { client, listener: null, timer: null, held: null, slot: 0, acked: 0, healthy: false, startedAt: Date.now() };
|
|
654
|
+
cast.listener = (event) => onCastFrame(s, cast, event);
|
|
655
|
+
s.cast = cast;
|
|
656
|
+
client.on('Page.screencastFrame', cast.listener);
|
|
503
657
|
try {
|
|
504
|
-
|
|
505
|
-
}
|
|
506
|
-
|
|
658
|
+
await client.send('Page.startScreencast', { format: 'jpeg', quality: PICTURE_QUALITY });
|
|
659
|
+
} catch (error) {
|
|
660
|
+
if (s.cast === cast) s.cast = null;
|
|
661
|
+
await detachCast(cast);
|
|
662
|
+
s.castFailedAt = Date.now();
|
|
663
|
+
throw error;
|
|
507
664
|
}
|
|
508
665
|
}
|
|
509
666
|
|
|
510
|
-
async function
|
|
511
|
-
|
|
667
|
+
async function detachCast(cast) {
|
|
668
|
+
clearTimeout(cast.timer);
|
|
669
|
+
cast.client.off('Page.screencastFrame', cast.listener);
|
|
670
|
+
await cast.client.send('Page.stopScreencast').catch(() => {});
|
|
512
671
|
}
|
|
513
672
|
|
|
514
|
-
async function
|
|
515
|
-
const
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
673
|
+
async function stopCast(s) {
|
|
674
|
+
const cast = s.cast;
|
|
675
|
+
s.cast = null;
|
|
676
|
+
if (cast) await detachCast(cast);
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
function onCastFrame(s, cast, event) {
|
|
680
|
+
if (s.cast !== cast) return;
|
|
681
|
+
const now = performance.now();
|
|
682
|
+
const acknowledge = () => {
|
|
683
|
+
if (s.cast !== cast) return;
|
|
684
|
+
cast.acked = performance.now();
|
|
685
|
+
cast.client.send('Page.screencastFrameAck', { sessionId: event.sessionId }).catch(() => {});
|
|
686
|
+
};
|
|
687
|
+
cast.healthy = true;
|
|
688
|
+
const size = jpegSize(Buffer.from(event.data, 'base64'));
|
|
689
|
+
if (!size || size.width !== s.width || size.height !== s.height) {
|
|
690
|
+
// Taken while the viewport was changing: not a picture of the page as it is.
|
|
691
|
+
acknowledge();
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
// When Chrome may send its next picture. The pace applies to every picture
|
|
695
|
+
// it sends, whether or not this one is kept.
|
|
696
|
+
cast.slot = Math.max(now, cast.slot + PICTURE_PERIOD_MS, lastPictureAt + ALL_PICTURES_PERIOD_MS);
|
|
697
|
+
lastPictureAt = cast.slot;
|
|
698
|
+
if (cast.slot > now) {
|
|
699
|
+
cast.held = acknowledge;
|
|
700
|
+
cast.timer = setTimeout(() => { cast.timer = null; cast.held = null; acknowledge(); }, cast.slot - now);
|
|
701
|
+
} else {
|
|
702
|
+
acknowledge();
|
|
703
|
+
}
|
|
704
|
+
// The same picture as the one held (the stream's first, right after one
|
|
705
|
+
// was taken for a first look): nothing changed, and nothing is sent.
|
|
706
|
+
if (s.frame && s.frame.data === event.data) return;
|
|
707
|
+
s.frameSeq += 1;
|
|
708
|
+
s.frame = { seq: s.frameSeq, data: event.data, width: size.width, height: size.height, at: new Date().toISOString() };
|
|
709
|
+
wakeViewers(s);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
// An action just happened on a page someone is watching. The picture whose
|
|
713
|
+
// acknowledgement is being held shows the page from before the action; let it
|
|
714
|
+
// through now so the next picture, the one with the action, is not kept
|
|
715
|
+
// waiting. The slot it was given stays taken, so the pane keeps its pace.
|
|
716
|
+
function hurryCast(s) {
|
|
717
|
+
const cast = s.cast;
|
|
718
|
+
if (!cast || !cast.timer) return;
|
|
719
|
+
const now = performance.now();
|
|
720
|
+
if (now - cast.acked < HURRY_PERIOD_MS) return;
|
|
721
|
+
clearTimeout(cast.timer);
|
|
722
|
+
cast.timer = null;
|
|
723
|
+
const release = cast.held;
|
|
724
|
+
cast.held = null;
|
|
725
|
+
if (release) release();
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
function leaseViewer(s) {
|
|
729
|
+
const lapsed = !viewLeased(s);
|
|
730
|
+
s.viewLease = Date.now() + VIEW_LEASE_MS;
|
|
731
|
+
clearTimeout(s.viewTimer);
|
|
732
|
+
s.viewTimer = setTimeout(() => {
|
|
733
|
+
s.viewTimer = null;
|
|
734
|
+
if (!viewLeased(s)) stopCast(s).catch(() => {});
|
|
735
|
+
}, VIEW_LEASE_MS + 100).unref?.();
|
|
736
|
+
if (s.cast) return Promise.resolve();
|
|
737
|
+
// Nobody was watching, so the picture held may be from any time since: the
|
|
738
|
+
// first request waits for a fresh one. With the lease still valid (the
|
|
739
|
+
// stream stopped for another reason) the picture is kept until one comes.
|
|
740
|
+
if (lapsed) s.frame = null;
|
|
741
|
+
return startCast(s).catch(() => {});
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
const castBroken = (s) => (s.cast
|
|
745
|
+
? !s.cast.healthy && Date.now() - s.cast.startedAt > CAST_GRACE_MS
|
|
746
|
+
: viewLeased(s) && Date.now() - s.castFailedAt < CAST_RETRY_MS);
|
|
747
|
+
|
|
748
|
+
const currentPicture = (s) => (s.frame && s.frame.width === s.width && s.frame.height === s.height ? s.frame : null);
|
|
749
|
+
|
|
750
|
+
// End a wait when the viewer goes away or the page is gone.
|
|
751
|
+
function waitForPicture(s, wait, signal) {
|
|
752
|
+
if (signal?.aborted || waitingViewers >= WAITING_VIEWERS_MAX) return Promise.resolve('skipped');
|
|
753
|
+
return new Promise((resolve) => {
|
|
754
|
+
const waiter = {};
|
|
755
|
+
const finish = (outcome) => {
|
|
756
|
+
clearTimeout(timer);
|
|
757
|
+
s.waiters.delete(waiter);
|
|
758
|
+
signal?.removeEventListener('abort', aborted);
|
|
759
|
+
waitingViewers -= 1;
|
|
760
|
+
resolve(outcome);
|
|
761
|
+
};
|
|
762
|
+
waiter.wake = () => finish('picture');
|
|
763
|
+
const aborted = () => finish('aborted');
|
|
764
|
+
const timer = setTimeout(() => finish('timeout'), wait);
|
|
765
|
+
s.waiters.add(waiter);
|
|
766
|
+
waitingViewers += 1;
|
|
767
|
+
if (signal) signal.addEventListener('abort', aborted, { once: true });
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
// Everything about the page except the picture, for a viewer. The title and
|
|
772
|
+
// history are read from the page at most METADATA_PERIOD_MS, and never inside
|
|
773
|
+
// the session queue: a viewer must not wait behind an agent's slow navigation
|
|
774
|
+
// to be answered.
|
|
775
|
+
async function refreshMetadata(s) {
|
|
776
|
+
const now = Date.now();
|
|
777
|
+
if (now - s.titleAt < METADATA_PERIOD_MS && now - s.historyAt < METADATA_PERIOD_MS) return;
|
|
778
|
+
s.titleAt = now;
|
|
779
|
+
s.historyAt = now;
|
|
780
|
+
await Promise.all([
|
|
781
|
+
refreshTitle(s),
|
|
782
|
+
historyState(s).then((history) => { s.history = history; })
|
|
783
|
+
]);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
function describeForViewer(s, extra = {}) {
|
|
787
|
+
return {
|
|
788
|
+
...describe(s),
|
|
789
|
+
...s.history,
|
|
790
|
+
width: s.width,
|
|
791
|
+
height: s.height,
|
|
792
|
+
seq: s.frameSeq,
|
|
793
|
+
page: { ...currentPage(s), scrollX: null, scrollY: null, capturedAt: (s.frame && s.frame.at) || null, revision: s.revision },
|
|
794
|
+
...extra
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
function pictureAnswer(s, picture) {
|
|
799
|
+
// A picture is not held back for a title read; the next answer carries it.
|
|
800
|
+
void refreshMetadata(s);
|
|
801
|
+
return describeForViewer(s, {
|
|
802
|
+
changed: true,
|
|
803
|
+
held: true,
|
|
804
|
+
image: picture.data,
|
|
805
|
+
width: picture.width,
|
|
806
|
+
height: picture.height,
|
|
807
|
+
seq: picture.seq,
|
|
808
|
+
page: { ...currentPage(s), width: picture.width, height: picture.height, scrollX: null, scrollY: null, capturedAt: picture.at, revision: s.revision }
|
|
809
|
+
});
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
// A picture taken on request, inside the session queue: the viewer's stream
|
|
813
|
+
// is left running. Stopping and restarting it for each capture was tried and
|
|
814
|
+
// is worse: every start costs a burst of five or six pictures in Chrome's
|
|
815
|
+
// first 150 ms. So a page has at most one capture out at a time and, once
|
|
816
|
+
// its stream is running, about one stream picture out for each
|
|
817
|
+
// acknowledgement; and an agent's capture never waits for a viewer's pictures.
|
|
818
|
+
async function takePicture(s) {
|
|
819
|
+
// A page that is still opening has nothing to take a picture of yet: a
|
|
820
|
+
// request that arrives then waits for the page instead of failing.
|
|
821
|
+
let taken = null;
|
|
822
|
+
for (const deadline = Date.now() + OPENING_PAGE_WAIT_MS; !taken; await sleep(OPENING_PAGE_RETRY_MS)) {
|
|
823
|
+
try {
|
|
824
|
+
taken = await capture(s);
|
|
825
|
+
} catch (error) {
|
|
826
|
+
if (!OPENING_PAGE.test(error.message) || Date.now() >= deadline) throw error;
|
|
530
827
|
}
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
828
|
+
}
|
|
829
|
+
lastPictureAt = performance.now();
|
|
830
|
+
const size = jpegSize(Buffer.from(taken.data, 'base64')) || { width: s.width, height: s.height };
|
|
831
|
+
s.frameSeq += 1;
|
|
832
|
+
s.frame = { seq: s.frameSeq, data: taken.data, width: size.width, height: size.height, at: new Date().toISOString() };
|
|
833
|
+
wakeViewers(s);
|
|
834
|
+
return s.frame;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
// A picture taken on request. Runs in the session queue, so it never meets a
|
|
838
|
+
// viewport change. This is what an agent's `snapshot` and an old client get.
|
|
839
|
+
async function fullSnapshot(s, options = {}) {
|
|
840
|
+
// CDP screenshot waits for a JS dialog and times out. Keep the last
|
|
841
|
+
// frame, but return the dialog so the human/agent can answer it.
|
|
842
|
+
if (s.pendingDialog) {
|
|
843
|
+
const history = await historyState(s);
|
|
537
844
|
return {
|
|
538
845
|
...describe(s),
|
|
539
|
-
...
|
|
540
|
-
image:
|
|
846
|
+
...history,
|
|
847
|
+
image: null,
|
|
541
848
|
width: s.width,
|
|
542
849
|
height: s.height,
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
scrollY: info.scrollY,
|
|
548
|
-
url: s.page.url(),
|
|
549
|
-
title: s.title || '',
|
|
550
|
-
capturedAt: new Date().toISOString(),
|
|
551
|
-
revision: s.revision
|
|
552
|
-
},
|
|
553
|
-
...(options.observation ? { observation: recordObservation(s, { scrollX: info.scrollX, scrollY: info.scrollY, layoutHash: info.hash }) } : {})
|
|
850
|
+
seq: s.frameSeq,
|
|
851
|
+
changed: false,
|
|
852
|
+
dialog: s.dialog,
|
|
853
|
+
page: { ...currentPage(s), scrollX: 0, scrollY: 0 }
|
|
554
854
|
};
|
|
555
|
-
}
|
|
855
|
+
}
|
|
856
|
+
await s.page.bringToFront();
|
|
857
|
+
const picture = await takePicture(s);
|
|
858
|
+
await refreshTitle(s);
|
|
859
|
+
// A cheap position read: the full layout signature is only needed when
|
|
860
|
+
// deciding whether a coordinate target is still valid.
|
|
861
|
+
const info = await dom.scrollPosition(s.page);
|
|
862
|
+
return {
|
|
863
|
+
...describe(s),
|
|
864
|
+
...(await historyState(s)),
|
|
865
|
+
image: picture.data,
|
|
866
|
+
width: picture.width,
|
|
867
|
+
height: picture.height,
|
|
868
|
+
seq: picture.seq,
|
|
869
|
+
changed: true,
|
|
870
|
+
page: {
|
|
871
|
+
width: picture.width,
|
|
872
|
+
height: picture.height,
|
|
873
|
+
scrollX: info.scrollX,
|
|
874
|
+
scrollY: info.scrollY,
|
|
875
|
+
url: s.page.url(),
|
|
876
|
+
title: s.title || '',
|
|
877
|
+
capturedAt: picture.at,
|
|
878
|
+
revision: s.revision
|
|
879
|
+
},
|
|
880
|
+
...(options.observation ? { observation: recordObservation(s, { scrollX: info.scrollX, scrollY: info.scrollY, layoutHash: info.hash }) } : {})
|
|
881
|
+
};
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
// How long to give Chrome's stream to bring a picture before one is taken.
|
|
885
|
+
// A stream that has not yet sent anything gets its start-up grace, no more.
|
|
886
|
+
function castPatience(s) {
|
|
887
|
+
const cast = s.cast;
|
|
888
|
+
if (!cast) return 0;
|
|
889
|
+
if (cast.healthy) return PICTURE_GRACE_MS;
|
|
890
|
+
return Math.max(0, Math.min(PICTURE_GRACE_MS, CAST_GRACE_MS - (Date.now() - cast.startedAt)));
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
async function viewerSnapshot(s, { watch, since, wait, signal }) {
|
|
894
|
+
const live = () => { if (sessions.get(s.id) !== s) deny('Browser pane no longer exists.', 404); };
|
|
895
|
+
live();
|
|
896
|
+
// A pane nobody can see: its title, address and page prompt only. No
|
|
897
|
+
// picture is taken and the stream is not started.
|
|
898
|
+
if (!watch) {
|
|
899
|
+
await refreshMetadata(s);
|
|
900
|
+
return describeForViewer(s, { changed: false, held: false });
|
|
901
|
+
}
|
|
902
|
+
await leaseViewer(s);
|
|
903
|
+
const deadline = Date.now() + wait;
|
|
904
|
+
let held = false;
|
|
905
|
+
for (;;) {
|
|
906
|
+
live();
|
|
907
|
+
if (signal?.aborted) return null;
|
|
908
|
+
if (s.pendingDialog) return describeForViewer(s, { changed: false, held: false, dialog: s.dialog });
|
|
909
|
+
const picture = currentPicture(s);
|
|
910
|
+
const left = deadline - Date.now();
|
|
911
|
+
if (!picture || castBroken(s)) {
|
|
912
|
+
// No picture of the page as it is now: a first look, or the viewport
|
|
913
|
+
// has just changed. Chrome's next one is quicker than a capture and is
|
|
914
|
+
// of the page as it is now, so give it a moment, then take one.
|
|
915
|
+
const patience = castBroken(s) ? 0 : castPatience(s);
|
|
916
|
+
if (left > 0 && patience > 0) {
|
|
917
|
+
const outcome = await waitForPicture(s, Math.min(left, patience), signal);
|
|
918
|
+
if (outcome === 'aborted') return null;
|
|
919
|
+
// No free waiting slot: take a picture now rather than ask the viewer to ask again with none in hand.
|
|
920
|
+
if (outcome !== 'skipped') continue;
|
|
921
|
+
}
|
|
922
|
+
// The all-panes pace holds for a picture taken one at a time too.
|
|
923
|
+
const turn = lastPictureAt + ALL_PICTURES_PERIOD_MS - performance.now();
|
|
924
|
+
if (turn > 0) await sleep(turn);
|
|
925
|
+
return enqueue(s, () => fullSnapshot(s));
|
|
926
|
+
}
|
|
927
|
+
if (since > s.frameSeq) since = -1; // The viewer has a picture of an earlier page.
|
|
928
|
+
if (picture.seq > since) return pictureAnswer(s, picture);
|
|
929
|
+
if (left <= 0) {
|
|
930
|
+
await refreshMetadata(s);
|
|
931
|
+
return describeForViewer(s, { changed: false, held });
|
|
932
|
+
}
|
|
933
|
+
// Do not wait past the moment a stream that has sent nothing would be
|
|
934
|
+
// given up on.
|
|
935
|
+
const grace = s.cast && !s.cast.healthy ? Math.max(0, s.cast.startedAt + CAST_GRACE_MS - Date.now()) : left;
|
|
936
|
+
const outcome = await waitForPicture(s, Math.min(left, grace), signal);
|
|
937
|
+
if (outcome === 'aborted') return null;
|
|
938
|
+
if (outcome === 'skipped') {
|
|
939
|
+
// Every waiting slot is taken (another viewer is holding it): answer
|
|
940
|
+
// now, saying so, and the viewer asks again shortly.
|
|
941
|
+
await refreshMetadata(s);
|
|
942
|
+
return describeForViewer(s, { changed: false, held: false });
|
|
943
|
+
}
|
|
944
|
+
// A picture came, or the wait ran out: look once more, then answer.
|
|
945
|
+
held = true;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
// What a viewer's request asks for, and a way to learn that the viewer went
|
|
950
|
+
// away while it waited. `{}` for every other caller: an agent, or an old
|
|
951
|
+
// client, gets a picture taken for it there and then.
|
|
952
|
+
function viewerRequest(req, res) {
|
|
953
|
+
const watch = req.query.watch;
|
|
954
|
+
if (watch !== '0' && watch !== '1') return {};
|
|
955
|
+
const controller = new AbortController();
|
|
956
|
+
res.once('close', () => { if (!res.writableEnded) controller.abort(); });
|
|
957
|
+
return {
|
|
958
|
+
viewer: {
|
|
959
|
+
watch: watch === '1',
|
|
960
|
+
since: Math.max(-1, Math.floor(Number(req.query.since) || -1)),
|
|
961
|
+
wait: Math.max(0, Math.min(PICTURE_WAIT_MAX_MS, Number(req.query.wait) || 0))
|
|
962
|
+
},
|
|
963
|
+
signal: controller.signal
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
async function snapshotTab(id, options = {}) {
|
|
968
|
+
const s = sessions.get(id);
|
|
969
|
+
if (!s) deny('Browser pane no longer exists.', 404);
|
|
970
|
+
if (options.viewer) return viewerSnapshot(s, { ...options.viewer, signal: options.signal });
|
|
971
|
+
return enqueue(s, () => fullSnapshot(s, options));
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
// Nothing of a page's picture stream outlives the page.
|
|
975
|
+
function settlePictures(s) {
|
|
976
|
+
clearTimeout(s.viewTimer);
|
|
977
|
+
s.viewLease = 0;
|
|
978
|
+
for (const waiter of [...s.waiters]) waiter.wake();
|
|
979
|
+
stopCast(s).catch(() => {});
|
|
556
980
|
}
|
|
557
981
|
|
|
558
982
|
/**
|
|
@@ -570,6 +994,9 @@ function installBrowserWorkspace(app) {
|
|
|
570
994
|
} else {
|
|
571
995
|
await s.page.bringToFront();
|
|
572
996
|
image = (await capture(s)).data;
|
|
997
|
+
// An agent's capture is never made to wait for a viewer's pictures,
|
|
998
|
+
// but it is counted against every pane's allowance.
|
|
999
|
+
lastPictureAt = performance.now();
|
|
573
1000
|
history = await historyState(s);
|
|
574
1001
|
}
|
|
575
1002
|
await refreshTitle(s);
|
|
@@ -593,7 +1020,7 @@ function installBrowserWorkspace(app) {
|
|
|
593
1020
|
const catalogued = await dom.catalog(s.page, { max: options.max, start: s.nextRef });
|
|
594
1021
|
s.nextRef = Math.max(s.nextRef, catalogued.nextRef);
|
|
595
1022
|
for (const element of catalogued.elements) rememberRef(s, element, observation.id);
|
|
596
|
-
payload.elements = catalogued.elements;
|
|
1023
|
+
payload.elements = catalogued.elements.map(redactControl);
|
|
597
1024
|
payload.frameCount = catalogued.frameCount;
|
|
598
1025
|
payload.truncated = catalogued.elements.length >= Math.max(1, Math.min(500, Number(options.max) || 200));
|
|
599
1026
|
}
|
|
@@ -619,7 +1046,7 @@ function installBrowserWorkspace(app) {
|
|
|
619
1046
|
return {
|
|
620
1047
|
...describe(s),
|
|
621
1048
|
observation,
|
|
622
|
-
elements: catalogued.elements,
|
|
1049
|
+
elements: catalogued.elements.map(redactControl),
|
|
623
1050
|
frameCount: catalogued.frameCount,
|
|
624
1051
|
truncated: catalogued.elements.length >= Math.max(1, Math.min(500, Number(options.max) || 200))
|
|
625
1052
|
};
|
|
@@ -653,6 +1080,7 @@ function installBrowserWorkspace(app) {
|
|
|
653
1080
|
// Read the title *after* the navigation. Returning the new address with
|
|
654
1081
|
// the previous page's title was the bug reported in the assessment.
|
|
655
1082
|
await refreshTitle(s);
|
|
1083
|
+
hurryCast(s);
|
|
656
1084
|
return describe(s);
|
|
657
1085
|
});
|
|
658
1086
|
}
|
|
@@ -903,10 +1331,16 @@ function installBrowserWorkspace(app) {
|
|
|
903
1331
|
else node.value = String(value ?? '');
|
|
904
1332
|
node.dispatchEvent(new Event('input', { bubbles: true }));
|
|
905
1333
|
node.dispatchEvent(new Event('change', { bubbles: true }));
|
|
906
|
-
return {
|
|
1334
|
+
return {
|
|
1335
|
+
ok: true,
|
|
1336
|
+
value: String(node.value ?? ''),
|
|
1337
|
+
// The field's type travels with its value so the server can
|
|
1338
|
+
// tell a password field before the value goes any further.
|
|
1339
|
+
type: node.tagName === 'INPUT' ? String(node.type || '').toLowerCase() : ''
|
|
1340
|
+
};
|
|
907
1341
|
}, data.value);
|
|
908
1342
|
if (!applied.ok) { actionStatus = STATUS.wrongKind; message = 'That element does not hold a value.'; break; }
|
|
909
|
-
control = { value: applied.value };
|
|
1343
|
+
control = { value: applied.value, type: applied.type };
|
|
910
1344
|
} else {
|
|
911
1345
|
const position = data.x !== undefined ? point(s, data) : null;
|
|
912
1346
|
if (!position) deny('select needs a ref or x/y coordinates.', 400);
|
|
@@ -951,8 +1385,10 @@ function installBrowserWorkspace(app) {
|
|
|
951
1385
|
message: message || undefined,
|
|
952
1386
|
warning: warning || undefined,
|
|
953
1387
|
target: target || undefined,
|
|
954
|
-
|
|
955
|
-
|
|
1388
|
+
// These two are every value a page reports about a control in an action
|
|
1389
|
+
// (a click or typing reading the field back, and the focused element).
|
|
1390
|
+
control: redactControl(control),
|
|
1391
|
+
focused: redactControl(focus),
|
|
956
1392
|
observationId: s.lastObservationId || null
|
|
957
1393
|
});
|
|
958
1394
|
return payload;
|
|
@@ -966,11 +1402,15 @@ function installBrowserWorkspace(app) {
|
|
|
966
1402
|
// caller's grant inside the queue so a revoked token cannot execute a
|
|
967
1403
|
// side effect it was authorized for moments earlier.
|
|
968
1404
|
if (typeof options.authorize === 'function') options.authorize();
|
|
969
|
-
return performAction(s, action, data)
|
|
1405
|
+
return performAction(s, action, data).then((outcome) => {
|
|
1406
|
+
// The picture being held back is from before the action.
|
|
1407
|
+
hurryCast(s);
|
|
1408
|
+
return outcome;
|
|
1409
|
+
});
|
|
970
1410
|
});
|
|
971
1411
|
}
|
|
972
1412
|
|
|
973
|
-
async function waitTab(id, conditions = {}) {
|
|
1413
|
+
async function waitTab(id, conditions = {}, options = {}) {
|
|
974
1414
|
const s = sessions.get(id);
|
|
975
1415
|
if (!s) deny('Browser pane no longer exists.', 404);
|
|
976
1416
|
const timeout = Math.max(100, Math.min(120000, Number(conditions.timeout) || 30000));
|
|
@@ -982,6 +1422,18 @@ function installBrowserWorkspace(app) {
|
|
|
982
1422
|
if (!hasUrl && !hasText && !hasSelector && !hasValue && !hasTitle) {
|
|
983
1423
|
deny('Provide url, title, text, selector, or value with ref.', 400);
|
|
984
1424
|
}
|
|
1425
|
+
if (hasValue) {
|
|
1426
|
+
// A password field's value never leaves the page, so it cannot be a
|
|
1427
|
+
// condition either: a match would confirm a guess of the value.
|
|
1428
|
+
const probe = await dom.resolveRef(s.page, conditions.ref);
|
|
1429
|
+
const type = probe
|
|
1430
|
+
? await probe.evaluate((node) => String((node && node.type) || '').toLowerCase()).catch(() => '')
|
|
1431
|
+
: '';
|
|
1432
|
+
if (probe) await probe.dispose().catch(() => {});
|
|
1433
|
+
if (type === 'password') {
|
|
1434
|
+
deny("A password field's value never leaves the page, so it cannot be waited on. Wait on a selector or on page text instead.", 400);
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
985
1437
|
const started = Date.now();
|
|
986
1438
|
const check = async () => {
|
|
987
1439
|
const url = (() => { try { return s.page.url(); } catch (_) { return ''; } })();
|
|
@@ -1007,7 +1459,10 @@ function installBrowserWorkspace(app) {
|
|
|
1007
1459
|
if (hasValue) {
|
|
1008
1460
|
const handle = await dom.resolveRef(s.page, conditions.ref);
|
|
1009
1461
|
if (handle) {
|
|
1010
|
-
|
|
1462
|
+
// Re-checked on every pass: a field that became a password field
|
|
1463
|
+
// after the wait started is never compared.
|
|
1464
|
+
const value = await handle.evaluate((node) => (node && 'value' in node && String(node.type || '').toLowerCase() !== 'password' ? String(node.value) : null))
|
|
1465
|
+
.catch(() => null);
|
|
1011
1466
|
if (value === conditions.value) return { matched: true, reason: 'value' };
|
|
1012
1467
|
}
|
|
1013
1468
|
}
|
|
@@ -1015,6 +1470,20 @@ function installBrowserWorkspace(app) {
|
|
|
1015
1470
|
};
|
|
1016
1471
|
let outcome = null;
|
|
1017
1472
|
for (;;) {
|
|
1473
|
+
// A caller that lost the right to read this page while waiting is told
|
|
1474
|
+
// so by the caller's own check rather than left waiting for an answer.
|
|
1475
|
+
if (typeof options.interrupted === 'function' && options.interrupted()) {
|
|
1476
|
+
return {
|
|
1477
|
+
ok: false,
|
|
1478
|
+
status: STATUS.unconfirmed,
|
|
1479
|
+
matched: false,
|
|
1480
|
+
reason: 'interrupted',
|
|
1481
|
+
elapsedMs: Date.now() - started,
|
|
1482
|
+
url: s.page.url(),
|
|
1483
|
+
title: s.title || '',
|
|
1484
|
+
observationId: s.lastObservationId || null
|
|
1485
|
+
};
|
|
1486
|
+
}
|
|
1018
1487
|
outcome = await check().catch(() => null);
|
|
1019
1488
|
if (outcome) break;
|
|
1020
1489
|
if (Date.now() - started >= timeout) break;
|
|
@@ -1048,6 +1517,8 @@ function installBrowserWorkspace(app) {
|
|
|
1048
1517
|
s.width = s.fixedWidth;
|
|
1049
1518
|
s.height = s.fixedHeight;
|
|
1050
1519
|
await s.page.setViewport({ width: s.width, height: s.height });
|
|
1520
|
+
// A viewer waiting for a picture now needs one of the new size.
|
|
1521
|
+
wakeViewers(s);
|
|
1051
1522
|
} else {
|
|
1052
1523
|
s.sizing = 'fit';
|
|
1053
1524
|
}
|
|
@@ -1060,12 +1531,21 @@ function installBrowserWorkspace(app) {
|
|
|
1060
1531
|
if (!s) return false;
|
|
1061
1532
|
clearTimeout(s.dialogTimer);
|
|
1062
1533
|
sessions.delete(id);
|
|
1534
|
+
settlePictures(s);
|
|
1063
1535
|
await s.context.close();
|
|
1064
1536
|
return true;
|
|
1065
1537
|
}
|
|
1066
1538
|
|
|
1067
1539
|
function getTab(id) { return sessions.get(id) || null; }
|
|
1068
1540
|
|
|
1541
|
+
// A pane workspace browser keeps its record in step with the pane's grant,
|
|
1542
|
+
// so what a snapshot says about a session is what the pane's routes enforce.
|
|
1543
|
+
function grantTab(id, permission) {
|
|
1544
|
+
const s = sessions.get(id);
|
|
1545
|
+
if (s && ['private', 'view', 'control'].includes(permission)) s.permission = permission;
|
|
1546
|
+
return s ? s.permission : null;
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1069
1549
|
async function setViewportTab(id, width, height, options = {}) {
|
|
1070
1550
|
const s = sessions.get(id);
|
|
1071
1551
|
if (!s) deny('Browser pane no longer exists.', 404);
|
|
@@ -1078,17 +1558,19 @@ function installBrowserWorkspace(app) {
|
|
|
1078
1558
|
s.width = Math.max(320, Math.min(1920, Math.round(Number(width) || 1280)));
|
|
1079
1559
|
s.height = Math.max(240, Math.min(1200, Math.round(Number(height) || 800)));
|
|
1080
1560
|
await s.page.setViewport({ width: s.width, height: s.height });
|
|
1561
|
+
// A viewer waiting for a picture now needs one of the new size.
|
|
1562
|
+
wakeViewers(s);
|
|
1081
1563
|
return { width: s.width, height: s.height, sizing: s.sizing };
|
|
1082
1564
|
});
|
|
1083
1565
|
}
|
|
1084
1566
|
|
|
1085
1567
|
// ── Routes ────────────────────────────────────────────────────────────────
|
|
1086
1568
|
|
|
1569
|
+
// The single-browser workspace's owner key is guarded exactly like the pane
|
|
1570
|
+
// workspace's: the same proof, the same refusals, in one place.
|
|
1087
1571
|
app.get('/api/browser/bootstrap', route(async (req, res) => {
|
|
1088
|
-
|
|
1089
|
-
if (
|
|
1090
|
-
const site = req.get('Sec-Fetch-Site');
|
|
1091
|
-
if (site && site !== 'same-origin' && site !== 'none') deny('Owner page required.');
|
|
1572
|
+
const refused = ownerPageRefusal(req);
|
|
1573
|
+
if (refused) deny(refused.body.error, refused.status, refused.body);
|
|
1092
1574
|
res.json({ key: humanKey });
|
|
1093
1575
|
}));
|
|
1094
1576
|
|
|
@@ -1147,7 +1629,8 @@ function installBrowserWorkspace(app) {
|
|
|
1147
1629
|
|
|
1148
1630
|
app.get('/api/browser/:id/snapshot', route(async (req, res) => {
|
|
1149
1631
|
session(req); // Permission check before the expensive capture.
|
|
1150
|
-
const snapshot = await snapshotTab(req.params.id);
|
|
1632
|
+
const snapshot = await snapshotTab(req.params.id, viewerRequest(req, res));
|
|
1633
|
+
if (!snapshot) return; // The viewer went away while its request waited.
|
|
1151
1634
|
session(req); // Recheck grants after asynchronous work.
|
|
1152
1635
|
res.json(snapshot);
|
|
1153
1636
|
}));
|
|
@@ -1213,11 +1696,13 @@ function installBrowserWorkspace(app) {
|
|
|
1213
1696
|
const s = session(req);
|
|
1214
1697
|
clearTimeout(s.dialogTimer);
|
|
1215
1698
|
sessions.delete(s.id);
|
|
1699
|
+
settlePictures(s);
|
|
1216
1700
|
await s.context.close();
|
|
1217
1701
|
res.json({ closed: true });
|
|
1218
1702
|
}));
|
|
1219
1703
|
|
|
1220
1704
|
const closeAll = async () => {
|
|
1705
|
+
for (const s of sessions.values()) settlePictures(s);
|
|
1221
1706
|
sessions.clear();
|
|
1222
1707
|
if (browserPromise) await (await browserPromise).close();
|
|
1223
1708
|
};
|
|
@@ -1226,6 +1711,7 @@ function installBrowserWorkspace(app) {
|
|
|
1226
1711
|
closeAll,
|
|
1227
1712
|
create: createTab,
|
|
1228
1713
|
snapshot: snapshotTab,
|
|
1714
|
+
viewerRequest,
|
|
1229
1715
|
observe: observeTab,
|
|
1230
1716
|
elements: elementsTab,
|
|
1231
1717
|
content: contentTab,
|
|
@@ -1235,6 +1721,7 @@ function installBrowserWorkspace(app) {
|
|
|
1235
1721
|
sizing: sizingTab,
|
|
1236
1722
|
close: closeTabInternal,
|
|
1237
1723
|
get: getTab,
|
|
1724
|
+
grant: grantTab,
|
|
1238
1725
|
setViewport: setViewportTab,
|
|
1239
1726
|
respondDialog,
|
|
1240
1727
|
describe,
|