@mayurajs/browser-viewer 0.0.0-stage → 1.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 ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,8 @@
1
+ Mayura
2
+ Copyright 2026 The Mayura Authors
3
+
4
+ This product includes software developed by the Mayura contributors.
5
+
6
+ Third-party dependencies retain their own copyright and license terms. Exact
7
+ resolved versions are recorded in pnpm-lock.yaml and reviewed by the release
8
+ artifact and dependency qualification procedures.
package/README.md CHANGED
@@ -1,3 +1,70 @@
1
- # Temporary Holding Version
1
+ # @mayurajs/browser-viewer
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A live viewer for `mayura/browser` browsers: a person watches what an agent's browser shows, and with a link that
4
+ allows it, uses it too. The active tab streams as JPEG frames over the browser's CDP connection (a screencast),
5
+ following the agent from tab to tab, to a page that needs nothing but an image tag. No dependency; any browser other
6
+ clients can join (local, `cdpBrowsers`, and most hosted providers), whatever its provider's own live view.
7
+
8
+ ```bash
9
+ npm install mayura @mayurajs/browser-viewer
10
+ ```
11
+
12
+ ```ts
13
+ import { createBrowsers } from 'mayura/browser';
14
+ import { localBrowsers } from 'mayura/browser/local';
15
+ import { serveBrowserViewer } from '@mayurajs/browser-viewer';
16
+
17
+ const browsers = createBrowsers(localBrowsers(), { maxBrowsers: 1, maxLifetimeMs: 30 * 60_000, origins: ['https://example.com'] });
18
+ const browser = await browsers.open();
19
+ const viewer = await serveBrowserViewer(); // http://127.0.0.1:<port>/, this machine only
20
+ const link = await viewer.share(browser); // view only, for an hour
21
+ console.log(link.url); // a secret: whoever has it can watch
22
+ const takeover = await viewer.share(browser, { interact: true, expiresInMs: 10 * 60_000 });
23
+ ```
24
+
25
+ - **Nothing is shown unless shared.** `share(browser)` makes a link with a 256-bit token in it; it stops working when
26
+ it expires (1 hour by default, at most 24), when `revoke()` is called, or when the browser ends, and whatever was
27
+ being watched through it ends then too. Links are kept by the token's hash.
28
+ - **Watching is the default; using is opt-in.** With `interact: true` the page sends the person's clicks, scrolling,
29
+ keys and pasted text to the browser, as input to the page shown. Everything the person does stays within the
30
+ browser's origins, as everything the agent does. Input is only taken as JSON from the viewer's own page (never
31
+ across sites), at most `maxInputPerSecond` (60) a link, 4 KiB each.
32
+ - **Bounded.** `maxViewersPerShare` (2) people watch a link at once, each over a CDP connection of their own;
33
+ `maxShares` (16) links are open at once. Frames come no faster than `maxFps` (5), and only as fast as each viewer
34
+ takes them; `quality` (60), `maxWidth` by `maxHeight` (1,280 by 800) and `maxFrameBytes` (2 MiB) bound their size.
35
+ - **The page** is served with `no-store`, `no-referrer` and a CSP allowing nothing but its own stream, input and
36
+ nonce'd script; `frameAncestors` lets your app's origins show it in a frame (none by default).
37
+
38
+ ## On your own server, on any runtime
39
+
40
+ `browserViewer(options)` is the same viewer as web-standard request handling: mount `handle(request)` under
41
+ `basePath` (it answers `<basePath>v/...` and returns undefined for anything else), and give people
42
+ `share(browser).path` on your origin. Behind your own authentication, the link's token is a second lock, not the
43
+ only one.
44
+
45
+ ```ts
46
+ import { browserViewer } from '@mayurajs/browser-viewer';
47
+
48
+ const viewer = browserViewer({ basePath: '/viewer/', frameAncestors: ['https://app.example'] });
49
+ // In your fetch handler:
50
+ const response = await viewer.handle(request);
51
+ if (response) return response;
52
+ ```
53
+
54
+ `serveBrowserViewer` (Node) listens on `127.0.0.1` unless given `host` with `remote: true`: links then travel in the
55
+ clear unless TLS is in front, and `publicUrl` (an https origin) is the address the links use.
56
+
57
+ ## The screencast alone
58
+
59
+ `browserScreencast(browser, { interact })` gives the frames themselves, an async iterable of JPEGs with the page's
60
+ size and tab, and `input(event)` for interactive ones, to show them however you like.
61
+
62
+ ## Browsers it cannot join
63
+
64
+ Each connection to Browserless starts a browser of its own, so its browsers give other clients no endpoint, and the
65
+ viewer refuses them. Most hosted providers also have a live view of their own, given as `browser.liveViewUrl`.
66
+
67
+ Options: `quality`, `maxWidth`, `maxHeight`, `maxFps`, `maxFrameBytes`, `webSocket`, `basePath`, `frameAncestors`,
68
+ `maxViewersPerShare`, `maxShares`, `maxInputPerSecond`; `serveBrowserViewer` adds `host`, `port`, `remote` and
69
+ `publicUrl`. Verified on the Chrome and Edge installed here. See the
70
+ [browser guide](https://mayurajs.com/docs/guides/browsers/). Apache-2.0.
@@ -0,0 +1,7 @@
1
+ export { browserScreencast } from './screencast.js';
2
+ export type { Screencast, ScreencastFrame, ScreencastOptions, ViewerInput } from './screencast.js';
3
+ export { browserViewer } from './viewer.js';
4
+ export type { BrowserViewer, BrowserViewerOptions, ViewerShare, ViewerShareOptions } from './viewer.js';
5
+ export { serveBrowserViewer } from './serve.js';
6
+ export type { ServeBrowserViewerOptions, ServedBrowserViewer } from './serve.js';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,YAAY,EAAE,UAAU,EAAE,eAAe,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnG,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,YAAY,EAAE,aAAa,EAAE,oBAAoB,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACxG,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAChD,YAAY,EAAE,yBAAyB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { browserScreencast } from './screencast.js';
2
+ export { browserViewer } from './viewer.js';
3
+ export { serveBrowserViewer } from './serve.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC","sourcesContent":["export { browserScreencast } from './screencast.js';\nexport type { Screencast, ScreencastFrame, ScreencastOptions, ViewerInput } from './screencast.js';\nexport { browserViewer } from './viewer.js';\nexport type { BrowserViewer, BrowserViewerOptions, ViewerShare, ViewerShareOptions } from './viewer.js';\nexport { serveBrowserViewer } from './serve.js';\nexport type { ServeBrowserViewerOptions, ServedBrowserViewer } from './serve.js';\n"]}
package/dist/page.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export declare function viewerPage({ interact, nonce }: {
2
+ readonly interact: boolean;
3
+ readonly nonce: string;
4
+ }): string;
5
+ //# sourceMappingURL=page.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../src/page.ts"],"names":[],"mappings":"AAsBA,wBAAgB,UAAU,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE;IAAE,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAkB9G"}
package/dist/page.js ADDED
@@ -0,0 +1,41 @@
1
+ // The viewer page: the stream as an image, and with interact, the person's mouse and keys sent back as input. Its
2
+ // script and style run only by the page's nonce; it reaches nothing but its own stream and input.
3
+ const script = `
4
+ const view = document.getElementById('view'); const state = document.getElementById('state');
5
+ const send = event => fetch('input', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(event) }).catch(() => undefined);
6
+ const at = event => { const box = view.getBoundingClientRect(); return { x: Math.min(1, Math.max(0, (event.clientX - box.left) / box.width)), y: Math.min(1, Math.max(0, (event.clientY - box.top) / box.height)) }; };
7
+ const mods = event => (event.altKey ? 1 : 0) | (event.ctrlKey ? 2 : 0) | (event.metaKey ? 4 : 0) | (event.shiftKey ? 8 : 0);
8
+ const button = event => ['left', 'middle', 'right'][event.button] ?? 'left';
9
+ let moved = 0;
10
+ view.addEventListener('mousedown', event => { event.preventDefault(); view.focus(); send({ type: 'mouse', action: 'down', ...at(event), button: button(event), clicks: Math.min(3, Math.max(1, event.detail)), modifiers: mods(event) }); });
11
+ view.addEventListener('mouseup', event => { event.preventDefault(); send({ type: 'mouse', action: 'up', ...at(event), button: button(event), clicks: Math.min(3, Math.max(1, event.detail)), modifiers: mods(event) }); });
12
+ view.addEventListener('mousemove', event => { const now = Date.now(); if (now - moved < 50) return; moved = now; send({ type: 'mouse', action: 'move', ...at(event), modifiers: mods(event) }); });
13
+ view.addEventListener('wheel', event => { event.preventDefault(); send({ type: 'wheel', ...at(event), dx: Math.round(event.deltaX), dy: Math.round(event.deltaY) }); }, { passive: false });
14
+ view.addEventListener('contextmenu', event => event.preventDefault());
15
+ for (const action of ['down', 'up']) view.addEventListener('key' + action, event => {
16
+ if (event.isComposing) return;
17
+ event.preventDefault(); send({ type: 'key', action, key: event.key, modifiers: mods(event) });
18
+ });
19
+ view.addEventListener('paste', event => { const text = event.clipboardData?.getData('text/plain'); if (text) { event.preventDefault(); send({ type: 'text', text: text.slice(0, 1000) }); } });
20
+ state.textContent = 'Live: you can use this browser. Click the page to type into it.';
21
+ `;
22
+ export function viewerPage({ interact, nonce }) {
23
+ return `<!doctype html>
24
+ <html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="referrer" content="no-referrer">
25
+ <title>Browser viewer</title>
26
+ <style nonce="${nonce}">
27
+ :root { color-scheme: light dark; --bg: #f6f6f4; --fg: #1d1d1b; --muted: #6b6b66; --edge: #d9d9d4; }
28
+ @media (prefers-color-scheme: dark) { :root { --bg: #151514; --fg: #ecece8; --muted: #9a9a94; --edge: #33332f; } }
29
+ * { box-sizing: border-box; }
30
+ body { margin: 0; min-height: 100vh; display: flex; flex-direction: column; background: var(--bg); color: var(--fg); font: 14px/1.4 system-ui, sans-serif; }
31
+ header { padding: 8px 16px; color: var(--muted); border-bottom: 1px solid var(--edge); }
32
+ main { flex: 1; display: flex; align-items: center; justify-content: center; padding: 16px; }
33
+ img { max-width: 100%; max-height: calc(100vh - 80px); border: 1px solid var(--edge); background: #fff; outline: none; }
34
+ img:focus-visible { outline: 2px solid #3b6fd8; }
35
+ </style></head>
36
+ <body><header id="state">Live: view only.</header>
37
+ <main><img id="view" src="stream" alt="The browser's page, live"${interact ? ' tabindex="0"' : ''}></main>
38
+ ${interact ? `<script nonce="${nonce}">${script}</script>` : ''}
39
+ </body></html>`;
40
+ }
41
+ //# sourceMappingURL=page.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page.js","sourceRoot":"","sources":["../src/page.ts"],"names":[],"mappings":"AAAA,kHAAkH;AAClH,kGAAkG;AAClG,MAAM,MAAM,GAAG;;;;;;;;;;;;;;;;;;CAkBd,CAAC;AAEF,MAAM,UAAU,UAAU,CAAC,EAAE,QAAQ,EAAE,KAAK,EAA0D;IACpG,OAAO;;;gBAGO,KAAK;;;;;;;;;;;kEAW6C,QAAQ,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE;EAC/F,QAAQ,CAAC,CAAC,CAAC,kBAAkB,KAAK,KAAK,MAAM,WAAW,CAAC,CAAC,CAAC,EAAE;eAChD,CAAC;AAChB,CAAC","sourcesContent":["// The viewer page: the stream as an image, and with interact, the person's mouse and keys sent back as input. Its\n// script and style run only by the page's nonce; it reaches nothing but its own stream and input.\nconst script = `\nconst view = document.getElementById('view'); const state = document.getElementById('state');\nconst send = event => fetch('input', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(event) }).catch(() => undefined);\nconst at = event => { const box = view.getBoundingClientRect(); return { x: Math.min(1, Math.max(0, (event.clientX - box.left) / box.width)), y: Math.min(1, Math.max(0, (event.clientY - box.top) / box.height)) }; };\nconst mods = event => (event.altKey ? 1 : 0) | (event.ctrlKey ? 2 : 0) | (event.metaKey ? 4 : 0) | (event.shiftKey ? 8 : 0);\nconst button = event => ['left', 'middle', 'right'][event.button] ?? 'left';\nlet moved = 0;\nview.addEventListener('mousedown', event => { event.preventDefault(); view.focus(); send({ type: 'mouse', action: 'down', ...at(event), button: button(event), clicks: Math.min(3, Math.max(1, event.detail)), modifiers: mods(event) }); });\nview.addEventListener('mouseup', event => { event.preventDefault(); send({ type: 'mouse', action: 'up', ...at(event), button: button(event), clicks: Math.min(3, Math.max(1, event.detail)), modifiers: mods(event) }); });\nview.addEventListener('mousemove', event => { const now = Date.now(); if (now - moved < 50) return; moved = now; send({ type: 'mouse', action: 'move', ...at(event), modifiers: mods(event) }); });\nview.addEventListener('wheel', event => { event.preventDefault(); send({ type: 'wheel', ...at(event), dx: Math.round(event.deltaX), dy: Math.round(event.deltaY) }); }, { passive: false });\nview.addEventListener('contextmenu', event => event.preventDefault());\nfor (const action of ['down', 'up']) view.addEventListener('key' + action, event => {\n if (event.isComposing) return;\n event.preventDefault(); send({ type: 'key', action, key: event.key, modifiers: mods(event) });\n});\nview.addEventListener('paste', event => { const text = event.clipboardData?.getData('text/plain'); if (text) { event.preventDefault(); send({ type: 'text', text: text.slice(0, 1000) }); } });\nstate.textContent = 'Live: you can use this browser. Click the page to type into it.';\n`;\n\nexport function viewerPage({ interact, nonce }: { readonly interact: boolean; readonly nonce: string }): string {\n return `<!doctype html>\n<html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><meta name=\"referrer\" content=\"no-referrer\">\n<title>Browser viewer</title>\n<style nonce=\"${nonce}\">\n:root { color-scheme: light dark; --bg: #f6f6f4; --fg: #1d1d1b; --muted: #6b6b66; --edge: #d9d9d4; }\n@media (prefers-color-scheme: dark) { :root { --bg: #151514; --fg: #ecece8; --muted: #9a9a94; --edge: #33332f; } }\n* { box-sizing: border-box; }\nbody { margin: 0; min-height: 100vh; display: flex; flex-direction: column; background: var(--bg); color: var(--fg); font: 14px/1.4 system-ui, sans-serif; }\nheader { padding: 8px 16px; color: var(--muted); border-bottom: 1px solid var(--edge); }\nmain { flex: 1; display: flex; align-items: center; justify-content: center; padding: 16px; }\nimg { max-width: 100%; max-height: calc(100vh - 80px); border: 1px solid var(--edge); background: #fff; outline: none; }\nimg:focus-visible { outline: 2px solid #3b6fd8; }\n</style></head>\n<body><header id=\"state\">Live: view only.</header>\n<main><img id=\"view\" src=\"stream\" alt=\"The browser's page, live\"${interact ? ' tabindex=\"0\"' : ''}></main>\n${interact ? `<script nonce=\"${nonce}\">${script}</script>` : ''}\n</body></html>`;\n}\n"]}
@@ -0,0 +1,73 @@
1
+ import { type Browser, type CdpSocketFactory } from 'mayura/browser';
2
+ export interface ScreencastOptions {
3
+ /** JPEG quality, 1 to 100; 60 by default. */
4
+ readonly quality?: number;
5
+ /** The widest frame, in pixels; 1,280 by default (320 to 3,840). */
6
+ readonly maxWidth?: number;
7
+ /** The tallest frame, in pixels; 800 by default (240 to 2,160). */
8
+ readonly maxHeight?: number;
9
+ /** The most frames a second; 5 by default (1 to 30). */
10
+ readonly maxFps?: number;
11
+ /** Frames larger than this are skipped, in bytes; 2 MiB by default. */
12
+ readonly maxFrameBytes?: number;
13
+ /** Lets `input` use the page, as a person would: off by default. */
14
+ readonly interact?: boolean;
15
+ /** For runtimes whose WebSocket cannot send the headers a browser's CDP connection needs. */
16
+ readonly webSocket?: CdpSocketFactory;
17
+ /** Ends the screencast. */
18
+ readonly signal?: AbortSignal;
19
+ }
20
+ export interface ScreencastFrame {
21
+ /** A JPEG of the page as shown. */
22
+ readonly data: Uint8Array;
23
+ readonly mediaType: 'image/jpeg';
24
+ /** The page's size in CSS pixels, which `input` coordinates are fractions of. */
25
+ readonly width: number;
26
+ readonly height: number;
27
+ /** The tab shown, as `browser.tabs()` names it. */
28
+ readonly tab: string;
29
+ }
30
+ /** What a person does in the viewer. Coordinates are fractions of the frame, 0 to 1 from the top left. */
31
+ export type ViewerInput = {
32
+ readonly type: 'mouse';
33
+ readonly action: 'down' | 'up' | 'move';
34
+ readonly x: number;
35
+ readonly y: number;
36
+ readonly button?: 'left' | 'middle' | 'right';
37
+ readonly clicks?: number;
38
+ readonly modifiers?: number;
39
+ } | {
40
+ readonly type: 'wheel';
41
+ readonly x: number;
42
+ readonly y: number;
43
+ readonly dx: number;
44
+ readonly dy: number;
45
+ } | {
46
+ readonly type: 'key';
47
+ readonly action: 'down' | 'up';
48
+ readonly key: string;
49
+ readonly modifiers?: number;
50
+ } | {
51
+ readonly type: 'text';
52
+ readonly text: string;
53
+ };
54
+ /** The frames of a browser's active tab as it changes, following the tab the browser has active. */
55
+ export interface Screencast extends AsyncIterable<ScreencastFrame> {
56
+ /** The next frame, waiting for one; undefined once the screencast ended. */
57
+ next(): Promise<ScreencastFrame | undefined>;
58
+ /** Uses the page shown, for screencasts with `interact`. */
59
+ input(event: ViewerInput, options?: {
60
+ readonly signal?: AbortSignal;
61
+ }): Promise<void>;
62
+ /** True once closed, or once its browser or connection ended. */
63
+ readonly ended: boolean;
64
+ close(): void;
65
+ }
66
+ /**
67
+ * A screencast of a `mayura/browser` browser: the active tab's frames as JPEGs, over a CDP connection of its own, and,
68
+ * with `interact`, a way for a person to use the page. Frames come no faster than `maxFps`, and only as fast as they
69
+ * are taken. The browser's origins hold for whatever a person does. It needs a browser other clients can join
70
+ * (`browser.cdp`).
71
+ */
72
+ export declare function browserScreencast(browser: Browser, options?: ScreencastOptions): Promise<Screencast>;
73
+ //# sourceMappingURL=screencast.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"screencast.d.ts","sourceRoot":"","sources":["../src/screencast.ts"],"names":[],"mappings":"AACA,OAAO,EAAc,KAAK,OAAO,EAAsB,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAErG,MAAM,WAAW,iBAAiB;IAChC,6CAA6C;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