ssh2-scp 3.0.1 → 3.2.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 +50 -1
- package/README.md +50 -1
- package/dist/cjs/folder-transfer.cjs +197 -8
- package/dist/cjs/ssh-fs.cjs +21 -8
- package/dist/esm/folder-transfer.d.ts +12 -0
- package/dist/esm/folder-transfer.js +197 -8
- package/dist/esm/ssh-fs.d.ts +11 -1
- package/dist/esm/ssh-fs.js +21 -8
- package/dist/esm/transfer.d.ts +19 -1
- package/package.json +6 -4
package/README-CN.md
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
- 通过 SSH2 shell/command 会话进行文件操作(无需 SFTP)
|
|
10
10
|
- 支持所有常用文件系统操作
|
|
11
|
+
- 支持非 UTF-8 编码(如 GBK),通过可选的 iconv-lite 实现
|
|
11
12
|
- 同时支持 ESM 和 CJS 导出
|
|
12
13
|
- 支持 TypeScript
|
|
13
14
|
|
|
@@ -49,10 +50,37 @@ client.on('ready', () => {
|
|
|
49
50
|
### 构造函数
|
|
50
51
|
|
|
51
52
|
```javascript
|
|
52
|
-
createSshFs(client)
|
|
53
|
+
createSshFs(client, options?)
|
|
53
54
|
```
|
|
54
55
|
|
|
55
56
|
- `client` - 已认证的 ssh2 Client 实例
|
|
57
|
+
- `options` - 可选配置对象:
|
|
58
|
+
- `iconv` - 用于非 UTF-8 编码支持的 iconv-lite 实例(需提供 `decode(buf, encoding)` 方法)
|
|
59
|
+
- `encoding` - 目标编码名称(默认:`'utf-8'`),仅在提供了 `iconv` 时生效
|
|
60
|
+
|
|
61
|
+
### 编码支持
|
|
62
|
+
|
|
63
|
+
默认情况下,ssh2-scp 假设远程服务器使用 UTF-8 编码。如果服务器使用其他编码(例如中文 Windows 系统的 GBK),可以传入 `iconv-lite` 实例来正确解码文件名和命令输出。
|
|
64
|
+
|
|
65
|
+
安装 `iconv-lite`:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
npm install iconv-lite
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
import iconv from 'iconv-lite'
|
|
73
|
+
import { createSshFs } from 'ssh2-scp'
|
|
74
|
+
|
|
75
|
+
// 创建支持 GBK 编码的 SshFs 实例
|
|
76
|
+
const fs = createSshFs(client, { iconv, encoding: 'gbk' })
|
|
77
|
+
|
|
78
|
+
// 文件名会从 GBK 正确解码为 Unicode
|
|
79
|
+
const files = await fs.list('/path/to/dir')
|
|
80
|
+
console.log(files[0].name) // '测试文件.txt',而不是乱码
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
> **注意:** 为了保持包的轻量,`iconv-lite` 不作为运行时依赖打包。只有在需要非 UTF-8 编码支持时才需要安装它。
|
|
56
84
|
|
|
57
85
|
### 方法
|
|
58
86
|
|
|
@@ -152,6 +180,25 @@ const transfer = new FolderTransfer(client, tar, {
|
|
|
152
180
|
await transfer.startTransfer()
|
|
153
181
|
```
|
|
154
182
|
|
|
183
|
+
### FolderTransfer 编码支持
|
|
184
|
+
|
|
185
|
+
在向非 UTF-8 文件名的服务器(如 GBK)传输文件夹时,可以传入 `iconv` 和 `encoding` 选项。文件名会在 tar 流中实时转换,通过流式 tar 头解析实现,无需远程服务器安装额外工具。
|
|
186
|
+
|
|
187
|
+
```javascript
|
|
188
|
+
import iconv from 'iconv-lite'
|
|
189
|
+
|
|
190
|
+
const transfer = new FolderTransfer(client, tar, {
|
|
191
|
+
type: 'download',
|
|
192
|
+
remotePath: '/remote/folder',
|
|
193
|
+
localPath: '/local/folder',
|
|
194
|
+
iconv,
|
|
195
|
+
encoding: 'gbk'
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
await transfer.startTransfer()
|
|
199
|
+
// 本地文件名已正确转为 UTF-8
|
|
200
|
+
```
|
|
201
|
+
|
|
155
202
|
### FolderTransfer 说明
|
|
156
203
|
|
|
157
204
|
- 构造方式:`new FolderTransfer(client, tarAdapter, options)`
|
|
@@ -159,6 +206,8 @@ await transfer.startTransfer()
|
|
|
159
206
|
- `remotePath` - 远程文件夹路径
|
|
160
207
|
- `localPath` - 本地文件夹路径
|
|
161
208
|
- `chunkSize` - tar 流管道使用的 high water mark
|
|
209
|
+
- `iconv` - 用于编码转换的 iconv-lite 实例(需提供 `encode(str, enc)` 和 `decode(buf, enc)` 方法)
|
|
210
|
+
- `encoding` - 远程文件系统编码(如 `'gbk'`),tar 流中的文件名会在该编码与 UTF-8 之间转换
|
|
162
211
|
- `tarAdapter` - 兼容 tar 的对象,需要提供 `c()` 和 `x()`;可直接传入 `tar`
|
|
163
212
|
- `pause()` / `resume()` - 暂停或继续当前流式传输
|
|
164
213
|
- `destroy()` - 中止当前文件夹传输
|
package/README.md
CHANGED
|
@@ -8,6 +8,7 @@ Remote file system operations/file transfer over SSH2 session without SFTP
|
|
|
8
8
|
|
|
9
9
|
- File operations via SSH2 shell/command session (no SFTP required)
|
|
10
10
|
- Supports all common file system operations
|
|
11
|
+
- Non-UTF-8 encoding support (e.g. GBK) via optional iconv-lite
|
|
11
12
|
- Both ESM and CJS exports
|
|
12
13
|
- TypeScript support
|
|
13
14
|
|
|
@@ -49,10 +50,37 @@ client.on('ready', () => {
|
|
|
49
50
|
### Constructor
|
|
50
51
|
|
|
51
52
|
```javascript
|
|
52
|
-
createSshFs(client)
|
|
53
|
+
createSshFs(client, options?)
|
|
53
54
|
```
|
|
54
55
|
|
|
55
56
|
- `client` - An authenticated ssh2 Client instance
|
|
57
|
+
- `options` - Optional configuration object:
|
|
58
|
+
- `iconv` - An iconv-lite instance for non-UTF-8 encoding support (must expose `decode(buf, encoding)`)
|
|
59
|
+
- `encoding` - Target encoding name (default: `'utf-8'`). Only effective when `iconv` is provided
|
|
60
|
+
|
|
61
|
+
### Encoding Support
|
|
62
|
+
|
|
63
|
+
By default, ssh2-scp assumes the remote server uses UTF-8 encoding. If your server uses a different encoding (e.g. GBK on Chinese Windows systems), pass an `iconv-lite` instance to decode file names and command output correctly.
|
|
64
|
+
|
|
65
|
+
Install `iconv-lite` in your project:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
npm install iconv-lite
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
import iconv from 'iconv-lite'
|
|
73
|
+
import { createSshFs } from 'ssh2-scp'
|
|
74
|
+
|
|
75
|
+
// Create an SshFs instance with GBK encoding support
|
|
76
|
+
const fs = createSshFs(client, { iconv, encoding: 'gbk' })
|
|
77
|
+
|
|
78
|
+
// File names are now decoded from GBK to Unicode
|
|
79
|
+
const files = await fs.list('/path/to/dir')
|
|
80
|
+
console.log(files[0].name) // '测试文件.txt' instead of garbled bytes
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
> **Note:** `iconv-lite` is not bundled as a runtime dependency to keep the package lightweight. You only need to install it if you require non-UTF-8 encoding support.
|
|
56
84
|
|
|
57
85
|
### Methods
|
|
58
86
|
|
|
@@ -152,6 +180,25 @@ const transfer = new FolderTransfer(client, tar, {
|
|
|
152
180
|
await transfer.startTransfer()
|
|
153
181
|
```
|
|
154
182
|
|
|
183
|
+
### FolderTransfer Encoding Support
|
|
184
|
+
|
|
185
|
+
When transferring folders to/from servers with non-UTF-8 filenames (e.g. GBK), pass `iconv` and `encoding` options. The filenames in the tar stream are converted in real-time using a streaming tar header transform — no remote-side tools required.
|
|
186
|
+
|
|
187
|
+
```javascript
|
|
188
|
+
import iconv from 'iconv-lite'
|
|
189
|
+
|
|
190
|
+
const transfer = new FolderTransfer(client, tar, {
|
|
191
|
+
type: 'download',
|
|
192
|
+
remotePath: '/remote/folder',
|
|
193
|
+
localPath: '/local/folder',
|
|
194
|
+
iconv,
|
|
195
|
+
encoding: 'gbk'
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
await transfer.startTransfer()
|
|
199
|
+
// Local files now have correct UTF-8 names
|
|
200
|
+
```
|
|
201
|
+
|
|
155
202
|
### FolderTransfer Notes
|
|
156
203
|
|
|
157
204
|
- Constructor: `new FolderTransfer(client, tarAdapter, options)`
|
|
@@ -159,6 +206,8 @@ await transfer.startTransfer()
|
|
|
159
206
|
- `remotePath` - Remote folder path
|
|
160
207
|
- `localPath` - Local folder path
|
|
161
208
|
- `chunkSize` - Stream high water mark used by the tar pipeline
|
|
209
|
+
- `iconv` - An iconv-lite instance for encoding conversion (must expose `encode(str, enc)` and `decode(buf, enc)`)
|
|
210
|
+
- `encoding` - Remote filesystem encoding (e.g. `'gbk'`). Filenames in the tar stream are converted between this encoding and UTF-8
|
|
162
211
|
- `tarAdapter` - A tar-compatible object that exposes `c()` and `x()`; `tar` works out of the box
|
|
163
212
|
- `pause()` / `resume()` - Pause or continue the active stream
|
|
164
213
|
- `destroy()` - Abort the current folder transfer
|
|
@@ -22,6 +22,15 @@ function _interopNamespaceDefault(e) {
|
|
|
22
22
|
}
|
|
23
23
|
const fs__namespace = /* @__PURE__ */ _interopNamespaceDefault(fs);
|
|
24
24
|
const path__namespace = /* @__PURE__ */ _interopNamespaceDefault(path);
|
|
25
|
+
const TAR_HEADER_SIZE = 512;
|
|
26
|
+
const TAR_NAME_OFFSET = 0;
|
|
27
|
+
const TAR_NAME_SIZE = 100;
|
|
28
|
+
const TAR_PREFIX_OFFSET = 345;
|
|
29
|
+
const TAR_PREFIX_SIZE = 155;
|
|
30
|
+
const TAR_CHKSUM_OFFSET = 148;
|
|
31
|
+
const TAR_CHKSUM_SIZE = 8;
|
|
32
|
+
const TAR_MAGIC_OFFSET = 257;
|
|
33
|
+
const TAR_MAGIC_SIZE = 8;
|
|
25
34
|
class FolderTransfer {
|
|
26
35
|
session;
|
|
27
36
|
tarAdapter;
|
|
@@ -33,12 +42,16 @@ class FolderTransfer {
|
|
|
33
42
|
platformPromise = null;
|
|
34
43
|
pauseTarget = null;
|
|
35
44
|
activeChannel = null;
|
|
45
|
+
iconv;
|
|
46
|
+
encoding;
|
|
36
47
|
constructor(session, tarAdapter, options) {
|
|
37
48
|
this.session = session;
|
|
38
49
|
this.tarAdapter = tarAdapter;
|
|
39
50
|
this.runRemoteCommand = this.runExec.bind(this);
|
|
40
51
|
this.options = options;
|
|
41
52
|
this.chunkSize = options.chunkSize || 32768;
|
|
53
|
+
this.iconv = options.iconv;
|
|
54
|
+
this.encoding = (options.encoding || "utf-8").toLowerCase().replace(/[^a-z0-9-]/g, "");
|
|
42
55
|
this.state = {
|
|
43
56
|
transferred: 0,
|
|
44
57
|
total: 0,
|
|
@@ -84,6 +97,166 @@ class FolderTransfer {
|
|
|
84
97
|
createAbortError() {
|
|
85
98
|
return new Error("Folder transfer aborted");
|
|
86
99
|
}
|
|
100
|
+
needsEncodingConversion() {
|
|
101
|
+
return !!this.iconv && this.encoding !== "utf-8" && this.encoding !== "utf8" && this.encoding !== "ascii";
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Creates a Transform stream that converts filename encoding in tar headers.
|
|
105
|
+
* Handles both USTAR headers and PAX extended headers.
|
|
106
|
+
* For PAX headers, parses the key=value pairs and converts the `path` value.
|
|
107
|
+
* For USTAR headers, converts the name/prefix fields directly.
|
|
108
|
+
* Recomputes checksums and sizes after modification.
|
|
109
|
+
*/
|
|
110
|
+
createTarEncodingTransform(srcEnc, dstEnc) {
|
|
111
|
+
const iconv = this.iconv;
|
|
112
|
+
let remaining = Buffer.alloc(0);
|
|
113
|
+
let skipBytes = 0;
|
|
114
|
+
const convertPath = (buf) => {
|
|
115
|
+
try {
|
|
116
|
+
return iconv.encode(iconv.decode(buf, srcEnc), dstEnc);
|
|
117
|
+
} catch {
|
|
118
|
+
return buf;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
return new stream.Transform({
|
|
122
|
+
transform(chunk, _encoding, callback) {
|
|
123
|
+
const buf = remaining.length > 0 ? Buffer.concat([remaining, chunk]) : Buffer.from(chunk);
|
|
124
|
+
let pos = 0;
|
|
125
|
+
const parts = [];
|
|
126
|
+
while (pos < buf.length) {
|
|
127
|
+
if (skipBytes > 0) {
|
|
128
|
+
const n = Math.min(skipBytes, buf.length - pos);
|
|
129
|
+
parts.push(buf.subarray(pos, pos + n));
|
|
130
|
+
pos += n;
|
|
131
|
+
skipBytes -= n;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (pos + TAR_HEADER_SIZE > buf.length) break;
|
|
135
|
+
const block = buf.subarray(pos, pos + TAR_HEADER_SIZE);
|
|
136
|
+
if (block[0] === 0 && block.every((b) => b === 0)) {
|
|
137
|
+
parts.push(buf.subarray(pos));
|
|
138
|
+
remaining = Buffer.alloc(0);
|
|
139
|
+
callback(null, Buffer.concat(parts));
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const magic = block.subarray(TAR_MAGIC_OFFSET, TAR_MAGIC_OFFSET + TAR_MAGIC_SIZE).toString("ascii").replace(/\0/g, "").trim();
|
|
143
|
+
if (magic !== "ustar") {
|
|
144
|
+
parts.push(block);
|
|
145
|
+
pos += TAR_HEADER_SIZE;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
const typeflag = String.fromCharCode(block[156]);
|
|
149
|
+
const sizeStr = block.subarray(124, 136).toString("ascii").replace(/\0/g, "").trim();
|
|
150
|
+
const dataSize = parseInt(sizeStr, 8) || 0;
|
|
151
|
+
const dataBlocks = Math.ceil(dataSize / TAR_HEADER_SIZE) * TAR_HEADER_SIZE;
|
|
152
|
+
if ((typeflag === "x" || typeflag === "g") && dataSize > 0) {
|
|
153
|
+
const totalBlocks = TAR_HEADER_SIZE + dataBlocks;
|
|
154
|
+
if (pos + totalBlocks > buf.length) break;
|
|
155
|
+
const paxData = buf.subarray(pos + TAR_HEADER_SIZE, pos + TAR_HEADER_SIZE + dataSize);
|
|
156
|
+
const paxStr = paxData.toString("utf-8");
|
|
157
|
+
const paxLines = paxStr.split("\n").filter((l) => l.length > 0);
|
|
158
|
+
let modified = false;
|
|
159
|
+
const newLines = [];
|
|
160
|
+
for (const line of paxLines) {
|
|
161
|
+
const spaceIdx = line.indexOf(" ");
|
|
162
|
+
if (spaceIdx === -1) {
|
|
163
|
+
newLines.push(line);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const kv = line.substring(spaceIdx + 1);
|
|
167
|
+
const eqIdx = kv.indexOf("=");
|
|
168
|
+
if (eqIdx === -1) {
|
|
169
|
+
newLines.push(line);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
const keyword = kv.substring(0, eqIdx);
|
|
173
|
+
const value = kv.substring(eqIdx + 1);
|
|
174
|
+
if (keyword === "path") {
|
|
175
|
+
const valueBuf = Buffer.from(value, "utf-8");
|
|
176
|
+
const converted2 = convertPath(valueBuf);
|
|
177
|
+
if (!converted2.equals(valueBuf)) {
|
|
178
|
+
const newValue = converted2.toString("utf-8");
|
|
179
|
+
const newKv = `${keyword}=${newValue}`;
|
|
180
|
+
const newLine = `${Buffer.byteLength(newKv, "utf-8") + 1} ${newKv}`;
|
|
181
|
+
newLines.push(newLine);
|
|
182
|
+
modified = true;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
newLines.push(line);
|
|
187
|
+
}
|
|
188
|
+
if (modified) {
|
|
189
|
+
const newPaxPayload = Buffer.from(newLines.join("\n") + "\n", "utf-8");
|
|
190
|
+
const header2 = Buffer.alloc(TAR_HEADER_SIZE);
|
|
191
|
+
block.copy(header2);
|
|
192
|
+
const newSizeStr = newPaxPayload.length.toString(8).padStart(11, "0") + "\0";
|
|
193
|
+
Buffer.from(newSizeStr).copy(header2, 124);
|
|
194
|
+
header2.fill(32, TAR_CHKSUM_OFFSET, TAR_CHKSUM_OFFSET + TAR_CHKSUM_SIZE);
|
|
195
|
+
let sum = 0;
|
|
196
|
+
for (let i = 0; i < TAR_HEADER_SIZE; i++) sum += header2[i];
|
|
197
|
+
Buffer.from(sum.toString(8).padStart(6, "0") + "\0 ").copy(header2, TAR_CHKSUM_OFFSET);
|
|
198
|
+
parts.push(header2);
|
|
199
|
+
pos += TAR_HEADER_SIZE;
|
|
200
|
+
const newBlocks = Math.ceil(newPaxPayload.length / TAR_HEADER_SIZE) * TAR_HEADER_SIZE;
|
|
201
|
+
const padded = Buffer.alloc(newBlocks);
|
|
202
|
+
newPaxPayload.copy(padded);
|
|
203
|
+
parts.push(padded);
|
|
204
|
+
pos += dataBlocks;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
parts.push(buf.subarray(pos, pos + totalBlocks));
|
|
208
|
+
pos += totalBlocks;
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const header = Buffer.from(block);
|
|
212
|
+
const nameBytes = header.subarray(TAR_NAME_OFFSET, TAR_NAME_OFFSET + TAR_NAME_SIZE);
|
|
213
|
+
const prefixBytes = header.subarray(TAR_PREFIX_OFFSET, TAR_PREFIX_OFFSET + TAR_PREFIX_SIZE);
|
|
214
|
+
const nameEnd = nameBytes.indexOf(0);
|
|
215
|
+
const prefixEnd = prefixBytes.indexOf(0);
|
|
216
|
+
const nameLen = nameEnd === -1 ? TAR_NAME_SIZE : nameEnd;
|
|
217
|
+
const prefixLen = prefixEnd === -1 ? TAR_PREFIX_SIZE : prefixEnd;
|
|
218
|
+
let fullPath;
|
|
219
|
+
if (prefixLen > 0) {
|
|
220
|
+
fullPath = Buffer.concat([prefixBytes.subarray(0, prefixLen), Buffer.from("/"), nameBytes.subarray(0, nameLen)]);
|
|
221
|
+
} else {
|
|
222
|
+
fullPath = nameBytes.subarray(0, nameLen);
|
|
223
|
+
}
|
|
224
|
+
const converted = convertPath(fullPath);
|
|
225
|
+
if (!converted.equals(fullPath)) {
|
|
226
|
+
header.fill(0, TAR_NAME_OFFSET, TAR_NAME_OFFSET + TAR_NAME_SIZE);
|
|
227
|
+
header.fill(0, TAR_PREFIX_OFFSET, TAR_PREFIX_OFFSET + TAR_PREFIX_SIZE);
|
|
228
|
+
if (converted.length <= TAR_NAME_SIZE) {
|
|
229
|
+
converted.copy(header, TAR_NAME_OFFSET);
|
|
230
|
+
} else {
|
|
231
|
+
const str = converted.toString("utf-8");
|
|
232
|
+
const ls = str.lastIndexOf("/");
|
|
233
|
+
if (ls > 0 && ls < str.length - 1) {
|
|
234
|
+
const pre = converted.subarray(0, ls);
|
|
235
|
+
const nm = converted.subarray(ls + 1);
|
|
236
|
+
if (pre.length <= TAR_PREFIX_SIZE && nm.length <= TAR_NAME_SIZE) {
|
|
237
|
+
pre.copy(header, TAR_PREFIX_OFFSET);
|
|
238
|
+
nm.copy(header, TAR_NAME_OFFSET);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
const tmp = Buffer.from(header);
|
|
243
|
+
tmp.fill(32, TAR_CHKSUM_OFFSET, TAR_CHKSUM_OFFSET + TAR_CHKSUM_SIZE);
|
|
244
|
+
let sum = 0;
|
|
245
|
+
for (let i = 0; i < TAR_HEADER_SIZE; i++) sum += tmp[i];
|
|
246
|
+
Buffer.from(sum.toString(8).padStart(6, "0") + "\0 ").copy(header, TAR_CHKSUM_OFFSET);
|
|
247
|
+
}
|
|
248
|
+
parts.push(header);
|
|
249
|
+
pos += TAR_HEADER_SIZE;
|
|
250
|
+
skipBytes = dataBlocks;
|
|
251
|
+
}
|
|
252
|
+
remaining = pos < buf.length ? buf.subarray(pos) : Buffer.alloc(0);
|
|
253
|
+
callback(null, parts.length > 0 ? Buffer.concat(parts) : void 0);
|
|
254
|
+
},
|
|
255
|
+
flush(callback) {
|
|
256
|
+
callback(null, remaining.length > 0 ? remaining : void 0);
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
}
|
|
87
260
|
async download() {
|
|
88
261
|
const { remotePath, localPath } = this.options;
|
|
89
262
|
const platform = await this.getRemotePlatform();
|
|
@@ -96,10 +269,18 @@ class FolderTransfer {
|
|
|
96
269
|
const closePromise = this.waitForChannelClose(channel);
|
|
97
270
|
const progressStream = this.createProgressStream();
|
|
98
271
|
const extractStream = this.tarAdapter.x({ cwd: localPath, strict: true });
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
272
|
+
if (this.needsEncodingConversion()) {
|
|
273
|
+
const encodingTransform = this.createTarEncodingTransform(this.encoding, "utf-8");
|
|
274
|
+
await Promise.all([
|
|
275
|
+
promises.pipeline(channel, progressStream, encodingTransform, extractStream),
|
|
276
|
+
closePromise
|
|
277
|
+
]);
|
|
278
|
+
} else {
|
|
279
|
+
await Promise.all([
|
|
280
|
+
promises.pipeline(channel, progressStream, extractStream),
|
|
281
|
+
closePromise
|
|
282
|
+
]);
|
|
283
|
+
}
|
|
103
284
|
}
|
|
104
285
|
async upload() {
|
|
105
286
|
const { remotePath, localPath } = this.options;
|
|
@@ -117,10 +298,18 @@ class FolderTransfer {
|
|
|
117
298
|
this.pauseTarget = packStream;
|
|
118
299
|
const progressStream = this.createProgressStream();
|
|
119
300
|
const remoteStdin = this.getChannelWritable(channel);
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
301
|
+
if (this.needsEncodingConversion()) {
|
|
302
|
+
const encodingTransform = this.createTarEncodingTransform("utf-8", this.encoding);
|
|
303
|
+
await Promise.all([
|
|
304
|
+
promises.pipeline(packStream, progressStream, encodingTransform, remoteStdin),
|
|
305
|
+
closePromise
|
|
306
|
+
]);
|
|
307
|
+
} else {
|
|
308
|
+
await Promise.all([
|
|
309
|
+
promises.pipeline(packStream, progressStream, remoteStdin),
|
|
310
|
+
closePromise
|
|
311
|
+
]);
|
|
312
|
+
}
|
|
124
313
|
}
|
|
125
314
|
async getRemotePlatform() {
|
|
126
315
|
if (!this.platformPromise) {
|
package/dist/cjs/ssh-fs.cjs
CHANGED
|
@@ -3,11 +3,21 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
class SshFs {
|
|
4
4
|
session;
|
|
5
5
|
_chunkSize = null;
|
|
6
|
+
iconv;
|
|
7
|
+
encoding;
|
|
6
8
|
async getChunkSize() {
|
|
7
9
|
return this.detectChunkSize();
|
|
8
10
|
}
|
|
9
|
-
constructor(session,
|
|
11
|
+
constructor(session, options) {
|
|
10
12
|
this.session = session;
|
|
13
|
+
this.iconv = options?.iconv;
|
|
14
|
+
this.encoding = options?.encoding || "utf-8";
|
|
15
|
+
}
|
|
16
|
+
decodeBuffer(buf) {
|
|
17
|
+
if (this.iconv) {
|
|
18
|
+
return this.iconv.decode(buf, this.encoding);
|
|
19
|
+
}
|
|
20
|
+
return buf.toString();
|
|
11
21
|
}
|
|
12
22
|
async detectChunkSize() {
|
|
13
23
|
this._chunkSize = 4 * 1024;
|
|
@@ -16,6 +26,9 @@ class SshFs {
|
|
|
16
26
|
getExecOpts() {
|
|
17
27
|
return {};
|
|
18
28
|
}
|
|
29
|
+
maskMode(mode) {
|
|
30
|
+
return mode & 4095;
|
|
31
|
+
}
|
|
19
32
|
getMonthIndex(month) {
|
|
20
33
|
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
|
21
34
|
return months.indexOf(month);
|
|
@@ -37,11 +50,11 @@ class SshFs {
|
|
|
37
50
|
} else {
|
|
38
51
|
let out = Buffer.from("");
|
|
39
52
|
stream.on("end", () => {
|
|
40
|
-
resolve(
|
|
53
|
+
resolve(this.decodeBuffer(out));
|
|
41
54
|
}).on("data", (data) => {
|
|
42
55
|
out = Buffer.concat([out, data]);
|
|
43
56
|
}).stderr.on("data", (data) => {
|
|
44
|
-
reject(
|
|
57
|
+
reject(this.decodeBuffer(data));
|
|
45
58
|
});
|
|
46
59
|
}
|
|
47
60
|
});
|
|
@@ -112,11 +125,11 @@ class SshFs {
|
|
|
112
125
|
} else {
|
|
113
126
|
let out = Buffer.from("");
|
|
114
127
|
stream.on("end", () => {
|
|
115
|
-
resolve(
|
|
128
|
+
resolve(this.decodeBuffer(out));
|
|
116
129
|
}).on("data", (data) => {
|
|
117
130
|
out = Buffer.concat([out, data]);
|
|
118
131
|
}).stderr.on("data", (data) => {
|
|
119
|
-
reject(
|
|
132
|
+
reject(this.decodeBuffer(data));
|
|
120
133
|
});
|
|
121
134
|
}
|
|
122
135
|
});
|
|
@@ -181,7 +194,7 @@ class SshFs {
|
|
|
181
194
|
return this.listFiles(remotePath);
|
|
182
195
|
}
|
|
183
196
|
async mkdir(remotePath, options = {}) {
|
|
184
|
-
const cmd = options.mode ? `mkdir -m ${options.mode.toString(8)} -p "${remotePath}"` : `mkdir -p "${remotePath}"`;
|
|
197
|
+
const cmd = options.mode ? `mkdir -m ${this.maskMode(options.mode).toString(8)} -p "${remotePath}"` : `mkdir -p "${remotePath}"`;
|
|
185
198
|
return this.runCmd(cmd);
|
|
186
199
|
}
|
|
187
200
|
async stat(remotePath) {
|
|
@@ -276,7 +289,7 @@ class SshFs {
|
|
|
276
289
|
};
|
|
277
290
|
}
|
|
278
291
|
chmod(remotePath, mode) {
|
|
279
|
-
return this.runCmd(`chmod ${mode.toString(8)} "${remotePath}"`);
|
|
292
|
+
return this.runCmd(`chmod ${this.maskMode(mode).toString(8)} "${remotePath}"`);
|
|
280
293
|
}
|
|
281
294
|
rename(remotePath, remotePathNew) {
|
|
282
295
|
return this.runCmd(`mv "${remotePath}" "${remotePathNew}"`);
|
|
@@ -336,7 +349,7 @@ class SshFs {
|
|
|
336
349
|
}
|
|
337
350
|
}
|
|
338
351
|
if (mode) {
|
|
339
|
-
await this.runCmd(`chmod ${mode.toString(8)} "${remotePath}"`);
|
|
352
|
+
await this.runCmd(`chmod ${this.maskMode(mode).toString(8)} "${remotePath}"`);
|
|
340
353
|
}
|
|
341
354
|
}
|
|
342
355
|
}
|
|
@@ -11,6 +11,8 @@ export declare class FolderTransfer {
|
|
|
11
11
|
private platformPromise;
|
|
12
12
|
private pauseTarget;
|
|
13
13
|
private activeChannel;
|
|
14
|
+
private iconv?;
|
|
15
|
+
private encoding;
|
|
14
16
|
constructor(session: Client, tarAdapter: FolderTransferTarAdapter, options: TransferOptions);
|
|
15
17
|
startTransfer(): Promise<void>;
|
|
16
18
|
pause(): void;
|
|
@@ -18,6 +20,8 @@ export declare class FolderTransfer {
|
|
|
18
20
|
getState(): TransferState;
|
|
19
21
|
destroy(): void;
|
|
20
22
|
private createAbortError;
|
|
23
|
+
private needsEncodingConversion;
|
|
24
|
+
private createTarEncodingTransform;
|
|
21
25
|
private download;
|
|
22
26
|
private upload;
|
|
23
27
|
private getRemotePlatform;
|
|
@@ -47,11 +51,19 @@ export declare interface FolderTransferTarAdapter {
|
|
|
47
51
|
}): NodeJS.WritableStream;
|
|
48
52
|
}
|
|
49
53
|
|
|
54
|
+
declare interface IconvDecoder {
|
|
55
|
+
decode: (buf: Buffer, encoding: string) => string;
|
|
56
|
+
}
|
|
57
|
+
|
|
50
58
|
export declare interface TransferOptions {
|
|
51
59
|
type: TransferType;
|
|
52
60
|
remotePath: string;
|
|
53
61
|
localPath: string;
|
|
54
62
|
chunkSize?: number;
|
|
63
|
+
iconv?: IconvDecoder & {
|
|
64
|
+
encode: (str: string, encoding: string) => Buffer;
|
|
65
|
+
};
|
|
66
|
+
encoding?: string;
|
|
55
67
|
onProgress?: (transferred: number, total: number) => void;
|
|
56
68
|
onData?: (count: number) => void;
|
|
57
69
|
}
|
|
@@ -2,6 +2,15 @@ import * as fs from "fs";
|
|
|
2
2
|
import * as path from "path";
|
|
3
3
|
import { Transform } from "stream";
|
|
4
4
|
import { pipeline } from "stream/promises";
|
|
5
|
+
const TAR_HEADER_SIZE = 512;
|
|
6
|
+
const TAR_NAME_OFFSET = 0;
|
|
7
|
+
const TAR_NAME_SIZE = 100;
|
|
8
|
+
const TAR_PREFIX_OFFSET = 345;
|
|
9
|
+
const TAR_PREFIX_SIZE = 155;
|
|
10
|
+
const TAR_CHKSUM_OFFSET = 148;
|
|
11
|
+
const TAR_CHKSUM_SIZE = 8;
|
|
12
|
+
const TAR_MAGIC_OFFSET = 257;
|
|
13
|
+
const TAR_MAGIC_SIZE = 8;
|
|
5
14
|
class FolderTransfer {
|
|
6
15
|
session;
|
|
7
16
|
tarAdapter;
|
|
@@ -13,12 +22,16 @@ class FolderTransfer {
|
|
|
13
22
|
platformPromise = null;
|
|
14
23
|
pauseTarget = null;
|
|
15
24
|
activeChannel = null;
|
|
25
|
+
iconv;
|
|
26
|
+
encoding;
|
|
16
27
|
constructor(session, tarAdapter, options) {
|
|
17
28
|
this.session = session;
|
|
18
29
|
this.tarAdapter = tarAdapter;
|
|
19
30
|
this.runRemoteCommand = this.runExec.bind(this);
|
|
20
31
|
this.options = options;
|
|
21
32
|
this.chunkSize = options.chunkSize || 32768;
|
|
33
|
+
this.iconv = options.iconv;
|
|
34
|
+
this.encoding = (options.encoding || "utf-8").toLowerCase().replace(/[^a-z0-9-]/g, "");
|
|
22
35
|
this.state = {
|
|
23
36
|
transferred: 0,
|
|
24
37
|
total: 0,
|
|
@@ -64,6 +77,166 @@ class FolderTransfer {
|
|
|
64
77
|
createAbortError() {
|
|
65
78
|
return new Error("Folder transfer aborted");
|
|
66
79
|
}
|
|
80
|
+
needsEncodingConversion() {
|
|
81
|
+
return !!this.iconv && this.encoding !== "utf-8" && this.encoding !== "utf8" && this.encoding !== "ascii";
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Creates a Transform stream that converts filename encoding in tar headers.
|
|
85
|
+
* Handles both USTAR headers and PAX extended headers.
|
|
86
|
+
* For PAX headers, parses the key=value pairs and converts the `path` value.
|
|
87
|
+
* For USTAR headers, converts the name/prefix fields directly.
|
|
88
|
+
* Recomputes checksums and sizes after modification.
|
|
89
|
+
*/
|
|
90
|
+
createTarEncodingTransform(srcEnc, dstEnc) {
|
|
91
|
+
const iconv = this.iconv;
|
|
92
|
+
let remaining = Buffer.alloc(0);
|
|
93
|
+
let skipBytes = 0;
|
|
94
|
+
const convertPath = (buf) => {
|
|
95
|
+
try {
|
|
96
|
+
return iconv.encode(iconv.decode(buf, srcEnc), dstEnc);
|
|
97
|
+
} catch {
|
|
98
|
+
return buf;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
return new Transform({
|
|
102
|
+
transform(chunk, _encoding, callback) {
|
|
103
|
+
const buf = remaining.length > 0 ? Buffer.concat([remaining, chunk]) : Buffer.from(chunk);
|
|
104
|
+
let pos = 0;
|
|
105
|
+
const parts = [];
|
|
106
|
+
while (pos < buf.length) {
|
|
107
|
+
if (skipBytes > 0) {
|
|
108
|
+
const n = Math.min(skipBytes, buf.length - pos);
|
|
109
|
+
parts.push(buf.subarray(pos, pos + n));
|
|
110
|
+
pos += n;
|
|
111
|
+
skipBytes -= n;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (pos + TAR_HEADER_SIZE > buf.length) break;
|
|
115
|
+
const block = buf.subarray(pos, pos + TAR_HEADER_SIZE);
|
|
116
|
+
if (block[0] === 0 && block.every((b) => b === 0)) {
|
|
117
|
+
parts.push(buf.subarray(pos));
|
|
118
|
+
remaining = Buffer.alloc(0);
|
|
119
|
+
callback(null, Buffer.concat(parts));
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const magic = block.subarray(TAR_MAGIC_OFFSET, TAR_MAGIC_OFFSET + TAR_MAGIC_SIZE).toString("ascii").replace(/\0/g, "").trim();
|
|
123
|
+
if (magic !== "ustar") {
|
|
124
|
+
parts.push(block);
|
|
125
|
+
pos += TAR_HEADER_SIZE;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const typeflag = String.fromCharCode(block[156]);
|
|
129
|
+
const sizeStr = block.subarray(124, 136).toString("ascii").replace(/\0/g, "").trim();
|
|
130
|
+
const dataSize = parseInt(sizeStr, 8) || 0;
|
|
131
|
+
const dataBlocks = Math.ceil(dataSize / TAR_HEADER_SIZE) * TAR_HEADER_SIZE;
|
|
132
|
+
if ((typeflag === "x" || typeflag === "g") && dataSize > 0) {
|
|
133
|
+
const totalBlocks = TAR_HEADER_SIZE + dataBlocks;
|
|
134
|
+
if (pos + totalBlocks > buf.length) break;
|
|
135
|
+
const paxData = buf.subarray(pos + TAR_HEADER_SIZE, pos + TAR_HEADER_SIZE + dataSize);
|
|
136
|
+
const paxStr = paxData.toString("utf-8");
|
|
137
|
+
const paxLines = paxStr.split("\n").filter((l) => l.length > 0);
|
|
138
|
+
let modified = false;
|
|
139
|
+
const newLines = [];
|
|
140
|
+
for (const line of paxLines) {
|
|
141
|
+
const spaceIdx = line.indexOf(" ");
|
|
142
|
+
if (spaceIdx === -1) {
|
|
143
|
+
newLines.push(line);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const kv = line.substring(spaceIdx + 1);
|
|
147
|
+
const eqIdx = kv.indexOf("=");
|
|
148
|
+
if (eqIdx === -1) {
|
|
149
|
+
newLines.push(line);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const keyword = kv.substring(0, eqIdx);
|
|
153
|
+
const value = kv.substring(eqIdx + 1);
|
|
154
|
+
if (keyword === "path") {
|
|
155
|
+
const valueBuf = Buffer.from(value, "utf-8");
|
|
156
|
+
const converted2 = convertPath(valueBuf);
|
|
157
|
+
if (!converted2.equals(valueBuf)) {
|
|
158
|
+
const newValue = converted2.toString("utf-8");
|
|
159
|
+
const newKv = `${keyword}=${newValue}`;
|
|
160
|
+
const newLine = `${Buffer.byteLength(newKv, "utf-8") + 1} ${newKv}`;
|
|
161
|
+
newLines.push(newLine);
|
|
162
|
+
modified = true;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
newLines.push(line);
|
|
167
|
+
}
|
|
168
|
+
if (modified) {
|
|
169
|
+
const newPaxPayload = Buffer.from(newLines.join("\n") + "\n", "utf-8");
|
|
170
|
+
const header2 = Buffer.alloc(TAR_HEADER_SIZE);
|
|
171
|
+
block.copy(header2);
|
|
172
|
+
const newSizeStr = newPaxPayload.length.toString(8).padStart(11, "0") + "\0";
|
|
173
|
+
Buffer.from(newSizeStr).copy(header2, 124);
|
|
174
|
+
header2.fill(32, TAR_CHKSUM_OFFSET, TAR_CHKSUM_OFFSET + TAR_CHKSUM_SIZE);
|
|
175
|
+
let sum = 0;
|
|
176
|
+
for (let i = 0; i < TAR_HEADER_SIZE; i++) sum += header2[i];
|
|
177
|
+
Buffer.from(sum.toString(8).padStart(6, "0") + "\0 ").copy(header2, TAR_CHKSUM_OFFSET);
|
|
178
|
+
parts.push(header2);
|
|
179
|
+
pos += TAR_HEADER_SIZE;
|
|
180
|
+
const newBlocks = Math.ceil(newPaxPayload.length / TAR_HEADER_SIZE) * TAR_HEADER_SIZE;
|
|
181
|
+
const padded = Buffer.alloc(newBlocks);
|
|
182
|
+
newPaxPayload.copy(padded);
|
|
183
|
+
parts.push(padded);
|
|
184
|
+
pos += dataBlocks;
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
parts.push(buf.subarray(pos, pos + totalBlocks));
|
|
188
|
+
pos += totalBlocks;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const header = Buffer.from(block);
|
|
192
|
+
const nameBytes = header.subarray(TAR_NAME_OFFSET, TAR_NAME_OFFSET + TAR_NAME_SIZE);
|
|
193
|
+
const prefixBytes = header.subarray(TAR_PREFIX_OFFSET, TAR_PREFIX_OFFSET + TAR_PREFIX_SIZE);
|
|
194
|
+
const nameEnd = nameBytes.indexOf(0);
|
|
195
|
+
const prefixEnd = prefixBytes.indexOf(0);
|
|
196
|
+
const nameLen = nameEnd === -1 ? TAR_NAME_SIZE : nameEnd;
|
|
197
|
+
const prefixLen = prefixEnd === -1 ? TAR_PREFIX_SIZE : prefixEnd;
|
|
198
|
+
let fullPath;
|
|
199
|
+
if (prefixLen > 0) {
|
|
200
|
+
fullPath = Buffer.concat([prefixBytes.subarray(0, prefixLen), Buffer.from("/"), nameBytes.subarray(0, nameLen)]);
|
|
201
|
+
} else {
|
|
202
|
+
fullPath = nameBytes.subarray(0, nameLen);
|
|
203
|
+
}
|
|
204
|
+
const converted = convertPath(fullPath);
|
|
205
|
+
if (!converted.equals(fullPath)) {
|
|
206
|
+
header.fill(0, TAR_NAME_OFFSET, TAR_NAME_OFFSET + TAR_NAME_SIZE);
|
|
207
|
+
header.fill(0, TAR_PREFIX_OFFSET, TAR_PREFIX_OFFSET + TAR_PREFIX_SIZE);
|
|
208
|
+
if (converted.length <= TAR_NAME_SIZE) {
|
|
209
|
+
converted.copy(header, TAR_NAME_OFFSET);
|
|
210
|
+
} else {
|
|
211
|
+
const str = converted.toString("utf-8");
|
|
212
|
+
const ls = str.lastIndexOf("/");
|
|
213
|
+
if (ls > 0 && ls < str.length - 1) {
|
|
214
|
+
const pre = converted.subarray(0, ls);
|
|
215
|
+
const nm = converted.subarray(ls + 1);
|
|
216
|
+
if (pre.length <= TAR_PREFIX_SIZE && nm.length <= TAR_NAME_SIZE) {
|
|
217
|
+
pre.copy(header, TAR_PREFIX_OFFSET);
|
|
218
|
+
nm.copy(header, TAR_NAME_OFFSET);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const tmp = Buffer.from(header);
|
|
223
|
+
tmp.fill(32, TAR_CHKSUM_OFFSET, TAR_CHKSUM_OFFSET + TAR_CHKSUM_SIZE);
|
|
224
|
+
let sum = 0;
|
|
225
|
+
for (let i = 0; i < TAR_HEADER_SIZE; i++) sum += tmp[i];
|
|
226
|
+
Buffer.from(sum.toString(8).padStart(6, "0") + "\0 ").copy(header, TAR_CHKSUM_OFFSET);
|
|
227
|
+
}
|
|
228
|
+
parts.push(header);
|
|
229
|
+
pos += TAR_HEADER_SIZE;
|
|
230
|
+
skipBytes = dataBlocks;
|
|
231
|
+
}
|
|
232
|
+
remaining = pos < buf.length ? buf.subarray(pos) : Buffer.alloc(0);
|
|
233
|
+
callback(null, parts.length > 0 ? Buffer.concat(parts) : void 0);
|
|
234
|
+
},
|
|
235
|
+
flush(callback) {
|
|
236
|
+
callback(null, remaining.length > 0 ? remaining : void 0);
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
}
|
|
67
240
|
async download() {
|
|
68
241
|
const { remotePath, localPath } = this.options;
|
|
69
242
|
const platform = await this.getRemotePlatform();
|
|
@@ -76,10 +249,18 @@ class FolderTransfer {
|
|
|
76
249
|
const closePromise = this.waitForChannelClose(channel);
|
|
77
250
|
const progressStream = this.createProgressStream();
|
|
78
251
|
const extractStream = this.tarAdapter.x({ cwd: localPath, strict: true });
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
252
|
+
if (this.needsEncodingConversion()) {
|
|
253
|
+
const encodingTransform = this.createTarEncodingTransform(this.encoding, "utf-8");
|
|
254
|
+
await Promise.all([
|
|
255
|
+
pipeline(channel, progressStream, encodingTransform, extractStream),
|
|
256
|
+
closePromise
|
|
257
|
+
]);
|
|
258
|
+
} else {
|
|
259
|
+
await Promise.all([
|
|
260
|
+
pipeline(channel, progressStream, extractStream),
|
|
261
|
+
closePromise
|
|
262
|
+
]);
|
|
263
|
+
}
|
|
83
264
|
}
|
|
84
265
|
async upload() {
|
|
85
266
|
const { remotePath, localPath } = this.options;
|
|
@@ -97,10 +278,18 @@ class FolderTransfer {
|
|
|
97
278
|
this.pauseTarget = packStream;
|
|
98
279
|
const progressStream = this.createProgressStream();
|
|
99
280
|
const remoteStdin = this.getChannelWritable(channel);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
281
|
+
if (this.needsEncodingConversion()) {
|
|
282
|
+
const encodingTransform = this.createTarEncodingTransform("utf-8", this.encoding);
|
|
283
|
+
await Promise.all([
|
|
284
|
+
pipeline(packStream, progressStream, encodingTransform, remoteStdin),
|
|
285
|
+
closePromise
|
|
286
|
+
]);
|
|
287
|
+
} else {
|
|
288
|
+
await Promise.all([
|
|
289
|
+
pipeline(packStream, progressStream, remoteStdin),
|
|
290
|
+
closePromise
|
|
291
|
+
]);
|
|
292
|
+
}
|
|
104
293
|
}
|
|
105
294
|
async getRemotePlatform() {
|
|
106
295
|
if (!this.platformPromise) {
|
package/dist/esm/ssh-fs.d.ts
CHANGED
|
@@ -18,15 +18,23 @@ export declare interface FileInfo {
|
|
|
18
18
|
group: number;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
export declare interface IconvDecoder {
|
|
22
|
+
decode: (buf: Buffer, encoding: string) => string;
|
|
23
|
+
}
|
|
24
|
+
|
|
21
25
|
export declare function parseModeFromLongname(longname: string): number;
|
|
22
26
|
|
|
23
27
|
export declare class SshFs {
|
|
24
28
|
private session;
|
|
25
29
|
private _chunkSize;
|
|
30
|
+
private iconv?;
|
|
31
|
+
private encoding;
|
|
26
32
|
getChunkSize(): Promise<number>;
|
|
27
|
-
constructor(session: Client,
|
|
33
|
+
constructor(session: Client, options?: SshFsOptions);
|
|
34
|
+
private decodeBuffer;
|
|
28
35
|
private detectChunkSize;
|
|
29
36
|
private getExecOpts;
|
|
37
|
+
private maskMode;
|
|
30
38
|
private getMonthIndex;
|
|
31
39
|
private runCmd;
|
|
32
40
|
private rmFolderCmd;
|
|
@@ -65,6 +73,8 @@ export declare class SshFs {
|
|
|
65
73
|
}
|
|
66
74
|
|
|
67
75
|
export declare interface SshFsOptions {
|
|
76
|
+
iconv?: IconvDecoder;
|
|
77
|
+
encoding?: string;
|
|
68
78
|
}
|
|
69
79
|
|
|
70
80
|
export declare interface Stats {
|
package/dist/esm/ssh-fs.js
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
class SshFs {
|
|
2
2
|
session;
|
|
3
3
|
_chunkSize = null;
|
|
4
|
+
iconv;
|
|
5
|
+
encoding;
|
|
4
6
|
async getChunkSize() {
|
|
5
7
|
return this.detectChunkSize();
|
|
6
8
|
}
|
|
7
|
-
constructor(session,
|
|
9
|
+
constructor(session, options) {
|
|
8
10
|
this.session = session;
|
|
11
|
+
this.iconv = options?.iconv;
|
|
12
|
+
this.encoding = options?.encoding || "utf-8";
|
|
13
|
+
}
|
|
14
|
+
decodeBuffer(buf) {
|
|
15
|
+
if (this.iconv) {
|
|
16
|
+
return this.iconv.decode(buf, this.encoding);
|
|
17
|
+
}
|
|
18
|
+
return buf.toString();
|
|
9
19
|
}
|
|
10
20
|
async detectChunkSize() {
|
|
11
21
|
this._chunkSize = 4 * 1024;
|
|
@@ -14,6 +24,9 @@ class SshFs {
|
|
|
14
24
|
getExecOpts() {
|
|
15
25
|
return {};
|
|
16
26
|
}
|
|
27
|
+
maskMode(mode) {
|
|
28
|
+
return mode & 4095;
|
|
29
|
+
}
|
|
17
30
|
getMonthIndex(month) {
|
|
18
31
|
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
|
19
32
|
return months.indexOf(month);
|
|
@@ -35,11 +48,11 @@ class SshFs {
|
|
|
35
48
|
} else {
|
|
36
49
|
let out = Buffer.from("");
|
|
37
50
|
stream.on("end", () => {
|
|
38
|
-
resolve(
|
|
51
|
+
resolve(this.decodeBuffer(out));
|
|
39
52
|
}).on("data", (data) => {
|
|
40
53
|
out = Buffer.concat([out, data]);
|
|
41
54
|
}).stderr.on("data", (data) => {
|
|
42
|
-
reject(
|
|
55
|
+
reject(this.decodeBuffer(data));
|
|
43
56
|
});
|
|
44
57
|
}
|
|
45
58
|
});
|
|
@@ -110,11 +123,11 @@ class SshFs {
|
|
|
110
123
|
} else {
|
|
111
124
|
let out = Buffer.from("");
|
|
112
125
|
stream.on("end", () => {
|
|
113
|
-
resolve(
|
|
126
|
+
resolve(this.decodeBuffer(out));
|
|
114
127
|
}).on("data", (data) => {
|
|
115
128
|
out = Buffer.concat([out, data]);
|
|
116
129
|
}).stderr.on("data", (data) => {
|
|
117
|
-
reject(
|
|
130
|
+
reject(this.decodeBuffer(data));
|
|
118
131
|
});
|
|
119
132
|
}
|
|
120
133
|
});
|
|
@@ -179,7 +192,7 @@ class SshFs {
|
|
|
179
192
|
return this.listFiles(remotePath);
|
|
180
193
|
}
|
|
181
194
|
async mkdir(remotePath, options = {}) {
|
|
182
|
-
const cmd = options.mode ? `mkdir -m ${options.mode.toString(8)} -p "${remotePath}"` : `mkdir -p "${remotePath}"`;
|
|
195
|
+
const cmd = options.mode ? `mkdir -m ${this.maskMode(options.mode).toString(8)} -p "${remotePath}"` : `mkdir -p "${remotePath}"`;
|
|
183
196
|
return this.runCmd(cmd);
|
|
184
197
|
}
|
|
185
198
|
async stat(remotePath) {
|
|
@@ -274,7 +287,7 @@ class SshFs {
|
|
|
274
287
|
};
|
|
275
288
|
}
|
|
276
289
|
chmod(remotePath, mode) {
|
|
277
|
-
return this.runCmd(`chmod ${mode.toString(8)} "${remotePath}"`);
|
|
290
|
+
return this.runCmd(`chmod ${this.maskMode(mode).toString(8)} "${remotePath}"`);
|
|
278
291
|
}
|
|
279
292
|
rename(remotePath, remotePathNew) {
|
|
280
293
|
return this.runCmd(`mv "${remotePath}" "${remotePathNew}"`);
|
|
@@ -334,7 +347,7 @@ class SshFs {
|
|
|
334
347
|
}
|
|
335
348
|
}
|
|
336
349
|
if (mode) {
|
|
337
|
-
await this.runCmd(`chmod ${mode.toString(8)} "${remotePath}"`);
|
|
350
|
+
await this.runCmd(`chmod ${this.maskMode(mode).toString(8)} "${remotePath}"`);
|
|
338
351
|
}
|
|
339
352
|
}
|
|
340
353
|
}
|
package/dist/esm/transfer.d.ts
CHANGED
|
@@ -16,13 +16,25 @@ declare interface FileInfo {
|
|
|
16
16
|
group: number;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
declare interface IconvDecoder {
|
|
20
|
+
decode: (buf: Buffer, encoding: string) => string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
declare interface IconvDecoder_2 {
|
|
24
|
+
decode: (buf: Buffer, encoding: string) => string;
|
|
25
|
+
}
|
|
26
|
+
|
|
19
27
|
declare class SshFs {
|
|
20
28
|
private session;
|
|
21
29
|
private _chunkSize;
|
|
30
|
+
private iconv?;
|
|
31
|
+
private encoding;
|
|
22
32
|
getChunkSize(): Promise<number>;
|
|
23
|
-
constructor(session: Client,
|
|
33
|
+
constructor(session: Client, options?: SshFsOptions);
|
|
34
|
+
private decodeBuffer;
|
|
24
35
|
private detectChunkSize;
|
|
25
36
|
private getExecOpts;
|
|
37
|
+
private maskMode;
|
|
26
38
|
private getMonthIndex;
|
|
27
39
|
private runCmd;
|
|
28
40
|
private rmFolderCmd;
|
|
@@ -61,6 +73,8 @@ declare class SshFs {
|
|
|
61
73
|
}
|
|
62
74
|
|
|
63
75
|
declare interface SshFsOptions {
|
|
76
|
+
iconv?: IconvDecoder_2;
|
|
77
|
+
encoding?: string;
|
|
64
78
|
}
|
|
65
79
|
|
|
66
80
|
declare interface Stats {
|
|
@@ -103,6 +117,10 @@ export declare interface TransferOptions {
|
|
|
103
117
|
remotePath: string;
|
|
104
118
|
localPath: string;
|
|
105
119
|
chunkSize?: number;
|
|
120
|
+
iconv?: IconvDecoder & {
|
|
121
|
+
encode: (str: string, encoding: string) => Buffer;
|
|
122
|
+
};
|
|
123
|
+
encoding?: string;
|
|
106
124
|
onProgress?: (transferred: number, total: number) => void;
|
|
107
125
|
onData?: (count: number) => void;
|
|
108
126
|
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ssh2-scp",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.2.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": "npm run build && node --test test/ssh2-fs.test.mjs test/folder-transfer.test.mjs && node test/transfer.test.mjs",
|
|
9
|
+
"test": "npm run build && node --test test/ssh2-fs.test.mjs test/ssh-fs-unit.test.mjs test/encoding.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"
|
|
@@ -49,11 +49,13 @@
|
|
|
49
49
|
"LICENSE"
|
|
50
50
|
],
|
|
51
51
|
"devDependencies": {
|
|
52
|
+
"@electerm/ssh2": "^1.19.0",
|
|
53
|
+
"@types/iconv-lite": "^0.0.1",
|
|
52
54
|
"@types/node": "^20.10.0",
|
|
53
55
|
"@types/ssh2": "^1.11.0",
|
|
54
56
|
"@types/tar": "^6.1.0",
|
|
55
57
|
"dotenv": "^16.4.0",
|
|
56
|
-
"
|
|
58
|
+
"iconv-lite": "^0.7.2",
|
|
57
59
|
"tar": "^6.2.0",
|
|
58
60
|
"typescript": "^5.3.0",
|
|
59
61
|
"vite": "^5.0.0",
|
|
@@ -62,4 +64,4 @@
|
|
|
62
64
|
"engines": {
|
|
63
65
|
"node": ">=22.0.0"
|
|
64
66
|
}
|
|
65
|
-
}
|
|
67
|
+
}
|