ssh-client-wrapper 3.3.2 → 3.4.0

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
  /**
@@ -107,13 +108,36 @@ const numberOptions = [
107
108
 
108
109
  const validate = ajv.compile(hostInfoSchema);
109
110
 
111
+ //CSI sequence, OSC sequence (terminated by BEL or ST) and the other two-character escape sequences
112
+ //eslint-disable-next-line no-control-regex
113
+ const reTerminalSequences = /\u001b\[[0-?]*[ -/]*[@-~]|\u001b\][^\u0007\u001b]*(?:\u0007|\u001b\\)|\u001b[@-Z\\^_]/g;
114
+
115
+ /**
116
+ * remove terminal control sequences from output of a pty
117
+ * ConPTY on Windows redraws the screen, so they surround the real output
118
+ * @param {string} str - output of a pty
119
+ * @returns {string} - str without control sequences
120
+ */
121
+ export const stripTerminalSequences = (str)=>{
122
+ return str.replace(reTerminalSequences, "");
123
+ };
124
+
110
125
  /**
111
126
  * bridge function to functions which need string
127
+ * control sequences are removed on win32, and func is not called if nothing is left
112
128
  * @param {Function} func - function
113
129
  * @param {data} data - binary data which can be converted to string
130
+ * @param {string} platform - platform name (for tests)
114
131
  */
115
- export const buff2String = (func, data)=>{
116
- func(data.toString().replace(/\r\n/g, EOL));
132
+ export const buff2String = (func, data, platform = process.platform)=>{
133
+ let str = data.toString();
134
+ if (platform === "win32") {
135
+ str = stripTerminalSequences(str);
136
+ if (str === "") {
137
+ return;
138
+ }
139
+ }
140
+ func(str.replace(/\r\n/g, EOL));
117
141
  };
118
142
 
119
143
  /**
@@ -223,6 +247,62 @@ export const isControlMasterEnabled = (hostInfo)=>{
223
247
  return process.platform !== "win32";
224
248
  };
225
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
+
226
306
  /**
227
307
  * return ssh option with some hard corded options
228
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.2",
3
+ "version": "3.4.0",
4
4
  "description": "wrapper library for open ssh client and rsync",
5
5
  "keywords": [
6
6
  "ssh",