|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import { EventEmitter } from "node:events" |
| 3 | +import { createServer } from "node:http" |
| 4 | +import type { AddressInfo } from "node:net" |
| 5 | +import { test } from "node:test" |
| 6 | +import { WebSocketServer } from "ws" |
| 7 | +import { streamBrowser } from "./browser-screencast.mjs" |
| 8 | + |
| 9 | +/** A stand-in for Chromium's debugging endpoint: a tab list, and per-tab sockets that record commands. */ |
| 10 | +async function fakeChromium() { |
| 11 | + const commands: { tab: string; method: string; params: Record<string, unknown> }[] = [] |
| 12 | + const tabs = new Map<string, import("ws").WebSocket>() |
| 13 | + let list: { id: string; url: string; title: string }[] = [] |
| 14 | + const server = createServer((request, response) => { |
| 15 | + const { port } = server.address() as AddressInfo |
| 16 | + response.setHeader("content-type", "application/json") |
| 17 | + response.end(JSON.stringify(list.map(tab => ({ ...tab, type: "page", webSocketDebuggerUrl: `ws://127.0.0.1:${port}/devtools/page/${tab.id}` })))) |
| 18 | + }) |
| 19 | + const sockets = new WebSocketServer({ server }) |
| 20 | + sockets.on("connection", (socket, request) => { |
| 21 | + const tab = request.url!.split("/").pop()! |
| 22 | + tabs.set(tab, socket) |
| 23 | + socket.on("message", data => { |
| 24 | + const { id, method, params } = JSON.parse(String(data)) |
| 25 | + commands.push({ tab, method, params }) |
| 26 | + socket.send(JSON.stringify({ id, result: {} })) |
| 27 | + }) |
| 28 | + }) |
| 29 | + await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve)) |
| 30 | + const { port } = server.address() as AddressInfo |
| 31 | + return { |
| 32 | + endpoint: `http://127.0.0.1:${port}`, |
| 33 | + commands, |
| 34 | + show(next: typeof list) { list = next }, |
| 35 | + frame(tab: string, sessionId: number) { |
| 36 | + tabs.get(tab)!.send(JSON.stringify({ method: "Page.screencastFrame", params: { data: Buffer.from(`jpeg-${sessionId}`).toString("base64"), sessionId, metadata: {} } })) |
| 37 | + }, |
| 38 | + close() { sockets.close(); server.close() }, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function fakeViewer() { |
| 43 | + const sent: (string | Buffer)[] = [] |
| 44 | + return Object.assign(new EventEmitter(), { readyState: 1, sent, send(message: string | Buffer) { sent.push(message) } }) |
| 45 | +} |
| 46 | + |
| 47 | +const until = async (check: () => boolean) => { |
| 48 | + for (let attempt = 0; attempt < 100 && !check(); attempt++) await new Promise(resolve => setTimeout(resolve, 20)) |
| 49 | + assert.ok(check()) |
| 50 | +} |
| 51 | + |
| 52 | +test("streams the front tab's frames and pages, acknowledging only frames the viewer drew", async () => { |
| 53 | + const chromium = await fakeChromium() |
| 54 | + const viewer = fakeViewer() |
| 55 | + chromium.show([{ id: "a", url: "https://example.com/", title: "Example" }, { id: "b", url: "https://example.org/", title: "Other" }]) |
| 56 | + const stop = streamBrowser(viewer, { endpoint: chromium.endpoint, pollMs: 20 }) |
| 57 | + try { |
| 58 | + await until(() => chromium.commands.some(command => command.tab === "a" && command.method === "Page.startScreencast")) |
| 59 | + assert.deepEqual(JSON.parse(String(viewer.sent[0])), { type: "page", url: "https://example.com/", title: "Example" }) |
| 60 | + |
| 61 | + chromium.frame("a", 7) |
| 62 | + chromium.frame("a", 8) |
| 63 | + await until(() => viewer.sent.filter(Buffer.isBuffer).length === 2) |
| 64 | + assert.equal(String(viewer.sent.find(Buffer.isBuffer)), "jpeg-7") |
| 65 | + assert.equal(chromium.commands.filter(command => command.method === "Page.screencastFrameAck").length, 0) |
| 66 | + |
| 67 | + viewer.emit("message", Buffer.from("ack"), false) |
| 68 | + await until(() => chromium.commands.some(command => command.method === "Page.screencastFrameAck")) |
| 69 | + assert.deepEqual(chromium.commands.filter(command => command.method === "Page.screencastFrameAck").map(command => command.params.sessionId), [7]) |
| 70 | + |
| 71 | + // Bringing another tab to the front moves the stream to it. |
| 72 | + chromium.show([{ id: "b", url: "https://example.org/", title: "Other" }, { id: "a", url: "https://example.com/", title: "Example" }]) |
| 73 | + await until(() => chromium.commands.some(command => command.tab === "b" && command.method === "Page.startScreencast")) |
| 74 | + await until(() => viewer.sent.some(message => typeof message === "string" && JSON.parse(message).url === "https://example.org/")) |
| 75 | + } finally { stop(); chromium.close() } |
| 76 | +}) |
| 77 | + |
| 78 | +test("anything but an acknowledgement ends the stream, so a viewer cannot send input", async () => { |
| 79 | + const chromium = await fakeChromium() |
| 80 | + const viewer = fakeViewer() |
| 81 | + let closed = false |
| 82 | + chromium.show([{ id: "a", url: "https://example.com/", title: "Example" }]) |
| 83 | + streamBrowser(viewer, { endpoint: chromium.endpoint, pollMs: 20, onClose: () => { closed = true } }) |
| 84 | + try { |
| 85 | + await until(() => chromium.commands.some(command => command.method === "Page.startScreencast")) |
| 86 | + viewer.emit("message", Buffer.from('{"method":"Input.dispatchMouseEvent"}'), true) |
| 87 | + assert.equal(closed, true) |
| 88 | + const sent = chromium.commands.length |
| 89 | + await new Promise(resolve => setTimeout(resolve, 100)) |
| 90 | + assert.equal(chromium.commands.length, sent) |
| 91 | + } finally { chromium.close() } |
| 92 | +}) |
| 93 | + |
| 94 | +test("refuses a debugging address that isn't Chromium's own loopback endpoint", async () => { |
| 95 | + const viewer = fakeViewer() |
| 96 | + const server = createServer((_request, response) => { |
| 97 | + response.setHeader("content-type", "application/json") |
| 98 | + response.end(JSON.stringify([{ id: "a", type: "page", url: "https://example.com/", title: "Example", webSocketDebuggerUrl: "ws://attacker.example:9222/devtools/page/a" }])) |
| 99 | + }) |
| 100 | + await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve)) |
| 101 | + const { port } = server.address() as AddressInfo |
| 102 | + const stop = streamBrowser(viewer, { endpoint: `http://127.0.0.1:${port}`, pollMs: 20 }) |
| 103 | + try { |
| 104 | + await new Promise(resolve => setTimeout(resolve, 150)) |
| 105 | + assert.equal(viewer.sent.length, 0) |
| 106 | + } finally { stop(); server.close() } |
| 107 | +}) |
0 commit comments