sindresorhus/clipboardy

windows wsl2 is slow with windows fallback binaries

achan-godaddy opened this issue · 2 comments

Not sure what has changed but unfortunately the binaries from win-clipboard are slower now, taking more than a few seconds where in the past it was much faster.

Perhaps we can use the os.release >= '5.10.60.1-microsoft-standard-WSL2' or just WSL2 to know when to use alternative binaries since these built in's should be supported on systems with wsl2.

The equivalent to the mac utils pbpaste in wsl/windows 10+ is:

#!/bin/bash
powershell.exe Get-Clipboard | sed 's/\r$//' | sed -z '$ s/\n$//'

We probably want to skip the sed to use the text as is.

and pbcopy (although clip.exe could may also be used directly without requiring tee that was more to emulate pbcopy's I believe)

#!/bin/bash
tee <&0 | clip.exe

Unfortunately can't submit a PR for this now but if this sounds good I may try to pick this up another time.

Xiot commented

I just noticed that its taking about 20s for the call to write to resolve on WSL2 for me.
Haven't dug too deep into it yet

Xiot commented

The way that I got around this was to pipe the data that I wanted to clip.exe directly.

const isWsl = require('is-wsl');
function copy(value) {
  if (!isWsl) {
    const clipboard = require('clipboardy');
    return clipboard.write(value);
  }

  return new Promise((resolve, reject) => {
    const child = require('child_process').exec(`echo -n '${value}' | clip.exe`);
    child.on('close', (code, sig) => {
      if (code === 0) {
        resolve();
      } else {
        reject(sig);
      }
    });
  });
}

There's probably a much better way to pipe the data and ensure that value is escaped properly, but this works in my case for now.