ssh2-scp 2.0.4 → 3.0.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/README-CN.md +45 -2
- package/README.md +45 -2
- package/dist/cjs/folder-transfer.cjs +292 -0
- package/dist/cjs/ssh-fs.cjs +25 -27
- package/dist/esm/folder-transfer.d.ts +69 -0
- package/dist/esm/folder-transfer.js +274 -0
- package/dist/esm/ssh-fs.d.ts +2 -1
- package/dist/esm/ssh-fs.js +26 -28
- package/dist/esm/transfer.d.ts +0 -1
- package/package.json +7 -2
package/README-CN.md
CHANGED
|
@@ -96,6 +96,8 @@ createSshFs(client)
|
|
|
96
96
|
|
|
97
97
|
## 文件传输
|
|
98
98
|
|
|
99
|
+
`Transfer` 仅用于单文件上传和下载。
|
|
100
|
+
|
|
99
101
|
```javascript
|
|
100
102
|
import { Transfer } from 'ssh2-scp/transfer'
|
|
101
103
|
|
|
@@ -115,12 +117,53 @@ await transfer.startTransfer()
|
|
|
115
117
|
### 传输选项
|
|
116
118
|
|
|
117
119
|
- `type` - 传输类型:`'download'`(下载)或 `'upload'`(上传)
|
|
118
|
-
- `remotePath` -
|
|
119
|
-
- `localPath` -
|
|
120
|
+
- `remotePath` - 远程文件路径
|
|
121
|
+
- `localPath` - 本地文件路径
|
|
120
122
|
- `chunkSize` - 传输块大小(默认:32768)
|
|
121
123
|
- `onProgress` - 进度回调函数 `(transferred, total) => void`
|
|
122
124
|
- `onData` - 数据回调函数 `(count) => void`
|
|
123
125
|
|
|
126
|
+
## 文件夹传输
|
|
127
|
+
|
|
128
|
+
`FolderTransfer` 会通过 SSH 命令通道流式传输 tar 归档。它直接从原始 `ssh2` `Client` 初始化,支持暂停/恢复,并同时支持 POSIX 服务器和带有 `tar` 的 Windows OpenSSH 服务器。
|
|
129
|
+
|
|
130
|
+
如果要使用文件夹传输,需要自行安装 tar 适配器:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
npm install tar
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
```javascript
|
|
137
|
+
import { Client } from 'ssh2'
|
|
138
|
+
import * as tar from 'tar'
|
|
139
|
+
import { FolderTransfer } from 'ssh2-scp/folder-transfer'
|
|
140
|
+
|
|
141
|
+
const client = new Client()
|
|
142
|
+
const transfer = new FolderTransfer(client, tar, {
|
|
143
|
+
type: 'upload',
|
|
144
|
+
localPath: '/local/folder',
|
|
145
|
+
remotePath: '/remote/folder',
|
|
146
|
+
chunkSize: 32768,
|
|
147
|
+
onProgress: (transferred, total) => {
|
|
148
|
+
console.log(`进度: ${transferred}/${total}`)
|
|
149
|
+
}
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
await transfer.startTransfer()
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### FolderTransfer 说明
|
|
156
|
+
|
|
157
|
+
- 构造方式:`new FolderTransfer(client, tarAdapter, options)`
|
|
158
|
+
- `type` - 传输类型:`'download'` 或 `'upload'`
|
|
159
|
+
- `remotePath` - 远程文件夹路径
|
|
160
|
+
- `localPath` - 本地文件夹路径
|
|
161
|
+
- `chunkSize` - tar 流管道使用的 high water mark
|
|
162
|
+
- `tarAdapter` - 兼容 tar 的对象,需要提供 `c()` 和 `x()`;可直接传入 `tar`
|
|
163
|
+
- `pause()` / `resume()` - 暂停或继续当前流式传输
|
|
164
|
+
- `destroy()` - 中止当前文件夹传输
|
|
165
|
+
- Windows 远端通过 PowerShell 加 `tar` 执行,Linux / 其他 POSIX 远端直接使用 `tar`
|
|
166
|
+
|
|
124
167
|
## 许可证
|
|
125
168
|
|
|
126
169
|
MIT
|
package/README.md
CHANGED
|
@@ -96,6 +96,8 @@ createSshFs(client)
|
|
|
96
96
|
|
|
97
97
|
## Transfer
|
|
98
98
|
|
|
99
|
+
`Transfer` is for single-file uploads and downloads.
|
|
100
|
+
|
|
99
101
|
```javascript
|
|
100
102
|
import { Transfer } from 'ssh2-scp/transfer'
|
|
101
103
|
|
|
@@ -115,12 +117,53 @@ await transfer.startTransfer()
|
|
|
115
117
|
### Transfer Options
|
|
116
118
|
|
|
117
119
|
- `type` - Transfer type: `'download'` or `'upload'`
|
|
118
|
-
- `remotePath` - Remote file
|
|
119
|
-
- `localPath` - Local file
|
|
120
|
+
- `remotePath` - Remote file path
|
|
121
|
+
- `localPath` - Local file path
|
|
120
122
|
- `chunkSize` - Chunk size for transfer (default: 32768)
|
|
121
123
|
- `onProgress` - Progress callback `(transferred, total) => void`
|
|
122
124
|
- `onData` - Data callback `(count) => void`
|
|
123
125
|
|
|
126
|
+
## Folder Transfer
|
|
127
|
+
|
|
128
|
+
`FolderTransfer` streams a tar archive over the SSH command channel. It is initialized from a raw `ssh2` `Client`, supports pause/resume, and targets both POSIX servers and Windows OpenSSH servers that provide `tar`.
|
|
129
|
+
|
|
130
|
+
Install a tar adapter explicitly if you want folder transfer support:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
npm install tar
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
```javascript
|
|
137
|
+
import { Client } from 'ssh2'
|
|
138
|
+
import * as tar from 'tar'
|
|
139
|
+
import { FolderTransfer } from 'ssh2-scp/folder-transfer'
|
|
140
|
+
|
|
141
|
+
const client = new Client()
|
|
142
|
+
const transfer = new FolderTransfer(client, tar, {
|
|
143
|
+
type: 'upload',
|
|
144
|
+
localPath: '/local/folder',
|
|
145
|
+
remotePath: '/remote/folder',
|
|
146
|
+
chunkSize: 32768,
|
|
147
|
+
onProgress: (transferred, total) => {
|
|
148
|
+
console.log(`Progress: ${transferred}/${total}`)
|
|
149
|
+
}
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
await transfer.startTransfer()
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### FolderTransfer Notes
|
|
156
|
+
|
|
157
|
+
- Constructor: `new FolderTransfer(client, tarAdapter, options)`
|
|
158
|
+
- `type` - Transfer type: `'download'` or `'upload'`
|
|
159
|
+
- `remotePath` - Remote folder path
|
|
160
|
+
- `localPath` - Local folder path
|
|
161
|
+
- `chunkSize` - Stream high water mark used by the tar pipeline
|
|
162
|
+
- `tarAdapter` - A tar-compatible object that exposes `c()` and `x()`; `tar` works out of the box
|
|
163
|
+
- `pause()` / `resume()` - Pause or continue the active stream
|
|
164
|
+
- `destroy()` - Abort the current folder transfer
|
|
165
|
+
- Windows remotes use PowerShell plus `tar`; Linux and other POSIX remotes use `tar` directly
|
|
166
|
+
|
|
124
167
|
## License
|
|
125
168
|
|
|
126
169
|
MIT
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const stream = require("stream");
|
|
6
|
+
const promises = require("stream/promises");
|
|
7
|
+
function _interopNamespaceDefault(e) {
|
|
8
|
+
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
9
|
+
if (e) {
|
|
10
|
+
for (const k in e) {
|
|
11
|
+
if (k !== "default") {
|
|
12
|
+
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
13
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: () => e[k]
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
n.default = e;
|
|
21
|
+
return Object.freeze(n);
|
|
22
|
+
}
|
|
23
|
+
const fs__namespace = /* @__PURE__ */ _interopNamespaceDefault(fs);
|
|
24
|
+
const path__namespace = /* @__PURE__ */ _interopNamespaceDefault(path);
|
|
25
|
+
class FolderTransfer {
|
|
26
|
+
session;
|
|
27
|
+
tarAdapter;
|
|
28
|
+
runRemoteCommand;
|
|
29
|
+
options;
|
|
30
|
+
chunkSize;
|
|
31
|
+
state;
|
|
32
|
+
aborted = false;
|
|
33
|
+
platformPromise = null;
|
|
34
|
+
pauseTarget = null;
|
|
35
|
+
activeChannel = null;
|
|
36
|
+
constructor(session, tarAdapter, options) {
|
|
37
|
+
this.session = session;
|
|
38
|
+
this.tarAdapter = tarAdapter;
|
|
39
|
+
this.runRemoteCommand = this.runExec.bind(this);
|
|
40
|
+
this.options = options;
|
|
41
|
+
this.chunkSize = options.chunkSize || 32768;
|
|
42
|
+
this.state = {
|
|
43
|
+
transferred: 0,
|
|
44
|
+
total: 0,
|
|
45
|
+
paused: false,
|
|
46
|
+
completed: false
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
async startTransfer() {
|
|
50
|
+
if (this.state.completed || this.state.error || this.aborted) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
if (this.options.type === "download") {
|
|
55
|
+
await this.download();
|
|
56
|
+
} else {
|
|
57
|
+
await this.upload();
|
|
58
|
+
}
|
|
59
|
+
this.state.completed = true;
|
|
60
|
+
} catch (err) {
|
|
61
|
+
this.state.error = err;
|
|
62
|
+
throw err;
|
|
63
|
+
} finally {
|
|
64
|
+
this.pauseTarget = null;
|
|
65
|
+
this.activeChannel = null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
pause() {
|
|
69
|
+
this.state.paused = true;
|
|
70
|
+
this.pauseTarget?.pause();
|
|
71
|
+
}
|
|
72
|
+
resume() {
|
|
73
|
+
this.state.paused = false;
|
|
74
|
+
this.pauseTarget?.resume();
|
|
75
|
+
}
|
|
76
|
+
getState() {
|
|
77
|
+
return { ...this.state };
|
|
78
|
+
}
|
|
79
|
+
destroy() {
|
|
80
|
+
this.aborted = true;
|
|
81
|
+
this.pauseTarget?.destroy?.(this.createAbortError());
|
|
82
|
+
this.activeChannel?.close();
|
|
83
|
+
}
|
|
84
|
+
createAbortError() {
|
|
85
|
+
return new Error("Folder transfer aborted");
|
|
86
|
+
}
|
|
87
|
+
async download() {
|
|
88
|
+
const { remotePath, localPath } = this.options;
|
|
89
|
+
const platform = await this.getRemotePlatform();
|
|
90
|
+
const normalizedRemotePath = this.normalizeRemotePath(remotePath, platform);
|
|
91
|
+
fs__namespace.mkdirSync(localPath, { recursive: true });
|
|
92
|
+
this.state.total = await this.getRemoteFolderSize(normalizedRemotePath, platform);
|
|
93
|
+
const channel = await this.execStream(this.buildDownloadCommand(normalizedRemotePath, platform));
|
|
94
|
+
this.activeChannel = channel;
|
|
95
|
+
this.pauseTarget = channel;
|
|
96
|
+
const closePromise = this.waitForChannelClose(channel);
|
|
97
|
+
const progressStream = this.createProgressStream();
|
|
98
|
+
const extractStream = this.tarAdapter.x({ cwd: localPath, strict: true });
|
|
99
|
+
await Promise.all([
|
|
100
|
+
promises.pipeline(channel, progressStream, extractStream),
|
|
101
|
+
closePromise
|
|
102
|
+
]);
|
|
103
|
+
}
|
|
104
|
+
async upload() {
|
|
105
|
+
const { remotePath, localPath } = this.options;
|
|
106
|
+
const localStats = fs__namespace.statSync(localPath);
|
|
107
|
+
if (!localStats.isDirectory()) {
|
|
108
|
+
throw new Error(`Local path is not a directory: ${localPath}`);
|
|
109
|
+
}
|
|
110
|
+
const platform = await this.getRemotePlatform();
|
|
111
|
+
const normalizedRemotePath = this.normalizeRemotePath(remotePath, platform);
|
|
112
|
+
this.state.total = await this.getLocalFolderSize(localPath);
|
|
113
|
+
const channel = await this.execStream(this.buildUploadCommand(normalizedRemotePath, platform));
|
|
114
|
+
this.activeChannel = channel;
|
|
115
|
+
const closePromise = this.waitForChannelClose(channel);
|
|
116
|
+
const packStream = this.tarAdapter.c({ cwd: localPath, portable: true }, ["."]);
|
|
117
|
+
this.pauseTarget = packStream;
|
|
118
|
+
const progressStream = this.createProgressStream();
|
|
119
|
+
const remoteStdin = this.getChannelWritable(channel);
|
|
120
|
+
await Promise.all([
|
|
121
|
+
promises.pipeline(packStream, progressStream, remoteStdin),
|
|
122
|
+
closePromise
|
|
123
|
+
]);
|
|
124
|
+
}
|
|
125
|
+
async getRemotePlatform() {
|
|
126
|
+
if (!this.platformPromise) {
|
|
127
|
+
this.platformPromise = this.runRemoteCommand("uname -s").then((output) => {
|
|
128
|
+
const value = output.trim().toLowerCase();
|
|
129
|
+
const platform = value.includes("windows") ? "windows" : "posix";
|
|
130
|
+
return platform;
|
|
131
|
+
}).catch(async () => {
|
|
132
|
+
await this.runRemoteCommand(this.toPowerShellCommand("$PSVersionTable.PSVersion.ToString() | Out-String"));
|
|
133
|
+
return "windows";
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
return await this.platformPromise;
|
|
137
|
+
}
|
|
138
|
+
async getRemoteFolderSize(remotePath, platform) {
|
|
139
|
+
const output = platform === "windows" ? await this.runRemoteCommand(this.toPowerShellCommand(`$sum=(Get-ChildItem -LiteralPath '${this.escapePowerShell(remotePath)}' -Recurse -Force | Where-Object { -not $_.PSIsContainer } | Measure-Object -Property Length -Sum).Sum; if ($null -eq $sum) { '0' } else { [int64]$sum }`)) : await this.runRemoteCommand(`find "${this.escapePosix(remotePath)}" -type f -exec stat -c %s {} + 2>/dev/null | awk '{s+=$1} END {print s+0}' || find "${this.escapePosix(remotePath)}" -type f -exec stat -f %z {} + 2>/dev/null | awk '{s+=$1} END {print s+0}'`);
|
|
140
|
+
return parseInt(output.trim(), 10) || 0;
|
|
141
|
+
}
|
|
142
|
+
normalizeRemotePath(remotePath, platform) {
|
|
143
|
+
if (platform !== "windows") {
|
|
144
|
+
return remotePath;
|
|
145
|
+
}
|
|
146
|
+
const trimmedPath = remotePath.trim().replace(/\\/g, "/");
|
|
147
|
+
return trimmedPath.replace(/^\/([A-Za-z]:(?:\/|$))/, "$1");
|
|
148
|
+
}
|
|
149
|
+
async getLocalFolderSize(localPath) {
|
|
150
|
+
const stack = [localPath];
|
|
151
|
+
let total = 0;
|
|
152
|
+
while (stack.length > 0) {
|
|
153
|
+
const currentPath = stack.pop();
|
|
154
|
+
const entries = await fs__namespace.promises.readdir(currentPath, { withFileTypes: true });
|
|
155
|
+
for (const entry of entries) {
|
|
156
|
+
const entryPath = path__namespace.join(currentPath, entry.name);
|
|
157
|
+
if (entry.isDirectory()) {
|
|
158
|
+
stack.push(entryPath);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (entry.isFile()) {
|
|
162
|
+
const stats = await fs__namespace.promises.stat(entryPath);
|
|
163
|
+
total += stats.size;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return total;
|
|
168
|
+
}
|
|
169
|
+
createProgressStream() {
|
|
170
|
+
return new stream.Transform({
|
|
171
|
+
highWaterMark: this.chunkSize,
|
|
172
|
+
transform: (chunk, _encoding, callback) => {
|
|
173
|
+
if (this.aborted) {
|
|
174
|
+
callback(this.createAbortError());
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
const byteLength = Buffer.isBuffer(chunk) ? chunk.length : Buffer.byteLength(String(chunk));
|
|
178
|
+
this.state.transferred += byteLength;
|
|
179
|
+
if (this.options.onProgress) {
|
|
180
|
+
const visibleTransferred = this.state.total > 0 ? Math.min(this.state.transferred, this.state.total) : this.state.transferred;
|
|
181
|
+
this.options.onProgress(visibleTransferred, this.state.total);
|
|
182
|
+
}
|
|
183
|
+
if (this.options.onData) {
|
|
184
|
+
this.options.onData(byteLength);
|
|
185
|
+
}
|
|
186
|
+
callback(null, chunk);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
buildDownloadCommand(remotePath, platform) {
|
|
191
|
+
if (platform === "windows") {
|
|
192
|
+
return this.toPowerShellCommand(`& { tar -cf - -C '${this.escapePowerShell(remotePath)}' . }`);
|
|
193
|
+
}
|
|
194
|
+
return `tar -cf - -C "${this.escapePosix(remotePath)}" .`;
|
|
195
|
+
}
|
|
196
|
+
buildUploadCommand(remotePath, platform) {
|
|
197
|
+
if (platform === "windows") {
|
|
198
|
+
return this.toPowerShellCommand(`& { New-Item -ItemType Directory -Force -Path '${this.escapePowerShell(remotePath)}' | Out-Null; tar -xf - -C '${this.escapePowerShell(remotePath)}' }`);
|
|
199
|
+
}
|
|
200
|
+
return `mkdir -p "${this.escapePosix(remotePath)}" && tar -xf - -C "${this.escapePosix(remotePath)}"`;
|
|
201
|
+
}
|
|
202
|
+
escapePosix(value) {
|
|
203
|
+
return value.replace(/(["\\`$])/g, "\\$1");
|
|
204
|
+
}
|
|
205
|
+
escapePowerShell(value) {
|
|
206
|
+
return value.replace(/'/g, "''");
|
|
207
|
+
}
|
|
208
|
+
toPowerShellCommand(script) {
|
|
209
|
+
return `powershell -NoProfile -Command "${script.replace(/"/g, '\\"')}"`;
|
|
210
|
+
}
|
|
211
|
+
getChannelWritable(channel) {
|
|
212
|
+
const withStdin = channel;
|
|
213
|
+
return withStdin.stdin || channel;
|
|
214
|
+
}
|
|
215
|
+
execStream(cmd) {
|
|
216
|
+
return new Promise((resolve, reject) => {
|
|
217
|
+
this.session.exec(cmd, (err, channel) => {
|
|
218
|
+
if (err) {
|
|
219
|
+
reject(err);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
resolve(channel);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
runExec(cmd) {
|
|
227
|
+
return new Promise((resolve, reject) => {
|
|
228
|
+
this.session.exec(cmd, (err, channel) => {
|
|
229
|
+
if (err) {
|
|
230
|
+
reject(err);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
let stdout = "";
|
|
234
|
+
let stderr = "";
|
|
235
|
+
channel.on("data", (chunk) => {
|
|
236
|
+
stdout += chunk.toString();
|
|
237
|
+
});
|
|
238
|
+
channel.stderr.on("data", (chunk) => {
|
|
239
|
+
stderr += chunk.toString();
|
|
240
|
+
});
|
|
241
|
+
channel.on("close", (code) => {
|
|
242
|
+
if (code && code !== 0) {
|
|
243
|
+
reject(new Error(stderr.trim() || `Command failed: ${cmd}`));
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (stderr.trim()) {
|
|
247
|
+
reject(new Error(stderr.trim()));
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
resolve(stdout);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
waitForChannelClose(channel) {
|
|
256
|
+
return new Promise((resolve, reject) => {
|
|
257
|
+
let stderr = "";
|
|
258
|
+
let settled = false;
|
|
259
|
+
const settle = (error) => {
|
|
260
|
+
if (settled) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
settled = true;
|
|
264
|
+
if (error) {
|
|
265
|
+
reject(error);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
resolve();
|
|
269
|
+
};
|
|
270
|
+
const handleExit = (code) => {
|
|
271
|
+
if (this.aborted) {
|
|
272
|
+
settle(this.createAbortError());
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (typeof code === "number" && code !== 0) {
|
|
276
|
+
settle(new Error(stderr.trim() || `Remote command failed with exit code ${code}`));
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
settle();
|
|
280
|
+
};
|
|
281
|
+
channel.stderr.on("data", (chunk) => {
|
|
282
|
+
stderr += chunk.toString();
|
|
283
|
+
});
|
|
284
|
+
channel.on("exit", handleExit);
|
|
285
|
+
channel.on("close", handleExit);
|
|
286
|
+
channel.on("error", (error) => {
|
|
287
|
+
settle(error);
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
exports.FolderTransfer = FolderTransfer;
|
package/dist/cjs/ssh-fs.cjs
CHANGED
|
@@ -20,30 +20,6 @@ class SshFs {
|
|
|
20
20
|
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
|
21
21
|
return months.indexOf(month);
|
|
22
22
|
}
|
|
23
|
-
parseMode(modeStr) {
|
|
24
|
-
const permMap = {
|
|
25
|
-
"---": 0,
|
|
26
|
-
"--x": 1,
|
|
27
|
-
"-w-": 2,
|
|
28
|
-
"-wx": 3,
|
|
29
|
-
"r--": 4,
|
|
30
|
-
"r-x": 5,
|
|
31
|
-
"rw-": 6,
|
|
32
|
-
"rwx": 7
|
|
33
|
-
};
|
|
34
|
-
const perms = modeStr.slice(1, 10);
|
|
35
|
-
const owner = permMap[perms.slice(0, 3)] || 0;
|
|
36
|
-
const group = permMap[perms.slice(3, 6)] || 0;
|
|
37
|
-
const other = permMap[perms.slice(6, 9)] || 0;
|
|
38
|
-
let fileType = 32768;
|
|
39
|
-
if (modeStr.startsWith("d")) fileType = 16384;
|
|
40
|
-
else if (modeStr.startsWith("l")) fileType = 40960;
|
|
41
|
-
else if (modeStr.startsWith("c")) fileType = 8192;
|
|
42
|
-
else if (modeStr.startsWith("b")) fileType = 24576;
|
|
43
|
-
else if (modeStr.startsWith("p")) fileType = 4096;
|
|
44
|
-
else if (modeStr.startsWith("s")) fileType = 49152;
|
|
45
|
-
return fileType | owner << 6 | group << 3 | other;
|
|
46
|
-
}
|
|
47
23
|
runCmd(cmd, timeout = 3e4) {
|
|
48
24
|
return new Promise((resolve, reject) => {
|
|
49
25
|
let timeoutId;
|
|
@@ -167,7 +143,7 @@ class SshFs {
|
|
|
167
143
|
const name = parts.slice(8).join(" ");
|
|
168
144
|
if (name === "." || name === "..") continue;
|
|
169
145
|
const type = parts[0].charAt(0);
|
|
170
|
-
const mode =
|
|
146
|
+
const mode = parseModeFromLongname(parts[0]);
|
|
171
147
|
const owner = parseInt(parts[2], 10);
|
|
172
148
|
const group = parseInt(parts[3], 10);
|
|
173
149
|
const size = parseInt(parts[4], 10);
|
|
@@ -231,7 +207,7 @@ class SshFs {
|
|
|
231
207
|
}
|
|
232
208
|
const size = parseInt(parts[4], 10);
|
|
233
209
|
const modeStr = parts[0];
|
|
234
|
-
const mode =
|
|
210
|
+
const mode = parseModeFromLongname(modeStr);
|
|
235
211
|
const uid = parseInt(parts[2], 10);
|
|
236
212
|
const gid = parseInt(parts[3], 10);
|
|
237
213
|
return {
|
|
@@ -280,7 +256,7 @@ class SshFs {
|
|
|
280
256
|
}
|
|
281
257
|
const size = parseInt(parts[4], 10);
|
|
282
258
|
const modeStr = parts[0];
|
|
283
|
-
const mode =
|
|
259
|
+
const mode = parseModeFromLongname(modeStr);
|
|
284
260
|
const uid = parseInt(parts[2], 10);
|
|
285
261
|
const gid = parseInt(parts[3], 10);
|
|
286
262
|
return {
|
|
@@ -367,5 +343,27 @@ class SshFs {
|
|
|
367
343
|
function createSshFs(session, options) {
|
|
368
344
|
return new SshFs(session, options);
|
|
369
345
|
}
|
|
346
|
+
function parseModeFromLongname(longname) {
|
|
347
|
+
if (!longname || longname.length < 10) return 0;
|
|
348
|
+
const typeMap = {
|
|
349
|
+
"-": 32768,
|
|
350
|
+
d: 16384,
|
|
351
|
+
l: 40960,
|
|
352
|
+
c: 8192,
|
|
353
|
+
b: 24576,
|
|
354
|
+
p: 4096,
|
|
355
|
+
s: 49152
|
|
356
|
+
};
|
|
357
|
+
const typeBits = typeMap[longname[0]] || 32768;
|
|
358
|
+
const permValues = [256, 128, 64, 32, 16, 8, 4, 2, 1];
|
|
359
|
+
let permBits = 0;
|
|
360
|
+
for (let i = 0; i < 9; i++) {
|
|
361
|
+
if (longname[i + 1] !== "-") {
|
|
362
|
+
permBits |= permValues[i];
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return typeBits | permBits;
|
|
366
|
+
}
|
|
370
367
|
exports.SshFs = SshFs;
|
|
371
368
|
exports.createSshFs = createSshFs;
|
|
369
|
+
exports.parseModeFromLongname = parseModeFromLongname;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Client } from 'ssh2';
|
|
2
|
+
|
|
3
|
+
export declare class FolderTransfer {
|
|
4
|
+
private session;
|
|
5
|
+
private tarAdapter;
|
|
6
|
+
private runRemoteCommand;
|
|
7
|
+
private options;
|
|
8
|
+
private chunkSize;
|
|
9
|
+
private state;
|
|
10
|
+
private aborted;
|
|
11
|
+
private platformPromise;
|
|
12
|
+
private pauseTarget;
|
|
13
|
+
private activeChannel;
|
|
14
|
+
constructor(session: Client, tarAdapter: FolderTransferTarAdapter, options: TransferOptions);
|
|
15
|
+
startTransfer(): Promise<void>;
|
|
16
|
+
pause(): void;
|
|
17
|
+
resume(): void;
|
|
18
|
+
getState(): TransferState;
|
|
19
|
+
destroy(): void;
|
|
20
|
+
private createAbortError;
|
|
21
|
+
private download;
|
|
22
|
+
private upload;
|
|
23
|
+
private getRemotePlatform;
|
|
24
|
+
private getRemoteFolderSize;
|
|
25
|
+
private normalizeRemotePath;
|
|
26
|
+
private getLocalFolderSize;
|
|
27
|
+
private createProgressStream;
|
|
28
|
+
private buildDownloadCommand;
|
|
29
|
+
private buildUploadCommand;
|
|
30
|
+
private escapePosix;
|
|
31
|
+
private escapePowerShell;
|
|
32
|
+
private toPowerShellCommand;
|
|
33
|
+
private getChannelWritable;
|
|
34
|
+
private execStream;
|
|
35
|
+
private runExec;
|
|
36
|
+
private waitForChannelClose;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export declare interface FolderTransferTarAdapter {
|
|
40
|
+
c(options: {
|
|
41
|
+
cwd: string;
|
|
42
|
+
portable?: boolean;
|
|
43
|
+
}, filePaths: string[]): NodeJS.ReadableStream;
|
|
44
|
+
x(options: {
|
|
45
|
+
cwd: string;
|
|
46
|
+
strict?: boolean;
|
|
47
|
+
}): NodeJS.WritableStream;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export declare interface TransferOptions {
|
|
51
|
+
type: TransferType;
|
|
52
|
+
remotePath: string;
|
|
53
|
+
localPath: string;
|
|
54
|
+
chunkSize?: number;
|
|
55
|
+
onProgress?: (transferred: number, total: number) => void;
|
|
56
|
+
onData?: (count: number) => void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export declare interface TransferState {
|
|
60
|
+
transferred: number;
|
|
61
|
+
total: number;
|
|
62
|
+
paused: boolean;
|
|
63
|
+
completed: boolean;
|
|
64
|
+
error?: Error;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export declare type TransferType = 'download' | 'upload';
|
|
68
|
+
|
|
69
|
+
export { }
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { Transform } from "stream";
|
|
4
|
+
import { pipeline } from "stream/promises";
|
|
5
|
+
class FolderTransfer {
|
|
6
|
+
session;
|
|
7
|
+
tarAdapter;
|
|
8
|
+
runRemoteCommand;
|
|
9
|
+
options;
|
|
10
|
+
chunkSize;
|
|
11
|
+
state;
|
|
12
|
+
aborted = false;
|
|
13
|
+
platformPromise = null;
|
|
14
|
+
pauseTarget = null;
|
|
15
|
+
activeChannel = null;
|
|
16
|
+
constructor(session, tarAdapter, options) {
|
|
17
|
+
this.session = session;
|
|
18
|
+
this.tarAdapter = tarAdapter;
|
|
19
|
+
this.runRemoteCommand = this.runExec.bind(this);
|
|
20
|
+
this.options = options;
|
|
21
|
+
this.chunkSize = options.chunkSize || 32768;
|
|
22
|
+
this.state = {
|
|
23
|
+
transferred: 0,
|
|
24
|
+
total: 0,
|
|
25
|
+
paused: false,
|
|
26
|
+
completed: false
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
async startTransfer() {
|
|
30
|
+
if (this.state.completed || this.state.error || this.aborted) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
if (this.options.type === "download") {
|
|
35
|
+
await this.download();
|
|
36
|
+
} else {
|
|
37
|
+
await this.upload();
|
|
38
|
+
}
|
|
39
|
+
this.state.completed = true;
|
|
40
|
+
} catch (err) {
|
|
41
|
+
this.state.error = err;
|
|
42
|
+
throw err;
|
|
43
|
+
} finally {
|
|
44
|
+
this.pauseTarget = null;
|
|
45
|
+
this.activeChannel = null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
pause() {
|
|
49
|
+
this.state.paused = true;
|
|
50
|
+
this.pauseTarget?.pause();
|
|
51
|
+
}
|
|
52
|
+
resume() {
|
|
53
|
+
this.state.paused = false;
|
|
54
|
+
this.pauseTarget?.resume();
|
|
55
|
+
}
|
|
56
|
+
getState() {
|
|
57
|
+
return { ...this.state };
|
|
58
|
+
}
|
|
59
|
+
destroy() {
|
|
60
|
+
this.aborted = true;
|
|
61
|
+
this.pauseTarget?.destroy?.(this.createAbortError());
|
|
62
|
+
this.activeChannel?.close();
|
|
63
|
+
}
|
|
64
|
+
createAbortError() {
|
|
65
|
+
return new Error("Folder transfer aborted");
|
|
66
|
+
}
|
|
67
|
+
async download() {
|
|
68
|
+
const { remotePath, localPath } = this.options;
|
|
69
|
+
const platform = await this.getRemotePlatform();
|
|
70
|
+
const normalizedRemotePath = this.normalizeRemotePath(remotePath, platform);
|
|
71
|
+
fs.mkdirSync(localPath, { recursive: true });
|
|
72
|
+
this.state.total = await this.getRemoteFolderSize(normalizedRemotePath, platform);
|
|
73
|
+
const channel = await this.execStream(this.buildDownloadCommand(normalizedRemotePath, platform));
|
|
74
|
+
this.activeChannel = channel;
|
|
75
|
+
this.pauseTarget = channel;
|
|
76
|
+
const closePromise = this.waitForChannelClose(channel);
|
|
77
|
+
const progressStream = this.createProgressStream();
|
|
78
|
+
const extractStream = this.tarAdapter.x({ cwd: localPath, strict: true });
|
|
79
|
+
await Promise.all([
|
|
80
|
+
pipeline(channel, progressStream, extractStream),
|
|
81
|
+
closePromise
|
|
82
|
+
]);
|
|
83
|
+
}
|
|
84
|
+
async upload() {
|
|
85
|
+
const { remotePath, localPath } = this.options;
|
|
86
|
+
const localStats = fs.statSync(localPath);
|
|
87
|
+
if (!localStats.isDirectory()) {
|
|
88
|
+
throw new Error(`Local path is not a directory: ${localPath}`);
|
|
89
|
+
}
|
|
90
|
+
const platform = await this.getRemotePlatform();
|
|
91
|
+
const normalizedRemotePath = this.normalizeRemotePath(remotePath, platform);
|
|
92
|
+
this.state.total = await this.getLocalFolderSize(localPath);
|
|
93
|
+
const channel = await this.execStream(this.buildUploadCommand(normalizedRemotePath, platform));
|
|
94
|
+
this.activeChannel = channel;
|
|
95
|
+
const closePromise = this.waitForChannelClose(channel);
|
|
96
|
+
const packStream = this.tarAdapter.c({ cwd: localPath, portable: true }, ["."]);
|
|
97
|
+
this.pauseTarget = packStream;
|
|
98
|
+
const progressStream = this.createProgressStream();
|
|
99
|
+
const remoteStdin = this.getChannelWritable(channel);
|
|
100
|
+
await Promise.all([
|
|
101
|
+
pipeline(packStream, progressStream, remoteStdin),
|
|
102
|
+
closePromise
|
|
103
|
+
]);
|
|
104
|
+
}
|
|
105
|
+
async getRemotePlatform() {
|
|
106
|
+
if (!this.platformPromise) {
|
|
107
|
+
this.platformPromise = this.runRemoteCommand("uname -s").then((output) => {
|
|
108
|
+
const value = output.trim().toLowerCase();
|
|
109
|
+
const platform = value.includes("windows") ? "windows" : "posix";
|
|
110
|
+
return platform;
|
|
111
|
+
}).catch(async () => {
|
|
112
|
+
await this.runRemoteCommand(this.toPowerShellCommand("$PSVersionTable.PSVersion.ToString() | Out-String"));
|
|
113
|
+
return "windows";
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return await this.platformPromise;
|
|
117
|
+
}
|
|
118
|
+
async getRemoteFolderSize(remotePath, platform) {
|
|
119
|
+
const output = platform === "windows" ? await this.runRemoteCommand(this.toPowerShellCommand(`$sum=(Get-ChildItem -LiteralPath '${this.escapePowerShell(remotePath)}' -Recurse -Force | Where-Object { -not $_.PSIsContainer } | Measure-Object -Property Length -Sum).Sum; if ($null -eq $sum) { '0' } else { [int64]$sum }`)) : await this.runRemoteCommand(`find "${this.escapePosix(remotePath)}" -type f -exec stat -c %s {} + 2>/dev/null | awk '{s+=$1} END {print s+0}' || find "${this.escapePosix(remotePath)}" -type f -exec stat -f %z {} + 2>/dev/null | awk '{s+=$1} END {print s+0}'`);
|
|
120
|
+
return parseInt(output.trim(), 10) || 0;
|
|
121
|
+
}
|
|
122
|
+
normalizeRemotePath(remotePath, platform) {
|
|
123
|
+
if (platform !== "windows") {
|
|
124
|
+
return remotePath;
|
|
125
|
+
}
|
|
126
|
+
const trimmedPath = remotePath.trim().replace(/\\/g, "/");
|
|
127
|
+
return trimmedPath.replace(/^\/([A-Za-z]:(?:\/|$))/, "$1");
|
|
128
|
+
}
|
|
129
|
+
async getLocalFolderSize(localPath) {
|
|
130
|
+
const stack = [localPath];
|
|
131
|
+
let total = 0;
|
|
132
|
+
while (stack.length > 0) {
|
|
133
|
+
const currentPath = stack.pop();
|
|
134
|
+
const entries = await fs.promises.readdir(currentPath, { withFileTypes: true });
|
|
135
|
+
for (const entry of entries) {
|
|
136
|
+
const entryPath = path.join(currentPath, entry.name);
|
|
137
|
+
if (entry.isDirectory()) {
|
|
138
|
+
stack.push(entryPath);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (entry.isFile()) {
|
|
142
|
+
const stats = await fs.promises.stat(entryPath);
|
|
143
|
+
total += stats.size;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return total;
|
|
148
|
+
}
|
|
149
|
+
createProgressStream() {
|
|
150
|
+
return new Transform({
|
|
151
|
+
highWaterMark: this.chunkSize,
|
|
152
|
+
transform: (chunk, _encoding, callback) => {
|
|
153
|
+
if (this.aborted) {
|
|
154
|
+
callback(this.createAbortError());
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const byteLength = Buffer.isBuffer(chunk) ? chunk.length : Buffer.byteLength(String(chunk));
|
|
158
|
+
this.state.transferred += byteLength;
|
|
159
|
+
if (this.options.onProgress) {
|
|
160
|
+
const visibleTransferred = this.state.total > 0 ? Math.min(this.state.transferred, this.state.total) : this.state.transferred;
|
|
161
|
+
this.options.onProgress(visibleTransferred, this.state.total);
|
|
162
|
+
}
|
|
163
|
+
if (this.options.onData) {
|
|
164
|
+
this.options.onData(byteLength);
|
|
165
|
+
}
|
|
166
|
+
callback(null, chunk);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
buildDownloadCommand(remotePath, platform) {
|
|
171
|
+
if (platform === "windows") {
|
|
172
|
+
return this.toPowerShellCommand(`& { tar -cf - -C '${this.escapePowerShell(remotePath)}' . }`);
|
|
173
|
+
}
|
|
174
|
+
return `tar -cf - -C "${this.escapePosix(remotePath)}" .`;
|
|
175
|
+
}
|
|
176
|
+
buildUploadCommand(remotePath, platform) {
|
|
177
|
+
if (platform === "windows") {
|
|
178
|
+
return this.toPowerShellCommand(`& { New-Item -ItemType Directory -Force -Path '${this.escapePowerShell(remotePath)}' | Out-Null; tar -xf - -C '${this.escapePowerShell(remotePath)}' }`);
|
|
179
|
+
}
|
|
180
|
+
return `mkdir -p "${this.escapePosix(remotePath)}" && tar -xf - -C "${this.escapePosix(remotePath)}"`;
|
|
181
|
+
}
|
|
182
|
+
escapePosix(value) {
|
|
183
|
+
return value.replace(/(["\\`$])/g, "\\$1");
|
|
184
|
+
}
|
|
185
|
+
escapePowerShell(value) {
|
|
186
|
+
return value.replace(/'/g, "''");
|
|
187
|
+
}
|
|
188
|
+
toPowerShellCommand(script) {
|
|
189
|
+
return `powershell -NoProfile -Command "${script.replace(/"/g, '\\"')}"`;
|
|
190
|
+
}
|
|
191
|
+
getChannelWritable(channel) {
|
|
192
|
+
const withStdin = channel;
|
|
193
|
+
return withStdin.stdin || channel;
|
|
194
|
+
}
|
|
195
|
+
execStream(cmd) {
|
|
196
|
+
return new Promise((resolve, reject) => {
|
|
197
|
+
this.session.exec(cmd, (err, channel) => {
|
|
198
|
+
if (err) {
|
|
199
|
+
reject(err);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
resolve(channel);
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
runExec(cmd) {
|
|
207
|
+
return new Promise((resolve, reject) => {
|
|
208
|
+
this.session.exec(cmd, (err, channel) => {
|
|
209
|
+
if (err) {
|
|
210
|
+
reject(err);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
let stdout = "";
|
|
214
|
+
let stderr = "";
|
|
215
|
+
channel.on("data", (chunk) => {
|
|
216
|
+
stdout += chunk.toString();
|
|
217
|
+
});
|
|
218
|
+
channel.stderr.on("data", (chunk) => {
|
|
219
|
+
stderr += chunk.toString();
|
|
220
|
+
});
|
|
221
|
+
channel.on("close", (code) => {
|
|
222
|
+
if (code && code !== 0) {
|
|
223
|
+
reject(new Error(stderr.trim() || `Command failed: ${cmd}`));
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (stderr.trim()) {
|
|
227
|
+
reject(new Error(stderr.trim()));
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
resolve(stdout);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
waitForChannelClose(channel) {
|
|
236
|
+
return new Promise((resolve, reject) => {
|
|
237
|
+
let stderr = "";
|
|
238
|
+
let settled = false;
|
|
239
|
+
const settle = (error) => {
|
|
240
|
+
if (settled) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
settled = true;
|
|
244
|
+
if (error) {
|
|
245
|
+
reject(error);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
resolve();
|
|
249
|
+
};
|
|
250
|
+
const handleExit = (code) => {
|
|
251
|
+
if (this.aborted) {
|
|
252
|
+
settle(this.createAbortError());
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (typeof code === "number" && code !== 0) {
|
|
256
|
+
settle(new Error(stderr.trim() || `Remote command failed with exit code ${code}`));
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
settle();
|
|
260
|
+
};
|
|
261
|
+
channel.stderr.on("data", (chunk) => {
|
|
262
|
+
stderr += chunk.toString();
|
|
263
|
+
});
|
|
264
|
+
channel.on("exit", handleExit);
|
|
265
|
+
channel.on("close", handleExit);
|
|
266
|
+
channel.on("error", (error) => {
|
|
267
|
+
settle(error);
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
export {
|
|
273
|
+
FolderTransfer
|
|
274
|
+
};
|
package/dist/esm/ssh-fs.d.ts
CHANGED
|
@@ -18,6 +18,8 @@ export declare interface FileInfo {
|
|
|
18
18
|
group: number;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
export declare function parseModeFromLongname(longname: string): number;
|
|
22
|
+
|
|
21
23
|
export declare class SshFs {
|
|
22
24
|
private session;
|
|
23
25
|
private _chunkSize;
|
|
@@ -26,7 +28,6 @@ export declare class SshFs {
|
|
|
26
28
|
private detectChunkSize;
|
|
27
29
|
private getExecOpts;
|
|
28
30
|
private getMonthIndex;
|
|
29
|
-
private parseMode;
|
|
30
31
|
private runCmd;
|
|
31
32
|
private rmFolderCmd;
|
|
32
33
|
private rmCmd;
|
package/dist/esm/ssh-fs.js
CHANGED
|
@@ -18,30 +18,6 @@ class SshFs {
|
|
|
18
18
|
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
|
19
19
|
return months.indexOf(month);
|
|
20
20
|
}
|
|
21
|
-
parseMode(modeStr) {
|
|
22
|
-
const permMap = {
|
|
23
|
-
"---": 0,
|
|
24
|
-
"--x": 1,
|
|
25
|
-
"-w-": 2,
|
|
26
|
-
"-wx": 3,
|
|
27
|
-
"r--": 4,
|
|
28
|
-
"r-x": 5,
|
|
29
|
-
"rw-": 6,
|
|
30
|
-
"rwx": 7
|
|
31
|
-
};
|
|
32
|
-
const perms = modeStr.slice(1, 10);
|
|
33
|
-
const owner = permMap[perms.slice(0, 3)] || 0;
|
|
34
|
-
const group = permMap[perms.slice(3, 6)] || 0;
|
|
35
|
-
const other = permMap[perms.slice(6, 9)] || 0;
|
|
36
|
-
let fileType = 32768;
|
|
37
|
-
if (modeStr.startsWith("d")) fileType = 16384;
|
|
38
|
-
else if (modeStr.startsWith("l")) fileType = 40960;
|
|
39
|
-
else if (modeStr.startsWith("c")) fileType = 8192;
|
|
40
|
-
else if (modeStr.startsWith("b")) fileType = 24576;
|
|
41
|
-
else if (modeStr.startsWith("p")) fileType = 4096;
|
|
42
|
-
else if (modeStr.startsWith("s")) fileType = 49152;
|
|
43
|
-
return fileType | owner << 6 | group << 3 | other;
|
|
44
|
-
}
|
|
45
21
|
runCmd(cmd, timeout = 3e4) {
|
|
46
22
|
return new Promise((resolve, reject) => {
|
|
47
23
|
let timeoutId;
|
|
@@ -165,7 +141,7 @@ class SshFs {
|
|
|
165
141
|
const name = parts.slice(8).join(" ");
|
|
166
142
|
if (name === "." || name === "..") continue;
|
|
167
143
|
const type = parts[0].charAt(0);
|
|
168
|
-
const mode =
|
|
144
|
+
const mode = parseModeFromLongname(parts[0]);
|
|
169
145
|
const owner = parseInt(parts[2], 10);
|
|
170
146
|
const group = parseInt(parts[3], 10);
|
|
171
147
|
const size = parseInt(parts[4], 10);
|
|
@@ -229,7 +205,7 @@ class SshFs {
|
|
|
229
205
|
}
|
|
230
206
|
const size = parseInt(parts[4], 10);
|
|
231
207
|
const modeStr = parts[0];
|
|
232
|
-
const mode =
|
|
208
|
+
const mode = parseModeFromLongname(modeStr);
|
|
233
209
|
const uid = parseInt(parts[2], 10);
|
|
234
210
|
const gid = parseInt(parts[3], 10);
|
|
235
211
|
return {
|
|
@@ -278,7 +254,7 @@ class SshFs {
|
|
|
278
254
|
}
|
|
279
255
|
const size = parseInt(parts[4], 10);
|
|
280
256
|
const modeStr = parts[0];
|
|
281
|
-
const mode =
|
|
257
|
+
const mode = parseModeFromLongname(modeStr);
|
|
282
258
|
const uid = parseInt(parts[2], 10);
|
|
283
259
|
const gid = parseInt(parts[3], 10);
|
|
284
260
|
return {
|
|
@@ -365,7 +341,29 @@ class SshFs {
|
|
|
365
341
|
function createSshFs(session, options) {
|
|
366
342
|
return new SshFs(session, options);
|
|
367
343
|
}
|
|
344
|
+
function parseModeFromLongname(longname) {
|
|
345
|
+
if (!longname || longname.length < 10) return 0;
|
|
346
|
+
const typeMap = {
|
|
347
|
+
"-": 32768,
|
|
348
|
+
d: 16384,
|
|
349
|
+
l: 40960,
|
|
350
|
+
c: 8192,
|
|
351
|
+
b: 24576,
|
|
352
|
+
p: 4096,
|
|
353
|
+
s: 49152
|
|
354
|
+
};
|
|
355
|
+
const typeBits = typeMap[longname[0]] || 32768;
|
|
356
|
+
const permValues = [256, 128, 64, 32, 16, 8, 4, 2, 1];
|
|
357
|
+
let permBits = 0;
|
|
358
|
+
for (let i = 0; i < 9; i++) {
|
|
359
|
+
if (longname[i + 1] !== "-") {
|
|
360
|
+
permBits |= permValues[i];
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return typeBits | permBits;
|
|
364
|
+
}
|
|
368
365
|
export {
|
|
369
366
|
SshFs,
|
|
370
|
-
createSshFs
|
|
367
|
+
createSshFs,
|
|
368
|
+
parseModeFromLongname
|
|
371
369
|
};
|
package/dist/esm/transfer.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ssh2-scp",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Remote file system operations over SSH2 session without SFTP",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "npm run build:esm && npm run build:cjs",
|
|
7
7
|
"build:esm": "vite build --config build/vite.config.ts --outDir dist/esm",
|
|
8
8
|
"build:cjs": "vite build --config build/vite.config.cjs.ts --outDir dist/cjs",
|
|
9
|
-
"test": "node test/ssh2-fs.test.mjs && node test/transfer.test.mjs",
|
|
9
|
+
"test": "npm run build && node --test test/ssh2-fs.test.mjs test/folder-transfer.test.mjs && node test/transfer.test.mjs",
|
|
10
10
|
"lint": "echo \"Lint not configured\" && exit 0",
|
|
11
11
|
"typecheck": "tsc --noEmit",
|
|
12
12
|
"bench": "node benchmark/benchmark.js"
|
|
@@ -35,6 +35,11 @@
|
|
|
35
35
|
"import": "./dist/esm/transfer.js",
|
|
36
36
|
"require": "./dist/cjs/transfer.cjs",
|
|
37
37
|
"types": "./dist/esm/transfer.d.ts"
|
|
38
|
+
},
|
|
39
|
+
"./folder-transfer": {
|
|
40
|
+
"import": "./dist/esm/folder-transfer.js",
|
|
41
|
+
"require": "./dist/cjs/folder-transfer.cjs",
|
|
42
|
+
"types": "./dist/esm/folder-transfer.d.ts"
|
|
38
43
|
}
|
|
39
44
|
},
|
|
40
45
|
"files": [
|