ssh-client-wrapper 3.3.3 → 3.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/rsyncExec.js CHANGED
@@ -6,7 +6,7 @@ import { glob, hasMagic } from "glob";
6
6
  import { connect, retryWrapper, sshExec } from "./sshExec.js";
7
7
  import { fork } from "./fork.js";
8
8
 
9
- import { sshCmd, rsyncCmd, getSshOption, resolveRetryableExitCodes } from "./util.js";
9
+ import { sshCmd, getRsyncCmd, getSshOption, resolveRetryableExitCodes } from "./util.js";
10
10
  import { expandRemotePath, formatRemoteArg, hasLiteralRemoteArgs, shellQuote } from "./remotePath.js";
11
11
 
12
12
  const debug = Debug("sshClientWrapper:debug:rsyncExec");
@@ -14,7 +14,7 @@ const debugVerbose = Debug("sshClientWrapper:verbose:rsyncExec");
14
14
 
15
15
  export async function checkRsyncVersion(timeout) {
16
16
  const rt = {};
17
- await fork(null, rsyncCmd, ["--version"], timeout, (output)=>{
17
+ await fork(null, getRsyncCmd(), ["--version"], timeout, (output)=>{
18
18
  const result = /^rsync *version *(\d+)\.(\d+)\.(\d+).*$/m.exec(output);
19
19
 
20
20
  if (result) {
@@ -91,9 +91,9 @@ export async function send(hostInfo, argSrc, dst, rsyncOpt, timeout, retryableEx
91
91
  await sshExec(hostInfo, `mkdir -p ${shellQuote(remoteDir)}`, 0, debugVerbose);
92
92
  }
93
93
  debug(`send ${src} to ${hostInfo.host}:${remoteDst}`);
94
- debugVerbose(`send: ${rsyncCmd} ${args.join(" ")}`);
94
+ debugVerbose(`send: ${getRsyncCmd()} ${args.join(" ")}`);
95
95
  const exitCodes = resolveRetryableExitCodes(hostInfo, retryableExitCodes, replaceRetryableExitCodes);
96
- return retryWrapper(fork.bind(null, hostInfo, rsyncCmd, args, timeout, null, exitCodes), hostInfo);
96
+ return retryWrapper(fork.bind(null, hostInfo, getRsyncCmd(), args, timeout, null, exitCodes), hostInfo);
97
97
  }
98
98
 
99
99
  /**
@@ -147,9 +147,9 @@ export async function recv(hostInfo, argSrc, dst, rsyncOpt, timeout, retryableEx
147
147
  await fs.mkdir(dstDir, { recursive: true });
148
148
 
149
149
  debug(`recv ${src.join(" ")} to ${dst}`);
150
- debugVerbose(`recv: ${rsyncCmd} ${args.join(" ")}`);
150
+ debugVerbose(`recv: ${getRsyncCmd()} ${args.join(" ")}`);
151
151
  const exitCodes = resolveRetryableExitCodes(hostInfo, retryableExitCodes, replaceRetryableExitCodes);
152
- return retryWrapper(fork.bind(null, hostInfo, rsyncCmd, args, timeout, null, exitCodes), hostInfo);
152
+ return retryWrapper(fork.bind(null, hostInfo, getRsyncCmd(), args, timeout, null, exitCodes), hostInfo);
153
153
  }
154
154
 
155
155
  /**
package/lib/util.js CHANGED
@@ -10,6 +10,7 @@ const debug = Debug("sshClientWrapper:debug:util");
10
10
 
11
11
  export const sshCmd = "ssh";
12
12
  export const rsyncCmd = "rsync";
13
+ export const gokrazyRsyncCmd = "gokr-rsync";
13
14
  export const acceptableRsyncRetrunCodes = [10, 11, 12, 13, 14];
14
15
 
15
16
  /**
@@ -246,6 +247,62 @@ export const isControlMasterEnabled = (hostInfo)=>{
246
247
  return process.platform !== "win32";
247
248
  };
248
249
 
250
+ /**
251
+ * check if a command is in PATH
252
+ * @param {string} cmd - command name
253
+ * @param {object} opt - options (platform, env, isFile and isExecutable are injectable for tests)
254
+ * @param {string} opt.platform - platform name
255
+ * @param {object} opt.env - environment variables
256
+ * @param {Function} opt.isFile - function which checks if given path is a file
257
+ * @param {Function} opt.isExecutable - function which checks if given path is executable (not used on win32)
258
+ * @returns {boolean} - true if cmd is found
259
+ */
260
+ export const commandExists = (cmd, opt = {})=>{
261
+ const { platform = process.platform, env = process.env } = opt;
262
+ if (platform === "win32") {
263
+ return resolveExecutable(cmd, opt) !== cmd;
264
+ }
265
+ const isExecutable = opt.isExecutable ?? ((p)=>{
266
+ try {
267
+ fs.accessSync(p, fs.constants.X_OK);
268
+ return fs.statSync(p).isFile();
269
+ } catch {
270
+ return false;
271
+ }
272
+ });
273
+ return (env.PATH ?? "").split(":").filter(Boolean)
274
+ .some((dir)=>{
275
+ return isExecutable(path.posix.join(dir, cmd));
276
+ });
277
+ };
278
+
279
+ /**
280
+ * find the rsync command to be used
281
+ * `rsync` if it is found in PATH, otherwise `gokr-rsync` (gokrazy/rsync) if it is found, otherwise `rsync`.
282
+ * @param {object} opt - options for commandExists (for tests)
283
+ * @returns {string} - command name
284
+ */
285
+ export const findRsyncCmd = (opt = {})=>{
286
+ for (const candidate of [rsyncCmd, gokrazyRsyncCmd]) {
287
+ if (commandExists(candidate, opt)) {
288
+ debug(`use ${candidate} as rsync`);
289
+ return candidate;
290
+ }
291
+ }
292
+ return rsyncCmd;
293
+ };
294
+
295
+ let rsyncCommand = null;
296
+
297
+ /**
298
+ * the rsync command to be used (cached after the first call)
299
+ * @returns {string} - command name
300
+ */
301
+ export const getRsyncCmd = ()=>{
302
+ rsyncCommand ??= findRsyncCmd();
303
+ return rsyncCommand;
304
+ };
305
+
249
306
  /**
250
307
  * return ssh option with some hard corded options
251
308
  * @param {object} hostInfo - host information object defined in index.js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ssh-client-wrapper",
3
- "version": "3.3.3",
3
+ "version": "3.4.1",
4
4
  "description": "wrapper library for open ssh client and rsync",
5
5
  "keywords": [
6
6
  "ssh",