dsh-remote-vs 0.11.1 → 0.13.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.en.md +2 -1
- package/README.md +2 -1
- package/lib/client.js +819 -125
- package/lib/index.js +550 -12
- package/lib/p2-router-fs.js +1 -1
- package/lib/types/archive.d.ts +30 -0
- package/lib/types/client/DownloadDock.d.ts +15 -0
- package/lib/types/client/download.d.ts +69 -0
- package/lib/types/client/downloads.d.ts +63 -0
- package/lib/types/client/i18n.d.ts +1 -1
- package/lib/types/client/listing-requests.d.ts +45 -0
- package/lib/types/pool.d.ts +106 -0
- package/lib/types/protocol.d.ts +14 -0
- package/lib/types/sftp.d.ts +73 -0
- package/lib/{worlds-a8nqW9mi.js → worlds-C6Xha1mQ.js} +520 -27
- package/package.json +1 -1
- package/src/archive.ts +60 -0
- package/src/client/DownloadDock.tsx +93 -0
- package/src/client/RemoteExplorer.tsx +168 -13
- package/src/client/RemoteRoot.tsx +2 -0
- package/src/client/SearchView.tsx +8 -0
- package/src/client/download.ts +212 -0
- package/src/client/downloads.ts +179 -0
- package/src/client/i18n.ts +28 -0
- package/src/client/listing-requests.ts +84 -0
- package/src/client/vsc/kit.tsx +30 -3
- package/src/client/vsc/vsc.css +124 -0
- package/src/pool.ts +377 -17
- package/src/protocol.ts +492 -474
- package/src/routes.ts +221 -2
- package/src/sftp.ts +637 -30
|
@@ -16879,6 +16879,58 @@ const LIST_ENTRY_CAP = 2e4;
|
|
|
16879
16879
|
/** Largest single upload accepted (matches the client-side planner cap). */
|
|
16880
16880
|
const UPLOAD_MAX_BYTES = 2 * 1024 * 1024 * 1024;
|
|
16881
16881
|
/**
|
|
16882
|
+
* How long one download stream may make no progress before it is failed.
|
|
16883
|
+
*
|
|
16884
|
+
* A download has no wall-clock budget for the same reason an upload does not: a
|
|
16885
|
+
* big file on a slow link is healthy for minutes. What must never happen is a
|
|
16886
|
+
* silently dead stream pinning the SFTP session (and its channel slot) forever,
|
|
16887
|
+
* so progress is the bound — any chunk resets this clock.
|
|
16888
|
+
*/
|
|
16889
|
+
const DOWNLOAD_IDLE_MS = 6e4;
|
|
16890
|
+
/** Upload progress bound (no body bytes and no write acknowledgements). */
|
|
16891
|
+
const UPLOAD_IDLE_MS = 6e4;
|
|
16892
|
+
/**
|
|
16893
|
+
* Parallel-transfer shape, shared by download and upload.
|
|
16894
|
+
*
|
|
16895
|
+
* A single `createReadStream`/`createWriteStream` keeps exactly ONE SFTP request
|
|
16896
|
+
* in flight per round trip; measured against a real host that cost about 8x
|
|
16897
|
+
* (4.4 MiB/s single stream vs 35.4 MiB/s for ssh2's pipelined fastGet on the
|
|
16898
|
+
* same link). 256 KiB chunks with 32 in-flight requests (8 MiB of app-side
|
|
16899
|
+
* buffers) are the same tuning ssh2's fastGet/fastPut use and bound the
|
|
16900
|
+
* per-transfer memory; on top of that ssh2 holds up to one 2 MiB channel window
|
|
16901
|
+
* per channel in flight, so a two-channel download peaks around 12 MiB.
|
|
16902
|
+
*/
|
|
16903
|
+
const TRANSFER_CHUNK_BYTES = 256 * 1024;
|
|
16904
|
+
/**
|
|
16905
|
+
* Deadline for a download's one-shot metadata calls (`lstat`, `open`, `fstat`).
|
|
16906
|
+
*
|
|
16907
|
+
* Nothing is streaming while they run, so there is no progress signal to watch
|
|
16908
|
+
* and no idle watchdog can help: a server that never answers one would pin the
|
|
16909
|
+
* transfer, its channels and its slots forever. The streaming phase stays
|
|
16910
|
+
* progress-bounded (`DOWNLOAD_IDLE_MS`) because a big file on a slow link is
|
|
16911
|
+
* healthy for minutes.
|
|
16912
|
+
*/
|
|
16913
|
+
const DOWNLOAD_META_TIMEOUT_MS = 6e4;
|
|
16914
|
+
/**
|
|
16915
|
+
* Deadline for the OPTIONAL second channel's handle open.
|
|
16916
|
+
*
|
|
16917
|
+
* This runs after the response headers are sent, so a hang here is worse than a
|
|
16918
|
+
* missing second channel: the client is already reading a 200 body. Short on
|
|
16919
|
+
* purpose — the fallback (one channel) is always correct.
|
|
16920
|
+
*/
|
|
16921
|
+
const EXTRA_CHANNEL_TIMEOUT_MS = 1e4;
|
|
16922
|
+
/**
|
|
16923
|
+
* How long a handle close may take before it is abandoned.
|
|
16924
|
+
*
|
|
16925
|
+
* ssh2 only settles the CLOSE requests that were pending when a channel closed:
|
|
16926
|
+
* a close issued on an already-dead channel never calls back (the payload is
|
|
16927
|
+
* dropped or written to a stale recipient). Awaiting that forever would hang the
|
|
16928
|
+
* transfer AFTER its body was delivered and pin both the channels and their
|
|
16929
|
+
* slots — the session teardown that follows settles the request anyway, so the
|
|
16930
|
+
* wait is abandoned and the pool's `finally` proceeds.
|
|
16931
|
+
*/
|
|
16932
|
+
const HANDLE_CLOSE_TIMEOUT_MS = 2e3;
|
|
16933
|
+
/**
|
|
16882
16934
|
* Cap for both per-path memory sets below (levels confirmed to exist, listings
|
|
16883
16935
|
* already reported as truncated). Each only ever saves round trips or a repeat
|
|
16884
16936
|
* log line, so a small window is enough and a miss costs nothing but the work
|
|
@@ -16899,6 +16951,38 @@ function remotePath(path) {
|
|
|
16899
16951
|
if (!normalized.startsWith("/")) throw new Error("remote path must be absolute");
|
|
16900
16952
|
return normalized;
|
|
16901
16953
|
}
|
|
16954
|
+
/**
|
|
16955
|
+
* Reject when `promise` has not settled within `ms`.
|
|
16956
|
+
*
|
|
16957
|
+
* The download's one-shot calls (`lstat`, `open`, `fstat`, an extra channel's
|
|
16958
|
+
* handle) have no progress signal, so this is the only thing between a stalled
|
|
16959
|
+
* server and a response — and two SFTP channels — pinned forever.
|
|
16960
|
+
*/
|
|
16961
|
+
function bounded(promise, ms, message) {
|
|
16962
|
+
return new Promise((resolve, reject) => {
|
|
16963
|
+
const timer = setTimeout(() => reject(new Error(message)), ms);
|
|
16964
|
+
timer.unref?.();
|
|
16965
|
+
promise.then((value) => {
|
|
16966
|
+
clearTimeout(timer);
|
|
16967
|
+
resolve(value);
|
|
16968
|
+
}, (error) => {
|
|
16969
|
+
clearTimeout(timer);
|
|
16970
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
16971
|
+
});
|
|
16972
|
+
});
|
|
16973
|
+
}
|
|
16974
|
+
/**
|
|
16975
|
+
* `Content-Disposition` for one attachment (RFC 6266 + RFC 5987).
|
|
16976
|
+
*
|
|
16977
|
+
* Receivers prefer `filename*` when both forms are present (RFC 6266 §4.3),
|
|
16978
|
+
* which is why a non-ASCII remote name survives the round trip; the quoted
|
|
16979
|
+
* ASCII copy is the fallback for clients that do not understand the extended
|
|
16980
|
+
* form. Quotes and backslashes are stripped from that fallback so they cannot
|
|
16981
|
+
* break the header.
|
|
16982
|
+
*/
|
|
16983
|
+
function attachmentHeader(fileName) {
|
|
16984
|
+
return `attachment; filename="${fileName.replace(/[^\u0020-\u007e]/g, "_").replace(/["\\]/g, "_")}"; filename*=UTF-8''${encodeURIComponent(fileName).replace(/['()*]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`)}`;
|
|
16985
|
+
}
|
|
16902
16986
|
var RemoteFiles = class {
|
|
16903
16987
|
pool;
|
|
16904
16988
|
/**
|
|
@@ -16913,7 +16997,7 @@ var RemoteFiles = class {
|
|
|
16913
16997
|
}
|
|
16914
16998
|
async list(alias, dir) {
|
|
16915
16999
|
const path = remotePath(dir);
|
|
16916
|
-
return this.pool.
|
|
17000
|
+
return this.pool.withBrowseSftp(alias, async (sftp) => {
|
|
16917
17001
|
let rows;
|
|
16918
17002
|
try {
|
|
16919
17003
|
rows = await readdirAsync(sftp, path);
|
|
@@ -16934,14 +17018,14 @@ var RemoteFiles = class {
|
|
|
16934
17018
|
}
|
|
16935
17019
|
async stat(alias, file) {
|
|
16936
17020
|
const path = remotePath(file);
|
|
16937
|
-
return this.pool.
|
|
17021
|
+
return this.pool.withBrowseSftp(alias, async (sftp) => {
|
|
16938
17022
|
const attrs = await lstatAsync(sftp, path);
|
|
16939
17023
|
return toEntry(posix.dirname(path), posix.basename(path), attrs);
|
|
16940
17024
|
});
|
|
16941
17025
|
}
|
|
16942
17026
|
async read(alias, file) {
|
|
16943
17027
|
const path = remotePath(file);
|
|
16944
|
-
return this.pool.
|
|
17028
|
+
return this.pool.withBrowseSftp(alias, async (sftp) => {
|
|
16945
17029
|
const attrs = await lstatAsync(sftp, path).catch(async (error) => {
|
|
16946
17030
|
throw await explainSftpFailure(sftp, path, "file", error) ?? error;
|
|
16947
17031
|
});
|
|
@@ -17021,6 +17105,70 @@ var RemoteFiles = class {
|
|
|
17021
17105
|
await unlinkAsync(sftp, partial).catch(() => void 0);
|
|
17022
17106
|
throw await explainSftpFailure(sftp, path, "file", error) ?? error;
|
|
17023
17107
|
}
|
|
17108
|
+
}, { timeoutMs: 0 });
|
|
17109
|
+
}
|
|
17110
|
+
/**
|
|
17111
|
+
* Stream one remote file into an HTTP response (download to the local machine).
|
|
17112
|
+
*
|
|
17113
|
+
* Streaming, not `read()`: `read()` caps at 8 MiB because it answers a JSON
|
|
17114
|
+
* editor read, while a download is whatever the user pointed at — a log, a
|
|
17115
|
+
* binary, an artifact. The file is opened through one SFTP handle before the
|
|
17116
|
+
* headers are written: symlinks are followed (lstat's size is the link
|
|
17117
|
+
* target's string length, not the file's), directories are refused, and the
|
|
17118
|
+
* content length comes from the same handle the body is read through. The
|
|
17119
|
+
* session has no wall-clock deadline (see `DOWNLOAD_IDLE_MS`); the caller's
|
|
17120
|
+
* response close and stream errors are both handled, so a cancelled download
|
|
17121
|
+
* never leaves the SFTP session or its channel slot pinned.
|
|
17122
|
+
*
|
|
17123
|
+
* @param alias - host alias.
|
|
17124
|
+
* @param file - absolute remote source path.
|
|
17125
|
+
* @param res - HTTP response to write the file into.
|
|
17126
|
+
*/
|
|
17127
|
+
async download(alias, file, res) {
|
|
17128
|
+
const path = remotePath(file);
|
|
17129
|
+
await this.pool.withTransferSftp(alias, async (sftp, addChannel) => {
|
|
17130
|
+
if (isDir((await bounded(lstatAsync(sftp, path).catch(async (error) => {
|
|
17131
|
+
throw await explainSftpFailure(sftp, path, "file", error) ?? error;
|
|
17132
|
+
}), DOWNLOAD_META_TIMEOUT_MS, `listing ${path} timed out after ${DOWNLOAD_META_TIMEOUT_MS / 1e3}s`)).mode)) throw new Error(`not a file: ${path} is a directory`);
|
|
17133
|
+
const handle = await bounded(openAsync(sftp, path).catch(async (error) => {
|
|
17134
|
+
throw await explainFollowFailure(sftp, path, error) ?? error;
|
|
17135
|
+
}), DOWNLOAD_META_TIMEOUT_MS, `opening ${path} timed out after ${DOWNLOAD_META_TIMEOUT_MS / 1e3}s`);
|
|
17136
|
+
const channels = [{
|
|
17137
|
+
sftp,
|
|
17138
|
+
handle
|
|
17139
|
+
}];
|
|
17140
|
+
try {
|
|
17141
|
+
const attrs = await bounded(fstatAsync(sftp, handle), DOWNLOAD_META_TIMEOUT_MS, `stat of ${path} timed out after ${DOWNLOAD_META_TIMEOUT_MS / 1e3}s`);
|
|
17142
|
+
if (isDir(attrs.mode)) throw new Error(`not a file: ${path} is a directory`);
|
|
17143
|
+
if (res.destroyed) return;
|
|
17144
|
+
res.writeHead(200, {
|
|
17145
|
+
"content-type": "application/octet-stream",
|
|
17146
|
+
"content-length": String(attrs.size),
|
|
17147
|
+
"content-disposition": attachmentHeader(posix.basename(path)),
|
|
17148
|
+
"cache-control": "no-store",
|
|
17149
|
+
"x-content-type-options": "nosniff"
|
|
17150
|
+
});
|
|
17151
|
+
if (attrs.size <= 0) {
|
|
17152
|
+
res.end();
|
|
17153
|
+
return;
|
|
17154
|
+
}
|
|
17155
|
+
if (attrs.size >= 4194304) for (let extra = 1; extra < 2; extra += 1) {
|
|
17156
|
+
const channel = await addChannel();
|
|
17157
|
+
if (channel === void 0) break;
|
|
17158
|
+
try {
|
|
17159
|
+
const extraHandle = await bounded(openAsync(channel, path), EXTRA_CHANNEL_TIMEOUT_MS, `opening ${path} on the second channel timed out after ${EXTRA_CHANNEL_TIMEOUT_MS / 1e3}s`);
|
|
17160
|
+
channels.push({
|
|
17161
|
+
sftp: channel,
|
|
17162
|
+
handle: extraHandle
|
|
17163
|
+
});
|
|
17164
|
+
} catch {
|
|
17165
|
+
break;
|
|
17166
|
+
}
|
|
17167
|
+
}
|
|
17168
|
+
await pipeChannelsToResponse(channels, attrs.size, res);
|
|
17169
|
+
} finally {
|
|
17170
|
+
for (const channel of channels) await closeQuietly(channel.sftp, channel.handle);
|
|
17171
|
+
}
|
|
17024
17172
|
});
|
|
17025
17173
|
}
|
|
17026
17174
|
async mkdir(alias, dir) {
|
|
@@ -17228,6 +17376,253 @@ function lstatAsync(sftp, path) {
|
|
|
17228
17376
|
});
|
|
17229
17377
|
});
|
|
17230
17378
|
}
|
|
17379
|
+
function statAsync(sftp, path) {
|
|
17380
|
+
return new Promise((resolve, reject) => {
|
|
17381
|
+
sftp.stat(path, (error, attrs) => {
|
|
17382
|
+
if (error !== void 0 && error !== null) reject(error);
|
|
17383
|
+
else resolve(attrs);
|
|
17384
|
+
});
|
|
17385
|
+
});
|
|
17386
|
+
}
|
|
17387
|
+
function openAsync(sftp, path, flags = "r") {
|
|
17388
|
+
return new Promise((resolve, reject) => {
|
|
17389
|
+
sftp.open(path, flags, (error, handle) => {
|
|
17390
|
+
if (error !== void 0 && error !== null) reject(error);
|
|
17391
|
+
else resolve(handle);
|
|
17392
|
+
});
|
|
17393
|
+
});
|
|
17394
|
+
}
|
|
17395
|
+
/** Read exactly `buffer.length` bytes at `position` (SFTP may answer short reads). */
|
|
17396
|
+
async function readHandleFully(sftp, handle, buffer, position) {
|
|
17397
|
+
let filled = 0;
|
|
17398
|
+
while (filled < buffer.length) {
|
|
17399
|
+
const read = await new Promise((resolve, reject) => {
|
|
17400
|
+
sftp.read(handle, buffer.subarray(filled), 0, buffer.length - filled, position + filled, (error, bytesRead) => {
|
|
17401
|
+
if (error !== void 0 && error !== null) reject(error);
|
|
17402
|
+
else resolve(bytesRead);
|
|
17403
|
+
});
|
|
17404
|
+
});
|
|
17405
|
+
if (read <= 0) throw new Error(`short read at ${position + filled}`);
|
|
17406
|
+
filled += read;
|
|
17407
|
+
}
|
|
17408
|
+
}
|
|
17409
|
+
/**
|
|
17410
|
+
* Copy an open remote file into an HTTP response with a bounded parallel read
|
|
17411
|
+
* window spread over one or more SFTP channels.
|
|
17412
|
+
*
|
|
17413
|
+
* `res` must already carry its headers (including content-length). Workers pread
|
|
17414
|
+
* 256 KiB chunks out of order into a bounded map, and one ordered writer copies
|
|
17415
|
+
* them to the socket, so the response bytes stay ordered while up to
|
|
17416
|
+
* `TRANSFER_CONCURRENCY` READs stay in flight. The chunk index picks the channel
|
|
17417
|
+
* (`index % channels.length`) so the in-flight bytes are spread across the SSH
|
|
17418
|
+
* channel windows instead of queueing behind one 2 MiB window. A closed response
|
|
17419
|
+
* (user cancel) resolves quietly — the route treats that as a cancellation, not
|
|
17420
|
+
* an error — and a no-progress watchdog fails a silently dead transfer.
|
|
17421
|
+
*/
|
|
17422
|
+
async function pipeChannelsToResponse(channels, size, res) {
|
|
17423
|
+
const chunkBytes = TRANSFER_CHUNK_BYTES;
|
|
17424
|
+
const totalChunks = Math.ceil(size / chunkBytes);
|
|
17425
|
+
const windowChunks = Math.min(32, totalChunks);
|
|
17426
|
+
const ready = /* @__PURE__ */ new Map();
|
|
17427
|
+
const chunkWaiters = /* @__PURE__ */ new Map();
|
|
17428
|
+
const slotWaiters = [];
|
|
17429
|
+
let slots = windowChunks;
|
|
17430
|
+
let nextRead = 0;
|
|
17431
|
+
let cancelled = false;
|
|
17432
|
+
let failure;
|
|
17433
|
+
let lastActivity = Date.now();
|
|
17434
|
+
const wakeSlots = () => {
|
|
17435
|
+
for (const wake of slotWaiters.splice(0)) wake();
|
|
17436
|
+
};
|
|
17437
|
+
const wakeChunks = () => {
|
|
17438
|
+
for (const wake of chunkWaiters.values()) wake();
|
|
17439
|
+
chunkWaiters.clear();
|
|
17440
|
+
};
|
|
17441
|
+
const signalChunk = (index) => {
|
|
17442
|
+
const wake = chunkWaiters.get(index);
|
|
17443
|
+
if (wake !== void 0) {
|
|
17444
|
+
chunkWaiters.delete(index);
|
|
17445
|
+
wake();
|
|
17446
|
+
}
|
|
17447
|
+
};
|
|
17448
|
+
const fail = (error) => {
|
|
17449
|
+
if (failure !== void 0) return;
|
|
17450
|
+
failure = error instanceof Error ? error : new Error(String(error));
|
|
17451
|
+
wakeSlots();
|
|
17452
|
+
wakeChunks();
|
|
17453
|
+
};
|
|
17454
|
+
const acquire = async () => {
|
|
17455
|
+
while (slots <= 0) await new Promise((resolve) => slotWaiters.push(resolve));
|
|
17456
|
+
slots -= 1;
|
|
17457
|
+
};
|
|
17458
|
+
const release = () => {
|
|
17459
|
+
slots += 1;
|
|
17460
|
+
const wake = slotWaiters.shift();
|
|
17461
|
+
if (wake !== void 0) wake();
|
|
17462
|
+
};
|
|
17463
|
+
/**
|
|
17464
|
+
* Channels whose READ failed. Their chunks are re-read on the first channel:
|
|
17465
|
+
* the extra channel is an optimisation, so losing it must degrade the transfer
|
|
17466
|
+
* to the single-channel shape it would have used anyway instead of failing a
|
|
17467
|
+
* download the first channel can still finish. (A channel that STALLS rather
|
|
17468
|
+
* than errors is not recoverable here: no bytes means the ordered writer has
|
|
17469
|
+
* nothing to reorder, and the no-progress watchdog ends the transfer.)
|
|
17470
|
+
*/
|
|
17471
|
+
const failedChannels = /* @__PURE__ */ new Set();
|
|
17472
|
+
const primary = channels[0];
|
|
17473
|
+
const channelFor = (index) => {
|
|
17474
|
+
const slot = channels.length > 1 ? index % channels.length : 0;
|
|
17475
|
+
if (slot === 0 || failedChannels.has(slot)) return {
|
|
17476
|
+
...primary,
|
|
17477
|
+
slot
|
|
17478
|
+
};
|
|
17479
|
+
return {
|
|
17480
|
+
...channels[slot],
|
|
17481
|
+
slot
|
|
17482
|
+
};
|
|
17483
|
+
};
|
|
17484
|
+
const worker = async () => {
|
|
17485
|
+
for (;;) {
|
|
17486
|
+
if (cancelled || failure !== void 0) return;
|
|
17487
|
+
await acquire();
|
|
17488
|
+
if (cancelled || failure !== void 0) {
|
|
17489
|
+
release();
|
|
17490
|
+
return;
|
|
17491
|
+
}
|
|
17492
|
+
const index = nextRead++;
|
|
17493
|
+
if (index >= totalChunks) {
|
|
17494
|
+
release();
|
|
17495
|
+
return;
|
|
17496
|
+
}
|
|
17497
|
+
const start = index * chunkBytes;
|
|
17498
|
+
const length = Math.min(chunkBytes, size - start);
|
|
17499
|
+
const buffer = Buffer.allocUnsafe(length);
|
|
17500
|
+
try {
|
|
17501
|
+
const channel = channelFor(index);
|
|
17502
|
+
try {
|
|
17503
|
+
await readHandleFully(channel.sftp, channel.handle, buffer, start);
|
|
17504
|
+
} catch (error) {
|
|
17505
|
+
if (channel.slot === 0 || cancelled || failure !== void 0) throw error;
|
|
17506
|
+
failedChannels.add(channel.slot);
|
|
17507
|
+
await readHandleFully(primary.sftp, primary.handle, buffer, start);
|
|
17508
|
+
}
|
|
17509
|
+
} catch (error) {
|
|
17510
|
+
release();
|
|
17511
|
+
fail(error);
|
|
17512
|
+
return;
|
|
17513
|
+
}
|
|
17514
|
+
lastActivity = Date.now();
|
|
17515
|
+
ready.set(index, buffer);
|
|
17516
|
+
signalChunk(index);
|
|
17517
|
+
}
|
|
17518
|
+
};
|
|
17519
|
+
const waitChunk = (index) => new Promise((resolve) => {
|
|
17520
|
+
if (ready.has(index) || failure !== void 0) resolve();
|
|
17521
|
+
else chunkWaiters.set(index, resolve);
|
|
17522
|
+
});
|
|
17523
|
+
const waitDrain = () => new Promise((resolve) => {
|
|
17524
|
+
if (cancelled || failure !== void 0) {
|
|
17525
|
+
resolve();
|
|
17526
|
+
return;
|
|
17527
|
+
}
|
|
17528
|
+
res.once("drain", resolve);
|
|
17529
|
+
});
|
|
17530
|
+
const onResponseClose = () => {
|
|
17531
|
+
if (res.writableEnded) return;
|
|
17532
|
+
cancelled = true;
|
|
17533
|
+
wakeSlots();
|
|
17534
|
+
wakeChunks();
|
|
17535
|
+
};
|
|
17536
|
+
res.on("close", onResponseClose);
|
|
17537
|
+
const idle = setInterval(() => {
|
|
17538
|
+
if (cancelled || failure !== void 0) return;
|
|
17539
|
+
if (Date.now() - lastActivity < 6e4) return;
|
|
17540
|
+
if (res.writableLength > 0) {
|
|
17541
|
+
lastActivity = Date.now();
|
|
17542
|
+
return;
|
|
17543
|
+
}
|
|
17544
|
+
fail(/* @__PURE__ */ new Error(`download stalled: no bytes for ${DOWNLOAD_IDLE_MS / 1e3}s`));
|
|
17545
|
+
}, Math.min(5e3, DOWNLOAD_IDLE_MS));
|
|
17546
|
+
idle.unref?.();
|
|
17547
|
+
const workers = Array.from({ length: windowChunks }, () => worker());
|
|
17548
|
+
try {
|
|
17549
|
+
for (let index = 0; index < totalChunks; index += 1) {
|
|
17550
|
+
if (cancelled) break;
|
|
17551
|
+
if (failure !== void 0) break;
|
|
17552
|
+
if (!ready.has(index)) await waitChunk(index);
|
|
17553
|
+
if (cancelled) break;
|
|
17554
|
+
if (failure !== void 0) break;
|
|
17555
|
+
const buffer = ready.get(index);
|
|
17556
|
+
if (buffer === void 0) continue;
|
|
17557
|
+
ready.delete(index);
|
|
17558
|
+
if (!res.write(buffer)) await waitDrain();
|
|
17559
|
+
release();
|
|
17560
|
+
lastActivity = Date.now();
|
|
17561
|
+
}
|
|
17562
|
+
} finally {
|
|
17563
|
+
clearInterval(idle);
|
|
17564
|
+
res.removeListener("close", onResponseClose);
|
|
17565
|
+
const drained = Promise.all(workers.map((workerPromise) => workerPromise.catch(() => void 0)));
|
|
17566
|
+
if (failure !== void 0 || cancelled) {
|
|
17567
|
+
cancelled = true;
|
|
17568
|
+
wakeSlots();
|
|
17569
|
+
wakeChunks();
|
|
17570
|
+
await Promise.race([drained, new Promise((resolve) => setTimeout(resolve, 250))]);
|
|
17571
|
+
} else await drained;
|
|
17572
|
+
}
|
|
17573
|
+
if (failure !== void 0) {
|
|
17574
|
+
res.destroy(failure);
|
|
17575
|
+
throw failure;
|
|
17576
|
+
}
|
|
17577
|
+
if (!cancelled) res.end();
|
|
17578
|
+
}
|
|
17579
|
+
function fstatAsync(sftp, handle) {
|
|
17580
|
+
return new Promise((resolve, reject) => {
|
|
17581
|
+
sftp.fstat(handle, (error, attrs) => {
|
|
17582
|
+
if (error !== void 0 && error !== null) reject(error);
|
|
17583
|
+
else resolve(attrs);
|
|
17584
|
+
});
|
|
17585
|
+
});
|
|
17586
|
+
}
|
|
17587
|
+
function closeAsync(sftp, handle) {
|
|
17588
|
+
return new Promise((resolve) => {
|
|
17589
|
+
try {
|
|
17590
|
+
sftp.close(handle, () => resolve());
|
|
17591
|
+
} catch {
|
|
17592
|
+
resolve();
|
|
17593
|
+
}
|
|
17594
|
+
});
|
|
17595
|
+
}
|
|
17596
|
+
/**
|
|
17597
|
+
* Close one handle, but never wait forever for a channel that is already gone.
|
|
17598
|
+
*
|
|
17599
|
+
* A close on a dead channel never calls back (see HANDLE_CLOSE_TIMEOUT_MS); the
|
|
17600
|
+
* session teardown that follows settles it, so abandoning the wait is safe and
|
|
17601
|
+
* keeps a delivered download (or a finished upload) from pinning its channels.
|
|
17602
|
+
*/
|
|
17603
|
+
function closeQuietly(sftp, handle) {
|
|
17604
|
+
const abandoned = new Promise((resolve) => {
|
|
17605
|
+
setTimeout(resolve, HANDLE_CLOSE_TIMEOUT_MS).unref?.();
|
|
17606
|
+
});
|
|
17607
|
+
return Promise.race([closeAsync(sftp, handle), abandoned]);
|
|
17608
|
+
}
|
|
17609
|
+
/**
|
|
17610
|
+
* Explain an `open` failure after lstat already saw a symlink: lstat reports
|
|
17611
|
+
* the LINK's type, so "the thing at the end of the link is a directory" only
|
|
17612
|
+
* shows up after the follow. `stat` follows the link and answers the question
|
|
17613
|
+
* a raw SFTP status never does.
|
|
17614
|
+
*/
|
|
17615
|
+
async function explainFollowFailure(sftp, path, cause) {
|
|
17616
|
+
const code = cause?.code;
|
|
17617
|
+
if (code !== SFTP_NO_SUCH_FILE && code !== SFTP_FAILURE) return void 0;
|
|
17618
|
+
let attrs;
|
|
17619
|
+
try {
|
|
17620
|
+
attrs = await statAsync(sftp, path);
|
|
17621
|
+
} catch (error) {
|
|
17622
|
+
return error?.code === SFTP_NO_SUCH_FILE ? /* @__PURE__ */ new Error(`no such file: ${path}`) : void 0;
|
|
17623
|
+
}
|
|
17624
|
+
if (isDir(attrs.mode)) return /* @__PURE__ */ new Error(`not a file: ${path} is a directory`);
|
|
17625
|
+
}
|
|
17231
17626
|
/**
|
|
17232
17627
|
* Read one window of a file.
|
|
17233
17628
|
*
|
|
@@ -17283,11 +17678,18 @@ function writeFileAsync(sftp, path, data) {
|
|
|
17283
17678
|
});
|
|
17284
17679
|
}
|
|
17285
17680
|
/**
|
|
17286
|
-
*
|
|
17681
|
+
* Stream one readable request body into a new remote file with a bounded
|
|
17682
|
+
* parallel write window.
|
|
17683
|
+
*
|
|
17684
|
+
* The mirror of `pipeChannelsToResponse`: incoming bytes are cut into
|
|
17685
|
+
* `TRANSFER_CHUNK_BYTES` pieces and up to `TRANSFER_CONCURRENCY` WRITEs are in
|
|
17686
|
+
* flight at their own offsets. A single `createWriteStream` keeps one WRITE per
|
|
17687
|
+
* round trip and was far slower on the same link (~1 MiB/s, so a 64 MiB body
|
|
17688
|
+
* could not even finish inside the old 60 s session deadline).
|
|
17287
17689
|
*
|
|
17288
|
-
*
|
|
17289
|
-
*
|
|
17290
|
-
*
|
|
17690
|
+
* The producer is paused while more than two windows of chunks are waiting, so
|
|
17691
|
+
* Host memory stays bounded. Aborted clients (no `end`) fail instead of hanging:
|
|
17692
|
+
* a stuck upload used to pin the SFTP session and block every later operation.
|
|
17291
17693
|
*
|
|
17292
17694
|
* @param sftp - live SFTP session.
|
|
17293
17695
|
* @param path - absolute remote destination.
|
|
@@ -17297,39 +17699,130 @@ function writeFileAsync(sftp, path, data) {
|
|
|
17297
17699
|
*/
|
|
17298
17700
|
function streamIntoFile(sftp, path, source, maxBytes) {
|
|
17299
17701
|
return new Promise((resolve, reject) => {
|
|
17300
|
-
const
|
|
17301
|
-
|
|
17702
|
+
const chunkBytes = TRANSFER_CHUNK_BYTES;
|
|
17703
|
+
const windowChunks = 32;
|
|
17704
|
+
const chunks = /* @__PURE__ */ new Map();
|
|
17705
|
+
let nextChunk = 0;
|
|
17706
|
+
let nextWrite = 0;
|
|
17707
|
+
let pendingWrites = 0;
|
|
17708
|
+
let received = 0;
|
|
17709
|
+
let carry;
|
|
17710
|
+
let handle;
|
|
17711
|
+
let ended = false;
|
|
17302
17712
|
let settled = false;
|
|
17303
|
-
|
|
17713
|
+
let lastActivity = Date.now();
|
|
17714
|
+
let idle;
|
|
17715
|
+
const finish = (error) => {
|
|
17304
17716
|
if (settled) return;
|
|
17305
17717
|
settled = true;
|
|
17718
|
+
if (idle !== void 0) clearInterval(idle);
|
|
17306
17719
|
try {
|
|
17307
|
-
|
|
17720
|
+
source.pause();
|
|
17308
17721
|
} catch {}
|
|
17309
|
-
|
|
17722
|
+
if (error === void 0) {
|
|
17723
|
+
resolve(received);
|
|
17724
|
+
return;
|
|
17725
|
+
}
|
|
17726
|
+
try {
|
|
17727
|
+
source.destroy?.(error);
|
|
17728
|
+
} catch {}
|
|
17729
|
+
if (handle !== void 0) {
|
|
17730
|
+
const current = handle;
|
|
17731
|
+
handle = void 0;
|
|
17732
|
+
closeQuietly(sftp, current);
|
|
17733
|
+
}
|
|
17734
|
+
reject(error);
|
|
17735
|
+
};
|
|
17736
|
+
const pump = () => {
|
|
17737
|
+
const current = handle;
|
|
17738
|
+
if (settled || current === void 0) return;
|
|
17739
|
+
while (pendingWrites < windowChunks && chunks.has(nextWrite)) {
|
|
17740
|
+
const index = nextWrite++;
|
|
17741
|
+
const buffer = chunks.get(index);
|
|
17742
|
+
if (buffer === void 0) break;
|
|
17743
|
+
chunks.delete(index);
|
|
17744
|
+
pendingWrites += 1;
|
|
17745
|
+
sftp.write(current, buffer, 0, buffer.length, index * chunkBytes, (error) => {
|
|
17746
|
+
pendingWrites -= 1;
|
|
17747
|
+
if (settled) return;
|
|
17748
|
+
if (error !== void 0 && error !== null) {
|
|
17749
|
+
finish(error instanceof Error ? error : new Error(String(error)));
|
|
17750
|
+
return;
|
|
17751
|
+
}
|
|
17752
|
+
lastActivity = Date.now();
|
|
17753
|
+
pump();
|
|
17754
|
+
maybeFinish();
|
|
17755
|
+
try {
|
|
17756
|
+
if (chunks.size < windowChunks) source.resume();
|
|
17757
|
+
} catch {}
|
|
17758
|
+
});
|
|
17759
|
+
}
|
|
17760
|
+
};
|
|
17761
|
+
const maybeFinish = () => {
|
|
17762
|
+
if (settled || !ended) return;
|
|
17763
|
+
if (pendingWrites > 0 || chunks.size > 0) return;
|
|
17764
|
+
const current = handle;
|
|
17765
|
+
handle = void 0;
|
|
17766
|
+
if (current === void 0) {
|
|
17767
|
+
finish();
|
|
17768
|
+
return;
|
|
17769
|
+
}
|
|
17770
|
+
closeQuietly(sftp, current).then(() => finish(), () => finish());
|
|
17771
|
+
};
|
|
17772
|
+
const enqueue = (buffer) => {
|
|
17773
|
+
chunks.set(nextChunk, buffer);
|
|
17774
|
+
nextChunk += 1;
|
|
17775
|
+
try {
|
|
17776
|
+
if (chunks.size + pendingWrites >= windowChunks * 2) source.pause();
|
|
17777
|
+
} catch {}
|
|
17778
|
+
pump();
|
|
17779
|
+
};
|
|
17780
|
+
const pushData = (chunk) => {
|
|
17781
|
+
const data = carry === void 0 ? chunk : Buffer.concat([carry, chunk]);
|
|
17782
|
+
let offset = 0;
|
|
17783
|
+
while (data.length - offset >= chunkBytes) {
|
|
17784
|
+
enqueue(Buffer.from(data.subarray(offset, offset + chunkBytes)));
|
|
17785
|
+
offset += chunkBytes;
|
|
17786
|
+
}
|
|
17787
|
+
carry = offset < data.length ? Buffer.from(data.subarray(offset)) : void 0;
|
|
17310
17788
|
};
|
|
17311
17789
|
source.on("data", (chunk) => {
|
|
17312
|
-
|
|
17313
|
-
|
|
17314
|
-
|
|
17315
|
-
|
|
17316
|
-
|
|
17317
|
-
fail(/* @__PURE__ */ new Error(`upload exceeds the ${formatBytes(maxBytes)} limit`));
|
|
17790
|
+
if (settled) return;
|
|
17791
|
+
received += chunk.length;
|
|
17792
|
+
if (received > maxBytes) {
|
|
17793
|
+
finish(/* @__PURE__ */ new Error(`upload exceeds the ${formatBytes(maxBytes)} limit`));
|
|
17794
|
+
return;
|
|
17318
17795
|
}
|
|
17796
|
+
lastActivity = Date.now();
|
|
17797
|
+
pushData(chunk);
|
|
17798
|
+
});
|
|
17799
|
+
source.on("end", () => {
|
|
17800
|
+
if (settled || ended) return;
|
|
17801
|
+
ended = true;
|
|
17802
|
+
if (carry !== void 0) {
|
|
17803
|
+
const tail = carry;
|
|
17804
|
+
carry = void 0;
|
|
17805
|
+
enqueue(tail);
|
|
17806
|
+
}
|
|
17807
|
+
maybeFinish();
|
|
17319
17808
|
});
|
|
17320
|
-
source.on("error",
|
|
17321
|
-
const aborted = () =>
|
|
17809
|
+
source.on("error", (error) => finish(error instanceof Error ? error : new Error(String(error))));
|
|
17810
|
+
const aborted = () => finish(/* @__PURE__ */ new Error("upload aborted by the client"));
|
|
17322
17811
|
source.on("aborted", aborted);
|
|
17323
17812
|
source.on("close", () => {
|
|
17324
17813
|
if (source.complete === false) aborted();
|
|
17325
17814
|
});
|
|
17326
|
-
|
|
17327
|
-
out.on("close", () => {
|
|
17815
|
+
idle = setInterval(() => {
|
|
17328
17816
|
if (settled) return;
|
|
17329
|
-
|
|
17330
|
-
|
|
17331
|
-
});
|
|
17332
|
-
|
|
17817
|
+
if (Date.now() - lastActivity < 6e4) return;
|
|
17818
|
+
finish(/* @__PURE__ */ new Error(`SFTP upload stalled: no progress for ${UPLOAD_IDLE_MS / 1e3}s`));
|
|
17819
|
+
}, Math.min(5e3, UPLOAD_IDLE_MS));
|
|
17820
|
+
idle.unref?.();
|
|
17821
|
+
openAsync(sftp, path, "w").then((opened) => {
|
|
17822
|
+
handle = opened;
|
|
17823
|
+
pump();
|
|
17824
|
+
maybeFinish();
|
|
17825
|
+
}, (error) => finish(error instanceof Error ? error : new Error(String(error))));
|
|
17333
17826
|
});
|
|
17334
17827
|
}
|
|
17335
17828
|
/** Human-facing size for an upload cap ("2 GiB", "16 KiB"). */
|
|
@@ -18821,4 +19314,4 @@ var RemoteWorlds = class {
|
|
|
18821
19314
|
}
|
|
18822
19315
|
};
|
|
18823
19316
|
//#endregion
|
|
18824
|
-
export { pushPipeline as a,
|
|
19317
|
+
export { pushPipeline as a, attachmentHeader as c, dshHome as d, ensurePrivateDir as f, probe as i, require_lib as l, storePath as m, isWindowsAbsolute as n, seedHelperCache as o, keysDir as p, helperAvailability as r, RemoteFiles as s, RemoteWorlds as t, aliasRoot as u };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-remote-vs",
|
|
3
3
|
"description": "VSCode-style remote development for DSH: SSH host manager (key/password), helper deploy pipeline with version check, remote file explorer + editor + terminal in dsh-better-sidebar.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.13.0",
|
|
5
5
|
"icon": "icon.svg",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"dsh",
|