$ show sindresorhus/clipboardy
clipboardy is a cross-platform Node.js library that provides a simple, unified API for accessing the system clipboard (copy/paste). It supports macOS, Windows, Linux (including Wayland), WSL, Android (Termux), and modern browsers. The library offers both asynchronous and synchronous methods for reading and writing text, and additionally supports image operations on macOS via the `clipboard-image` package. The implementation automatically detects the platform at runtime and delegates to the appropriate system commands (e.g., `pbcopy`/`pbpaste` on macOS, `Set-Clipboard`/`Get-Clipboard` PowerShell cmdlets on Windows, `xsel` on Linux with a bundled fallback, `wl-copy`/`wl-paste` on Wayland). It carefully handles edge cases such as the Wayland clipboard daemon's background process hang (by redirecting stderr to a temporary file) and gracefully falls back to X11 tools when Wayland is unavailable. The code is well-tested, with a comprehensive test suite covering various content types, error handling, and platform-specific behaviors. The library is a mature, widely-used piece of infrastructure (nearly 2000 stars) that exemplifies clean, reliable Node.js package design.
The LLM identified 4 files worth highlighting for reuse:
Implements clipboard access via `wl-clipboard` tools on Wayland. The key innovation is handling `wl-copy`'s background process: it creates a temporary file for stderr to avoid the hang that would occur if pipes were used (the forked daemon holds the pipe open). It also provides a graceful fallback to the X11 (linux.js) implementation when Wayland is not available. This pattern of managing child process file descriptors to avoid blocking is a non-obvious, reusable technique.
Provides clipboard access on Linux via the `xsel` command, with a bundled fallback binary. The error handling is sophisticated: it distinguishes between 'command not found' (ENOENT) and display server errors, producing clear, actionable error messages. The fallback mechanism is cleanly abstracted, allowing the same code to attempt the system binary first and then the bundled one. This pattern is reusable for any Node.js module that needs to invoke system utilities with a fallback.
Handles Windows clipboard access using PowerShell cmdlets (`Set-Clipboard`/`Get-Clipboard`) as the primary method, with a fallback to a bundled native binary. The PowerShell commands are implemented as raw scripts that read stdin byte-by-byte to preserve newlines (including trailing newlines), avoiding common pitfalls with PowerShell's automatic line-by-line enumeration. The fallback binary is selected based on system architecture (32-bit, 64-bit, ARM64). This dual-fallback pattern is a robust solution for Windows environments where PowerShell may be restricted or unavailable.
The main entry point that selects the appropriate platform backend using a strategy pattern. It evaluates `process.platform`, `isWSL`, and `isWayland()` to choose between macOS, Windows, Linux, Wayland, WSL, Android/Termux, and browser implementations. The platform detection logic is clean, extensible, and isolated from the actual clipboard operations. This pattern is directly reusable in any cross-platform Node.js module that needs to dispatch to platform-specific implementations.