-
-
Notifications
You must be signed in to change notification settings - Fork 77
Draft PR for HTTP based copy and paste functionality #368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| import type { IncomingMessage, ServerResponse } from "node:http" | ||||||||||||||||||||||||||||||||||||||||||||
| import { spawnSync } from "node:child_process" | ||||||||||||||||||||||||||||||||||||||||||||
| import os from "node:os" | ||||||||||||||||||||||||||||||||||||||||||||
| import fs from "node:fs" | ||||||||||||||||||||||||||||||||||||||||||||
| import crypto from "node:crypto" | ||||||||||||||||||||||||||||||||||||||||||||
| import logger from "../../utils/logger" | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -752,3 +754,127 @@ export async function handleWhipSignalingExchange( | |||||||||||||||||||||||||||||||||||||||||||
| }, 100) | ||||||||||||||||||||||||||||||||||||||||||||
| req.on("close", () => clearInterval(answerCheckInterval)) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| function writeHostClipboard(text: string): void { | ||||||||||||||||||||||||||||||||||||||||||||
| const platform = os.platform() | ||||||||||||||||||||||||||||||||||||||||||||
| if (platform === "darwin") { | ||||||||||||||||||||||||||||||||||||||||||||
| spawnSync("pbcopy", { input: text }) | ||||||||||||||||||||||||||||||||||||||||||||
| } else if (platform === "win32") { | ||||||||||||||||||||||||||||||||||||||||||||
| spawnSync("clip", { input: text }) | ||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||
| const procWl = spawnSync("wl-copy", { input: text }) | ||||||||||||||||||||||||||||||||||||||||||||
| if (procWl.status !== 0) { | ||||||||||||||||||||||||||||||||||||||||||||
| const procXclip = spawnSync("xclip", ["-selection", "clipboard"], { | ||||||||||||||||||||||||||||||||||||||||||||
| input: text, | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| if (procXclip.status !== 0) { | ||||||||||||||||||||||||||||||||||||||||||||
| spawnSync("xsel", ["--clipboard", "--input"], { input: text }) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| // 100 MiB — far beyond any realistic clipboard content; prevents spawnSync | ||||||||||||||||||||||||||||||||||||||||||||
| // from silently truncating stdout when the default 1 MiB maxBuffer is exceeded. | ||||||||||||||||||||||||||||||||||||||||||||
| const CLIPBOARD_MAX_BUFFER = 100 * 1024 * 1024 | ||||||||||||||||||||||||||||||||||||||||||||
| function readHostClipboard(): string { | ||||||||||||||||||||||||||||||||||||||||||||
| const platform = os.platform() | ||||||||||||||||||||||||||||||||||||||||||||
| if (platform === "darwin") { | ||||||||||||||||||||||||||||||||||||||||||||
| const proc = spawnSync("pbpaste", { | ||||||||||||||||||||||||||||||||||||||||||||
| encoding: "utf-8", | ||||||||||||||||||||||||||||||||||||||||||||
| maxBuffer: CLIPBOARD_MAX_BUFFER, | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| if (proc.error) throw proc.error | ||||||||||||||||||||||||||||||||||||||||||||
| return proc.stdout || "" | ||||||||||||||||||||||||||||||||||||||||||||
| } else if (platform === "win32") { | ||||||||||||||||||||||||||||||||||||||||||||
| const proc = spawnSync( | ||||||||||||||||||||||||||||||||||||||||||||
| "powershell", | ||||||||||||||||||||||||||||||||||||||||||||
| ["-NoProfile", "-Command", "Get-Clipboard"], | ||||||||||||||||||||||||||||||||||||||||||||
| { encoding: "utf-8", maxBuffer: CLIPBOARD_MAX_BUFFER }, | ||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||
| if (proc.error) throw proc.error | ||||||||||||||||||||||||||||||||||||||||||||
| return (proc.stdout || "").replace(/\r\n$/, "").replace(/\n$/, "") | ||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||
| const procWl = spawnSync("wl-paste", ["--no-newline"], { | ||||||||||||||||||||||||||||||||||||||||||||
| encoding: "utf-8", | ||||||||||||||||||||||||||||||||||||||||||||
| maxBuffer: CLIPBOARD_MAX_BUFFER, | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| if (procWl.status === 0 && !procWl.error) { | ||||||||||||||||||||||||||||||||||||||||||||
| return procWl.stdout || "" | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| const procXclip = spawnSync("xclip", ["-selection", "clipboard", "-o"], { | ||||||||||||||||||||||||||||||||||||||||||||
| encoding: "utf-8", | ||||||||||||||||||||||||||||||||||||||||||||
| maxBuffer: CLIPBOARD_MAX_BUFFER, | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| if (procXclip.status === 0 && !procXclip.error) { | ||||||||||||||||||||||||||||||||||||||||||||
| return procXclip.stdout || "" | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| const procXsel = spawnSync("xsel", ["--clipboard", "--output"], { | ||||||||||||||||||||||||||||||||||||||||||||
| encoding: "utf-8", | ||||||||||||||||||||||||||||||||||||||||||||
| maxBuffer: CLIPBOARD_MAX_BUFFER, | ||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||
| if (procXsel.status === 0 && !procXsel.error) { | ||||||||||||||||||||||||||||||||||||||||||||
| return procXsel.stdout || "" | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| return "" | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| export async function handleClipboardCopy( | ||||||||||||||||||||||||||||||||||||||||||||
| req: IncomingMessage, | ||||||||||||||||||||||||||||||||||||||||||||
| res: ServerResponse, | ||||||||||||||||||||||||||||||||||||||||||||
| ): Promise<void> { | ||||||||||||||||||||||||||||||||||||||||||||
| if (!requireAuth(req, res)) return | ||||||||||||||||||||||||||||||||||||||||||||
| const bodyText = await readBody(req) | ||||||||||||||||||||||||||||||||||||||||||||
| const { sessionId } = JSON.parse(bodyText || "{}") as { | ||||||||||||||||||||||||||||||||||||||||||||
| sessionId?: string | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
Arbaaz123676 marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||
| if (!sessionId) { | ||||||||||||||||||||||||||||||||||||||||||||
| json(res, 400, { error: "sessionId is required" }) | ||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| const inputPc = inputConnections.get(sessionId) | ||||||||||||||||||||||||||||||||||||||||||||
| if (!inputPc) { | ||||||||||||||||||||||||||||||||||||||||||||
| json(res, 404, { error: "Input connection not active" }) | ||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| await inputPc.handleMessage({ type: "copy" }) | ||||||||||||||||||||||||||||||||||||||||||||
| await new Promise((resolve) => setTimeout(resolve, 100)) | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+839
to
+840
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Unguarded copy-trigger + fixed 100ms delay race before reading clipboard. Two related concerns here:
🛠️ Proposed fix- await inputPc.handleMessage({ type: "copy" })
- await new Promise((resolve) => setTimeout(resolve, 100))
- try {
- const text = readHostClipboard()
- json(res, 200, { text })
- } catch (err) {
- logger.error(`Failed to read host clipboard: ${String(err)}`)
- json(res, 500, { error: "Failed to read host clipboard" })
- }
+ try {
+ await inputPc.handleMessage({ type: "copy" })
+ } catch (err) {
+ logger.error(`Failed to trigger host copy: ${String(err)}`)
+ json(res, 500, { error: "Failed to trigger host copy" })
+ return
+ }
+ try {
+ let text = ""
+ for (let i = 0; i < 5; i++) {
+ await new Promise((resolve) => setTimeout(resolve, 50))
+ text = readHostClipboard()
+ if (text) break
+ }
+ json(res, 200, { text })
+ } catch (err) {
+ logger.error(`Failed to read host clipboard: ${String(err)}`)
+ json(res, 500, { error: "Failed to read host clipboard" })
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||
| const text = readHostClipboard() | ||||||||||||||||||||||||||||||||||||||||||||
| json(res, 200, { text }) | ||||||||||||||||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||||||||||||||||
| logger.error(`Failed to read host clipboard: ${String(err)}`) | ||||||||||||||||||||||||||||||||||||||||||||
| json(res, 500, { error: "Failed to read host clipboard" }) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| export async function handleClipboardPaste( | ||||||||||||||||||||||||||||||||||||||||||||
| req: IncomingMessage, | ||||||||||||||||||||||||||||||||||||||||||||
| res: ServerResponse, | ||||||||||||||||||||||||||||||||||||||||||||
| ): Promise<void> { | ||||||||||||||||||||||||||||||||||||||||||||
| if (!requireAuth(req, res)) return | ||||||||||||||||||||||||||||||||||||||||||||
| const bodyText = await readBody(req) | ||||||||||||||||||||||||||||||||||||||||||||
| const { sessionId, text } = JSON.parse(bodyText || "{}") as { | ||||||||||||||||||||||||||||||||||||||||||||
| sessionId?: string | ||||||||||||||||||||||||||||||||||||||||||||
| text?: string | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if (!sessionId || typeof text !== "string") { | ||||||||||||||||||||||||||||||||||||||||||||
| json(res, 400, { error: "sessionId and text are required" }) | ||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| const inputPc = inputConnections.get(sessionId) | ||||||||||||||||||||||||||||||||||||||||||||
| if (!inputPc) { | ||||||||||||||||||||||||||||||||||||||||||||
| json(res, 404, { error: "Input connection not active" }) | ||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||
| writeHostClipboard(text) | ||||||||||||||||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||||||||||||||||
| logger.error(`Failed to write host clipboard (non-fatal): ${String(err)}`) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||
| await inputPc.handleMessage({ type: "text", text }) | ||||||||||||||||||||||||||||||||||||||||||||
| json(res, 200, { ok: true }) | ||||||||||||||||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||||||||||||||||
| logger.error(`Failed to inject text: ${String(err)}`) | ||||||||||||||||||||||||||||||||||||||||||||
| json(res, 500, { error: "Failed to inject pasted text" }) | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.