Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/server/clipboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,28 @@ describe("Clipboard helper", () => {
expect(text).toBe("windows clipboard text")
expect(spawn).toHaveBeenCalledWith(
"powershell.exe",
expect.arrayContaining(["Get-Clipboard -Raw"]),
expect.any(Object),
expect.arrayContaining([
expect.stringMatching(
/\[Console\]::OutputEncoding\s*=\s*\[System\.Text\.Encoding\]::UTF8;\s*Get-Clipboard -Raw/,
Comment thread
Namthenikki marked this conversation as resolved.
Resolved
),
]),
expect.objectContaining({ windowsHide: true }),
)
})

it("should keep a character split across stdout chunks", async () => {
vi.mocked(os.platform).mockReturnValue("win32")
const proc = createMockProcess({})
vi.mocked(spawn).mockReturnValue(proc)
const rocket = Buffer.from("🚀")

const pending = getSystemClipboard()
proc.stdout.emit("data", rocket.subarray(0, 2))
proc.stdout.emit("data", rocket.subarray(2))

await expect(pending).resolves.toBe("🚀")
})

it("should read clipboard on linux using fallback when first command fails", async () => {
vi.mocked(os.platform).mockReturnValue("linux")
const origEnv = process.env.XDG_SESSION_TYPE
Expand Down
13 changes: 8 additions & 5 deletions src/server/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,20 @@ function spawnWithStdin(
*/
function spawnCaptureOutput(cmd: string, args: string[]): Promise<string> {
return new Promise((resolve, reject) => {
const child = spawn(cmd, args, { stdio: ["ignore", "pipe", "ignore"] })
let output = ""
const child = spawn(cmd, args, {
stdio: ["ignore", "pipe", "ignore"],
windowsHide: true,
})
const chunks: Buffer[] = []

child.stdout.on("data", (chunk: Buffer) => {
output += chunk.toString("utf-8")
chunks.push(chunk)
})

child.on("error", reject)

child.on("close", (code) => {
if (code === 0) resolve(output)
if (code === 0) resolve(Buffer.concat(chunks).toString("utf-8"))
else reject(new Error(`${cmd} exited with code ${code}`))
})
})
Expand All @@ -64,7 +67,7 @@ export async function getSystemClipboard(): Promise<string> {
const text = await spawnCaptureOutput("powershell.exe", [
"-NoProfile",
"-Command",
"Get-Clipboard -Raw",
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-Clipboard -Raw",
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
])
return text.replace(/\r?\n$/, "")
}
Expand Down
Loading